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