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