15 GetFollowedCommunitiesResponse,
33 GetFollowedCommunities,
37 PrivateMessageResponse,
38 PrivateMessagesResponse,
39 GetPersonMentionsResponse,
49 BanFromCommunityResponse,
52 } from 'lemmy-js-client';
54 export interface API {
59 export let alpha: API = {
60 client: new LemmyHttp('http://localhost:8541'),
63 export let beta: API = {
64 client: new LemmyHttp('http://localhost:8551'),
67 export let gamma: API = {
68 client: new LemmyHttp('http://localhost:8561'),
71 export let delta: API = {
72 client: new LemmyHttp('http://localhost:8571'),
75 export let epsilon: API = {
76 client: new LemmyHttp('http://localhost:8581'),
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 banPersonFromSite(
296 ): Promise<BanPersonResponse> {
297 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
298 // Use short-hand search url
299 let form: BanPerson = {
305 return api.client.banPerson(form);
308 export async function banPersonFromCommunity(
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<GetPersonMentionsResponse> {
417 let form: GetPersonMentions = {
422 return api.client.getPersonMentions(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 = {
454 return api.client.createCommunity(form);
457 export async function getCommunity(
460 ): Promise<CommunityResponse> {
461 let form: GetCommunity = {
464 return api.client.getCommunity(form);
467 export async function deleteCommunity(
471 ): Promise<CommunityResponse> {
472 let form: DeleteCommunity = {
477 return api.client.deleteCommunity(form);
480 export async function removeCommunity(
484 ): Promise<CommunityResponse> {
485 let form: RemoveCommunity = {
490 return api.client.removeCommunity(form);
493 export async function createPrivateMessage(
496 ): Promise<PrivateMessageResponse> {
497 let content = 'A jest test federated private message';
498 let form: CreatePrivateMessage = {
503 return api.client.createPrivateMessage(form);
506 export async function editPrivateMessage(
508 private_message_id: number
509 ): Promise<PrivateMessageResponse> {
510 let updatedContent = 'A jest test federated private message edited';
511 let form: EditPrivateMessage = {
512 content: updatedContent,
516 return api.client.editPrivateMessage(form);
519 export async function deletePrivateMessage(
522 private_message_id: number
523 ): Promise<PrivateMessageResponse> {
524 let form: DeletePrivateMessage = {
529 return api.client.deletePrivateMessage(form);
532 export async function registerUser(
534 username: string = randomString(5)
535 ): Promise<LoginResponse> {
536 let form: Register = {
539 password_verify: 'test',
542 return api.client.register(form);
545 export async function saveUserSettingsBio(
548 ): Promise<LoginResponse> {
549 let form: SaveUserSettings = {
552 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
553 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
556 send_notifications_to_email: false,
557 bio: 'a changed bio',
560 return saveUserSettings(api, form);
563 export async function saveUserSettings(
565 form: SaveUserSettings
566 ): Promise<LoginResponse> {
567 return api.client.saveUserSettings(form);
570 export async function getSite(
573 ): Promise<GetSiteResponse> {
574 let form: GetSite = {
577 return api.client.getSite(form);
580 export async function listPrivateMessages(
582 ): Promise<PrivateMessagesResponse> {
583 let form: GetPrivateMessages = {
588 return api.client.getPrivateMessages(form);
591 export async function unfollowRemotes(
593 ): Promise<GetFollowedCommunitiesResponse> {
594 // Unfollow all remote communities
595 let followed = await checkFollowedCommunities(api);
596 let remoteFollowed = followed.communities.filter(
597 c => c.community.local == false
599 for (let cu of remoteFollowed) {
600 await followCommunity(api, false, cu.community.id);
602 let followed2 = await checkFollowedCommunities(api);
606 export async function followBeta(api: API): Promise<CommunityResponse> {
608 let search = await searchForBetaCommunity(api);
609 let com = search.communities.find(c => c.community.local == false);
611 let follow = await followCommunity(api, true, com.community.id);
616 export function delay(millis: number = 500) {
617 return new Promise(resolve => 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));