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(
509 private_message_id: number
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(
523 private_message_id: number
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',
543 return api.client.register(form);
546 export async function saveUserSettingsBio(
549 ): Promise<LoginResponse> {
550 let form: SaveUserSettings = {
553 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
554 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
557 send_notifications_to_email: false,
558 bio: 'a changed bio',
561 return saveUserSettings(api, form);
564 export async function saveUserSettings(
566 form: SaveUserSettings
567 ): Promise<LoginResponse> {
568 return api.client.saveUserSettings(form);
571 export async function getSite(
574 ): Promise<GetSiteResponse> {
575 let form: GetSite = {
578 return api.client.getSite(form);
581 export async function listPrivateMessages(
583 ): Promise<PrivateMessagesResponse> {
584 let form: GetPrivateMessages = {
589 return api.client.getPrivateMessages(form);
592 export async function unfollowRemotes(
594 ): Promise<GetFollowedCommunitiesResponse> {
595 // Unfollow all remote communities
596 let followed = await checkFollowedCommunities(api);
597 let remoteFollowed = followed.communities.filter(
598 c => c.community.local == false
600 for (let cu of remoteFollowed) {
601 await followCommunity(api, false, cu.community.id);
603 let followed2 = await checkFollowedCommunities(api);
607 export async function followBeta(api: API): Promise<CommunityResponse> {
609 let search = await searchForBetaCommunity(api);
610 let com = search.communities.find(c => c.community.local == false);
612 let follow = await followCommunity(api, true, com.community.id);
617 export function delay(millis: number = 500) {
618 return new Promise(resolve => setTimeout(resolve, millis));
621 export function longDelay() {
625 export function wrapper(form: any): string {
626 return JSON.stringify(form);
629 function randomString(length: number): string {
631 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
632 var charactersLength = characters.length;
633 for (var i = 0; i < length; i++) {
634 result += characters.charAt(Math.floor(Math.random() * charactersLength));