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