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 { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDetailsResponse";
62 import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails";
64 export interface API {
69 export let alpha: API = {
70 client: new LemmyHttp("http://127.0.0.1:8541"),
74 export let beta: API = {
75 client: new LemmyHttp("http://127.0.0.1:8551"),
79 export let gamma: API = {
80 client: new LemmyHttp("http://127.0.0.1:8561"),
84 export let delta: API = {
85 client: new LemmyHttp("http://127.0.0.1:8571"),
89 export let epsilon: API = {
90 client: new LemmyHttp("http://127.0.0.1:8581"),
94 const password = "lemmylemmy";
96 export async function setupLogins() {
97 let formAlpha: Login = {
98 username_or_email: "lemmy_alpha",
101 let resAlpha = alpha.client.login(formAlpha);
103 let formBeta: Login = {
104 username_or_email: "lemmy_beta",
107 let resBeta = beta.client.login(formBeta);
109 let formGamma: Login = {
110 username_or_email: "lemmy_gamma",
113 let resGamma = gamma.client.login(formGamma);
115 let formDelta: Login = {
116 username_or_email: "lemmy_delta",
119 let resDelta = delta.client.login(formDelta);
121 let formEpsilon: Login = {
122 username_or_email: "lemmy_epsilon",
125 let resEpsilon = epsilon.client.login(formEpsilon);
127 let res = await Promise.all([
135 alpha.auth = res[0].jwt ?? "";
136 beta.auth = res[1].jwt ?? "";
137 gamma.auth = res[2].jwt ?? "";
138 delta.auth = res[3].jwt ?? "";
139 epsilon.auth = res[4].jwt ?? "";
141 // Registration applications are now enabled by default, need to disable them
142 let editSiteForm: EditSite = {
143 registration_mode: "Open",
144 rate_limit_message: 999,
145 rate_limit_post: 999,
146 rate_limit_register: 999,
147 rate_limit_image: 999,
148 rate_limit_comment: 999,
149 rate_limit_search: 999,
153 // Set the blocks and auths for each
154 editSiteForm.auth = alpha.auth;
155 editSiteForm.allowed_instances = [
161 await alpha.client.editSite(editSiteForm);
163 editSiteForm.auth = beta.auth;
164 editSiteForm.allowed_instances = [
170 await beta.client.editSite(editSiteForm);
172 editSiteForm.auth = gamma.auth;
173 editSiteForm.allowed_instances = [
179 await gamma.client.editSite(editSiteForm);
181 editSiteForm.allowed_instances = ["lemmy-beta"];
182 editSiteForm.auth = delta.auth;
183 await delta.client.editSite(editSiteForm);
185 editSiteForm.auth = epsilon.auth;
186 editSiteForm.allowed_instances = [];
187 editSiteForm.blocked_instances = ["lemmy-alpha"];
188 await epsilon.client.editSite(editSiteForm);
190 // Create the main alpha/beta communities
191 await createCommunity(alpha, "main");
192 await createCommunity(beta, "main");
195 export async function createPost(
197 community_id: number,
198 ): Promise<PostResponse> {
199 let name = randomString(5);
200 let body = randomString(10);
201 let url = "https://google.com/";
202 let form: CreatePost = {
209 return api.client.createPost(form);
212 export async function editPost(api: API, post: Post): Promise<PostResponse> {
213 let name = "A jest test federated post, updated";
214 let form: EditPost = {
219 return api.client.editPost(form);
222 export async function deletePost(
226 ): Promise<PostResponse> {
227 let form: DeletePost = {
232 return api.client.deletePost(form);
235 export async function removePost(
239 ): Promise<PostResponse> {
240 let form: RemovePost = {
245 return api.client.removePost(form);
248 export async function featurePost(
252 ): Promise<PostResponse> {
253 let form: FeaturePost = {
256 feature_type: "Community",
259 return api.client.featurePost(form);
262 export async function lockPost(
266 ): Promise<PostResponse> {
267 let form: LockPost = {
272 return api.client.lockPost(form);
275 export async function resolvePost(
278 ): Promise<ResolveObjectResponse> {
279 let form: ResolveObject = {
283 return api.client.resolveObject(form);
286 export async function searchPostLocal(
289 ): Promise<SearchResponse> {
296 return api.client.search(form);
299 export async function getPost(
302 ): Promise<GetPostResponse> {
303 let form: GetPost = {
307 return api.client.getPost(form);
310 export async function getComments(
313 ): Promise<GetCommentsResponse> {
314 let form: GetComments = {
320 return api.client.getComments(form);
323 export async function resolveComment(
326 ): Promise<ResolveObjectResponse> {
327 let form: ResolveObject = {
331 return api.client.resolveObject(form);
334 export async function resolveBetaCommunity(
336 ): Promise<ResolveObjectResponse> {
337 // Use short-hand search url
338 let form: ResolveObject = {
339 q: "!main@lemmy-beta:8551",
342 return api.client.resolveObject(form);
345 export async function resolveCommunity(
348 ): Promise<ResolveObjectResponse> {
349 let form: ResolveObject = {
353 return api.client.resolveObject(form);
356 export async function resolvePerson(
359 ): Promise<ResolveObjectResponse> {
360 let form: ResolveObject = {
364 return api.client.resolveObject(form);
367 export async function banPersonFromSite(
371 remove_data: boolean,
372 ): Promise<BanPersonResponse> {
373 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
374 let form: BanPerson = {
377 remove_data: remove_data,
380 return api.client.banPerson(form);
383 export async function banPersonFromCommunity(
386 community_id: number,
387 remove_data: boolean,
389 ): Promise<BanFromCommunityResponse> {
390 let form: BanFromCommunity = {
393 remove_data: remove_data,
397 return api.client.banFromCommunity(form);
400 export async function followCommunity(
403 community_id: number,
404 ): Promise<CommunityResponse> {
405 let form: FollowCommunity = {
410 return api.client.followCommunity(form);
413 export async function likePost(
417 ): Promise<PostResponse> {
418 let form: CreatePostLike = {
424 return api.client.likePost(form);
427 export async function createComment(
431 content = "a jest test comment",
432 ): Promise<CommentResponse> {
433 let form: CreateComment = {
439 return api.client.createComment(form);
442 export async function editComment(
445 content = "A jest test federated comment update",
446 ): Promise<CommentResponse> {
447 let form: EditComment = {
452 return api.client.editComment(form);
455 export async function deleteComment(
459 ): Promise<CommentResponse> {
460 let form: DeleteComment = {
465 return api.client.deleteComment(form);
468 export async function removeComment(
472 ): Promise<CommentResponse> {
473 let form: RemoveComment = {
478 return api.client.removeComment(form);
481 export async function getMentions(
483 ): Promise<GetPersonMentionsResponse> {
484 let form: GetPersonMentions = {
489 return api.client.getPersonMentions(form);
492 export async function likeComment(
496 ): Promise<CommentResponse> {
497 let form: CreateCommentLike = {
498 comment_id: comment.id,
502 return api.client.likeComment(form);
505 export async function createCommunity(
507 name_: string = randomString(5),
508 ): Promise<CommunityResponse> {
509 let description = "a sample description";
510 let form: CreateCommunity = {
516 return api.client.createCommunity(form);
519 export async function getCommunity(
522 ): Promise<CommunityResponse> {
523 let form: GetCommunity = {
527 return api.client.getCommunity(form);
530 export async function deleteCommunity(
533 community_id: number,
534 ): Promise<CommunityResponse> {
535 let form: DeleteCommunity = {
540 return api.client.deleteCommunity(form);
543 export async function removeCommunity(
546 community_id: number,
547 ): Promise<CommunityResponse> {
548 let form: RemoveCommunity = {
553 return api.client.removeCommunity(form);
556 export async function createPrivateMessage(
558 recipient_id: number,
559 ): Promise<PrivateMessageResponse> {
560 let content = "A jest test federated private message";
561 let form: CreatePrivateMessage = {
566 return api.client.createPrivateMessage(form);
569 export async function editPrivateMessage(
571 private_message_id: number,
572 ): Promise<PrivateMessageResponse> {
573 let updatedContent = "A jest test federated private message edited";
574 let form: EditPrivateMessage = {
575 content: updatedContent,
579 return api.client.editPrivateMessage(form);
582 export async function deletePrivateMessage(
585 private_message_id: number,
586 ): Promise<PrivateMessageResponse> {
587 let form: DeletePrivateMessage = {
592 return api.client.deletePrivateMessage(form);
595 export async function registerUser(
597 username: string = randomString(5),
598 ): Promise<LoginResponse> {
599 let form: Register = {
602 password_verify: password,
605 return api.client.register(form);
608 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
609 let form: SaveUserSettings = {
612 default_sort_type: "Active",
613 default_listing_type: "All",
614 interface_language: "en",
616 send_notifications_to_email: false,
617 bio: "a changed bio",
620 return saveUserSettings(api, form);
623 export async function saveUserSettingsFederated(
625 ): Promise<LoginResponse> {
626 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
627 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
628 let bio = "a changed bio";
629 let form: SaveUserSettings = {
631 default_sort_type: "Hot",
632 default_listing_type: "All",
633 interface_language: "",
636 display_name: "user321",
638 send_notifications_to_email: false,
642 return await saveUserSettings(alpha, form);
645 export async function saveUserSettings(
647 form: SaveUserSettings,
648 ): Promise<LoginResponse> {
649 return api.client.saveUserSettings(form);
651 export async function getPersonDetails(
654 ): Promise<GetPersonDetailsResponse> {
655 let form: GetPersonDetails = {
657 person_id: person_id,
659 return api.client.getPersonDetails(form);
662 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
663 let form: DeleteAccount = {
667 return api.client.deleteAccount(form);
670 export async function getSite(api: API): Promise<GetSiteResponse> {
671 let form: GetSite = {
674 return api.client.getSite(form);
677 export async function listPrivateMessages(
679 ): Promise<PrivateMessagesResponse> {
680 let form: GetPrivateMessages = {
684 return api.client.getPrivateMessages(form);
687 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
688 // Unfollow all remote communities
689 let site = await getSite(api);
691 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
692 for (let cu of remoteFollowed) {
693 await followCommunity(api, false, cu.community.id);
695 let siteRes = await getSite(api);
699 export async function followBeta(api: API): Promise<CommunityResponse> {
700 let betaCommunity = (await resolveBetaCommunity(api)).community;
702 let follow = await followCommunity(api, true, betaCommunity.community.id);
705 return Promise.reject("no community worked");
709 export async function reportPost(
713 ): Promise<PostReportResponse> {
714 let form: CreatePostReport = {
719 return api.client.createPostReport(form);
722 export async function listPostReports(
724 ): Promise<ListPostReportsResponse> {
725 let form: ListPostReports = {
728 return api.client.listPostReports(form);
731 export async function reportComment(
735 ): Promise<CommentReportResponse> {
736 let form: CreateCommentReport = {
741 return api.client.createCommentReport(form);
744 export async function listCommentReports(
746 ): Promise<ListCommentReportsResponse> {
747 let form: ListCommentReports = {
750 return api.client.listCommentReports(form);
753 export function delay(millis = 500) {
754 return new Promise(resolve => setTimeout(resolve, millis));
757 export function longDelay() {
761 export function wrapper(form: any): string {
762 return JSON.stringify(form);
765 export function randomString(length: number): string {
768 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
769 var charactersLength = characters.length;
770 for (var i = 0; i < length; i++) {
771 result += characters.charAt(Math.floor(Math.random() * charactersLength));
776 export async function unfollows() {
777 await unfollowRemotes(alpha);
778 await unfollowRemotes(gamma);
779 await unfollowRemotes(delta);
780 await unfollowRemotes(epsilon);
783 export function getCommentParentId(comment: Comment): number | undefined {
784 let split = comment.path.split(".");
788 if (split.length > 1) {
789 return Number(split[split.length - 2]);