UNPKG

@subsocial/api

Version:
425 lines (424 loc) 20.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubsocialApi = void 0; const utils_1 = require("./flatteners/utils"); const basic_1 = require("./basic"); const utils_2 = require("@subsocial/utils"); const flatteners_1 = require("./flatteners"); const connections_1 = require("../connections"); class SubsocialApi { constructor(props) { this._base = new basic_1.BasicSubsocialApi(props); } /** Create an instance of subsocial api with given config and connected substrate node */ static create(_a) { var { substrateNodeUrl } = _a, props = __rest(_a, ["substrateNodeUrl"]); return __awaiter(this, void 0, void 0, function* () { const substrateApi = yield (0, connections_1.getSubstrateApi)(substrateNodeUrl); return new SubsocialApi(Object.assign({ substrateApi }, props)); }); } /** @deprecated */ get subsocial() { return this._base; } /** * @deprecated * Getter for an ApiPromise to connect to Subsocial nodes and query chain states, but in raw format. * The raw formatted data needs to be flattened first in order to be readable by JavaScript. * * Accessors for private field {@link _base}*/ get base() { return this._base; } /** * Getter for an ApiPromise to connect to Subsocial nodes and query chain states. * It is useful in the case of querying data about posts, spaces, and profiles. * * Accessors for private field {@link _base.substrate}*/ get blockchain() { return this._base.substrate; } /** * Getter for an aggregated ApiPromise to work with IPFS. * It is useful for getting the contents of spaces, posts, and profiles. * * Accessors for private field {@link _base.ipfs}*/ get ipfs() { return this._base.ipfs; } /** * Getter for a Polkadot ApiPromise without wrappers to call functions from Subsocial Pallets. * It is useful for creating specific txs or queries. * * Accessors for private field {@link _base.substrate.api} **/ get substrateApi() { return this._base.substrate.api; } //------------------------------------------------ // Structs from chain /** * Find and load an array of information about space structs (can be Unlisted or Public, depending on `filters`) from the Subsocial blockchain and IPFS * by a given array of space `ids` and `filters` object. * * * @param ids - An array of the space ids of the desired space structs. * * @param filters - (Optional) An object containing a visibility filter (`hidden` field) and content filter (with or without IPFS content). * * @returns An array of information about space structs aggregated from the Subsocial blockchain and IPFS. If there are no * space structs corresponding to the given space `ids` and `filters`, an empty array is returned. */ findSpaceStructs(ids, filters) { return __awaiter(this, void 0, void 0, function* () { const structs = yield this.blockchain.findSpaces(Object.assign({ ids: (0, utils_2.idsToBns)(ids) }, filters)); return (0, flatteners_1.flattenSpaceStructs)(structs); }); } /** * Find and load an array of information about post structs (can be Unlisted or Public, depending on `filters`) from the Subsocial blockchain and IPFS * by a given array of post `ids` and `filters` object. * * * @param ids - An array of the post ids of the desired post structs. * * @param filters - (Optional) An object containing a visibility filter (`hidden` field) and content filter (with or without IPFS content). * * @returns An array of information about post structs aggregated from the Subsocial blockchain and IPFS. If there are no * post structs corresponding to the given post `ids` and `filters` object, an empty array is returned. */ findPostStructs(ids, filters) { return __awaiter(this, void 0, void 0, function* () { const structs = yield this.blockchain.findPosts(Object.assign({ ids: (0, utils_2.idsToBns)(ids) }, filters)); return (0, flatteners_1.flattenPostStructs)(structs); }); } /** * Find and load information about space structs (can be Unlisted or Public, depending on `filters`) from the Subsocial blockchain and IPFS * by a given space `id` and `filters` object. * * * @param id - The space id of the desired space structs. * * @param filters - (Optional) An object containing a visibility filter (`hidden` field) and content filter (with or without IPFS content). * * @returns Data about space structs aggregated from the Subsocial blockchain and IPFS. If there are no * space structs corresponding to the given space `id` and `filters` object, undefined is returned. */ findSpaceStruct(id, filters) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_2.getFirstOrUndefined)(yield this.findSpaceStructs([id], filters)); }); } /** * Find and load information about post structs (can be Unlisted or Public, depending on `filters`) from the Subsocial blockchain and IPFS * by a given post `id` and `filters` object. * * * @param id - The post id of the desired post structs. * * @param filters - (Optional) An object containing a visibility filter (`hidden` field) and content filter (with or without IPFS content). * * @returns Data about post structs aggregated from the Subsocial blockchain and IPFS. If there are no * post structs corresponding to the given post `id` and `filters` object, undefined is returned. */ findPostStruct(id, filters) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_2.getFirstOrUndefined)(yield this.findPostStructs([id], filters)); }); } //------------------------------------------------ // Spaces /** * Find and load data about a space (can be Unlisted or Public, depending on filters set on `query`) from the Subsocial blockchain and IPFS by a given `query` object. * * * @param query - An object containing the desired space `id`, a visibility filter (`hidden` field), and a content filter (with or without IPFS content). * * @returns Data about a space aggregated from the Subsocial blockchain and IPFS. If there is no * space corresponding to the given `query` object, undefined is returned. */ findSpace(query) { return __awaiter(this, void 0, void 0, function* () { const spaces = yield this.findSpaces(Object.assign(Object.assign({}, query), { ids: [(0, utils_2.idToBn)(query.id)] })); return (0, utils_2.getFirstOrUndefined)(spaces); }); } /** * Find and load an array of information about spaces (can be Unlisted or Public, depending on `filter` object) from the Subsocial blockchain and IPFS by a given `filter` object. * * * @param filter - An object containing the desired space `ids`, a visibility filter (`hidden` field), and a content filter (with or without IPFS content). * * @returns An array of data about desired spaces aggregated from the Subsocial blockchain and IPFS. If there are no * spaces corresponding to the given `filter` object, undefined is returned. */ findSpaces(filter) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewSpaceDataArray)(yield this.base.findSpaces(filter)); }); } /** * Find and load an array of information about public spaces from the Subsocial blockchain and IPFS by a given array of * space `ids`. * * A space is considered public if it meets the following conditions: * - The `hidden` field on its blockchain structure is `false`. * - There is a corresponding JSON file that represents the space's content on IPFS. * * @param ids - An array of the ids of the desired spaces. * * @returns An array of data about the desired spaces aggregated from the Subsocial blockchain and IPFS. If there are no * spaces corresponding to the given array of `ids`, an empty array is returned. */ findPublicSpaces(ids) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewSpaceDataArray)(yield this.base.findPublicSpaces((0, utils_2.idsToBns)(ids))); }); } /** * Find and load an array of information about unlisted spaces from the Subsocial blockchain and IPFS by a given array of * space `ids`. * * A space is considered unlisted if it meets either of these conditions: * - The `hidden` field on its blockchain structure is `true`. * - There is no corresponding JSON file that represents the space's content on IPFS. * * @param ids - An array of the ids of the desired spaces. * * @returns An array of data about the desired spaces aggregated from the Subsocial blockchain and IPFS. If there are no * spaces corresponding to the given array of `ids`, an empty array is returned. */ findUnlistedSpaces(ids) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewSpaceDataArray)(yield this.base.findUnlistedSpaces((0, utils_2.idsToBns)(ids))); }); } //------------------------------------------------ // Posts /** * Find and load data about a post from the Subsocial blockchain and IPFS by a given `query` object. * * * @param query - An object containing the desired post `id`, a visibility filter (`hidden` field), and a content filter (with or without IPFS content). * * @returns Data about a post aggregated from the Subsocial blockchain and IPFS. If there is no * post corresponding to the given `query` object, undefined is returned. */ findPost(query) { return __awaiter(this, void 0, void 0, function* () { const posts = yield this.findPosts(Object.assign(Object.assign({}, query), { ids: [(0, utils_2.idToBn)(query.id)] })); return (0, utils_2.getFirstOrUndefined)(posts); }); } /** * Find and load an array of information about posts (can be Unlisted or Public, depending on `filter` object) from the Subsocial blockchain and IPFS by a given `filter` object. * * * @param filter - An object containing the desired post `ids`, a visibility filter (`hidden` field), and a content filter (with or without IPFS content). * * @returns An array of data about the desired posts aggregated from the Subsocial blockchain and IPFS. If there are no * posts corresponding to the given `filter` object, an empty array is returned. */ findPosts(filter) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostDataArray)(yield this.base.findPosts(filter)); }); } /** * Find and load an array of public posts from the Subsocial blockchain and IPFS by a given array of post `ids`. * * A post is considered public if it meets the following conditions: * - The `hidden` field on its blockchain structure is `false`. * - There is a corresponding JSON file that represents the space's content on IPFS. * * @param ids - An array of the ids of the desired posts. * * @returns An array of data about the desired posts aggregated from the Subsocial blockchain and IPFS. If there are no * posts corresponding to the given array of `ids`, an empty array is returned. */ findPublicPosts(ids) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostDataArray)(yield this.base.findPublicPosts((0, utils_2.idsToBns)(ids))); }); } //------------------------------------------------ // Posts with details /** * Find and load information about a post with some of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findPostWithSomeDetails(query) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithSomeDetails)(yield this.base.findPostWithSomeDetails(query)); }); } /** * Find and load information about a post with all of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findPostWithAllDetails(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithAllDetails)(yield this.base.findPostWithAllDetails((0, utils_2.idToBn)(id))); }); } /** * Find and load an array of information about a post with all of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findPostsWithAllDetails(query) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithAllDetailsArray)(yield this.base.findPostsWithAllDetails(query)); }); } /** * Find and load an array of information about a public post with some of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findPublicPostsWithSomeDetails(query) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithSomeDetailsArray)(yield this.base.findPublicPostsWithSomeDetails(query)); }); } /** * Find and load an array of information about a public post with all of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findPublicPostsWithAllDetails(ids) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithAllDetailsArray)(yield this.base.findPublicPostsWithAllDetails((0, utils_2.idsToBns)(ids))); }); } /** * Find and load an array of information about an unlisted post with all of its details: extension (PostExtensionData), owner (SpaceData), and space (SpaceData). * These methods are **deprecated**, because they are slow (although quite useful for UI). */ findUnlistedPostsWithAllDetails(ids) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.convertToNewPostWithAllDetailsArray)(yield this.base.findUnlistedPostsWithAllDetails((0, utils_2.idsToBns)(ids))); }); } //------------------------------------------------ // Profile spaces /** * Find and load an array of information about profile spaces from Subsocial blockchain and IPFS by a given array of * account ids `accounts`. * * A profile space is just a space set to a profile. * * @param accounts - An array of the account ids related to the desired profile spaces * * @returns Find and load an array of information about the profile space of each accountId in the array * from the Subsocial blockchain and IPFS. * */ findProfileSpaces(accounts) { return __awaiter(this, void 0, void 0, function* () { const profileSpaces = yield this.base.findProfileSpaces(accounts); return (0, utils_1.convertToNewSpaceDataArray)(profileSpaces); }); } /** * Find and load information about a profile space from the Subsocial blockchain and IPFS using an account id. * * * A profile space is just a space set to a profile. * * @param account - An account id corresponding to a desired space profile. * * @returns Data about the desired profile space aggregated from the Subsocial blockchain and IPFS. If there is space corresponding to the given account id, * `undefined` is returned. */ findProfileSpace(account) { return __awaiter(this, void 0, void 0, function* () { const profileSpaces = yield this.findProfileSpaces([account]); return (0, utils_2.getFirstOrUndefined)(profileSpaces); }); } //------------------------------------------------ // Domains /** * Find and load an array of information about some domain structs from the Subsocial blockchain by a given array of `domainNames`. * * * @param domainNames - An array of domain names. * * @returns An array of data about the desired domain structs aggregated from the Subsocial blockchain. If there are no domains corresponding to the given array of `domainNames`, * an empty array is returned. */ findDomains(domainNames) { return __awaiter(this, void 0, void 0, function* () { const structs = yield this.blockchain.registeredDomains(domainNames); return (0, flatteners_1.flattenDomainStructs)(structs); }); } /** * Find and load data about a domain from the Subsocial blockchain by a given string of `domainName`. * * * @param domainName - A string that is a domain name. * * @returns Data about a desired domain name from the Subsocial blockchain. If there is no domain name corresponding to the given `domainName`, * `undefined` is returned. */ findDomain(domainName) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_2.getFirstOrUndefined)(yield this.findDomains([domainName])); }); } //------------------------------------------------ // Reactions /** * Find and load data about a reaction from the Subsocial blockchain by a given `id`. * * * @param id - The desired reaction id * * @returns Data about the reaction consisting the reaction kind, creation, update metadata if any. If there is no * reaction with given id, undefined is returned. */ findReaction(id) { return __awaiter(this, void 0, void 0, function* () { const reaction = yield this.blockchain.findReaction((0, utils_2.idToBn)(id)); if (!reaction) return undefined; return (0, flatteners_1.flattenReaction)(reaction); }); } /** * Find and load an array of information about a reactions from the Subsocial blockchain from all the given id in `ids` array. * * * @param ids - An array containing the desired reaction `ids`. * * @returns An array of data about desired reactions. */ findReactions(id) { return __awaiter(this, void 0, void 0, function* () { const reactions = yield this.blockchain.findReactions((0, utils_2.idsToBns)(id)); return (0, flatteners_1.flattenReactions)(reactions); }); } } exports.SubsocialApi = SubsocialApi;