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