1 import { LemmyHttp } from "lemmy-js-client";
2 import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
3 import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
4 import { EditPost } from "lemmy-js-client/dist/types/EditPost";
5 import { EditSite } from "lemmy-js-client/dist/types/EditSite";
6 import { FeaturePost } from "lemmy-js-client/dist/types/FeaturePost";
7 import { GetComments } from "lemmy-js-client/dist/types/GetComments";
8 import { GetCommentsResponse } from "lemmy-js-client/dist/types/GetCommentsResponse";
9 import { GetPost } from "lemmy-js-client/dist/types/GetPost";
10 import { GetPostResponse } from "lemmy-js-client/dist/types/GetPostResponse";
11 import { LockPost } from "lemmy-js-client/dist/types/LockPost";
12 import { Login } from "lemmy-js-client/dist/types/Login";
13 import { Post } from "lemmy-js-client/dist/types/Post";
14 import { PostResponse } from "lemmy-js-client/dist/types/PostResponse";
15 import { RemovePost } from "lemmy-js-client/dist/types/RemovePost";
16 import { ResolveObject } from "lemmy-js-client/dist/types/ResolveObject";
17 import { ResolveObjectResponse } from "lemmy-js-client/dist/types/ResolveObjectResponse";
18 import { Search } from "lemmy-js-client/dist/types/Search";
19 import { SearchResponse } from "lemmy-js-client/dist/types/SearchResponse";
20 import { Comment } from "lemmy-js-client/dist/types/Comment";
21 import { BanPersonResponse } from "lemmy-js-client/dist/types/BanPersonResponse";
22 import { BanPerson } from "lemmy-js-client/dist/types/BanPerson";
23 import { BanFromCommunityResponse } from "lemmy-js-client/dist/types/BanFromCommunityResponse";
24 import { BanFromCommunity } from "lemmy-js-client/dist/types/BanFromCommunity";
25 import { CommunityResponse } from "lemmy-js-client/dist/types/CommunityResponse";
26 import { FollowCommunity } from "lemmy-js-client/dist/types/FollowCommunity";
27 import { CreatePostLike } from "lemmy-js-client/dist/types/CreatePostLike";
28 import { CommentResponse } from "lemmy-js-client/dist/types/CommentResponse";
29 import { CreateComment } from "lemmy-js-client/dist/types/CreateComment";
30 import { EditComment } from "lemmy-js-client/dist/types/EditComment";
31 import { DeleteComment } from "lemmy-js-client/dist/types/DeleteComment";
32 import { RemoveComment } from "lemmy-js-client/dist/types/RemoveComment";
33 import { GetPersonMentionsResponse } from "lemmy-js-client/dist/types/GetPersonMentionsResponse";
34 import { GetPersonMentions } from "lemmy-js-client/dist/types/GetPersonMentions";
35 import { CreateCommentLike } from "lemmy-js-client/dist/types/CreateCommentLike";
36 import { CreateCommunity } from "lemmy-js-client/dist/types/CreateCommunity";
37 import { GetCommunity } from "lemmy-js-client/dist/types/GetCommunity";
38 import { DeleteCommunity } from "lemmy-js-client/dist/types/DeleteCommunity";
39 import { RemoveCommunity } from "lemmy-js-client/dist/types/RemoveCommunity";
40 import { PrivateMessageResponse } from "lemmy-js-client/dist/types/PrivateMessageResponse";
41 import { CreatePrivateMessage } from "lemmy-js-client/dist/types/CreatePrivateMessage";
42 import { EditPrivateMessage } from "lemmy-js-client/dist/types/EditPrivateMessage";
43 import { DeletePrivateMessage } from "lemmy-js-client/dist/types/DeletePrivateMessage";
44 import { LoginResponse } from "lemmy-js-client/dist/types/LoginResponse";
45 import { Register } from "lemmy-js-client/dist/types/Register";
46 import { SaveUserSettings } from "lemmy-js-client/dist/types/SaveUserSettings";
47 import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount";
48 import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse";
49 import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse";
50 import { GetSite } from "lemmy-js-client/dist/types/GetSite";
51 import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse";
52 import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages";
53 import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse";
54 import { CreatePostReport } from "lemmy-js-client/dist/types/CreatePostReport";
55 import { ListPostReportsResponse } from "lemmy-js-client/dist/types/ListPostReportsResponse";
56 import { ListPostReports } from "lemmy-js-client/dist/types/ListPostReports";
57 import { CommentReportResponse } from "lemmy-js-client/dist/types/CommentReportResponse";
58 import { CreateCommentReport } from "lemmy-js-client/dist/types/CreateCommentReport";
59 import { ListCommentReportsResponse } from "lemmy-js-client/dist/types/ListCommentReportsResponse";
60 import { ListCommentReports } from "lemmy-js-client/dist/types/ListCommentReports";
61 import { GetPostsResponse } from "lemmy-js-client/dist/types/GetPostsResponse";
62 import { GetPosts } from "lemmy-js-client/dist/types/GetPosts";
63 import { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDetailsResponse";
64 import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails";
66 export interface API {
71 export let alpha: API = {
72 client: new LemmyHttp("http://127.0.0.1:8541"),
76 export let beta: API = {
77 client: new LemmyHttp("http://127.0.0.1:8551"),
81 export let gamma: API = {
82 client: new LemmyHttp("http://127.0.0.1:8561"),
86 export let delta: API = {
87 client: new LemmyHttp("http://127.0.0.1:8571"),
91 export let epsilon: API = {
92 client: new LemmyHttp("http://127.0.0.1:8581"),
96 const password = "lemmylemmy";
98 export async function setupLogins() {
99 let formAlpha: Login = {
100 username_or_email: "lemmy_alpha",
103 let resAlpha = alpha.client.login(formAlpha);
105 let formBeta: Login = {
106 username_or_email: "lemmy_beta",
109 let resBeta = beta.client.login(formBeta);
111 let formGamma: Login = {
112 username_or_email: "lemmy_gamma",
115 let resGamma = gamma.client.login(formGamma);
117 let formDelta: Login = {
118 username_or_email: "lemmy_delta",
121 let resDelta = delta.client.login(formDelta);
123 let formEpsilon: Login = {
124 username_or_email: "lemmy_epsilon",
127 let resEpsilon = epsilon.client.login(formEpsilon);
129 let res = await Promise.all([
137 alpha.auth = res[0].jwt ?? "";
138 beta.auth = res[1].jwt ?? "";
139 gamma.auth = res[2].jwt ?? "";
140 delta.auth = res[3].jwt ?? "";
141 epsilon.auth = res[4].jwt ?? "";
143 // Registration applications are now enabled by default, need to disable them
144 let editSiteForm: EditSite = {
145 registration_mode: "Open",
146 rate_limit_message: 999,
147 rate_limit_post: 999,
148 rate_limit_register: 999,
149 rate_limit_image: 999,
150 rate_limit_comment: 999,
151 rate_limit_search: 999,
155 // Set the blocks and auths for each
156 editSiteForm.auth = alpha.auth;
157 editSiteForm.allowed_instances = [
163 await alpha.client.editSite(editSiteForm);
165 editSiteForm.auth = beta.auth;
166 editSiteForm.allowed_instances = [
172 await beta.client.editSite(editSiteForm);
174 editSiteForm.auth = gamma.auth;
175 editSiteForm.allowed_instances = [
181 await gamma.client.editSite(editSiteForm);
183 editSiteForm.allowed_instances = ["lemmy-beta"];
184 editSiteForm.auth = delta.auth;
185 await delta.client.editSite(editSiteForm);
187 editSiteForm.auth = epsilon.auth;
188 editSiteForm.allowed_instances = [];
189 editSiteForm.blocked_instances = ["lemmy-alpha"];
190 await epsilon.client.editSite(editSiteForm);
192 // Create the main alpha/beta communities
193 // Ignore thrown errors of duplicates
195 await createCommunity(alpha, "main");
196 await createCommunity(beta, "main");
200 export async function createPost(
202 community_id: number,
203 ): Promise<PostResponse> {
204 let name = randomString(5);
205 let body = randomString(10);
206 let url = "https://google.com/";
207 let form: CreatePost = {
214 return api.client.createPost(form);
217 export async function editPost(api: API, post: Post): Promise<PostResponse> {
218 let name = "A jest test federated post, updated";
219 let form: EditPost = {
224 return api.client.editPost(form);
227 export async function deletePost(
231 ): Promise<PostResponse> {
232 let form: DeletePost = {
237 return api.client.deletePost(form);
240 export async function removePost(
244 ): Promise<PostResponse> {
245 let form: RemovePost = {
250 return api.client.removePost(form);
253 export async function featurePost(
257 ): Promise<PostResponse> {
258 let form: FeaturePost = {
261 feature_type: "Community",
264 return api.client.featurePost(form);
267 export async function lockPost(
271 ): Promise<PostResponse> {
272 let form: LockPost = {
277 return api.client.lockPost(form);
280 export async function resolvePost(
283 ): Promise<ResolveObjectResponse> {
284 let form: ResolveObject = {
288 return api.client.resolveObject(form);
291 export async function searchPostLocal(
294 ): Promise<SearchResponse> {
301 return api.client.search(form);
304 export async function getPost(
307 ): Promise<GetPostResponse> {
308 let form: GetPost = {
312 return api.client.getPost(form);
315 export async function getComments(
318 ): Promise<GetCommentsResponse> {
319 let form: GetComments = {
325 return api.client.getComments(form);
328 export async function resolveComment(
331 ): Promise<ResolveObjectResponse> {
332 let form: ResolveObject = {
336 return api.client.resolveObject(form);
339 export async function resolveBetaCommunity(
341 ): Promise<ResolveObjectResponse> {
342 // Use short-hand search url
343 let form: ResolveObject = {
344 q: "!main@lemmy-beta:8551",
347 return api.client.resolveObject(form);
350 export async function resolveCommunity(
353 ): Promise<ResolveObjectResponse> {
354 let form: ResolveObject = {
358 return api.client.resolveObject(form);
361 export async function resolvePerson(
364 ): Promise<ResolveObjectResponse> {
365 let form: ResolveObject = {
369 return api.client.resolveObject(form);
372 export async function banPersonFromSite(
376 remove_data: boolean,
377 ): Promise<BanPersonResponse> {
378 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
379 let form: BanPerson = {
382 remove_data: remove_data,
385 return api.client.banPerson(form);
388 export async function banPersonFromCommunity(
391 community_id: number,
392 remove_data: boolean,
394 ): Promise<BanFromCommunityResponse> {
395 let form: BanFromCommunity = {
398 remove_data: remove_data,
402 return api.client.banFromCommunity(form);
405 export async function followCommunity(
408 community_id: number,
409 ): Promise<CommunityResponse> {
410 let form: FollowCommunity = {
415 return api.client.followCommunity(form);
418 export async function likePost(
422 ): Promise<PostResponse> {
423 let form: CreatePostLike = {
429 return api.client.likePost(form);
432 export async function createComment(
436 content = "a jest test comment",
437 ): Promise<CommentResponse> {
438 let form: CreateComment = {
444 return api.client.createComment(form);
447 export async function editComment(
450 content = "A jest test federated comment update",
451 ): Promise<CommentResponse> {
452 let form: EditComment = {
457 return api.client.editComment(form);
460 export async function deleteComment(
464 ): Promise<CommentResponse> {
465 let form: DeleteComment = {
470 return api.client.deleteComment(form);
473 export async function removeComment(
477 ): Promise<CommentResponse> {
478 let form: RemoveComment = {
483 return api.client.removeComment(form);
486 export async function getMentions(
488 ): Promise<GetPersonMentionsResponse> {
489 let form: GetPersonMentions = {
494 return api.client.getPersonMentions(form);
497 export async function likeComment(
501 ): Promise<CommentResponse> {
502 let form: CreateCommentLike = {
503 comment_id: comment.id,
507 return api.client.likeComment(form);
510 export async function createCommunity(
512 name_: string = randomString(5),
513 ): Promise<CommunityResponse> {
514 let description = "a sample description";
515 let form: CreateCommunity = {
521 return api.client.createCommunity(form);
524 export async function getCommunity(
527 ): Promise<CommunityResponse> {
528 let form: GetCommunity = {
532 return api.client.getCommunity(form);
535 export async function deleteCommunity(
538 community_id: number,
539 ): Promise<CommunityResponse> {
540 let form: DeleteCommunity = {
545 return api.client.deleteCommunity(form);
548 export async function removeCommunity(
551 community_id: number,
552 ): Promise<CommunityResponse> {
553 let form: RemoveCommunity = {
558 return api.client.removeCommunity(form);
561 export async function createPrivateMessage(
563 recipient_id: number,
564 ): Promise<PrivateMessageResponse> {
565 let content = "A jest test federated private message";
566 let form: CreatePrivateMessage = {
571 return api.client.createPrivateMessage(form);
574 export async function editPrivateMessage(
576 private_message_id: number,
577 ): Promise<PrivateMessageResponse> {
578 let updatedContent = "A jest test federated private message edited";
579 let form: EditPrivateMessage = {
580 content: updatedContent,
584 return api.client.editPrivateMessage(form);
587 export async function deletePrivateMessage(
590 private_message_id: number,
591 ): Promise<PrivateMessageResponse> {
592 let form: DeletePrivateMessage = {
597 return api.client.deletePrivateMessage(form);
600 export async function registerUser(
602 username: string = randomString(5),
603 ): Promise<LoginResponse> {
604 let form: Register = {
607 password_verify: password,
610 return api.client.register(form);
613 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
614 let form: SaveUserSettings = {
619 default_sort_type: "Active",
620 default_listing_type: "All",
621 interface_language: "en",
623 send_notifications_to_email: false,
624 bio: "a changed bio",
627 return saveUserSettings(api, form);
630 export async function saveUserSettingsFederated(
632 ): Promise<LoginResponse> {
633 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
634 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
635 let bio = "a changed bio";
636 let form: SaveUserSettings = {
640 default_sort_type: "Hot",
641 default_listing_type: "All",
642 interface_language: "",
645 display_name: "user321",
647 send_notifications_to_email: false,
651 return await saveUserSettings(alpha, form);
654 export async function saveUserSettings(
656 form: SaveUserSettings,
657 ): Promise<LoginResponse> {
658 return api.client.saveUserSettings(form);
660 export async function getPersonDetails(
663 ): Promise<GetPersonDetailsResponse> {
664 let form: GetPersonDetails = {
666 person_id: person_id,
668 return api.client.getPersonDetails(form);
671 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
672 let form: DeleteAccount = {
676 return api.client.deleteAccount(form);
679 export async function getSite(api: API): Promise<GetSiteResponse> {
680 let form: GetSite = {
683 return api.client.getSite(form);
686 export async function listPrivateMessages(
688 ): Promise<PrivateMessagesResponse> {
689 let form: GetPrivateMessages = {
693 return api.client.getPrivateMessages(form);
696 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
697 // Unfollow all remote communities
698 let site = await getSite(api);
700 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
701 for (let cu of remoteFollowed) {
702 await followCommunity(api, false, cu.community.id);
704 let siteRes = await getSite(api);
708 export async function followBeta(api: API): Promise<CommunityResponse> {
709 let betaCommunity = (await resolveBetaCommunity(api)).community;
711 let follow = await followCommunity(api, true, betaCommunity.community.id);
714 return Promise.reject("no community worked");
718 export async function reportPost(
722 ): Promise<PostReportResponse> {
723 let form: CreatePostReport = {
728 return api.client.createPostReport(form);
731 export async function listPostReports(
733 ): Promise<ListPostReportsResponse> {
734 let form: ListPostReports = {
737 return api.client.listPostReports(form);
740 export async function reportComment(
744 ): Promise<CommentReportResponse> {
745 let form: CreateCommentReport = {
750 return api.client.createCommentReport(form);
753 export async function listCommentReports(
755 ): Promise<ListCommentReportsResponse> {
756 let form: ListCommentReports = {
759 return api.client.listCommentReports(form);
762 export function getPosts(
764 moderator_view = false,
765 ): Promise<GetPostsResponse> {
766 let form: GetPosts = {
770 return api.client.getPosts(form);
773 export function delay(millis = 500) {
774 return new Promise(resolve => setTimeout(resolve, millis));
777 export function longDelay() {
781 export function wrapper(form: any): string {
782 return JSON.stringify(form);
785 export function randomString(length: number): string {
788 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
789 var charactersLength = characters.length;
790 for (var i = 0; i < length; i++) {
791 result += characters.charAt(Math.floor(Math.random() * charactersLength));
796 export async function unfollows() {
797 await unfollowRemotes(alpha);
798 await unfollowRemotes(gamma);
799 await unfollowRemotes(delta);
800 await unfollowRemotes(epsilon);
803 export function getCommentParentId(comment: Comment): number | undefined {
804 let split = comment.path.split(".");
808 if (split.length > 1) {
809 return Number(split[split.length - 2]);