@oap75/api
Version:
JavaScript API for Subsocial blockchain.
120 lines (119 loc) • 5.74 kB
JavaScript
"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.BasicSubsocialApi = void 0;
const utils_1 = require("@subsocial/utils");
const ipfs_1 = require("../ipfs");
const substrate_1 = require("../substrate");
const common_1 = require("../utils/common");
const content_filter_1 = require("../filters/content-filter");
/** Using this class, you can get all the data of posts, spaces and profiles from blockchain storages and ipfs */
class BasicSubsocialApi {
constructor(props) {
const { substrateApi, ipfsNodeUrl, offchainUrl } = props, context = __rest(props, ["substrateApi", "ipfsNodeUrl", "offchainUrl"]);
this._substrate = new substrate_1.SubsocialSubstrateApi(Object.assign({ api: substrateApi }, context));
this._ipfs = new ipfs_1.SubsocialIpfsApi(Object.assign({ ipfsNodeUrl, offchainUrl }, context));
}
/** Accessors for privat field {@link _substrate}*/
get substrate() {
return this._substrate;
}
/** Accessors for privat field {@link _ipfs}*/
get ipfs() {
return this._ipfs;
}
/** Get an array of data from blockchain storages and ipfs that is passed in the parameters of the method
* @param findStructs gets an array of structures by ids
* @param findContents gets contents by cids
*/
findDataArray(ids, findStructs, findContents) {
return __awaiter(this, void 0, void 0, function* () {
const structs = yield findStructs(ids);
const cids = (0, common_1.getUniqueIds)((0, ipfs_1.getCidsOfStructs)(structs));
const contents = yield findContents(cids);
return structs.map(struct => {
const hash = (0, ipfs_1.getIpfsCidOfStruct)(struct);
const content = hash ? contents[hash] : undefined;
return { struct, content };
});
});
}
// ---------------------------------------------------------------------
// Multiple
/** Find and load an array of spaces */
findSpaces(filter) {
return __awaiter(this, void 0, void 0, function* () {
const findStructs = this.substrate.findSpaces.bind(this.substrate, filter);
const findContents = this.ipfs.findSpaces.bind(this.ipfs);
const spaces = yield this.findDataArray(filter.ids, findStructs, findContents);
return (0, content_filter_1.contentFilter)({
structs: spaces,
withContentOnly: filter.withContentOnly
});
});
}
/** Find and load an array of posts */
findPosts(filter) {
return __awaiter(this, void 0, void 0, function* () {
const findStructs = this.substrate.findPosts.bind(this.substrate, filter);
const findContents = this.ipfs.findPosts.bind(this.ipfs);
const posts = yield this.findDataArray(filter.ids, findStructs, findContents);
return (0, content_filter_1.contentFilter)({
structs: posts,
withContentOnly: filter.withContentOnly
});
});
}
/** Find and load an array of profiles */
findProfiles(ids) {
return __awaiter(this, void 0, void 0, function* () {
const findStructs = this.substrate.findSocialAccounts.bind(this.substrate);
const findContents = this.ipfs.findProfiles.bind(this.ipfs);
const profiles = yield this.findDataArray(ids, findStructs, findContents);
return profiles.map(x => {
const profile = x.struct.profile.unwrapOr(undefined);
return Object.assign(Object.assign({}, x), { profile });
});
});
}
// ---------------------------------------------------------------------
// Single
/** Find and load single space */
findSpace({ id, visibility }) {
return __awaiter(this, void 0, void 0, function* () {
return (0, utils_1.getFirstOrUndefined)(yield this.findSpaces({ ids: [id], visibility }));
});
}
/** Find and load single post */
findPost({ id, visibility }) {
return __awaiter(this, void 0, void 0, function* () {
return (0, utils_1.getFirstOrUndefined)(yield this.findPosts({ ids: [id], visibility }));
});
}
/** Find and load single profile */
findProfile(id) {
return __awaiter(this, void 0, void 0, function* () {
return (0, utils_1.getFirstOrUndefined)(yield this.findProfiles([id]));
});
}
}
exports.BasicSubsocialApi = BasicSubsocialApi;