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