@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,685 @@
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 PkcJs from "../../lib/pkc-js/index.js";
11
+ import validator from "../../lib/validator.js";
12
+ import chain from "../../lib/chain/index.js";
13
+ import assert from "assert";
14
+ import localForage from "localforage";
15
+ import isEqual from "lodash.isequal";
16
+ import localForageLru from "../../lib/localforage-lru/index.js";
17
+ import utils from "../../lib/utils/index.js";
18
+ import { getDefaultChainProviders, getDefaultPkcOptions, overwritePkcOptions, } from "./account-generator.js";
19
+ import { getAccountsEditsSummary, sanitizeStoredAccountComment } from "./utils.js";
20
+ import { getPkcClientOptions, normalizeAccountProtocolConfig, withProtocolAliases, } from "../../lib/pkc-compat.js";
21
+ import Logger from "@pkcprotocol/pkc-logger";
22
+ const log = Logger("bitsocial-react-hooks:accounts:stores");
23
+ // Storage keeps the existing namespace so current installs reuse the same IndexedDB data.
24
+ const accountsDatabaseNamespace = "bitsocialReactHooks";
25
+ const getAccountsDatabaseName = (databaseName) => `${accountsDatabaseNamespace}-${databaseName}`;
26
+ const getPerAccountDatabaseName = (databaseName, accountId) => `${getAccountsDatabaseName(databaseName)}-${accountId}`;
27
+ const accountsDatabase = localForage.createInstance({ name: getAccountsDatabaseName("accounts") });
28
+ const accountsMetadataDatabase = localForage.createInstance({
29
+ name: getAccountsDatabaseName("accountsMetadata"),
30
+ });
31
+ const storageVersionKey = "__storageVersion";
32
+ const votesLatestIndexKey = "__commentCidToLatestIndex";
33
+ const editsTargetToIndicesKey = "__targetToIndices";
34
+ const editsSummaryKey = "__summary";
35
+ const commentStorageVersion = 2;
36
+ const voteStorageVersion = 1;
37
+ const editStorageVersion = 1;
38
+ // TODO: remove this eventually after everyone has migrated
39
+ // migrate to name with safe prefix
40
+ const migrate = () => __awaiter(void 0, void 0, void 0, function* () {
41
+ const previousAccountsDatabase = localForage.createInstance({ name: "accounts" });
42
+ const previousAccountsMetadataDatabase = localForage.createInstance({ name: "accountsMetadata" });
43
+ // no previous db to migrate
44
+ if (!(yield previousAccountsMetadataDatabase.getItem("activeAccountId"))) {
45
+ return;
46
+ }
47
+ // db already migrated
48
+ if (yield accountsMetadataDatabase.getItem("activeAccountId")) {
49
+ return;
50
+ }
51
+ // migrate
52
+ const promises = [];
53
+ for (const key of yield previousAccountsDatabase.keys()) {
54
+ promises.push(previousAccountsDatabase.getItem(key).then((value) => accountsDatabase.setItem(key, value)));
55
+ }
56
+ for (const key of yield previousAccountsMetadataDatabase.keys()) {
57
+ promises.push(previousAccountsMetadataDatabase
58
+ .getItem(key)
59
+ .then((value) => accountsMetadataDatabase.setItem(key, value)));
60
+ }
61
+ const accountIds = yield previousAccountsMetadataDatabase.getItem("accountIds");
62
+ if (Array.isArray(accountIds)) {
63
+ const databaseNames = [
64
+ "accountComments",
65
+ "accountVotes",
66
+ "accountCommentsReplies",
67
+ "accountEdits",
68
+ ];
69
+ for (const databaseName of databaseNames) {
70
+ for (const accountId of accountIds) {
71
+ const previousDatabase = localForage.createInstance({
72
+ name: `${databaseName}-${accountId}`,
73
+ });
74
+ const database = localForage.createInstance({
75
+ name: getPerAccountDatabaseName(databaseName, accountId),
76
+ });
77
+ for (const key of yield previousDatabase.keys()) {
78
+ promises.push(previousDatabase.getItem(key).then((value) => database.setItem(key, value)));
79
+ }
80
+ }
81
+ }
82
+ }
83
+ yield Promise.all(promises);
84
+ });
85
+ const getAccounts = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
86
+ validator.validateAccountsDatabaseGetAccountsArguments(accountIds);
87
+ const accounts = {};
88
+ const promises = [];
89
+ for (const accountId of accountIds) {
90
+ promises.push(accountsDatabase.getItem(accountId));
91
+ }
92
+ const accountsArray = yield Promise.all(promises);
93
+ for (const [i, accountId] of accountIds.entries()) {
94
+ assert(accountsArray[i], `accountId '${accountId}' not found in database`);
95
+ accounts[accountId] = normalizeAccountProtocolConfig(yield migrateAccount(accountsArray[i]), getDefaultChainProviders());
96
+ // protocol options aren't saved to database if they are default
97
+ if (!accounts[accountId].pkcOptions && !accounts[accountId].pkcOptions) {
98
+ accounts[accountId].pkcOptions = getDefaultPkcOptions();
99
+ }
100
+ const protocolOptions = Object.assign(Object.assign({}, (accounts[accountId].pkcOptions || accounts[accountId].pkcOptions)), overwritePkcOptions);
101
+ const pkc = yield PkcJs.PKC(getPkcClientOptions(accounts[accountId], protocolOptions));
102
+ // handle errors or error events are uncaught
103
+ // no need to log them because pkc-js already logs them
104
+ pkc.on("error", (error) => log.error("uncaught pkc instance error, should never happen", { error }));
105
+ accounts[accountId] = withProtocolAliases(accounts[accountId], pkc, protocolOptions);
106
+ }
107
+ return accounts;
108
+ });
109
+ const accountVersion = 5;
110
+ const migrateAccount = (account) => __awaiter(void 0, void 0, void 0, function* () {
111
+ var _a, _b, _c, _d, _e;
112
+ account = normalizeAccountProtocolConfig(account);
113
+ let version = account.version || 1;
114
+ // version 2
115
+ if (version === 1) {
116
+ version++;
117
+ if ((_a = account.pkcOptions) === null || _a === void 0 ? void 0 : _a.ipfsHttpClientsOptions) {
118
+ account.pkcOptions.kuboRpcClientsOptions = account.pkcOptions.ipfsHttpClientsOptions;
119
+ delete account.pkcOptions.ipfsHttpClientsOptions;
120
+ }
121
+ if ((_b = account.pkcOptions) === null || _b === void 0 ? void 0 : _b.pubsubHttpClientsOptions) {
122
+ account.pkcOptions.pubsubKuboRpcClientsOptions = account.pkcOptions.pubsubHttpClientsOptions;
123
+ delete account.pkcOptions.pubsubHttpClientsOptions;
124
+ }
125
+ }
126
+ // version 3
127
+ if (version === 2) {
128
+ version++;
129
+ if (!account.author.wallets) {
130
+ account.author.wallets = {};
131
+ }
132
+ if (!account.author.wallets.eth) {
133
+ account.author.wallets.eth = yield chain.getEthWalletFromPkcPrivateKey(account.signer.privateKey, account.address);
134
+ }
135
+ }
136
+ if (version === 3) {
137
+ version++;
138
+ // in version 3, wallets had timestamps in ms, should be seconds
139
+ if (((_e = (_d = (_c = account.author) === null || _c === void 0 ? void 0 : _c.wallets) === null || _d === void 0 ? void 0 : _d.eth) === null || _e === void 0 ? void 0 : _e.timestamp) > 1e12) {
140
+ account.author.wallets.eth = yield chain.getEthWalletFromPkcPrivateKey(account.signer.privateKey, account.address);
141
+ }
142
+ }
143
+ if (version === 4) {
144
+ version++;
145
+ account = normalizeAccountProtocolConfig(account);
146
+ }
147
+ account.version = accountVersion;
148
+ return account;
149
+ });
150
+ const getAccount = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
151
+ const accounts = yield getAccounts([accountId]);
152
+ return accounts[accountId];
153
+ });
154
+ const getExportedAccountJson = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
155
+ assert(accountId && typeof accountId === "string", `getAccountJson argument accountId '${accountId}' invalid`);
156
+ // do not serialize or instantiate anything (unlike getAccount)
157
+ const account = yield accountsDatabase.getItem(accountId);
158
+ if (!account) {
159
+ throw Error(`getAccountJson no account in database with accountId '${accountId}'`);
160
+ }
161
+ const accountCommentsDatabase = getAccountCommentsDatabase(accountId);
162
+ const accountVotesDatabase = getAccountVotesDatabase(accountId);
163
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
164
+ yield ensureAccountCommentsDatabaseLayout(accountId);
165
+ const [accountComments, accountVotes, accountEdits] = yield Promise.all([
166
+ getDatabaseAsArray(accountCommentsDatabase),
167
+ getDatabaseAsArray(accountVotesDatabase),
168
+ getDatabaseAsArray(accountEditsDatabase),
169
+ ]);
170
+ return JSON.stringify({ account, accountComments, accountVotes, accountEdits });
171
+ });
172
+ // accountVotes, accountComments and accountEdits are indexeddb
173
+ // databases formed like an array (keys are numbers)
174
+ const getDatabaseAsArray = (database) => __awaiter(void 0, void 0, void 0, function* () {
175
+ const length = (yield database.getItem("length")) || 0;
176
+ let promises = [];
177
+ let i = 0;
178
+ while (i < length) {
179
+ promises.push(database.getItem(String(i++)));
180
+ }
181
+ const items = yield Promise.all(promises);
182
+ return items;
183
+ });
184
+ const removeFunctionsAndSensitiveFields = (publication) => {
185
+ const sanitizedPublication = {};
186
+ for (const key in publication) {
187
+ if (key === "signer" || key === "author" || typeof publication[key] === "function") {
188
+ continue;
189
+ }
190
+ sanitizedPublication[key] = publication[key];
191
+ }
192
+ return sanitizedPublication;
193
+ };
194
+ const isNumericDatabaseKey = (key) => /^[0-9]+$/.test(key);
195
+ const rebuildVotesLatestIndex = (votes) => {
196
+ const latestIndexByCommentCid = {};
197
+ for (const [index, vote] of votes.entries()) {
198
+ if (vote === null || vote === void 0 ? void 0 : vote.commentCid) {
199
+ latestIndexByCommentCid[vote.commentCid] = index;
200
+ }
201
+ }
202
+ return latestIndexByCommentCid;
203
+ };
204
+ const rebuildEditsTargetIndexes = (edits) => {
205
+ const targetToIndices = {};
206
+ for (const [index, edit] of edits.entries()) {
207
+ const editTarget = getAccountEditTarget(edit);
208
+ if (!editTarget) {
209
+ continue;
210
+ }
211
+ if (!targetToIndices[editTarget]) {
212
+ targetToIndices[editTarget] = [];
213
+ }
214
+ targetToIndices[editTarget].push(index);
215
+ }
216
+ return targetToIndices;
217
+ };
218
+ const addAccount = (account) => __awaiter(void 0, void 0, void 0, function* () {
219
+ var _a;
220
+ validator.validateAccountsDatabaseAddAccountArguments(account);
221
+ let accountIds = yield accountsMetadataDatabase.getItem("accountIds");
222
+ // handle no duplicate names
223
+ if (accountIds === null || accountIds === void 0 ? void 0 : accountIds.length) {
224
+ const accounts = yield getAccounts(accountIds);
225
+ for (const accountId of accountIds) {
226
+ if (accountId !== account.id && accounts[accountId].name === account.name) {
227
+ throw Error(`account name '${account.name}' already exists in database`);
228
+ }
229
+ }
230
+ }
231
+ // handle updating accounts database
232
+ const accountToPutInDatabase = normalizeAccountProtocolConfig(Object.assign(Object.assign({}, account), { pkc: undefined }));
233
+ const protocolOptions = accountToPutInDatabase.pkcOptions;
234
+ accountToPutInDatabase.pkcOptions = protocolOptions;
235
+ // don't save default protocol options in database in case they change
236
+ if (JSON.stringify(protocolOptions) === JSON.stringify(getDefaultPkcOptions())) {
237
+ delete accountToPutInDatabase.pkcOptions;
238
+ }
239
+ if (JSON.stringify(accountToPutInDatabase.chainProviders) ===
240
+ JSON.stringify(getDefaultChainProviders())) {
241
+ delete accountToPutInDatabase.chainProviders;
242
+ }
243
+ // make sure accountToPutInDatabase protocol options are valid
244
+ if (protocolOptions) {
245
+ const pkc = yield PkcJs.PKC(getPkcClientOptions(accountToPutInDatabase, protocolOptions));
246
+ pkc.on("error", () => { });
247
+ void ((_a = pkc.destroy) === null || _a === void 0 ? void 0 : _a.call(pkc)); // gc; errors intentionally unhandled to avoid uncounted callback
248
+ }
249
+ yield accountsDatabase.setItem(accountToPutInDatabase.id, accountToPutInDatabase);
250
+ // handle updating accountNamesToAccountIds database
251
+ let accountNamesToAccountIds = yield accountsMetadataDatabase.getItem("accountNamesToAccountIds");
252
+ if (!accountNamesToAccountIds) {
253
+ accountNamesToAccountIds = {};
254
+ }
255
+ accountNamesToAccountIds[account.name] = account.id;
256
+ yield accountsMetadataDatabase.setItem("accountNamesToAccountIds", accountNamesToAccountIds);
257
+ // handle updating accountIds database
258
+ if (!accountIds) {
259
+ accountIds = [account.id];
260
+ }
261
+ if (!accountIds.includes(account.id)) {
262
+ accountIds.push(account.id);
263
+ }
264
+ yield accountsMetadataDatabase.setItem("accountIds", accountIds);
265
+ // handle updating activeAccountId database
266
+ if (accountIds.length === 1) {
267
+ yield accountsMetadataDatabase.setItem("activeAccountId", account.id);
268
+ }
269
+ });
270
+ const removeAccount = (account) => __awaiter(void 0, void 0, void 0, function* () {
271
+ assert((account === null || account === void 0 ? void 0 : account.id) && typeof (account === null || account === void 0 ? void 0 : account.id) === "string", `accountsDatabase.removeAccount invalid account.id '${account.id}'`);
272
+ // handle updating accounts database
273
+ yield accountsDatabase.removeItem(account.id);
274
+ // handle updating accountNamesToAccountIds database
275
+ let accountNamesToAccountIds = yield accountsMetadataDatabase.getItem("accountNamesToAccountIds");
276
+ if (!accountNamesToAccountIds) {
277
+ accountNamesToAccountIds = {};
278
+ }
279
+ delete accountNamesToAccountIds[account.name];
280
+ yield accountsMetadataDatabase.setItem("accountNamesToAccountIds", accountNamesToAccountIds);
281
+ // handle updating accountIds database
282
+ let accountIds = yield accountsMetadataDatabase.getItem("accountIds");
283
+ accountIds = (accountIds || []).filter((accountId) => accountId !== account.id);
284
+ yield accountsMetadataDatabase.setItem("accountIds", accountIds);
285
+ // handle updating activeAccountId database
286
+ const activeAccountId = yield accountsMetadataDatabase.getItem("activeAccountId");
287
+ if (activeAccountId === account.id) {
288
+ if (accountIds.length) {
289
+ yield accountsMetadataDatabase.setItem("activeAccountId", accountIds[0]);
290
+ }
291
+ else {
292
+ yield accountsMetadataDatabase.removeItem("activeAccountId");
293
+ }
294
+ }
295
+ const accountCommentsDatabase = getAccountCommentsDatabase(account.id);
296
+ yield accountCommentsDatabase.clear();
297
+ const accountVotesDatabase = getAccountVotesDatabase(account.id);
298
+ yield accountVotesDatabase.clear();
299
+ const accountCommentsRepliesDatabase = getAccountCommentsRepliesDatabase(account.id);
300
+ yield accountCommentsRepliesDatabase.clear();
301
+ const accountEditsDatabase = getAccountEditsDatabase(account.id);
302
+ yield accountEditsDatabase.clear();
303
+ });
304
+ const accountsCommentsDatabases = {};
305
+ const accountCommentsLayoutMigrations = {};
306
+ const getAccountCommentsDatabase = (accountId) => {
307
+ assert(accountId && typeof accountId === "string", `getAccountCommentsDatabase '${accountId}' not a string`);
308
+ if (!accountsCommentsDatabases[accountId]) {
309
+ accountsCommentsDatabases[accountId] = localForage.createInstance({
310
+ name: getPerAccountDatabaseName("accountComments", accountId),
311
+ });
312
+ }
313
+ return accountsCommentsDatabases[accountId];
314
+ };
315
+ const ensureAccountCommentsDatabaseLayout = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
316
+ const accountCommentsDatabase = getAccountCommentsDatabase(accountId);
317
+ if ((yield accountCommentsDatabase.getItem(storageVersionKey)) === commentStorageVersion) {
318
+ return;
319
+ }
320
+ if (!accountCommentsLayoutMigrations[accountId]) {
321
+ accountCommentsLayoutMigrations[accountId] = (() => __awaiter(void 0, void 0, void 0, function* () {
322
+ if ((yield accountCommentsDatabase.getItem(storageVersionKey)) === commentStorageVersion) {
323
+ return;
324
+ }
325
+ const comments = yield getDatabaseAsArray(accountCommentsDatabase);
326
+ const updatedComments = comments
327
+ .map((comment) => (comment ? sanitizeStoredAccountComment(comment) : undefined))
328
+ .filter((comment) => comment !== undefined);
329
+ const rewritePromises = [];
330
+ for (const [index, updatedComment] of updatedComments.entries()) {
331
+ if (!isEqual(updatedComment, comments[index])) {
332
+ rewritePromises.push(accountCommentsDatabase.setItem(String(index), updatedComment));
333
+ }
334
+ }
335
+ for (let index = updatedComments.length; index < comments.length; index++) {
336
+ rewritePromises.push(accountCommentsDatabase.removeItem(String(index)));
337
+ }
338
+ rewritePromises.push(accountCommentsDatabase.setItem("length", updatedComments.length));
339
+ yield Promise.all(rewritePromises);
340
+ yield accountCommentsDatabase.setItem(storageVersionKey, commentStorageVersion);
341
+ }))().finally(() => {
342
+ delete accountCommentsLayoutMigrations[accountId];
343
+ });
344
+ }
345
+ yield accountCommentsLayoutMigrations[accountId];
346
+ });
347
+ const deleteAccountComment = (accountId, accountCommentIndex) => __awaiter(void 0, void 0, void 0, function* () {
348
+ const accountCommentsDatabase = getAccountCommentsDatabase(accountId);
349
+ yield ensureAccountCommentsDatabaseLayout(accountId);
350
+ const length = (yield accountCommentsDatabase.getItem("length")) || 0;
351
+ assert(accountCommentIndex >= 0 && accountCommentIndex < length, `deleteAccountComment accountCommentIndex '${accountCommentIndex}' out of range [0, ${length})`);
352
+ const items = yield getDatabaseAsArray(accountCommentsDatabase);
353
+ items.splice(accountCommentIndex, 1);
354
+ const newLength = length - 1;
355
+ const promises = [];
356
+ for (let i = 0; i < newLength; i++) {
357
+ promises.push(accountCommentsDatabase.setItem(String(i), items[i]));
358
+ }
359
+ promises.push(accountCommentsDatabase.removeItem(String(length - 1)));
360
+ promises.push(accountCommentsDatabase.setItem("length", newLength));
361
+ yield Promise.all(promises);
362
+ });
363
+ const addAccountComment = (accountId, comment, accountCommentIndex) => __awaiter(void 0, void 0, void 0, function* () {
364
+ const accountCommentsDatabase = getAccountCommentsDatabase(accountId);
365
+ yield ensureAccountCommentsDatabaseLayout(accountId);
366
+ const length = (yield accountCommentsDatabase.getItem("length")) || 0;
367
+ comment = sanitizeStoredAccountComment(comment);
368
+ if (typeof accountCommentIndex === "number") {
369
+ assert(accountCommentIndex < length, `addAccountComment cannot edit comment no comment in database at accountCommentIndex '${accountCommentIndex}'`);
370
+ yield Promise.all([
371
+ accountCommentsDatabase.setItem(String(accountCommentIndex), comment),
372
+ accountCommentsDatabase.setItem(storageVersionKey, commentStorageVersion),
373
+ ]);
374
+ }
375
+ else {
376
+ yield Promise.all([
377
+ accountCommentsDatabase.setItem(String(length), comment),
378
+ accountCommentsDatabase.setItem(storageVersionKey, commentStorageVersion),
379
+ accountCommentsDatabase.setItem("length", length + 1),
380
+ ]);
381
+ }
382
+ });
383
+ const getAccountComments = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
384
+ const accountCommentsDatabase = getAccountCommentsDatabase(accountId);
385
+ yield ensureAccountCommentsDatabaseLayout(accountId);
386
+ const length = (yield accountCommentsDatabase.getItem("length")) || 0;
387
+ if (length === 0) {
388
+ return [];
389
+ }
390
+ let promises = [];
391
+ let i = 0;
392
+ while (i < length) {
393
+ promises.push(accountCommentsDatabase.getItem(String(i++)));
394
+ }
395
+ const comments = yield Promise.all(promises);
396
+ // add index and account id to account comments for easier updating
397
+ for (const i in comments) {
398
+ comments[i] = sanitizeStoredAccountComment(comments[i]);
399
+ comments[i].index = Number(i);
400
+ comments[i].accountId = accountId;
401
+ }
402
+ return comments;
403
+ });
404
+ const getAccountsComments = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
405
+ assert(Array.isArray(accountIds), `getAccountsComments invalid accountIds '${accountIds}' not an array`);
406
+ const promises = [];
407
+ for (const accountId of accountIds) {
408
+ promises.push(getAccountComments(accountId));
409
+ }
410
+ const accountsCommentsArray = yield Promise.all(promises);
411
+ const accountsComments = {};
412
+ for (const [i, accountId] of accountIds.entries()) {
413
+ accountsComments[accountId] = accountsCommentsArray[i];
414
+ }
415
+ return accountsComments;
416
+ });
417
+ const accountsVotesDatabases = {};
418
+ const getAccountVotesDatabase = (accountId) => {
419
+ assert(accountId && typeof accountId === "string", `getAccountVotesDatabase '${accountId}' not a string`);
420
+ if (!accountsVotesDatabases[accountId]) {
421
+ accountsVotesDatabases[accountId] = localForage.createInstance({
422
+ name: getPerAccountDatabaseName("accountVotes", accountId),
423
+ });
424
+ }
425
+ return accountsVotesDatabases[accountId];
426
+ };
427
+ const ensureAccountVotesDatabaseLayout = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
428
+ const accountVotesDatabase = getAccountVotesDatabase(accountId);
429
+ if ((yield accountVotesDatabase.getItem(storageVersionKey)) === voteStorageVersion) {
430
+ return;
431
+ }
432
+ const votes = yield getDatabaseAsArray(accountVotesDatabase);
433
+ const latestIndexByCommentCid = rebuildVotesLatestIndex(votes);
434
+ const keys = yield accountVotesDatabase.keys();
435
+ const duplicateKeysToDelete = keys.filter((key) => !isNumericDatabaseKey(key) &&
436
+ key !== "length" &&
437
+ key !== storageVersionKey &&
438
+ key !== votesLatestIndexKey &&
439
+ latestIndexByCommentCid[key] !== undefined);
440
+ yield Promise.all([
441
+ ...duplicateKeysToDelete.map((key) => accountVotesDatabase.removeItem(key)),
442
+ accountVotesDatabase.setItem(votesLatestIndexKey, latestIndexByCommentCid),
443
+ accountVotesDatabase.setItem(storageVersionKey, voteStorageVersion),
444
+ ]);
445
+ });
446
+ const addAccountVote = (accountId, createVoteOptions) => __awaiter(void 0, void 0, void 0, function* () {
447
+ assert((createVoteOptions === null || createVoteOptions === void 0 ? void 0 : createVoteOptions.commentCid) && typeof (createVoteOptions === null || createVoteOptions === void 0 ? void 0 : createVoteOptions.commentCid) === "string", `addAccountVote createVoteOptions.commentCid '${createVoteOptions === null || createVoteOptions === void 0 ? void 0 : createVoteOptions.commentCid}' not a string`);
448
+ const accountVotesDatabase = getAccountVotesDatabase(accountId);
449
+ yield ensureAccountVotesDatabaseLayout(accountId);
450
+ const length = (yield accountVotesDatabase.getItem("length")) || 0;
451
+ const vote = removeFunctionsAndSensitiveFields(createVoteOptions);
452
+ const existingLatestIndexByCommentCid = yield accountVotesDatabase.getItem(votesLatestIndexKey);
453
+ const latestIndexByCommentCid = Object.assign(Object.assign({}, existingLatestIndexByCommentCid), { [vote.commentCid]: length });
454
+ yield Promise.all([
455
+ accountVotesDatabase.setItem(String(length), vote),
456
+ accountVotesDatabase.setItem(votesLatestIndexKey, latestIndexByCommentCid),
457
+ accountVotesDatabase.setItem(storageVersionKey, voteStorageVersion),
458
+ accountVotesDatabase.setItem("length", length + 1),
459
+ ]);
460
+ });
461
+ const getAccountVotes = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
462
+ const accountVotesDatabase = getAccountVotesDatabase(accountId);
463
+ yield ensureAccountVotesDatabaseLayout(accountId);
464
+ const latestIndexByCommentCid = (yield accountVotesDatabase.getItem(votesLatestIndexKey)) || {};
465
+ const votes = {};
466
+ const latestIndexes = Object.values(latestIndexByCommentCid);
467
+ if (latestIndexes.length === 0) {
468
+ return votes;
469
+ }
470
+ const promises = latestIndexes.map((index) => accountVotesDatabase.getItem(String(index)));
471
+ const votesArray = yield Promise.all(promises);
472
+ for (const vote of votesArray) {
473
+ votes[vote === null || vote === void 0 ? void 0 : vote.commentCid] = vote;
474
+ }
475
+ return votes;
476
+ });
477
+ const getAccountsVotes = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
478
+ assert(Array.isArray(accountIds), `getAccountsVotes invalid accountIds '${accountIds}' not an array`);
479
+ const promises = [];
480
+ for (const accountId of accountIds) {
481
+ promises.push(getAccountVotes(accountId));
482
+ }
483
+ const accountsVotesArray = yield Promise.all(promises);
484
+ const accountsVotes = {};
485
+ for (const [i, accountId] of accountIds.entries()) {
486
+ accountsVotes[accountId] = accountsVotesArray[i];
487
+ }
488
+ return accountsVotes;
489
+ });
490
+ const accountsCommentsRepliesDatabases = {};
491
+ const getAccountCommentsRepliesDatabase = (accountId) => {
492
+ assert(accountId && typeof accountId === "string", `getAccountCommentsRepliesDatabase '${accountId}' not a string`);
493
+ if (!accountsCommentsRepliesDatabases[accountId]) {
494
+ accountsCommentsRepliesDatabases[accountId] = localForageLru.createInstance({
495
+ name: getPerAccountDatabaseName("accountCommentsReplies", accountId),
496
+ size: 1000,
497
+ });
498
+ }
499
+ return accountsCommentsRepliesDatabases[accountId];
500
+ };
501
+ const addAccountCommentReply = (accountId, reply) => __awaiter(void 0, void 0, void 0, function* () {
502
+ const accountCommentsRepliesDatabase = getAccountCommentsRepliesDatabase(accountId);
503
+ yield accountCommentsRepliesDatabase.setItem(reply.cid, utils.clone(reply));
504
+ });
505
+ const getAccountCommentsReplies = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
506
+ const accountCommentsRepliesDatabase = getAccountCommentsRepliesDatabase(accountId);
507
+ const accountCommentsRepliesEntries = yield accountCommentsRepliesDatabase.entries();
508
+ const replies = {};
509
+ for (const [, reply] of accountCommentsRepliesEntries) {
510
+ // @ts-ignore
511
+ replies[reply.cid] = reply;
512
+ }
513
+ return replies;
514
+ });
515
+ const getAccountsCommentsReplies = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
516
+ assert(Array.isArray(accountIds), `getAccountsCommentsReplies invalid accountIds '${accountIds}' not an array`);
517
+ const promises = [];
518
+ for (const accountId of accountIds) {
519
+ promises.push(getAccountCommentsReplies(accountId));
520
+ }
521
+ const accountsCommentsRepliesArray = yield Promise.all(promises);
522
+ const accountsCommentsReplies = {};
523
+ for (const [i, accountId] of accountIds.entries()) {
524
+ accountsCommentsReplies[accountId] = accountsCommentsRepliesArray[i];
525
+ }
526
+ return accountsCommentsReplies;
527
+ });
528
+ const accountsEditsDatabases = {};
529
+ const getAccountEditsDatabase = (accountId) => {
530
+ assert(accountId && typeof accountId === "string", `getAccountEditsDatabase '${accountId}' not a string`);
531
+ if (!accountsEditsDatabases[accountId]) {
532
+ accountsEditsDatabases[accountId] = localForage.createInstance({
533
+ name: getPerAccountDatabaseName("accountEdits", accountId),
534
+ });
535
+ }
536
+ return accountsEditsDatabases[accountId];
537
+ };
538
+ const getAccountEditTarget = (edit) => (edit === null || edit === void 0 ? void 0 : edit.commentCid) || (edit === null || edit === void 0 ? void 0 : edit.communityAddress) || (edit === null || edit === void 0 ? void 0 : edit.communityAddress);
539
+ const persistAccountEditsIndexes = (accountId, edits) => __awaiter(void 0, void 0, void 0, function* () {
540
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
541
+ const targetToIndices = rebuildEditsTargetIndexes(edits);
542
+ const summary = getAccountsEditsSummary(Object.fromEntries(Object.entries(targetToIndices).map(([target, indices]) => [
543
+ target,
544
+ indices.map((index) => edits[index]).filter(Boolean),
545
+ ])));
546
+ yield Promise.all([
547
+ accountEditsDatabase.setItem(editsTargetToIndicesKey, targetToIndices),
548
+ accountEditsDatabase.setItem(editsSummaryKey, summary),
549
+ accountEditsDatabase.setItem(storageVersionKey, editStorageVersion),
550
+ ]);
551
+ return { targetToIndices, summary };
552
+ });
553
+ const ensureAccountEditsDatabaseLayout = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
554
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
555
+ if ((yield accountEditsDatabase.getItem(storageVersionKey)) === editStorageVersion) {
556
+ return;
557
+ }
558
+ const edits = yield getDatabaseAsArray(accountEditsDatabase);
559
+ const keys = yield accountEditsDatabase.keys();
560
+ const duplicateKeysToDelete = keys.filter((key) => !isNumericDatabaseKey(key) &&
561
+ key !== "length" &&
562
+ key !== storageVersionKey &&
563
+ key !== editsTargetToIndicesKey &&
564
+ key !== editsSummaryKey &&
565
+ edits.some((edit) => getAccountEditTarget(edit) === key));
566
+ yield Promise.all(duplicateKeysToDelete.map((key) => accountEditsDatabase.removeItem(key)));
567
+ yield persistAccountEditsIndexes(accountId, edits);
568
+ });
569
+ const addAccountEdit = (accountId, createEditOptions) => __awaiter(void 0, void 0, void 0, function* () {
570
+ const editTarget = getAccountEditTarget(createEditOptions);
571
+ assert(typeof editTarget === "string", `addAccountEdit target '${editTarget}' not a string`);
572
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
573
+ yield ensureAccountEditsDatabaseLayout(accountId);
574
+ const length = (yield accountEditsDatabase.getItem("length")) || 0;
575
+ const edit = removeFunctionsAndSensitiveFields(createEditOptions);
576
+ const existingEdits = yield getDatabaseAsArray(accountEditsDatabase);
577
+ existingEdits[length] = edit;
578
+ yield Promise.all([
579
+ accountEditsDatabase.setItem(String(length), edit),
580
+ accountEditsDatabase.setItem(storageVersionKey, editStorageVersion),
581
+ accountEditsDatabase.setItem("length", length + 1),
582
+ ]);
583
+ yield persistAccountEditsIndexes(accountId, existingEdits);
584
+ });
585
+ const doesStoredAccountEditMatch = (storedAccountEdit, targetStoredAccountEdit) => (storedAccountEdit === null || storedAccountEdit === void 0 ? void 0 : storedAccountEdit.clientId) && (targetStoredAccountEdit === null || targetStoredAccountEdit === void 0 ? void 0 : targetStoredAccountEdit.clientId)
586
+ ? storedAccountEdit.clientId === targetStoredAccountEdit.clientId
587
+ : isEqual(storedAccountEdit, targetStoredAccountEdit);
588
+ const deleteAccountEdit = (accountId, editToDelete) => __awaiter(void 0, void 0, void 0, function* () {
589
+ const editTarget = getAccountEditTarget(editToDelete);
590
+ assert(typeof editTarget === "string", `deleteAccountEdit target '${editTarget}' not a string`);
591
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
592
+ yield ensureAccountEditsDatabaseLayout(accountId);
593
+ const length = (yield accountEditsDatabase.getItem("length")) || 0;
594
+ const items = yield getDatabaseAsArray(accountEditsDatabase);
595
+ let deletedEdit = false;
596
+ const nextItems = items.filter((item) => {
597
+ if (!deletedEdit && doesStoredAccountEditMatch(item, editToDelete)) {
598
+ deletedEdit = true;
599
+ return false;
600
+ }
601
+ return true;
602
+ });
603
+ const newLength = nextItems.length;
604
+ const promises = [];
605
+ for (let i = 0; i < newLength; i++) {
606
+ promises.push(accountEditsDatabase.setItem(String(i), nextItems[i]));
607
+ }
608
+ if (length > newLength) {
609
+ promises.push(accountEditsDatabase.removeItem(String(length - 1)));
610
+ promises.push(accountEditsDatabase.setItem("length", newLength));
611
+ }
612
+ yield Promise.all(promises);
613
+ yield persistAccountEditsIndexes(accountId, nextItems);
614
+ return deletedEdit;
615
+ });
616
+ const getAccountEdits = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
617
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
618
+ yield ensureAccountEditsDatabaseLayout(accountId);
619
+ const targetToIndices = (yield accountEditsDatabase.getItem(editsTargetToIndicesKey)) || {};
620
+ const edits = {};
621
+ const targets = Object.keys(targetToIndices);
622
+ if (targets.length === 0) {
623
+ return edits;
624
+ }
625
+ for (const target of targets) {
626
+ const targetIndices = targetToIndices[target];
627
+ const targetEdits = yield Promise.all(targetIndices.map((index) => accountEditsDatabase.getItem(String(index))));
628
+ edits[target] = targetEdits.filter(Boolean);
629
+ }
630
+ return edits;
631
+ });
632
+ const getAccountEditsSummary = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
633
+ const accountEditsDatabase = getAccountEditsDatabase(accountId);
634
+ yield ensureAccountEditsDatabaseLayout(accountId);
635
+ return (yield accountEditsDatabase.getItem(editsSummaryKey)) || {};
636
+ });
637
+ const getAccountsEdits = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
638
+ assert(Array.isArray(accountIds), `getAccountsEdits invalid accountIds '${accountIds}' not an array`);
639
+ const promises = [];
640
+ for (const accountId of accountIds) {
641
+ promises.push(getAccountEdits(accountId));
642
+ }
643
+ const accountsEditsArray = yield Promise.all(promises);
644
+ const accountsEdits = {};
645
+ for (const [i, accountId] of accountIds.entries()) {
646
+ accountsEdits[accountId] = accountsEditsArray[i];
647
+ }
648
+ return accountsEdits;
649
+ });
650
+ const getAccountsEditsSummaries = (accountIds) => __awaiter(void 0, void 0, void 0, function* () {
651
+ assert(Array.isArray(accountIds), `getAccountsEditsSummaries invalid accountIds '${accountIds}' not an array`);
652
+ const accountsEditsSummaries = yield Promise.all(accountIds.map((accountId) => getAccountEditsSummary(accountId)));
653
+ return Object.fromEntries(accountIds.map((accountId, index) => [accountId, accountsEditsSummaries[index]]));
654
+ });
655
+ const database = {
656
+ accountsDatabase,
657
+ accountsMetadataDatabase,
658
+ getAccountsVotes,
659
+ getAccountVotes,
660
+ addAccountVote,
661
+ getAccountsComments,
662
+ getAccountComments,
663
+ addAccountComment,
664
+ deleteAccountComment,
665
+ addAccount,
666
+ removeAccount,
667
+ getExportedAccountJson,
668
+ getAccounts,
669
+ getAccount,
670
+ addAccountCommentReply,
671
+ getAccountCommentsReplies,
672
+ getAccountsCommentsReplies,
673
+ getAccountsEdits,
674
+ getAccountEdits,
675
+ getAccountsEditsSummaries,
676
+ getAccountEditsSummary,
677
+ addAccountEdit,
678
+ deleteAccountEdit,
679
+ accountVersion,
680
+ migrate,
681
+ getAccountsDatabaseName,
682
+ getPerAccountDatabaseName,
683
+ };
684
+ export default database;
685
+ //# sourceMappingURL=accounts-database.js.map