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