]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
e4306c94c0de6d94b22b83987aa0ffea7e1d64a8
[lemmy.git] / api_tests / src / shared.ts
1 import { LemmyHttp } from "lemmy-js-client";
2 import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
3 import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
4 import { EditPost } from "lemmy-js-client/dist/types/EditPost";
5 import { EditSite } from "lemmy-js-client/dist/types/EditSite";
6 import { FeaturePost } from "lemmy-js-client/dist/types/FeaturePost";
7 import { GetComments } from "lemmy-js-client/dist/types/GetComments";
8 import { GetCommentsResponse } from "lemmy-js-client/dist/types/GetCommentsResponse";
9 import { GetPost } from "lemmy-js-client/dist/types/GetPost";
10 import { GetPostResponse } from "lemmy-js-client/dist/types/GetPostResponse";
11 import { LockPost } from "lemmy-js-client/dist/types/LockPost";
12 import { Login } from "lemmy-js-client/dist/types/Login";
13 import { Post } from "lemmy-js-client/dist/types/Post";
14 import { PostResponse } from "lemmy-js-client/dist/types/PostResponse";
15 import { RemovePost } from "lemmy-js-client/dist/types/RemovePost";
16 import { ResolveObject } from "lemmy-js-client/dist/types/ResolveObject";
17 import { ResolveObjectResponse } from "lemmy-js-client/dist/types/ResolveObjectResponse";
18 import { Search } from "lemmy-js-client/dist/types/Search";
19 import { SearchResponse } from "lemmy-js-client/dist/types/SearchResponse";
20 import { Comment } from "lemmy-js-client/dist/types/Comment";
21 import { BanPersonResponse } from "lemmy-js-client/dist/types/BanPersonResponse";
22 import { BanPerson } from "lemmy-js-client/dist/types/BanPerson";
23 import { BanFromCommunityResponse } from "lemmy-js-client/dist/types/BanFromCommunityResponse";
24 import { BanFromCommunity } from "lemmy-js-client/dist/types/BanFromCommunity";
25 import { CommunityResponse } from "lemmy-js-client/dist/types/CommunityResponse";
26 import { FollowCommunity } from "lemmy-js-client/dist/types/FollowCommunity";
27 import { CreatePostLike } from "lemmy-js-client/dist/types/CreatePostLike";
28 import { CommentResponse } from "lemmy-js-client/dist/types/CommentResponse";
29 import { CreateComment } from "lemmy-js-client/dist/types/CreateComment";
30 import { EditComment } from "lemmy-js-client/dist/types/EditComment";
31 import { DeleteComment } from "lemmy-js-client/dist/types/DeleteComment";
32 import { RemoveComment } from "lemmy-js-client/dist/types/RemoveComment";
33 import { GetPersonMentionsResponse } from "lemmy-js-client/dist/types/GetPersonMentionsResponse";
34 import { GetPersonMentions } from "lemmy-js-client/dist/types/GetPersonMentions";
35 import { CreateCommentLike } from "lemmy-js-client/dist/types/CreateCommentLike";
36 import { CreateCommunity } from "lemmy-js-client/dist/types/CreateCommunity";
37 import { GetCommunity } from "lemmy-js-client/dist/types/GetCommunity";
38 import { DeleteCommunity } from "lemmy-js-client/dist/types/DeleteCommunity";
39 import { RemoveCommunity } from "lemmy-js-client/dist/types/RemoveCommunity";
40 import { PrivateMessageResponse } from "lemmy-js-client/dist/types/PrivateMessageResponse";
41 import { CreatePrivateMessage } from "lemmy-js-client/dist/types/CreatePrivateMessage";
42 import { EditPrivateMessage } from "lemmy-js-client/dist/types/EditPrivateMessage";
43 import { DeletePrivateMessage } from "lemmy-js-client/dist/types/DeletePrivateMessage";
44 import { LoginResponse } from "lemmy-js-client/dist/types/LoginResponse";
45 import { Register } from "lemmy-js-client/dist/types/Register";
46 import { SaveUserSettings } from "lemmy-js-client/dist/types/SaveUserSettings";
47 import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount";
48 import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse";
49 import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse";
50 import { GetSite } from "lemmy-js-client/dist/types/GetSite";
51 import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse";
52 import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages";
53 import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse";
54 import { CreatePostReport } from "lemmy-js-client/dist/types/CreatePostReport";
55 import { ListPostReportsResponse } from "lemmy-js-client/dist/types/ListPostReportsResponse";
56 import { ListPostReports } from "lemmy-js-client/dist/types/ListPostReports";
57 import { CommentReportResponse } from "lemmy-js-client/dist/types/CommentReportResponse";
58 import { CreateCommentReport } from "lemmy-js-client/dist/types/CreateCommentReport";
59 import { ListCommentReportsResponse } from "lemmy-js-client/dist/types/ListCommentReportsResponse";
60 import { ListCommentReports } from "lemmy-js-client/dist/types/ListCommentReports";
61 import { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDetailsResponse";
62 import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails";
63
64 export interface API {
65   client: LemmyHttp;
66   auth: string;
67 }
68
69 export let alpha: API = {
70   client: new LemmyHttp("http://127.0.0.1:8541"),
71   auth: "",
72 };
73
74 export let beta: API = {
75   client: new LemmyHttp("http://127.0.0.1:8551"),
76   auth: "",
77 };
78
79 export let gamma: API = {
80   client: new LemmyHttp("http://127.0.0.1:8561"),
81   auth: "",
82 };
83
84 export let delta: API = {
85   client: new LemmyHttp("http://127.0.0.1:8571"),
86   auth: "",
87 };
88
89 export let epsilon: API = {
90   client: new LemmyHttp("http://127.0.0.1:8581"),
91   auth: "",
92 };
93
94 const password = "lemmylemmy";
95
96 export async function setupLogins() {
97   let formAlpha: Login = {
98     username_or_email: "lemmy_alpha",
99     password,
100   };
101   let resAlpha = alpha.client.login(formAlpha);
102
103   let formBeta: Login = {
104     username_or_email: "lemmy_beta",
105     password,
106   };
107   let resBeta = beta.client.login(formBeta);
108
109   let formGamma: Login = {
110     username_or_email: "lemmy_gamma",
111     password,
112   };
113   let resGamma = gamma.client.login(formGamma);
114
115   let formDelta: Login = {
116     username_or_email: "lemmy_delta",
117     password,
118   };
119   let resDelta = delta.client.login(formDelta);
120
121   let formEpsilon: Login = {
122     username_or_email: "lemmy_epsilon",
123     password,
124   };
125   let resEpsilon = epsilon.client.login(formEpsilon);
126
127   let res = await Promise.all([
128     resAlpha,
129     resBeta,
130     resGamma,
131     resDelta,
132     resEpsilon,
133   ]);
134
135   alpha.auth = res[0].jwt ?? "";
136   beta.auth = res[1].jwt ?? "";
137   gamma.auth = res[2].jwt ?? "";
138   delta.auth = res[3].jwt ?? "";
139   epsilon.auth = res[4].jwt ?? "";
140
141   // Registration applications are now enabled by default, need to disable them
142   let editSiteForm: EditSite = {
143     registration_mode: "Open",
144     rate_limit_message: 999,
145     rate_limit_post: 999,
146     rate_limit_register: 999,
147     rate_limit_image: 999,
148     rate_limit_comment: 999,
149     rate_limit_search: 999,
150     auth: "",
151   };
152
153   // Set the blocks and auths for each
154   editSiteForm.auth = alpha.auth;
155   editSiteForm.allowed_instances = [
156     "lemmy-beta",
157     "lemmy-gamma",
158     "lemmy-delta",
159     "lemmy-epsilon",
160   ];
161   await alpha.client.editSite(editSiteForm);
162
163   editSiteForm.auth = beta.auth;
164   editSiteForm.allowed_instances = [
165     "lemmy-alpha",
166     "lemmy-gamma",
167     "lemmy-delta",
168     "lemmy-epsilon",
169   ];
170   await beta.client.editSite(editSiteForm);
171
172   editSiteForm.auth = gamma.auth;
173   editSiteForm.allowed_instances = [
174     "lemmy-alpha",
175     "lemmy-beta",
176     "lemmy-delta",
177     "lemmy-epsilon",
178   ];
179   await gamma.client.editSite(editSiteForm);
180
181   editSiteForm.allowed_instances = ["lemmy-beta"];
182   editSiteForm.auth = delta.auth;
183   await delta.client.editSite(editSiteForm);
184
185   editSiteForm.auth = epsilon.auth;
186   editSiteForm.allowed_instances = [];
187   editSiteForm.blocked_instances = ["lemmy-alpha"];
188   await epsilon.client.editSite(editSiteForm);
189
190   // Create the main alpha/beta communities
191   // Ignore thrown errors of duplicates
192   try {
193     await createCommunity(alpha, "main");
194     await createCommunity(beta, "main");
195   } catch (_) {}
196 }
197
198 export async function createPost(
199   api: API,
200   community_id: number,
201 ): Promise<PostResponse> {
202   let name = randomString(5);
203   let body = randomString(10);
204   let url = "https://google.com/";
205   let form: CreatePost = {
206     name,
207     url,
208     body,
209     auth: api.auth,
210     community_id,
211   };
212   return api.client.createPost(form);
213 }
214
215 export async function editPost(api: API, post: Post): Promise<PostResponse> {
216   let name = "A jest test federated post, updated";
217   let form: EditPost = {
218     name,
219     post_id: post.id,
220     auth: api.auth,
221   };
222   return api.client.editPost(form);
223 }
224
225 export async function deletePost(
226   api: API,
227   deleted: boolean,
228   post: Post,
229 ): Promise<PostResponse> {
230   let form: DeletePost = {
231     post_id: post.id,
232     deleted: deleted,
233     auth: api.auth,
234   };
235   return api.client.deletePost(form);
236 }
237
238 export async function removePost(
239   api: API,
240   removed: boolean,
241   post: Post,
242 ): Promise<PostResponse> {
243   let form: RemovePost = {
244     post_id: post.id,
245     removed,
246     auth: api.auth,
247   };
248   return api.client.removePost(form);
249 }
250
251 export async function featurePost(
252   api: API,
253   featured: boolean,
254   post: Post,
255 ): Promise<PostResponse> {
256   let form: FeaturePost = {
257     post_id: post.id,
258     featured,
259     feature_type: "Community",
260     auth: api.auth,
261   };
262   return api.client.featurePost(form);
263 }
264
265 export async function lockPost(
266   api: API,
267   locked: boolean,
268   post: Post,
269 ): Promise<PostResponse> {
270   let form: LockPost = {
271     post_id: post.id,
272     locked,
273     auth: api.auth,
274   };
275   return api.client.lockPost(form);
276 }
277
278 export async function resolvePost(
279   api: API,
280   post: Post,
281 ): Promise<ResolveObjectResponse> {
282   let form: ResolveObject = {
283     q: post.ap_id,
284     auth: api.auth,
285   };
286   return api.client.resolveObject(form);
287 }
288
289 export async function searchPostLocal(
290   api: API,
291   post: Post,
292 ): Promise<SearchResponse> {
293   let form: Search = {
294     q: post.name,
295     type_: "Posts",
296     sort: "TopAll",
297     auth: api.auth,
298   };
299   return api.client.search(form);
300 }
301
302 export async function getPost(
303   api: API,
304   post_id: number,
305 ): Promise<GetPostResponse> {
306   let form: GetPost = {
307     id: post_id,
308     auth: api.auth,
309   };
310   return api.client.getPost(form);
311 }
312
313 export async function getComments(
314   api: API,
315   post_id: number,
316 ): Promise<GetCommentsResponse> {
317   let form: GetComments = {
318     post_id: post_id,
319     type_: "All",
320     sort: "New",
321     auth: api.auth,
322   };
323   return api.client.getComments(form);
324 }
325
326 export async function resolveComment(
327   api: API,
328   comment: Comment,
329 ): Promise<ResolveObjectResponse> {
330   let form: ResolveObject = {
331     q: comment.ap_id,
332     auth: api.auth,
333   };
334   return api.client.resolveObject(form);
335 }
336
337 export async function resolveBetaCommunity(
338   api: API,
339 ): Promise<ResolveObjectResponse> {
340   // Use short-hand search url
341   let form: ResolveObject = {
342     q: "!main@lemmy-beta:8551",
343     auth: api.auth,
344   };
345   return api.client.resolveObject(form);
346 }
347
348 export async function resolveCommunity(
349   api: API,
350   q: string,
351 ): Promise<ResolveObjectResponse> {
352   let form: ResolveObject = {
353     q,
354     auth: api.auth,
355   };
356   return api.client.resolveObject(form);
357 }
358
359 export async function resolvePerson(
360   api: API,
361   apShortname: string,
362 ): Promise<ResolveObjectResponse> {
363   let form: ResolveObject = {
364     q: apShortname,
365     auth: api.auth,
366   };
367   return api.client.resolveObject(form);
368 }
369
370 export async function banPersonFromSite(
371   api: API,
372   person_id: number,
373   ban: boolean,
374   remove_data: boolean,
375 ): Promise<BanPersonResponse> {
376   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
377   let form: BanPerson = {
378     person_id,
379     ban,
380     remove_data: remove_data,
381     auth: api.auth,
382   };
383   return api.client.banPerson(form);
384 }
385
386 export async function banPersonFromCommunity(
387   api: API,
388   person_id: number,
389   community_id: number,
390   remove_data: boolean,
391   ban: boolean,
392 ): Promise<BanFromCommunityResponse> {
393   let form: BanFromCommunity = {
394     person_id,
395     community_id,
396     remove_data: remove_data,
397     ban,
398     auth: api.auth,
399   };
400   return api.client.banFromCommunity(form);
401 }
402
403 export async function followCommunity(
404   api: API,
405   follow: boolean,
406   community_id: number,
407 ): Promise<CommunityResponse> {
408   let form: FollowCommunity = {
409     community_id,
410     follow,
411     auth: api.auth,
412   };
413   return api.client.followCommunity(form);
414 }
415
416 export async function likePost(
417   api: API,
418   score: number,
419   post: Post,
420 ): Promise<PostResponse> {
421   let form: CreatePostLike = {
422     post_id: post.id,
423     score: score,
424     auth: api.auth,
425   };
426
427   return api.client.likePost(form);
428 }
429
430 export async function createComment(
431   api: API,
432   post_id: number,
433   parent_id?: number,
434   content = "a jest test comment",
435 ): Promise<CommentResponse> {
436   let form: CreateComment = {
437     content,
438     post_id,
439     parent_id,
440     auth: api.auth,
441   };
442   return api.client.createComment(form);
443 }
444
445 export async function editComment(
446   api: API,
447   comment_id: number,
448   content = "A jest test federated comment update",
449 ): Promise<CommentResponse> {
450   let form: EditComment = {
451     content,
452     comment_id,
453     auth: api.auth,
454   };
455   return api.client.editComment(form);
456 }
457
458 export async function deleteComment(
459   api: API,
460   deleted: boolean,
461   comment_id: number,
462 ): Promise<CommentResponse> {
463   let form: DeleteComment = {
464     comment_id,
465     deleted,
466     auth: api.auth,
467   };
468   return api.client.deleteComment(form);
469 }
470
471 export async function removeComment(
472   api: API,
473   removed: boolean,
474   comment_id: number,
475 ): Promise<CommentResponse> {
476   let form: RemoveComment = {
477     comment_id,
478     removed,
479     auth: api.auth,
480   };
481   return api.client.removeComment(form);
482 }
483
484 export async function getMentions(
485   api: API,
486 ): Promise<GetPersonMentionsResponse> {
487   let form: GetPersonMentions = {
488     sort: "New",
489     unread_only: false,
490     auth: api.auth,
491   };
492   return api.client.getPersonMentions(form);
493 }
494
495 export async function likeComment(
496   api: API,
497   score: number,
498   comment: Comment,
499 ): Promise<CommentResponse> {
500   let form: CreateCommentLike = {
501     comment_id: comment.id,
502     score,
503     auth: api.auth,
504   };
505   return api.client.likeComment(form);
506 }
507
508 export async function createCommunity(
509   api: API,
510   name_: string = randomString(5),
511 ): Promise<CommunityResponse> {
512   let description = "a sample description";
513   let form: CreateCommunity = {
514     name: name_,
515     title: name_,
516     description,
517     auth: api.auth,
518   };
519   return api.client.createCommunity(form);
520 }
521
522 export async function getCommunity(
523   api: API,
524   id: number,
525 ): Promise<CommunityResponse> {
526   let form: GetCommunity = {
527     id,
528     auth: api.auth,
529   };
530   return api.client.getCommunity(form);
531 }
532
533 export async function deleteCommunity(
534   api: API,
535   deleted: boolean,
536   community_id: number,
537 ): Promise<CommunityResponse> {
538   let form: DeleteCommunity = {
539     community_id,
540     deleted,
541     auth: api.auth,
542   };
543   return api.client.deleteCommunity(form);
544 }
545
546 export async function removeCommunity(
547   api: API,
548   removed: boolean,
549   community_id: number,
550 ): Promise<CommunityResponse> {
551   let form: RemoveCommunity = {
552     community_id,
553     removed,
554     auth: api.auth,
555   };
556   return api.client.removeCommunity(form);
557 }
558
559 export async function createPrivateMessage(
560   api: API,
561   recipient_id: number,
562 ): Promise<PrivateMessageResponse> {
563   let content = "A jest test federated private message";
564   let form: CreatePrivateMessage = {
565     content,
566     recipient_id,
567     auth: api.auth,
568   };
569   return api.client.createPrivateMessage(form);
570 }
571
572 export async function editPrivateMessage(
573   api: API,
574   private_message_id: number,
575 ): Promise<PrivateMessageResponse> {
576   let updatedContent = "A jest test federated private message edited";
577   let form: EditPrivateMessage = {
578     content: updatedContent,
579     private_message_id,
580     auth: api.auth,
581   };
582   return api.client.editPrivateMessage(form);
583 }
584
585 export async function deletePrivateMessage(
586   api: API,
587   deleted: boolean,
588   private_message_id: number,
589 ): Promise<PrivateMessageResponse> {
590   let form: DeletePrivateMessage = {
591     deleted,
592     private_message_id,
593     auth: api.auth,
594   };
595   return api.client.deletePrivateMessage(form);
596 }
597
598 export async function registerUser(
599   api: API,
600   username: string = randomString(5),
601 ): Promise<LoginResponse> {
602   let form: Register = {
603     username,
604     password,
605     password_verify: password,
606     show_nsfw: true,
607   };
608   return api.client.register(form);
609 }
610
611 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
612   let form: SaveUserSettings = {
613     show_nsfw: true,
614     blur_nsfw: false,
615     auto_expand: true,
616     theme: "darkly",
617     default_sort_type: "Active",
618     default_listing_type: "All",
619     interface_language: "en",
620     show_avatars: true,
621     send_notifications_to_email: false,
622     bio: "a changed bio",
623     auth: api.auth,
624   };
625   return saveUserSettings(api, form);
626 }
627
628 export async function saveUserSettingsFederated(
629   api: API,
630 ): Promise<LoginResponse> {
631   let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
632   let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
633   let bio = "a changed bio";
634   let form: SaveUserSettings = {
635     show_nsfw: false,
636     blur_nsfw: true,
637     auto_expand: false,
638     default_sort_type: "Hot",
639     default_listing_type: "All",
640     interface_language: "",
641     avatar,
642     banner,
643     display_name: "user321",
644     show_avatars: false,
645     send_notifications_to_email: false,
646     bio,
647     auth: api.auth,
648   };
649   return await saveUserSettings(alpha, form);
650 }
651
652 export async function saveUserSettings(
653   api: API,
654   form: SaveUserSettings,
655 ): Promise<LoginResponse> {
656   return api.client.saveUserSettings(form);
657 }
658 export async function getPersonDetails(
659   api: API,
660   person_id: number,
661 ): Promise<GetPersonDetailsResponse> {
662   let form: GetPersonDetails = {
663     auth: api.auth,
664     person_id: person_id,
665   };
666   return api.client.getPersonDetails(form);
667 }
668
669 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
670   let form: DeleteAccount = {
671     auth: api.auth,
672     password,
673   };
674   return api.client.deleteAccount(form);
675 }
676
677 export async function getSite(api: API): Promise<GetSiteResponse> {
678   let form: GetSite = {
679     auth: api.auth,
680   };
681   return api.client.getSite(form);
682 }
683
684 export async function listPrivateMessages(
685   api: API,
686 ): Promise<PrivateMessagesResponse> {
687   let form: GetPrivateMessages = {
688     auth: api.auth,
689     unread_only: false,
690   };
691   return api.client.getPrivateMessages(form);
692 }
693
694 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
695   // Unfollow all remote communities
696   let site = await getSite(api);
697   let remoteFollowed =
698     site.my_user?.follows.filter(c => c.community.local == false) ?? [];
699   for (let cu of remoteFollowed) {
700     await followCommunity(api, false, cu.community.id);
701   }
702   let siteRes = await getSite(api);
703   return siteRes;
704 }
705
706 export async function followBeta(api: API): Promise<CommunityResponse> {
707   let betaCommunity = (await resolveBetaCommunity(api)).community;
708   if (betaCommunity) {
709     let follow = await followCommunity(api, true, betaCommunity.community.id);
710     return follow;
711   } else {
712     return Promise.reject("no community worked");
713   }
714 }
715
716 export async function reportPost(
717   api: API,
718   post_id: number,
719   reason: string,
720 ): Promise<PostReportResponse> {
721   let form: CreatePostReport = {
722     post_id,
723     reason,
724     auth: api.auth,
725   };
726   return api.client.createPostReport(form);
727 }
728
729 export async function listPostReports(
730   api: API,
731 ): Promise<ListPostReportsResponse> {
732   let form: ListPostReports = {
733     auth: api.auth,
734   };
735   return api.client.listPostReports(form);
736 }
737
738 export async function reportComment(
739   api: API,
740   comment_id: number,
741   reason: string,
742 ): Promise<CommentReportResponse> {
743   let form: CreateCommentReport = {
744     comment_id,
745     reason,
746     auth: api.auth,
747   };
748   return api.client.createCommentReport(form);
749 }
750
751 export async function listCommentReports(
752   api: API,
753 ): Promise<ListCommentReportsResponse> {
754   let form: ListCommentReports = {
755     auth: api.auth,
756   };
757   return api.client.listCommentReports(form);
758 }
759
760 export function delay(millis = 500) {
761   return new Promise(resolve => setTimeout(resolve, millis));
762 }
763
764 export function longDelay() {
765   return delay(10000);
766 }
767
768 export function wrapper(form: any): string {
769   return JSON.stringify(form);
770 }
771
772 export function randomString(length: number): string {
773   var result = "";
774   var characters =
775     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
776   var charactersLength = characters.length;
777   for (var i = 0; i < length; i++) {
778     result += characters.charAt(Math.floor(Math.random() * charactersLength));
779   }
780   return result;
781 }
782
783 export async function unfollows() {
784   await unfollowRemotes(alpha);
785   await unfollowRemotes(gamma);
786   await unfollowRemotes(delta);
787   await unfollowRemotes(epsilon);
788 }
789
790 export function getCommentParentId(comment: Comment): number | undefined {
791   let split = comment.path.split(".");
792   // remove the 0
793   split.shift();
794
795   if (split.length > 1) {
796     return Number(split[split.length - 2]);
797   } else {
798     return undefined;
799   }
800 }