UNPKG

allostasis-js-sdk

Version:
1,391 lines 281 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const http_client_1 = require("@ceramicnetwork/http-client"); const client_1 = require("@composedb/client"); const definition_1 = require("./constants/definition"); const definition_stage_1 = require("./constants/definition-stage"); const startup_definition_1 = require("./constants/startup-definition"); const startup_definition_stage_1 = require("./constants/startup-definition-stage"); const platform_definition_1 = require("./constants/platform-definition"); const platform_definition_stage_1 = require("./constants/platform-definition-stage"); const store_1 = require("./utils/store"); const lodash_1 = __importDefault(require("lodash")); const dayjs_1 = __importDefault(require("dayjs")); const kubo_rpc_client_1 = require("kubo-rpc-client"); const nakama_js_1 = require("@heroiclabs/nakama-js"); const dids_1 = require("dids"); const sha256_1 = require("@stablelib/sha256"); const uint8Array_1 = require("./utils/uint8Array"); const key_did_provider_ed25519_1 = require("key-did-provider-ed25519"); const key_did_resolver_1 = __importDefault(require("key-did-resolver")); class Allostasis { constructor(community, options) { /* ** Create education for user */ this.createEducation = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const education = yield this.composeClient.executeQuery(` mutation { createEducation(input: { content: { profileID: "${params.profileID}", title: "${params.title}", school: "${params.school}", city: "${params.city}", description: "${params.description}", startDate: "${params.startDate}", endDate: "${params.endDate}", isDeleted: false } }) { document { creator { id } id profileID title school city description startDate endDate isDeleted } } } `); if (education.errors != null && education.errors.length > 0) { reject(education); } else { resolve(education.data.createEducation.document); } } catch (e) { reject(e); } })); }); /* ** Update education of a user */ this.updateEducation = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const education = yield this.composeClient.executeQuery(` mutation { updateEducation(input: { id: "${params.id}", content: { profileID: "${params.profileID}", title: "${params.title}", school: "${params.school}", city: "${params.city}", description: "${params.description}", startDate: "${params.startDate}", endDate: "${params.endDate}", isDeleted: ${params.isDeleted} } }) { document { creator { id } id profileID title school city description startDate endDate isDeleted } } } `); if (education.errors != null && education.errors.length > 0) { reject(education); } else { resolve(education.data.updateEducation.document); } } catch (e) { reject(e); } })); }); /* ** Create experience for user */ this.createExperience = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const experience = yield this.composeClient.executeQuery(` mutation { createExperience(input: { content: { profileID: "${params.profileID}", title: "${params.title}", company: "${params.company}", city: "${params.city}", description: "${params.description}", startDate: "${params.startDate}", endDate: "${params.endDate}", isDeleted: false } }) { document { creator { id } id profileID title company city description startDate endDate isDeleted } } } `); if (experience.errors != null && experience.errors.length > 0) { reject(experience); } else { resolve(experience.data.createExperience.document); } } catch (e) { reject(e); } })); }); /* ** Update experience of a user */ this.updateExperience = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const experience = yield this.composeClient.executeQuery(` mutation { updateExperience(input: { id: "${params.id}", content: { profileID: "${params.profileID}", title: "${params.title}", company: "${params.company}", city: "${params.city}", description: "${params.description}", startDate: "${params.startDate}", endDate: "${params.endDate}", isDeleted: ${params.isDeleted} } }) { document { creator { id } id profileID title company city description startDate endDate isDeleted } } } `); if (experience.errors != null && experience.errors.length > 0) { reject(experience); } else { resolve(experience.data.updateExperience.document); } } catch (e) { reject(e); } })); }); /* ** Create an article comment */ this.createArticleComment = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const create = yield this.composeClient.executeQuery(` mutation { createArticleComment(input: { content: { content: "${params.content}", articleID: "${params.articleID}", profileID: "${params.profileID}", createdAt: "${(0, dayjs_1.default)().toISOString()}", ${params.replyingTo ? `replyingToID: "${params.replyingTo}",` : ''} isDeleted: false } }) { document { creator { id } id content createdAt articleID profileID profile { id displayName avatar nakamaID publicEncryptionDID { id } bio } isDeleted replyingToID } } } `); if (create.errors != null && create.errors.length > 0) { reject(create); } else { resolve((_b = (_a = create.data) === null || _a === void 0 ? void 0 : _a.createArticleComment) === null || _b === void 0 ? void 0 : _b.document); } } catch (e) { reject(e); } })); }); /* ** Like or unlike an article */ this.likeArticle = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const liked = yield this.composeClient.executeQuery(` query { articleLikeIndex( filters: { and: [ { where: { articleID: { equalTo: "${params.articleID}" } } }, { where: { profileID: { equalTo: "${params.profileID}" } } } ] }, last: 1 ) { edges { node { creator { id } id articleID profileID profile { id displayName avatar nakamaID publicEncryptionDID { id } bio } isDeleted } } } } `); if (liked.errors != null && liked.errors.length > 0) { reject(liked); } else { if (liked.data.articleLikeIndex.edges.length > 0) { if (liked.data.articleLikeIndex.edges[0].node.isDeleted) { yield this.composeClient.executeQuery(` mutation { createArticleLike(input: { content: { articleID: "${params.articleID}", profileID: "${params.profileID}", isDeleted: false } }) { document { creator { id } id articleID profileID isDeleted } } } `); resolve(true); } else { yield this.composeClient.executeQuery(` mutation { updateArticleLike(input: { id: "${liked.data.articleLikeIndex.edges[0].node.id}", content: { isDeleted: true } }) { document { creator { id } id articleID profileID isDeleted } } } `); resolve(false); } } else { yield this.composeClient.executeQuery(` mutation { createArticleLike(input: { content: { articleID: "${params.articleID}", profileID: "${params.profileID}", isDeleted: false } }) { document { creator { id } id articleID profileID isDeleted } } } `); resolve(true); } } } catch (e) { reject(e); } })); }); /* ** Create a post comment */ this.createPostComment = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { var _c, _d; try { const create = yield this.composeClient.executeQuery(` mutation { createPostComment(input: { content: { content: "${params.content}", postID: "${params.postID}", profileID: "${params.profileID}", createdAt: "${(0, dayjs_1.default)().toISOString()}", ${params.replyingTo ? `replyingToID: "${params.replyingTo}",` : ''} isDeleted: false } }) { document { creator { id } id content createdAt postID profileID profile { id displayName avatar nakamaID publicEncryptionDID { id } bio } isDeleted replyingToID } } } `); if (create.errors != null && create.errors.length > 0) { reject(create); } else { resolve((_d = (_c = create.data) === null || _c === void 0 ? void 0 : _c.createPostComment) === null || _d === void 0 ? void 0 : _d.document); } } catch (e) { reject(e); } })); }); /* ** Like or unlike a post */ this.likePost = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const liked = yield this.composeClient.executeQuery(` query { postLikeIndex( filters: { and: [ { where: { postID: { equalTo: "${params.postID}" } } }, { where: { profileID: { equalTo: "${params.profileID}" } } } ] }, last: 1 ) { edges { node { creator { id } id postID profileID profile { id displayName avatar nakamaID publicEncryptionDID { id } bio } isDeleted } } } } `); if (liked.errors != null && liked.errors.length > 0) { reject(liked); } else { if (liked.data.postLikeIndex.edges.length > 0) { if (liked.data.postLikeIndex.edges[0].node.isDeleted) { yield this.composeClient.executeQuery(` mutation { createPostLike(input: { content: { postID: "${params.postID}", profileID: "${params.profileID}", isDeleted: false } }) { document { creator { id } id postID profileID isDeleted } } } `); resolve(true); } else { yield this.composeClient.executeQuery(` mutation { updatePostLike(input: { id: "${liked.data.postLikeIndex.edges[0].node.id}", content: { isDeleted: true } }) { document { creator { id } id postID profileID isDeleted } } } `); resolve(false); } } else { yield this.composeClient.executeQuery(` mutation { createPostLike(input: { content: { postID: "${params.postID}", profileID: "${params.profileID}", isDeleted: false } }) { document { creator { id } id postID profileID isDeleted } } } `); resolve(true); } } } catch (e) { reject(e); } })); }); /* ** Follow or unfollow a user */ this.follow = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const followed = yield this.composeClient.executeQuery(` query { followIndex( filters: { and: [ { where: { profileID: { equalTo: "${params.profileID}" } } }, { where: { targetProfileID: { equalTo: "${params.targetProfileID}" } } } ] }, last: 1 ) { edges { node { creator { id } id targetProfileID profileID isDeleted } } } } `); if (followed.errors != null && followed.errors.length > 0) { reject(followed); } else { if (followed.data.followIndex.edges.length > 0) { if (followed.data.followIndex.edges[0].node.isDeleted) { yield this.composeClient.executeQuery(` mutation { createFollow(input: { content: { profileID: "${params.profileID}", targetProfileID: "${params.targetProfileID}", isDeleted: false } }) { document { creator { id } id targetProfileID profileID isDeleted } } } `); resolve(true); } else { yield this.composeClient.executeQuery(` mutation { updateFollow(input: { id: "${followed.data.followIndex.edges[0].node.id}", content: { isDeleted: true } }) { document { creator { id } id targetProfileID profileID isDeleted } } } `); resolve(false); } } else { yield this.composeClient.executeQuery(` mutation { createFollow(input: { content: { profileID: "${params.profileID}", targetProfileID: "${params.targetProfileID}", isDeleted: false } }) { document { creator { id } id targetProfileID profileID isDeleted } } } `); resolve(true); } } } catch (e) { reject(e); } })); }); /* ** User follows others or not */ this.userFollows = (params) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const followed = yield this.composeClient.executeQuery(` query { followIndex( filters: { and: [ { where: { profileID: { equalTo: "${params.profileID}" } } }, { where: { targetProfileID: { equalTo: "${params.targetProfileID}" } } } ] }, last: 1 ) { edges { node { creator { id } id targetProfileID profileID isDeleted } } } } `); if (followed.errors != null && followed.errors.length > 0) { reject(followed); } else { if (followed.data.followIndex.edges.length > 0) { if (followed.data.followIndex.edges[0].node.isDeleted) { resolve(false); } else { resolve(true); } } else { resolve(false); } } } catch (e) { reject(e); } })); }); this.nodeURL = options.nodeURL; this.community = community; if (options.env) { this.env = options.env; } else { this.env = 'production'; } if (options.model) { this.model = options.model; } else { this.model = 'user'; } if (options.providerType) { this.providerType = options.providerType; } else { this.providerType = 'metamask'; } if (!options.provider) { if (window.ethereum) { this.provider = window.ethereum; } else { console.log('Allostasis', 'An ethereum provider is required to proceed with the connection to Ceramic.'); } } else { this.provider = options.provider; } if (options.nakama) { this.nakamaClient = new nakama_js_1.Client(options.nakama.key, options.nakama.server, options.nakama.port, options.nakama.useSSL); } this.ceramic = new http_client_1.CeramicClient(options.nodeURL); this.composeClient = new client_1.ComposeClient({ ceramic: options.nodeURL, definition: this.model === 'user' ? this.env === 'production' ? definition_1.definition : definition_stage_1.definition : this.model === 'startup' ? this.env === 'production' ? startup_definition_1.definition : startup_definition_stage_1.definition : this.env === 'production' ? platform_definition_1.definition : platform_definition_stage_1.definition }); if (options.infura) { this.ipfs = (0, kubo_rpc_client_1.create)({ url: options.infura.url, headers: { authorization: `Basic ${btoa(`${options.infura.projectId}:${options.infura.apiKey}`)}` } }); } } /* ** Connect the user */ connect(address) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { const store = new store_1.Store(); try { const entropy = yield this.provider.request({ method: 'personal_sign', params: [ 'Give this app permission to read or write your private data on ceramic', address ] }); const seed = (0, sha256_1.hash)((0, uint8Array_1.uint8Array)(entropy.slice(2))); let seed_json = JSON.stringify(seed, (key, value) => { if (value instanceof Uint8Array) { return Array.from(value); } return value; }); yield store.setItem('ceramic:did_seed', seed_json); console.log(seed, 'seed'); const did = new dids_1.DID({ resolver: key_did_resolver_1.default.getResolver(), provider: new key_did_provider_ed25519_1.Ed25519Provider(seed) }); const didStr = yield did.authenticate(); console.log(did, 'did'); // set ceramic & composeDB DID this.ceramic.did = did; this.composeClient.setDID(did); // set encryption DID this.encryptionDid = did; this.authenticatedEncryptionDid = didStr; // authenticate nakama if (this.nakamaClient) { try { this.nakamaSession = yield this.nakamaClient.authenticateCustom(address); } catch (e) { // nakama failed } } resolve({ did: didStr, authenticatedEncryptionDid: this.authenticatedEncryptionDid }); } catch (e) { store.removeItem('ceramic:did_seed'); reject(e); } })); }); } /* ** Disconnect the user */ disconnect() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { const store = new store_1.Store(); try { yield store.removeItem('ceramic:did_seed'); resolve(true); } catch (e) { reject(e); } })); }); } /* ** Check the connection status of the user */ isConnected(address) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { yield this.ceramic; const store = new store_1.Store(); const seed_json_value = yield store.getItem('ceramic:did_seed'); if (seed_json_value) { let seed = new Uint8Array(JSON.parse(seed_json_value)); console.log(seed, 'seed'); try { const did = new dids_1.DID({ resolver: key_did_resolver_1.default.getResolver(), provider: new key_did_provider_ed25519_1.Ed25519Provider(seed) }); const didStr = yield did.authenticate(); // connect ceramic this.ceramic.did = did; this.composeClient.setDID(did); // set encryption DID this.encryptionDid = did; this.authenticatedEncryptionDid = didStr; // connect to Nakama try { if (this.nakamaClient) { this.nakamaSession = yield this.nakamaClient.authenticateCustom(address); } } catch (e) { // nakama failed } resolve({ did: didStr, authenticatedEncryptionDid: this.authenticatedEncryptionDid }); } catch (e) { reject(e); } } else { reject('seed not found'); } })); }); } /* ** Create or update profile for signed used */ createOrUpdateProfile(params) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { (() => __awaiter(this, void 0, void 0, function* () { try { const create = yield this.composeClient.executeQuery(` mutation { createProfile(input: { content: { ${Object.keys(params) .filter((x) => x === 'displayName' || x === 'email' || x === 'avatar' || x === 'cover' || x === 'bio' || x === 'accountType' || x === 'age' || x === 'skills' || x === 'gender' || x === 'phoneNumber' || x === 'address' || x === 'socialLinks' || x === 'publicEncryptionDID' || x === 'nakamaID') .map((key) => { if (key === 'skills') { return `${key}: [${params[key] .map((i) => `"${i}"`) .join(',')}]`; } else if (key === 'socialLinks') { return `${key}: [${params[key] .map((i) => `"${i}"`) .join(',')}]`; } else { return `${key}: "${params[key]}"`; } }) .join(',')} } }) { document { creator { id } id displayName email avatar cover bio accountType age skills gender phoneNumber address socialLinks nakamaID publicEncryptionDID { id } } } } `); if (create.errors != null && create.errors.length > 0) { reject(create); } else { switch (this.community) { case 'greenia': resolve(Object.assign(Object.assign({}, create.data.createProfile.document), { id: create.data.createProfile.document.id })); // const createGreenia = await this.composeClient.executeQuery<{ // createGreeniaProfile: { document: GreeniaProfile }; // }>(` // mutation { // createGreeniaProfile(input: { // content: { // profileID: "${create.data.createProfile.document.id}", // ${Object.keys(params) // .filter((x) => x === 'greeniaRelatedProperty') // .map((key) => { // return `${key}: "${params[key]}"`; // }) // .join(',')} // } // }) // { // document { // id // } // } // } // `); // if ( // createGreenia.errors != null && // createGreenia.errors.length > 0 // ) { // reject(createGreenia); // } else { // resolve({ // ...create.data.createProfile.document, // ...createGreenia.data.createGreeniaProfile.document, // id: create.data.createProfile.document.id, // greeniaProfileId: // createGreenia.data.createGreeniaProfile.document.id // } as GreeniaProfile); // } break; default: reject('Wrong community'); } } } catch (e) { reject(e); } }))(); }); }); } /* ** Get profile of signed account */ getProfile() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { (() => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; try { const profile = yield this.composeClient.executeQuery(` query { viewer { profile { creator { id } id displayName email avatar cover bio accountType age skills gender phoneNumber address socialLinks nakamaID publicEncryptionDID { id } experiences(filters: { where: { isDeleted: { equalTo: false } } }, last: 300) { edges { node { creator { id } id city title company endDate startDate description isDeleted } } } educations(filters: { where: { isDeleted: { equalTo: false } } }, last: 300) { edges { node { creator { id } id city title school endDate startDate description isDeleted } } } postsCount(filters: { where: { isDeleted: { equalTo: false } } }) posts(filters: { where: { isDeleted: { equalTo: false } } }, sorting: { createdAt: DESC }, last: 1000) { edges { node { creator { id } id body isDeleted createdAt tag1 tag2 tag3 tag4 tag5 tag6 tag7 tag8 tag9 tag10 attachment externalURL commentsCount(filters: { where: { isDeleted: { equalTo: false } } }) comments(filters: { where: { isDeleted: { equalTo: false } } }, last: 1000) { edges { node { creator { id } id content replyingToID createdAt isDeleted profileID profile { creator { id } id displayName avatar } } } } likesCount(filters: { where: { isDeleted: { equalTo: false } } }) profile { creator { id } id avatar displayName postsCount(filters: { where: { isDeleted: { equalTo: false } } }) followersCount(filters: { where: { isDeleted: { equalTo: false } } }) followingsCount(filters: { where: { isDeleted: { equalTo: false } } }) } } } } articlesCount(filters: { where: { isDeleted: { equalTo: false } } }) articles(filters: { where: { isDeleted: { equalTo: false } } }, sorting: { createdAt: DESC }, last: 1000) { edges { node { creator { id } id abstract visualAbstract body price isDeleted isEncrypted createdAt tag1 tag2 tag3 tag4 tag5 tag6 tag7 tag8 tag9 tag10 attachment externalURL encryptedSymmetricKey unifiedAccessControlConditions commentsCount(filters: { where: { isDeleted: { equalTo: false } } }) comments(filters: { where: { isDeleted: { equalTo: false } } }, last: 1000) { edges { node { creator { id } id content replyingToID createdAt isDeleted profileID profile { creator { id } id displayName avatar } } } } likesCount(filters: { where: { isDeleted: { equalTo: false } } }) profile { creator { id } id avatar displayName postsCount(filters: { where: { isDeleted: { equalTo: false } } }) followersCount(filters: { where: { isDeleted: { equalTo: false } } }) followingsCount(filters: { where: { isDeleted: { equalTo: false } } }) } } } } followersCount(filters: { where: { isDeleted: { equalTo: false } } }) followers(filters: { where: { isDeleted: { equalTo: false } } }, last: 1000) { edges { node { creator { id } id isDeleted profileID profile { creator { id } displayName avatar postsCount(filters: { where: { isDeleted: { equalTo: false } } }) followersCount(filters: { where: { isDeleted: { equalTo: false } } }) followingsCount(filters: { where: { isDeleted: { equalTo: false } } }) } } } } followingsCount(filters: { where: { isDeleted: { equalTo: false } } }) followings(filters: { where: { isDeleted: { equalTo: false } } }, last: 1000) { edges { node { creator { id } id isDeleted targetProfileID targetProfile { creator { id } displayName avatar postsCount(filters: { where: { isDeleted: { equalTo: false } } }) followersCount(filters: { where: { isDeleted: { equalTo: false } } }) followingsCount(filters: { where: { isDeleted: { equalTo: false } } }) } } } } chatsCount(filters: { where: { isDeleted: { equalTo: false } }