]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Merge branch 'main' into add_jerboa_link
[lemmy.git] / api_tests / src / shared.ts
1 import {
2   Login,
3   LoginResponse,
4   CreatePost,
5   EditPost,
6   CreateComment,
7   DeletePost,
8   RemovePost,
9   StickyPost,
10   LockPost,
11   PostResponse,
12   SearchResponse,
13   FollowCommunity,
14   CommunityResponse,
15   GetPostResponse,
16   Register,
17   Comment,
18   EditComment,
19   DeleteComment,
20   RemoveComment,
21   Search,
22   CommentResponse,
23   GetCommunity,
24   CreateCommunity,
25   DeleteCommunity,
26   RemoveCommunity,
27   GetPersonMentions,
28   CreateCommentLike,
29   CreatePostLike,
30   EditPrivateMessage,
31   DeletePrivateMessage,
32   GetPrivateMessages,
33   GetSite,
34   GetPost,
35   PrivateMessageResponse,
36   PrivateMessagesResponse,
37   GetPersonMentionsResponse,
38   SaveUserSettings,
39   SortType,
40   ListingType,
41   GetSiteResponse,
42   SearchType,
43   LemmyHttp,
44   BanPersonResponse,
45   BanPerson,
46   BanFromCommunity,
47   BanFromCommunityResponse,
48   Post,
49   CreatePrivateMessage,
50   ResolveObjectResponse,
51   ResolveObject,
52   CreatePostReport,
53   PostReport,
54   ListPostReports,
55   PostReportResponse,
56   ListPostReportsResponse,
57   CreateCommentReport,
58   CommentReportResponse,
59   ListCommentReports,
60   ListCommentReportsResponse,
61 } from 'lemmy-js-client';
62
63 export interface API {
64   client: LemmyHttp;
65   auth?: string;
66 }
67
68 export let alpha: API = {
69   client: new LemmyHttp('http://127.0.0.1:8541'),
70 };
71
72 export let beta: API = {
73   client: new LemmyHttp('http://127.0.0.1:8551'),
74 };
75
76 export let gamma: API = {
77   client: new LemmyHttp('http://127.0.0.1:8561'),
78 };
79
80 export let delta: API = {
81   client: new LemmyHttp('http://127.0.0.1:8571'),
82 };
83
84 export let epsilon: API = {
85   client: new LemmyHttp('http://127.0.0.1:8581'),
86 };
87
88 const password = 'lemmylemmy'
89
90 export async function setupLogins() {
91   let formAlpha: Login = {
92     username_or_email: 'lemmy_alpha',
93     password,
94   };
95   let resAlpha = alpha.client.login(formAlpha);
96
97   let formBeta = {
98     username_or_email: 'lemmy_beta',
99     password,
100   };
101   let resBeta = beta.client.login(formBeta);
102
103   let formGamma = {
104     username_or_email: 'lemmy_gamma',
105     password,
106   };
107   let resGamma = gamma.client.login(formGamma);
108
109   let formDelta = {
110     username_or_email: 'lemmy_delta',
111     password,
112   };
113   let resDelta = delta.client.login(formDelta);
114
115   let formEpsilon = {
116     username_or_email: 'lemmy_epsilon',
117     password,
118   };
119   let resEpsilon = epsilon.client.login(formEpsilon);
120
121   let res = await Promise.all([
122     resAlpha,
123     resBeta,
124     resGamma,
125     resDelta,
126     resEpsilon,
127   ]);
128
129   alpha.auth = res[0].jwt;
130   beta.auth = res[1].jwt;
131   gamma.auth = res[2].jwt;
132   delta.auth = res[3].jwt;
133   epsilon.auth = res[4].jwt;
134 }
135
136 export async function createPost(
137   api: API,
138   community_id: number
139 ): Promise<PostResponse> {
140   let name = randomString(5);
141   let body = randomString(10);
142   let url = 'https://google.com/';
143   let form: CreatePost = {
144     name,
145     url,
146     body,
147     auth: api.auth,
148     community_id,
149     nsfw: false,
150   };
151   return api.client.createPost(form);
152 }
153
154 export async function editPost(api: API, post: Post): Promise<PostResponse> {
155   let name = 'A jest test federated post, updated';
156   let form: EditPost = {
157     name,
158     post_id: post.id,
159     auth: api.auth,
160     nsfw: false,
161   };
162   return api.client.editPost(form);
163 }
164
165 export async function deletePost(
166   api: API,
167   deleted: boolean,
168   post: Post
169 ): Promise<PostResponse> {
170   let form: DeletePost = {
171     post_id: post.id,
172     deleted: deleted,
173     auth: api.auth,
174   };
175   return api.client.deletePost(form);
176 }
177
178 export async function removePost(
179   api: API,
180   removed: boolean,
181   post: Post
182 ): Promise<PostResponse> {
183   let form: RemovePost = {
184     post_id: post.id,
185     removed,
186     auth: api.auth,
187   };
188   return api.client.removePost(form);
189 }
190
191 export async function stickyPost(
192   api: API,
193   stickied: boolean,
194   post: Post
195 ): Promise<PostResponse> {
196   let form: StickyPost = {
197     post_id: post.id,
198     stickied,
199     auth: api.auth,
200   };
201   return api.client.stickyPost(form);
202 }
203
204 export async function lockPost(
205   api: API,
206   locked: boolean,
207   post: Post
208 ): Promise<PostResponse> {
209   let form: LockPost = {
210     post_id: post.id,
211     locked,
212     auth: api.auth,
213   };
214   return api.client.lockPost(form);
215 }
216
217 export async function resolvePost(
218   api: API,
219   post: Post
220 ): Promise<ResolveObjectResponse> {
221   let form: ResolveObject = {
222     q: post.ap_id,
223   };
224   return api.client.resolveObject(form);
225 }
226
227 export async function searchPostLocal(
228   api: API,
229   post: Post
230 ): Promise<SearchResponse> {
231   let form: Search = {
232     q: post.name,
233     type_: SearchType.Posts,
234     sort: SortType.TopAll,
235   };
236   return api.client.search(form);
237 }
238
239 export async function getPost(
240   api: API,
241   post_id: number
242 ): Promise<GetPostResponse> {
243   let form: GetPost = {
244     id: post_id,
245   };
246   return api.client.getPost(form);
247 }
248
249 export async function resolveComment(
250   api: API,
251   comment: Comment
252 ): Promise<ResolveObjectResponse> {
253   let form: ResolveObject = {
254     q: comment.ap_id,
255   };
256   return api.client.resolveObject(form);
257 }
258
259 export async function resolveBetaCommunity(
260   api: API
261 ): Promise<ResolveObjectResponse> {
262   // Use short-hand search url
263   let form: ResolveObject = {
264     q: '!main@lemmy-beta:8551',
265   };
266   return api.client.resolveObject(form);
267 }
268
269 export async function resolveCommunity(
270   api: API,
271   q: string
272 ): Promise<ResolveObjectResponse> {
273   let form: ResolveObject = {
274     q,
275   };
276   return api.client.resolveObject(form);
277 }
278
279 export async function resolvePerson(
280   api: API,
281   apShortname: string
282 ): Promise<ResolveObjectResponse> {
283   let form: ResolveObject = {
284     q: apShortname,
285   };
286   return api.client.resolveObject(form);
287 }
288
289 export async function banPersonFromSite(
290   api: API,
291   person_id: number,
292   ban: boolean,
293   remove_data: boolean,
294 ): Promise<BanPersonResponse> {
295   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
296   let form: BanPerson = {
297     person_id,
298     ban,
299     remove_data,
300     auth: api.auth,
301   };
302   return api.client.banPerson(form);
303 }
304
305 export async function banPersonFromCommunity(
306   api: API,
307   person_id: number,
308   community_id: number,
309   remove_data: boolean,
310   ban: boolean
311 ): Promise<BanFromCommunityResponse> {
312   let form: BanFromCommunity = {
313     person_id,
314     community_id,
315     remove_data,
316     ban,
317     auth: api.auth,
318   };
319   return api.client.banFromCommunity(form);
320 }
321
322 export async function followCommunity(
323   api: API,
324   follow: boolean,
325   community_id: number
326 ): Promise<CommunityResponse> {
327   let form: FollowCommunity = {
328     community_id,
329     follow,
330     auth: api.auth,
331   };
332   return api.client.followCommunity(form);
333 }
334
335 export async function likePost(
336   api: API,
337   score: number,
338   post: Post
339 ): Promise<PostResponse> {
340   let form: CreatePostLike = {
341     post_id: post.id,
342     score: score,
343     auth: api.auth,
344   };
345
346   return api.client.likePost(form);
347 }
348
349 export async function createComment(
350   api: API,
351   post_id: number,
352   parent_id?: number,
353   content = 'a jest test comment'
354 ): Promise<CommentResponse> {
355   let form: CreateComment = {
356     content,
357     post_id,
358     parent_id,
359     auth: api.auth,
360   };
361   return api.client.createComment(form);
362 }
363
364 export async function editComment(
365   api: API,
366   comment_id: number,
367   content = 'A jest test federated comment update'
368 ): Promise<CommentResponse> {
369   let form: EditComment = {
370     content,
371     comment_id,
372     auth: api.auth,
373   };
374   return api.client.editComment(form);
375 }
376
377 export async function deleteComment(
378   api: API,
379   deleted: boolean,
380   comment_id: number
381 ): Promise<CommentResponse> {
382   let form: DeleteComment = {
383     comment_id,
384     deleted,
385     auth: api.auth,
386   };
387   return api.client.deleteComment(form);
388 }
389
390 export async function removeComment(
391   api: API,
392   removed: boolean,
393   comment_id: number
394 ): Promise<CommentResponse> {
395   let form: RemoveComment = {
396     comment_id,
397     removed,
398     auth: api.auth,
399   };
400   return api.client.removeComment(form);
401 }
402
403 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
404   let form: GetPersonMentions = {
405     sort: SortType.New,
406     unread_only: false,
407     auth: api.auth,
408   };
409   return api.client.getPersonMentions(form);
410 }
411
412 export async function likeComment(
413   api: API,
414   score: number,
415   comment: Comment
416 ): Promise<CommentResponse> {
417   let form: CreateCommentLike = {
418     comment_id: comment.id,
419     score,
420     auth: api.auth,
421   };
422   return api.client.likeComment(form);
423 }
424
425 export async function createCommunity(
426   api: API,
427   name_: string = randomString(5)
428 ): Promise<CommunityResponse> {
429   let description = 'a sample description';
430   let form: CreateCommunity = {
431     name: name_,
432     title: name_,
433     description,
434     nsfw: false,
435     auth: api.auth,
436   };
437   return api.client.createCommunity(form);
438 }
439
440 export async function getCommunity(
441   api: API,
442   id: number
443 ): Promise<CommunityResponse> {
444   let form: GetCommunity = {
445     id,
446   };
447   return api.client.getCommunity(form);
448 }
449
450 export async function deleteCommunity(
451   api: API,
452   deleted: boolean,
453   community_id: number
454 ): Promise<CommunityResponse> {
455   let form: DeleteCommunity = {
456     community_id,
457     deleted,
458     auth: api.auth,
459   };
460   return api.client.deleteCommunity(form);
461 }
462
463 export async function removeCommunity(
464   api: API,
465   removed: boolean,
466   community_id: number
467 ): Promise<CommunityResponse> {
468   let form: RemoveCommunity = {
469     community_id,
470     removed,
471     auth: api.auth,
472   };
473   return api.client.removeCommunity(form);
474 }
475
476 export async function createPrivateMessage(
477   api: API,
478   recipient_id: number
479 ): Promise<PrivateMessageResponse> {
480   let content = 'A jest test federated private message';
481   let form: CreatePrivateMessage = {
482     content,
483     recipient_id,
484     auth: api.auth,
485   };
486   return api.client.createPrivateMessage(form);
487 }
488
489 export async function editPrivateMessage(
490   api: API,
491   private_message_id: number
492 ): Promise<PrivateMessageResponse> {
493   let updatedContent = 'A jest test federated private message edited';
494   let form: EditPrivateMessage = {
495     content: updatedContent,
496     private_message_id,
497     auth: api.auth,
498   };
499   return api.client.editPrivateMessage(form);
500 }
501
502 export async function deletePrivateMessage(
503   api: API,
504   deleted: boolean,
505   private_message_id: number
506 ): Promise<PrivateMessageResponse> {
507   let form: DeletePrivateMessage = {
508     deleted,
509     private_message_id,
510     auth: api.auth,
511   };
512   return api.client.deletePrivateMessage(form);
513 }
514
515 export async function registerUser(
516   api: API,
517   username: string = randomString(5)
518 ): Promise<LoginResponse> {
519   let form: Register = {
520     username,
521     password,
522     password_verify: password,
523     show_nsfw: true,
524   };
525   return api.client.register(form);
526 }
527
528 export async function saveUserSettingsBio(
529   api: API
530 ): Promise<LoginResponse> {
531   let form: SaveUserSettings = {
532     show_nsfw: true,
533     theme: 'darkly',
534     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
535     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
536     lang: 'en',
537     show_avatars: true,
538     send_notifications_to_email: false,
539     bio: 'a changed bio',
540     auth: api.auth,
541   };
542   return saveUserSettings(api, form);
543 }
544
545 export async function saveUserSettings(
546   api: API,
547   form: SaveUserSettings
548 ): Promise<LoginResponse> {
549   return api.client.saveUserSettings(form);
550 }
551
552 export async function getSite(
553   api: API
554 ): Promise<GetSiteResponse> {
555   let form: GetSite = {
556     auth: api.auth,
557   };
558   return api.client.getSite(form);
559 }
560
561 export async function listPrivateMessages(
562   api: API
563 ): Promise<PrivateMessagesResponse> {
564   let form: GetPrivateMessages = {
565     auth: api.auth,
566     unread_only: false,
567     limit: 999,
568   };
569   return api.client.getPrivateMessages(form);
570 }
571
572 export async function unfollowRemotes(
573   api: API
574 ): Promise<GetSiteResponse> {
575   // Unfollow all remote communities
576   let site = await getSite(api);
577   let remoteFollowed = site.my_user.follows.filter(
578     c => c.community.local == false
579   );
580   for (let cu of remoteFollowed) {
581     await followCommunity(api, false, cu.community.id);
582   }
583   let siteRes = await getSite(api);
584   return siteRes;
585 }
586
587 export async function followBeta(api: API): Promise<CommunityResponse> {
588   let betaCommunity = (await resolveBetaCommunity(api)).community;
589   if (betaCommunity) {
590     let follow = await followCommunity(api, true, betaCommunity.community.id);
591     return follow;
592   }
593 }
594
595 export async function reportPost(
596   api: API,
597   post_id: number,
598   reason: string,
599 ): Promise<PostReportResponse> {
600   let form: CreatePostReport = {
601     post_id,
602     reason,
603     auth: api.auth,
604   };
605   return api.client.createPostReport(form);
606 }
607
608 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
609   let form: ListPostReports = {
610     auth: api.auth,
611   };
612   return api.client.listPostReports(form);
613 }
614
615 export async function reportComment(
616   api: API,
617   comment_id: number,
618   reason: string,
619 ): Promise<CommentReportResponse> {
620   let form: CreateCommentReport = {
621     comment_id,
622     reason,
623     auth: api.auth,
624   };
625   return api.client.createCommentReport(form);
626 }
627
628 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
629   let form: ListCommentReports = {
630     auth: api.auth,
631   };
632   return api.client.listCommentReports(form);
633 }
634
635 export function delay(millis: number = 500) {
636   return new Promise(resolve => setTimeout(resolve, millis));
637 }
638
639 export function longDelay() {
640   return delay(10000);
641 }
642
643 export function wrapper(form: any): string {
644   return JSON.stringify(form);
645 }
646
647 export function randomString(length: number): string {
648   var result = '';
649   var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
650   var charactersLength = characters.length;
651   for (var i = 0; i < length; i++) {
652     result += characters.charAt(Math.floor(Math.random() * charactersLength));
653   }
654   return result;
655 }
656
657 export async function unfollows() {
658   await unfollowRemotes(alpha);
659   await unfollowRemotes(gamma);
660   await unfollowRemotes(delta);
661   await unfollowRemotes(epsilon);
662 }