@bitsocial/bitsocial-react-hooks 0.1.43 → 0.1.44
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.
- package/CHANGELOG.md +16 -0
- package/README.md +10 -4
- package/dist/hooks/actions/actions.d.ts.map +1 -1
- package/dist/hooks/actions/actions.js +31 -1
- package/dist/hooks/actions/actions.js.map +1 -1
- package/dist/hooks/feeds/feeds.d.ts.map +1 -1
- package/dist/hooks/feeds/feeds.js +8 -3
- package/dist/hooks/feeds/feeds.js.map +1 -1
- package/dist/stores/accounts/accounts-actions.d.ts +5 -0
- package/dist/stores/accounts/accounts-actions.d.ts.map +1 -1
- package/dist/stores/accounts/accounts-actions.js +120 -4
- package/dist/stores/accounts/accounts-actions.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +3 -2
|
@@ -106,6 +106,11 @@ const applyPublicationWordfilters = (publicationType, options, account) => __awa
|
|
|
106
106
|
// Active publish-session tracking for pending comments (Task 3)
|
|
107
107
|
const activePublishSessions = new Map();
|
|
108
108
|
const abandonedPublishSessionIds = new Set();
|
|
109
|
+
// A vote has no pending account comment to delete, so abandoning one restores the account vote that
|
|
110
|
+
// the optimistic write in publishVote replaced. Keyed per comment because that is what the caller
|
|
111
|
+
// abandons, and because rapid votes on the same comment share one revert target.
|
|
112
|
+
const activeVotePublishSessions = new Map();
|
|
113
|
+
const getVotePublishSessionKey = (accountId, commentCid) => `${accountId}:${commentCid}`;
|
|
109
114
|
const getClientsSnapshotForState = (clients) => {
|
|
110
115
|
if (!clients || typeof clients !== "object") {
|
|
111
116
|
return undefined;
|
|
@@ -203,6 +208,42 @@ const abandonAndStopPublishSession = (accountId, index) => {
|
|
|
203
208
|
}
|
|
204
209
|
activePublishSessions.delete(session.sessionId);
|
|
205
210
|
};
|
|
211
|
+
const startVotePublishSession = (accountId, commentCid, previousAccountVote) => {
|
|
212
|
+
const key = getVotePublishSessionKey(accountId, commentCid);
|
|
213
|
+
const session = activeVotePublishSessions.get(key);
|
|
214
|
+
// a retry or a rapid second vote joins the running session so the revert target stays the vote the
|
|
215
|
+
// user had before any of them was published
|
|
216
|
+
if (session)
|
|
217
|
+
return session;
|
|
218
|
+
const startedSession = {
|
|
219
|
+
previousAccountVote,
|
|
220
|
+
publications: new Set(),
|
|
221
|
+
creating: 0,
|
|
222
|
+
};
|
|
223
|
+
activeVotePublishSessions.set(key, startedSession);
|
|
224
|
+
return startedSession;
|
|
225
|
+
};
|
|
226
|
+
const endVotePublishSession = (accountId, commentCid) => {
|
|
227
|
+
activeVotePublishSessions.delete(getVotePublishSessionKey(accountId, commentCid));
|
|
228
|
+
};
|
|
229
|
+
const restoreAccountVote = (accountId, commentCid, previousAccountVote) => __awaiter(void 0, void 0, void 0, function* () {
|
|
230
|
+
var _a;
|
|
231
|
+
const currentAccountVote = (_a = accountsStore.getState().accountsVotes[accountId]) === null || _a === void 0 ? void 0 : _a[commentCid];
|
|
232
|
+
// nothing was written optimistically, so there is nothing to restore
|
|
233
|
+
if (currentAccountVote === previousAccountVote)
|
|
234
|
+
return;
|
|
235
|
+
// the account votes database only appends, so a comment with no earlier vote is restored to an
|
|
236
|
+
// explicit neutral vote rather than by deleting the abandoned one
|
|
237
|
+
const restoredAccountVote = previousAccountVote !== null && previousAccountVote !== void 0 ? previousAccountVote : {
|
|
238
|
+
commentCid,
|
|
239
|
+
vote: 0,
|
|
240
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
241
|
+
};
|
|
242
|
+
yield accountsDatabase.addAccountVote(accountId, restoredAccountVote);
|
|
243
|
+
accountsStore.setState(({ accountsVotes }) => ({
|
|
244
|
+
accountsVotes: Object.assign(Object.assign({}, accountsVotes), { [accountId]: Object.assign(Object.assign({}, accountsVotes[accountId]), { [commentCid]: restoredAccountVote }) }),
|
|
245
|
+
}));
|
|
246
|
+
});
|
|
206
247
|
const isPublishSessionAbandoned = (sessionId) => abandonedPublishSessionIds.has(sessionId);
|
|
207
248
|
const getPublishSession = (sessionId) => activePublishSessions.get(sessionId);
|
|
208
249
|
/** Returns state update or {} when accountComment not yet in state (no-op). Exported for coverage. */
|
|
@@ -1232,11 +1273,31 @@ export const publishVote = (publishVoteOptions, accountName) => __awaiter(void 0
|
|
|
1232
1273
|
const accountComment = accountCommentInfo
|
|
1233
1274
|
? (_a = accountsState.accountsComments[accountCommentInfo.accountId]) === null || _a === void 0 ? void 0 : _a[accountCommentInfo.accountCommentIndex]
|
|
1234
1275
|
: undefined;
|
|
1235
|
-
const
|
|
1236
|
-
|
|
1276
|
+
const previousAccountVote = (_b = accountsState.accountsVotes[account.id]) === null || _b === void 0 ? void 0 : _b[createVoteOptions.commentCid];
|
|
1277
|
+
const storedCreateVoteOptions = addOptimisticVoteMetadata(normalizePublicationOptionsForStore(createVoteOptions), previousAccountVote, getFreshestLoadedComment(createVoteOptions.commentCid, accountComment));
|
|
1278
|
+
const accountVote = Object.assign(Object.assign({}, storedCreateVoteOptions), {
|
|
1279
|
+
// remove signer and author because not needed and they expose private key
|
|
1280
|
+
signer: undefined, author: undefined });
|
|
1281
|
+
const votePublishSession = startVotePublishSession(account.id, createVoteOptions.commentCid, previousAccountVote);
|
|
1282
|
+
const votePublishSessionKey = getVotePublishSessionKey(account.id, createVoteOptions.commentCid);
|
|
1283
|
+
const isVotePublishSessionActive = () => activeVotePublishSessions.get(votePublishSessionKey) === votePublishSession;
|
|
1284
|
+
const createSessionVote = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
1285
|
+
votePublishSession.creating++;
|
|
1286
|
+
try {
|
|
1287
|
+
return backfillPublicationCommunityAddress(yield account.pkc.createVote(createVoteOptions), createVoteOptions);
|
|
1288
|
+
}
|
|
1289
|
+
finally {
|
|
1290
|
+
votePublishSession.creating--;
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
let vote = yield createSessionVote();
|
|
1294
|
+
// abandoned while the vote was being created: nothing was published or written, so stop here
|
|
1295
|
+
if (!isVotePublishSessionActive())
|
|
1296
|
+
return;
|
|
1237
1297
|
let lastChallenge;
|
|
1238
1298
|
const publishAndRetryFailedChallengeVerification = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
1239
1299
|
var _a;
|
|
1300
|
+
votePublishSession.publications.add(vote);
|
|
1240
1301
|
vote.once("challenge", (challenge) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1241
1302
|
lastChallenge = challenge;
|
|
1242
1303
|
publishVoteOptions.onChallenge(challenge, withPublishChallengeAnswersCompat(vote));
|
|
@@ -1248,11 +1309,30 @@ export const publishVote = (publishVoteOptions, accountName) => __awaiter(void 0
|
|
|
1248
1309
|
lastChallenge &&
|
|
1249
1310
|
!hasTerminalChallengeVerificationError(challengeVerification)) {
|
|
1250
1311
|
// publish again automatically on fail
|
|
1312
|
+
votePublishSession.publications.delete(vote);
|
|
1251
1313
|
createVoteOptions = Object.assign(Object.assign({}, createVoteOptions), { timestamp: Math.floor(Date.now() / 1000) });
|
|
1252
|
-
vote =
|
|
1314
|
+
vote = yield createSessionVote();
|
|
1315
|
+
if (!isVotePublishSessionActive())
|
|
1316
|
+
return;
|
|
1253
1317
|
lastChallenge = undefined;
|
|
1254
1318
|
publishAndRetryFailedChallengeVerification();
|
|
1255
1319
|
}
|
|
1320
|
+
else {
|
|
1321
|
+
// terminal: this vote is no longer abandonable
|
|
1322
|
+
votePublishSession.publications.delete(vote);
|
|
1323
|
+
if (challengeVerification.challengeSuccess) {
|
|
1324
|
+
// the verified vote is the one the account has now, so abandoning the rest of the burst
|
|
1325
|
+
// restores it instead of the vote from before the burst
|
|
1326
|
+
votePublishSession.previousAccountVote = accountVote;
|
|
1327
|
+
}
|
|
1328
|
+
// the session ends only once no other publication of the burst is waiting on its challenge
|
|
1329
|
+
// or still being created, and only if a newer session has not replaced it in the meantime
|
|
1330
|
+
if (votePublishSession.publications.size === 0 &&
|
|
1331
|
+
votePublishSession.creating === 0 &&
|
|
1332
|
+
isVotePublishSessionActive()) {
|
|
1333
|
+
activeVotePublishSessions.delete(votePublishSessionKey);
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1256
1336
|
}));
|
|
1257
1337
|
vote.on("error", (error) => { var _a; return (_a = publishVoteOptions.onError) === null || _a === void 0 ? void 0 : _a.call(publishVoteOptions, error, vote); });
|
|
1258
1338
|
// TODO: add publishingState to account votes
|
|
@@ -1271,9 +1351,45 @@ export const publishVote = (publishVoteOptions, accountName) => __awaiter(void 0
|
|
|
1271
1351
|
yield accountsDatabase.addAccountVote(account.id, storedCreateVoteOptions);
|
|
1272
1352
|
log("accountsActions.publishVote", { createVoteOptions });
|
|
1273
1353
|
accountsStore.setState(({ accountsVotes }) => ({
|
|
1274
|
-
accountsVotes: Object.assign(Object.assign({}, accountsVotes), { [account.id]: Object.assign(Object.assign({}, accountsVotes[account.id]), { [storedCreateVoteOptions.commentCid]:
|
|
1354
|
+
accountsVotes: Object.assign(Object.assign({}, accountsVotes), { [account.id]: Object.assign(Object.assign({}, accountsVotes[account.id]), { [storedCreateVoteOptions.commentCid]: accountVote }) }),
|
|
1275
1355
|
}));
|
|
1276
1356
|
});
|
|
1357
|
+
/**
|
|
1358
|
+
* Stops the vote publications still waiting on a challenge for a comment and restores the account
|
|
1359
|
+
* vote they optimistically replaced. Does nothing once the vote has been verified.
|
|
1360
|
+
*/
|
|
1361
|
+
export const abandonVote = (commentCid, accountName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1362
|
+
var _a;
|
|
1363
|
+
const { accounts, accountNamesToAccountIds, activeAccountId } = accountsStore.getState();
|
|
1364
|
+
assert(accounts && accountNamesToAccountIds && activeAccountId, `can't use accountsStore.accountActions before initialized`);
|
|
1365
|
+
let account = accounts[activeAccountId];
|
|
1366
|
+
if (accountName) {
|
|
1367
|
+
const accountId = accountNamesToAccountIds[accountName];
|
|
1368
|
+
account = accounts[accountId];
|
|
1369
|
+
}
|
|
1370
|
+
assert(account === null || account === void 0 ? void 0 : account.id, `accountsActions.abandonVote account.id '${account === null || account === void 0 ? void 0 : account.id}' doesn't exist`);
|
|
1371
|
+
assert(commentCid && typeof commentCid === "string", `accountsActions.abandonVote commentCid '${commentCid}' not a string`);
|
|
1372
|
+
const session = activeVotePublishSessions.get(getVotePublishSessionKey(account.id, commentCid));
|
|
1373
|
+
if (!session)
|
|
1374
|
+
return;
|
|
1375
|
+
endVotePublishSession(account.id, commentCid);
|
|
1376
|
+
for (const publication of session.publications) {
|
|
1377
|
+
try {
|
|
1378
|
+
const stop = (_a = publication === null || publication === void 0 ? void 0 : publication.stop) === null || _a === void 0 ? void 0 : _a.bind(publication);
|
|
1379
|
+
if (typeof stop === "function")
|
|
1380
|
+
yield stop();
|
|
1381
|
+
}
|
|
1382
|
+
catch (e) {
|
|
1383
|
+
log.error("vote.stop() error during abandon", {
|
|
1384
|
+
accountId: account.id,
|
|
1385
|
+
commentCid,
|
|
1386
|
+
error: e,
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
yield restoreAccountVote(account.id, commentCid, session.previousAccountVote);
|
|
1391
|
+
log("accountsActions.abandonVote", { accountId: account.id, commentCid });
|
|
1392
|
+
});
|
|
1277
1393
|
export const publishCommentEdit = (publishCommentEditOptions, accountName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1278
1394
|
const { accounts, accountNamesToAccountIds, activeAccountId } = accountsStore.getState();
|
|
1279
1395
|
assert(accounts && accountNamesToAccountIds && activeAccountId, `can't use accountsStore.accountActions before initialized`);
|