@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.
- package/LICENSE +674 -0
- package/README.md +1365 -0
- package/dist/hooks/accounts/accounts.d.ts +64 -0
- package/dist/hooks/accounts/accounts.d.ts.map +1 -0
- package/dist/hooks/accounts/accounts.js +706 -0
- package/dist/hooks/accounts/accounts.js.map +1 -0
- package/dist/hooks/accounts/index.d.ts +2 -0
- package/dist/hooks/accounts/index.d.ts.map +1 -0
- package/dist/hooks/accounts/index.js +2 -0
- package/dist/hooks/accounts/index.js.map +1 -0
- package/dist/hooks/accounts/utils.d.ts +6 -0
- package/dist/hooks/accounts/utils.d.ts.map +1 -0
- package/dist/hooks/accounts/utils.js +226 -0
- package/dist/hooks/accounts/utils.js.map +1 -0
- package/dist/hooks/actions/actions.d.ts +19 -0
- package/dist/hooks/actions/actions.d.ts.map +1 -0
- package/dist/hooks/actions/actions.js +552 -0
- package/dist/hooks/actions/actions.js.map +1 -0
- package/dist/hooks/actions/index.d.ts +2 -0
- package/dist/hooks/actions/index.d.ts.map +1 -0
- package/dist/hooks/actions/index.js +2 -0
- package/dist/hooks/actions/index.js.map +1 -0
- package/dist/hooks/authors/author-avatars.d.ts +28 -0
- package/dist/hooks/authors/author-avatars.d.ts.map +1 -0
- package/dist/hooks/authors/author-avatars.js +191 -0
- package/dist/hooks/authors/author-avatars.js.map +1 -0
- package/dist/hooks/authors/authors.d.ts +37 -0
- package/dist/hooks/authors/authors.d.ts.map +1 -0
- package/dist/hooks/authors/authors.js +509 -0
- package/dist/hooks/authors/authors.js.map +1 -0
- package/dist/hooks/authors/index.d.ts +2 -0
- package/dist/hooks/authors/index.d.ts.map +1 -0
- package/dist/hooks/authors/index.js +2 -0
- package/dist/hooks/authors/index.js.map +1 -0
- package/dist/hooks/authors/utils.d.ts +4 -0
- package/dist/hooks/authors/utils.d.ts.map +1 -0
- package/dist/hooks/authors/utils.js +21 -0
- package/dist/hooks/authors/utils.js.map +1 -0
- package/dist/hooks/comments.d.ts +17 -0
- package/dist/hooks/comments.d.ts.map +1 -0
- package/dist/hooks/comments.js +351 -0
- package/dist/hooks/comments.js.map +1 -0
- package/dist/hooks/communities.d.ts +31 -0
- package/dist/hooks/communities.d.ts.map +1 -0
- package/dist/hooks/communities.js +389 -0
- package/dist/hooks/communities.js.map +1 -0
- package/dist/hooks/feeds/feeds.d.ts +18 -0
- package/dist/hooks/feeds/feeds.d.ts.map +1 -0
- package/dist/hooks/feeds/feeds.js +315 -0
- package/dist/hooks/feeds/feeds.js.map +1 -0
- package/dist/hooks/feeds/index.d.ts +2 -0
- package/dist/hooks/feeds/index.d.ts.map +1 -0
- package/dist/hooks/feeds/index.js +2 -0
- package/dist/hooks/feeds/index.js.map +1 -0
- package/dist/hooks/pkc-rpc.d.ts +7 -0
- package/dist/hooks/pkc-rpc.d.ts.map +1 -0
- package/dist/hooks/pkc-rpc.js +88 -0
- package/dist/hooks/pkc-rpc.js.map +1 -0
- package/dist/hooks/replies.d.ts +5 -0
- package/dist/hooks/replies.d.ts.map +1 -0
- package/dist/hooks/replies.js +155 -0
- package/dist/hooks/replies.js.map +1 -0
- package/dist/hooks/states.d.ts +15 -0
- package/dist/hooks/states.d.ts.map +1 -0
- package/dist/hooks/states.js +213 -0
- package/dist/hooks/states.js.map +1 -0
- package/dist/hooks/utils/use-interval.d.ts +3 -0
- package/dist/hooks/utils/use-interval.d.ts.map +1 -0
- package/dist/hooks/utils/use-interval.js +36 -0
- package/dist/hooks/utils/use-interval.js.map +1 -0
- package/dist/hooks/utils/use-previous.d.ts +1 -0
- package/dist/hooks/utils/use-previous.js +10 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +128 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/chain/chain.d.ts +36 -0
- package/dist/lib/chain/chain.d.ts.map +1 -0
- package/dist/lib/chain/chain.js +195 -0
- package/dist/lib/chain/chain.js.map +1 -0
- package/dist/lib/chain/index.d.ts +4 -0
- package/dist/lib/chain/index.d.ts.map +1 -0
- package/dist/lib/chain/index.js +4 -0
- package/dist/lib/chain/index.js.map +1 -0
- package/dist/lib/community-address.d.ts +6 -0
- package/dist/lib/community-address.d.ts.map +1 -0
- package/dist/lib/community-address.js +26 -0
- package/dist/lib/community-address.js.map +1 -0
- package/dist/lib/community-ref.d.ts +23 -0
- package/dist/lib/community-ref.d.ts.map +1 -0
- package/dist/lib/community-ref.js +113 -0
- package/dist/lib/community-ref.js.map +1 -0
- package/dist/lib/debug-utils.d.ts +9 -0
- package/dist/lib/debug-utils.d.ts.map +1 -0
- package/dist/lib/debug-utils.js +21 -0
- package/dist/lib/debug-utils.js.map +1 -0
- package/dist/lib/feed-sort-type.d.ts +2 -0
- package/dist/lib/feed-sort-type.d.ts.map +1 -0
- package/dist/lib/feed-sort-type.js +22 -0
- package/dist/lib/feed-sort-type.js.map +1 -0
- package/dist/lib/localforage-lru/index.d.ts +3 -0
- package/dist/lib/localforage-lru/index.d.ts.map +1 -0
- package/dist/lib/localforage-lru/index.js +46 -0
- package/dist/lib/localforage-lru/index.js.map +1 -0
- package/dist/lib/localforage-lru/localforage-lru.d.ts +6 -0
- package/dist/lib/localforage-lru/localforage-lru.d.ts.map +1 -0
- package/dist/lib/localforage-lru/localforage-lru.js +182 -0
- package/dist/lib/localforage-lru/localforage-lru.js.map +1 -0
- package/dist/lib/pkc-compat.d.ts +25 -0
- package/dist/lib/pkc-compat.d.ts.map +1 -0
- package/dist/lib/pkc-compat.js +131 -0
- package/dist/lib/pkc-compat.js.map +1 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.d.ts +3 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.d.ts.map +1 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.js +280 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.js.map +1 -0
- package/dist/lib/pkc-js/index.d.ts +11 -0
- package/dist/lib/pkc-js/index.d.ts.map +1 -0
- package/dist/lib/pkc-js/index.js +85 -0
- package/dist/lib/pkc-js/index.js.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.d.ts +3 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.d.ts.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.js +1235 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.js.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock.d.ts +137 -0
- package/dist/lib/pkc-js/pkc-js-mock.d.ts.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock.js +644 -0
- package/dist/lib/pkc-js/pkc-js-mock.js.map +1 -0
- package/dist/lib/polyfill.d.ts +3 -0
- package/dist/lib/polyfill.d.ts.map +1 -0
- package/dist/lib/polyfill.js +14 -0
- package/dist/lib/polyfill.js.map +1 -0
- package/dist/lib/protocol-compat.d.ts +14 -0
- package/dist/lib/protocol-compat.d.ts.map +1 -0
- package/dist/lib/protocol-compat.js +67 -0
- package/dist/lib/protocol-compat.js.map +1 -0
- package/dist/lib/test-utils.d.ts +29 -0
- package/dist/lib/test-utils.d.ts.map +1 -0
- package/dist/lib/test-utils.js +184 -0
- package/dist/lib/test-utils.js.map +1 -0
- package/dist/lib/utils/comment-moderation.d.ts +4 -0
- package/dist/lib/utils/comment-moderation.d.ts.map +1 -0
- package/dist/lib/utils/comment-moderation.js +56 -0
- package/dist/lib/utils/comment-moderation.js.map +1 -0
- package/dist/lib/utils/index.d.ts +4 -0
- package/dist/lib/utils/index.d.ts.map +1 -0
- package/dist/lib/utils/index.js +4 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/utils.d.ts +23 -0
- package/dist/lib/utils/utils.d.ts.map +1 -0
- package/dist/lib/utils/utils.js +375 -0
- package/dist/lib/utils/utils.js.map +1 -0
- package/dist/lib/validator.d.ts +30 -0
- package/dist/lib/validator.d.ts.map +1 -0
- package/dist/lib/validator.js +307 -0
- package/dist/lib/validator.js.map +1 -0
- package/dist/stores/accounts/account-generator.d.ts +51 -0
- package/dist/stores/accounts/account-generator.d.ts.map +1 -0
- package/dist/stores/accounts/account-generator.js +160 -0
- package/dist/stores/accounts/account-generator.js.map +1 -0
- package/dist/stores/accounts/accounts-actions-internal.d.ts +8 -0
- package/dist/stores/accounts/accounts-actions-internal.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-actions-internal.js +403 -0
- package/dist/stores/accounts/accounts-actions-internal.js.map +1 -0
- package/dist/stores/accounts/accounts-actions.d.ts +46 -0
- package/dist/stores/accounts/accounts-actions.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-actions.js +1341 -0
- package/dist/stores/accounts/accounts-actions.js.map +1 -0
- package/dist/stores/accounts/accounts-database.d.ts +34 -0
- package/dist/stores/accounts/accounts-database.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-database.js +685 -0
- package/dist/stores/accounts/accounts-database.js.map +1 -0
- package/dist/stores/accounts/accounts-store.d.ts +32 -0
- package/dist/stores/accounts/accounts-store.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-store.js +169 -0
- package/dist/stores/accounts/accounts-store.js.map +1 -0
- package/dist/stores/accounts/index.d.ts +4 -0
- package/dist/stores/accounts/index.d.ts.map +1 -0
- package/dist/stores/accounts/index.js +4 -0
- package/dist/stores/accounts/index.js.map +1 -0
- package/dist/stores/accounts/utils.d.ts +49 -0
- package/dist/stores/accounts/utils.d.ts.map +1 -0
- package/dist/stores/accounts/utils.js +419 -0
- package/dist/stores/accounts/utils.js.map +1 -0
- package/dist/stores/authors-comments/authors-comments-store.d.ts +37 -0
- package/dist/stores/authors-comments/authors-comments-store.d.ts.map +1 -0
- package/dist/stores/authors-comments/authors-comments-store.js +338 -0
- package/dist/stores/authors-comments/authors-comments-store.js.map +1 -0
- package/dist/stores/authors-comments/index.d.ts +4 -0
- package/dist/stores/authors-comments/index.d.ts.map +1 -0
- package/dist/stores/authors-comments/index.js +4 -0
- package/dist/stores/authors-comments/index.js.map +1 -0
- package/dist/stores/authors-comments/utils.d.ts +14 -0
- package/dist/stores/authors-comments/utils.d.ts.map +1 -0
- package/dist/stores/authors-comments/utils.js +81 -0
- package/dist/stores/authors-comments/utils.js.map +1 -0
- package/dist/stores/comments/comments-store.d.ts +19 -0
- package/dist/stores/comments/comments-store.d.ts.map +1 -0
- package/dist/stores/comments/comments-store.js +385 -0
- package/dist/stores/comments/comments-store.js.map +1 -0
- package/dist/stores/comments/index.d.ts +4 -0
- package/dist/stores/comments/index.d.ts.map +1 -0
- package/dist/stores/comments/index.js +4 -0
- package/dist/stores/comments/index.js.map +1 -0
- package/dist/stores/communities/communities-store.d.ts +17 -0
- package/dist/stores/communities/communities-store.d.ts.map +1 -0
- package/dist/stores/communities/communities-store.js +304 -0
- package/dist/stores/communities/communities-store.js.map +1 -0
- package/dist/stores/communities/index.d.ts +4 -0
- package/dist/stores/communities/index.d.ts.map +1 -0
- package/dist/stores/communities/index.js +4 -0
- package/dist/stores/communities/index.js.map +1 -0
- package/dist/stores/communities-pages/communities-pages-store.d.ts +23 -0
- package/dist/stores/communities-pages/communities-pages-store.d.ts.map +1 -0
- package/dist/stores/communities-pages/communities-pages-store.js +316 -0
- package/dist/stores/communities-pages/communities-pages-store.js.map +1 -0
- package/dist/stores/communities-pages/index.d.ts +4 -0
- package/dist/stores/communities-pages/index.d.ts.map +1 -0
- package/dist/stores/communities-pages/index.js +4 -0
- package/dist/stores/communities-pages/index.js.map +1 -0
- package/dist/stores/feeds/feed-sorter.d.ts +5 -0
- package/dist/stores/feeds/feed-sorter.d.ts.map +1 -0
- package/dist/stores/feeds/feed-sorter.js +135 -0
- package/dist/stores/feeds/feed-sorter.js.map +1 -0
- package/dist/stores/feeds/feeds-store.d.ts +25 -0
- package/dist/stores/feeds/feeds-store.d.ts.map +1 -0
- package/dist/stores/feeds/feeds-store.js +459 -0
- package/dist/stores/feeds/feeds-store.js.map +1 -0
- package/dist/stores/feeds/index.d.ts +4 -0
- package/dist/stores/feeds/index.d.ts.map +1 -0
- package/dist/stores/feeds/index.js +4 -0
- package/dist/stores/feeds/index.js.map +1 -0
- package/dist/stores/feeds/utils.d.ts +43 -0
- package/dist/stores/feeds/utils.d.ts.map +1 -0
- package/dist/stores/feeds/utils.js +736 -0
- package/dist/stores/feeds/utils.js.map +1 -0
- package/dist/stores/replies/index.d.ts +4 -0
- package/dist/stores/replies/index.d.ts.map +1 -0
- package/dist/stores/replies/index.js +4 -0
- package/dist/stores/replies/index.js.map +1 -0
- package/dist/stores/replies/replies-comments-store.d.ts +8 -0
- package/dist/stores/replies/replies-comments-store.d.ts.map +1 -0
- package/dist/stores/replies/replies-comments-store.js +23 -0
- package/dist/stores/replies/replies-comments-store.js.map +1 -0
- package/dist/stores/replies/replies-store.d.ts +29 -0
- package/dist/stores/replies/replies-store.d.ts.map +1 -0
- package/dist/stores/replies/replies-store.js +413 -0
- package/dist/stores/replies/replies-store.js.map +1 -0
- package/dist/stores/replies/utils.d.ts +25 -0
- package/dist/stores/replies/utils.d.ts.map +1 -0
- package/dist/stores/replies/utils.js +549 -0
- package/dist/stores/replies/utils.js.map +1 -0
- package/dist/stores/replies-pages/index.d.ts +4 -0
- package/dist/stores/replies-pages/index.d.ts.map +1 -0
- package/dist/stores/replies-pages/index.js +4 -0
- package/dist/stores/replies-pages/index.js.map +1 -0
- package/dist/stores/replies-pages/replies-pages-store.d.ts +20 -0
- package/dist/stores/replies-pages/replies-pages-store.d.ts.map +1 -0
- package/dist/stores/replies-pages/replies-pages-store.js +270 -0
- package/dist/stores/replies-pages/replies-pages-store.js.map +1 -0
- package/dist/stores/replies-pages/utils.d.ts +3 -0
- package/dist/stores/replies-pages/utils.d.ts.map +1 -0
- package/dist/stores/replies-pages/utils.js +43 -0
- package/dist/stores/replies-pages/utils.js.map +1 -0
- package/dist/types.d.ts +638 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +160 -0
|
@@ -0,0 +1,736 @@
|
|
|
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 { getCommunityPages, getCommunityFirstPageCid } from "../communities-pages/index.js";
|
|
11
|
+
import accountsStore from "../accounts/index.js";
|
|
12
|
+
import feedSorter from "./feed-sorter.js";
|
|
13
|
+
import { communityPostsCacheExpired, commentIsValid, removeInvalidComments } from "../../lib/utils/index.js";
|
|
14
|
+
import { getCommentCommunityAddress, normalizeCommentCommunityAddress } from "../../lib/pkc-compat.js";
|
|
15
|
+
import { doesAddressMatchCommunityRef, getCommunityRefKeys, getMatchingCommunityRefKeys, } from "../../lib/community-ref.js";
|
|
16
|
+
import Logger from "@pkcprotocol/pkc-logger";
|
|
17
|
+
const log = Logger("bitsocial-react-hooks:feeds:stores");
|
|
18
|
+
const getFeedCommunityRefs = (feedOptions) => feedOptions.communities || [];
|
|
19
|
+
const getFeedCommunityKeys = (feedOptions) => feedOptions.communityKeys || getCommunityRefKeys(getFeedCommunityRefs(feedOptions));
|
|
20
|
+
const getCommentFreshness = (comment) => { var _a, _b; return Math.max((_a = comment === null || comment === void 0 ? void 0 : comment.updatedAt) !== null && _a !== void 0 ? _a : 0, (_b = comment === null || comment === void 0 ? void 0 : comment.timestamp) !== null && _b !== void 0 ? _b : 0, 0); };
|
|
21
|
+
const commentMatchesModQueue = (comment, modQueue) => {
|
|
22
|
+
const modQueueName = modQueue === null || modQueue === void 0 ? void 0 : modQueue[0];
|
|
23
|
+
if (!modQueueName) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
if (modQueueName === "pendingApproval") {
|
|
27
|
+
return (comment === null || comment === void 0 ? void 0 : comment.pendingApproval) === true;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
};
|
|
31
|
+
const getFeedPost = (post, communityRef, community, modQueue, freshestComments) => {
|
|
32
|
+
const normalizedPost = normalizeCommentCommunityAddress(post);
|
|
33
|
+
const freshestComment = post.cid
|
|
34
|
+
? normalizeCommentCommunityAddress(freshestComments === null || freshestComments === void 0 ? void 0 : freshestComments[post.cid])
|
|
35
|
+
: undefined;
|
|
36
|
+
const postCommunityAddress = getCommentCommunityAddress(normalizedPost);
|
|
37
|
+
if (!doesAddressMatchCommunityRef(postCommunityAddress, communityRef, community)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!commentMatchesModQueue(normalizedPost, modQueue)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (!freshestComment ||
|
|
44
|
+
getCommentFreshness(freshestComment) <= getCommentFreshness(normalizedPost)) {
|
|
45
|
+
return normalizedPost;
|
|
46
|
+
}
|
|
47
|
+
if (!commentMatchesModQueue(freshestComment, modQueue)) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!doesAddressMatchCommunityRef(getCommentCommunityAddress(freshestComment), communityRef, community)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
return freshestComment;
|
|
54
|
+
};
|
|
55
|
+
const reconcileLoadedModQueueFeed = (feedOptions, loadedFeed, filteredSortedFeed) => {
|
|
56
|
+
var _a;
|
|
57
|
+
if (!((_a = feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.modQueue) === null || _a === void 0 ? void 0 : _a[0]) || !(loadedFeed === null || loadedFeed === void 0 ? void 0 : loadedFeed.length)) {
|
|
58
|
+
return loadedFeed;
|
|
59
|
+
}
|
|
60
|
+
const filteredSortedFeedByCid = new Map();
|
|
61
|
+
for (const post of filteredSortedFeed) {
|
|
62
|
+
if (post.cid) {
|
|
63
|
+
filteredSortedFeedByCid.set(post.cid, post);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
let changed = false;
|
|
67
|
+
const nextLoadedFeed = [];
|
|
68
|
+
for (const post of loadedFeed) {
|
|
69
|
+
if (!post.cid) {
|
|
70
|
+
nextLoadedFeed.push(post);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const sourcePost = filteredSortedFeedByCid.get(post.cid);
|
|
74
|
+
if (!sourcePost) {
|
|
75
|
+
changed = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (getCommentFreshness(sourcePost) > getCommentFreshness(post)) {
|
|
79
|
+
nextLoadedFeed.push(sourcePost);
|
|
80
|
+
changed = true;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
nextLoadedFeed.push(post);
|
|
84
|
+
}
|
|
85
|
+
return changed ? nextLoadedFeed : loadedFeed;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Calculate the feeds from all the loaded community pages, filter and sort them
|
|
89
|
+
*/
|
|
90
|
+
export const getFilteredSortedFeeds = (feedsOptions, communities, communitiesPages, accounts, freshestComments) => {
|
|
91
|
+
var _a, _b, _c, _d;
|
|
92
|
+
// calculate each feed
|
|
93
|
+
let feeds = {};
|
|
94
|
+
for (const feedName in feedsOptions) {
|
|
95
|
+
const communityRefs = getFeedCommunityRefs(feedsOptions[feedName]);
|
|
96
|
+
const communityKeys = getFeedCommunityKeys(feedsOptions[feedName]);
|
|
97
|
+
let { sortType, accountId, filter, newerThan, modQueue } = feedsOptions[feedName];
|
|
98
|
+
const newerThanTimestamp = newerThan ? Math.floor(Date.now() / 1000) - newerThan : undefined;
|
|
99
|
+
let pageType = "posts";
|
|
100
|
+
if (modQueue === null || modQueue === void 0 ? void 0 : modQueue[0]) {
|
|
101
|
+
// TODO: allow multiple modQueue at once, fow now only use first in array
|
|
102
|
+
sortType = modQueue[0];
|
|
103
|
+
pageType = "modQueue";
|
|
104
|
+
}
|
|
105
|
+
// find all fetched posts
|
|
106
|
+
const bufferedFeedPosts = [];
|
|
107
|
+
// add each comment from each page, do not filter at this stage, filter after sorting
|
|
108
|
+
for (const [communityIndex, communityKey] of communityKeys.entries()) {
|
|
109
|
+
const communityRef = communityRefs[communityIndex];
|
|
110
|
+
// community hasn't loaded yet
|
|
111
|
+
const community = communities[communityKey];
|
|
112
|
+
if (!community || !communityRef) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// if cache is expired and has internet access, don't use, wait for next community update
|
|
116
|
+
if (communityPostsCacheExpired(community) && window.navigator.onLine) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
// use community preloaded posts if any
|
|
120
|
+
const preloadedPosts = getPreloadedPosts(community, sortType);
|
|
121
|
+
if (preloadedPosts) {
|
|
122
|
+
for (const post of preloadedPosts) {
|
|
123
|
+
// posts are manually validated, could have fake communityAddress
|
|
124
|
+
if (!doesAddressMatchCommunityRef(getCommentCommunityAddress(post), communityRef, community)) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const nextPost = getFeedPost(post, communityRef, community, modQueue, freshestComments);
|
|
128
|
+
if (nextPost) {
|
|
129
|
+
bufferedFeedPosts.push(nextPost);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// add all posts from community pages
|
|
134
|
+
const communityPages = getCommunityPages(community, sortType, communitiesPages, pageType, accountId);
|
|
135
|
+
for (const communityPage of communityPages) {
|
|
136
|
+
if (communityPage === null || communityPage === void 0 ? void 0 : communityPage.comments) {
|
|
137
|
+
for (const post of communityPage.comments) {
|
|
138
|
+
// posts are manually validated, could have fake communityAddress
|
|
139
|
+
if (!doesAddressMatchCommunityRef(getCommentCommunityAddress(post), communityRef, community)) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
const nextPost = getFeedPost(post, communityRef, community, modQueue, freshestComments);
|
|
143
|
+
if (nextPost) {
|
|
144
|
+
bufferedFeedPosts.push(nextPost);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// sort the feed before filtering to get more accurate results
|
|
151
|
+
const originalSortType = feedsOptions[feedName].sortType;
|
|
152
|
+
const sortedBufferedFeedPosts = feedSorter.sort(originalSortType, bufferedFeedPosts);
|
|
153
|
+
// filter the feed
|
|
154
|
+
const filteredSortedBufferedFeedPosts = [];
|
|
155
|
+
for (const post of sortedBufferedFeedPosts) {
|
|
156
|
+
// address is blocked
|
|
157
|
+
if (((_a = accounts[accountId]) === null || _a === void 0 ? void 0 : _a.blockedAddresses[getCommentCommunityAddress(post) || ""]) ||
|
|
158
|
+
(((_b = post.author) === null || _b === void 0 ? void 0 : _b.address) && ((_c = accounts[accountId]) === null || _c === void 0 ? void 0 : _c.blockedAddresses[post.author.address]))) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
// comment cid is blocked
|
|
162
|
+
if ((_d = accounts[accountId]) === null || _d === void 0 ? void 0 : _d.blockedCids[post.cid]) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
// if a feed has more than 1 sub, don't include pinned posts
|
|
166
|
+
// TODO: add test to check if pinned are filtered
|
|
167
|
+
if (post.pinned && communityKeys.length > 1) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
// feedOptions filter function
|
|
171
|
+
if (filter && !filter.filter(post)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
// filter posts older than newerThan option
|
|
175
|
+
if (newerThanTimestamp) {
|
|
176
|
+
if (sortType === "active") {
|
|
177
|
+
if ((post.lastReplyTimestamp || post.timestamp) <= newerThanTimestamp) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
if (post.timestamp <= newerThanTimestamp) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
filteredSortedBufferedFeedPosts.push(post);
|
|
188
|
+
}
|
|
189
|
+
feeds[feedName] = filteredSortedBufferedFeedPosts;
|
|
190
|
+
}
|
|
191
|
+
return feeds;
|
|
192
|
+
};
|
|
193
|
+
const getPreloadedPosts = (community, sortType) => {
|
|
194
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
195
|
+
let preloadedPosts = (_c = (_b = (_a = community.posts) === null || _a === void 0 ? void 0 : _a.pages) === null || _b === void 0 ? void 0 : _b[sortType]) === null || _c === void 0 ? void 0 : _c.comments;
|
|
196
|
+
if (preloadedPosts) {
|
|
197
|
+
return preloadedPosts;
|
|
198
|
+
}
|
|
199
|
+
const hasPageCids = Object.keys(((_d = community.posts) === null || _d === void 0 ? void 0 : _d.pageCids) || {}).length !== 0;
|
|
200
|
+
if (hasPageCids) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const pages = Object.values(((_e = community.posts) === null || _e === void 0 ? void 0 : _e.pages) || {});
|
|
204
|
+
if (!pages.length) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const nextCids = pages.map((page) => page === null || page === void 0 ? void 0 : page.nextCid).filter((nextCid) => !!nextCid);
|
|
208
|
+
if (nextCids.length > 0) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
// if has a preloaded page, but no pageCids and no nextCids, it means all posts fit in a single preloaded page
|
|
212
|
+
// so any sort type can be used, and later be resorted by the client
|
|
213
|
+
if ((_g = (_f = pages[0]) === null || _f === void 0 ? void 0 : _f.comments) === null || _g === void 0 ? void 0 : _g.length) {
|
|
214
|
+
return pages[0].comments;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
export const getLoadedFeeds = (feedsOptions, filteredSortedFeeds, loadedFeeds, bufferedFeeds, accounts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
218
|
+
var _a;
|
|
219
|
+
const nextLoadedFeeds = Object.assign({}, loadedFeeds);
|
|
220
|
+
let loadedFeedsChanged = false;
|
|
221
|
+
for (const feedName in feedsOptions) {
|
|
222
|
+
const { pageNumber, postsPerPage, accountId } = feedsOptions[feedName];
|
|
223
|
+
const pkc = (_a = accounts[accountId]) === null || _a === void 0 ? void 0 : _a.pkc;
|
|
224
|
+
const loadedFeedPostCount = pageNumber * postsPerPage;
|
|
225
|
+
const currentLoadedFeed = reconcileLoadedModQueueFeed(feedsOptions[feedName], loadedFeeds[feedName] || [], filteredSortedFeeds[feedName] || []);
|
|
226
|
+
if (currentLoadedFeed !== loadedFeeds[feedName]) {
|
|
227
|
+
nextLoadedFeeds[feedName] = currentLoadedFeed;
|
|
228
|
+
loadedFeedsChanged = true;
|
|
229
|
+
}
|
|
230
|
+
const missingPostsCount = loadedFeedPostCount - currentLoadedFeed.filter((post) => post.index === undefined).length;
|
|
231
|
+
// get new posts from buffered feed
|
|
232
|
+
const bufferedFeed = bufferedFeeds[feedName] || [];
|
|
233
|
+
let missingPosts = [];
|
|
234
|
+
for (const post of bufferedFeed) {
|
|
235
|
+
if (missingPosts.length >= missingPostsCount) {
|
|
236
|
+
missingPosts = yield removeInvalidComments(missingPosts, { validateReplies: false }, pkc);
|
|
237
|
+
// only stop if there were no invalid comments
|
|
238
|
+
if (missingPosts.length >= missingPostsCount) {
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
missingPosts.push(post);
|
|
243
|
+
}
|
|
244
|
+
// the current loaded feed already exist and doesn't need new posts
|
|
245
|
+
if (missingPosts.length === 0 &&
|
|
246
|
+
loadedFeeds[feedName] &&
|
|
247
|
+
currentLoadedFeed === loadedFeeds[feedName]) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
nextLoadedFeeds[feedName] = [...currentLoadedFeed, ...missingPosts];
|
|
251
|
+
if (missingPosts.length > 0) {
|
|
252
|
+
loadedFeedsChanged = true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
// add account comments
|
|
256
|
+
const accountCommentsChangedFeeds = addAccountsComments(feedsOptions, nextLoadedFeeds);
|
|
257
|
+
// do nothing if there are no missing posts
|
|
258
|
+
if (!loadedFeedsChanged && !accountCommentsChangedFeeds) {
|
|
259
|
+
return loadedFeeds;
|
|
260
|
+
}
|
|
261
|
+
return nextLoadedFeeds;
|
|
262
|
+
});
|
|
263
|
+
export const addAccountsComments = (feedsOptions, loadedFeeds) => {
|
|
264
|
+
let loadedFeedsChanged = false;
|
|
265
|
+
const accountsComments = accountsStore.getState().accountsComments || {};
|
|
266
|
+
for (const feedName in feedsOptions) {
|
|
267
|
+
const { accountId, accountComments: accountCommentsOptions } = feedsOptions[feedName];
|
|
268
|
+
const communityRefs = getFeedCommunityRefs(feedsOptions[feedName]);
|
|
269
|
+
const { newerThan, append } = accountCommentsOptions || {};
|
|
270
|
+
if (!newerThan) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const newerThanTimestamp = newerThan === Infinity ? 0 : Math.floor(Date.now() / 1000) - newerThan;
|
|
274
|
+
const isNewerThan = (post) => post.timestamp > newerThanTimestamp;
|
|
275
|
+
const accountComments = accountsComments[accountId] || [];
|
|
276
|
+
const accountPosts = accountComments.filter((comment) => {
|
|
277
|
+
// is a reply, not a post
|
|
278
|
+
if (comment.parentCid || comment.depth > 0) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
if (!isNewerThan(comment)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
return (getMatchingCommunityRefKeys(communityRefs, getCommentCommunityAddress(comment)).length > 0);
|
|
285
|
+
});
|
|
286
|
+
const validAccountIndices = new Set(accountPosts.map((p) => p.index));
|
|
287
|
+
const accountCidToPost = new Map();
|
|
288
|
+
for (const p of accountPosts) {
|
|
289
|
+
if (p.cid)
|
|
290
|
+
accountCidToPost.set(p.cid, p);
|
|
291
|
+
}
|
|
292
|
+
let loadedFeed = loadedFeeds[feedName] || [];
|
|
293
|
+
// prune stale local-account entries and replace when cid matches but index changed
|
|
294
|
+
const prunedLoadedFeed = [];
|
|
295
|
+
for (const post of loadedFeed) {
|
|
296
|
+
if (post.index === undefined) {
|
|
297
|
+
prunedLoadedFeed.push(post);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (!validAccountIndices.has(post.index)) {
|
|
301
|
+
loadedFeedsChanged = true;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (post.cid) {
|
|
305
|
+
const freshAccountPost = accountCidToPost.get(post.cid);
|
|
306
|
+
if (freshAccountPost && freshAccountPost.index !== post.index) {
|
|
307
|
+
prunedLoadedFeed.push(freshAccountPost);
|
|
308
|
+
loadedFeedsChanged = true;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
prunedLoadedFeed.push(post);
|
|
313
|
+
}
|
|
314
|
+
loadedFeed = loadedFeeds[feedName] = prunedLoadedFeed;
|
|
315
|
+
if (!accountPosts.length) {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
// if a loaded comment doesn't have a cid, then it's pending
|
|
319
|
+
// and pending account comments should always have unique timestamps
|
|
320
|
+
const loadedFeedMap = new Map();
|
|
321
|
+
loadedFeed.forEach((post, loadedFeedIndex) => {
|
|
322
|
+
if (post.cid)
|
|
323
|
+
loadedFeedMap.set(post.cid, loadedFeedIndex);
|
|
324
|
+
if (post.index)
|
|
325
|
+
loadedFeedMap.set(post.index, loadedFeedIndex);
|
|
326
|
+
if (!post.cid)
|
|
327
|
+
loadedFeedMap.set(post.timestamp, loadedFeedIndex);
|
|
328
|
+
});
|
|
329
|
+
for (const accountPost of accountPosts) {
|
|
330
|
+
// account post with cid already added
|
|
331
|
+
if (accountPost.cid && loadedFeedMap.has(accountPost.cid)) {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
// account post without cid already added, but now we have the cid
|
|
335
|
+
if (accountPost.cid && loadedFeedMap.has(accountPost.index)) {
|
|
336
|
+
const loadedFeedIndex = loadedFeedMap.get(accountPost.index);
|
|
337
|
+
// update the feed with the accountPost.cid now that we have it
|
|
338
|
+
loadedFeed[loadedFeedIndex] = accountPost;
|
|
339
|
+
loadedFeedsChanged = true;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (loadedFeedMap.has(accountPost.index)) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
// pending account post without cid already added
|
|
346
|
+
if (!accountPost.cid && loadedFeedMap.has(accountPost.timestamp)) {
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (append) {
|
|
350
|
+
loadedFeed.push(accountPost);
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
loadedFeed.unshift(accountPost);
|
|
354
|
+
}
|
|
355
|
+
loadedFeedsChanged = true;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return loadedFeedsChanged;
|
|
359
|
+
};
|
|
360
|
+
export const getBufferedFeedsWithoutLoadedFeeds = (bufferedFeeds, loadedFeeds) => {
|
|
361
|
+
var _a, _b, _c, _d, _e;
|
|
362
|
+
// contruct a list of posts already loaded to remove them from buffered feeds
|
|
363
|
+
const loadedFeedsPosts = {};
|
|
364
|
+
for (const feedName in loadedFeeds) {
|
|
365
|
+
loadedFeedsPosts[feedName] = new Set();
|
|
366
|
+
for (const post of loadedFeeds[feedName]) {
|
|
367
|
+
loadedFeedsPosts[feedName].add(post.cid);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const newBufferedFeeds = {};
|
|
371
|
+
for (const feedName in bufferedFeeds) {
|
|
372
|
+
newBufferedFeeds[feedName] = [];
|
|
373
|
+
let bufferedFeedPostChanged = false;
|
|
374
|
+
for (const [i, post] of bufferedFeeds[feedName].entries()) {
|
|
375
|
+
if ((_a = loadedFeedsPosts[feedName]) === null || _a === void 0 ? void 0 : _a.has(post.cid)) {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
newBufferedFeeds[feedName].push(post);
|
|
379
|
+
if (!bufferedFeedPostChanged &&
|
|
380
|
+
(((_b = newBufferedFeeds[feedName][i]) === null || _b === void 0 ? void 0 : _b.cid) !== ((_c = bufferedFeeds[feedName][i]) === null || _c === void 0 ? void 0 : _c.cid) ||
|
|
381
|
+
(((_d = newBufferedFeeds[feedName][i]) === null || _d === void 0 ? void 0 : _d.updatedAt) || 0) >
|
|
382
|
+
(((_e = bufferedFeeds[feedName][i]) === null || _e === void 0 ? void 0 : _e.updatedAt) || 0))) {
|
|
383
|
+
bufferedFeedPostChanged = true;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (!bufferedFeedPostChanged &&
|
|
387
|
+
newBufferedFeeds[feedName].length === bufferedFeeds[feedName].length) {
|
|
388
|
+
newBufferedFeeds[feedName] = bufferedFeeds[feedName];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return newBufferedFeeds;
|
|
392
|
+
};
|
|
393
|
+
export const getUpdatedFeeds = (feedsOptions, filteredSortedFeeds, updatedFeeds, loadedFeeds, accounts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
394
|
+
var _a, _b, _c;
|
|
395
|
+
// contruct a list of posts already loaded to remove them from buffered feeds
|
|
396
|
+
const updatedFeedsPosts = {};
|
|
397
|
+
for (const feedName in updatedFeeds) {
|
|
398
|
+
updatedFeedsPosts[feedName] = {};
|
|
399
|
+
for (const [index, updatedPost] of updatedFeeds[feedName].entries()) {
|
|
400
|
+
updatedFeedsPosts[feedName][updatedPost.cid] = { index, updatedPost };
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const newUpdatedFeeds = Object.assign({}, updatedFeeds);
|
|
404
|
+
for (const feedName in filteredSortedFeeds) {
|
|
405
|
+
const pkc = (_b = accounts[(_a = feedsOptions[feedName]) === null || _a === void 0 ? void 0 : _a.accountId]) === null || _b === void 0 ? void 0 : _b.pkc;
|
|
406
|
+
const updatedFeed = [...(updatedFeeds[feedName] || [])];
|
|
407
|
+
const onlyHasNewPosts = updatedFeed.length === 0;
|
|
408
|
+
let updatedFeedChanged = false;
|
|
409
|
+
// add new posts from loadedFeed posts
|
|
410
|
+
while (updatedFeed.length < loadedFeeds[feedName].length) {
|
|
411
|
+
updatedFeed[updatedFeed.length] = loadedFeeds[feedName][updatedFeed.length];
|
|
412
|
+
updatedFeedChanged = true;
|
|
413
|
+
}
|
|
414
|
+
// add updated post from filteredSortedFeed
|
|
415
|
+
if (!onlyHasNewPosts) {
|
|
416
|
+
const promises = [];
|
|
417
|
+
for (const post of filteredSortedFeeds[feedName]) {
|
|
418
|
+
if ((_c = updatedFeedsPosts[feedName]) === null || _c === void 0 ? void 0 : _c[post.cid]) {
|
|
419
|
+
const { index, updatedPost } = updatedFeedsPosts[feedName][post.cid];
|
|
420
|
+
// faster to validate comments async
|
|
421
|
+
promises.push((() => __awaiter(void 0, void 0, void 0, function* () {
|
|
422
|
+
if ((post.updatedAt || 0) > (updatedPost.updatedAt || 0) &&
|
|
423
|
+
(yield commentIsValid(post, { validateReplies: false }, pkc))) {
|
|
424
|
+
updatedFeed[index] = post;
|
|
425
|
+
updatedFeedChanged = true;
|
|
426
|
+
}
|
|
427
|
+
}))());
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
yield Promise.all(promises);
|
|
431
|
+
}
|
|
432
|
+
if (updatedFeedChanged) {
|
|
433
|
+
newUpdatedFeeds[feedName] = updatedFeed;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return newUpdatedFeeds;
|
|
437
|
+
});
|
|
438
|
+
// find with communities have posts newer (or ranked higher) than the loaded feeds
|
|
439
|
+
// can be used to display "new posts in x, y, z subs" alert, like on twitter
|
|
440
|
+
export const getFeedsCommunityKeysWithNewerPosts = (feedsOptions, filteredSortedFeeds, loadedFeeds, previousFeedsCommunityKeysWithNewerPosts) => {
|
|
441
|
+
const feedsCommunityKeysWithNewerPosts = {};
|
|
442
|
+
for (const feedName in loadedFeeds) {
|
|
443
|
+
const loadedFeed = loadedFeeds[feedName];
|
|
444
|
+
const cidsInLoadedFeed = new Set();
|
|
445
|
+
for (const post of loadedFeed) {
|
|
446
|
+
cidsInLoadedFeed.add(post.cid);
|
|
447
|
+
}
|
|
448
|
+
const communityKeysWithNewerPostsSet = new Set();
|
|
449
|
+
for (const [i, post] of filteredSortedFeeds[feedName].entries()) {
|
|
450
|
+
if (i >= loadedFeed.length) {
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
// if any post in filteredSortedFeeds ranks higher than the loaded feed count, it's a newer post
|
|
454
|
+
if (!cidsInLoadedFeed.has(post.cid)) {
|
|
455
|
+
const postCommunityAddress = getCommentCommunityAddress(post);
|
|
456
|
+
if (postCommunityAddress) {
|
|
457
|
+
getMatchingCommunityRefKeys(getFeedCommunityRefs(feedsOptions[feedName] || {}), postCommunityAddress).forEach((communityKey) => communityKeysWithNewerPostsSet.add(communityKey));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const communityKeysWithNewerPosts = [...communityKeysWithNewerPostsSet];
|
|
462
|
+
// don't update the array if the data is the same to avoid rerenders
|
|
463
|
+
const previousCommunityKeysWithNewerPosts = previousFeedsCommunityKeysWithNewerPosts[feedName] || [];
|
|
464
|
+
if (communityKeysWithNewerPosts.length === previousCommunityKeysWithNewerPosts.length &&
|
|
465
|
+
communityKeysWithNewerPosts.toString() === previousCommunityKeysWithNewerPosts.toString()) {
|
|
466
|
+
feedsCommunityKeysWithNewerPosts[feedName] =
|
|
467
|
+
previousFeedsCommunityKeysWithNewerPosts[feedName];
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
feedsCommunityKeysWithNewerPosts[feedName] = communityKeysWithNewerPosts;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return feedsCommunityKeysWithNewerPosts;
|
|
474
|
+
};
|
|
475
|
+
// find how many posts are left in each communities in a buffereds feeds
|
|
476
|
+
export const getFeedsCommunitiesPostCounts = (feedsOptions, feeds) => {
|
|
477
|
+
const feedsCommunitiesPostCounts = {};
|
|
478
|
+
for (const feedName in feedsOptions) {
|
|
479
|
+
const communityKeys = getFeedCommunityKeys(feedsOptions[feedName]);
|
|
480
|
+
const communityRefs = getFeedCommunityRefs(feedsOptions[feedName]);
|
|
481
|
+
feedsCommunitiesPostCounts[feedName] = {};
|
|
482
|
+
for (const communityKey of communityKeys) {
|
|
483
|
+
feedsCommunitiesPostCounts[feedName][communityKey] = 0;
|
|
484
|
+
}
|
|
485
|
+
for (const comment of feeds[feedName] || []) {
|
|
486
|
+
const commentCommunityAddress = getCommentCommunityAddress(comment);
|
|
487
|
+
if (commentCommunityAddress) {
|
|
488
|
+
getMatchingCommunityRefKeys(communityRefs, commentCommunityAddress).forEach((communityKey) => {
|
|
489
|
+
feedsCommunitiesPostCounts[feedName][communityKey] =
|
|
490
|
+
(feedsCommunitiesPostCounts[feedName][communityKey] || 0) + 1;
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return feedsCommunitiesPostCounts;
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* Get which feeds have more posts, i.e. have not reached the final page of all subs
|
|
499
|
+
*/
|
|
500
|
+
export const getFeedsHaveMore = (feedsOptions, bufferedFeeds, communities, communitiesPages, accounts) => {
|
|
501
|
+
var _a, _b;
|
|
502
|
+
const feedsHaveMore = {};
|
|
503
|
+
feedsLoop: for (const feedName in feedsOptions) {
|
|
504
|
+
// if the feed still has buffered posts, then it still has more
|
|
505
|
+
if ((_a = bufferedFeeds[feedName]) === null || _a === void 0 ? void 0 : _a.length) {
|
|
506
|
+
feedsHaveMore[feedName] = true;
|
|
507
|
+
continue feedsLoop;
|
|
508
|
+
}
|
|
509
|
+
const communityRefs = getFeedCommunityRefs(feedsOptions[feedName]);
|
|
510
|
+
const communityKeys = getFeedCommunityKeys(feedsOptions[feedName]);
|
|
511
|
+
let { sortType, accountId, modQueue } = feedsOptions[feedName];
|
|
512
|
+
let pageType = "posts";
|
|
513
|
+
if (modQueue === null || modQueue === void 0 ? void 0 : modQueue[0]) {
|
|
514
|
+
// TODO: allow multiple modQueue at once, fow now only use first in array
|
|
515
|
+
sortType = modQueue[0];
|
|
516
|
+
pageType = "modQueue";
|
|
517
|
+
}
|
|
518
|
+
communityKeysLoop: for (const [communityIndex, communityKey] of communityKeys.entries()) {
|
|
519
|
+
const community = communities[communityKey];
|
|
520
|
+
const communityRef = communityRefs[communityIndex];
|
|
521
|
+
const isBlockedCommunity = Object.keys(((_b = accounts[accountId]) === null || _b === void 0 ? void 0 : _b.blockedAddresses) || {}).some((blockedAddress) => communityRef && doesAddressMatchCommunityRef(blockedAddress, communityRef, community));
|
|
522
|
+
// don't consider the sub if the address is blocked
|
|
523
|
+
if (isBlockedCommunity) {
|
|
524
|
+
continue communityKeysLoop;
|
|
525
|
+
}
|
|
526
|
+
// if at least 1 community hasn't loaded yet, then the feed still has more
|
|
527
|
+
if (!(community === null || community === void 0 ? void 0 : community.updatedAt)) {
|
|
528
|
+
feedsHaveMore[feedName] = true;
|
|
529
|
+
continue feedsLoop;
|
|
530
|
+
}
|
|
531
|
+
// if at least 1 community has posts cache expired, then the feed still has more
|
|
532
|
+
if (communityPostsCacheExpired(community)) {
|
|
533
|
+
feedsHaveMore[feedName] = true;
|
|
534
|
+
continue feedsLoop;
|
|
535
|
+
}
|
|
536
|
+
const firstPageCid = getCommunityFirstPageCid(community, sortType, pageType);
|
|
537
|
+
// TODO: if a loaded community doesn't have a first page, it's unclear what we should do
|
|
538
|
+
// should we try to use another sort type by default, like 'hot', or should we just ignore it?
|
|
539
|
+
// 'continue' to ignore it for now
|
|
540
|
+
if (!firstPageCid) {
|
|
541
|
+
continue communityKeysLoop;
|
|
542
|
+
}
|
|
543
|
+
const pages = getCommunityPages(community, sortType, communitiesPages, pageType, accountId);
|
|
544
|
+
// if first page isn't loaded yet, then the feed still has more
|
|
545
|
+
if (!pages.length) {
|
|
546
|
+
feedsHaveMore[feedName] = true;
|
|
547
|
+
continue feedsLoop;
|
|
548
|
+
}
|
|
549
|
+
const lastPage = pages[pages.length - 1];
|
|
550
|
+
if (lastPage.nextCid) {
|
|
551
|
+
feedsHaveMore[feedName] = true;
|
|
552
|
+
continue feedsLoop;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
// if buffered feeds are empty and no last page of any community has a next page, then has more is false
|
|
556
|
+
feedsHaveMore[feedName] = false;
|
|
557
|
+
}
|
|
558
|
+
return feedsHaveMore;
|
|
559
|
+
};
|
|
560
|
+
// get all communities pages cids of all feeds, use to check if a communitiesStore change should trigger updateFeeds
|
|
561
|
+
export const getFeedsCommunities = (feedsOptions, communities) => {
|
|
562
|
+
// find all feeds communities
|
|
563
|
+
const feedsCommunityAddresses = new Set();
|
|
564
|
+
Object.keys(feedsOptions).forEach((i) => getFeedCommunityKeys(feedsOptions[i]).forEach((a) => feedsCommunityAddresses.add(a)));
|
|
565
|
+
// use map for performance increase when checking size
|
|
566
|
+
const feedsCommunities = new Map();
|
|
567
|
+
for (const communityAddress of feedsCommunityAddresses) {
|
|
568
|
+
feedsCommunities.set(communityAddress, communities[communityAddress]);
|
|
569
|
+
}
|
|
570
|
+
return feedsCommunities;
|
|
571
|
+
};
|
|
572
|
+
export const feedsCommunitiesChanged = (previousFeedsCommunities, feedsCommunities) => {
|
|
573
|
+
if (previousFeedsCommunities.size !== feedsCommunities.size) {
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
for (let communityAddress of previousFeedsCommunities.keys()) {
|
|
577
|
+
// check if the object is still the same
|
|
578
|
+
if (previousFeedsCommunities.get(communityAddress) !== feedsCommunities.get(communityAddress)) {
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return false;
|
|
583
|
+
};
|
|
584
|
+
// get all communities pages cids of all feeds, use to check if a communitiesStore change should trigger updateFeeds
|
|
585
|
+
export const getFeedsCommunitiesFirstPageCids = (feedsCommunities) => {
|
|
586
|
+
var _a, _b, _c;
|
|
587
|
+
// find all the feeds communities first page cids
|
|
588
|
+
const feedsCommunitiesFirstPageCids = new Set();
|
|
589
|
+
for (const community of feedsCommunities.values()) {
|
|
590
|
+
if (!(community === null || community === void 0 ? void 0 : community.posts) && !(community === null || community === void 0 ? void 0 : community.modQueue)) {
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
// check pages
|
|
594
|
+
if ((_a = community.posts) === null || _a === void 0 ? void 0 : _a.pages) {
|
|
595
|
+
for (const page of Object.values(community.posts.pages)) {
|
|
596
|
+
if (page === null || page === void 0 ? void 0 : page.nextCid) {
|
|
597
|
+
feedsCommunitiesFirstPageCids.add(page === null || page === void 0 ? void 0 : page.nextCid);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
// check pageCids
|
|
602
|
+
if ((_b = community.posts) === null || _b === void 0 ? void 0 : _b.pageCids) {
|
|
603
|
+
for (const pageCid of Object.values(community.posts.pageCids)) {
|
|
604
|
+
if (pageCid) {
|
|
605
|
+
feedsCommunitiesFirstPageCids.add(pageCid);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
// TODO: would be more performant to only check modQueue if there's a feedOptions with modQueue
|
|
610
|
+
if ((_c = community.modQueue) === null || _c === void 0 ? void 0 : _c.pageCids) {
|
|
611
|
+
for (const pageCid of Object.values(community.modQueue.pageCids)) {
|
|
612
|
+
if (pageCid) {
|
|
613
|
+
feedsCommunitiesFirstPageCids.add(pageCid);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return [...feedsCommunitiesFirstPageCids].sort();
|
|
619
|
+
};
|
|
620
|
+
// get all communities posts pages first post updatedAts, use to check if a communitiesStore change should trigger updateFeeds
|
|
621
|
+
export const getFeedsCommunitiesPostsPagesFirstUpdatedAts = (feedsCommunities) => {
|
|
622
|
+
var _a, _b, _c;
|
|
623
|
+
let feedsCommunitiesPostsPagesFirstUpdatedAts = "";
|
|
624
|
+
for (const community of feedsCommunities.values()) {
|
|
625
|
+
for (const page of Object.values(((_a = community === null || community === void 0 ? void 0 : community.posts) === null || _a === void 0 ? void 0 : _a.pages) || {})) {
|
|
626
|
+
if ((_c = (_b = page === null || page === void 0 ? void 0 : page.comments) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.updatedAt) {
|
|
627
|
+
feedsCommunitiesPostsPagesFirstUpdatedAts +=
|
|
628
|
+
page.comments[0].cid + page.comments[0].updatedAt;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return feedsCommunitiesPostsPagesFirstUpdatedAts;
|
|
633
|
+
};
|
|
634
|
+
// get number of feeds community that are loaded
|
|
635
|
+
export const getFeedsCommunitiesLoadedCount = (feedsCommunities) => {
|
|
636
|
+
let count = 0;
|
|
637
|
+
for (const community of feedsCommunities.values()) {
|
|
638
|
+
if (community === null || community === void 0 ? void 0 : community.updatedAt) {
|
|
639
|
+
count++;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return count;
|
|
643
|
+
};
|
|
644
|
+
export const getAccountsBlockedAddresses = (accounts) => {
|
|
645
|
+
const blockedAddressesSet = new Set();
|
|
646
|
+
for (const { blockedAddresses } of Object.values(accounts)) {
|
|
647
|
+
for (const address in blockedAddresses) {
|
|
648
|
+
if (blockedAddresses[address]) {
|
|
649
|
+
blockedAddressesSet.add(address);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return [...blockedAddressesSet].sort();
|
|
654
|
+
};
|
|
655
|
+
export const accountsBlockedAddressesChanged = (previousAccountsBlockedAddresses, accountsBlockedAddresses) => {
|
|
656
|
+
if (previousAccountsBlockedAddresses.length !== accountsBlockedAddresses.length) {
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
for (const i in previousAccountsBlockedAddresses) {
|
|
660
|
+
// check if the object is still the same
|
|
661
|
+
if (previousAccountsBlockedAddresses[i] !== accountsBlockedAddresses[i]) {
|
|
662
|
+
return true;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return false;
|
|
666
|
+
};
|
|
667
|
+
export const feedsHaveChangedBlockedAddresses = (feedsOptions, bufferedFeeds, blockedAddresses, previousBlockedAddresses) => {
|
|
668
|
+
var _a;
|
|
669
|
+
// find the difference between current and previous blocked addresses
|
|
670
|
+
const changedBlockedAddresses = blockedAddresses
|
|
671
|
+
.filter((x) => !previousBlockedAddresses.includes(x))
|
|
672
|
+
.concat(previousBlockedAddresses.filter((x) => !blockedAddresses.includes(x)));
|
|
673
|
+
// if changed blocked addresses arent used in the feeds, do nothing
|
|
674
|
+
for (const address of changedBlockedAddresses) {
|
|
675
|
+
for (const feedName in feedsOptions) {
|
|
676
|
+
const feedOptions = feedsOptions[feedName];
|
|
677
|
+
if (getMatchingCommunityRefKeys(getFeedCommunityRefs(feedOptions), address).some((communityKey) => getFeedCommunityKeys(feedOptions).includes(communityKey))) {
|
|
678
|
+
return true;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
// feeds posts author addresses have a changed blocked address
|
|
683
|
+
// NOTE: because of this, if an author address is unblocked, feeds won't update until some other event causes a feed update
|
|
684
|
+
// it seems preferable to causing unnecessary rerenders every time an unused block event occurs
|
|
685
|
+
const changedBlockedAddressesSet = new Set(changedBlockedAddresses);
|
|
686
|
+
for (const feedName in bufferedFeeds) {
|
|
687
|
+
for (const post of bufferedFeeds[feedName] || []) {
|
|
688
|
+
if (((_a = post === null || post === void 0 ? void 0 : post.author) === null || _a === void 0 ? void 0 : _a.address) && changedBlockedAddressesSet.has(post.author.address)) {
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return false;
|
|
694
|
+
};
|
|
695
|
+
export const getAccountsBlockedCids = (accounts) => {
|
|
696
|
+
const blockedCidsSet = new Set();
|
|
697
|
+
for (const { blockedCids } of Object.values(accounts)) {
|
|
698
|
+
for (const address in blockedCids) {
|
|
699
|
+
if (blockedCids[address]) {
|
|
700
|
+
blockedCidsSet.add(address);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return [...blockedCidsSet].sort();
|
|
705
|
+
};
|
|
706
|
+
export const accountsBlockedCidsChanged = (previousAccountsBlockedCids, accountsBlockedCids) => {
|
|
707
|
+
if (previousAccountsBlockedCids.length !== accountsBlockedCids.length) {
|
|
708
|
+
return true;
|
|
709
|
+
}
|
|
710
|
+
for (const i in previousAccountsBlockedCids) {
|
|
711
|
+
// check if the object is still the same
|
|
712
|
+
if (previousAccountsBlockedCids[i] !== accountsBlockedCids[i]) {
|
|
713
|
+
return true;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return false;
|
|
717
|
+
};
|
|
718
|
+
export const feedsHaveChangedBlockedCids = (feedsOptions, bufferedFeeds, blockedCids, previousBlockedCids) => {
|
|
719
|
+
// find the difference between current and previous blocked addresses
|
|
720
|
+
const changedBlockedCids = blockedCids
|
|
721
|
+
.filter((x) => !previousBlockedCids.includes(x))
|
|
722
|
+
.concat(previousBlockedCids.filter((x) => !blockedCids.includes(x)));
|
|
723
|
+
// feeds posts author addresses have a changed blocked address
|
|
724
|
+
// NOTE: because of this, if a cid is unblocked, feeds won't update until some other event causes a feed update
|
|
725
|
+
// it seems preferable to causing unnecessary rerenders every time an unused block event occurs
|
|
726
|
+
const changedBlockedCidsSet = new Set(changedBlockedCids);
|
|
727
|
+
for (const feedName in bufferedFeeds) {
|
|
728
|
+
for (const post of bufferedFeeds[feedName] || []) {
|
|
729
|
+
if ((post === null || post === void 0 ? void 0 : post.cid) && changedBlockedCidsSet.has(post === null || post === void 0 ? void 0 : post.cid)) {
|
|
730
|
+
return true;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
return false;
|
|
735
|
+
};
|
|
736
|
+
//# sourceMappingURL=utils.js.map
|