UNPKG

@oap75/api

Version:
254 lines (253 loc) 11.6 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 basic_1 = require("./basic"); const loadPostStructs_1 = require("../utils/loadPostStructs"); const utils_1 = require("@subsocial/utils"); class SubsocialApi extends basic_1.BasicSubsocialApi { constructor() { super(...arguments); this.structFinders = { findSpaces: this.findPublicSpaces.bind(this), findPosts: this.findPublicPosts.bind(this), findProfiles: this.findProfiles.bind(this) }; } findAllSpaces(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findSpaces({ ids }); }); } /** * Find and load an array of information about public spaces from Subsocial blockchain and IPFS by a given array of * space `ids`. * * Space is considered public if it meets the next conditions: * - The `hidden` field on its' blockchain structure is `false`. * - And there is a corresponding JSON file that represents the space's content on IPFS. * * @param ids - An array of ids of desired spaces. * * @returns An array of data about desired spaces aggregated from Subsocial blockchain and IPFS. If no corresponding * spaces to given array of `ids`, an empty array is returned. */ findPublicSpaces(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findSpaces({ ids, visibility: 'onlyPublic', withContentOnly: true }); }); } /** * Find and load an array of information about unlisted spaces from Subsocial blockchain and IPFS by a given array of * space `ids`. * * Space is considered unlisted if it meets either of these conditions: * - The `hidden` field on it's blockchain structure is `true`. * - Or there is no corresponding JSON file that represents the space's content on IPFS. * * @param ids - An array of ids of desired spaces. * * @returns An array of data about desired spaces aggregated from Subsocial blockchain and IPFS. If no corresponding * spaces to given array of `ids`, an empty array is returned. */ findUnlistedSpaces(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findSpaces({ ids, visibility: 'onlyUnlisted' }); }); } findAllPosts(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findPosts({ ids }); }); } /** * Find and load an array of information about public posts from Subsocial blockchain and IPFS by a given array of * post `ids`. * * Post is considered public if it meets the next conditions: * - The `hidden` field on its' blockchain structure is `false`. * - And there is a corresponding JSON file that represents the post's content on IPFS. * * @param ids - An array of ids of desired posts. * * @returns An array of data about desired posts aggregated from Subsocial blockchain and IPFS. If no corresponding * posts to given array of `ids`, an empty array is returned. */ findPublicPosts(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findPosts({ ids, visibility: 'onlyPublic', withContentOnly: true }); }); } /** * Find and load an array of information about unlisted posts from Subsocial blockchain and IPFS by a given array of * post `ids`. * * Post is considered unlisted if it meets either of these conditions: * - The `hidden` field on it's blockchain structure is `true`. * - Or there is no corresponding JSON file that represents the post's content on IPFS. * * @param ids - An array of ids of desired posts * * @returns An array of data about desired posts aggregated from Subsocial blockchain and IPFS. If no corresponding * posts to given array of `ids`, an empty array is returned. */ findUnlistedPosts(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findPosts({ ids, visibility: 'onlyUnlisted' }); }); } /** Find and load posts with their extension and owner's profile (if defined). */ findPostsWithSomeDetails(filter) { return __awaiter(this, void 0, void 0, function* () { const posts = yield this.findPosts(filter); return (0, loadPostStructs_1.loadAndSetPostRelatedStructs)(posts, this.structFinders, filter); }); } findPublicPostsWithSomeDetails(filter) { return __awaiter(this, void 0, void 0, function* () { return this.findPostsWithSomeDetails(Object.assign(Object.assign({}, filter), { visibility: 'onlyPublic' })); }); } findUnlistedPostsWithSomeDetails(filter) { return __awaiter(this, void 0, void 0, function* () { return this.findPostsWithSomeDetails(Object.assign(Object.assign({}, filter), { visibility: 'onlyUnlisted' })); }); } findPostsWithAllDetails({ ids, visibility }) { return __awaiter(this, void 0, void 0, function* () { return this.findPostsWithSomeDetails({ ids, withSpace: true, withOwner: true, visibility }); }); } findPublicPostsWithAllDetails(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findPostsWithAllDetails({ ids, visibility: 'onlyPublic' }); }); } findUnlistedPostsWithAllDetails(ids) { return __awaiter(this, void 0, void 0, function* () { return this.findPostsWithAllDetails({ ids, visibility: 'onlyUnlisted' }); }); } // Functions that return a single element /** * Find and load information about a public space from Subsocial blockchain and IPFS using space id. * * Space is considered public if it meets these conditions: * - The `hidden` field on it's blockchain structure is `false`. * - And there is a corresponding JSON file that represents the space's content on IPFS. * * @param id - Id of desired space. * * @returns Data about desired space aggregated from blockchain and IPFS. If no corresponding space to given id, * `undefined` is returned. */ findPublicSpace(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPublicSpaces([id])); }); } /** * Find and load information about an unlisted space from blockchain and from IPFS by a given space id. * * Space is considered unlisted if it meets either of these conditions: * - The `hidden` field on it's blockchain structure is `true`. * - Or there is no corresponding JSON file that represents the space's content on IPFS. * * @param id - Id of desired space. * * @returns Data about a desired space aggregated from blockchain and IPFS. If no corresponding space to given id, * `undefined` is returned. */ findUnlistedSpace(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findUnlistedSpaces([id])); }); } /** * Find and load information about a public post from Subsocial blockchain and IPFS using post id. * * Post is considered public if it meets the next conditions: * - The `hidden` field on it's blockchain structure is `false`. * - And there is a corresponding JSON file that represents the post's content on IPFS. * * @param id - Id of desired post. * * @returns Data about desired post aggregated from blockchain and IPFS. If no corresponding post to given id, * `undefined` is returned. */ findPublicPost(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPublicPosts([id])); }); } /** * Find and load information about an unlisted post from blockchain and from IPFS by a given post id. * * Post is considered unlisted if it meets either of these conditions: * - The `hidden` field on it's blockchain structure is `true`. * - Or there is no corresponding JSON file that represents the post's content on IPFS. * * @param id - Id of desired post. * * @returns Data about desired post aggregated from blockchain and IPFS. If no corresponding post to given id, * `undefined` is returned. */ findUnlistedPost(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findUnlistedPosts([id])); }); } findPostWithSomeDetails(_a) { var { id } = _a, opts = __rest(_a, ["id"]); return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPostsWithSomeDetails(Object.assign({ ids: [id] }, opts))); }); } findPublicPostWithSomeDetails(_a) { var { id } = _a, opts = __rest(_a, ["id"]); return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPublicPostsWithSomeDetails(Object.assign({ ids: [id] }, opts))); }); } findUnlistedPostWithSomeDetails(_a) { var { id } = _a, opts = __rest(_a, ["id"]); return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findUnlistedPostsWithSomeDetails(Object.assign({ ids: [id] }, opts))); }); } findPostWithAllDetails(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPostsWithAllDetails({ ids: [id] })); }); } findPublicPostWithAllDetails(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findPublicPostsWithAllDetails([id])); }); } findUnlistedPostWithAllDetails(id) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.getFirstOrUndefined)(yield this.findUnlistedPostsWithAllDetails([id])); }); } } exports.SubsocialApi = SubsocialApi;