UNPKG

@oap75/api

Version:
172 lines (171 loc) 7.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flattenProfileStructs = exports.flattenProfileStruct = exports.asPublicProfileStruct = exports.asCommentStruct = exports.asSharedPostStruct = exports.flattenPostStructs = exports.flattenPostStruct = exports.flattenSpaceStructs = exports.flattenSpaceStruct = exports.flattenPermisions = exports.flattenCommonFields = exports.getUniqueSpaceIds = exports.getUniqueContentIds = exports.getUniqueOwnerIds = exports.getUniqueIds = void 0; const utils_1 = require("@subsocial/utils"); function getUniqueIds(structs, idFieldName) { const ids = new Set(); structs.forEach((x) => { const edStruct = x.struct; const struct = (0, utils_1.notEmptyObj)(edStruct) ? edStruct : x; const id = struct[idFieldName]; if (id && !ids.has(id)) { ids.add(id); } }); return Array.from(ids); } exports.getUniqueIds = getUniqueIds; const getUniqueOwnerIds = (entities) => getUniqueIds(entities, 'ownerId'); exports.getUniqueOwnerIds = getUniqueOwnerIds; const getUniqueContentIds = (entities) => getUniqueIds(entities, 'contentId'); exports.getUniqueContentIds = getUniqueContentIds; const getUniqueSpaceIds = (entities) => getUniqueIds(entities, 'spaceId'); exports.getUniqueSpaceIds = getUniqueSpaceIds; function getUpdatedFields({ updated }) { const maybeUpdated = updated.unwrapOr(undefined); let res = { isUpdated: updated.isSome, }; if (maybeUpdated) { res = Object.assign(Object.assign({}, res), { updatedByAccount: maybeUpdated.account.toHuman(), updatedAtBlock: maybeUpdated.block.toNumber(), updatedAtTime: maybeUpdated.time.toNumber() }); } return res; } function getContentFields({ content }) { let res = {}; if (content.isIpfs) { res = { contentId: content.asIpfs.toHuman() }; } return res; } function flattenCommonFields(struct) { const { created } = struct; return Object.assign(Object.assign({ // created: createdByAccount: created.account.toHuman(), createdAtBlock: created.block.toNumber(), createdAtTime: created.time.toNumber() }, getUpdatedFields(struct)), getContentFields(struct)); } exports.flattenCommonFields = flattenCommonFields; function flattenSpaceOrPostStruct(struct) { return Object.assign(Object.assign({}, flattenCommonFields(struct)), { id: struct.id.toString(), ownerId: struct.owner.toHuman(), hidden: struct.hidden.isTrue }); } const flattenPermisions = (permissions) => { const flatPermissions = {}; if (permissions) { for (const [key, value] of permissions) { const permissionSet = value.unwrapOr(undefined); const permission = {}; permissionSet === null || permissionSet === void 0 ? void 0 : permissionSet.forEach(x => { permission[x.toString()] = true; }); flatPermissions[`${key}Permissions`] = permission; } } return flatPermissions; }; exports.flattenPermisions = flattenPermisions; function flattenSpaceStruct(struct) { var _a, _b, _c; const postsCount = struct.postsCount.toNumber(); const hiddenPostsCount = struct.hiddenPostsCount.toNumber(); const visiblePostsCount = postsCount - hiddenPostsCount; const flatPermissions = (0, exports.flattenPermisions)(struct.permissions.unwrapOr(undefined)); let parentField = {}; if (struct.parentId.isSome) { parentField = { parentId: struct.parentId.unwrap().toString() }; } let handleField = {}; if (struct.handle.isSome) { handleField = { handle: (_a = struct.handle.toHuman()) === null || _a === void 0 ? void 0 : _a.toString() }; } return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, flattenSpaceOrPostStruct(struct)), parentField), handleField), flatPermissions), { canFollowerCreatePosts: !!((_b = flatPermissions.followerPermissions) === null || _b === void 0 ? void 0 : _b.CreatePosts), canEveryoneCreatePosts: !!((_c = flatPermissions.everyonePermissions) === null || _c === void 0 ? void 0 : _c.CreatePosts), //TODO: check CreatePosts permissions in everyone set postsCount, hiddenPostsCount, visiblePostsCount, followersCount: struct.followersCount.toNumber(), score: struct.score.toNumber() }); } exports.flattenSpaceStruct = flattenSpaceStruct; function flattenSpaceStructs(structs) { return structs.map(flattenSpaceStruct); } exports.flattenSpaceStructs = flattenSpaceStructs; function flattenPostExtension(struct) { const { isSharedPost, isComment } = struct.extension; let normExt = {}; if (isSharedPost) { const sharedPost = { sharedPostId: struct.extension.asSharedPost.toString() }; normExt = sharedPost; } else if (isComment) { const { parentId, rootPostId } = struct.extension.asComment; const comment = { rootPostId: rootPostId.toString() }; if (parentId.isSome) { comment.parentId = parentId.unwrap().toString(); } normExt = comment; } return normExt; } function flattenPostStruct(struct) { const repliesCount = struct.repliesCount.toNumber(); const hiddenRepliesCount = struct.hiddenRepliesCount.toNumber(); const visibleRepliesCount = repliesCount - hiddenRepliesCount; const { isRegularPost, isSharedPost, isComment } = struct.extension; const extensionFields = flattenPostExtension(struct); let spaceField = {}; if (struct.spaceId.isSome) { spaceField = { spaceId: struct.spaceId.unwrap().toString(), }; } return Object.assign(Object.assign(Object.assign(Object.assign({}, flattenSpaceOrPostStruct(struct)), spaceField), extensionFields), { repliesCount, hiddenRepliesCount, visibleRepliesCount, sharesCount: struct.sharesCount.toNumber(), upvotesCount: struct.upvotesCount.toNumber(), downvotesCount: struct.downvotesCount.toNumber(), score: struct.score.toNumber(), isRegularPost, isSharedPost, isComment }); } exports.flattenPostStruct = flattenPostStruct; function flattenPostStructs(structs) { return structs.map(flattenPostStruct); } exports.flattenPostStructs = flattenPostStructs; function asSharedPostStruct(post) { if (!post.isSharedPost) throw new Error('Not a shared post'); return post; } exports.asSharedPostStruct = asSharedPostStruct; function asCommentStruct(post) { if (!post.isComment) throw new Error('Not a comment'); return post; } exports.asCommentStruct = asCommentStruct; function asPublicProfileStruct(profile) { if (!profile.hasProfile) throw new Error('Account has no profile'); return profile; } exports.asPublicProfileStruct = asPublicProfileStruct; function flattenProfileStruct(struct) { var _a, _b; const profile = (_a = struct.profile) === null || _a === void 0 ? void 0 : _a.unwrapOr(undefined); const hasProfile = (_b = struct.profile) === null || _b === void 0 ? void 0 : _b.isSome; const maybeProfile = profile ? flattenCommonFields(profile) : {}; return Object.assign({ id: struct.id.toString(), followersCount: struct.followersCount.toNumber(), followingAccountsCount: struct.followingAccountsCount.toNumber(), followingSpacesCount: struct.followingSpacesCount.toNumber(), reputation: struct.reputation.toNumber(), hasProfile }, maybeProfile); } exports.flattenProfileStruct = flattenProfileStruct; function flattenProfileStructs(accounts) { return accounts.map(flattenProfileStruct); } exports.flattenProfileStructs = flattenProfileStructs;