Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions src/lib/plebbit-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,45 @@ export const getPlebbitCommunityAddresses = (plebbit: any): string[] => {
return [];
};

export const withLegacySubplebbitAddress = <T extends Record<string, any>>(options: T): T => {
export const normalizePublicationOptionsForPlebbit = <T extends Record<string, any>>(
_plebbit: any,
options: T,
): T => {
const communityAddress = options.communityAddress ?? options.subplebbitAddress;
if (!communityAddress) {
return options;
}
return {
...options,
communityAddress,
subplebbitAddress: options.subplebbitAddress ?? communityAddress,
};
// The pinned plebbit-js dependency still documents and validates publication payloads with
// legacy subplebbit* field names even when some community lifecycle methods are renamed.
const normalized: Record<string, any> = { ...options, subplebbitAddress: communityAddress };
delete normalized.communityAddress;
return normalized as T;
};

export const normalizePublicationOptionsForStore = <T extends Record<string, any>>(
options: T,
): T => {
const communityAddress = options.communityAddress ?? options.subplebbitAddress;
if (!communityAddress) {
return options;
}
const normalized: Record<string, any> = { ...options, communityAddress };
delete normalized.subplebbitAddress;
return normalized as T;
};

export const normalizeCommunityEditOptionsForPlebbit = <T extends Record<string, any>>(
plebbit: any,
options: T,
): T => {
const normalized: Record<string, any> = normalizePublicationOptionsForPlebbit(plebbit, options);
const editOptions = normalized.communityEdit ?? normalized.subplebbitEdit;
if (!editOptions) {
return normalized as T;
}
normalized.subplebbitEdit = editOptions;
delete normalized.communityEdit;
return normalized as T;
};

export const getCommentCommunityAddress = (comment: any): string | undefined =>
Expand All @@ -43,6 +72,25 @@ export const normalizeCommentCommunityAddress = <T extends Record<string, any> |
return { ...comment, communityAddress: comment.subplebbitAddress } as T;
};

export const backfillPublicationCommunityAddress = <
T extends Record<string, any> | undefined,
O extends Record<string, any> | undefined,
>(
publication: T,
options: O,
): T => {
if (!publication || publication.communityAddress) {
return publication;
}
const communityAddress =
publication.subplebbitAddress ?? options?.communityAddress ?? options?.subplebbitAddress;
if (!communityAddress) {
return publication;
}
publication.communityAddress = communityAddress;
return publication;
};

export const createPlebbitCommunity = async (plebbit: any, options: any) => {
const createCommunity = getPlebbitCreateCommunity(plebbit);
assert(typeof createCommunity === "function", "plebbit createCommunity/createSubplebbit missing");
Expand Down
10 changes: 9 additions & 1 deletion src/stores/accounts/accounts-actions-internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ describe("accounts-actions-internal", () => {
0,
);

expect(account.plebbit.createComment).toHaveBeenCalledWith(plainComment);
expect(account.plebbit.createComment).toHaveBeenCalledWith(
expect.objectContaining({
cid: "plain-cid",
author: expect.objectContaining({ address: account.author.address }),
subplebbitAddress: "sub.eth",
depth: 0,
}),
);
expect(createdComment.communityAddress).toBe("sub.eth");

await act(async () => {
createdComment.emit("update", { ...plainComment, cid: "plain-cid" });
Expand Down
16 changes: 15 additions & 1 deletion src/stores/accounts/accounts-actions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import {
Community,
} from "../../types";
import utils from "../../lib/utils";
import {
backfillPublicationCommunityAddress,
normalizePublicationOptionsForStore,
normalizePublicationOptionsForPlebbit,
} from "../../lib/plebbit-compat";
import { addShortAddressesToAccountComment } from "./utils";

// TODO: we currently subscribe to updates for every single comment
// in the user's account history. This probably does not scale, we
Expand Down Expand Up @@ -50,7 +56,12 @@ export const startUpdatingAccountCommentOnCommentUpdateEvents = async (

// comment is not a `Comment` instance
if (!comment.on) {
comment = await account.plebbit.createComment(comment);
comment = backfillPublicationCommunityAddress(
await account.plebbit.createComment(
normalizePublicationOptionsForPlebbit(account.plebbit, comment),
),
comment,
);
}

comment.on("update", async (updatedComment: Comment) => {
Expand All @@ -77,6 +88,9 @@ export const startUpdatingAccountCommentOnCommentUpdateEvents = async (

// merge should not be needed if plebbit-js is implemented properly, but no harm in fixing potential errors
updatedComment = utils.merge(commentArgument, comment, updatedComment);
updatedComment = addShortAddressesToAccountComment(
normalizePublicationOptionsForStore(updatedComment) as Comment,
) as Comment;
await accountsDatabase.addAccountComment(account.id, updatedComment, currentIndex);
log("startUpdatingAccountCommentOnCommentUpdateEvents comment update", {
commentCid: comment.cid,
Expand Down
Loading
Loading