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