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