1 import { None, Some, Option } from "@sniptt/monads";
36 PrivateMessageResponse,
37 PrivateMessagesResponse,
38 GetPersonMentionsResponse,
48 BanFromCommunityResponse,
51 ResolveObjectResponse,
56 ListPostReportsResponse,
58 CommentReportResponse,
60 ListCommentReportsResponse,
62 DeleteAccountResponse,
67 } from "lemmy-js-client";
69 export interface API {
74 export let alpha: API = {
75 client: new LemmyHttp("http://127.0.0.1:8541"),
79 export let beta: API = {
80 client: new LemmyHttp("http://127.0.0.1:8551"),
84 export let gamma: API = {
85 client: new LemmyHttp("http://127.0.0.1:8561"),
89 export let delta: API = {
90 client: new LemmyHttp("http://127.0.0.1:8571"),
94 export let epsilon: API = {
95 client: new LemmyHttp("http://127.0.0.1:8581"),
99 const password = "lemmylemmy";
101 export async function setupLogins() {
102 let formAlpha = new Login({
103 username_or_email: "lemmy_alpha",
106 let resAlpha = alpha.client.login(formAlpha);
108 let formBeta = new Login({
109 username_or_email: "lemmy_beta",
112 let resBeta = beta.client.login(formBeta);
114 let formGamma = new Login({
115 username_or_email: "lemmy_gamma",
118 let resGamma = gamma.client.login(formGamma);
120 let formDelta = new Login({
121 username_or_email: "lemmy_delta",
124 let resDelta = delta.client.login(formDelta);
126 let formEpsilon = new Login({
127 username_or_email: "lemmy_epsilon",
130 let resEpsilon = epsilon.client.login(formEpsilon);
132 let res = await Promise.all([
140 alpha.auth = res[0].jwt;
141 beta.auth = res[1].jwt;
142 gamma.auth = res[2].jwt;
143 delta.auth = res[3].jwt;
144 epsilon.auth = res[4].jwt;
146 // Registration applications are now enabled by default, need to disable them
147 let editSiteForm = new EditSite({
148 require_application: Some(false),
149 federation_debug: Some(true),
155 enable_downvotes: None,
156 open_registration: None,
158 community_creation_admin_only: None,
159 require_email_verification: None,
160 application_question: None,
161 private_instance: None,
163 default_post_listing_type: None,
164 legal_information: None,
165 application_email_admins: None,
166 hide_modlog_mod_names: None,
167 discussion_languages: None,
168 slur_filter_regex: None,
169 actor_name_max_length: None,
170 rate_limit_message: Some(999),
171 rate_limit_message_per_second: None,
172 rate_limit_post: Some(999),
173 rate_limit_post_per_second: None,
174 rate_limit_register: Some(999),
175 rate_limit_register_per_second: None,
176 rate_limit_image: Some(999),
177 rate_limit_image_per_second: None,
178 rate_limit_comment: Some(999),
179 rate_limit_comment_per_second: None,
180 rate_limit_search: Some(999),
181 rate_limit_search_per_second: None,
182 federation_enabled: None,
183 federation_strict_allowlist: None,
184 federation_http_fetch_retry_limit: None,
185 federation_worker_count: None,
186 captcha_enabled: None,
187 captcha_difficulty: None,
188 allowed_instances: None,
189 blocked_instances: None,
193 // Set the blocks and auths for each
194 editSiteForm.auth = alpha.auth.unwrap();
195 editSiteForm.allowed_instances = Some([
201 await alpha.client.editSite(editSiteForm);
203 editSiteForm.auth = beta.auth.unwrap();
204 editSiteForm.allowed_instances = Some([
210 await beta.client.editSite(editSiteForm);
212 editSiteForm.auth = gamma.auth.unwrap();
213 editSiteForm.allowed_instances = Some([
219 await gamma.client.editSite(editSiteForm);
221 editSiteForm.allowed_instances = Some(["lemmy-beta"]);
222 editSiteForm.auth = delta.auth.unwrap();
223 await delta.client.editSite(editSiteForm);
225 editSiteForm.auth = epsilon.auth.unwrap();
226 editSiteForm.allowed_instances = Some([]);
227 editSiteForm.blocked_instances = Some(["lemmy-alpha"]);
228 await epsilon.client.editSite(editSiteForm);
230 // Create the main alpha/beta communities
231 await createCommunity(alpha, "main");
232 await createCommunity(beta, "main");
235 export async function createPost(
238 ): Promise<PostResponse> {
239 let name = randomString(5);
240 let body = Some(randomString(10));
241 let url = Some("https://google.com/");
242 let form = new CreatePost({
246 auth: api.auth.unwrap(),
252 return api.client.createPost(form);
255 export async function editPost(api: API, post: Post): Promise<PostResponse> {
256 let name = Some("A jest test federated post, updated");
257 let form = new EditPost({
260 auth: api.auth.unwrap(),
266 return api.client.editPost(form);
269 export async function deletePost(
273 ): Promise<PostResponse> {
274 let form = new DeletePost({
277 auth: api.auth.unwrap(),
279 return api.client.deletePost(form);
282 export async function removePost(
286 ): Promise<PostResponse> {
287 let form = new RemovePost({
290 auth: api.auth.unwrap(),
293 return api.client.removePost(form);
296 export async function stickyPost(
300 ): Promise<PostResponse> {
301 let form = new StickyPost({
304 auth: api.auth.unwrap(),
306 return api.client.stickyPost(form);
309 export async function lockPost(
313 ): Promise<PostResponse> {
314 let form = new LockPost({
317 auth: api.auth.unwrap(),
319 return api.client.lockPost(form);
322 export async function resolvePost(
325 ): Promise<ResolveObjectResponse> {
326 let form = new ResolveObject({
330 return api.client.resolveObject(form);
333 export async function searchPostLocal(
336 ): Promise<SearchResponse> {
337 let form = new Search({
339 type_: Some(SearchType.Posts),
340 sort: Some(SortType.TopAll),
342 community_name: None,
349 return api.client.search(form);
352 export async function getPost(
355 ): Promise<GetPostResponse> {
356 let form = new GetPost({
361 return api.client.getPost(form);
364 export async function getComments(
367 ): Promise<GetCommentsResponse> {
368 let form = new GetComments({
369 post_id: Some(post_id),
370 type_: Some(ListingType.All),
371 sort: Some(CommentSortType.New), // TODO this sort might be wrong
376 community_name: None,
381 return api.client.getComments(form);
384 export async function resolveComment(
387 ): Promise<ResolveObjectResponse> {
388 let form = new ResolveObject({
392 return api.client.resolveObject(form);
395 export async function resolveBetaCommunity(
397 ): Promise<ResolveObjectResponse> {
398 // Use short-hand search url
399 let form = new ResolveObject({
400 q: "!main@lemmy-beta:8551",
403 return api.client.resolveObject(form);
406 export async function resolveCommunity(
409 ): Promise<ResolveObjectResponse> {
410 let form = new ResolveObject({
414 return api.client.resolveObject(form);
417 export async function resolvePerson(
420 ): Promise<ResolveObjectResponse> {
421 let form = new ResolveObject({
425 return api.client.resolveObject(form);
428 export async function banPersonFromSite(
433 ): Promise<BanPersonResponse> {
434 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
435 let form = new BanPerson({
438 remove_data: Some(remove_data),
439 auth: api.auth.unwrap(),
443 return api.client.banPerson(form);
446 export async function banPersonFromCommunity(
449 community_id: number,
450 remove_data: boolean,
452 ): Promise<BanFromCommunityResponse> {
453 let form = new BanFromCommunity({
456 remove_data: Some(remove_data),
460 auth: api.auth.unwrap(),
462 return api.client.banFromCommunity(form);
465 export async function followCommunity(
469 ): Promise<CommunityResponse> {
470 let form = new FollowCommunity({
473 auth: api.auth.unwrap(),
475 return api.client.followCommunity(form);
478 export async function likePost(
482 ): Promise<PostResponse> {
483 let form = new CreatePostLike({
486 auth: api.auth.unwrap(),
489 return api.client.likePost(form);
492 export async function createComment(
495 parent_id: Option<number>,
496 content = "a jest test comment"
497 ): Promise<CommentResponse> {
498 let form = new CreateComment({
504 auth: api.auth.unwrap(),
506 return api.client.createComment(form);
509 export async function editComment(
512 content = Some("A jest test federated comment update")
513 ): Promise<CommentResponse> {
514 let form = new EditComment({
520 auth: api.auth.unwrap(),
522 return api.client.editComment(form);
525 export async function deleteComment(
529 ): Promise<CommentResponse> {
530 let form = new DeleteComment({
533 auth: api.auth.unwrap(),
535 return api.client.deleteComment(form);
538 export async function removeComment(
542 ): Promise<CommentResponse> {
543 let form = new RemoveComment({
547 auth: api.auth.unwrap(),
549 return api.client.removeComment(form);
552 export async function getMentions(
554 ): Promise<GetPersonMentionsResponse> {
555 let form = new GetPersonMentions({
556 sort: Some(CommentSortType.New),
557 unread_only: Some(false),
558 auth: api.auth.unwrap(),
562 return api.client.getPersonMentions(form);
565 export async function likeComment(
569 ): Promise<CommentResponse> {
570 let form = new CreateCommentLike({
571 comment_id: comment.id,
573 auth: api.auth.unwrap(),
575 return api.client.likeComment(form);
578 export async function createCommunity(
580 name_: string = randomString(5)
581 ): Promise<CommunityResponse> {
582 let description = Some("a sample description");
583 let form = new CreateCommunity({
590 posting_restricted_to_mods: None,
591 auth: api.auth.unwrap(),
593 return api.client.createCommunity(form);
596 export async function getCommunity(
599 ): Promise<CommunityResponse> {
600 let form = new GetCommunity({
605 return api.client.getCommunity(form);
608 export async function deleteCommunity(
612 ): Promise<CommunityResponse> {
613 let form = new DeleteCommunity({
616 auth: api.auth.unwrap(),
618 return api.client.deleteCommunity(form);
621 export async function removeCommunity(
625 ): Promise<CommunityResponse> {
626 let form = new RemoveCommunity({
631 auth: api.auth.unwrap(),
633 return api.client.removeCommunity(form);
636 export async function createPrivateMessage(
639 ): Promise<PrivateMessageResponse> {
640 let content = "A jest test federated private message";
641 let form = new CreatePrivateMessage({
644 auth: api.auth.unwrap(),
646 return api.client.createPrivateMessage(form);
649 export async function editPrivateMessage(
651 private_message_id: number
652 ): Promise<PrivateMessageResponse> {
653 let updatedContent = "A jest test federated private message edited";
654 let form = new EditPrivateMessage({
655 content: updatedContent,
657 auth: api.auth.unwrap(),
659 return api.client.editPrivateMessage(form);
662 export async function deletePrivateMessage(
665 private_message_id: number
666 ): Promise<PrivateMessageResponse> {
667 let form = new DeletePrivateMessage({
670 auth: api.auth.unwrap(),
672 return api.client.deletePrivateMessage(form);
675 export async function registerUser(
677 username: string = randomString(5)
678 ): Promise<LoginResponse> {
679 let form = new Register({
682 password_verify: password,
686 captcha_answer: None,
690 return api.client.register(form);
693 export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
694 let form = new SaveUserSettings({
695 show_nsfw: Some(true),
696 theme: Some("darkly"),
697 default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Active)),
698 default_listing_type: Some(
699 Object.keys(ListingType).indexOf(ListingType.All)
701 interface_language: Some("en"),
702 show_avatars: Some(true),
703 send_notifications_to_email: Some(false),
704 bio: Some("a changed bio"),
709 matrix_user_id: None,
711 show_read_posts: None,
712 show_bot_accounts: None,
713 show_new_post_notifs: None,
715 discussion_languages: None,
716 auth: api.auth.unwrap(),
718 return saveUserSettings(api, form);
721 export async function saveUserSettingsFederated(
723 ): Promise<LoginResponse> {
724 let avatar = Some("https://image.flaticon.com/icons/png/512/35/35896.png");
725 let banner = Some("https://image.flaticon.com/icons/png/512/36/35896.png");
726 let bio = Some("a changed bio");
727 let form = new SaveUserSettings({
728 show_nsfw: Some(false),
730 default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Hot)),
731 default_listing_type: Some(
732 Object.keys(ListingType).indexOf(ListingType.All)
734 interface_language: Some(""),
737 display_name: Some("user321"),
738 show_avatars: Some(false),
739 send_notifications_to_email: Some(false),
743 show_read_posts: None,
744 matrix_user_id: None,
746 show_bot_accounts: None,
747 show_new_post_notifs: None,
748 discussion_languages: None,
749 auth: api.auth.unwrap(),
751 return await saveUserSettings(alpha, form);
754 export async function saveUserSettings(
756 form: SaveUserSettings
757 ): Promise<LoginResponse> {
758 return api.client.saveUserSettings(form);
761 export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
762 let form = new DeleteAccount({
763 auth: api.auth.unwrap(),
766 return api.client.deleteAccount(form);
769 export async function getSite(api: API): Promise<GetSiteResponse> {
770 let form = new GetSite({
773 return api.client.getSite(form);
776 export async function listPrivateMessages(
778 ): Promise<PrivateMessagesResponse> {
779 let form = new GetPrivateMessages({
780 auth: api.auth.unwrap(),
781 unread_only: Some(false),
785 return api.client.getPrivateMessages(form);
788 export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
789 // Unfollow all remote communities
790 let site = await getSite(api);
791 let remoteFollowed = site.my_user
793 .follows.filter(c => c.community.local == false);
794 for (let cu of remoteFollowed) {
795 await followCommunity(api, false, cu.community.id);
797 let siteRes = await getSite(api);
801 export async function followBeta(api: API): Promise<CommunityResponse> {
802 let betaCommunity = (await resolveBetaCommunity(api)).community;
803 if (betaCommunity.isSome()) {
804 let follow = await followCommunity(
807 betaCommunity.unwrap().community.id
811 return Promise.reject("no community worked");
815 export async function reportPost(
819 ): Promise<PostReportResponse> {
820 let form = new CreatePostReport({
823 auth: api.auth.unwrap(),
825 return api.client.createPostReport(form);
828 export async function listPostReports(
830 ): Promise<ListPostReportsResponse> {
831 let form = new ListPostReports({
832 auth: api.auth.unwrap(),
836 unresolved_only: None,
838 return api.client.listPostReports(form);
841 export async function reportComment(
845 ): Promise<CommentReportResponse> {
846 let form = new CreateCommentReport({
849 auth: api.auth.unwrap(),
851 return api.client.createCommentReport(form);
854 export async function listCommentReports(
856 ): Promise<ListCommentReportsResponse> {
857 let form = new ListCommentReports({
861 unresolved_only: None,
862 auth: api.auth.unwrap(),
864 return api.client.listCommentReports(form);
867 export function delay(millis = 500) {
868 return new Promise(resolve => setTimeout(resolve, millis));
871 export function longDelay() {
875 export function wrapper(form: any): string {
876 return JSON.stringify(form);
879 export function randomString(length: number): string {
882 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
883 var charactersLength = characters.length;
884 for (var i = 0; i < length; i++) {
885 result += characters.charAt(Math.floor(Math.random() * charactersLength));
890 export async function unfollows() {
891 await unfollowRemotes(alpha);
892 await unfollowRemotes(gamma);
893 await unfollowRemotes(delta);
894 await unfollowRemotes(epsilon);
897 export function getCommentParentId(comment: Comment): Option<number> {
898 let split = comment.path.split(".");
902 if (split.length > 1) {
903 return Some(Number(split[split.length - 2]));