Skip to content

Commit

Permalink
Merge pull request #4404 from coralproject/fix/show-reported-state-on…
Browse files Browse the repository at this point in the history
…-archived-stories

Show reported state on archived stories
  • Loading branch information
nick-funk authored Nov 20, 2023
2 parents dc018d5 + 5646ac2 commit e1e3126
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
46 changes: 29 additions & 17 deletions server/src/core/server/graph/loaders/Comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ const mapVisibleComment = (user?: Pick<User, "role">) => {
};
};

interface ActionPresenceArgs {
commentID: string;
isArchived: boolean;
}

/**
* mapVisibleComments will map each comment an array to an array of Comment and
* null.
Expand Down Expand Up @@ -250,24 +255,31 @@ export default (ctx: GraphContext) => ({
isArchived
).then(primeCommentsFromConnection(ctx));
},
retrieveMyActionPresence: new DataLoader<string, GQLActionPresence>(
(commentIDs: string[]) => {
if (!ctx.user) {
// This should only ever be accessed when a user is logged in. It should
// be safe to get the user here, but we'll throw an error anyways just
// in case.
throw new Error("can't get action presence of an undefined user");
}

return retrieveManyUserActionPresence(
ctx.mongo,
ctx.cache.commentActions,
ctx.tenant.id,
ctx.user.id,
commentIDs
);
retrieveMyActionPresence: new DataLoader<
ActionPresenceArgs,
GQLActionPresence
>(async (args: ActionPresenceArgs[]) => {
if (!ctx.user) {
// This should only ever be accessed when a user is logged in. It should
// be safe to get the user here, but we'll throw an error anyways just
// in case.
throw new Error("can't get action presence of an undefined user");
}
),

const commentIDs = args.map((rd) => rd.commentID);
const hasArchivedData = args.some((rd) => rd.isArchived);

const result = await retrieveManyUserActionPresence(
ctx.mongo,
ctx.cache.commentActions,
ctx.tenant.id,
ctx.user.id,
commentIDs,
hasArchivedData
);

return result;
}),
forUser: (userID: string, { first, orderBy, after }: UserToCommentsArgs) =>
retrieveCommentUserConnection(ctx.mongo, ctx.tenant.id, userID, {
first: defaultTo(first, 10),
Expand Down
12 changes: 10 additions & 2 deletions server/src/core/server/graph/resolvers/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,22 @@ export const Comment: GQLCommentTypeResolver<comment.Comment> = {
commentID: id,
},
}),
viewerActionPresence: (c, input, ctx, info) => {
viewerActionPresence: async (c, input, ctx, info) => {
if (!ctx.user) {
return null;
}

setCacheHint(info, { scope: CacheScope.Private });

return ctx.loaders.Comments.retrieveMyActionPresence.load(c.id);
const story = await ctx.loaders.Stories.find.load({ id: c.storyID });
if (!story) {
throw new StoryNotFoundError(c.storyID);
}

return ctx.loaders.Comments.retrieveMyActionPresence.load({
commentID: c.id,
isArchived: !!story.isArchived,
});
},

parentCount: (c) => getDepth(c),
Expand Down
9 changes: 7 additions & 2 deletions server/src/core/server/models/action/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ export async function retrieveManyUserActionPresence(
commentActionsCache: CommentActionsCache,
tenantID: string,
userID: string | null,
commentIDs: string[]
commentIDs: string[],
isArchived = false
): Promise<GQLActionPresence[]> {
let actions: Readonly<CommentAction>[] = [];

Expand All @@ -386,7 +387,11 @@ export async function retrieveManyUserActionPresence(
}
}
} else {
const cursor = mongo.commentActions().find(
const collection =
mongo.archive && isArchived
? mongo.archivedCommentActions()
: mongo.commentActions();
const cursor = collection.find(
{
tenantID,
userID,
Expand Down

0 comments on commit e1e3126

Please sign in to comment.