]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Handle displaying of deleted and removed posts/comments (fixes #2624) (#3286)
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(180000);
2
3 import { PostResponse } from "lemmy-js-client/dist/types/PostResponse";
4 import {
5   alpha,
6   beta,
7   gamma,
8   setupLogins,
9   createPost,
10   getPost,
11   resolveComment,
12   likeComment,
13   followBeta,
14   resolveBetaCommunity,
15   createComment,
16   editComment,
17   deleteComment,
18   removeComment,
19   getMentions,
20   resolvePost,
21   unfollowRemotes,
22   createCommunity,
23   registerUser,
24   reportComment,
25   listCommentReports,
26   randomString,
27   API,
28   unfollows,
29   getComments,
30   getCommentParentId,
31   resolveCommunity,
32   getPersonDetails,
33 } from "./shared";
34 import { CommentView } from "lemmy-js-client/dist/types/CommentView";
35
36 let postRes: PostResponse;
37
38 beforeAll(async () => {
39   await setupLogins();
40   await unfollows();
41   await followBeta(alpha);
42   await followBeta(gamma);
43   let betaCommunity = (await resolveBetaCommunity(alpha)).community;
44   if (betaCommunity) {
45     postRes = await createPost(alpha, betaCommunity.community.id);
46   }
47 });
48
49 afterAll(async () => {
50   await unfollows();
51 });
52
53 function assertCommentFederation(
54   commentOne?: CommentView,
55   commentTwo?: CommentView,
56 ) {
57   expect(commentOne?.comment.ap_id).toBe(commentTwo?.comment.ap_id);
58   expect(commentOne?.comment.content).toBe(commentTwo?.comment.content);
59   expect(commentOne?.creator.name).toBe(commentTwo?.creator.name);
60   expect(commentOne?.community.actor_id).toBe(commentTwo?.community.actor_id);
61   expect(commentOne?.comment.published).toBe(commentTwo?.comment.published);
62   expect(commentOne?.comment.updated).toBe(commentOne?.comment.updated);
63   expect(commentOne?.comment.deleted).toBe(commentOne?.comment.deleted);
64   expect(commentOne?.comment.removed).toBe(commentOne?.comment.removed);
65 }
66
67 test("Create a comment", async () => {
68   let commentRes = await createComment(alpha, postRes.post_view.post.id);
69   expect(commentRes.comment_view.comment.content).toBeDefined();
70   expect(commentRes.comment_view.community.local).toBe(false);
71   expect(commentRes.comment_view.creator.local).toBe(true);
72   expect(commentRes.comment_view.counts.score).toBe(1);
73
74   // Make sure that comment is liked on beta
75   let betaComment = (
76     await resolveComment(beta, commentRes.comment_view.comment)
77   ).comment;
78   expect(betaComment).toBeDefined();
79   expect(betaComment?.community.local).toBe(true);
80   expect(betaComment?.creator.local).toBe(false);
81   expect(betaComment?.counts.score).toBe(1);
82   assertCommentFederation(betaComment, commentRes.comment_view);
83 });
84
85 test("Create a comment in a non-existent post", async () => {
86   let commentRes = (await createComment(alpha, -1)) as any;
87   expect(commentRes.error).toBe("couldnt_find_post");
88 });
89
90 test("Update a comment", async () => {
91   let commentRes = await createComment(alpha, postRes.post_view.post.id);
92   // Federate the comment first
93   let betaComment = (
94     await resolveComment(beta, commentRes.comment_view.comment)
95   ).comment;
96   assertCommentFederation(betaComment, commentRes.comment_view);
97
98   let updateCommentRes = await editComment(
99     alpha,
100     commentRes.comment_view.comment.id,
101   );
102   expect(updateCommentRes.comment_view.comment.content).toBe(
103     "A jest test federated comment update",
104   );
105   expect(updateCommentRes.comment_view.community.local).toBe(false);
106   expect(updateCommentRes.comment_view.creator.local).toBe(true);
107
108   // Make sure that post is updated on beta
109   let betaCommentUpdated = (
110     await resolveComment(beta, commentRes.comment_view.comment)
111   ).comment;
112   assertCommentFederation(betaCommentUpdated, updateCommentRes.comment_view);
113 });
114
115 test("Delete a comment", async () => {
116   let commentRes = await createComment(alpha, postRes.post_view.post.id);
117
118   let deleteCommentRes = await deleteComment(
119     alpha,
120     true,
121     commentRes.comment_view.comment.id,
122   );
123   expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
124
125   // Make sure that comment is undefined on beta
126   let betaCommentRes = (await resolveComment(
127     beta,
128     commentRes.comment_view.comment,
129   )) as any;
130   expect(betaCommentRes.error).toBe("couldnt_find_object");
131
132   let undeleteCommentRes = await deleteComment(
133     alpha,
134     false,
135     commentRes.comment_view.comment.id,
136   );
137   expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
138
139   // Make sure that comment is undeleted on beta
140   let betaComment2 = (
141     await resolveComment(beta, commentRes.comment_view.comment)
142   ).comment;
143   expect(betaComment2?.comment.deleted).toBe(false);
144   assertCommentFederation(betaComment2, undeleteCommentRes.comment_view);
145 });
146
147 test("Remove a comment from admin and community on the same instance", async () => {
148   let commentRes = await createComment(alpha, postRes.post_view.post.id);
149
150   // Get the id for beta
151   let betaCommentId = (
152     await resolveComment(beta, commentRes.comment_view.comment)
153   ).comment?.comment.id;
154
155   if (!betaCommentId) {
156     throw "beta comment id is missing";
157   }
158
159   // The beta admin removes it (the community lives on beta)
160   let removeCommentRes = await removeComment(beta, true, betaCommentId);
161   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
162
163   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
164   let refetchedPostComments = await getPersonDetails(
165     alpha,
166     commentRes.comment_view.comment.creator_id,
167   );
168   console.log(refetchedPostComments.comments[0].comment);
169   expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
170
171   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
172   expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
173
174   // Make sure that comment is unremoved on beta
175   let refetchedPostComments2 = await getComments(
176     alpha,
177     postRes.post_view.post.id,
178   );
179   expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
180   assertCommentFederation(
181     refetchedPostComments2.comments[0],
182     unremoveCommentRes.comment_view,
183   );
184 });
185
186 test("Remove a comment from admin and community on different instance", async () => {
187   let alpha_user = await registerUser(alpha);
188   let newAlphaApi: API = {
189     client: alpha.client,
190     auth: alpha_user.jwt ?? "",
191   };
192
193   // New alpha user creates a community, post, and comment.
194   let newCommunity = await createCommunity(newAlphaApi);
195   let newPost = await createPost(
196     newAlphaApi,
197     newCommunity.community_view.community.id,
198   );
199   let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
200   expect(commentRes.comment_view.comment.content).toBeDefined();
201
202   // Beta searches that to cache it, then removes it
203   let betaComment = (
204     await resolveComment(beta, commentRes.comment_view.comment)
205   ).comment;
206
207   if (!betaComment) {
208     throw "beta comment missing";
209   }
210
211   let removeCommentRes = await removeComment(
212     beta,
213     true,
214     betaComment.comment.id,
215   );
216   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
217
218   // Make sure its not removed on alpha
219   let refetchedPostComments = await getComments(
220     alpha,
221     newPost.post_view.post.id,
222   );
223   expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
224   assertCommentFederation(
225     refetchedPostComments.comments[0],
226     commentRes.comment_view,
227   );
228 });
229
230 test("Unlike a comment", async () => {
231   let commentRes = await createComment(alpha, postRes.post_view.post.id);
232   let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
233   expect(unlike.comment_view.counts.score).toBe(0);
234
235   // Make sure that post is unliked on beta
236   let betaComment = (
237     await resolveComment(beta, commentRes.comment_view.comment)
238   ).comment;
239   expect(betaComment).toBeDefined();
240   expect(betaComment?.community.local).toBe(true);
241   expect(betaComment?.creator.local).toBe(false);
242   expect(betaComment?.counts.score).toBe(0);
243 });
244
245 test("Federated comment like", async () => {
246   let commentRes = await createComment(alpha, postRes.post_view.post.id);
247
248   // Find the comment on beta
249   let betaComment = (
250     await resolveComment(beta, commentRes.comment_view.comment)
251   ).comment;
252
253   if (!betaComment) {
254     throw "Missing beta comment";
255   }
256
257   let like = await likeComment(beta, 1, betaComment.comment);
258   expect(like.comment_view.counts.score).toBe(2);
259
260   // Get the post from alpha, check the likes
261   let postComments = await getComments(alpha, postRes.post_view.post.id);
262   expect(postComments.comments[0].counts.score).toBe(2);
263 });
264
265 test("Reply to a comment", async () => {
266   // Create a comment on alpha, find it on beta
267   let commentRes = await createComment(alpha, postRes.post_view.post.id);
268   let betaComment = (
269     await resolveComment(beta, commentRes.comment_view.comment)
270   ).comment;
271
272   if (!betaComment) {
273     throw "Missing beta comment";
274   }
275
276   // find that comment id on beta
277
278   // Reply from beta
279   let replyRes = await createComment(
280     beta,
281     betaComment.post.id,
282     betaComment.comment.id,
283   );
284   expect(replyRes.comment_view.comment.content).toBeDefined();
285   expect(replyRes.comment_view.community.local).toBe(true);
286   expect(replyRes.comment_view.creator.local).toBe(true);
287   expect(getCommentParentId(replyRes.comment_view.comment)).toBe(
288     betaComment.comment.id,
289   );
290   expect(replyRes.comment_view.counts.score).toBe(1);
291
292   // Make sure that comment is seen on alpha
293   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
294   // comment, isn't working.
295   // let searchAlpha = await searchComment(alpha, replyRes.comment);
296   let postComments = await getComments(alpha, postRes.post_view.post.id);
297   let alphaComment = postComments.comments[0];
298   expect(alphaComment.comment.content).toBeDefined();
299   expect(getCommentParentId(alphaComment.comment)).toBe(
300     postComments.comments[1].comment.id,
301   );
302   expect(alphaComment.community.local).toBe(false);
303   expect(alphaComment.creator.local).toBe(false);
304   expect(alphaComment.counts.score).toBe(1);
305   assertCommentFederation(alphaComment, replyRes.comment_view);
306 });
307
308 test("Mention beta", async () => {
309   // Create a mention on alpha
310   let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551";
311   let commentRes = await createComment(alpha, postRes.post_view.post.id);
312   let mentionRes = await createComment(
313     alpha,
314     postRes.post_view.post.id,
315     commentRes.comment_view.comment.id,
316     mentionContent,
317   );
318   expect(mentionRes.comment_view.comment.content).toBeDefined();
319   expect(mentionRes.comment_view.community.local).toBe(false);
320   expect(mentionRes.comment_view.creator.local).toBe(true);
321   expect(mentionRes.comment_view.counts.score).toBe(1);
322
323   let mentionsRes = await getMentions(beta);
324   expect(mentionsRes.mentions[0].comment.content).toBeDefined();
325   expect(mentionsRes.mentions[0].community.local).toBe(true);
326   expect(mentionsRes.mentions[0].creator.local).toBe(false);
327   expect(mentionsRes.mentions[0].counts.score).toBe(1);
328 });
329
330 test("Comment Search", async () => {
331   let commentRes = await createComment(alpha, postRes.post_view.post.id);
332   let betaComment = (
333     await resolveComment(beta, commentRes.comment_view.comment)
334   ).comment;
335   assertCommentFederation(betaComment, commentRes.comment_view);
336 });
337
338 test("A and G subscribe to B (center) A posts, G mentions B, it gets announced to A", async () => {
339   // Create a local post
340   let alphaCommunity = (await resolveCommunity(alpha, "!main@lemmy-alpha:8541"))
341     .community;
342
343   if (!alphaCommunity) {
344     throw "Missing alpha community";
345   }
346
347   let alphaPost = await createPost(alpha, alphaCommunity.community.id);
348   expect(alphaPost.post_view.community.local).toBe(true);
349
350   // Make sure gamma sees it
351   let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post;
352
353   if (!gammaPost) {
354     throw "Missing gamma post";
355   }
356
357   let commentContent =
358     "A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551";
359   let commentRes = await createComment(
360     gamma,
361     gammaPost.post.id,
362     undefined,
363     commentContent,
364   );
365   expect(commentRes.comment_view.comment.content).toBe(commentContent);
366   expect(commentRes.comment_view.community.local).toBe(false);
367   expect(commentRes.comment_view.creator.local).toBe(true);
368   expect(commentRes.comment_view.counts.score).toBe(1);
369
370   // Make sure alpha sees it
371   let alphaPostComments2 = await getComments(
372     alpha,
373     alphaPost.post_view.post.id,
374   );
375   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
376   expect(alphaPostComments2.comments[0].community.local).toBe(true);
377   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
378   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
379   assertCommentFederation(
380     alphaPostComments2.comments[0],
381     commentRes.comment_view,
382   );
383
384   // Make sure beta has mentions
385   let mentionsRes = await getMentions(beta);
386   expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
387   expect(mentionsRes.mentions[0].community.local).toBe(false);
388   expect(mentionsRes.mentions[0].creator.local).toBe(false);
389   // TODO this is failing because fetchInReplyTos aren't getting score
390   // expect(mentionsRes.mentions[0].score).toBe(1);
391 });
392
393 test("Check that activity from another instance is sent to third instance", async () => {
394   // Alpha and gamma users follow beta community
395   let alphaFollow = await followBeta(alpha);
396   expect(alphaFollow.community_view.community.local).toBe(false);
397   expect(alphaFollow.community_view.community.name).toBe("main");
398
399   let gammaFollow = await followBeta(gamma);
400   expect(gammaFollow.community_view.community.local).toBe(false);
401   expect(gammaFollow.community_view.community.name).toBe("main");
402
403   // Create a post on beta
404   let betaPost = await createPost(beta, 2);
405   expect(betaPost.post_view.community.local).toBe(true);
406
407   // Make sure gamma and alpha see it
408   let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post;
409   if (!gammaPost) {
410     throw "Missing gamma post";
411   }
412   expect(gammaPost.post).toBeDefined();
413
414   let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post;
415   if (!alphaPost) {
416     throw "Missing alpha post";
417   }
418   expect(alphaPost.post).toBeDefined();
419
420   // The bug: gamma comments, and alpha should see it.
421   let commentContent = "Comment from gamma";
422   let commentRes = await createComment(
423     gamma,
424     gammaPost.post.id,
425     undefined,
426     commentContent,
427   );
428   expect(commentRes.comment_view.comment.content).toBe(commentContent);
429   expect(commentRes.comment_view.community.local).toBe(false);
430   expect(commentRes.comment_view.creator.local).toBe(true);
431   expect(commentRes.comment_view.counts.score).toBe(1);
432
433   // Make sure alpha sees it
434   let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
435   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
436   expect(alphaPostComments2.comments[0].community.local).toBe(false);
437   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
438   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
439   assertCommentFederation(
440     alphaPostComments2.comments[0],
441     commentRes.comment_view,
442   );
443
444   await unfollowRemotes(alpha);
445   await unfollowRemotes(gamma);
446 });
447
448 test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => {
449   // Unfollow all remote communities
450   let site = await unfollowRemotes(alpha);
451   expect(
452     site.my_user?.follows.filter(c => c.community.local == false).length,
453   ).toBe(0);
454
455   // B creates a post, and two comments, should be invisible to A
456   let postRes = await createPost(beta, 2);
457   expect(postRes.post_view.post.name).toBeDefined();
458
459   let parentCommentContent = "An invisible top level comment from beta";
460   let parentCommentRes = await createComment(
461     beta,
462     postRes.post_view.post.id,
463     undefined,
464     parentCommentContent,
465   );
466   expect(parentCommentRes.comment_view.comment.content).toBe(
467     parentCommentContent,
468   );
469
470   // B creates a comment, then a child one of that.
471   let childCommentContent = "An invisible child comment from beta";
472   let childCommentRes = await createComment(
473     beta,
474     postRes.post_view.post.id,
475     parentCommentRes.comment_view.comment.id,
476     childCommentContent,
477   );
478   expect(childCommentRes.comment_view.comment.content).toBe(
479     childCommentContent,
480   );
481
482   // Follow beta again
483   let follow = await followBeta(alpha);
484   expect(follow.community_view.community.local).toBe(false);
485   expect(follow.community_view.community.name).toBe("main");
486
487   // An update to the child comment on beta, should push the post, parent, and child to alpha now
488   let updatedCommentContent = "An update child comment from beta";
489   let updateRes = await editComment(
490     beta,
491     childCommentRes.comment_view.comment.id,
492     updatedCommentContent,
493   );
494   expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
495
496   // Get the post from alpha
497   let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post;
498   if (!alphaPostB) {
499     throw "Missing alpha post B";
500   }
501
502   let alphaPost = await getPost(alpha, alphaPostB.post.id);
503   let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
504   expect(alphaPost.post_view.post.name).toBeDefined();
505   assertCommentFederation(
506     alphaPostComments.comments[1],
507     parentCommentRes.comment_view,
508   );
509   assertCommentFederation(
510     alphaPostComments.comments[0],
511     updateRes.comment_view,
512   );
513   expect(alphaPost.post_view.community.local).toBe(false);
514   expect(alphaPost.post_view.creator.local).toBe(false);
515
516   await unfollowRemotes(alpha);
517 });
518
519 test("Report a comment", async () => {
520   let betaCommunity = (await resolveBetaCommunity(beta)).community;
521   if (!betaCommunity) {
522     throw "Missing beta community";
523   }
524   let postRes = (await createPost(beta, betaCommunity.community.id)).post_view
525     .post;
526   expect(postRes).toBeDefined();
527   let commentRes = (await createComment(beta, postRes.id)).comment_view.comment;
528   expect(commentRes).toBeDefined();
529
530   let alphaComment = (await resolveComment(alpha, commentRes)).comment?.comment;
531   if (!alphaComment) {
532     throw "Missing alpha comment";
533   }
534
535   let alphaReport = (
536     await reportComment(alpha, alphaComment.id, randomString(10))
537   ).comment_report_view.comment_report;
538
539   let betaReport = (await listCommentReports(beta)).comment_reports[0]
540     .comment_report;
541   expect(betaReport).toBeDefined();
542   expect(betaReport.resolved).toBe(false);
543   expect(betaReport.original_comment_text).toBe(
544     alphaReport.original_comment_text,
545   );
546   expect(betaReport.reason).toBe(alphaReport.reason);
547 });