Some checks failed
morphit-release / Build + publish release tarball (push) Has been cancelled
258 lines
12 KiB
JavaScript
258 lines
12 KiB
JavaScript
"use strict";
|
|
/**
|
|
* @file Blurt Nexus (A.k.a. bridge) helpers.
|
|
* @author BeBlurt <https://beblurt.com/@beblurt>
|
|
* @description adaptation from Johan Nordberg <code@johan-nordberg.com> Database API helpers
|
|
* @license
|
|
* Copyright (c) 2022 BeBlurt. All Rights Reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
* are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistribution of source code must retain the above copyright notice, this
|
|
* list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistribution in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
* You acknowledge that this software is not designed, licensed or intended for use
|
|
* in the design, construction, operation or maintenance of any military facility.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Nexus = void 0;
|
|
class Nexus {
|
|
constructor(client) {
|
|
this.client = client;
|
|
}
|
|
/**
|
|
* Convenience for calling Nexus/Bridge RPC methods directly.
|
|
*
|
|
* Backing JSON-RPC API namespace: `bridge`.
|
|
* Nexus responses are indexed Layer 2 views, not Layer 1 consensus objects.
|
|
*/
|
|
call(method, params) {
|
|
return this.client.call('bridge', method, params);
|
|
}
|
|
accountNotifications(accountOrOptions, min_score = 25, last_id = null, limit = 100) {
|
|
const params = typeof accountOrOptions === 'string'
|
|
? { account: accountOrOptions, min_score, last_id, limit }
|
|
: {
|
|
account: accountOrOptions.account,
|
|
min_score: accountOrOptions.min_score ?? 25,
|
|
last_id: accountOrOptions.last_id ?? null,
|
|
limit: accountOrOptions.limit ?? 100
|
|
};
|
|
return this.call('account_notifications', params);
|
|
}
|
|
getAccountPosts(sortOrOptions, account, start_author = '', start_permlink = '', limit = 20, observer = null) {
|
|
const params = typeof sortOrOptions === 'string'
|
|
? { sort: sortOrOptions, account, start_author, start_permlink, limit, observer }
|
|
: {
|
|
sort: sortOrOptions.sort,
|
|
account: sortOrOptions.account,
|
|
start_author: sortOrOptions.start_author ?? '',
|
|
start_permlink: sortOrOptions.start_permlink ?? '',
|
|
limit: sortOrOptions.limit ?? 20,
|
|
observer: sortOrOptions.observer ?? null
|
|
};
|
|
return this.call('get_account_posts', params);
|
|
}
|
|
/**
|
|
* Return the lightweight Nexus post header for a post, or `null` when missing.
|
|
* Backing JSON-RPC method: `bridge.get_post_header`.
|
|
*/
|
|
getPostHeader(author, permlink) {
|
|
return this.call('get_post_header', { author, permlink });
|
|
}
|
|
/**
|
|
* Normalize a condenser-style post object into the Nexus bridge post shape.
|
|
* Backing JSON-RPC method: `bridge.normalize_post`.
|
|
*/
|
|
normalizePost(post) {
|
|
return this.call('normalize_post', { post });
|
|
}
|
|
getCommunity(nameOrOptions, observer = null) {
|
|
const params = typeof nameOrOptions === 'string'
|
|
? { name: nameOrOptions, observer }
|
|
: { name: nameOrOptions.name, observer: nameOrOptions.observer ?? null };
|
|
return this.call('get_community', params);
|
|
}
|
|
/**
|
|
* For a community/account: returns role, title, subscribed state.
|
|
* @param name The community account concerned.
|
|
* @param account The account to fetch.
|
|
*/
|
|
getCommunityContext(name, account) {
|
|
return this.call('get_community_context', { name, account });
|
|
}
|
|
/**
|
|
* Gives a flattened discussion tree starting at given post. Modified `get_state` thread implementation.
|
|
* Returns an Object whose key is "author/permlink".
|
|
*/
|
|
getDiscussion(author, permlink) {
|
|
return this.call('get_discussion', { author, permlink });
|
|
}
|
|
/**
|
|
* Get payout stats for building treemap.
|
|
* @param limit Number of result, default: 250 [optional].
|
|
*/
|
|
getPayoutStats(limit = 250) {
|
|
return this.call('get_payout_stats', { limit });
|
|
}
|
|
getPost(authorOrOptions, permlink, observer = null) {
|
|
const params = typeof authorOrOptions === 'string'
|
|
? { author: authorOrOptions, permlink, observer }
|
|
: { author: authorOrOptions.author, permlink: authorOrOptions.permlink, observer: authorOrOptions.observer ?? null };
|
|
return this.call('get_post', params);
|
|
}
|
|
getProfile(accountOrOptions, observer = null) {
|
|
const params = typeof accountOrOptions === 'string'
|
|
? { account: accountOrOptions, observer }
|
|
: { account: accountOrOptions.account, observer: accountOrOptions.observer ?? null };
|
|
return this.call('get_profile', params);
|
|
}
|
|
getRankedPosts(sortOrOptions, start_author = '', start_permlink = '', limit = 20, tag = null, observer = null) {
|
|
const params = typeof sortOrOptions === 'string'
|
|
? { sort: sortOrOptions, start_author, start_permlink, limit, tag, observer }
|
|
: {
|
|
sort: sortOrOptions.sort,
|
|
start_author: sortOrOptions.start_author ?? '',
|
|
start_permlink: sortOrOptions.start_permlink ?? '',
|
|
limit: sortOrOptions.limit ?? 20,
|
|
tag: sortOrOptions.tag ?? null,
|
|
observer: sortOrOptions.observer ?? null
|
|
};
|
|
return this.call('get_ranked_posts', params);
|
|
}
|
|
/**
|
|
* Returns a lists of all communities `account` subscribes to, plus role and title in each.
|
|
* The content of a community `account` subscribes to is [community name, community title, role_id, title].
|
|
* @param account The account to fetch.
|
|
*/
|
|
listAllSubscriptions(account) {
|
|
return this.call('list_all_subscriptions', { account });
|
|
}
|
|
listCommunities(lastOrOptions = '', limit = 100, query = null, sort = 'rank', observer = null) {
|
|
const params = typeof lastOrOptions === 'object' && lastOrOptions !== null
|
|
? {
|
|
last: lastOrOptions.last ?? '',
|
|
limit: lastOrOptions.limit ?? 100,
|
|
query: lastOrOptions.query ?? null,
|
|
sort: lastOrOptions.sort ?? 'rank',
|
|
observer: lastOrOptions.observer ?? null
|
|
}
|
|
: { last: lastOrOptions, limit, query, sort, observer };
|
|
return this.call('list_communities', params);
|
|
}
|
|
/**
|
|
* Returns a list of community account-roles (anyone with non-guest status).
|
|
* The content of a community account-roles array is [community name, role_id, title].
|
|
* @param community Community category name (account).
|
|
* @param last Name of subscriber, paging mechanism [optional].
|
|
* @param limit limit Number of listed communities, default: 50 [optional].
|
|
*/
|
|
listCommunityRoles(community, last = '', limit = 50) {
|
|
return this.call('list_community_roles', { community, last, limit });
|
|
}
|
|
/**
|
|
* Returns community accounts with custom titles.
|
|
* Backing JSON-RPC method: `bridge.list_community_titles`.
|
|
*/
|
|
listCommunityTitles(community, last = '', limit = 50) {
|
|
return this.call('list_community_titles', { community, last, limit });
|
|
}
|
|
/**
|
|
* Returns top communities as [community name, community title].
|
|
* Backing JSON-RPC method: `bridge.list_top_communities`.
|
|
*/
|
|
listTopCommunities(limit = 25) {
|
|
return this.call('list_top_communities', { limit });
|
|
}
|
|
/**
|
|
* Returns a list of communities by new subscriber count.
|
|
* The content of a community array is [community name, community title].
|
|
* @param limit Number of listed communities, default: 25 [optional].
|
|
* @deprecated Use the correctly spelled `listPopCommunities` alias. This historical misspelling remains supported for backward compatibility.
|
|
*/
|
|
listPopComunities(limit = 25) {
|
|
return this.call('list_pop_communities', { limit });
|
|
}
|
|
/** Correctly spelled alias for `listPopComunities`. */
|
|
listPopCommunities(limit = 25) {
|
|
return this.listPopComunities(limit);
|
|
}
|
|
/**
|
|
* Returns a list of subscribers for a given community.
|
|
* The content of a subscriber array is [account, role_id, title, created_at].
|
|
* @param community Community category name (account).
|
|
* @param last Last account, paging mechanism [optional].
|
|
* @param limit Number of results, default: 100 [optional].
|
|
*/
|
|
listSubscribers(community, last = '', limit = 100) {
|
|
return this.call('list_subscribers', { community, last, limit });
|
|
}
|
|
/**
|
|
* Return top trending topics as [name, title] pairs.
|
|
* Backing JSON-RPC method: `bridge.get_trending_topics`.
|
|
*/
|
|
getTrendingTopics(limit = 10, observer = null) {
|
|
return this.call('get_trending_topics', { limit, observer });
|
|
}
|
|
postNotifications(authorOrOptions, permlink, min_score = 25, last_id = null, limit = 100) {
|
|
const params = typeof authorOrOptions === 'string'
|
|
? { author: authorOrOptions, permlink, min_score, last_id, limit }
|
|
: {
|
|
author: authorOrOptions.author,
|
|
permlink: authorOrOptions.permlink,
|
|
min_score: authorOrOptions.min_score ?? 25,
|
|
last_id: authorOrOptions.last_id ?? null,
|
|
limit: authorOrOptions.limit ?? 100
|
|
};
|
|
return this.call('post_notifications', params);
|
|
}
|
|
unreadNotifications(accountOrOptions, min_score = 25) {
|
|
const params = typeof accountOrOptions === 'string'
|
|
? { account: accountOrOptions, min_score }
|
|
: { account: accountOrOptions.account, min_score: accountOrOptions.min_score ?? 25 };
|
|
return this.call('unread_notifications', params);
|
|
}
|
|
referralAccounts(referrerOrOptions = null, campaign_id = null, limit = 100, last_created_at = null) {
|
|
const params = typeof referrerOrOptions === 'object' && referrerOrOptions !== null
|
|
? {
|
|
referrer: referrerOrOptions.referrer ?? null,
|
|
campaign_id: referrerOrOptions.campaign_id ?? null,
|
|
limit: referrerOrOptions.limit ?? 100,
|
|
last_created_at: referrerOrOptions.last_created_at ?? null
|
|
}
|
|
: { referrer: referrerOrOptions, campaign_id, limit, last_created_at };
|
|
return this.call('referral_accounts', params);
|
|
}
|
|
referralAccountsCount(referrerOrOptions = null, campaign_id = null, start_date = null, end_date = null) {
|
|
const params = typeof referrerOrOptions === 'object' && referrerOrOptions !== null
|
|
? {
|
|
referrer: referrerOrOptions.referrer ?? null,
|
|
campaign_id: referrerOrOptions.campaign_id ?? null,
|
|
start_date: referrerOrOptions.start_date ?? null,
|
|
end_date: referrerOrOptions.end_date ?? null
|
|
}
|
|
: { referrer: referrerOrOptions, campaign_id, start_date, end_date };
|
|
return this.call('referral_accounts_count', params);
|
|
}
|
|
}
|
|
exports.Nexus = Nexus;
|