35 PrivateMessageResponse,
36 PrivateMessagesResponse,
37 GetPersonMentionsResponse,
47 BanFromCommunityResponse,
50 ResolveObjectResponse,
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 const password = 'lemmylemmy'
81 export async function setupLogins() {
82 let formAlpha: Login = {
83 username_or_email: 'lemmy_alpha',
86 let resAlpha = alpha.client.login(formAlpha);
89 username_or_email: 'lemmy_beta',
92 let resBeta = beta.client.login(formBeta);
95 username_or_email: 'lemmy_gamma',
98 let resGamma = gamma.client.login(formGamma);
101 username_or_email: 'lemmy_delta',
104 let resDelta = delta.client.login(formDelta);
107 username_or_email: 'lemmy_epsilon',
110 let resEpsilon = epsilon.client.login(formEpsilon);
112 let res = await Promise.all([
120 alpha.auth = res[0].jwt;
121 beta.auth = res[1].jwt;
122 gamma.auth = res[2].jwt;
123 delta.auth = res[3].jwt;
124 epsilon.auth = res[4].jwt;
127 export async function createPost(
130 ): Promise<PostResponse> {
131 let name = randomString(5);
132 let body = randomString(10);
133 let url = 'https://google.com/';
134 let form: CreatePost = {
142 return api.client.createPost(form);
145 export async function editPost(api: API, post: Post): Promise<PostResponse> {
146 let name = 'A jest test federated post, updated';
147 let form: EditPost = {
153 return api.client.editPost(form);
156 export async function deletePost(
160 ): Promise<PostResponse> {
161 let form: DeletePost = {
166 return api.client.deletePost(form);
169 export async function removePost(
173 ): Promise<PostResponse> {
174 let form: RemovePost = {
179 return api.client.removePost(form);
182 export async function stickyPost(
186 ): Promise<PostResponse> {
187 let form: StickyPost = {
192 return api.client.stickyPost(form);
195 export async function lockPost(
199 ): Promise<PostResponse> {
200 let form: LockPost = {
205 return api.client.lockPost(form);
208 export async function resolvePost(
211 ): Promise<ResolveObjectResponse> {
212 let form: ResolveObject = {
215 return api.client.resolveObject(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 resolveComment(
243 ): Promise<ResolveObjectResponse> {
244 let form: ResolveObject = {
247 return api.client.resolveObject(form);
250 export async function resolveBetaCommunity(
252 ): Promise<ResolveObjectResponse> {
253 // Use short-hand search url
254 let form: ResolveObject = {
255 q: '!main@lemmy-beta:8551',
257 return api.client.resolveObject(form);
260 export async function resolveCommunity(
263 ): Promise<ResolveObjectResponse> {
264 let form: ResolveObject = {
267 return api.client.resolveObject(form);
270 export async function resolvePerson(
273 ): Promise<ResolveObjectResponse> {
274 let form: ResolveObject = {
277 return api.client.resolveObject(form);
280 export async function banPersonFromSite(
284 ): Promise<BanPersonResponse> {
285 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
286 let form: BanPerson = {
292 return api.client.banPerson(form);
295 export async function banPersonFromCommunity(
298 community_id: number,
300 ): Promise<BanFromCommunityResponse> {
301 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
302 let form: BanFromCommunity = {
309 return api.client.banFromCommunity(form);
312 export async function followCommunity(
316 ): Promise<CommunityResponse> {
317 let form: FollowCommunity = {
322 return api.client.followCommunity(form);
325 export async function likePost(
329 ): Promise<PostResponse> {
330 let form: CreatePostLike = {
336 return api.client.likePost(form);
339 export async function createComment(
343 content = 'a jest test comment'
344 ): Promise<CommentResponse> {
345 let form: CreateComment = {
351 return api.client.createComment(form);
354 export async function editComment(
357 content = 'A jest test federated comment update'
358 ): Promise<CommentResponse> {
359 let form: EditComment = {
364 return api.client.editComment(form);
367 export async function deleteComment(
371 ): Promise<CommentResponse> {
372 let form: DeleteComment = {
377 return api.client.deleteComment(form);
380 export async function removeComment(
384 ): Promise<CommentResponse> {
385 let form: RemoveComment = {
390 return api.client.removeComment(form);
393 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
394 let form: GetPersonMentions = {
399 return api.client.getPersonMentions(form);
402 export async function likeComment(
406 ): Promise<CommentResponse> {
407 let form: CreateCommentLike = {
408 comment_id: comment.id,
412 return api.client.likeComment(form);
415 export async function createCommunity(
417 name_: string = randomString(5)
418 ): Promise<CommunityResponse> {
419 let description = 'a sample description';
420 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
421 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
422 let form: CreateCommunity = {
431 return api.client.createCommunity(form);
434 export async function getCommunity(
437 ): Promise<CommunityResponse> {
438 let form: GetCommunity = {
441 return api.client.getCommunity(form);
444 export async function deleteCommunity(
448 ): Promise<CommunityResponse> {
449 let form: DeleteCommunity = {
454 return api.client.deleteCommunity(form);
457 export async function removeCommunity(
461 ): Promise<CommunityResponse> {
462 let form: RemoveCommunity = {
467 return api.client.removeCommunity(form);
470 export async function createPrivateMessage(
473 ): Promise<PrivateMessageResponse> {
474 let content = 'A jest test federated private message';
475 let form: CreatePrivateMessage = {
480 return api.client.createPrivateMessage(form);
483 export async function editPrivateMessage(
485 private_message_id: number
486 ): Promise<PrivateMessageResponse> {
487 let updatedContent = 'A jest test federated private message edited';
488 let form: EditPrivateMessage = {
489 content: updatedContent,
493 return api.client.editPrivateMessage(form);
496 export async function deletePrivateMessage(
499 private_message_id: number
500 ): Promise<PrivateMessageResponse> {
501 let form: DeletePrivateMessage = {
506 return api.client.deletePrivateMessage(form);
509 export async function registerUser(
511 username: string = randomString(5)
512 ): Promise<LoginResponse> {
513 let form: Register = {
516 password_verify: password,
519 return api.client.register(form);
522 export async function saveUserSettingsBio(
524 ): Promise<LoginResponse> {
525 let form: SaveUserSettings = {
528 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
529 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
532 send_notifications_to_email: false,
533 bio: 'a changed bio',
536 return saveUserSettings(api, form);
539 export async function saveUserSettings(
541 form: SaveUserSettings
542 ): Promise<LoginResponse> {
543 return api.client.saveUserSettings(form);
546 export async function getSite(
548 ): Promise<GetSiteResponse> {
549 let form: GetSite = {
552 return api.client.getSite(form);
555 export async function listPrivateMessages(
557 ): Promise<PrivateMessagesResponse> {
558 let form: GetPrivateMessages = {
563 return api.client.getPrivateMessages(form);
566 export async function unfollowRemotes(
568 ): Promise<GetSiteResponse> {
569 // Unfollow all remote communities
570 let site = await getSite(api);
571 let remoteFollowed = site.my_user.follows.filter(
572 c => c.community.local == false
574 for (let cu of remoteFollowed) {
575 await followCommunity(api, false, cu.community.id);
577 let siteRes = await getSite(api);
581 export async function followBeta(api: API): Promise<CommunityResponse> {
582 let betaCommunity = (await resolveBetaCommunity(api)).community;
584 let follow = await followCommunity(api, true, betaCommunity.community.id);
589 export function delay(millis: number = 500) {
590 return new Promise(resolve => setTimeout(resolve, millis));
593 export function longDelay() {
597 export function wrapper(form: any): string {
598 return JSON.stringify(form);
601 function randomString(length: number): string {
603 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
604 var charactersLength = characters.length;
605 for (var i = 0; i < length; i++) {
606 result += characters.charAt(Math.floor(Math.random() * charactersLength));