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({
153 enable_downvotes: None,
154 open_registration: None,
156 community_creation_admin_only: None,
157 require_email_verification: None,
158 require_application: Some(false),
159 application_question: None,
160 private_instance: None,
162 legal_information: None,
163 default_post_listing_type: None,
166 editSiteForm.auth = alpha.auth.unwrap();
167 await alpha.client.editSite(editSiteForm);
168 editSiteForm.auth = beta.auth.unwrap();
169 await beta.client.editSite(editSiteForm);
170 editSiteForm.auth = gamma.auth.unwrap();
171 await gamma.client.editSite(editSiteForm);
172 editSiteForm.auth = delta.auth.unwrap();
173 await delta.client.editSite(editSiteForm);
174 editSiteForm.auth = epsilon.auth.unwrap();
175 await epsilon.client.editSite(editSiteForm);
177 // Create the main alpha/beta communities
178 await createCommunity(alpha, "main");
179 await createCommunity(beta, "main");
182 export async function createPost(
185 ): Promise<PostResponse> {
186 let name = randomString(5);
187 let body = Some(randomString(10));
188 let url = Some('https://google.com/');
189 let form = new CreatePost({
193 auth: api.auth.unwrap(),
198 return api.client.createPost(form);
201 export async function editPost(api: API, post: Post): Promise<PostResponse> {
202 let name = Some('A jest test federated post, updated');
203 let form = new EditPost({
206 auth: api.auth.unwrap(),
211 return api.client.editPost(form);
214 export async function deletePost(
218 ): Promise<PostResponse> {
219 let form = new DeletePost({
222 auth: api.auth.unwrap(),
224 return api.client.deletePost(form);
227 export async function removePost(
231 ): Promise<PostResponse> {
232 let form = new RemovePost({
235 auth: api.auth.unwrap(),
238 return api.client.removePost(form);
241 export async function stickyPost(
245 ): Promise<PostResponse> {
246 let form = new StickyPost({
249 auth: api.auth.unwrap(),
251 return api.client.stickyPost(form);
254 export async function lockPost(
258 ): Promise<PostResponse> {
259 let form = new LockPost({
262 auth: api.auth.unwrap(),
264 return api.client.lockPost(form);
267 export async function resolvePost(
270 ): Promise<ResolveObjectResponse> {
271 let form = new ResolveObject({
275 return api.client.resolveObject(form);
278 export async function searchPostLocal(
281 ): Promise<SearchResponse> {
282 let form = new Search({
284 type_: Some(SearchType.Posts),
285 sort: Some(SortType.TopAll),
287 community_name: None,
294 return api.client.search(form);
297 export async function getPost(
300 ): Promise<GetPostResponse> {
301 let form = new GetPost({
306 return api.client.getPost(form);
309 export async function getComments(
312 ): Promise<GetCommentsResponse> {
313 let form = new GetComments({
314 post_id: Some(post_id),
315 type_: Some(ListingType.All),
316 sort: Some(CommentSortType.New), // TODO this sort might be wrong
321 community_name: None,
326 return api.client.getComments(form);
329 export async function resolveComment(
332 ): Promise<ResolveObjectResponse> {
333 let form = new ResolveObject({
337 return api.client.resolveObject(form);
340 export async function resolveBetaCommunity(
342 ): Promise<ResolveObjectResponse> {
343 // Use short-hand search url
344 let form = new ResolveObject({
345 q: '!main@lemmy-beta:8551',
348 return api.client.resolveObject(form);
351 export async function resolveCommunity(
354 ): Promise<ResolveObjectResponse> {
355 let form = new ResolveObject({
359 return api.client.resolveObject(form);
362 export async function resolvePerson(
365 ): Promise<ResolveObjectResponse> {
366 let form = new ResolveObject({
370 return api.client.resolveObject(form);
373 export async function banPersonFromSite(
378 ): Promise<BanPersonResponse> {
379 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
380 let form = new BanPerson({
383 remove_data: Some(remove_data),
384 auth: api.auth.unwrap(),
388 return api.client.banPerson(form);
391 export async function banPersonFromCommunity(
394 community_id: number,
395 remove_data: boolean,
397 ): Promise<BanFromCommunityResponse> {
398 let form = new BanFromCommunity({
401 remove_data: Some(remove_data),
405 auth: api.auth.unwrap(),
407 return api.client.banFromCommunity(form);
410 export async function followCommunity(
414 ): Promise<CommunityResponse> {
415 let form = new FollowCommunity({
418 auth: api.auth.unwrap()
420 return api.client.followCommunity(form);
423 export async function likePost(
427 ): Promise<PostResponse> {
428 let form = new CreatePostLike({
431 auth: api.auth.unwrap()
434 return api.client.likePost(form);
437 export async function createComment(
440 parent_id: Option<number>,
441 content = 'a jest test comment'
442 ): Promise<CommentResponse> {
443 let form = new CreateComment({
448 auth: api.auth.unwrap(),
450 return api.client.createComment(form);
453 export async function editComment(
456 content = 'A jest test federated comment update'
457 ): Promise<CommentResponse> {
458 let form = new EditComment({
462 auth: api.auth.unwrap()
464 return api.client.editComment(form);
467 export async function deleteComment(
471 ): Promise<CommentResponse> {
472 let form = new DeleteComment({
475 auth: api.auth.unwrap(),
477 return api.client.deleteComment(form);
480 export async function removeComment(
484 ): Promise<CommentResponse> {
485 let form = new RemoveComment({
489 auth: api.auth.unwrap(),
491 return api.client.removeComment(form);
494 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
495 let form = new GetPersonMentions({
496 sort: Some(CommentSortType.New),
497 unread_only: Some(false),
498 auth: api.auth.unwrap(),
502 return api.client.getPersonMentions(form);
505 export async function likeComment(
509 ): Promise<CommentResponse> {
510 let form = new CreateCommentLike({
511 comment_id: comment.id,
513 auth: api.auth.unwrap(),
515 return api.client.likeComment(form);
518 export async function createCommunity(
520 name_: string = randomString(5)
521 ): Promise<CommunityResponse> {
522 let description = Some('a sample description');
523 let form = new CreateCommunity({
530 posting_restricted_to_mods: None,
531 auth: api.auth.unwrap(),
533 return api.client.createCommunity(form);
536 export async function getCommunity(
539 ): Promise<CommunityResponse> {
540 let form = new GetCommunity({
545 return api.client.getCommunity(form);
548 export async function deleteCommunity(
552 ): Promise<CommunityResponse> {
553 let form = new DeleteCommunity({
556 auth: api.auth.unwrap(),
558 return api.client.deleteCommunity(form);
561 export async function removeCommunity(
565 ): Promise<CommunityResponse> {
566 let form = new RemoveCommunity({
571 auth: api.auth.unwrap(),
573 return api.client.removeCommunity(form);
576 export async function createPrivateMessage(
579 ): Promise<PrivateMessageResponse> {
580 let content = 'A jest test federated private message';
581 let form = new CreatePrivateMessage({
584 auth: api.auth.unwrap(),
586 return api.client.createPrivateMessage(form);
589 export async function editPrivateMessage(
591 private_message_id: number
592 ): Promise<PrivateMessageResponse> {
593 let updatedContent = 'A jest test federated private message edited';
594 let form = new EditPrivateMessage({
595 content: updatedContent,
597 auth: api.auth.unwrap(),
599 return api.client.editPrivateMessage(form);
602 export async function deletePrivateMessage(
605 private_message_id: number
606 ): Promise<PrivateMessageResponse> {
607 let form = new DeletePrivateMessage({
610 auth: api.auth.unwrap(),
612 return api.client.deletePrivateMessage(form);
615 export async function registerUser(
617 username: string = randomString(5)
618 ): Promise<LoginResponse> {
619 let form = new Register({
622 password_verify: password,
626 captcha_answer: None,
630 return api.client.register(form);
633 export async function saveUserSettingsBio(
635 ): Promise<LoginResponse> {
636 let form = new SaveUserSettings({
637 show_nsfw: Some(true),
638 theme: Some('darkly'),
639 default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Active)),
640 default_listing_type: Some(Object.keys(ListingType).indexOf(ListingType.All)),
642 show_avatars: Some(true),
643 send_notifications_to_email: Some(false),
644 bio: Some('a changed bio'),
649 matrix_user_id: None,
651 show_read_posts: None,
652 show_bot_accounts: None,
653 show_new_post_notifs: None,
655 auth: api.auth.unwrap(),
657 return saveUserSettings(api, form);
660 export async function saveUserSettingsFederated(
662 ): Promise<LoginResponse> {
663 let avatar = Some('https://image.flaticon.com/icons/png/512/35/35896.png');
664 let banner = Some('https://image.flaticon.com/icons/png/512/36/35896.png');
665 let bio = Some('a changed bio');
666 let form = new SaveUserSettings({
667 show_nsfw: Some(false),
669 default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Hot)),
670 default_listing_type: Some(Object.keys(ListingType).indexOf(ListingType.All)),
674 display_name: Some('user321'),
675 show_avatars: Some(false),
676 send_notifications_to_email: Some(false),
680 show_read_posts: None,
681 matrix_user_id: None,
683 show_bot_accounts: None,
684 show_new_post_notifs: None,
685 auth: api.auth.unwrap(),
687 return await saveUserSettings(alpha, form);
690 export async function saveUserSettings(
692 form: SaveUserSettings
693 ): Promise<LoginResponse> {
694 return api.client.saveUserSettings(form);
697 export async function deleteUser(
699 ): Promise<DeleteAccountResponse> {
700 let form = new DeleteAccount({
701 auth: api.auth.unwrap(),
704 return api.client.deleteAccount(form);
707 export async function getSite(
709 ): Promise<GetSiteResponse> {
710 let form = new GetSite({
713 return api.client.getSite(form);
716 export async function listPrivateMessages(
718 ): Promise<PrivateMessagesResponse> {
719 let form = new GetPrivateMessages({
720 auth: api.auth.unwrap(),
721 unread_only: Some(false),
725 return api.client.getPrivateMessages(form);
728 export async function unfollowRemotes(
730 ): Promise<GetSiteResponse> {
731 // Unfollow all remote communities
732 let site = await getSite(api);
733 let remoteFollowed = site.my_user.unwrap().follows.filter(
734 c => c.community.local == false
736 for (let cu of remoteFollowed) {
737 await followCommunity(api, false, cu.community.id);
739 let siteRes = await getSite(api);
743 export async function followBeta(api: API): Promise<CommunityResponse> {
744 let betaCommunity = (await resolveBetaCommunity(api)).community;
745 if (betaCommunity.isSome()) {
746 let follow = await followCommunity(api, true, betaCommunity.unwrap().community.id);
749 return Promise.reject("no community worked");
753 export async function reportPost(
757 ): Promise<PostReportResponse> {
758 let form = new CreatePostReport({
761 auth: api.auth.unwrap(),
763 return api.client.createPostReport(form);
766 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
767 let form = new ListPostReports({
768 auth: api.auth.unwrap(),
772 unresolved_only: None,
774 return api.client.listPostReports(form);
777 export async function reportComment(
781 ): Promise<CommentReportResponse> {
782 let form = new CreateCommentReport({
785 auth: api.auth.unwrap(),
787 return api.client.createCommentReport(form);
790 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
791 let form = new ListCommentReports({
795 unresolved_only: None,
796 auth: api.auth.unwrap(),
798 return api.client.listCommentReports(form);
801 export function delay(millis: number = 500) {
802 return new Promise(resolve => setTimeout(resolve, millis));
805 export function longDelay() {
809 export function wrapper(form: any): string {
810 return JSON.stringify(form);
813 export function randomString(length: number): string {
815 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
816 var charactersLength = characters.length;
817 for (var i = 0; i < length; i++) {
818 result += characters.charAt(Math.floor(Math.random() * charactersLength));
823 export async function unfollows() {
824 await unfollowRemotes(alpha);
825 await unfollowRemotes(gamma);
826 await unfollowRemotes(delta);
827 await unfollowRemotes(epsilon);
830 export function getCommentParentId(comment: Comment): Option<number> {
831 let split = comment.path.split(".");
835 if (split.length > 1) {
836 return Some(Number(split[split.length - 2]));