5 GetUnreadCountResponse,
8 } from "lemmy-js-client";
9 import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
10 import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
11 import { EditPost } from "lemmy-js-client/dist/types/EditPost";
12 import { EditSite } from "lemmy-js-client/dist/types/EditSite";
13 import { FeaturePost } from "lemmy-js-client/dist/types/FeaturePost";
14 import { GetComments } from "lemmy-js-client/dist/types/GetComments";
15 import { GetCommentsResponse } from "lemmy-js-client/dist/types/GetCommentsResponse";
16 import { GetPost } from "lemmy-js-client/dist/types/GetPost";
17 import { GetPostResponse } from "lemmy-js-client/dist/types/GetPostResponse";
18 import { LockPost } from "lemmy-js-client/dist/types/LockPost";
19 import { Login } from "lemmy-js-client/dist/types/Login";
20 import { Post } from "lemmy-js-client/dist/types/Post";
21 import { PostResponse } from "lemmy-js-client/dist/types/PostResponse";
22 import { RemovePost } from "lemmy-js-client/dist/types/RemovePost";
23 import { ResolveObject } from "lemmy-js-client/dist/types/ResolveObject";
24 import { ResolveObjectResponse } from "lemmy-js-client/dist/types/ResolveObjectResponse";
25 import { Search } from "lemmy-js-client/dist/types/Search";
26 import { SearchResponse } from "lemmy-js-client/dist/types/SearchResponse";
27 import { Comment } from "lemmy-js-client/dist/types/Comment";
28 import { BanPersonResponse } from "lemmy-js-client/dist/types/BanPersonResponse";
29 import { BanPerson } from "lemmy-js-client/dist/types/BanPerson";
30 import { BanFromCommunityResponse } from "lemmy-js-client/dist/types/BanFromCommunityResponse";
31 import { BanFromCommunity } from "lemmy-js-client/dist/types/BanFromCommunity";
32 import { CommunityResponse } from "lemmy-js-client/dist/types/CommunityResponse";
33 import { FollowCommunity } from "lemmy-js-client/dist/types/FollowCommunity";
34 import { CreatePostLike } from "lemmy-js-client/dist/types/CreatePostLike";
35 import { CommentResponse } from "lemmy-js-client/dist/types/CommentResponse";
36 import { CreateComment } from "lemmy-js-client/dist/types/CreateComment";
37 import { EditComment } from "lemmy-js-client/dist/types/EditComment";
38 import { DeleteComment } from "lemmy-js-client/dist/types/DeleteComment";
39 import { RemoveComment } from "lemmy-js-client/dist/types/RemoveComment";
40 import { GetPersonMentionsResponse } from "lemmy-js-client/dist/types/GetPersonMentionsResponse";
41 import { GetPersonMentions } from "lemmy-js-client/dist/types/GetPersonMentions";
42 import { CreateCommentLike } from "lemmy-js-client/dist/types/CreateCommentLike";
43 import { CreateCommunity } from "lemmy-js-client/dist/types/CreateCommunity";
44 import { GetCommunity } from "lemmy-js-client/dist/types/GetCommunity";
45 import { DeleteCommunity } from "lemmy-js-client/dist/types/DeleteCommunity";
46 import { RemoveCommunity } from "lemmy-js-client/dist/types/RemoveCommunity";
47 import { PrivateMessageResponse } from "lemmy-js-client/dist/types/PrivateMessageResponse";
48 import { CreatePrivateMessage } from "lemmy-js-client/dist/types/CreatePrivateMessage";
49 import { EditPrivateMessage } from "lemmy-js-client/dist/types/EditPrivateMessage";
50 import { DeletePrivateMessage } from "lemmy-js-client/dist/types/DeletePrivateMessage";
51 import { LoginResponse } from "lemmy-js-client/dist/types/LoginResponse";
52 import { Register } from "lemmy-js-client/dist/types/Register";
53 import { SaveUserSettings } from "lemmy-js-client/dist/types/SaveUserSettings";
54 import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount";
55 import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse";
56 import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse";
57 import { GetSite } from "lemmy-js-client/dist/types/GetSite";
58 import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse";
59 import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages";
60 import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse";
61 import { CreatePostReport } from "lemmy-js-client/dist/types/CreatePostReport";
62 import { ListPostReportsResponse } from "lemmy-js-client/dist/types/ListPostReportsResponse";
63 import { ListPostReports } from "lemmy-js-client/dist/types/ListPostReports";
64 import { CommentReportResponse } from "lemmy-js-client/dist/types/CommentReportResponse";
65 import { CreateCommentReport } from "lemmy-js-client/dist/types/CreateCommentReport";
66 import { ListCommentReportsResponse } from "lemmy-js-client/dist/types/ListCommentReportsResponse";
67 import { ListCommentReports } from "lemmy-js-client/dist/types/ListCommentReports";
68 import { GetPostsResponse } from "lemmy-js-client/dist/types/GetPostsResponse";
69 import { GetPosts } from "lemmy-js-client/dist/types/GetPosts";
70 import { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDetailsResponse";
71 import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails";
73 export interface API {
78 export let alpha: API = {
79 client: new LemmyHttp("http://127.0.0.1:8541"),
83 export let beta: API = {
84 client: new LemmyHttp("http://127.0.0.1:8551"),
88 export let gamma: API = {
89 client: new LemmyHttp("http://127.0.0.1:8561"),
93 export let delta: API = {
94 client: new LemmyHttp("http://127.0.0.1:8571"),
98 export let epsilon: API = {
99 client: new LemmyHttp("http://127.0.0.1:8581"),
103 const password = "lemmylemmy";
105 export async function setupLogins() {
106 let formAlpha: Login = {
107 username_or_email: "lemmy_alpha",
110 let resAlpha = alpha.client.login(formAlpha);
112 let formBeta: Login = {
113 username_or_email: "lemmy_beta",
116 let resBeta = beta.client.login(formBeta);
118 let formGamma: Login = {
119 username_or_email: "lemmy_gamma",
122 let resGamma = gamma.client.login(formGamma);
124 let formDelta: Login = {
125 username_or_email: "lemmy_delta",
128 let resDelta = delta.client.login(formDelta);
130 let formEpsilon: Login = {
131 username_or_email: "lemmy_epsilon",
134 let resEpsilon = epsilon.client.login(formEpsilon);
136 let res = await Promise.all([
144 alpha.auth = res[0].jwt ?? "";
145 beta.auth = res[1].jwt ?? "";
146 gamma.auth = res[2].jwt ?? "";
147 delta.auth = res[3].jwt ?? "";
148 epsilon.auth = res[4].jwt ?? "";
150 // Registration applications are now enabled by default, need to disable them
151 let editSiteForm: EditSite = {
152 registration_mode: "Open",
153 rate_limit_message: 999,
154 rate_limit_post: 999,
155 rate_limit_register: 999,
156 rate_limit_image: 999,
157 rate_limit_comment: 999,
158 rate_limit_search: 999,
162 // Set the blocks and auths for each
163 editSiteForm.auth = alpha.auth;
164 editSiteForm.allowed_instances = [
170 await alpha.client.editSite(editSiteForm);
172 editSiteForm.auth = beta.auth;
173 editSiteForm.allowed_instances = [
179 await beta.client.editSite(editSiteForm);
181 editSiteForm.auth = gamma.auth;
182 editSiteForm.allowed_instances = [
188 await gamma.client.editSite(editSiteForm);
190 editSiteForm.allowed_instances = ["lemmy-beta"];
191 editSiteForm.auth = delta.auth;
192 await delta.client.editSite(editSiteForm);
194 editSiteForm.auth = epsilon.auth;
195 editSiteForm.allowed_instances = [];
196 editSiteForm.blocked_instances = ["lemmy-alpha"];
197 await epsilon.client.editSite(editSiteForm);
199 // Create the main alpha/beta communities
200 // Ignore thrown errors of duplicates
202 await createCommunity(alpha, "main");
203 await createCommunity(beta, "main");
207 export async function createPost(
209 community_id: number,
210 ): Promise<PostResponse> {
211 let name = randomString(5);
212 let body = randomString(10);
213 let url = "https://google.com/";
214 let form: CreatePost = {
221 return api.client.createPost(form);
224 export async function editPost(api: API, post: Post): Promise<PostResponse> {
225 let name = "A jest test federated post, updated";
226 let form: EditPost = {
231 return api.client.editPost(form);
234 export async function deletePost(
238 ): Promise<PostResponse> {
239 let form: DeletePost = {
244 return api.client.deletePost(form);
247 export async function removePost(
251 ): Promise<PostResponse> {
252 let form: RemovePost = {
257 return api.client.removePost(form);
260 export async function featurePost(
264 ): Promise<PostResponse> {
265 let form: FeaturePost = {
268 feature_type: "Community",
271 return api.client.featurePost(form);
274 export async function lockPost(
278 ): Promise<PostResponse> {
279 let form: LockPost = {
284 return api.client.lockPost(form);
287 export async function resolvePost(
290 ): Promise<ResolveObjectResponse> {
291 let form: ResolveObject = {
295 return api.client.resolveObject(form);
298 export async function searchPostLocal(
301 ): Promise<SearchResponse> {
308 return api.client.search(form);
311 export async function getPost(
314 ): Promise<GetPostResponse> {
315 let form: GetPost = {
319 return api.client.getPost(form);
322 export async function getComments(
325 ): Promise<GetCommentsResponse> {
326 let form: GetComments = {
332 return api.client.getComments(form);
335 export async function getUnreadCount(
337 ): Promise<GetUnreadCountResponse> {
338 let form: GetUnreadCount = {
341 return api.client.getUnreadCount(form);
344 export async function getReplies(api: API): Promise<GetRepliesResponse> {
345 let form: GetReplies = {
350 return api.client.getReplies(form);
353 export async function resolveComment(
356 ): Promise<ResolveObjectResponse> {
357 let form: ResolveObject = {
361 return api.client.resolveObject(form);
364 export async function resolveBetaCommunity(
366 ): Promise<ResolveObjectResponse> {
367 // Use short-hand search url
368 let form: ResolveObject = {
369 q: "!main@lemmy-beta:8551",
372 return api.client.resolveObject(form);
375 export async function resolveCommunity(
378 ): Promise<ResolveObjectResponse> {
379 let form: ResolveObject = {
383 return api.client.resolveObject(form);
386 export async function resolvePerson(
389 ): Promise<ResolveObjectResponse> {
390 let form: ResolveObject = {
394 return api.client.resolveObject(form);
397 export async function banPersonFromSite(
401 remove_data: boolean,
402 ): Promise<BanPersonResponse> {
403 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
404 let form: BanPerson = {
407 remove_data: remove_data,
410 return api.client.banPerson(form);
413 export async function banPersonFromCommunity(
416 community_id: number,
417 remove_data: boolean,
419 ): Promise<BanFromCommunityResponse> {
420 let form: BanFromCommunity = {
423 remove_data: remove_data,
427 return api.client.banFromCommunity(form);
430 export async function followCommunity(
433 community_id: number,
434 ): Promise<CommunityResponse> {
435 let form: FollowCommunity = {
440 return api.client.followCommunity(form);
443 export async function likePost(
447 ): Promise<PostResponse> {
448 let form: CreatePostLike = {
454 return api.client.likePost(form);
457 export async function createComment(
461 content = "a jest test comment",
462 ): Promise<CommentResponse> {
463 let form: CreateComment = {
469 return api.client.createComment(form);
472 export async function editComment(
475 content = "A jest test federated comment update",
476 ): Promise<CommentResponse> {
477 let form: EditComment = {
482 return api.client.editComment(form);
485 export async function deleteComment(
489 ): Promise<CommentResponse> {
490 let form: DeleteComment = {
495 return api.client.deleteComment(form);
498 export async function removeComment(
502 ): Promise<CommentResponse> {
503 let form: RemoveComment = {
508 return api.client.removeComment(form);
511 export async function getMentions(
513 ): Promise<GetPersonMentionsResponse> {
514 let form: GetPersonMentions = {
519 return api.client.getPersonMentions(form);
522 export async function likeComment(
526 ): Promise<CommentResponse> {
527 let form: CreateCommentLike = {
528 comment_id: comment.id,
532 return api.client.likeComment(form);
535 export async function createCommunity(
537 name_: string = randomString(5),
538 ): Promise<CommunityResponse> {
539 let description = "a sample description";
540 let form: CreateCommunity = {
546 return api.client.createCommunity(form);
549 export async function getCommunity(
552 ): Promise<CommunityResponse> {
553 let form: GetCommunity = {
557 return api.client.getCommunity(form);
560 export async function deleteCommunity(
563 community_id: number,
564 ): Promise<CommunityResponse> {
565 let form: DeleteCommunity = {
570 return api.client.deleteCommunity(form);
573 export async function removeCommunity(
576 community_id: number,
577 ): Promise<CommunityResponse> {
578 let form: RemoveCommunity = {
583 return api.client.removeCommunity(form);
586 export async function createPrivateMessage(
588 recipient_id: number,
589 ): Promise<PrivateMessageResponse> {
590 let content = "A jest test federated private message";
591 let form: CreatePrivateMessage = {
596 return api.client.createPrivateMessage(form);
599 export async function editPrivateMessage(
601 private_message_id: number,
602 ): Promise<PrivateMessageResponse> {
603 let updatedContent = "A jest test federated private message edited";
604 let form: EditPrivateMessage = {
605 content: updatedContent,
609 return api.client.editPrivateMessage(form);
612 export async function deletePrivateMessage(
615 private_message_id: number,
616 ): Promise<PrivateMessageResponse> {
617 let form: DeletePrivateMessage = {
622 return api.client.deletePrivateMessage(form);
625 export async function registerUser(
627 username: string = randomString(5),
628 ): Promise<LoginResponse> {
629 let form: Register = {
632 password_verify: password,
635 return api.client.register(form);
638 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
639 let form: SaveUserSettings = {
644 default_sort_type: "Active",
645 default_listing_type: "All",
646 interface_language: "en",
648 send_notifications_to_email: false,
649 bio: "a changed bio",
652 return saveUserSettings(api, form);
655 export async function saveUserSettingsFederated(
657 ): Promise<LoginResponse> {
658 let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
659 let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
660 let bio = "a changed bio";
661 let form: SaveUserSettings = {
665 default_sort_type: "Hot",
666 default_listing_type: "All",
667 interface_language: "",
670 display_name: "user321",
672 send_notifications_to_email: false,
676 return await saveUserSettings(alpha, form);
679 export async function saveUserSettings(
681 form: SaveUserSettings,
682 ): Promise<LoginResponse> {
683 return api.client.saveUserSettings(form);
685 export async function getPersonDetails(
688 ): Promise<GetPersonDetailsResponse> {
689 let form: GetPersonDetails = {
691 person_id: person_id,
693 return api.client.getPersonDetails(form);
696 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
697 let form: DeleteAccount = {
701 return api.client.deleteAccount(form);
704 export async function getSite(api: API): Promise<GetSiteResponse> {
705 let form: GetSite = {
708 return api.client.getSite(form);
711 export async function listPrivateMessages(
713 ): Promise<PrivateMessagesResponse> {
714 let form: GetPrivateMessages = {
718 return api.client.getPrivateMessages(form);
721 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
722 // Unfollow all remote communities
723 let site = await getSite(api);
725 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
726 for (let cu of remoteFollowed) {
727 await followCommunity(api, false, cu.community.id);
729 let siteRes = await getSite(api);
733 export async function followBeta(api: API): Promise<CommunityResponse> {
734 let betaCommunity = (await resolveBetaCommunity(api)).community;
736 let follow = await followCommunity(api, true, betaCommunity.community.id);
739 return Promise.reject("no community worked");
743 export async function reportPost(
747 ): Promise<PostReportResponse> {
748 let form: CreatePostReport = {
753 return api.client.createPostReport(form);
756 export async function listPostReports(
758 ): Promise<ListPostReportsResponse> {
759 let form: ListPostReports = {
762 return api.client.listPostReports(form);
765 export async function reportComment(
769 ): Promise<CommentReportResponse> {
770 let form: CreateCommentReport = {
775 return api.client.createCommentReport(form);
778 export async function listCommentReports(
780 ): Promise<ListCommentReportsResponse> {
781 let form: ListCommentReports = {
784 return api.client.listCommentReports(form);
787 export function getPosts(
789 moderator_view = false,
790 ): Promise<GetPostsResponse> {
791 let form: GetPosts = {
795 return api.client.getPosts(form);
798 export function delay(millis = 500) {
799 return new Promise(resolve => setTimeout(resolve, millis));
802 export function longDelay() {
806 export function wrapper(form: any): string {
807 return JSON.stringify(form);
810 export function randomString(length: number): string {
813 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
814 var charactersLength = characters.length;
815 for (var i = 0; i < length; i++) {
816 result += characters.charAt(Math.floor(Math.random() * charactersLength));
821 export async function unfollows() {
822 await unfollowRemotes(alpha);
823 await unfollowRemotes(gamma);
824 await unfollowRemotes(delta);
825 await unfollowRemotes(epsilon);
828 export function getCommentParentId(comment: Comment): number | undefined {
829 let split = comment.path.split(".");
833 if (split.length > 1) {
834 return Number(split[split.length - 2]);