UNPKG

nem-voting

Version:
120 lines 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const nem_library_1 = require("nem-library"); const rxjs_1 = require("rxjs"); const utils_1 = require("./utils"); const constants_1 = require("./constants"); /** * Contains the info for a poll index, public or private */ class PollIndex { /** * @internal */ constructor(address, isPrivate, headers, creator, lastId) { this.fetchNextPage = () => { return utils_1.getPageOfTransactionsWithString(this.address, 100, "poll:", this.lastId, this.creator) .map((transactions) => { if (transactions.length === 0) { return []; } this.lastId = transactions[transactions.length - 1].getTransactionInfo().id; const headers = transactions.map((transaction) => { try { if (transaction.type !== nem_library_1.TransactionTypes.TRANSFER || !transaction.signer) { return null; } const header = JSON.parse(transaction.message.plain().replace("poll:", "")); return { title: header.title, type: header.type, doe: header.doe, address: new nem_library_1.Address(header.address), creator: transaction.signer.address, whitelist: header.whitelist, }; } catch (err) { return null; } }).filter((h) => h !== null).map((h) => h); this.headers = this.headers.concat(headers); return headers; }); }; this.address = address; this.isPrivate = isPrivate; this.headers = headers; this.creator = creator; this.lastId = lastId; } } /** * Gets a poll index from its address with all of its broadcasted polls * @param address - the index account address * @return Observable<PollIndex> */ PollIndex.fromAddress = (address, lastId) => { let indexObject; let index; let indexMessageObservable; if (address.plain() === new nem_library_1.Address(constants_1.PollConstants.MAINNET_POLL_INDEX).plain() || address.plain() === new nem_library_1.Address(constants_1.PollConstants.TESTNET_POLL_INDEX).plain()) { indexMessageObservable = rxjs_1.Observable.fromPromise(Promise.resolve("pollIndex:{\"isPrivate\":false}")); } else { indexMessageObservable = utils_1.getFirstMessageWithString("pollIndex:", address); } return indexMessageObservable .switchMap((indexMessage) => { indexObject = JSON.parse(indexMessage.replace("pollIndex:", "")); index = (indexObject.isPrivate) ? new PollIndex(address, indexObject.isPrivate, [], new nem_library_1.Address(indexObject.creator), lastId) : new PollIndex(address, indexObject.isPrivate, [], undefined, lastId); return index.fetchNextPage(); }).map((_) => { return index; }); }; /** * Creates a new poll Index * @param isPrivate - will create a private index if true * @param creatorAddress - needed only if the index is private * @return Observable<PollIndex> */ PollIndex.create = (isPrivate, creatorAddress) => { const address = utils_1.generateRandomAddress(); // const ownMessage = "createdPollIndex:" + address.plain(); const obj = { private: isPrivate, }; if (isPrivate) { obj.creator = creatorAddress; } const indexMessage = "pollIndex:" + JSON.stringify(obj); return { address, transaction: utils_1.getMessageTransaction(indexMessage, address), }; }; exports.PollIndex = PollIndex; /** * Gets the addresses for all the poll indexes created by an address * @param creator - the address of the creator of the indexes we want * @return Observable<Address[]> */ const getCreatedIndexAddresses = (creator) => { return utils_1.getTransactionsWithString("createdPollIndex:", creator, creator) .map((transactions) => { return transactions.map((transaction) => { try { const address = transaction.message.plain().replace("createdPollIndex:", ""); return new nem_library_1.Address(address); } catch (err) { return null; } }).filter((h) => h !== null).map((h) => h); }); }; exports.getCreatedIndexAddresses = getCreatedIndexAddresses; //# sourceMappingURL=poll-index.js.map