@bitsocial/bitsocial-react-hooks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +1365 -0
  3. package/dist/hooks/accounts/accounts.d.ts +64 -0
  4. package/dist/hooks/accounts/accounts.d.ts.map +1 -0
  5. package/dist/hooks/accounts/accounts.js +706 -0
  6. package/dist/hooks/accounts/accounts.js.map +1 -0
  7. package/dist/hooks/accounts/index.d.ts +2 -0
  8. package/dist/hooks/accounts/index.d.ts.map +1 -0
  9. package/dist/hooks/accounts/index.js +2 -0
  10. package/dist/hooks/accounts/index.js.map +1 -0
  11. package/dist/hooks/accounts/utils.d.ts +6 -0
  12. package/dist/hooks/accounts/utils.d.ts.map +1 -0
  13. package/dist/hooks/accounts/utils.js +226 -0
  14. package/dist/hooks/accounts/utils.js.map +1 -0
  15. package/dist/hooks/actions/actions.d.ts +19 -0
  16. package/dist/hooks/actions/actions.d.ts.map +1 -0
  17. package/dist/hooks/actions/actions.js +552 -0
  18. package/dist/hooks/actions/actions.js.map +1 -0
  19. package/dist/hooks/actions/index.d.ts +2 -0
  20. package/dist/hooks/actions/index.d.ts.map +1 -0
  21. package/dist/hooks/actions/index.js +2 -0
  22. package/dist/hooks/actions/index.js.map +1 -0
  23. package/dist/hooks/authors/author-avatars.d.ts +28 -0
  24. package/dist/hooks/authors/author-avatars.d.ts.map +1 -0
  25. package/dist/hooks/authors/author-avatars.js +191 -0
  26. package/dist/hooks/authors/author-avatars.js.map +1 -0
  27. package/dist/hooks/authors/authors.d.ts +37 -0
  28. package/dist/hooks/authors/authors.d.ts.map +1 -0
  29. package/dist/hooks/authors/authors.js +509 -0
  30. package/dist/hooks/authors/authors.js.map +1 -0
  31. package/dist/hooks/authors/index.d.ts +2 -0
  32. package/dist/hooks/authors/index.d.ts.map +1 -0
  33. package/dist/hooks/authors/index.js +2 -0
  34. package/dist/hooks/authors/index.js.map +1 -0
  35. package/dist/hooks/authors/utils.d.ts +4 -0
  36. package/dist/hooks/authors/utils.d.ts.map +1 -0
  37. package/dist/hooks/authors/utils.js +21 -0
  38. package/dist/hooks/authors/utils.js.map +1 -0
  39. package/dist/hooks/comments.d.ts +17 -0
  40. package/dist/hooks/comments.d.ts.map +1 -0
  41. package/dist/hooks/comments.js +351 -0
  42. package/dist/hooks/comments.js.map +1 -0
  43. package/dist/hooks/communities.d.ts +31 -0
  44. package/dist/hooks/communities.d.ts.map +1 -0
  45. package/dist/hooks/communities.js +389 -0
  46. package/dist/hooks/communities.js.map +1 -0
  47. package/dist/hooks/feeds/feeds.d.ts +18 -0
  48. package/dist/hooks/feeds/feeds.d.ts.map +1 -0
  49. package/dist/hooks/feeds/feeds.js +315 -0
  50. package/dist/hooks/feeds/feeds.js.map +1 -0
  51. package/dist/hooks/feeds/index.d.ts +2 -0
  52. package/dist/hooks/feeds/index.d.ts.map +1 -0
  53. package/dist/hooks/feeds/index.js +2 -0
  54. package/dist/hooks/feeds/index.js.map +1 -0
  55. package/dist/hooks/pkc-rpc.d.ts +7 -0
  56. package/dist/hooks/pkc-rpc.d.ts.map +1 -0
  57. package/dist/hooks/pkc-rpc.js +88 -0
  58. package/dist/hooks/pkc-rpc.js.map +1 -0
  59. package/dist/hooks/replies.d.ts +5 -0
  60. package/dist/hooks/replies.d.ts.map +1 -0
  61. package/dist/hooks/replies.js +155 -0
  62. package/dist/hooks/replies.js.map +1 -0
  63. package/dist/hooks/states.d.ts +15 -0
  64. package/dist/hooks/states.d.ts.map +1 -0
  65. package/dist/hooks/states.js +213 -0
  66. package/dist/hooks/states.js.map +1 -0
  67. package/dist/hooks/utils/use-interval.d.ts +3 -0
  68. package/dist/hooks/utils/use-interval.d.ts.map +1 -0
  69. package/dist/hooks/utils/use-interval.js +36 -0
  70. package/dist/hooks/utils/use-interval.js.map +1 -0
  71. package/dist/hooks/utils/use-previous.d.ts +1 -0
  72. package/dist/hooks/utils/use-previous.js +10 -0
  73. package/dist/index.d.ts +82 -0
  74. package/dist/index.d.ts.map +1 -0
  75. package/dist/index.js +128 -0
  76. package/dist/index.js.map +1 -0
  77. package/dist/lib/chain/chain.d.ts +36 -0
  78. package/dist/lib/chain/chain.d.ts.map +1 -0
  79. package/dist/lib/chain/chain.js +195 -0
  80. package/dist/lib/chain/chain.js.map +1 -0
  81. package/dist/lib/chain/index.d.ts +4 -0
  82. package/dist/lib/chain/index.d.ts.map +1 -0
  83. package/dist/lib/chain/index.js +4 -0
  84. package/dist/lib/chain/index.js.map +1 -0
  85. package/dist/lib/community-address.d.ts +6 -0
  86. package/dist/lib/community-address.d.ts.map +1 -0
  87. package/dist/lib/community-address.js +26 -0
  88. package/dist/lib/community-address.js.map +1 -0
  89. package/dist/lib/community-ref.d.ts +23 -0
  90. package/dist/lib/community-ref.d.ts.map +1 -0
  91. package/dist/lib/community-ref.js +113 -0
  92. package/dist/lib/community-ref.js.map +1 -0
  93. package/dist/lib/debug-utils.d.ts +9 -0
  94. package/dist/lib/debug-utils.d.ts.map +1 -0
  95. package/dist/lib/debug-utils.js +21 -0
  96. package/dist/lib/debug-utils.js.map +1 -0
  97. package/dist/lib/feed-sort-type.d.ts +2 -0
  98. package/dist/lib/feed-sort-type.d.ts.map +1 -0
  99. package/dist/lib/feed-sort-type.js +22 -0
  100. package/dist/lib/feed-sort-type.js.map +1 -0
  101. package/dist/lib/localforage-lru/index.d.ts +3 -0
  102. package/dist/lib/localforage-lru/index.d.ts.map +1 -0
  103. package/dist/lib/localforage-lru/index.js +46 -0
  104. package/dist/lib/localforage-lru/index.js.map +1 -0
  105. package/dist/lib/localforage-lru/localforage-lru.d.ts +6 -0
  106. package/dist/lib/localforage-lru/localforage-lru.d.ts.map +1 -0
  107. package/dist/lib/localforage-lru/localforage-lru.js +182 -0
  108. package/dist/lib/localforage-lru/localforage-lru.js.map +1 -0
  109. package/dist/lib/pkc-compat.d.ts +25 -0
  110. package/dist/lib/pkc-compat.d.ts.map +1 -0
  111. package/dist/lib/pkc-compat.js +131 -0
  112. package/dist/lib/pkc-compat.js.map +1 -0
  113. package/dist/lib/pkc-js/fixtures/markdown-example.d.ts +3 -0
  114. package/dist/lib/pkc-js/fixtures/markdown-example.d.ts.map +1 -0
  115. package/dist/lib/pkc-js/fixtures/markdown-example.js +280 -0
  116. package/dist/lib/pkc-js/fixtures/markdown-example.js.map +1 -0
  117. package/dist/lib/pkc-js/index.d.ts +11 -0
  118. package/dist/lib/pkc-js/index.d.ts.map +1 -0
  119. package/dist/lib/pkc-js/index.js +85 -0
  120. package/dist/lib/pkc-js/index.js.map +1 -0
  121. package/dist/lib/pkc-js/pkc-js-mock-content.d.ts +3 -0
  122. package/dist/lib/pkc-js/pkc-js-mock-content.d.ts.map +1 -0
  123. package/dist/lib/pkc-js/pkc-js-mock-content.js +1235 -0
  124. package/dist/lib/pkc-js/pkc-js-mock-content.js.map +1 -0
  125. package/dist/lib/pkc-js/pkc-js-mock.d.ts +137 -0
  126. package/dist/lib/pkc-js/pkc-js-mock.d.ts.map +1 -0
  127. package/dist/lib/pkc-js/pkc-js-mock.js +644 -0
  128. package/dist/lib/pkc-js/pkc-js-mock.js.map +1 -0
  129. package/dist/lib/polyfill.d.ts +3 -0
  130. package/dist/lib/polyfill.d.ts.map +1 -0
  131. package/dist/lib/polyfill.js +14 -0
  132. package/dist/lib/polyfill.js.map +1 -0
  133. package/dist/lib/protocol-compat.d.ts +14 -0
  134. package/dist/lib/protocol-compat.d.ts.map +1 -0
  135. package/dist/lib/protocol-compat.js +67 -0
  136. package/dist/lib/protocol-compat.js.map +1 -0
  137. package/dist/lib/test-utils.d.ts +29 -0
  138. package/dist/lib/test-utils.d.ts.map +1 -0
  139. package/dist/lib/test-utils.js +184 -0
  140. package/dist/lib/test-utils.js.map +1 -0
  141. package/dist/lib/utils/comment-moderation.d.ts +4 -0
  142. package/dist/lib/utils/comment-moderation.d.ts.map +1 -0
  143. package/dist/lib/utils/comment-moderation.js +56 -0
  144. package/dist/lib/utils/comment-moderation.js.map +1 -0
  145. package/dist/lib/utils/index.d.ts +4 -0
  146. package/dist/lib/utils/index.d.ts.map +1 -0
  147. package/dist/lib/utils/index.js +4 -0
  148. package/dist/lib/utils/index.js.map +1 -0
  149. package/dist/lib/utils/utils.d.ts +23 -0
  150. package/dist/lib/utils/utils.d.ts.map +1 -0
  151. package/dist/lib/utils/utils.js +375 -0
  152. package/dist/lib/utils/utils.js.map +1 -0
  153. package/dist/lib/validator.d.ts +30 -0
  154. package/dist/lib/validator.d.ts.map +1 -0
  155. package/dist/lib/validator.js +307 -0
  156. package/dist/lib/validator.js.map +1 -0
  157. package/dist/stores/accounts/account-generator.d.ts +51 -0
  158. package/dist/stores/accounts/account-generator.d.ts.map +1 -0
  159. package/dist/stores/accounts/account-generator.js +160 -0
  160. package/dist/stores/accounts/account-generator.js.map +1 -0
  161. package/dist/stores/accounts/accounts-actions-internal.d.ts +8 -0
  162. package/dist/stores/accounts/accounts-actions-internal.d.ts.map +1 -0
  163. package/dist/stores/accounts/accounts-actions-internal.js +403 -0
  164. package/dist/stores/accounts/accounts-actions-internal.js.map +1 -0
  165. package/dist/stores/accounts/accounts-actions.d.ts +46 -0
  166. package/dist/stores/accounts/accounts-actions.d.ts.map +1 -0
  167. package/dist/stores/accounts/accounts-actions.js +1341 -0
  168. package/dist/stores/accounts/accounts-actions.js.map +1 -0
  169. package/dist/stores/accounts/accounts-database.d.ts +34 -0
  170. package/dist/stores/accounts/accounts-database.d.ts.map +1 -0
  171. package/dist/stores/accounts/accounts-database.js +685 -0
  172. package/dist/stores/accounts/accounts-database.js.map +1 -0
  173. package/dist/stores/accounts/accounts-store.d.ts +32 -0
  174. package/dist/stores/accounts/accounts-store.d.ts.map +1 -0
  175. package/dist/stores/accounts/accounts-store.js +169 -0
  176. package/dist/stores/accounts/accounts-store.js.map +1 -0
  177. package/dist/stores/accounts/index.d.ts +4 -0
  178. package/dist/stores/accounts/index.d.ts.map +1 -0
  179. package/dist/stores/accounts/index.js +4 -0
  180. package/dist/stores/accounts/index.js.map +1 -0
  181. package/dist/stores/accounts/utils.d.ts +49 -0
  182. package/dist/stores/accounts/utils.d.ts.map +1 -0
  183. package/dist/stores/accounts/utils.js +419 -0
  184. package/dist/stores/accounts/utils.js.map +1 -0
  185. package/dist/stores/authors-comments/authors-comments-store.d.ts +37 -0
  186. package/dist/stores/authors-comments/authors-comments-store.d.ts.map +1 -0
  187. package/dist/stores/authors-comments/authors-comments-store.js +338 -0
  188. package/dist/stores/authors-comments/authors-comments-store.js.map +1 -0
  189. package/dist/stores/authors-comments/index.d.ts +4 -0
  190. package/dist/stores/authors-comments/index.d.ts.map +1 -0
  191. package/dist/stores/authors-comments/index.js +4 -0
  192. package/dist/stores/authors-comments/index.js.map +1 -0
  193. package/dist/stores/authors-comments/utils.d.ts +14 -0
  194. package/dist/stores/authors-comments/utils.d.ts.map +1 -0
  195. package/dist/stores/authors-comments/utils.js +81 -0
  196. package/dist/stores/authors-comments/utils.js.map +1 -0
  197. package/dist/stores/comments/comments-store.d.ts +19 -0
  198. package/dist/stores/comments/comments-store.d.ts.map +1 -0
  199. package/dist/stores/comments/comments-store.js +385 -0
  200. package/dist/stores/comments/comments-store.js.map +1 -0
  201. package/dist/stores/comments/index.d.ts +4 -0
  202. package/dist/stores/comments/index.d.ts.map +1 -0
  203. package/dist/stores/comments/index.js +4 -0
  204. package/dist/stores/comments/index.js.map +1 -0
  205. package/dist/stores/communities/communities-store.d.ts +17 -0
  206. package/dist/stores/communities/communities-store.d.ts.map +1 -0
  207. package/dist/stores/communities/communities-store.js +304 -0
  208. package/dist/stores/communities/communities-store.js.map +1 -0
  209. package/dist/stores/communities/index.d.ts +4 -0
  210. package/dist/stores/communities/index.d.ts.map +1 -0
  211. package/dist/stores/communities/index.js +4 -0
  212. package/dist/stores/communities/index.js.map +1 -0
  213. package/dist/stores/communities-pages/communities-pages-store.d.ts +23 -0
  214. package/dist/stores/communities-pages/communities-pages-store.d.ts.map +1 -0
  215. package/dist/stores/communities-pages/communities-pages-store.js +316 -0
  216. package/dist/stores/communities-pages/communities-pages-store.js.map +1 -0
  217. package/dist/stores/communities-pages/index.d.ts +4 -0
  218. package/dist/stores/communities-pages/index.d.ts.map +1 -0
  219. package/dist/stores/communities-pages/index.js +4 -0
  220. package/dist/stores/communities-pages/index.js.map +1 -0
  221. package/dist/stores/feeds/feed-sorter.d.ts +5 -0
  222. package/dist/stores/feeds/feed-sorter.d.ts.map +1 -0
  223. package/dist/stores/feeds/feed-sorter.js +135 -0
  224. package/dist/stores/feeds/feed-sorter.js.map +1 -0
  225. package/dist/stores/feeds/feeds-store.d.ts +25 -0
  226. package/dist/stores/feeds/feeds-store.d.ts.map +1 -0
  227. package/dist/stores/feeds/feeds-store.js +459 -0
  228. package/dist/stores/feeds/feeds-store.js.map +1 -0
  229. package/dist/stores/feeds/index.d.ts +4 -0
  230. package/dist/stores/feeds/index.d.ts.map +1 -0
  231. package/dist/stores/feeds/index.js +4 -0
  232. package/dist/stores/feeds/index.js.map +1 -0
  233. package/dist/stores/feeds/utils.d.ts +43 -0
  234. package/dist/stores/feeds/utils.d.ts.map +1 -0
  235. package/dist/stores/feeds/utils.js +736 -0
  236. package/dist/stores/feeds/utils.js.map +1 -0
  237. package/dist/stores/replies/index.d.ts +4 -0
  238. package/dist/stores/replies/index.d.ts.map +1 -0
  239. package/dist/stores/replies/index.js +4 -0
  240. package/dist/stores/replies/index.js.map +1 -0
  241. package/dist/stores/replies/replies-comments-store.d.ts +8 -0
  242. package/dist/stores/replies/replies-comments-store.d.ts.map +1 -0
  243. package/dist/stores/replies/replies-comments-store.js +23 -0
  244. package/dist/stores/replies/replies-comments-store.js.map +1 -0
  245. package/dist/stores/replies/replies-store.d.ts +29 -0
  246. package/dist/stores/replies/replies-store.d.ts.map +1 -0
  247. package/dist/stores/replies/replies-store.js +413 -0
  248. package/dist/stores/replies/replies-store.js.map +1 -0
  249. package/dist/stores/replies/utils.d.ts +25 -0
  250. package/dist/stores/replies/utils.d.ts.map +1 -0
  251. package/dist/stores/replies/utils.js +549 -0
  252. package/dist/stores/replies/utils.js.map +1 -0
  253. package/dist/stores/replies-pages/index.d.ts +4 -0
  254. package/dist/stores/replies-pages/index.d.ts.map +1 -0
  255. package/dist/stores/replies-pages/index.js +4 -0
  256. package/dist/stores/replies-pages/index.js.map +1 -0
  257. package/dist/stores/replies-pages/replies-pages-store.d.ts +20 -0
  258. package/dist/stores/replies-pages/replies-pages-store.d.ts.map +1 -0
  259. package/dist/stores/replies-pages/replies-pages-store.js +270 -0
  260. package/dist/stores/replies-pages/replies-pages-store.js.map +1 -0
  261. package/dist/stores/replies-pages/utils.d.ts +3 -0
  262. package/dist/stores/replies-pages/utils.d.ts.map +1 -0
  263. package/dist/stores/replies-pages/utils.js +43 -0
  264. package/dist/stores/replies-pages/utils.js.map +1 -0
  265. package/dist/types.d.ts +638 -0
  266. package/dist/types.d.ts.map +1 -0
  267. package/dist/types.js +3 -0
  268. package/dist/types.js.map +1 -0
  269. package/package.json +160 -0
@@ -0,0 +1,644 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import EventEmitter from "events";
11
+ const loadingTime = 10;
12
+ export const simulateLoadingTime = () => new Promise((r) => setTimeout(r, loadingTime));
13
+ // keep a list of created and edited owner communities
14
+ // to reinitialize them with pkc.createCommunity()
15
+ let createdOwnerCommunities = {};
16
+ let editedOwnerCommunities = {};
17
+ // reset the pkc-js global state in between tests
18
+ export const resetPkcJsMock = () => {
19
+ createdOwnerCommunities = {};
20
+ editedOwnerCommunities = {};
21
+ };
22
+ export const debugPkcJsMock = () => {
23
+ console.log({ createdOwnerCommunities, editedOwnerCommunities });
24
+ };
25
+ class NameResolverClient extends EventEmitter {
26
+ constructor() {
27
+ super(...arguments);
28
+ this.state = "stopped";
29
+ }
30
+ }
31
+ export class PKC extends EventEmitter {
32
+ constructor(options = {}) {
33
+ super();
34
+ this.clients = (() => {
35
+ const pkcRpcClients = {
36
+ "http://localhost:9138": new PkcRpcClient(),
37
+ };
38
+ return {
39
+ pkcRpcClients,
40
+ };
41
+ })();
42
+ this.nameResolvers = (options === null || options === void 0 ? void 0 : options.nameResolvers) || [];
43
+ this._clientsManager = {
44
+ clients: {
45
+ nameResolvers: this.nameResolvers.reduce((resolverClients, resolver) => (Object.assign(Object.assign({}, resolverClients), { [resolver.key]: new NameResolverClient() })), {}),
46
+ },
47
+ };
48
+ }
49
+ resolveAuthorAddress(options) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const resolver = this.nameResolvers.find((nameResolver) => { var _a; return (_a = nameResolver === null || nameResolver === void 0 ? void 0 : nameResolver.canResolve) === null || _a === void 0 ? void 0 : _a.call(nameResolver, { name: options.address }); });
52
+ const resolverClient = resolver && this._clientsManager.clients.nameResolvers[resolver.key];
53
+ if (resolverClient) {
54
+ resolverClient.state = "resolving-author-name";
55
+ resolverClient.emit("statechange", resolverClient.state);
56
+ }
57
+ yield simulateLoadingTime();
58
+ if (resolverClient) {
59
+ resolverClient.state = "stopped";
60
+ resolverClient.emit("statechange", resolverClient.state);
61
+ }
62
+ return "resolved author address";
63
+ });
64
+ }
65
+ resolveAuthorName(options) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ return this.resolveAuthorAddress(options);
68
+ });
69
+ }
70
+ createSigner() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ return {
73
+ privateKey: "private key",
74
+ address: "address",
75
+ };
76
+ });
77
+ }
78
+ createCommunity(createCommunityOptions) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ if (!createCommunityOptions) {
81
+ createCommunityOptions = {};
82
+ }
83
+ const communityIdentifier = createCommunityOptions.address ||
84
+ createCommunityOptions.name ||
85
+ createCommunityOptions.publicKey;
86
+ // no address provided so probably a user creating an owner community
87
+ if (!communityIdentifier) {
88
+ createCommunityOptions = Object.assign(Object.assign({}, createCommunityOptions), { address: "created community address" });
89
+ // createdCommunityAddresses.push('created community address')
90
+ createdOwnerCommunities[createCommunityOptions.address] = Object.assign({}, createCommunityOptions);
91
+ }
92
+ // only address provided, so could be a previously created owner community
93
+ // add props from previously created sub
94
+ else if (createdOwnerCommunities[createCommunityOptions.address] &&
95
+ JSON.stringify(Object.keys(createCommunityOptions)) === '["address"]') {
96
+ for (const prop in createdOwnerCommunities[createCommunityOptions.address]) {
97
+ if (createdOwnerCommunities[createCommunityOptions.address][prop]) {
98
+ createCommunityOptions[prop] =
99
+ createdOwnerCommunities[createCommunityOptions.address][prop];
100
+ }
101
+ }
102
+ }
103
+ // add edited props if owner community was edited in the past
104
+ if (editedOwnerCommunities[createCommunityOptions.address]) {
105
+ for (const prop in editedOwnerCommunities[createCommunityOptions.address]) {
106
+ if (editedOwnerCommunities[createCommunityOptions.address][prop]) {
107
+ createCommunityOptions[prop] =
108
+ editedOwnerCommunities[createCommunityOptions.address][prop];
109
+ }
110
+ }
111
+ }
112
+ return new Community(createCommunityOptions);
113
+ });
114
+ }
115
+ getCommunity(options) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const address = options === null || options === void 0 ? void 0 : options.address;
118
+ yield simulateLoadingTime();
119
+ const createCommunityOptions = { address };
120
+ const community = new Community(createCommunityOptions);
121
+ community.title = community.address + " title";
122
+ const hotPageCid = community.address + " page cid hot";
123
+ community.posts.pages.hot = community.posts.pageToGet(hotPageCid);
124
+ community.posts.pageCids = {
125
+ hot: hotPageCid,
126
+ topAll: community.address + " page cid topAll",
127
+ new: community.address + " page cid new",
128
+ active: community.address + " page cid active",
129
+ };
130
+ community.modQueue.pageCids = {
131
+ pendingApproval: community.address + " page cid pendingApproval",
132
+ };
133
+ return community;
134
+ });
135
+ }
136
+ // TODO: implement event communitieschange
137
+ get communities() {
138
+ return [
139
+ ...new Set([
140
+ "list community address 1",
141
+ "list community address 2",
142
+ ...Object.keys(createdOwnerCommunities),
143
+ ]),
144
+ ];
145
+ }
146
+ createComment(createCommentOptions) {
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ return new Comment(createCommentOptions);
149
+ });
150
+ }
151
+ getComment(options) {
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ const cid = options === null || options === void 0 ? void 0 : options.cid;
154
+ yield simulateLoadingTime();
155
+ const createCommentOptions = Object.assign({ cid,
156
+ // useComment() requires timestamp or will use account comment instead of comment from store
157
+ timestamp: 1670000000 }, this.commentToGet(cid));
158
+ return new Comment(createCommentOptions);
159
+ });
160
+ }
161
+ // mock this method to get a comment with different content, timestamp, address, etc
162
+ commentToGet(commentCid) {
163
+ return {
164
+ // content: 'mock some content'
165
+ // author: {address: 'mock some address'},
166
+ // timestamp: 1234
167
+ };
168
+ }
169
+ createVote() {
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ return new Vote();
172
+ });
173
+ }
174
+ createCommentEdit(createCommentEditOptions) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ return new CommentEdit(createCommentEditOptions);
177
+ });
178
+ }
179
+ createCommentModeration(createCommentModerationOptions) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ return new CommentModeration(createCommentModerationOptions);
182
+ });
183
+ }
184
+ createCommunityEdit(createCommunityEditOptions) {
185
+ return __awaiter(this, void 0, void 0, function* () {
186
+ return new CommunityEdit(createCommunityEditOptions);
187
+ });
188
+ }
189
+ fetchCid(options) {
190
+ return __awaiter(this, void 0, void 0, function* () {
191
+ const cid = options === null || options === void 0 ? void 0 : options.cid;
192
+ if (cid === null || cid === void 0 ? void 0 : cid.startsWith("statscid")) {
193
+ return JSON.stringify({ hourActiveUserCount: 1 });
194
+ }
195
+ throw Error(`pkc.fetchCid not implemented in pkc-js mock for cid '${cid}'`);
196
+ });
197
+ }
198
+ pubsubSubscribe(communityAddress) {
199
+ return __awaiter(this, void 0, void 0, function* () { });
200
+ }
201
+ pubsubUnsubscribe(communityAddress) {
202
+ return __awaiter(this, void 0, void 0, function* () { });
203
+ }
204
+ validateComment(comment, validateCommentOptions) {
205
+ return __awaiter(this, void 0, void 0, function* () { });
206
+ }
207
+ }
208
+ class PkcRpcClient extends EventEmitter {
209
+ constructor() {
210
+ super();
211
+ this.state = "connecting";
212
+ this.settings = undefined;
213
+ // simulate connecting to the rpc
214
+ setTimeout(() => {
215
+ this.state = "connected";
216
+ this.settings = { challenges: {} };
217
+ this.emit("statechange", this.state);
218
+ this.emit("settingschange", this.settings);
219
+ }, 10);
220
+ }
221
+ setSettings(settings) {
222
+ return __awaiter(this, void 0, void 0, function* () {
223
+ this.settings = settings;
224
+ this.emit("settingschange", this.settings);
225
+ });
226
+ }
227
+ }
228
+ export class Pages {
229
+ constructor(pagesOptions) {
230
+ this.pageCids = {};
231
+ this.pages = {};
232
+ Object.defineProperty(this, "community", {
233
+ value: pagesOptions === null || pagesOptions === void 0 ? void 0 : pagesOptions.community,
234
+ enumerable: false,
235
+ });
236
+ Object.defineProperty(this, "comment", { value: pagesOptions === null || pagesOptions === void 0 ? void 0 : pagesOptions.comment, enumerable: false });
237
+ }
238
+ getPage(options) {
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ const cid = options === null || options === void 0 ? void 0 : options.cid;
241
+ // need to wait twice otherwise react renders too fast and fetches too many pages in advance
242
+ yield simulateLoadingTime();
243
+ return this.pageToGet(cid);
244
+ });
245
+ }
246
+ validatePage(page) {
247
+ return __awaiter(this, void 0, void 0, function* () { });
248
+ }
249
+ // mock this method to get pages with different content, or use to getPage without simulated loading time
250
+ pageToGet(pageCid) {
251
+ var _a, _b;
252
+ const communityAddress = ((_a = this.community) === null || _a === void 0 ? void 0 : _a.address) || ((_b = this.comment) === null || _b === void 0 ? void 0 : _b.communityAddress);
253
+ const isPendingApprovalPage = pageCid.includes("pendingApproval");
254
+ const page = {
255
+ nextCid: communityAddress + " " + pageCid + " - next page cid",
256
+ comments: [],
257
+ };
258
+ const postCount = 100;
259
+ let index = 0;
260
+ while (index++ < postCount) {
261
+ const comment = {
262
+ timestamp: index,
263
+ cid: pageCid + " comment cid " + index,
264
+ communityAddress,
265
+ upvoteCount: index,
266
+ downvoteCount: 10,
267
+ author: {
268
+ address: pageCid + " author address " + index,
269
+ },
270
+ updatedAt: index,
271
+ };
272
+ if (isPendingApprovalPage) {
273
+ comment.pendingApproval = true;
274
+ }
275
+ page.comments.push(comment);
276
+ }
277
+ return page;
278
+ }
279
+ }
280
+ export class Community extends EventEmitter {
281
+ constructor(createCommunityOptions) {
282
+ var _a, _b, _c, _d, _e, _f;
283
+ super();
284
+ this.updateCalledTimes = 0;
285
+ this.updating = false;
286
+ this.firstUpdate = true;
287
+ this.address =
288
+ (createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.address) ||
289
+ (createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.name) ||
290
+ (createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.publicKey);
291
+ this.title = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.title;
292
+ this.description = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.description;
293
+ this.statsCid = "statscid";
294
+ this.state = "stopped";
295
+ this.updatingState = "stopped";
296
+ this.updatedAt = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.updatedAt;
297
+ this.posts = new Pages({ community: this });
298
+ // add community.posts from createCommunityOptions
299
+ if ((_a = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.posts) === null || _a === void 0 ? void 0 : _a.pages) {
300
+ this.posts.pages = (_b = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.posts) === null || _b === void 0 ? void 0 : _b.pages;
301
+ }
302
+ if ((_c = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.posts) === null || _c === void 0 ? void 0 : _c.pageCids) {
303
+ this.posts.pageCids = (_d = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.posts) === null || _d === void 0 ? void 0 : _d.pageCids;
304
+ }
305
+ this.modQueue = new Pages({ community: this });
306
+ // add community.modQueue from createCommunityOptions
307
+ if ((_e = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.modQueue) === null || _e === void 0 ? void 0 : _e.pageCids) {
308
+ this.modQueue.pageCids = (_f = createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.modQueue) === null || _f === void 0 ? void 0 : _f.pageCids;
309
+ }
310
+ if (createCommunityOptions) {
311
+ for (const prop in createCommunityOptions) {
312
+ if (createCommunityOptions[prop] !== undefined) {
313
+ const descriptor = Object.getOwnPropertyDescriptor(this, prop) ||
314
+ Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), prop);
315
+ if (descriptor && !descriptor.writable && !descriptor.set) {
316
+ continue;
317
+ }
318
+ // @ts-ignore
319
+ this[prop] = createCommunityOptions[prop];
320
+ }
321
+ }
322
+ }
323
+ const lookupKeys = ["address", "name", "publicKey"];
324
+ const createCommunityOptionKeys = Object.keys(createCommunityOptions || {});
325
+ const hasLookupIdentifier = Boolean((createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.address) ||
326
+ (createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.name) ||
327
+ (createCommunityOptions === null || createCommunityOptions === void 0 ? void 0 : createCommunityOptions.publicKey));
328
+ const lookupOnly = hasLookupIdentifier &&
329
+ createCommunityOptionKeys.length > 0 &&
330
+ createCommunityOptionKeys.every((key) => lookupKeys.includes(key));
331
+ // only trigger a first update for lookup-only remote community instances
332
+ if (!lookupOnly) {
333
+ this.firstUpdate = false;
334
+ }
335
+ }
336
+ update() {
337
+ return __awaiter(this, void 0, void 0, function* () {
338
+ this.updateCalledTimes++;
339
+ if (this.updateCalledTimes > 1) {
340
+ throw Error("with the current hooks, community.update() should be called maximum 1 times, this number might change if the hooks change and is only there to catch bugs, the real comment.update() can be called infinite times");
341
+ }
342
+ if (!this.address) {
343
+ throw Error(`can't update without community.address`);
344
+ }
345
+ // don't update twice
346
+ if (this.updating) {
347
+ return;
348
+ }
349
+ this.updating = true;
350
+ this.state = "updating";
351
+ this.updatingState = "fetching-ipns";
352
+ this.emit("statechange", "updating");
353
+ this.emit("updatingstatechange", "fetching-ipns");
354
+ simulateLoadingTime().then(() => {
355
+ this.simulateUpdateEvent();
356
+ });
357
+ });
358
+ }
359
+ delete() {
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ if (this.address) {
362
+ delete createdOwnerCommunities[this.address];
363
+ delete editedOwnerCommunities[this.address];
364
+ }
365
+ });
366
+ }
367
+ simulateUpdateEvent() {
368
+ if (this.firstUpdate) {
369
+ this.simulateFirstUpdateEvent();
370
+ return;
371
+ }
372
+ this.description = this.address + " description updated";
373
+ // @ts-ignore
374
+ this.updatedAt = this.updatedAt + 1;
375
+ this.updatingState = "succeeded";
376
+ this.emit("update", this);
377
+ this.emit("updatingstatechange", "succeeded");
378
+ }
379
+ // the first update event adds all the field from getCommunity
380
+ simulateFirstUpdateEvent() {
381
+ return __awaiter(this, void 0, void 0, function* () {
382
+ this.firstUpdate = false;
383
+ this.updatedAt = Math.floor(Date.now() / 1000);
384
+ this.title = this.address + " title";
385
+ const hotPageCid = this.address + " page cid hot";
386
+ this.posts.pages.hot = this.posts.pageToGet(hotPageCid);
387
+ this.posts.pageCids = {
388
+ hot: hotPageCid,
389
+ topAll: this.address + " page cid topAll",
390
+ new: this.address + " page cid new",
391
+ active: this.address + " page cid active",
392
+ };
393
+ this.modQueue.pageCids = {
394
+ pendingApproval: this.address + " page cid pendingApproval",
395
+ };
396
+ // simulate the ipns update
397
+ this.updatingState = "succeeded";
398
+ this.emit("update", this);
399
+ this.emit("updatingstatechange", "succeeded");
400
+ // simulate the next update
401
+ this.updatingState = "fetching-ipns";
402
+ this.emit("updatingstatechange", "fetching-ipns");
403
+ simulateLoadingTime().then(() => {
404
+ this.simulateUpdateEvent();
405
+ });
406
+ });
407
+ }
408
+ // use getting to easily mock it
409
+ get roles() {
410
+ return this.rolesToGet();
411
+ }
412
+ // mock this method to get different roles
413
+ rolesToGet() {
414
+ return {};
415
+ }
416
+ edit(editCommunityOptions) {
417
+ return __awaiter(this, void 0, void 0, function* () {
418
+ if (!this.address || typeof this.address !== "string") {
419
+ throw Error(`can't community.edit with no community.address`);
420
+ }
421
+ const previousAddress = this.address;
422
+ // do community.edit
423
+ for (const prop in editCommunityOptions) {
424
+ if (editCommunityOptions[prop] !== undefined) {
425
+ const descriptor = Object.getOwnPropertyDescriptor(this, prop) ||
426
+ Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), prop);
427
+ if (descriptor && !descriptor.writable && !descriptor.set) {
428
+ continue;
429
+ }
430
+ // @ts-ignore
431
+ this[prop] = editCommunityOptions[prop];
432
+ }
433
+ }
434
+ // keep a list of edited communities to reinitialize
435
+ // them with pkc.createCommunity()
436
+ editedOwnerCommunities[this.address] = {
437
+ address: this.address,
438
+ title: this.title,
439
+ description: this.description,
440
+ };
441
+ // handle change of community.address
442
+ if (editCommunityOptions.address) {
443
+ // apply address change to editedOwnerCommunities
444
+ editedOwnerCommunities[previousAddress] = {
445
+ address: this.address,
446
+ title: this.title,
447
+ description: this.description,
448
+ };
449
+ delete editedOwnerCommunities[previousAddress];
450
+ // apply address change to createdOwnerCommunities
451
+ createdOwnerCommunities[this.address] = Object.assign(Object.assign({}, createdOwnerCommunities[previousAddress]), { address: this.address });
452
+ delete createdOwnerCommunities[previousAddress];
453
+ }
454
+ });
455
+ }
456
+ }
457
+ // make roles enumarable so it acts like a regular prop
458
+ Object.defineProperty(Community.prototype, "roles", { enumerable: true });
459
+ let challengeRequestCount = 0;
460
+ let challengeAnswerCount = 0;
461
+ class Publication extends EventEmitter {
462
+ constructor() {
463
+ super(...arguments);
464
+ this.challengeRequestId = `r${++challengeRequestCount}`;
465
+ this.challengeAnswerId = `a${++challengeAnswerCount}`;
466
+ }
467
+ publish() {
468
+ return __awaiter(this, void 0, void 0, function* () {
469
+ this.state = "publishing";
470
+ this.publishingState = "publishing-challenge-request";
471
+ this.emit("statechange", "publishing");
472
+ this.emit("publishingstatechange", "publishing-challenge-request");
473
+ yield simulateLoadingTime();
474
+ this.simulateChallengeEvent();
475
+ });
476
+ }
477
+ simulateChallengeEvent() {
478
+ this.publishingState = "waiting-challenge-answers";
479
+ this.emit("publishingstatechange", "waiting-challenge-answers");
480
+ const challenge = { type: "text", challenge: "2+2=?" };
481
+ const challengeMessage = {
482
+ type: "CHALLENGE",
483
+ challengeRequestId: this.challengeRequestId,
484
+ challenges: [challenge],
485
+ };
486
+ this.emit("challenge", challengeMessage, this);
487
+ }
488
+ publishChallengeAnswers(challengeAnswers) {
489
+ return __awaiter(this, void 0, void 0, function* () {
490
+ this.publishingState = "publishing-challenge-answer";
491
+ this.emit("publishingstatechange", "publishing-challenge-answer");
492
+ yield simulateLoadingTime();
493
+ this.publishingState = "waiting-challenge-verification";
494
+ this.emit("publishingstatechange", "waiting-challenge-verification");
495
+ yield simulateLoadingTime();
496
+ this.simulateChallengeVerificationEvent();
497
+ });
498
+ }
499
+ simulateChallengeVerificationEvent() {
500
+ // if publication has content, create cid for this content and add it to comment and challengeVerificationMessage
501
+ this.cid = this.content && `${this.content} cid`;
502
+ const commentUpdate = this.cid && { cid: this.cid };
503
+ const challengeVerificationMessage = {
504
+ type: "CHALLENGEVERIFICATION",
505
+ challengeRequestId: this.challengeRequestId,
506
+ challengeAnswerId: this.challengeAnswerId,
507
+ challengeSuccess: true,
508
+ commentUpdate,
509
+ };
510
+ this.emit("challengeverification", challengeVerificationMessage, this);
511
+ this.publishingState = "succeeded";
512
+ this.emit("publishingstatechange", "succeeded");
513
+ }
514
+ stop() {
515
+ if (this.state === "publishing" || this.publishingState !== "stopped") {
516
+ this.state = "stopped";
517
+ this.publishingState = "stopped";
518
+ this.emit("statechange", "stopped");
519
+ this.emit("publishingstatechange", "stopped");
520
+ }
521
+ if (this.updating || this.updatingState !== "stopped") {
522
+ this.state = "stopped";
523
+ this.updating = false;
524
+ this.updatingState = "stopped";
525
+ this.emit("statechange", "stopped");
526
+ this.emit("updatingstatechange", "stopped");
527
+ }
528
+ }
529
+ }
530
+ export class Comment extends Publication {
531
+ constructor(createCommentOptions) {
532
+ var _a, _b, _c, _d, _e;
533
+ super();
534
+ this.updateCalledTimes = 0;
535
+ this.updating = false;
536
+ this.cid = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.cid;
537
+ this.upvoteCount = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.upvoteCount;
538
+ this.downvoteCount = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.downvoteCount;
539
+ this.content = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.content;
540
+ this.author = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.author;
541
+ this.timestamp = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.timestamp;
542
+ this.parentCid = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.parentCid;
543
+ this.communityAddress = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.communityAddress;
544
+ this.state = "stopped";
545
+ this.updatingState = "stopped";
546
+ this.publishingState = "stopped";
547
+ if ((_a = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.author) === null || _a === void 0 ? void 0 : _a.address) {
548
+ this.author.shortAddress = `short ${createCommentOptions.author.address}`;
549
+ }
550
+ this.replies = new Pages({ comment: this });
551
+ // add comment.replies from createCommentOptions
552
+ if ((_b = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.replies) === null || _b === void 0 ? void 0 : _b.pages) {
553
+ this.replies.pages = (_c = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.replies) === null || _c === void 0 ? void 0 : _c.pages;
554
+ }
555
+ if ((_d = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.replies) === null || _d === void 0 ? void 0 : _d.pageCids) {
556
+ this.replies.pageCids = (_e = createCommentOptions === null || createCommentOptions === void 0 ? void 0 : createCommentOptions.replies) === null || _e === void 0 ? void 0 : _e.pageCids;
557
+ }
558
+ }
559
+ update() {
560
+ return __awaiter(this, void 0, void 0, function* () {
561
+ this.updateCalledTimes++;
562
+ if (this.updateCalledTimes > 5) {
563
+ throw Error("with the current hooks, comment.update() should be called maximum 5 times, this number might change if the hooks change and is only there to catch bugs, the real comment.update() can be called infinite times");
564
+ }
565
+ // don't update twice
566
+ if (this.updating) {
567
+ return;
568
+ }
569
+ this.updating = true;
570
+ this.state = "updating";
571
+ this.updatingState = "fetching-ipfs";
572
+ this.emit("statechange", "updating");
573
+ this.emit("updatingstatechange", "fetching-ipfs");
574
+ simulateLoadingTime().then(() => {
575
+ this.simulateUpdateEvent();
576
+ });
577
+ });
578
+ }
579
+ simulateUpdateEvent() {
580
+ if (!this.updating) {
581
+ return;
582
+ }
583
+ // if timestamp isn't defined, simulate fetching the comment ipfs
584
+ if (!this.timestamp) {
585
+ this.simulateFetchCommentIpfsUpdateEvent();
586
+ return;
587
+ }
588
+ // simulate finding vote counts on an IPNS record
589
+ this.upvoteCount = typeof this.upvoteCount === "number" ? this.upvoteCount + 2 : 3;
590
+ this.downvoteCount = typeof this.downvoteCount === "number" ? this.downvoteCount + 1 : 1;
591
+ this.updatedAt = Math.floor(Date.now() / 1000);
592
+ this.updatingState = "succeeded";
593
+ this.emit("update", this);
594
+ this.emit("updatingstatechange", "succeeded");
595
+ }
596
+ simulateFetchCommentIpfsUpdateEvent() {
597
+ return __awaiter(this, void 0, void 0, function* () {
598
+ if (!this.updating) {
599
+ return;
600
+ }
601
+ // use pkc.getComment() so mocking PKC.prototype.getComment works
602
+ const commentIpfs = yield new PKC().getComment({ cid: this.cid || "" });
603
+ if (!this.updating) {
604
+ return;
605
+ }
606
+ this.content = commentIpfs.content;
607
+ this.author = commentIpfs.author;
608
+ this.timestamp = commentIpfs.timestamp;
609
+ this.parentCid = commentIpfs.parentCid;
610
+ this.communityAddress = commentIpfs.communityAddress;
611
+ // simulate the ipns update
612
+ this.updatingState = "fetching-update-ipns";
613
+ this.emit("update", this);
614
+ this.emit("updatingstatechange", "fetching-update-ipns");
615
+ simulateLoadingTime().then(() => {
616
+ this.simulateUpdateEvent();
617
+ });
618
+ });
619
+ }
620
+ }
621
+ export class Vote extends Publication {
622
+ }
623
+ export class CommentEdit extends Publication {
624
+ }
625
+ export class CommentModeration extends Publication {
626
+ }
627
+ export class CommunityEdit extends Publication {
628
+ }
629
+ const createPkc = (...args) => __awaiter(void 0, void 0, void 0, function* () {
630
+ return new PKC(...args);
631
+ });
632
+ createPkc.getShortAddress = (options) => {
633
+ const address = options === null || options === void 0 ? void 0 : options.address;
634
+ if (address.includes(".")) {
635
+ return address;
636
+ }
637
+ return address.substring(2, 14);
638
+ };
639
+ createPkc.getShortCid = (options) => {
640
+ const cid = options === null || options === void 0 ? void 0 : options.cid;
641
+ return cid.substring(2, 14);
642
+ };
643
+ export default createPkc;
644
+ //# sourceMappingURL=pkc-js-mock.js.map