]> Untitled Git - lemmy.git/blob - ui/src/api_tests/shared.ts
Translated using Weblate (Italian)
[lemmy.git] / ui / src / api_tests / shared.ts
1 import fetch from 'node-fetch';
2
3 import {
4   LoginForm,
5   LoginResponse,
6   Post,
7   PostForm,
8   Comment,
9   DeletePostForm,
10   RemovePostForm,
11   StickyPostForm,
12   LockPostForm,
13   PostResponse,
14   SearchResponse,
15   FollowCommunityForm,
16   CommunityResponse,
17   GetFollowedCommunitiesResponse,
18   GetPostResponse,
19   RegisterForm,
20   CommentForm,
21   DeleteCommentForm,
22   RemoveCommentForm,
23   CommentResponse,
24   CommunityForm,
25   DeleteCommunityForm,
26   RemoveCommunityForm,
27   CommentLikeForm,
28   CreatePostLikeForm,
29   PrivateMessageForm,
30   EditPrivateMessageForm,
31   DeletePrivateMessageForm,
32   PrivateMessageResponse,
33   PrivateMessagesResponse,
34   GetUserMentionsResponse,
35   UserSettingsForm,
36   SortType,
37   ListingType,
38   GetSiteResponse,
39 } from '../interfaces';
40
41 export interface API {
42   url: string;
43   auth?: string;
44 }
45
46 function apiUrl(api: API) {
47   return `${api.url}/api/v1`;
48 }
49
50 export let alpha: API = {
51   url: 'http://localhost:8540',
52 };
53
54 export let beta: API = {
55   url: 'http://localhost:8550',
56 };
57
58 export let gamma: API = {
59   url: 'http://localhost:8560',
60 };
61
62 export async function setupLogins() {
63   let form: LoginForm = {
64     username_or_email: 'lemmy_alpha',
65     password: 'lemmy',
66   };
67
68   let resA: Promise<LoginResponse> = fetch(`${apiUrl(alpha)}/user/login`, {
69     method: 'POST',
70     headers: {
71       'Content-Type': 'application/json',
72     },
73     body: wrapper(form),
74   }).then(d => d.json());
75
76   let formB = {
77     username_or_email: 'lemmy_beta',
78     password: 'lemmy',
79   };
80
81   let resB: Promise<LoginResponse> = fetch(`${apiUrl(beta)}/user/login`, {
82     method: 'POST',
83     headers: {
84       'Content-Type': 'application/json',
85     },
86     body: wrapper(formB),
87   }).then(d => d.json());
88
89   let formC = {
90     username_or_email: 'lemmy_gamma',
91     password: 'lemmy',
92   };
93
94   let resG: Promise<LoginResponse> = fetch(`${apiUrl(gamma)}/user/login`, {
95     method: 'POST',
96     headers: {
97       'Content-Type': 'application/json',
98     },
99     body: wrapper(formC),
100   }).then(d => d.json());
101
102   let res = await Promise.all([resA, resB, resG]);
103   alpha.auth = res[0].jwt;
104   beta.auth = res[1].jwt;
105   gamma.auth = res[2].jwt;
106 }
107
108 export async function createPost(
109   api: API,
110   community_id: number
111 ): Promise<PostResponse> {
112   let name = 'A jest test post';
113   let postForm: PostForm = {
114     name,
115     auth: api.auth,
116     community_id,
117     nsfw: false,
118   };
119
120   let createPostRes: PostResponse = await fetch(`${apiUrl(api)}/post`, {
121     method: 'POST',
122     headers: {
123       'Content-Type': 'application/json',
124     },
125     body: wrapper(postForm),
126   }).then(d => d.json());
127   return createPostRes;
128 }
129
130 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
131   let name = 'A jest test federated post, updated';
132   let postForm: PostForm = {
133     name,
134     edit_id: post.id,
135     auth: api.auth,
136     nsfw: false,
137   };
138
139   let updateResponse: PostResponse = await fetch(`${apiUrl(api)}/post`, {
140     method: 'PUT',
141     headers: {
142       'Content-Type': 'application/json',
143     },
144     body: wrapper(postForm),
145   }).then(d => d.json());
146   return updateResponse;
147 }
148
149 export async function deletePost(
150   api: API,
151   deleted: boolean,
152   post: Post
153 ): Promise<PostResponse> {
154   let deletePostForm: DeletePostForm = {
155     edit_id: post.id,
156     deleted: deleted,
157     auth: api.auth,
158   };
159
160   let deletePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/delete`, {
161     method: 'POST',
162     headers: {
163       'Content-Type': 'application/json',
164     },
165     body: wrapper(deletePostForm),
166   }).then(d => d.json());
167   return deletePostRes;
168 }
169
170 export async function removePost(
171   api: API,
172   removed: boolean,
173   post: Post
174 ): Promise<PostResponse> {
175   let removePostForm: RemovePostForm = {
176     edit_id: post.id,
177     removed,
178     auth: api.auth,
179   };
180
181   let removePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/remove`, {
182     method: 'POST',
183     headers: {
184       'Content-Type': 'application/json',
185     },
186     body: wrapper(removePostForm),
187   }).then(d => d.json());
188   return removePostRes;
189 }
190
191 export async function stickyPost(
192   api: API,
193   stickied: boolean,
194   post: Post
195 ): Promise<PostResponse> {
196   let stickyPostForm: StickyPostForm = {
197     edit_id: post.id,
198     stickied,
199     auth: api.auth,
200   };
201
202   let stickyRes: PostResponse = await fetch(`${apiUrl(api)}/post/sticky`, {
203     method: 'POST',
204     headers: {
205       'Content-Type': 'application/json',
206     },
207     body: wrapper(stickyPostForm),
208   }).then(d => d.json());
209
210   return stickyRes;
211 }
212
213 export async function lockPost(
214   api: API,
215   locked: boolean,
216   post: Post
217 ): Promise<PostResponse> {
218   let lockPostForm: LockPostForm = {
219     edit_id: post.id,
220     locked,
221     auth: api.auth,
222   };
223
224   let lockRes: PostResponse = await fetch(`${apiUrl(api)}/post/lock`, {
225     method: 'POST',
226     headers: {
227       'Content-Type': 'application/json',
228     },
229     body: wrapper(lockPostForm),
230   }).then(d => d.json());
231
232   return lockRes;
233 }
234
235 export async function searchPost(
236   api: API,
237   post: Post
238 ): Promise<SearchResponse> {
239   let searchUrl = `${apiUrl(api)}/search?q=${post.ap_id}&type_=All&sort=TopAll`;
240   let searchResponse: SearchResponse = await fetch(searchUrl, {
241     method: 'GET',
242   }).then(d => d.json());
243   return searchResponse;
244 }
245
246 export async function getPost(
247   api: API,
248   post_id: number
249 ): Promise<GetPostResponse> {
250   let getPostUrl = `${apiUrl(api)}/post?id=${post_id}`;
251   let getPostRes: GetPostResponse = await fetch(getPostUrl, {
252     method: 'GET',
253   }).then(d => d.json());
254
255   return getPostRes;
256 }
257
258 export async function searchComment(
259   api: API,
260   comment: Comment
261 ): Promise<SearchResponse> {
262   let searchUrl = `${apiUrl(api)}/search?q=${
263     comment.ap_id
264   }&type_=All&sort=TopAll`;
265   let searchResponse: SearchResponse = await fetch(searchUrl, {
266     method: 'GET',
267   }).then(d => d.json());
268   return searchResponse;
269 }
270
271 export async function searchForBetaCommunity(
272   api: API
273 ): Promise<SearchResponse> {
274   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
275   // Use short-hand search url
276   let searchUrl = `${apiUrl(
277     api
278   )}/search?q=!main@lemmy-beta:8550&type_=All&sort=TopAll`;
279
280   let searchResponse: SearchResponse = await fetch(searchUrl, {
281     method: 'GET',
282   }).then(d => d.json());
283   return searchResponse;
284 }
285
286 export async function searchForUser(
287   api: API,
288   apShortname: string
289 ): Promise<SearchResponse> {
290   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
291   // Use short-hand search url
292   let searchUrl = `${apiUrl(
293     api
294   )}/search?q=${apShortname}&type_=All&sort=TopAll`;
295
296   let searchResponse: SearchResponse = await fetch(searchUrl, {
297     method: 'GET',
298   }).then(d => d.json());
299   return searchResponse;
300 }
301
302 export async function followCommunity(
303   api: API,
304   follow: boolean,
305   community_id: number
306 ): Promise<CommunityResponse> {
307   let followForm: FollowCommunityForm = {
308     community_id,
309     follow,
310     auth: api.auth,
311   };
312
313   let followRes: CommunityResponse = await fetch(
314     `${apiUrl(api)}/community/follow`,
315     {
316       method: 'POST',
317       headers: {
318         'Content-Type': 'application/json',
319       },
320       body: wrapper(followForm),
321     }
322   )
323     .then(d => d.json())
324     .catch(_e => {});
325
326   return followRes;
327 }
328
329 export async function checkFollowedCommunities(
330   api: API
331 ): Promise<GetFollowedCommunitiesResponse> {
332   let followedCommunitiesUrl = `${apiUrl(
333     api
334   )}/user/followed_communities?&auth=${api.auth}`;
335   let followedCommunitiesRes: GetFollowedCommunitiesResponse = await fetch(
336     followedCommunitiesUrl,
337     {
338       method: 'GET',
339     }
340   ).then(d => d.json());
341   return followedCommunitiesRes;
342 }
343
344 export async function likePost(
345   api: API,
346   score: number,
347   post: Post
348 ): Promise<PostResponse> {
349   let likePostForm: CreatePostLikeForm = {
350     post_id: post.id,
351     score: score,
352     auth: api.auth,
353   };
354
355   let likePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/like`, {
356     method: 'POST',
357     headers: {
358       'Content-Type': 'application/json',
359     },
360     body: wrapper(likePostForm),
361   }).then(d => d.json());
362
363   return likePostRes;
364 }
365
366 export async function createComment(
367   api: API,
368   post_id: number,
369   parent_id?: number,
370   content = 'a jest test comment'
371 ): Promise<CommentResponse> {
372   let commentForm: CommentForm = {
373     content,
374     post_id,
375     parent_id,
376     auth: api.auth,
377   };
378
379   let createResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
380     method: 'POST',
381     headers: {
382       'Content-Type': 'application/json',
383     },
384     body: wrapper(commentForm),
385   }).then(d => d.json());
386   return createResponse;
387 }
388
389 export async function updateComment(
390   api: API,
391   edit_id: number,
392   content = 'A jest test federated comment update'
393 ): Promise<CommentResponse> {
394   let commentForm: CommentForm = {
395     content,
396     edit_id,
397     auth: api.auth,
398   };
399
400   let updateResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
401     method: 'PUT',
402     headers: {
403       'Content-Type': 'application/json',
404     },
405     body: wrapper(commentForm),
406   }).then(d => d.json());
407   return updateResponse;
408 }
409
410 export async function deleteComment(
411   api: API,
412   deleted: boolean,
413   edit_id: number
414 ): Promise<CommentResponse> {
415   let deleteCommentForm: DeleteCommentForm = {
416     edit_id,
417     deleted,
418     auth: api.auth,
419   };
420
421   let deleteCommentRes: CommentResponse = await fetch(
422     `${apiUrl(api)}/comment/delete`,
423     {
424       method: 'POST',
425       headers: {
426         'Content-Type': 'application/json',
427       },
428       body: wrapper(deleteCommentForm),
429     }
430   ).then(d => d.json());
431   return deleteCommentRes;
432 }
433
434 export async function removeComment(
435   api: API,
436   removed: boolean,
437   edit_id: number
438 ): Promise<CommentResponse> {
439   let removeCommentForm: RemoveCommentForm = {
440     edit_id,
441     removed,
442     auth: api.auth,
443   };
444
445   let removeCommentRes: CommentResponse = await fetch(
446     `${apiUrl(api)}/comment/remove`,
447     {
448       method: 'POST',
449       headers: {
450         'Content-Type': 'application/json',
451       },
452       body: wrapper(removeCommentForm),
453     }
454   ).then(d => d.json());
455   return removeCommentRes;
456 }
457
458 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
459   let getMentionUrl = `${apiUrl(
460     api
461   )}/user/mention?sort=New&unread_only=false&auth=${api.auth}`;
462   let getMentionsRes: GetUserMentionsResponse = await fetch(getMentionUrl, {
463     method: 'GET',
464   }).then(d => d.json());
465   return getMentionsRes;
466 }
467
468 export async function likeComment(
469   api: API,
470   score: number,
471   comment: Comment
472 ): Promise<CommentResponse> {
473   let likeCommentForm: CommentLikeForm = {
474     comment_id: comment.id,
475     score,
476     auth: api.auth,
477   };
478
479   let likeCommentRes: CommentResponse = await fetch(
480     `${apiUrl(api)}/comment/like`,
481     {
482       method: 'POST',
483       headers: {
484         'Content-Type': 'application/json',
485       },
486       body: wrapper(likeCommentForm),
487     }
488   ).then(d => d.json());
489   return likeCommentRes;
490 }
491
492 export async function createCommunity(
493   api: API,
494   name_: string = randomString(5)
495 ): Promise<CommunityResponse> {
496   let communityForm: CommunityForm = {
497     name: name_,
498     title: name_,
499     category_id: 1,
500     nsfw: false,
501     auth: api.auth,
502   };
503
504   let createCommunityRes: CommunityResponse = await fetch(
505     `${apiUrl(api)}/community`,
506     {
507       method: 'POST',
508       headers: {
509         'Content-Type': 'application/json',
510       },
511       body: wrapper(communityForm),
512     }
513   ).then(d => d.json());
514   return createCommunityRes;
515 }
516
517 export async function deleteCommunity(
518   api: API,
519   deleted: boolean,
520   edit_id: number
521 ): Promise<CommunityResponse> {
522   let deleteCommunityForm: DeleteCommunityForm = {
523     edit_id,
524     deleted,
525     auth: api.auth,
526   };
527
528   let deleteResponse: CommunityResponse = await fetch(
529     `${apiUrl(api)}/community/delete`,
530     {
531       method: 'POST',
532       headers: {
533         'Content-Type': 'application/json',
534       },
535       body: wrapper(deleteCommunityForm),
536     }
537   ).then(d => d.json());
538   return deleteResponse;
539 }
540
541 export async function removeCommunity(
542   api: API,
543   removed: boolean,
544   edit_id: number
545 ): Promise<CommunityResponse> {
546   let removeCommunityForm: RemoveCommunityForm = {
547     edit_id,
548     removed,
549     auth: api.auth,
550   };
551
552   let removeResponse: CommunityResponse = await fetch(
553     `${apiUrl(api)}/community/remove`,
554     {
555       method: 'POST',
556       headers: {
557         'Content-Type': 'application/json',
558       },
559       body: wrapper(removeCommunityForm),
560     }
561   ).then(d => d.json());
562   return removeResponse;
563 }
564
565 export async function createPrivateMessage(
566   api: API,
567   recipient_id: number
568 ): Promise<PrivateMessageResponse> {
569   let content = 'A jest test federated private message';
570   let privateMessageForm: PrivateMessageForm = {
571     content,
572     recipient_id,
573     auth: api.auth,
574   };
575
576   let createRes: PrivateMessageResponse = await fetch(
577     `${apiUrl(api)}/private_message`,
578     {
579       method: 'POST',
580       headers: {
581         'Content-Type': 'application/json',
582       },
583       body: wrapper(privateMessageForm),
584     }
585   ).then(d => d.json());
586   return createRes;
587 }
588
589 export async function updatePrivateMessage(
590   api: API,
591   edit_id: number
592 ): Promise<PrivateMessageResponse> {
593   let updatedContent = 'A jest test federated private message edited';
594   let updatePrivateMessageForm: EditPrivateMessageForm = {
595     content: updatedContent,
596     edit_id,
597     auth: api.auth,
598   };
599
600   let updateRes: PrivateMessageResponse = await fetch(
601     `${apiUrl(api)}/private_message`,
602     {
603       method: 'PUT',
604       headers: {
605         'Content-Type': 'application/json',
606       },
607       body: wrapper(updatePrivateMessageForm),
608     }
609   ).then(d => d.json());
610   return updateRes;
611 }
612
613 export async function deletePrivateMessage(
614   api: API,
615   deleted: boolean,
616   edit_id: number
617 ): Promise<PrivateMessageResponse> {
618   let deletePrivateMessageForm: DeletePrivateMessageForm = {
619     deleted,
620     edit_id,
621     auth: api.auth,
622   };
623
624   let deleteRes: PrivateMessageResponse = await fetch(
625     `${apiUrl(api)}/private_message/delete`,
626     {
627       method: 'POST',
628       headers: {
629         'Content-Type': 'application/json',
630       },
631       body: wrapper(deletePrivateMessageForm),
632     }
633   ).then(d => d.json());
634
635   return deleteRes;
636 }
637
638 export async function registerUser(
639   api: API,
640   username: string = randomString(5)
641 ): Promise<LoginResponse> {
642   let registerForm: RegisterForm = {
643     username,
644     password: 'test',
645     password_verify: 'test',
646     admin: false,
647     show_nsfw: true,
648   };
649
650   let registerRes: Promise<LoginResponse> = fetch(
651     `${apiUrl(api)}/user/register`,
652     {
653       method: 'POST',
654       headers: {
655         'Content-Type': 'application/json',
656       },
657       body: wrapper(registerForm),
658     }
659   ).then(d => d.json());
660
661   return registerRes;
662 }
663
664 export async function saveUserSettingsBio(
665   api: API,
666   auth: string
667 ): Promise<LoginResponse> {
668   let form: UserSettingsForm = {
669     show_nsfw: true,
670     theme: 'darkly',
671     default_sort_type: SortType.Hot,
672     default_listing_type: ListingType.All,
673     lang: 'en',
674     show_avatars: true,
675     send_notifications_to_email: false,
676     bio: 'a changed bio',
677     auth,
678   };
679
680   let res: Promise<LoginResponse> = fetch(
681     `${apiUrl(api)}/user/save_user_settings`,
682     {
683       method: 'PUT',
684       headers: {
685         'Content-Type': 'application/json',
686       },
687       body: wrapper(form),
688     }
689   ).then(d => d.json());
690   return res;
691 }
692
693 export async function getSite(
694   api: API,
695   auth: string
696 ): Promise<GetSiteResponse> {
697   let siteUrl = `${apiUrl(api)}/site?auth=${auth}`;
698
699   let res: GetSiteResponse = await fetch(siteUrl, {
700     method: 'GET',
701   }).then(d => d.json());
702   return res;
703 }
704
705 export async function listPrivateMessages(
706   api: API
707 ): Promise<PrivateMessagesResponse> {
708   let getPrivateMessagesUrl = `${apiUrl(api)}/private_message/list?auth=${
709     api.auth
710   }&unread_only=false&limit=999`;
711
712   let getPrivateMessagesRes: PrivateMessagesResponse = await fetch(
713     getPrivateMessagesUrl,
714     {
715       method: 'GET',
716     }
717   ).then(d => d.json());
718   return getPrivateMessagesRes;
719 }
720
721 export async function unfollowRemotes(
722   api: API
723 ): Promise<GetFollowedCommunitiesResponse> {
724   // Unfollow all remote communities
725   let followed = await checkFollowedCommunities(api);
726   let remoteFollowed = followed.communities.filter(
727     c => c.community_local == false
728   );
729   for (let cu of remoteFollowed) {
730     await followCommunity(api, false, cu.community_id);
731   }
732   let followed2 = await checkFollowedCommunities(api);
733   return followed2;
734 }
735
736 export async function followBeta(api: API): Promise<CommunityResponse> {
737   await unfollowRemotes(api);
738
739   // Cache it
740   let search = await searchForBetaCommunity(api);
741   let com = search.communities.filter(c => c.local == false);
742   if (com[0]) {
743     let follow = await followCommunity(api, true, com[0].id);
744     return follow;
745   }
746 }
747
748 export function wrapper(form: any): string {
749   return JSON.stringify(form);
750 }
751
752 function randomString(length: number): string {
753   var result = '';
754   var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
755   var charactersLength = characters.length;
756   for (var i = 0; i < length; i++) {
757     result += characters.charAt(Math.floor(Math.random() * charactersLength));
758   }
759   return result;
760 }