15 GetFollowedCommunitiesResponse,
33 GetFollowedCommunities,
37 PrivateMessageResponse,
38 PrivateMessagesResponse,
39 GetUserMentionsResponse,
49 BanFromCommunityResponse,
52 } from 'lemmy-js-client';
54 export interface API {
59 export let alpha: API = {
60 client: new LemmyHttp('http://localhost:8541/api/v2'),
63 export let beta: API = {
64 client: new LemmyHttp('http://localhost:8551/api/v2'),
67 export let gamma: API = {
68 client: new LemmyHttp('http://localhost:8561/api/v2'),
71 export let delta: API = {
72 client: new LemmyHttp('http://localhost:8571/api/v2'),
75 export let epsilon: API = {
76 client: new LemmyHttp('http://localhost:8581/api/v2'),
79 export async function setupLogins() {
80 let formAlpha: Login = {
81 username_or_email: 'lemmy_alpha',
84 let resAlpha = alpha.client.login(formAlpha);
87 username_or_email: 'lemmy_beta',
90 let resBeta = beta.client.login(formBeta);
93 username_or_email: 'lemmy_gamma',
96 let resGamma = gamma.client.login(formGamma);
99 username_or_email: 'lemmy_delta',
102 let resDelta = delta.client.login(formDelta);
105 username_or_email: 'lemmy_epsilon',
108 let resEpsilon = epsilon.client.login(formEpsilon);
110 let res = await Promise.all([
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;
125 export async function createPost(
128 ): Promise<PostResponse> {
129 let name = randomString(5);
130 let body = randomString(10);
131 let url = 'https://google.com/';
132 let form: CreatePost = {
140 return api.client.createPost(form);
143 export async function editPost(api: API, post: Post): Promise<PostResponse> {
144 let name = 'A jest test federated post, updated';
145 let form: EditPost = {
151 return api.client.editPost(form);
154 export async function deletePost(
158 ): Promise<PostResponse> {
159 let form: DeletePost = {
164 return api.client.deletePost(form);
167 export async function removePost(
171 ): Promise<PostResponse> {
172 let form: RemovePost = {
177 return api.client.removePost(form);
180 export async function stickyPost(
184 ): Promise<PostResponse> {
185 let form: StickyPost = {
190 return api.client.stickyPost(form);
193 export async function lockPost(
197 ): Promise<PostResponse> {
198 let form: LockPost = {
203 return api.client.lockPost(form);
206 export async function searchPost(
209 ): Promise<SearchResponse> {
212 type_: SearchType.Posts,
213 sort: SortType.TopAll,
215 return api.client.search(form);
218 export async function searchPostLocal(
221 ): Promise<SearchResponse> {
224 type_: SearchType.Posts,
225 sort: SortType.TopAll,
227 return api.client.search(form);
230 export async function getPost(
233 ): Promise<GetPostResponse> {
234 let form: GetPost = {
237 return api.client.getPost(form);
240 export async function searchComment(
243 ): Promise<SearchResponse> {
246 type_: SearchType.Comments,
247 sort: SortType.TopAll,
249 return api.client.search(form);
252 export async function searchForBetaCommunity(
254 ): Promise<SearchResponse> {
255 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
256 // Use short-hand search url
258 q: '!main@lemmy-beta:8551',
259 type_: SearchType.Communities,
260 sort: SortType.TopAll,
262 return api.client.search(form);
265 export async function searchForCommunity(
268 ): Promise<SearchResponse> {
269 // Use short-hand search url
272 type_: SearchType.Communities,
273 sort: SortType.TopAll,
275 return api.client.search(form);
278 export async function searchForUser(
281 ): Promise<SearchResponse> {
282 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
283 // Use short-hand search url
286 type_: SearchType.Users,
287 sort: SortType.TopAll,
289 return api.client.search(form);
292 export async function banUserFromSite(
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 = {
305 return api.client.banUser(form);
308 export async function banUserFromCommunity(
311 community_id: number,
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 = {
323 return api.client.banFromCommunity(form);
326 export async function followCommunity(
330 ): Promise<CommunityResponse> {
331 let form: FollowCommunity = {
336 return api.client.followCommunity(form);
339 export async function checkFollowedCommunities(
341 ): Promise<GetFollowedCommunitiesResponse> {
342 let form: GetFollowedCommunities = {
345 return api.client.getFollowedCommunities(form);
348 export async function likePost(
352 ): Promise<PostResponse> {
353 let form: CreatePostLike = {
359 return api.client.likePost(form);
362 export async function createComment(
366 content = 'a jest test comment'
367 ): Promise<CommentResponse> {
368 let form: CreateComment = {
374 return api.client.createComment(form);
377 export async function editComment(
380 content = 'A jest test federated comment update'
381 ): Promise<CommentResponse> {
382 let form: EditComment = {
387 return api.client.editComment(form);
390 export async function deleteComment(
394 ): Promise<CommentResponse> {
395 let form: DeleteComment = {
400 return api.client.deleteComment(form);
403 export async function removeComment(
407 ): Promise<CommentResponse> {
408 let form: RemoveComment = {
413 return api.client.removeComment(form);
416 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
417 let form: GetUserMentions = {
422 return api.client.getUserMentions(form);
425 export async function likeComment(
429 ): Promise<CommentResponse> {
430 let form: CreateCommentLike = {
431 comment_id: comment.id,
435 return api.client.likeComment(form);
438 export async function createCommunity(
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 = {
455 return api.client.createCommunity(form);
458 export async function getCommunity(
461 ): Promise<CommunityResponse> {
462 let form: GetCommunity = {
465 return api.client.getCommunity(form);
468 export async function deleteCommunity(
472 ): Promise<CommunityResponse> {
473 let form: DeleteCommunity = {
478 return api.client.deleteCommunity(form);
481 export async function removeCommunity(
485 ): Promise<CommunityResponse> {
486 let form: RemoveCommunity = {
491 return api.client.removeCommunity(form);
494 export async function createPrivateMessage(
497 ): Promise<PrivateMessageResponse> {
498 let content = 'A jest test federated private message';
499 let form: CreatePrivateMessage = {
504 return api.client.createPrivateMessage(form);
507 export async function editPrivateMessage(
510 ): Promise<PrivateMessageResponse> {
511 let updatedContent = 'A jest test federated private message edited';
512 let form: EditPrivateMessage = {
513 content: updatedContent,
517 return api.client.editPrivateMessage(form);
520 export async function deletePrivateMessage(
524 ): Promise<PrivateMessageResponse> {
525 let form: DeletePrivateMessage = {
530 return api.client.deletePrivateMessage(form);
533 export async function registerUser(
535 username: string = randomString(5)
536 ): Promise<LoginResponse> {
537 let form: Register = {
540 password_verify: 'test',
544 return api.client.register(form);
547 export async function saveUserSettingsBio(
550 ): Promise<LoginResponse> {
551 let form: SaveUserSettings = {
554 default_sort_type: SortType.Active,
555 default_listing_type: ListingType.All,
558 send_notifications_to_email: false,
559 bio: 'a changed bio',
562 return saveUserSettings(api, form);
565 export async function saveUserSettings(
567 form: SaveUserSettings
568 ): Promise<LoginResponse> {
569 return api.client.saveUserSettings(form);
572 export async function getSite(
575 ): Promise<GetSiteResponse> {
576 let form: GetSite = {
579 return api.client.getSite(form);
582 export async function listPrivateMessages(
584 ): Promise<PrivateMessagesResponse> {
585 let form: GetPrivateMessages = {
590 return api.client.getPrivateMessages(form);
593 export async function unfollowRemotes(
595 ): Promise<GetFollowedCommunitiesResponse> {
596 // Unfollow all remote communities
597 let followed = await checkFollowedCommunities(api);
598 let remoteFollowed = followed.communities.filter(
599 c => c.community.local == false
601 for (let cu of remoteFollowed) {
602 await followCommunity(api, false, cu.community.id);
604 let followed2 = await checkFollowedCommunities(api);
608 export async function followBeta(api: API): Promise<CommunityResponse> {
610 let search = await searchForBetaCommunity(api);
611 let com = search.communities.find(c => c.community.local == false);
613 let follow = await followCommunity(api, true, com.community.id);
618 export function delay(millis: number = 500) {
619 return new Promise(resolve => setTimeout(resolve, millis));
622 export function longDelay() {
626 export function wrapper(form: any): string {
627 return JSON.stringify(form);
630 function randomString(length: number): string {
632 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
633 var charactersLength = characters.length;
634 for (var i = 0; i < length; i++) {
635 result += characters.charAt(Math.floor(Math.random() * charactersLength));