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";
62 export interface API {
67 export let alpha: API = {
68 client: new LemmyHttp("http://127.0.0.1:8541"),
72 export let beta: API = {
73 client: new LemmyHttp("http://127.0.0.1:8551"),
77 export let gamma: API = {
78 client: new LemmyHttp("http://127.0.0.1:8561"),
82 export let delta: API = {
83 client: new LemmyHttp("http://127.0.0.1:8571"),
87 export let epsilon: API = {
88 client: new LemmyHttp("http://127.0.0.1:8581"),
92 const password = "lemmylemmy";
94 export async function setupLogins() {
95 let formAlpha: Login = {
96 username_or_email: "lemmy_alpha",
99 let resAlpha = alpha.client.login(formAlpha);
101 let formBeta: Login = {
102 username_or_email: "lemmy_beta",
105 let resBeta = beta.client.login(formBeta);
107 let formGamma: Login = {
108 username_or_email: "lemmy_gamma",
111 let resGamma = gamma.client.login(formGamma);
113 let formDelta: Login = {
114 username_or_email: "lemmy_delta",
117 let resDelta = delta.client.login(formDelta);
119 let formEpsilon: Login = {
120 username_or_email: "lemmy_epsilon",
123 let resEpsilon = epsilon.client.login(formEpsilon);
125 let res = await Promise.all([
133 alpha.auth = res[0].jwt ?? "";
134 beta.auth = res[1].jwt ?? "";
135 gamma.auth = res[2].jwt ?? "";
136 delta.auth = res[3].jwt ?? "";
137 epsilon.auth = res[4].jwt ?? "";
139 // Registration applications are now enabled by default, need to disable them
140 let editSiteForm: EditSite = {
141 registration_mode: "Open",
142 rate_limit_message: 999,
143 rate_limit_post: 999,
144 rate_limit_register: 999,
145 rate_limit_image: 999,
146 rate_limit_comment: 999,
147 rate_limit_search: 999,
151 // Set the blocks and auths for each
152 editSiteForm.auth = alpha.auth;
153 editSiteForm.allowed_instances = [
159 await alpha.client.editSite(editSiteForm);
161 editSiteForm.auth = beta.auth;
162 editSiteForm.allowed_instances = [
168 await beta.client.editSite(editSiteForm);
170 editSiteForm.auth = gamma.auth;
171 editSiteForm.allowed_instances = [
177 await gamma.client.editSite(editSiteForm);
179 editSiteForm.allowed_instances = ["lemmy-beta"];
180 editSiteForm.auth = delta.auth;
181 await delta.client.editSite(editSiteForm);
183 editSiteForm.auth = epsilon.auth;
184 editSiteForm.allowed_instances = [];
185 editSiteForm.blocked_instances = ["lemmy-alpha"];
186 await epsilon.client.editSite(editSiteForm);
188 // Create the main alpha/beta communities
189 await createCommunity(alpha, "main");
190 await createCommunity(beta, "main");
193 export async function createPost(
195 community_id: number,
196 ): Promise<PostResponse> {
197 let name = randomString(5);
198 let body = randomString(10);
199 let url = "https://google.com/";
200 let form: CreatePost = {
207 return api.client.createPost(form);
210 export async function editPost(api: API, post: Post): Promise<PostResponse> {
211 let name = "A jest test federated post, updated";
212 let form: EditPost = {
217 return api.client.editPost(form);
220 export async function deletePost(
224 ): Promise<PostResponse> {
225 let form: DeletePost = {
230 return api.client.deletePost(form);
233 export async function removePost(
237 ): Promise<PostResponse> {
238 let form: RemovePost = {
243 return api.client.removePost(form);
246 export async function featurePost(
250 ): Promise<PostResponse> {
251 let form: FeaturePost = {
254 feature_type: "Community",
257 return api.client.featurePost(form);
260 export async function lockPost(
264 ): Promise<PostResponse> {
265 let form: LockPost = {
270 return api.client.lockPost(form);
273 export async function resolvePost(
276 ): Promise<ResolveObjectResponse> {
277 let form: ResolveObject = {
281 return api.client.resolveObject(form);
284 export async function searchPostLocal(
287 ): Promise<SearchResponse> {
294 return api.client.search(form);
297 export async function getPost(
300 ): Promise<GetPostResponse> {
301 let form: GetPost = {
305 return api.client.getPost(form);
308 export async function getComments(
311 ): Promise<GetCommentsResponse> {
312 let form: GetComments = {
318 return api.client.getComments(form);
321 export async function resolveComment(
324 ): Promise<ResolveObjectResponse> {
325 let form: ResolveObject = {
329 return api.client.resolveObject(form);
332 export async function resolveBetaCommunity(
334 ): Promise<ResolveObjectResponse> {
335 // Use short-hand search url
336 let form: ResolveObject = {
337 q: "!main@lemmy-beta:8551",
340 return api.client.resolveObject(form);
343 export async function resolveCommunity(
346 ): Promise<ResolveObjectResponse> {
347 let form: ResolveObject = {
351 return api.client.resolveObject(form);
354 export async function resolvePerson(
357 ): Promise<ResolveObjectResponse> {
358 let form: ResolveObject = {
362 return api.client.resolveObject(form);
365 export async function banPersonFromSite(
369 remove_data: boolean,
370 ): Promise<BanPersonResponse> {
371 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
372 let form: BanPerson = {
375 remove_data: remove_data,
378 return api.client.banPerson(form);
381 export async function banPersonFromCommunity(
384 community_id: number,
385 remove_data: boolean,
387 ): Promise<BanFromCommunityResponse> {
388 let form: BanFromCommunity = {
391 remove_data: remove_data,
395 return api.client.banFromCommunity(form);
398 export async function followCommunity(
401 community_id: number,
402 ): Promise<CommunityResponse> {
403 let form: FollowCommunity = {
408 return api.client.followCommunity(form);
411 export async function likePost(
415 ): Promise<PostResponse> {
416 let form: CreatePostLike = {
422 return api.client.likePost(form);
425 export async function createComment(
429 content = "a jest test comment",
430 ): Promise<CommentResponse> {
431 let form: CreateComment = {
437 return api.client.createComment(form);
440 export async function editComment(
443 content = "A jest test federated comment update",
444 ): Promise<CommentResponse> {
445 let form: EditComment = {
450 return api.client.editComment(form);
453 export async function deleteComment(
457 ): Promise<CommentResponse> {
458 let form: DeleteComment = {
463 return api.client.deleteComment(form);
466 export async function removeComment(
470 ): Promise<CommentResponse> {
471 let form: RemoveComment = {
476 return api.client.removeComment(form);
479 export async function getMentions(
481 ): Promise<GetPersonMentionsResponse> {
482 let form: GetPersonMentions = {
487 return api.client.getPersonMentions(form);
490 export async function likeComment(
494 ): Promise<CommentResponse> {
495 let form: CreateCommentLike = {
496 comment_id: comment.id,
500 return api.client.likeComment(form);
503 export async function createCommunity(
505 name_: string = randomString(5),
506 ): Promise<CommunityResponse> {
507 let description = "a sample description";
508 let form: CreateCommunity = {
514 return api.client.createCommunity(form);
517 export async function getCommunity(
520 ): Promise<CommunityResponse> {
521 let form: GetCommunity = {
525 return api.client.getCommunity(form);
528 export async function deleteCommunity(
531 community_id: number,
532 ): Promise<CommunityResponse> {
533 let form: DeleteCommunity = {
538 return api.client.deleteCommunity(form);
541 export async function removeCommunity(
544 community_id: number,
545 ): Promise<CommunityResponse> {
546 let form: RemoveCommunity = {
551 return api.client.removeCommunity(form);
554 export async function createPrivateMessage(
556 recipient_id: number,
557 ): Promise<PrivateMessageResponse> {
558 let content = "A jest test federated private message";
559 let form: CreatePrivateMessage = {
564 return api.client.createPrivateMessage(form);
567 export async function editPrivateMessage(
569 private_message_id: number,
570 ): Promise<PrivateMessageResponse> {
571 let updatedContent = "A jest test federated private message edited";
572 let form: EditPrivateMessage = {
573 content: updatedContent,
577 return api.client.editPrivateMessage(form);
580 export async function deletePrivateMessage(
583 private_message_id: number,
584 ): Promise<PrivateMessageResponse> {
585 let form: DeletePrivateMessage = {
590 return api.client.deletePrivateMessage(form);
593 export async function registerUser(
595 username: string = randomString(5),
596 ): Promise<LoginResponse> {
597 let form: Register = {
600 password_verify: password,
603 return api.client.register(form);
606 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
607 let form: SaveUserSettings = {
610 default_sort_type: "Active",
611 default_listing_type: "All",
612 interface_language: "en",
614 send_notifications_to_email: false,
615 bio: "a changed bio",
618 return saveUserSettings(api, form);
621 export async function saveUserSettingsFederated(
623 ): Promise<LoginResponse> {
624 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
625 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
626 let bio = "a changed bio";
627 let form: SaveUserSettings = {
629 default_sort_type: "Hot",
630 default_listing_type: "All",
631 interface_language: "",
634 display_name: "user321",
636 send_notifications_to_email: false,
640 return await saveUserSettings(alpha, form);
643 export async function saveUserSettings(
645 form: SaveUserSettings,
646 ): Promise<LoginResponse> {
647 return api.client.saveUserSettings(form);
650 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
651 let form: DeleteAccount = {
655 return api.client.deleteAccount(form);
658 export async function getSite(api: API): Promise<GetSiteResponse> {
659 let form: GetSite = {
662 return api.client.getSite(form);
665 export async function listPrivateMessages(
667 ): Promise<PrivateMessagesResponse> {
668 let form: GetPrivateMessages = {
672 return api.client.getPrivateMessages(form);
675 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
676 // Unfollow all remote communities
677 let site = await getSite(api);
679 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
680 for (let cu of remoteFollowed) {
681 await followCommunity(api, false, cu.community.id);
683 let siteRes = await getSite(api);
687 export async function followBeta(api: API): Promise<CommunityResponse> {
688 let betaCommunity = (await resolveBetaCommunity(api)).community;
690 let follow = await followCommunity(api, true, betaCommunity.community.id);
693 return Promise.reject("no community worked");
697 export async function reportPost(
701 ): Promise<PostReportResponse> {
702 let form: CreatePostReport = {
707 return api.client.createPostReport(form);
710 export async function listPostReports(
712 ): Promise<ListPostReportsResponse> {
713 let form: ListPostReports = {
716 return api.client.listPostReports(form);
719 export async function reportComment(
723 ): Promise<CommentReportResponse> {
724 let form: CreateCommentReport = {
729 return api.client.createCommentReport(form);
732 export async function listCommentReports(
734 ): Promise<ListCommentReportsResponse> {
735 let form: ListCommentReports = {
738 return api.client.listCommentReports(form);
741 export function delay(millis = 500) {
742 return new Promise(resolve => setTimeout(resolve, millis));
745 export function longDelay() {
749 export function wrapper(form: any): string {
750 return JSON.stringify(form);
753 export function randomString(length: number): string {
756 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
757 var charactersLength = characters.length;
758 for (var i = 0; i < length; i++) {
759 result += characters.charAt(Math.floor(Math.random() * charactersLength));
764 export async function unfollows() {
765 await unfollowRemotes(alpha);
766 await unfollowRemotes(gamma);
767 await unfollowRemotes(delta);
768 await unfollowRemotes(epsilon);
771 export function getCommentParentId(comment: Comment): number | undefined {
772 let split = comment.path.split(".");
776 if (split.length > 1) {
777 return Number(split[split.length - 2]);