15 GetFollowedCommunitiesResponse,
31 EditPrivateMessageForm,
32 DeletePrivateMessageForm,
33 GetFollowedCommunitiesForm,
34 GetPrivateMessagesForm,
37 PrivateMessageResponse,
38 PrivateMessagesResponse,
39 GetUserMentionsResponse,
49 BanFromCommunityResponse,
50 } from 'lemmy-js-client';
52 export interface API {
57 export let alpha: API = {
58 client: new LemmyHttp('http://localhost:8541/api/v1'),
61 export let beta: API = {
62 client: new LemmyHttp('http://localhost:8551/api/v1'),
65 export let gamma: API = {
66 client: new LemmyHttp('http://localhost:8561/api/v1'),
69 export let delta: API = {
70 client: new LemmyHttp('http://localhost:8571/api/v1'),
73 export let epsilon: API = {
74 client: new LemmyHttp('http://localhost:8581/api/v1'),
77 export async function setupLogins() {
78 let formAlpha: LoginForm = {
79 username_or_email: 'lemmy_alpha',
82 let resAlpha = alpha.client.login(formAlpha);
85 username_or_email: 'lemmy_beta',
88 let resBeta = beta.client.login(formBeta);
91 username_or_email: 'lemmy_gamma',
94 let resGamma = gamma.client.login(formGamma);
97 username_or_email: 'lemmy_delta',
100 let resDelta = delta.client.login(formDelta);
103 username_or_email: 'lemmy_epsilon',
106 let resEpsilon = epsilon.client.login(formEpsilon);
108 let res = await Promise.all([
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;
123 export async function createPost(
126 ): Promise<PostResponse> {
127 let name = randomString(5);
128 let body = randomString(10);
129 let url = 'https://google.com/';
130 let form: PostForm = {
138 return api.client.createPost(form);
141 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
142 let name = 'A jest test federated post, updated';
143 let form: PostForm = {
149 return api.client.editPost(form);
152 export async function deletePost(
156 ): Promise<PostResponse> {
157 let form: DeletePostForm = {
162 return api.client.deletePost(form);
165 export async function removePost(
169 ): Promise<PostResponse> {
170 let form: RemovePostForm = {
175 return api.client.removePost(form);
178 export async function stickyPost(
182 ): Promise<PostResponse> {
183 let form: StickyPostForm = {
188 return api.client.stickyPost(form);
191 export async function lockPost(
195 ): Promise<PostResponse> {
196 let form: LockPostForm = {
201 return api.client.lockPost(form);
204 export async function searchPost(
207 ): Promise<SearchResponse> {
208 let form: SearchForm = {
210 type_: SearchType.Posts,
211 sort: SortType.TopAll,
213 return api.client.search(form);
216 export async function searchPostLocal(
219 ): Promise<SearchResponse> {
220 let form: SearchForm = {
222 type_: SearchType.Posts,
223 sort: SortType.TopAll,
225 return api.client.search(form);
228 export async function getPost(
231 ): Promise<GetPostResponse> {
232 let form: GetPostForm = {
235 return api.client.getPost(form);
238 export async function searchComment(
241 ): Promise<SearchResponse> {
242 let form: SearchForm = {
244 type_: SearchType.Comments,
245 sort: SortType.TopAll,
247 return api.client.search(form);
250 export async function searchForBetaCommunity(
252 ): Promise<SearchResponse> {
253 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
254 // Use short-hand search url
255 let form: SearchForm = {
256 q: '!main@lemmy-beta:8551',
257 type_: SearchType.Communities,
258 sort: SortType.TopAll,
260 return api.client.search(form);
263 export async function searchForCommunity(
266 ): Promise<SearchResponse> {
267 // Use short-hand search url
268 let form: SearchForm = {
270 type_: SearchType.Communities,
271 sort: SortType.TopAll,
273 return api.client.search(form);
276 export async function searchForUser(
279 ): Promise<SearchResponse> {
280 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
281 // Use short-hand search url
282 let form: SearchForm = {
284 type_: SearchType.Users,
285 sort: SortType.TopAll,
287 return api.client.search(form);
290 export async function banUserFromSite(
294 ): Promise<BanUserResponse> {
295 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
296 // Use short-hand search url
297 let form: BanUserForm = {
302 return api.client.banUser(form);
305 export async function banUserFromCommunity(
308 community_id: number,
310 ): Promise<BanFromCommunityResponse> {
311 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
312 // Use short-hand search url
313 let form: BanFromCommunityForm = {
319 return api.client.banFromCommunity(form);
322 export async function followCommunity(
326 ): Promise<CommunityResponse> {
327 let form: FollowCommunityForm = {
332 return api.client.followCommunity(form);
335 export async function checkFollowedCommunities(
337 ): Promise<GetFollowedCommunitiesResponse> {
338 let form: GetFollowedCommunitiesForm = {
341 return api.client.getFollowedCommunities(form);
344 export async function likePost(
348 ): Promise<PostResponse> {
349 let form: CreatePostLikeForm = {
355 return api.client.likePost(form);
358 export async function createComment(
362 content = 'a jest test comment'
363 ): Promise<CommentResponse> {
364 let form: CommentForm = {
370 return api.client.createComment(form);
373 export async function updateComment(
376 content = 'A jest test federated comment update'
377 ): Promise<CommentResponse> {
378 let form: CommentForm = {
383 return api.client.editComment(form);
386 export async function deleteComment(
390 ): Promise<CommentResponse> {
391 let form: DeleteCommentForm = {
396 return api.client.deleteComment(form);
399 export async function removeComment(
403 ): Promise<CommentResponse> {
404 let form: RemoveCommentForm = {
409 return api.client.removeComment(form);
412 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
413 let form: GetUserMentionsForm = {
418 return api.client.getUserMentions(form);
421 export async function likeComment(
425 ): Promise<CommentResponse> {
426 let form: CommentLikeForm = {
427 comment_id: comment.id,
431 return api.client.likeComment(form);
434 export async function createCommunity(
436 name_: string = randomString(5)
437 ): Promise<CommunityResponse> {
438 let description = 'a sample description';
439 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
440 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
441 let form: CommunityForm = {
451 return api.client.createCommunity(form);
454 export async function getCommunity(
457 ): Promise<CommunityResponse> {
458 let form: GetCommunityForm = {
461 return api.client.getCommunity(form);
464 export async function deleteCommunity(
468 ): Promise<CommunityResponse> {
469 let form: DeleteCommunityForm = {
474 return api.client.deleteCommunity(form);
477 export async function removeCommunity(
481 ): Promise<CommunityResponse> {
482 let form: RemoveCommunityForm = {
487 return api.client.removeCommunity(form);
490 export async function createPrivateMessage(
493 ): Promise<PrivateMessageResponse> {
494 let content = 'A jest test federated private message';
495 let form: PrivateMessageForm = {
500 return api.client.createPrivateMessage(form);
503 export async function updatePrivateMessage(
506 ): Promise<PrivateMessageResponse> {
507 let updatedContent = 'A jest test federated private message edited';
508 let form: EditPrivateMessageForm = {
509 content: updatedContent,
513 return api.client.editPrivateMessage(form);
516 export async function deletePrivateMessage(
520 ): Promise<PrivateMessageResponse> {
521 let form: DeletePrivateMessageForm = {
526 return api.client.deletePrivateMessage(form);
529 export async function registerUser(
531 username: string = randomString(5)
532 ): Promise<LoginResponse> {
533 let form: RegisterForm = {
536 password_verify: 'test',
540 return api.client.register(form);
543 export async function saveUserSettingsBio(
546 ): Promise<LoginResponse> {
547 let form: UserSettingsForm = {
550 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
551 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
554 send_notifications_to_email: false,
555 bio: 'a changed bio',
558 return saveUserSettings(api, form);
561 export async function saveUserSettings(
563 form: UserSettingsForm
564 ): Promise<LoginResponse> {
565 return api.client.saveUserSettings(form);
568 export async function getSite(
571 ): Promise<GetSiteResponse> {
572 let form: GetSiteForm = {
575 return api.client.getSite(form);
578 export async function listPrivateMessages(
580 ): Promise<PrivateMessagesResponse> {
581 let form: GetPrivateMessagesForm = {
586 return api.client.getPrivateMessages(form);
589 export async function unfollowRemotes(
591 ): Promise<GetFollowedCommunitiesResponse> {
592 // Unfollow all remote communities
593 let followed = await checkFollowedCommunities(api);
594 let remoteFollowed = followed.communities.filter(
595 c => c.community_local == false
597 for (let cu of remoteFollowed) {
598 await followCommunity(api, false, cu.community_id);
600 let followed2 = await checkFollowedCommunities(api);
604 export async function followBeta(api: API): Promise<CommunityResponse> {
606 let search = await searchForBetaCommunity(api);
607 let com = search.communities.filter(c => c.local == false);
609 let follow = await followCommunity(api, true, com[0].id);
614 export function delay(millis: number = 500) {
615 return new Promise((resolve, _reject) => {
616 setTimeout(_ => resolve(), millis);
620 export function longDelay() {
624 export function wrapper(form: any): string {
625 return JSON.stringify(form);
628 function randomString(length: number): string {
630 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
631 var charactersLength = characters.length;
632 for (var i = 0; i < length; i++) {
633 result += characters.charAt(Math.floor(Math.random() * charactersLength));