35 PrivateMessageResponse,
36 PrivateMessagesResponse,
37 GetPersonMentionsResponse,
47 BanFromCommunityResponse,
50 ResolveObjectResponse,
55 ListPostReportsResponse,
57 CommentReportResponse,
59 ListCommentReportsResponse,
62 } from 'lemmy-js-client';
64 export interface API {
69 export let alpha: API = {
70 client: new LemmyHttp('http://127.0.0.1:8541'),
73 export let beta: API = {
74 client: new LemmyHttp('http://127.0.0.1:8551'),
77 export let gamma: API = {
78 client: new LemmyHttp('http://127.0.0.1:8561'),
81 export let delta: API = {
82 client: new LemmyHttp('http://127.0.0.1:8571'),
85 export let epsilon: API = {
86 client: new LemmyHttp('http://127.0.0.1:8581'),
89 const password = 'lemmylemmy'
91 export async function setupLogins() {
92 let formAlpha: Login = {
93 username_or_email: 'lemmy_alpha',
96 let resAlpha = alpha.client.login(formAlpha);
99 username_or_email: 'lemmy_beta',
102 let resBeta = beta.client.login(formBeta);
105 username_or_email: 'lemmy_gamma',
108 let resGamma = gamma.client.login(formGamma);
111 username_or_email: 'lemmy_delta',
114 let resDelta = delta.client.login(formDelta);
117 username_or_email: 'lemmy_epsilon',
120 let resEpsilon = epsilon.client.login(formEpsilon);
122 let res = await Promise.all([
130 alpha.auth = res[0].jwt;
131 beta.auth = res[1].jwt;
132 gamma.auth = res[2].jwt;
133 delta.auth = res[3].jwt;
134 epsilon.auth = res[4].jwt;
136 // regstration applications are now enabled by default, need to disable them
137 await alpha.client.editSite({ require_application: false, auth: alpha.auth});
138 await beta.client.editSite({ require_application: false, auth: beta.auth});
139 await gamma.client.editSite({ require_application: false, auth: gamma.auth});
140 await delta.client.editSite({ require_application: false, auth: delta.auth});
141 await epsilon.client.editSite({ require_application: false, auth: epsilon.auth});
143 // Create the main beta community, follow it
144 await createCommunity(beta, "main");
145 await followBeta(beta);
148 export async function createPost(
151 ): Promise<PostResponse> {
152 let name = randomString(5);
153 let body = randomString(10);
154 let url = 'https://google.com/';
155 let form: CreatePost = {
163 return api.client.createPost(form);
166 export async function editPost(api: API, post: Post): Promise<PostResponse> {
167 let name = 'A jest test federated post, updated';
168 let form: EditPost = {
174 return api.client.editPost(form);
177 export async function deletePost(
181 ): Promise<PostResponse> {
182 let form: DeletePost = {
187 return api.client.deletePost(form);
190 export async function removePost(
194 ): Promise<PostResponse> {
195 let form: RemovePost = {
200 return api.client.removePost(form);
203 export async function stickyPost(
207 ): Promise<PostResponse> {
208 let form: StickyPost = {
213 return api.client.stickyPost(form);
216 export async function lockPost(
220 ): Promise<PostResponse> {
221 let form: LockPost = {
226 return api.client.lockPost(form);
229 export async function resolvePost(
232 ): Promise<ResolveObjectResponse> {
233 let form: ResolveObject = {
236 return api.client.resolveObject(form);
239 export async function searchPostLocal(
242 ): Promise<SearchResponse> {
245 type_: SearchType.Posts,
246 sort: SortType.TopAll,
248 return api.client.search(form);
251 export async function getPost(
254 ): Promise<GetPostResponse> {
255 let form: GetPost = {
258 return api.client.getPost(form);
261 export async function resolveComment(
264 ): Promise<ResolveObjectResponse> {
265 let form: ResolveObject = {
268 return api.client.resolveObject(form);
271 export async function resolveBetaCommunity(
273 ): Promise<ResolveObjectResponse> {
274 // Use short-hand search url
275 let form: ResolveObject = {
276 q: '!main@lemmy-beta:8551',
278 return api.client.resolveObject(form);
281 export async function resolveCommunity(
284 ): Promise<ResolveObjectResponse> {
285 let form: ResolveObject = {
288 return api.client.resolveObject(form);
291 export async function resolvePerson(
294 ): Promise<ResolveObjectResponse> {
295 let form: ResolveObject = {
298 return api.client.resolveObject(form);
301 export async function banPersonFromSite(
306 ): Promise<BanPersonResponse> {
307 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
308 let form: BanPerson = {
314 return api.client.banPerson(form);
317 export async function banPersonFromCommunity(
320 community_id: number,
321 remove_data: boolean,
323 ): Promise<BanFromCommunityResponse> {
324 let form: BanFromCommunity = {
331 return api.client.banFromCommunity(form);
334 export async function followCommunity(
338 ): Promise<CommunityResponse> {
339 let form: FollowCommunity = {
344 return api.client.followCommunity(form);
347 export async function likePost(
351 ): Promise<PostResponse> {
352 let form: CreatePostLike = {
358 return api.client.likePost(form);
361 export async function createComment(
365 content = 'a jest test comment'
366 ): Promise<CommentResponse> {
367 let form: CreateComment = {
373 return api.client.createComment(form);
376 export async function editComment(
379 content = 'A jest test federated comment update'
380 ): Promise<CommentResponse> {
381 let form: EditComment = {
386 return api.client.editComment(form);
389 export async function deleteComment(
393 ): Promise<CommentResponse> {
394 let form: DeleteComment = {
399 return api.client.deleteComment(form);
402 export async function removeComment(
406 ): Promise<CommentResponse> {
407 let form: RemoveComment = {
412 return api.client.removeComment(form);
415 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
416 let form: GetPersonMentions = {
421 return api.client.getPersonMentions(form);
424 export async function likeComment(
428 ): Promise<CommentResponse> {
429 let form: CreateCommentLike = {
430 comment_id: comment.id,
434 return api.client.likeComment(form);
437 export async function createCommunity(
439 name_: string = randomString(5)
440 ): Promise<CommunityResponse> {
441 let description = 'a sample description';
442 let form: CreateCommunity = {
449 return api.client.createCommunity(form);
452 export async function getCommunity(
455 ): Promise<CommunityResponse> {
456 let form: GetCommunity = {
459 return api.client.getCommunity(form);
462 export async function deleteCommunity(
466 ): Promise<CommunityResponse> {
467 let form: DeleteCommunity = {
472 return api.client.deleteCommunity(form);
475 export async function removeCommunity(
479 ): Promise<CommunityResponse> {
480 let form: RemoveCommunity = {
485 return api.client.removeCommunity(form);
488 export async function createPrivateMessage(
491 ): Promise<PrivateMessageResponse> {
492 let content = 'A jest test federated private message';
493 let form: CreatePrivateMessage = {
498 return api.client.createPrivateMessage(form);
501 export async function editPrivateMessage(
503 private_message_id: number
504 ): Promise<PrivateMessageResponse> {
505 let updatedContent = 'A jest test federated private message edited';
506 let form: EditPrivateMessage = {
507 content: updatedContent,
511 return api.client.editPrivateMessage(form);
514 export async function deletePrivateMessage(
517 private_message_id: number
518 ): Promise<PrivateMessageResponse> {
519 let form: DeletePrivateMessage = {
524 return api.client.deletePrivateMessage(form);
527 export async function registerUser(
529 username: string = randomString(5)
530 ): Promise<LoginResponse> {
531 let form: Register = {
534 password_verify: password,
537 return api.client.register(form);
540 export async function saveUserSettingsBio(
542 ): Promise<LoginResponse> {
543 let form: SaveUserSettings = {
546 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
547 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
550 send_notifications_to_email: false,
551 bio: 'a changed bio',
554 return saveUserSettings(api, form);
557 export async function saveUserSettings(
559 form: SaveUserSettings
560 ): Promise<LoginResponse> {
561 return api.client.saveUserSettings(form);
564 export async function deleteUser(
566 ): Promise<DeleteAccountResponse> {
567 let form: DeleteAccount = {
571 return api.client.deleteAccount(form);
574 export async function getSite(
576 ): Promise<GetSiteResponse> {
577 let form: GetSite = {
580 return api.client.getSite(form);
583 export async function listPrivateMessages(
585 ): Promise<PrivateMessagesResponse> {
586 let form: GetPrivateMessages = {
590 return api.client.getPrivateMessages(form);
593 export async function unfollowRemotes(
595 ): Promise<GetSiteResponse> {
596 // Unfollow all remote communities
597 let site = await getSite(api);
598 let remoteFollowed = site.my_user.follows.filter(
599 c => c.community.local == false
601 for (let cu of remoteFollowed) {
602 await followCommunity(api, false, cu.community.id);
604 let siteRes = await getSite(api);
608 export async function followBeta(api: API): Promise<CommunityResponse> {
609 let betaCommunity = (await resolveBetaCommunity(api)).community;
611 let follow = await followCommunity(api, true, betaCommunity.community.id);
616 export async function reportPost(
620 ): Promise<PostReportResponse> {
621 let form: CreatePostReport = {
626 return api.client.createPostReport(form);
629 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
630 let form: ListPostReports = {
633 return api.client.listPostReports(form);
636 export async function reportComment(
640 ): Promise<CommentReportResponse> {
641 let form: CreateCommentReport = {
646 return api.client.createCommentReport(form);
649 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
650 let form: ListCommentReports = {
653 return api.client.listCommentReports(form);
656 export function delay(millis: number = 500) {
657 return new Promise(resolve => setTimeout(resolve, millis));
660 export function longDelay() {
664 export function wrapper(form: any): string {
665 return JSON.stringify(form);
668 export function randomString(length: number): string {
670 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
671 var charactersLength = characters.length;
672 for (var i = 0; i < length; i++) {
673 result += characters.charAt(Math.floor(Math.random() * charactersLength));
678 export async function unfollows() {
679 await unfollowRemotes(alpha);
680 await unfollowRemotes(gamma);
681 await unfollowRemotes(delta);
682 await unfollowRemotes(epsilon);