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});
144 export async function createPost(
147 ): Promise<PostResponse> {
148 let name = randomString(5);
149 let body = randomString(10);
150 let url = 'https://google.com/';
151 let form: CreatePost = {
159 return api.client.createPost(form);
162 export async function editPost(api: API, post: Post): Promise<PostResponse> {
163 let name = 'A jest test federated post, updated';
164 let form: EditPost = {
170 return api.client.editPost(form);
173 export async function deletePost(
177 ): Promise<PostResponse> {
178 let form: DeletePost = {
183 return api.client.deletePost(form);
186 export async function removePost(
190 ): Promise<PostResponse> {
191 let form: RemovePost = {
196 return api.client.removePost(form);
199 export async function stickyPost(
203 ): Promise<PostResponse> {
204 let form: StickyPost = {
209 return api.client.stickyPost(form);
212 export async function lockPost(
216 ): Promise<PostResponse> {
217 let form: LockPost = {
222 return api.client.lockPost(form);
225 export async function resolvePost(
228 ): Promise<ResolveObjectResponse> {
229 let form: ResolveObject = {
232 return api.client.resolveObject(form);
235 export async function searchPostLocal(
238 ): Promise<SearchResponse> {
241 type_: SearchType.Posts,
242 sort: SortType.TopAll,
244 return api.client.search(form);
247 export async function getPost(
250 ): Promise<GetPostResponse> {
251 let form: GetPost = {
254 return api.client.getPost(form);
257 export async function resolveComment(
260 ): Promise<ResolveObjectResponse> {
261 let form: ResolveObject = {
264 return api.client.resolveObject(form);
267 export async function resolveBetaCommunity(
269 ): Promise<ResolveObjectResponse> {
270 // Use short-hand search url
271 let form: ResolveObject = {
272 q: '!main@lemmy-beta:8551',
274 return api.client.resolveObject(form);
277 export async function resolveCommunity(
280 ): Promise<ResolveObjectResponse> {
281 let form: ResolveObject = {
284 return api.client.resolveObject(form);
287 export async function resolvePerson(
290 ): Promise<ResolveObjectResponse> {
291 let form: ResolveObject = {
294 return api.client.resolveObject(form);
297 export async function banPersonFromSite(
301 remove_data: boolean,
302 ): Promise<BanPersonResponse> {
303 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
304 let form: BanPerson = {
310 return api.client.banPerson(form);
313 export async function banPersonFromCommunity(
316 community_id: number,
317 remove_data: boolean,
319 ): Promise<BanFromCommunityResponse> {
320 let form: BanFromCommunity = {
327 return api.client.banFromCommunity(form);
330 export async function followCommunity(
334 ): Promise<CommunityResponse> {
335 let form: FollowCommunity = {
340 return api.client.followCommunity(form);
343 export async function likePost(
347 ): Promise<PostResponse> {
348 let form: CreatePostLike = {
354 return api.client.likePost(form);
357 export async function createComment(
361 content = 'a jest test comment'
362 ): Promise<CommentResponse> {
363 let form: CreateComment = {
369 return api.client.createComment(form);
372 export async function editComment(
375 content = 'A jest test federated comment update'
376 ): Promise<CommentResponse> {
377 let form: EditComment = {
382 return api.client.editComment(form);
385 export async function deleteComment(
389 ): Promise<CommentResponse> {
390 let form: DeleteComment = {
395 return api.client.deleteComment(form);
398 export async function removeComment(
402 ): Promise<CommentResponse> {
403 let form: RemoveComment = {
408 return api.client.removeComment(form);
411 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
412 let form: GetPersonMentions = {
417 return api.client.getPersonMentions(form);
420 export async function likeComment(
424 ): Promise<CommentResponse> {
425 let form: CreateCommentLike = {
426 comment_id: comment.id,
430 return api.client.likeComment(form);
433 export async function createCommunity(
435 name_: string = randomString(5)
436 ): Promise<CommunityResponse> {
437 let description = 'a sample description';
438 let form: CreateCommunity = {
445 return api.client.createCommunity(form);
448 export async function getCommunity(
451 ): Promise<CommunityResponse> {
452 let form: GetCommunity = {
455 return api.client.getCommunity(form);
458 export async function deleteCommunity(
462 ): Promise<CommunityResponse> {
463 let form: DeleteCommunity = {
468 return api.client.deleteCommunity(form);
471 export async function removeCommunity(
475 ): Promise<CommunityResponse> {
476 let form: RemoveCommunity = {
481 return api.client.removeCommunity(form);
484 export async function createPrivateMessage(
487 ): Promise<PrivateMessageResponse> {
488 let content = 'A jest test federated private message';
489 let form: CreatePrivateMessage = {
494 return api.client.createPrivateMessage(form);
497 export async function editPrivateMessage(
499 private_message_id: number
500 ): Promise<PrivateMessageResponse> {
501 let updatedContent = 'A jest test federated private message edited';
502 let form: EditPrivateMessage = {
503 content: updatedContent,
507 return api.client.editPrivateMessage(form);
510 export async function deletePrivateMessage(
513 private_message_id: number
514 ): Promise<PrivateMessageResponse> {
515 let form: DeletePrivateMessage = {
520 return api.client.deletePrivateMessage(form);
523 export async function registerUser(
525 username: string = randomString(5)
526 ): Promise<LoginResponse> {
527 let form: Register = {
530 password_verify: password,
533 return api.client.register(form);
536 export async function saveUserSettingsBio(
538 ): Promise<LoginResponse> {
539 let form: SaveUserSettings = {
542 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
543 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
546 send_notifications_to_email: false,
547 bio: 'a changed bio',
550 return saveUserSettings(api, form);
553 export async function saveUserSettings(
555 form: SaveUserSettings
556 ): Promise<LoginResponse> {
557 return api.client.saveUserSettings(form);
560 export async function deleteUser(
562 ): Promise<DeleteAccountResponse> {
563 let form: DeleteAccount = {
567 return api.client.deleteAccount(form);
570 export async function getSite(
572 ): Promise<GetSiteResponse> {
573 let form: GetSite = {
576 return api.client.getSite(form);
579 export async function listPrivateMessages(
581 ): Promise<PrivateMessagesResponse> {
582 let form: GetPrivateMessages = {
587 return api.client.getPrivateMessages(form);
590 export async function unfollowRemotes(
592 ): Promise<GetSiteResponse> {
593 // Unfollow all remote communities
594 let site = await getSite(api);
595 let remoteFollowed = site.my_user.follows.filter(
596 c => c.community.local == false
598 for (let cu of remoteFollowed) {
599 await followCommunity(api, false, cu.community.id);
601 let siteRes = await getSite(api);
605 export async function followBeta(api: API): Promise<CommunityResponse> {
606 let betaCommunity = (await resolveBetaCommunity(api)).community;
608 let follow = await followCommunity(api, true, betaCommunity.community.id);
613 export async function reportPost(
617 ): Promise<PostReportResponse> {
618 let form: CreatePostReport = {
623 return api.client.createPostReport(form);
626 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
627 let form: ListPostReports = {
630 return api.client.listPostReports(form);
633 export async function reportComment(
637 ): Promise<CommentReportResponse> {
638 let form: CreateCommentReport = {
643 return api.client.createCommentReport(form);
646 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
647 let form: ListCommentReports = {
650 return api.client.listCommentReports(form);
653 export function delay(millis: number = 500) {
654 return new Promise(resolve => setTimeout(resolve, millis));
657 export function longDelay() {
661 export function wrapper(form: any): string {
662 return JSON.stringify(form);
665 export function randomString(length: number): string {
667 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
668 var charactersLength = characters.length;
669 for (var i = 0; i < length; i++) {
670 result += characters.charAt(Math.floor(Math.random() * charactersLength));
675 export async function unfollows() {
676 await unfollowRemotes(alpha);
677 await unfollowRemotes(gamma);
678 await unfollowRemotes(delta);
679 await unfollowRemotes(epsilon);