34 PrivateMessageResponse,
35 PrivateMessagesResponse,
36 GetPersonMentionsResponse,
46 BanFromCommunityResponse,
49 ResolveObjectResponse,
54 ListPostReportsResponse,
56 CommentReportResponse,
58 ListCommentReportsResponse,
60 DeleteAccountResponse,
68 } from "lemmy-js-client";
70 export interface API {
75 export let alpha: API = {
76 client: new LemmyHttp("http://127.0.0.1:8541"),
80 export let beta: API = {
81 client: new LemmyHttp("http://127.0.0.1:8551"),
85 export let gamma: API = {
86 client: new LemmyHttp("http://127.0.0.1:8561"),
90 export let delta: API = {
91 client: new LemmyHttp("http://127.0.0.1:8571"),
95 export let epsilon: API = {
96 client: new LemmyHttp("http://127.0.0.1:8581"),
100 const password = "lemmylemmy";
102 export async function setupLogins() {
103 let formAlpha: Login = {
104 username_or_email: "lemmy_alpha",
107 let resAlpha = alpha.client.login(formAlpha);
109 let formBeta: Login = {
110 username_or_email: "lemmy_beta",
113 let resBeta = beta.client.login(formBeta);
115 let formGamma: Login = {
116 username_or_email: "lemmy_gamma",
119 let resGamma = gamma.client.login(formGamma);
121 let formDelta: Login = {
122 username_or_email: "lemmy_delta",
125 let resDelta = delta.client.login(formDelta);
127 let formEpsilon: Login = {
128 username_or_email: "lemmy_epsilon",
131 let resEpsilon = epsilon.client.login(formEpsilon);
133 let res = await Promise.all([
141 alpha.auth = res[0].jwt ?? "";
142 beta.auth = res[1].jwt ?? "";
143 gamma.auth = res[2].jwt ?? "";
144 delta.auth = res[3].jwt ?? "";
145 epsilon.auth = res[4].jwt ?? "";
147 // Registration applications are now enabled by default, need to disable them
148 let editSiteForm: EditSite = {
149 registration_mode: RegistrationMode.Open,
150 rate_limit_message: 999,
151 rate_limit_post: 999,
152 rate_limit_register: 999,
153 rate_limit_image: 999,
154 rate_limit_comment: 999,
155 rate_limit_search: 999,
159 // Set the blocks and auths for each
160 editSiteForm.auth = alpha.auth;
161 editSiteForm.allowed_instances = [
167 await alpha.client.editSite(editSiteForm);
169 editSiteForm.auth = beta.auth;
170 editSiteForm.allowed_instances = [
176 await beta.client.editSite(editSiteForm);
178 editSiteForm.auth = gamma.auth;
179 editSiteForm.allowed_instances = [
185 await gamma.client.editSite(editSiteForm);
187 editSiteForm.allowed_instances = ["lemmy-beta"];
188 editSiteForm.auth = delta.auth;
189 await delta.client.editSite(editSiteForm);
191 editSiteForm.auth = epsilon.auth;
192 editSiteForm.allowed_instances = [];
193 editSiteForm.blocked_instances = ["lemmy-alpha"];
194 await epsilon.client.editSite(editSiteForm);
196 // Create the main alpha/beta communities
197 await createCommunity(alpha, "main");
198 await createCommunity(beta, "main");
201 export async function createPost(
204 ): Promise<PostResponse> {
205 let name = randomString(5);
206 let body = randomString(10);
207 let url = "https://google.com/";
208 let form: CreatePost = {
215 return api.client.createPost(form);
218 export async function editPost(api: API, post: Post): Promise<PostResponse> {
219 let name = "A jest test federated post, updated";
220 let form: EditPost = {
225 return api.client.editPost(form);
228 export async function deletePost(
232 ): Promise<PostResponse> {
233 let form: DeletePost = {
238 return api.client.deletePost(form);
241 export async function removePost(
245 ): Promise<PostResponse> {
246 let form: RemovePost = {
251 return api.client.removePost(form);
254 export async function featurePost(
258 ): Promise<PostResponse> {
259 let form: FeaturePost = {
262 feature_type: PostFeatureType.Community,
265 return api.client.featurePost(form);
268 export async function lockPost(
272 ): Promise<PostResponse> {
273 let form: LockPost = {
278 return api.client.lockPost(form);
281 export async function resolvePost(
284 ): Promise<ResolveObjectResponse> {
285 let form: ResolveObject = {
289 return api.client.resolveObject(form);
292 export async function searchPostLocal(
295 ): Promise<SearchResponse> {
298 type_: SearchType.Posts,
299 sort: SortType.TopAll,
302 return api.client.search(form);
305 export async function getPost(
308 ): Promise<GetPostResponse> {
309 let form: GetPost = {
313 return api.client.getPost(form);
316 export async function getComments(
319 ): Promise<GetCommentsResponse> {
320 let form: GetComments = {
322 type_: ListingType.All,
323 sort: CommentSortType.New,
326 return api.client.getComments(form);
329 export async function resolveComment(
332 ): Promise<ResolveObjectResponse> {
333 let form: ResolveObject = {
337 return api.client.resolveObject(form);
340 export async function resolveBetaCommunity(
342 ): Promise<ResolveObjectResponse> {
343 // Use short-hand search url
344 let form: ResolveObject = {
345 q: "!main@lemmy-beta:8551",
348 return api.client.resolveObject(form);
351 export async function resolveCommunity(
354 ): Promise<ResolveObjectResponse> {
355 let form: ResolveObject = {
359 return api.client.resolveObject(form);
362 export async function resolvePerson(
365 ): Promise<ResolveObjectResponse> {
366 let form: ResolveObject = {
370 return api.client.resolveObject(form);
373 export async function banPersonFromSite(
378 ): Promise<BanPersonResponse> {
379 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
380 let form: BanPerson = {
383 remove_data: remove_data,
386 return api.client.banPerson(form);
389 export async function banPersonFromCommunity(
392 community_id: number,
393 remove_data: boolean,
395 ): Promise<BanFromCommunityResponse> {
396 let form: BanFromCommunity = {
399 remove_data: remove_data,
403 return api.client.banFromCommunity(form);
406 export async function followCommunity(
410 ): Promise<CommunityResponse> {
411 let form: FollowCommunity = {
416 return api.client.followCommunity(form);
419 export async function likePost(
423 ): Promise<PostResponse> {
424 let form: CreatePostLike = {
430 return api.client.likePost(form);
433 export async function createComment(
437 content = "a jest test comment"
438 ): Promise<CommentResponse> {
439 let form: CreateComment = {
445 return api.client.createComment(form);
448 export async function editComment(
451 content = "A jest test federated comment update"
452 ): Promise<CommentResponse> {
453 let form: EditComment = {
458 return api.client.editComment(form);
461 export async function deleteComment(
465 ): Promise<CommentResponse> {
466 let form: DeleteComment = {
471 return api.client.deleteComment(form);
474 export async function removeComment(
478 ): Promise<CommentResponse> {
479 let form: RemoveComment = {
484 return api.client.removeComment(form);
487 export async function getMentions(
489 ): Promise<GetPersonMentionsResponse> {
490 let form: GetPersonMentions = {
491 sort: CommentSortType.New,
495 return api.client.getPersonMentions(form);
498 export async function likeComment(
502 ): Promise<CommentResponse> {
503 let form: CreateCommentLike = {
504 comment_id: comment.id,
508 return api.client.likeComment(form);
511 export async function createCommunity(
513 name_: string = randomString(5)
514 ): Promise<CommunityResponse> {
515 let description = "a sample description";
516 let form: CreateCommunity = {
522 return api.client.createCommunity(form);
525 export async function getCommunity(
528 ): Promise<CommunityResponse> {
529 let form: GetCommunity = {
533 return api.client.getCommunity(form);
536 export async function deleteCommunity(
540 ): Promise<CommunityResponse> {
541 let form: DeleteCommunity = {
546 return api.client.deleteCommunity(form);
549 export async function removeCommunity(
553 ): Promise<CommunityResponse> {
554 let form: RemoveCommunity = {
559 return api.client.removeCommunity(form);
562 export async function createPrivateMessage(
565 ): Promise<PrivateMessageResponse> {
566 let content = "A jest test federated private message";
567 let form: CreatePrivateMessage = {
572 return api.client.createPrivateMessage(form);
575 export async function editPrivateMessage(
577 private_message_id: number
578 ): Promise<PrivateMessageResponse> {
579 let updatedContent = "A jest test federated private message edited";
580 let form: EditPrivateMessage = {
581 content: updatedContent,
585 return api.client.editPrivateMessage(form);
588 export async function deletePrivateMessage(
591 private_message_id: number
592 ): Promise<PrivateMessageResponse> {
593 let form: DeletePrivateMessage = {
598 return api.client.deletePrivateMessage(form);
601 export async function registerUser(
603 username: string = randomString(5)
604 ): Promise<LoginResponse> {
605 let form: Register = {
608 password_verify: password,
611 return api.client.register(form);
614 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
615 let form: SaveUserSettings = {
618 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
619 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
620 interface_language: "en",
622 send_notifications_to_email: false,
623 bio: "a changed bio",
626 return saveUserSettings(api, form);
629 export async function saveUserSettingsFederated(
631 ): Promise<LoginResponse> {
632 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
633 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
634 let bio = "a changed bio";
635 let form: SaveUserSettings = {
637 default_sort_type: Object.keys(SortType).indexOf(SortType.Hot),
638 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
639 interface_language: "",
642 display_name: "user321",
644 send_notifications_to_email: false,
648 return await saveUserSettings(alpha, form);
651 export async function saveUserSettings(
653 form: SaveUserSettings
654 ): Promise<LoginResponse> {
655 return api.client.saveUserSettings(form);
658 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
659 let form: DeleteAccount = {
663 return api.client.deleteAccount(form);
666 export async function getSite(api: API): Promise<GetSiteResponse> {
667 let form: GetSite = {
670 return api.client.getSite(form);
673 export async function listPrivateMessages(
675 ): Promise<PrivateMessagesResponse> {
676 let form: GetPrivateMessages = {
680 return api.client.getPrivateMessages(form);
683 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
684 // Unfollow all remote communities
685 let site = await getSite(api);
687 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
688 for (let cu of remoteFollowed) {
689 await followCommunity(api, false, cu.community.id);
691 let siteRes = await getSite(api);
695 export async function followBeta(api: API): Promise<CommunityResponse> {
696 let betaCommunity = (await resolveBetaCommunity(api)).community;
698 let follow = await followCommunity(api, true, betaCommunity.community.id);
701 return Promise.reject("no community worked");
705 export async function reportPost(
709 ): Promise<PostReportResponse> {
710 let form: CreatePostReport = {
715 return api.client.createPostReport(form);
718 export async function listPostReports(
720 ): Promise<ListPostReportsResponse> {
721 let form: ListPostReports = {
724 return api.client.listPostReports(form);
727 export async function reportComment(
731 ): Promise<CommentReportResponse> {
732 let form: CreateCommentReport = {
737 return api.client.createCommentReport(form);
740 export async function listCommentReports(
742 ): Promise<ListCommentReportsResponse> {
743 let form: ListCommentReports = {
746 return api.client.listCommentReports(form);
749 export function delay(millis = 500) {
750 return new Promise(resolve => setTimeout(resolve, millis));
753 export function longDelay() {
757 export function wrapper(form: any): string {
758 return JSON.stringify(form);
761 export function randomString(length: number): string {
764 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
765 var charactersLength = characters.length;
766 for (var i = 0; i < length; i++) {
767 result += characters.charAt(Math.floor(Math.random() * charactersLength));
772 export async function unfollows() {
773 await unfollowRemotes(alpha);
774 await unfollowRemotes(gamma);
775 await unfollowRemotes(delta);
776 await unfollowRemotes(epsilon);
779 export function getCommentParentId(comment: Comment): number | undefined {
780 let split = comment.path.split(".");
784 if (split.length > 1) {
785 return Number(split[split.length - 2]);