15 GetFollowedCommunitiesResponse,
31 EditPrivateMessageForm,
32 DeletePrivateMessageForm,
33 GetFollowedCommunitiesForm,
34 GetPrivateMessagesForm,
37 PrivateMessageResponse,
38 PrivateMessagesResponse,
39 GetUserMentionsResponse,
46 } from 'lemmy-js-client';
48 export interface API {
53 export let alpha: API = {
54 client: new LemmyHttp('http://localhost:8541/api/v1'),
57 export let beta: API = {
58 client: new LemmyHttp('http://localhost:8551/api/v1'),
61 export let gamma: API = {
62 client: new LemmyHttp('http://localhost:8561/api/v1'),
65 export let delta: API = {
66 client: new LemmyHttp('http://localhost:8571/api/v1'),
69 export let epsilon: API = {
70 client: new LemmyHttp('http://localhost:8581/api/v1'),
73 export async function setupLogins() {
74 let formAlpha: LoginForm = {
75 username_or_email: 'lemmy_alpha',
78 let resAlpha = alpha.client.login(formAlpha);
81 username_or_email: 'lemmy_beta',
84 let resBeta = beta.client.login(formBeta);
87 username_or_email: 'lemmy_gamma',
90 let resGamma = gamma.client.login(formGamma);
93 username_or_email: 'lemmy_delta',
96 let resDelta = delta.client.login(formDelta);
99 username_or_email: 'lemmy_epsilon',
102 let resEpsilon = epsilon.client.login(formEpsilon);
104 let res = await Promise.all([
112 alpha.auth = res[0].jwt;
113 beta.auth = res[1].jwt;
114 gamma.auth = res[2].jwt;
115 delta.auth = res[3].jwt;
116 epsilon.auth = res[4].jwt;
119 export async function createPost(
122 ): Promise<PostResponse> {
123 let name = 'A jest test post';
124 let body = 'Some body';
125 let url = 'https://google.com/';
126 let form: PostForm = {
134 return api.client.createPost(form);
137 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
138 let name = 'A jest test federated post, updated';
139 let form: PostForm = {
145 return api.client.editPost(form);
148 export async function deletePost(
152 ): Promise<PostResponse> {
153 let form: DeletePostForm = {
158 return api.client.deletePost(form);
161 export async function removePost(
165 ): Promise<PostResponse> {
166 let form: RemovePostForm = {
171 return api.client.removePost(form);
174 export async function stickyPost(
178 ): Promise<PostResponse> {
179 let form: StickyPostForm = {
184 return api.client.stickyPost(form);
187 export async function lockPost(
191 ): Promise<PostResponse> {
192 let form: LockPostForm = {
197 return api.client.lockPost(form);
200 export async function searchPost(
203 ): Promise<SearchResponse> {
204 let form: SearchForm = {
206 type_: SearchType.Posts,
207 sort: SortType.TopAll,
209 return api.client.search(form);
212 export async function getPost(
215 ): Promise<GetPostResponse> {
216 let form: GetPostForm = {
219 return api.client.getPost(form);
222 export async function searchComment(
225 ): Promise<SearchResponse> {
226 let form: SearchForm = {
228 type_: SearchType.Comments,
229 sort: SortType.TopAll,
231 return api.client.search(form);
234 export async function searchForBetaCommunity(
236 ): Promise<SearchResponse> {
237 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
238 // Use short-hand search url
239 let form: SearchForm = {
240 q: '!main@lemmy-beta:8551',
241 type_: SearchType.Communities,
242 sort: SortType.TopAll,
244 return api.client.search(form);
247 export async function searchForCommunity(
250 ): Promise<SearchResponse> {
251 // Use short-hand search url
252 let form: SearchForm = {
254 type_: SearchType.Communities,
255 sort: SortType.TopAll,
257 return api.client.search(form);
260 export async function searchForUser(
263 ): Promise<SearchResponse> {
264 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
265 // Use short-hand search url
266 let form: SearchForm = {
268 type_: SearchType.Users,
269 sort: SortType.TopAll,
271 return api.client.search(form);
274 export async function followCommunity(
278 ): Promise<CommunityResponse> {
279 let form: FollowCommunityForm = {
284 return api.client.followCommunity(form);
287 export async function checkFollowedCommunities(
289 ): Promise<GetFollowedCommunitiesResponse> {
290 let form: GetFollowedCommunitiesForm = {
293 return api.client.getFollowedCommunities(form);
296 export async function likePost(
300 ): Promise<PostResponse> {
301 let form: CreatePostLikeForm = {
307 return api.client.likePost(form);
310 export async function createComment(
314 content = 'a jest test comment'
315 ): Promise<CommentResponse> {
316 let form: CommentForm = {
322 return api.client.createComment(form);
325 export async function updateComment(
328 content = 'A jest test federated comment update'
329 ): Promise<CommentResponse> {
330 let form: CommentForm = {
335 return api.client.editComment(form);
338 export async function deleteComment(
342 ): Promise<CommentResponse> {
343 let form: DeleteCommentForm = {
348 return api.client.deleteComment(form);
351 export async function removeComment(
355 ): Promise<CommentResponse> {
356 let form: RemoveCommentForm = {
361 return api.client.removeComment(form);
364 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
365 let form: GetUserMentionsForm = {
370 return api.client.getUserMentions(form);
373 export async function likeComment(
377 ): Promise<CommentResponse> {
378 let form: CommentLikeForm = {
379 comment_id: comment.id,
383 return api.client.likeComment(form);
386 export async function createCommunity(
388 name_: string = randomString(5)
389 ): Promise<CommunityResponse> {
390 let description = 'a sample description';
391 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
392 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
393 let form: CommunityForm = {
403 return api.client.createCommunity(form);
406 export async function getCommunity(
409 ): Promise<CommunityResponse> {
410 let form: GetCommunityForm = {
413 return api.client.getCommunity(form);
416 export async function deleteCommunity(
420 ): Promise<CommunityResponse> {
421 let form: DeleteCommunityForm = {
426 return api.client.deleteCommunity(form);
429 export async function removeCommunity(
433 ): Promise<CommunityResponse> {
434 let form: RemoveCommunityForm = {
439 return api.client.removeCommunity(form);
442 export async function createPrivateMessage(
445 ): Promise<PrivateMessageResponse> {
446 let content = 'A jest test federated private message';
447 let form: PrivateMessageForm = {
452 return api.client.createPrivateMessage(form);
455 export async function updatePrivateMessage(
458 ): Promise<PrivateMessageResponse> {
459 let updatedContent = 'A jest test federated private message edited';
460 let form: EditPrivateMessageForm = {
461 content: updatedContent,
465 return api.client.editPrivateMessage(form);
468 export async function deletePrivateMessage(
472 ): Promise<PrivateMessageResponse> {
473 let form: DeletePrivateMessageForm = {
478 return api.client.deletePrivateMessage(form);
481 export async function registerUser(
483 username: string = randomString(5)
484 ): Promise<LoginResponse> {
485 let form: RegisterForm = {
488 password_verify: 'test',
492 return api.client.register(form);
495 export async function saveUserSettingsBio(
498 ): Promise<LoginResponse> {
499 let form: UserSettingsForm = {
502 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
503 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
506 send_notifications_to_email: false,
507 bio: 'a changed bio',
510 return saveUserSettings(api, form);
513 export async function saveUserSettings(
515 form: UserSettingsForm
516 ): Promise<LoginResponse> {
517 return api.client.saveUserSettings(form);
520 export async function getSite(
523 ): Promise<GetSiteResponse> {
524 let form: GetSiteForm = {
527 return api.client.getSite(form);
530 export async function listPrivateMessages(
532 ): Promise<PrivateMessagesResponse> {
533 let form: GetPrivateMessagesForm = {
538 return api.client.getPrivateMessages(form);
541 export async function unfollowRemotes(
543 ): Promise<GetFollowedCommunitiesResponse> {
544 // Unfollow all remote communities
545 let followed = await checkFollowedCommunities(api);
546 let remoteFollowed = followed.communities.filter(
547 c => c.community_local == false
549 for (let cu of remoteFollowed) {
550 await followCommunity(api, false, cu.community_id);
552 let followed2 = await checkFollowedCommunities(api);
556 export async function followBeta(api: API): Promise<CommunityResponse> {
557 await unfollowRemotes(api);
560 let search = await searchForBetaCommunity(api);
561 let com = search.communities.filter(c => c.local == false);
563 let follow = await followCommunity(api, true, com[0].id);
568 export function delay(millis: number = 500) {
569 return new Promise((resolve, _reject) => {
570 setTimeout(_ => resolve(), millis);
574 export function longDelay() {
578 export function wrapper(form: any): string {
579 return JSON.stringify(form);
582 function randomString(length: number): string {
584 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
585 var charactersLength = characters.length;
586 for (var i = 0; i < length; i++) {
587 result += characters.charAt(Math.floor(Math.random() * charactersLength));