]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Fix federation of community removal/deletion, added docs (#125)
[lemmy.git] / api_tests / src / shared.ts
1 import {
2   LoginForm,
3   LoginResponse,
4   Post,
5   PostForm,
6   Comment,
7   DeletePostForm,
8   RemovePostForm,
9   StickyPostForm,
10   LockPostForm,
11   PostResponse,
12   SearchResponse,
13   FollowCommunityForm,
14   CommunityResponse,
15   GetFollowedCommunitiesResponse,
16   GetPostResponse,
17   RegisterForm,
18   CommentForm,
19   DeleteCommentForm,
20   RemoveCommentForm,
21   SearchForm,
22   CommentResponse,
23   GetCommunityForm,
24   CommunityForm,
25   DeleteCommunityForm,
26   RemoveCommunityForm,
27   GetUserMentionsForm,
28   CommentLikeForm,
29   CreatePostLikeForm,
30   PrivateMessageForm,
31   EditPrivateMessageForm,
32   DeletePrivateMessageForm,
33   GetFollowedCommunitiesForm,
34   GetPrivateMessagesForm,
35   GetSiteForm,
36   GetPostForm,
37   PrivateMessageResponse,
38   PrivateMessagesResponse,
39   GetUserMentionsResponse,
40   UserSettingsForm,
41   SortType,
42   ListingType,
43   GetSiteResponse,
44   SearchType,
45   LemmyHttp,
46 } from 'lemmy-js-client';
47
48 export interface API {
49   client: LemmyHttp;
50   auth?: string;
51 }
52
53 export let alpha: API = {
54   client: new LemmyHttp('http://localhost:8541/api/v1'),
55 };
56
57 export let beta: API = {
58   client: new LemmyHttp('http://localhost:8551/api/v1'),
59 };
60
61 export let gamma: API = {
62   client: new LemmyHttp('http://localhost:8561/api/v1'),
63 };
64
65 export let delta: API = {
66   client: new LemmyHttp('http://localhost:8571/api/v1'),
67 };
68
69 export let epsilon: API = {
70   client: new LemmyHttp('http://localhost:8581/api/v1'),
71 };
72
73 export async function setupLogins() {
74   let formAlpha: LoginForm = {
75     username_or_email: 'lemmy_alpha',
76     password: 'lemmy',
77   };
78   let resAlpha = alpha.client.login(formAlpha);
79
80   let formBeta = {
81     username_or_email: 'lemmy_beta',
82     password: 'lemmy',
83   };
84   let resBeta = beta.client.login(formBeta);
85
86   let formGamma = {
87     username_or_email: 'lemmy_gamma',
88     password: 'lemmy',
89   };
90   let resGamma = gamma.client.login(formGamma);
91
92   let formDelta = {
93     username_or_email: 'lemmy_delta',
94     password: 'lemmy',
95   };
96   let resDelta = delta.client.login(formDelta);
97
98   let formEpsilon = {
99     username_or_email: 'lemmy_epsilon',
100     password: 'lemmy',
101   };
102   let resEpsilon = epsilon.client.login(formEpsilon);
103
104   let res = await Promise.all([
105     resAlpha,
106     resBeta,
107     resGamma,
108     resDelta,
109     resEpsilon,
110   ]);
111
112   alpha.auth = res[0].jwt;
113   beta.auth = res[1].jwt;
114   gamma.auth = res[2].jwt;
115   delta.auth = res[3].jwt;
116   epsilon.auth = res[4].jwt;
117 }
118
119 export async function createPost(
120   api: API,
121   community_id: number
122 ): Promise<PostResponse> {
123   let name = 'A jest test post';
124   let body = 'Some body';
125   let url = 'https://google.com/';
126   let form: PostForm = {
127     name,
128     url,
129     body,
130     auth: api.auth,
131     community_id,
132     nsfw: false,
133   };
134   return api.client.createPost(form);
135 }
136
137 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
138   let name = 'A jest test federated post, updated';
139   let form: PostForm = {
140     name,
141     edit_id: post.id,
142     auth: api.auth,
143     nsfw: false,
144   };
145   return api.client.editPost(form);
146 }
147
148 export async function deletePost(
149   api: API,
150   deleted: boolean,
151   post: Post
152 ): Promise<PostResponse> {
153   let form: DeletePostForm = {
154     edit_id: post.id,
155     deleted: deleted,
156     auth: api.auth,
157   };
158   return api.client.deletePost(form);
159 }
160
161 export async function removePost(
162   api: API,
163   removed: boolean,
164   post: Post
165 ): Promise<PostResponse> {
166   let form: RemovePostForm = {
167     edit_id: post.id,
168     removed,
169     auth: api.auth,
170   };
171   return api.client.removePost(form);
172 }
173
174 export async function stickyPost(
175   api: API,
176   stickied: boolean,
177   post: Post
178 ): Promise<PostResponse> {
179   let form: StickyPostForm = {
180     edit_id: post.id,
181     stickied,
182     auth: api.auth,
183   };
184   return api.client.stickyPost(form);
185 }
186
187 export async function lockPost(
188   api: API,
189   locked: boolean,
190   post: Post
191 ): Promise<PostResponse> {
192   let form: LockPostForm = {
193     edit_id: post.id,
194     locked,
195     auth: api.auth,
196   };
197   return api.client.lockPost(form);
198 }
199
200 export async function searchPost(
201   api: API,
202   post: Post
203 ): Promise<SearchResponse> {
204   let form: SearchForm = {
205     q: post.ap_id,
206     type_: SearchType.Posts,
207     sort: SortType.TopAll,
208   };
209   return api.client.search(form);
210 }
211
212 export async function getPost(
213   api: API,
214   post_id: number
215 ): Promise<GetPostResponse> {
216   let form: GetPostForm = {
217     id: post_id,
218   };
219   return api.client.getPost(form);
220 }
221
222 export async function searchComment(
223   api: API,
224   comment: Comment
225 ): Promise<SearchResponse> {
226   let form: SearchForm = {
227     q: comment.ap_id,
228     type_: SearchType.Comments,
229     sort: SortType.TopAll,
230   };
231   return api.client.search(form);
232 }
233
234 export async function searchForBetaCommunity(
235   api: API
236 ): Promise<SearchResponse> {
237   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
238   // Use short-hand search url
239   let form: SearchForm = {
240     q: '!main@lemmy-beta:8551',
241     type_: SearchType.Communities,
242     sort: SortType.TopAll,
243   };
244   return api.client.search(form);
245 }
246
247 export async function searchForCommunity(
248   api: API,
249   q: string,
250 ): Promise<SearchResponse> {
251   // Use short-hand search url
252   let form: SearchForm = {
253     q,
254     type_: SearchType.Communities,
255     sort: SortType.TopAll,
256   };
257   return api.client.search(form);
258 }
259
260 export async function searchForUser(
261   api: API,
262   apShortname: string
263 ): Promise<SearchResponse> {
264   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
265   // Use short-hand search url
266   let form: SearchForm = {
267     q: apShortname,
268     type_: SearchType.Users,
269     sort: SortType.TopAll,
270   };
271   return api.client.search(form);
272 }
273
274 export async function followCommunity(
275   api: API,
276   follow: boolean,
277   community_id: number
278 ): Promise<CommunityResponse> {
279   let form: FollowCommunityForm = {
280     community_id,
281     follow,
282     auth: api.auth,
283   };
284   return api.client.followCommunity(form);
285 }
286
287 export async function checkFollowedCommunities(
288   api: API
289 ): Promise<GetFollowedCommunitiesResponse> {
290   let form: GetFollowedCommunitiesForm = {
291     auth: api.auth,
292   };
293   return api.client.getFollowedCommunities(form);
294 }
295
296 export async function likePost(
297   api: API,
298   score: number,
299   post: Post
300 ): Promise<PostResponse> {
301   let form: CreatePostLikeForm = {
302     post_id: post.id,
303     score: score,
304     auth: api.auth,
305   };
306
307   return api.client.likePost(form);
308 }
309
310 export async function createComment(
311   api: API,
312   post_id: number,
313   parent_id?: number,
314   content = 'a jest test comment'
315 ): Promise<CommentResponse> {
316   let form: CommentForm = {
317     content,
318     post_id,
319     parent_id,
320     auth: api.auth,
321   };
322   return api.client.createComment(form);
323 }
324
325 export async function updateComment(
326   api: API,
327   edit_id: number,
328   content = 'A jest test federated comment update'
329 ): Promise<CommentResponse> {
330   let form: CommentForm = {
331     content,
332     edit_id,
333     auth: api.auth,
334   };
335   return api.client.editComment(form);
336 }
337
338 export async function deleteComment(
339   api: API,
340   deleted: boolean,
341   edit_id: number
342 ): Promise<CommentResponse> {
343   let form: DeleteCommentForm = {
344     edit_id,
345     deleted,
346     auth: api.auth,
347   };
348   return api.client.deleteComment(form);
349 }
350
351 export async function removeComment(
352   api: API,
353   removed: boolean,
354   edit_id: number
355 ): Promise<CommentResponse> {
356   let form: RemoveCommentForm = {
357     edit_id,
358     removed,
359     auth: api.auth,
360   };
361   return api.client.removeComment(form);
362 }
363
364 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
365   let form: GetUserMentionsForm = {
366     sort: SortType.New,
367     unread_only: false,
368     auth: api.auth,
369   };
370   return api.client.getUserMentions(form);
371 }
372
373 export async function likeComment(
374   api: API,
375   score: number,
376   comment: Comment
377 ): Promise<CommentResponse> {
378   let form: CommentLikeForm = {
379     comment_id: comment.id,
380     score,
381     auth: api.auth,
382   };
383   return api.client.likeComment(form);
384 }
385
386 export async function createCommunity(
387   api: API,
388   name_: string = randomString(5)
389 ): Promise<CommunityResponse> {
390   let description = 'a sample description';
391   let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
392   let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
393   let form: CommunityForm = {
394     name: name_,
395     title: name_,
396     description,
397     icon,
398     banner,
399     category_id: 1,
400     nsfw: false,
401     auth: api.auth,
402   };
403   return api.client.createCommunity(form);
404 }
405
406 export async function getCommunity(
407   api: API,
408   id: number,
409 ): Promise<CommunityResponse> {
410   let form: GetCommunityForm = {
411     id,
412   };
413   return api.client.getCommunity(form);
414 }
415
416 export async function deleteCommunity(
417   api: API,
418   deleted: boolean,
419   edit_id: number
420 ): Promise<CommunityResponse> {
421   let form: DeleteCommunityForm = {
422     edit_id,
423     deleted,
424     auth: api.auth,
425   };
426   return api.client.deleteCommunity(form);
427 }
428
429 export async function removeCommunity(
430   api: API,
431   removed: boolean,
432   edit_id: number
433 ): Promise<CommunityResponse> {
434   let form: RemoveCommunityForm = {
435     edit_id,
436     removed,
437     auth: api.auth,
438   };
439   return api.client.removeCommunity(form);
440 }
441
442 export async function createPrivateMessage(
443   api: API,
444   recipient_id: number
445 ): Promise<PrivateMessageResponse> {
446   let content = 'A jest test federated private message';
447   let form: PrivateMessageForm = {
448     content,
449     recipient_id,
450     auth: api.auth,
451   };
452   return api.client.createPrivateMessage(form);
453 }
454
455 export async function updatePrivateMessage(
456   api: API,
457   edit_id: number
458 ): Promise<PrivateMessageResponse> {
459   let updatedContent = 'A jest test federated private message edited';
460   let form: EditPrivateMessageForm = {
461     content: updatedContent,
462     edit_id,
463     auth: api.auth,
464   };
465   return api.client.editPrivateMessage(form);
466 }
467
468 export async function deletePrivateMessage(
469   api: API,
470   deleted: boolean,
471   edit_id: number
472 ): Promise<PrivateMessageResponse> {
473   let form: DeletePrivateMessageForm = {
474     deleted,
475     edit_id,
476     auth: api.auth,
477   };
478   return api.client.deletePrivateMessage(form);
479 }
480
481 export async function registerUser(
482   api: API,
483   username: string = randomString(5)
484 ): Promise<LoginResponse> {
485   let form: RegisterForm = {
486     username,
487     password: 'test',
488     password_verify: 'test',
489     admin: false,
490     show_nsfw: true,
491   };
492   return api.client.register(form);
493 }
494
495 export async function saveUserSettingsBio(
496   api: API,
497   auth: string
498 ): Promise<LoginResponse> {
499   let form: UserSettingsForm = {
500     show_nsfw: true,
501     theme: 'darkly',
502     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
503     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
504     lang: 'en',
505     show_avatars: true,
506     send_notifications_to_email: false,
507     bio: 'a changed bio',
508     auth,
509   };
510   return saveUserSettings(api, form);
511 }
512
513 export async function saveUserSettings(
514   api: API,
515   form: UserSettingsForm
516 ): Promise<LoginResponse> {
517   return api.client.saveUserSettings(form);
518 }
519
520 export async function getSite(
521   api: API,
522   auth: string
523 ): Promise<GetSiteResponse> {
524   let form: GetSiteForm = {
525     auth,
526   };
527   return api.client.getSite(form);
528 }
529
530 export async function listPrivateMessages(
531   api: API
532 ): Promise<PrivateMessagesResponse> {
533   let form: GetPrivateMessagesForm = {
534     auth: api.auth,
535     unread_only: false,
536     limit: 999,
537   };
538   return api.client.getPrivateMessages(form);
539 }
540
541 export async function unfollowRemotes(
542   api: API
543 ): Promise<GetFollowedCommunitiesResponse> {
544   // Unfollow all remote communities
545   let followed = await checkFollowedCommunities(api);
546   let remoteFollowed = followed.communities.filter(
547     c => c.community_local == false
548   );
549   for (let cu of remoteFollowed) {
550     await followCommunity(api, false, cu.community_id);
551   }
552   let followed2 = await checkFollowedCommunities(api);
553   return followed2;
554 }
555
556 export async function followBeta(api: API): Promise<CommunityResponse> {
557   await unfollowRemotes(api);
558
559   // Cache it
560   let search = await searchForBetaCommunity(api);
561   let com = search.communities.filter(c => c.local == false);
562   if (com[0]) {
563     let follow = await followCommunity(api, true, com[0].id);
564     return follow;
565   }
566 }
567
568 export function delay(millis: number = 500) {
569   return new Promise((resolve, _reject) => {
570     setTimeout(_ => resolve(), millis);
571   });
572 }
573
574 export function longDelay() {
575   return delay(10000);
576 }
577
578 export function wrapper(form: any): string {
579   return JSON.stringify(form);
580 }
581
582 function randomString(length: number): string {
583   var result = '';
584   var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
585   var charactersLength = characters.length;
586   for (var i = 0; i < length; i++) {
587     result += characters.charAt(Math.floor(Math.random() * charactersLength));
588   }
589   return result;
590 }