getters.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (_) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. exports.__esModule = true;
  39. exports.nextWorkerId = exports.nextOpeningId = exports.providerStatus = exports.proposalDetail = exports.activeProposals = exports.pendingProposals = exports.activeProposalCount = exports.proposalCount = exports.currentCategoryId = exports.currentThreadId = exports.currentPostId = exports.categoryById = exports.memberHandleByAccount = exports.memberIdByAccount = exports.memberHandle = exports.membership = exports.currentChannelId = void 0;
  40. var announcements_1 = require("./announcements");
  41. var node_fetch_1 = require("node-fetch");
  42. // channel
  43. exports.currentChannelId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  44. var id;
  45. return __generator(this, function (_a) {
  46. switch (_a.label) {
  47. case 0: return [4 /*yield*/, api.query.contentWorkingGroup.nextChannelId()];
  48. case 1:
  49. id = _a.sent();
  50. return [2 /*return*/, Number(id) - 1];
  51. }
  52. });
  53. }); };
  54. // members
  55. exports.membership = function (api, id) { return __awaiter(void 0, void 0, void 0, function () {
  56. return __generator(this, function (_a) {
  57. switch (_a.label) {
  58. case 0: return [4 /*yield*/, api.query.members.membershipById(id)];
  59. case 1: return [2 /*return*/, _a.sent()];
  60. }
  61. });
  62. }); };
  63. exports.memberHandle = function (api, id) { return __awaiter(void 0, void 0, void 0, function () {
  64. var member;
  65. return __generator(this, function (_a) {
  66. switch (_a.label) {
  67. case 0: return [4 /*yield*/, exports.membership(api, id)];
  68. case 1:
  69. member = _a.sent();
  70. return [2 /*return*/, member.handle.toJSON()];
  71. }
  72. });
  73. }); };
  74. exports.memberIdByAccount = function (api, account) { return __awaiter(void 0, void 0, void 0, function () {
  75. var ids;
  76. return __generator(this, function (_a) {
  77. switch (_a.label) {
  78. case 0: return [4 /*yield*/, api.query.members.memberIdsByRootAccountId(account)];
  79. case 1:
  80. ids = _a.sent();
  81. return [2 /*return*/, ids.length ? ids[0] : 0];
  82. }
  83. });
  84. }); };
  85. exports.memberHandleByAccount = function (api, account) { return __awaiter(void 0, void 0, void 0, function () {
  86. var id, handle;
  87. return __generator(this, function (_a) {
  88. switch (_a.label) {
  89. case 0: return [4 /*yield*/, api.query.members.memberIdsByRootAccountId(account)];
  90. case 1:
  91. id = _a.sent();
  92. return [4 /*yield*/, exports.memberHandle(api, id)];
  93. case 2:
  94. handle = _a.sent();
  95. return [2 /*return*/, handle === 'joystream_storage_member' ? 'joystream' : handle];
  96. }
  97. });
  98. }); };
  99. // forum
  100. exports.categoryById = function (api, id) { return __awaiter(void 0, void 0, void 0, function () {
  101. var category;
  102. return __generator(this, function (_a) {
  103. switch (_a.label) {
  104. case 0: return [4 /*yield*/, api.query.forum.categoryById(id)];
  105. case 1:
  106. category = _a.sent();
  107. return [2 /*return*/, category];
  108. }
  109. });
  110. }); };
  111. exports.currentPostId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  112. var postId;
  113. return __generator(this, function (_a) {
  114. switch (_a.label) {
  115. case 0: return [4 /*yield*/, api.query.forum.nextPostId()];
  116. case 1:
  117. postId = _a.sent();
  118. return [2 /*return*/, Number(postId) - 1];
  119. }
  120. });
  121. }); };
  122. exports.currentThreadId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  123. var threadId;
  124. return __generator(this, function (_a) {
  125. switch (_a.label) {
  126. case 0: return [4 /*yield*/, api.query.forum.nextThreadId()];
  127. case 1:
  128. threadId = _a.sent();
  129. return [2 /*return*/, Number(threadId) - 1];
  130. }
  131. });
  132. }); };
  133. exports.currentCategoryId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  134. var categoryId;
  135. return __generator(this, function (_a) {
  136. switch (_a.label) {
  137. case 0: return [4 /*yield*/, api.query.forum.nextCategoryId()];
  138. case 1:
  139. categoryId = _a.sent();
  140. return [2 /*return*/, Number(categoryId) - 1];
  141. }
  142. });
  143. }); };
  144. // proposals
  145. exports.proposalCount = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  146. var proposalCount;
  147. return __generator(this, function (_a) {
  148. switch (_a.label) {
  149. case 0: return [4 /*yield*/, api.query.proposalsEngine.proposalCount()];
  150. case 1:
  151. proposalCount = _a.sent();
  152. return [2 /*return*/, proposalCount.toJSON() || 0];
  153. }
  154. });
  155. }); };
  156. exports.activeProposalCount = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  157. var proposalCount;
  158. return __generator(this, function (_a) {
  159. switch (_a.label) {
  160. case 0: return [4 /*yield*/, api.query.proposalsEngine.activeProposalCount()];
  161. case 1:
  162. proposalCount = _a.sent();
  163. return [2 /*return*/, proposalCount || 0];
  164. }
  165. });
  166. }); };
  167. exports.pendingProposals = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  168. var pending, _a, _b;
  169. return __generator(this, function (_c) {
  170. switch (_c.label) {
  171. case 0:
  172. _b = (_a = api.query.proposalsEngine).pendingExecutionProposalIds;
  173. return [4 /*yield*/, exports.activeProposalCount(api)];
  174. case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])
  175. //const pending: ProposalArray = pendingProposals.toJSON();
  176. ];
  177. case 2:
  178. pending = _c.sent();
  179. //const pending: ProposalArray = pendingProposals.toJSON();
  180. if (pending.length)
  181. console.debug('pending proposals', pending);
  182. return [2 /*return*/, pending];
  183. }
  184. });
  185. }); };
  186. exports.activeProposals = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  187. var active, _a, _b;
  188. return __generator(this, function (_c) {
  189. switch (_c.label) {
  190. case 0:
  191. _b = (_a = api.query.proposalsEngine).activeProposalIds;
  192. return [4 /*yield*/, exports.activeProposalCount(api)];
  193. case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])
  194. //const active: ProposalArray = result.toJSON();
  195. ];
  196. case 2:
  197. active = _c.sent();
  198. //const active: ProposalArray = result.toJSON();
  199. if (active.length)
  200. console.debug('active proposals', active);
  201. return [2 /*return*/, active];
  202. }
  203. });
  204. }); };
  205. var getProposalType = function (api, id) { return __awaiter(void 0, void 0, void 0, function () {
  206. var details, type;
  207. return __generator(this, function (_a) {
  208. switch (_a.label) {
  209. case 0: return [4 /*yield*/, api.query.proposalsCodex.proposalDetailsByProposalId(id)];
  210. case 1:
  211. details = _a.sent();
  212. type = Object.getOwnPropertyNames(details.toJSON())[0];
  213. return [2 /*return*/, type];
  214. }
  215. });
  216. }); };
  217. exports.proposalDetail = function (api, id) { return __awaiter(void 0, void 0, void 0, function () {
  218. var proposal, status, stage, _a, finalizedAt, proposalStatus, result, exec, description, parameters, proposerId, votingResults, author, title, type, args, message, createdAt;
  219. return __generator(this, function (_b) {
  220. switch (_b.label) {
  221. case 0: return [4 /*yield*/, api.query.proposalsEngine.proposals(id)];
  222. case 1:
  223. proposal = _b.sent();
  224. status = proposal.status;
  225. stage = status.isActive ? 'Active' : 'Finalized';
  226. _a = status["as" + stage], finalizedAt = _a.finalizedAt, proposalStatus = _a.proposalStatus;
  227. result = proposalStatus
  228. ? (proposalStatus.isApproved && 'Approved') ||
  229. (proposalStatus.isCanceled && 'Canceled') ||
  230. (proposalStatus.isExpired && 'Expired') ||
  231. (proposalStatus.isRejected && 'Rejected') ||
  232. (proposalStatus.isSlashed && 'Slashed') ||
  233. (proposalStatus.isVetoed && 'Vetoed')
  234. : 'Pending';
  235. exec = proposalStatus ? proposalStatus['Approved'] : null;
  236. description = proposal.description, parameters = proposal.parameters, proposerId = proposal.proposerId, votingResults = proposal.votingResults;
  237. return [4 /*yield*/, exports.memberHandle(api, proposerId)];
  238. case 2:
  239. author = _b.sent();
  240. title = proposal.title.toString();
  241. return [4 /*yield*/, getProposalType(api, id)];
  242. case 3:
  243. type = _b.sent();
  244. args = [String(id), title, type, stage, result, author];
  245. message = announcements_1.formatProposalMessage(args);
  246. createdAt = Number(proposal.createdAt);
  247. return [2 /*return*/, {
  248. id: id,
  249. title: title,
  250. createdAt: createdAt,
  251. finalizedAt: finalizedAt,
  252. parameters: JSON.stringify(parameters),
  253. message: message,
  254. stage: stage,
  255. result: result,
  256. exec: exec,
  257. description: description.toHuman(),
  258. votes: votingResults,
  259. type: type,
  260. author: author,
  261. authorId: Number(proposerId)
  262. }];
  263. }
  264. });
  265. }); };
  266. // storage providers
  267. exports.providerStatus = function (domain) { return __awaiter(void 0, void 0, void 0, function () {
  268. var res, e_1;
  269. return __generator(this, function (_a) {
  270. switch (_a.label) {
  271. case 0:
  272. _a.trys.push([0, 2, , 3]);
  273. return [4 /*yield*/, node_fetch_1["default"]("https://" + domain + ":5001/api/v0/version")];
  274. case 1:
  275. res = _a.sent();
  276. return [2 /*return*/, res.status >= 400 ? false : true];
  277. case 2:
  278. e_1 = _a.sent();
  279. return [2 /*return*/, false];
  280. case 3: return [2 /*return*/];
  281. }
  282. });
  283. }); };
  284. exports.nextOpeningId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  285. var id;
  286. return __generator(this, function (_a) {
  287. switch (_a.label) {
  288. case 0: return [4 /*yield*/, api.query.storageWorkingGroup.nextOpeningId()];
  289. case 1:
  290. id = _a.sent();
  291. return [2 /*return*/, id.toJSON()];
  292. }
  293. });
  294. }); };
  295. exports.nextWorkerId = function (api) { return __awaiter(void 0, void 0, void 0, function () {
  296. var id;
  297. return __generator(this, function (_a) {
  298. switch (_a.label) {
  299. case 0: return [4 /*yield*/, api.query.storageWorkingGroup.nextWorkerId()];
  300. case 1:
  301. id = _a.sent();
  302. return [2 /*return*/, id.toJSON()];
  303. }
  304. });
  305. }); };