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 // Ignore thrown errors of duplicates
193 await createCommunity(alpha, "main");
194 await createCommunity(beta, "main");
198 export async function createPost(
200 community_id: number,
201 ): Promise<PostResponse> {
202 let name = randomString(5);
203 let body = randomString(10);
204 let url = "https://google.com/";
205 let form: CreatePost = {
212 return api.client.createPost(form);
215 export async function editPost(api: API, post: Post): Promise<PostResponse> {
216 let name = "A jest test federated post, updated";
217 let form: EditPost = {
222 return api.client.editPost(form);
225 export async function deletePost(
229 ): Promise<PostResponse> {
230 let form: DeletePost = {
235 return api.client.deletePost(form);
238 export async function removePost(
242 ): Promise<PostResponse> {
243 let form: RemovePost = {
248 return api.client.removePost(form);
251 export async function featurePost(
255 ): Promise<PostResponse> {
256 let form: FeaturePost = {
259 feature_type: "Community",
262 return api.client.featurePost(form);
265 export async function lockPost(
269 ): Promise<PostResponse> {
270 let form: LockPost = {
275 return api.client.lockPost(form);
278 export async function resolvePost(
281 ): Promise<ResolveObjectResponse> {
282 let form: ResolveObject = {
286 return api.client.resolveObject(form);
289 export async function searchPostLocal(
292 ): Promise<SearchResponse> {
299 return api.client.search(form);
302 export async function getPost(
305 ): Promise<GetPostResponse> {
306 let form: GetPost = {
310 return api.client.getPost(form);
313 export async function getComments(
316 ): Promise<GetCommentsResponse> {
317 let form: GetComments = {
323 return api.client.getComments(form);
326 export async function resolveComment(
329 ): Promise<ResolveObjectResponse> {
330 let form: ResolveObject = {
334 return api.client.resolveObject(form);
337 export async function resolveBetaCommunity(
339 ): Promise<ResolveObjectResponse> {
340 // Use short-hand search url
341 let form: ResolveObject = {
342 q: "!main@lemmy-beta:8551",
345 return api.client.resolveObject(form);
348 export async function resolveCommunity(
351 ): Promise<ResolveObjectResponse> {
352 let form: ResolveObject = {
356 return api.client.resolveObject(form);
359 export async function resolvePerson(
362 ): Promise<ResolveObjectResponse> {
363 let form: ResolveObject = {
367 return api.client.resolveObject(form);
370 export async function banPersonFromSite(
374 remove_data: boolean,
375 ): Promise<BanPersonResponse> {
376 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
377 let form: BanPerson = {
380 remove_data: remove_data,
383 return api.client.banPerson(form);
386 export async function banPersonFromCommunity(
389 community_id: number,
390 remove_data: boolean,
392 ): Promise<BanFromCommunityResponse> {
393 let form: BanFromCommunity = {
396 remove_data: remove_data,
400 return api.client.banFromCommunity(form);
403 export async function followCommunity(
406 community_id: number,
407 ): Promise<CommunityResponse> {
408 let form: FollowCommunity = {
413 return api.client.followCommunity(form);
416 export async function likePost(
420 ): Promise<PostResponse> {
421 let form: CreatePostLike = {
427 return api.client.likePost(form);
430 export async function createComment(
434 content = "a jest test comment",
435 ): Promise<CommentResponse> {
436 let form: CreateComment = {
442 return api.client.createComment(form);
445 export async function editComment(
448 content = "A jest test federated comment update",
449 ): Promise<CommentResponse> {
450 let form: EditComment = {
455 return api.client.editComment(form);
458 export async function deleteComment(
462 ): Promise<CommentResponse> {
463 let form: DeleteComment = {
468 return api.client.deleteComment(form);
471 export async function removeComment(
475 ): Promise<CommentResponse> {
476 let form: RemoveComment = {
481 return api.client.removeComment(form);
484 export async function getMentions(
486 ): Promise<GetPersonMentionsResponse> {
487 let form: GetPersonMentions = {
492 return api.client.getPersonMentions(form);
495 export async function likeComment(
499 ): Promise<CommentResponse> {
500 let form: CreateCommentLike = {
501 comment_id: comment.id,
505 return api.client.likeComment(form);
508 export async function createCommunity(
510 name_: string = randomString(5),
511 ): Promise<CommunityResponse> {
512 let description = "a sample description";
513 let form: CreateCommunity = {
519 return api.client.createCommunity(form);
522 export async function getCommunity(
525 ): Promise<CommunityResponse> {
526 let form: GetCommunity = {
530 return api.client.getCommunity(form);
533 export async function deleteCommunity(
536 community_id: number,
537 ): Promise<CommunityResponse> {
538 let form: DeleteCommunity = {
543 return api.client.deleteCommunity(form);
546 export async function removeCommunity(
549 community_id: number,
550 ): Promise<CommunityResponse> {
551 let form: RemoveCommunity = {
556 return api.client.removeCommunity(form);
559 export async function createPrivateMessage(
561 recipient_id: number,
562 ): Promise<PrivateMessageResponse> {
563 let content = "A jest test federated private message";
564 let form: CreatePrivateMessage = {
569 return api.client.createPrivateMessage(form);
572 export async function editPrivateMessage(
574 private_message_id: number,
575 ): Promise<PrivateMessageResponse> {
576 let updatedContent = "A jest test federated private message edited";
577 let form: EditPrivateMessage = {
578 content: updatedContent,
582 return api.client.editPrivateMessage(form);
585 export async function deletePrivateMessage(
588 private_message_id: number,
589 ): Promise<PrivateMessageResponse> {
590 let form: DeletePrivateMessage = {
595 return api.client.deletePrivateMessage(form);
598 export async function registerUser(
600 username: string = randomString(5),
601 ): Promise<LoginResponse> {
602 let form: Register = {
605 password_verify: password,
608 return api.client.register(form);
611 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
612 let form: SaveUserSettings = {
617 default_sort_type: "Active",
618 default_listing_type: "All",
619 interface_language: "en",
621 send_notifications_to_email: false,
622 bio: "a changed bio",
625 return saveUserSettings(api, form);
628 export async function saveUserSettingsFederated(
630 ): Promise<LoginResponse> {
631 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
632 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
633 let bio = "a changed bio";
634 let form: SaveUserSettings = {
638 default_sort_type: "Hot",
639 default_listing_type: "All",
640 interface_language: "",
643 display_name: "user321",
645 send_notifications_to_email: false,
649 return await saveUserSettings(alpha, form);
652 export async function saveUserSettings(
654 form: SaveUserSettings,
655 ): Promise<LoginResponse> {
656 return api.client.saveUserSettings(form);
658 export async function getPersonDetails(
661 ): Promise<GetPersonDetailsResponse> {
662 let form: GetPersonDetails = {
664 person_id: person_id,
666 return api.client.getPersonDetails(form);
669 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
670 let form: DeleteAccount = {
674 return api.client.deleteAccount(form);
677 export async function getSite(api: API): Promise<GetSiteResponse> {
678 let form: GetSite = {
681 return api.client.getSite(form);
684 export async function listPrivateMessages(
686 ): Promise<PrivateMessagesResponse> {
687 let form: GetPrivateMessages = {
691 return api.client.getPrivateMessages(form);
694 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
695 // Unfollow all remote communities
696 let site = await getSite(api);
698 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
699 for (let cu of remoteFollowed) {
700 await followCommunity(api, false, cu.community.id);
702 let siteRes = await getSite(api);
706 export async function followBeta(api: API): Promise<CommunityResponse> {
707 let betaCommunity = (await resolveBetaCommunity(api)).community;
709 let follow = await followCommunity(api, true, betaCommunity.community.id);
712 return Promise.reject("no community worked");
716 export async function reportPost(
720 ): Promise<PostReportResponse> {
721 let form: CreatePostReport = {
726 return api.client.createPostReport(form);
729 export async function listPostReports(
731 ): Promise<ListPostReportsResponse> {
732 let form: ListPostReports = {
735 return api.client.listPostReports(form);
738 export async function reportComment(
742 ): Promise<CommentReportResponse> {
743 let form: CreateCommentReport = {
748 return api.client.createCommentReport(form);
751 export async function listCommentReports(
753 ): Promise<ListCommentReportsResponse> {
754 let form: ListCommentReports = {
757 return api.client.listCommentReports(form);
760 export function delay(millis = 500) {
761 return new Promise(resolve => setTimeout(resolve, millis));
764 export function longDelay() {
768 export function wrapper(form: any): string {
769 return JSON.stringify(form);
772 export function randomString(length: number): string {
775 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
776 var charactersLength = characters.length;
777 for (var i = 0; i < length; i++) {
778 result += characters.charAt(Math.floor(Math.random() * charactersLength));
783 export async function unfollows() {
784 await unfollowRemotes(alpha);
785 await unfollowRemotes(gamma);
786 await unfollowRemotes(delta);
787 await unfollowRemotes(epsilon);
790 export function getCommentParentId(comment: Comment): number | undefined {
791 let split = comment.path.split(".");
795 if (split.length > 1) {
796 return Number(split[split.length - 2]);