]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
1eec6dcfb3fa38cbb5e164bca70c34c314db6b4b
[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://localhost:8541'),
70 };
71
72 export let beta: API = {
73   client: new LemmyHttp('http://localhost:8551'),
74 };
75
76 export let gamma: API = {
77   client: new LemmyHttp('http://localhost:8561'),
78 };
79
80 export let delta: API = {
81   client: new LemmyHttp('http://localhost:8571'),
82 };
83
84 export let epsilon: API = {
85   client: new LemmyHttp('http://localhost: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 ): Promise<BanPersonResponse> {
294   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
295   let form: BanPerson = {
296     person_id,
297     ban,
298     remove_data: false,
299     auth: api.auth,
300   };
301   return api.client.banPerson(form);
302 }
303
304 export async function banPersonFromCommunity(
305   api: API,
306   person_id: number,
307   community_id: number,
308   ban: boolean
309 ): Promise<BanFromCommunityResponse> {
310   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
311   let form: BanFromCommunity = {
312     person_id,
313     community_id,
314     remove_data: false,
315     ban,
316     auth: api.auth,
317   };
318   return api.client.banFromCommunity(form);
319 }
320
321 export async function followCommunity(
322   api: API,
323   follow: boolean,
324   community_id: number
325 ): Promise<CommunityResponse> {
326   let form: FollowCommunity = {
327     community_id,
328     follow,
329     auth: api.auth,
330   };
331   return api.client.followCommunity(form);
332 }
333
334 export async function likePost(
335   api: API,
336   score: number,
337   post: Post
338 ): Promise<PostResponse> {
339   let form: CreatePostLike = {
340     post_id: post.id,
341     score: score,
342     auth: api.auth,
343   };
344
345   return api.client.likePost(form);
346 }
347
348 export async function createComment(
349   api: API,
350   post_id: number,
351   parent_id?: number,
352   content = 'a jest test comment'
353 ): Promise<CommentResponse> {
354   let form: CreateComment = {
355     content,
356     post_id,
357     parent_id,
358     auth: api.auth,
359   };
360   return api.client.createComment(form);
361 }
362
363 export async function editComment(
364   api: API,
365   comment_id: number,
366   content = 'A jest test federated comment update'
367 ): Promise<CommentResponse> {
368   let form: EditComment = {
369     content,
370     comment_id,
371     auth: api.auth,
372   };
373   return api.client.editComment(form);
374 }
375
376 export async function deleteComment(
377   api: API,
378   deleted: boolean,
379   comment_id: number
380 ): Promise<CommentResponse> {
381   let form: DeleteComment = {
382     comment_id,
383     deleted,
384     auth: api.auth,
385   };
386   return api.client.deleteComment(form);
387 }
388
389 export async function removeComment(
390   api: API,
391   removed: boolean,
392   comment_id: number
393 ): Promise<CommentResponse> {
394   let form: RemoveComment = {
395     comment_id,
396     removed,
397     auth: api.auth,
398   };
399   return api.client.removeComment(form);
400 }
401
402 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
403   let form: GetPersonMentions = {
404     sort: SortType.New,
405     unread_only: false,
406     auth: api.auth,
407   };
408   return api.client.getPersonMentions(form);
409 }
410
411 export async function likeComment(
412   api: API,
413   score: number,
414   comment: Comment
415 ): Promise<CommentResponse> {
416   let form: CreateCommentLike = {
417     comment_id: comment.id,
418     score,
419     auth: api.auth,
420   };
421   return api.client.likeComment(form);
422 }
423
424 export async function createCommunity(
425   api: API,
426   name_: string = randomString(5)
427 ): Promise<CommunityResponse> {
428   let description = 'a sample description';
429   let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
430   let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
431   let form: CreateCommunity = {
432     name: name_,
433     title: name_,
434     description,
435     icon,
436     banner,
437     nsfw: false,
438     auth: api.auth,
439   };
440   return api.client.createCommunity(form);
441 }
442
443 export async function getCommunity(
444   api: API,
445   id: number
446 ): Promise<CommunityResponse> {
447   let form: GetCommunity = {
448     id,
449   };
450   return api.client.getCommunity(form);
451 }
452
453 export async function deleteCommunity(
454   api: API,
455   deleted: boolean,
456   community_id: number
457 ): Promise<CommunityResponse> {
458   let form: DeleteCommunity = {
459     community_id,
460     deleted,
461     auth: api.auth,
462   };
463   return api.client.deleteCommunity(form);
464 }
465
466 export async function removeCommunity(
467   api: API,
468   removed: boolean,
469   community_id: number
470 ): Promise<CommunityResponse> {
471   let form: RemoveCommunity = {
472     community_id,
473     removed,
474     auth: api.auth,
475   };
476   return api.client.removeCommunity(form);
477 }
478
479 export async function createPrivateMessage(
480   api: API,
481   recipient_id: number
482 ): Promise<PrivateMessageResponse> {
483   let content = 'A jest test federated private message';
484   let form: CreatePrivateMessage = {
485     content,
486     recipient_id,
487     auth: api.auth,
488   };
489   return api.client.createPrivateMessage(form);
490 }
491
492 export async function editPrivateMessage(
493   api: API,
494   private_message_id: number
495 ): Promise<PrivateMessageResponse> {
496   let updatedContent = 'A jest test federated private message edited';
497   let form: EditPrivateMessage = {
498     content: updatedContent,
499     private_message_id,
500     auth: api.auth,
501   };
502   return api.client.editPrivateMessage(form);
503 }
504
505 export async function deletePrivateMessage(
506   api: API,
507   deleted: boolean,
508   private_message_id: number
509 ): Promise<PrivateMessageResponse> {
510   let form: DeletePrivateMessage = {
511     deleted,
512     private_message_id,
513     auth: api.auth,
514   };
515   return api.client.deletePrivateMessage(form);
516 }
517
518 export async function registerUser(
519   api: API,
520   username: string = randomString(5)
521 ): Promise<LoginResponse> {
522   let form: Register = {
523     username,
524     password,
525     password_verify: password,
526     show_nsfw: true,
527   };
528   return api.client.register(form);
529 }
530
531 export async function saveUserSettingsBio(
532   api: API
533 ): Promise<LoginResponse> {
534   let form: SaveUserSettings = {
535     show_nsfw: true,
536     theme: 'darkly',
537     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
538     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
539     lang: 'en',
540     show_avatars: true,
541     send_notifications_to_email: false,
542     bio: 'a changed bio',
543     auth: api.auth,
544   };
545   return saveUserSettings(api, form);
546 }
547
548 export async function saveUserSettings(
549   api: API,
550   form: SaveUserSettings
551 ): Promise<LoginResponse> {
552   return api.client.saveUserSettings(form);
553 }
554
555 export async function getSite(
556   api: API
557 ): Promise<GetSiteResponse> {
558   let form: GetSite = {
559     auth: api.auth,
560   };
561   return api.client.getSite(form);
562 }
563
564 export async function listPrivateMessages(
565   api: API
566 ): Promise<PrivateMessagesResponse> {
567   let form: GetPrivateMessages = {
568     auth: api.auth,
569     unread_only: false,
570     limit: 999,
571   };
572   return api.client.getPrivateMessages(form);
573 }
574
575 export async function unfollowRemotes(
576   api: API
577 ): Promise<GetSiteResponse> {
578   // Unfollow all remote communities
579   let site = await getSite(api);
580   let remoteFollowed = site.my_user.follows.filter(
581     c => c.community.local == false
582   );
583   for (let cu of remoteFollowed) {
584     await followCommunity(api, false, cu.community.id);
585   }
586   let siteRes = await getSite(api);
587   return siteRes;
588 }
589
590 export async function followBeta(api: API): Promise<CommunityResponse> {
591   let betaCommunity = (await resolveBetaCommunity(api)).community;
592   if (betaCommunity) {
593     let follow = await followCommunity(api, true, betaCommunity.community.id);
594     return follow;
595   }
596 }
597
598 export async function reportPost(
599   api: API,
600   post_id: number,
601   reason: string,
602 ): Promise<PostReportResponse> {
603   let form: CreatePostReport = {
604     post_id,
605     reason,
606     auth: api.auth,
607   };
608   return api.client.createPostReport(form);
609 }
610
611 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
612   let form: ListPostReports = {
613     auth: api.auth,
614   };
615   return api.client.listPostReports(form);
616 }
617
618 export async function reportComment(
619   api: API,
620   comment_id: number,
621   reason: string,
622 ): Promise<CommentReportResponse> {
623   let form: CreateCommentReport = {
624     comment_id,
625     reason,
626     auth: api.auth,
627   };
628   return api.client.createCommentReport(form);
629 }
630
631 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
632   let form: ListCommentReports = {
633     auth: api.auth,
634   };
635   return api.client.listCommentReports(form);
636 }
637
638 export function delay(millis: number = 500) {
639   return new Promise(resolve => setTimeout(resolve, millis));
640 }
641
642 export function longDelay() {
643   return delay(10000);
644 }
645
646 export function wrapper(form: any): string {
647   return JSON.stringify(form);
648 }
649
650 export function randomString(length: number): string {
651   var result = '';
652   var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
653   var charactersLength = characters.length;
654   for (var i = 0; i < length; i++) {
655     result += characters.charAt(Math.floor(Math.random() * charactersLength));
656   }
657   return result;
658 }