34 PrivateMessageResponse,
35 PrivateMessagesResponse,
36 GetPersonMentionsResponse,
46 BanFromCommunityResponse,
49 ResolveObjectResponse,
54 ListPostReportsResponse,
56 CommentReportResponse,
58 ListCommentReportsResponse,
60 DeleteAccountResponse,
68 } from "lemmy-js-client";
70 export interface API {
75 export let alpha: API = {
76 client: new LemmyHttp("http://127.0.0.1:8541"),
80 export let beta: API = {
81 client: new LemmyHttp("http://127.0.0.1:8551"),
85 export let gamma: API = {
86 client: new LemmyHttp("http://127.0.0.1:8561"),
90 export let delta: API = {
91 client: new LemmyHttp("http://127.0.0.1:8571"),
95 export let epsilon: API = {
96 client: new LemmyHttp("http://127.0.0.1:8581"),
100 const password = "lemmylemmy";
102 export async function setupLogins() {
103 let formAlpha: Login = {
104 username_or_email: "lemmy_alpha",
107 let resAlpha = alpha.client.login(formAlpha);
109 let formBeta: Login = {
110 username_or_email: "lemmy_beta",
113 let resBeta = beta.client.login(formBeta);
115 let formGamma: Login = {
116 username_or_email: "lemmy_gamma",
119 let resGamma = gamma.client.login(formGamma);
121 let formDelta: Login = {
122 username_or_email: "lemmy_delta",
125 let resDelta = delta.client.login(formDelta);
127 let formEpsilon: Login = {
128 username_or_email: "lemmy_epsilon",
131 let resEpsilon = epsilon.client.login(formEpsilon);
133 let res = await Promise.all([
141 alpha.auth = res[0].jwt ?? "";
142 beta.auth = res[1].jwt ?? "";
143 gamma.auth = res[2].jwt ?? "";
144 delta.auth = res[3].jwt ?? "";
145 epsilon.auth = res[4].jwt ?? "";
147 // Registration applications are now enabled by default, need to disable them
148 let editSiteForm: EditSite = {
149 registration_mode: RegistrationMode.Open,
150 federation_debug: true,
151 rate_limit_message: 999,
152 rate_limit_post: 999,
153 rate_limit_register: 999,
154 rate_limit_image: 999,
155 rate_limit_comment: 999,
156 rate_limit_search: 999,
160 // Set the blocks and auths for each
161 editSiteForm.auth = alpha.auth;
162 editSiteForm.allowed_instances = [
168 await alpha.client.editSite(editSiteForm);
170 editSiteForm.auth = beta.auth;
171 editSiteForm.allowed_instances = [
177 await beta.client.editSite(editSiteForm);
179 editSiteForm.auth = gamma.auth;
180 editSiteForm.allowed_instances = [
186 await gamma.client.editSite(editSiteForm);
188 editSiteForm.allowed_instances = ["lemmy-beta"];
189 editSiteForm.auth = delta.auth;
190 await delta.client.editSite(editSiteForm);
192 editSiteForm.auth = epsilon.auth;
193 editSiteForm.allowed_instances = [];
194 editSiteForm.blocked_instances = ["lemmy-alpha"];
195 await epsilon.client.editSite(editSiteForm);
197 // Create the main alpha/beta communities
198 await createCommunity(alpha, "main");
199 await createCommunity(beta, "main");
202 export async function createPost(
205 ): Promise<PostResponse> {
206 let name = randomString(5);
207 let body = randomString(10);
208 let url = "https://google.com/";
209 let form: CreatePost = {
216 return api.client.createPost(form);
219 export async function editPost(api: API, post: Post): Promise<PostResponse> {
220 let name = "A jest test federated post, updated";
221 let form: EditPost = {
226 return api.client.editPost(form);
229 export async function deletePost(
233 ): Promise<PostResponse> {
234 let form: DeletePost = {
239 return api.client.deletePost(form);
242 export async function removePost(
246 ): Promise<PostResponse> {
247 let form: RemovePost = {
252 return api.client.removePost(form);
255 export async function featurePost(
259 ): Promise<PostResponse> {
260 let form: FeaturePost = {
263 feature_type: PostFeatureType.Community,
266 return api.client.featurePost(form);
269 export async function lockPost(
273 ): Promise<PostResponse> {
274 let form: LockPost = {
279 return api.client.lockPost(form);
282 export async function resolvePost(
285 ): Promise<ResolveObjectResponse> {
286 let form: ResolveObject = {
290 return api.client.resolveObject(form);
293 export async function searchPostLocal(
296 ): Promise<SearchResponse> {
299 type_: SearchType.Posts,
300 sort: SortType.TopAll,
303 return api.client.search(form);
306 export async function getPost(
309 ): Promise<GetPostResponse> {
310 let form: GetPost = {
314 return api.client.getPost(form);
317 export async function getComments(
320 ): Promise<GetCommentsResponse> {
321 let form: GetComments = {
323 type_: ListingType.All,
324 sort: CommentSortType.New,
327 return api.client.getComments(form);
330 export async function resolveComment(
333 ): Promise<ResolveObjectResponse> {
334 let form: ResolveObject = {
338 return api.client.resolveObject(form);
341 export async function resolveBetaCommunity(
343 ): Promise<ResolveObjectResponse> {
344 // Use short-hand search url
345 let form: ResolveObject = {
346 q: "!main@lemmy-beta:8551",
349 return api.client.resolveObject(form);
352 export async function resolveCommunity(
355 ): Promise<ResolveObjectResponse> {
356 let form: ResolveObject = {
360 return api.client.resolveObject(form);
363 export async function resolvePerson(
366 ): Promise<ResolveObjectResponse> {
367 let form: ResolveObject = {
371 return api.client.resolveObject(form);
374 export async function banPersonFromSite(
379 ): Promise<BanPersonResponse> {
380 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
381 let form: BanPerson = {
384 remove_data: remove_data,
387 return api.client.banPerson(form);
390 export async function banPersonFromCommunity(
393 community_id: number,
394 remove_data: boolean,
396 ): Promise<BanFromCommunityResponse> {
397 let form: BanFromCommunity = {
400 remove_data: remove_data,
404 return api.client.banFromCommunity(form);
407 export async function followCommunity(
411 ): Promise<CommunityResponse> {
412 let form: FollowCommunity = {
417 return api.client.followCommunity(form);
420 export async function likePost(
424 ): Promise<PostResponse> {
425 let form: CreatePostLike = {
431 return api.client.likePost(form);
434 export async function createComment(
438 content = "a jest test comment"
439 ): Promise<CommentResponse> {
440 let form: CreateComment = {
446 return api.client.createComment(form);
449 export async function editComment(
452 content = "A jest test federated comment update"
453 ): Promise<CommentResponse> {
454 let form: EditComment = {
459 return api.client.editComment(form);
462 export async function deleteComment(
466 ): Promise<CommentResponse> {
467 let form: DeleteComment = {
472 return api.client.deleteComment(form);
475 export async function removeComment(
479 ): Promise<CommentResponse> {
480 let form: RemoveComment = {
485 return api.client.removeComment(form);
488 export async function getMentions(
490 ): Promise<GetPersonMentionsResponse> {
491 let form: GetPersonMentions = {
492 sort: CommentSortType.New,
496 return api.client.getPersonMentions(form);
499 export async function likeComment(
503 ): Promise<CommentResponse> {
504 let form: CreateCommentLike = {
505 comment_id: comment.id,
509 return api.client.likeComment(form);
512 export async function createCommunity(
514 name_: string = randomString(5)
515 ): Promise<CommunityResponse> {
516 let description = "a sample description";
517 let form: CreateCommunity = {
523 return api.client.createCommunity(form);
526 export async function getCommunity(
529 ): Promise<CommunityResponse> {
530 let form: GetCommunity = {
534 return api.client.getCommunity(form);
537 export async function deleteCommunity(
541 ): Promise<CommunityResponse> {
542 let form: DeleteCommunity = {
547 return api.client.deleteCommunity(form);
550 export async function removeCommunity(
554 ): Promise<CommunityResponse> {
555 let form: RemoveCommunity = {
560 return api.client.removeCommunity(form);
563 export async function createPrivateMessage(
566 ): Promise<PrivateMessageResponse> {
567 let content = "A jest test federated private message";
568 let form: CreatePrivateMessage = {
573 return api.client.createPrivateMessage(form);
576 export async function editPrivateMessage(
578 private_message_id: number
579 ): Promise<PrivateMessageResponse> {
580 let updatedContent = "A jest test federated private message edited";
581 let form: EditPrivateMessage = {
582 content: updatedContent,
586 return api.client.editPrivateMessage(form);
589 export async function deletePrivateMessage(
592 private_message_id: number
593 ): Promise<PrivateMessageResponse> {
594 let form: DeletePrivateMessage = {
599 return api.client.deletePrivateMessage(form);
602 export async function registerUser(
604 username: string = randomString(5)
605 ): Promise<LoginResponse> {
606 let form: Register = {
609 password_verify: password,
612 return api.client.register(form);
615 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
616 let form: SaveUserSettings = {
619 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
620 default_listing_type: Object.keys(ListingType).indexOf(ListingType.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 = {
638 default_sort_type: Object.keys(SortType).indexOf(SortType.Hot),
639 default_listing_type: Object.keys(ListingType).indexOf(ListingType.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);
659 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
660 let form: DeleteAccount = {
664 return api.client.deleteAccount(form);
667 export async function getSite(api: API): Promise<GetSiteResponse> {
668 let form: GetSite = {
671 return api.client.getSite(form);
674 export async function listPrivateMessages(
676 ): Promise<PrivateMessagesResponse> {
677 let form: GetPrivateMessages = {
681 return api.client.getPrivateMessages(form);
684 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
685 // Unfollow all remote communities
686 let site = await getSite(api);
688 site.my_user?.follows.filter(c => c.community.local == false) ?? [];
689 for (let cu of remoteFollowed) {
690 await followCommunity(api, false, cu.community.id);
692 let siteRes = await getSite(api);
696 export async function followBeta(api: API): Promise<CommunityResponse> {
697 let betaCommunity = (await resolveBetaCommunity(api)).community;
699 let follow = await followCommunity(api, true, betaCommunity.community.id);
702 return Promise.reject("no community worked");
706 export async function reportPost(
710 ): Promise<PostReportResponse> {
711 let form: CreatePostReport = {
716 return api.client.createPostReport(form);
719 export async function listPostReports(
721 ): Promise<ListPostReportsResponse> {
722 let form: ListPostReports = {
725 return api.client.listPostReports(form);
728 export async function reportComment(
732 ): Promise<CommentReportResponse> {
733 let form: CreateCommentReport = {
738 return api.client.createCommentReport(form);
741 export async function listCommentReports(
743 ): Promise<ListCommentReportsResponse> {
744 let form: ListCommentReports = {
747 return api.client.listCommentReports(form);
750 export function delay(millis = 500) {
751 return new Promise(resolve => setTimeout(resolve, millis));
754 export function longDelay() {
758 export function wrapper(form: any): string {
759 return JSON.stringify(form);
762 export function randomString(length: number): string {
765 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
766 var charactersLength = characters.length;
767 for (var i = 0; i < length; i++) {
768 result += characters.charAt(Math.floor(Math.random() * charactersLength));
773 export async function unfollows() {
774 await unfollowRemotes(alpha);
775 await unfollowRemotes(gamma);
776 await unfollowRemotes(delta);
777 await unfollowRemotes(epsilon);
780 export function getCommentParentId(comment: Comment): number | undefined {
781 let split = comment.path.split(".");
785 if (split.length > 1) {
786 return Number(split[split.length - 2]);