1 import fetch from 'node-fetch';
17 GetFollowedCommunitiesResponse,
30 EditPrivateMessageForm,
31 DeletePrivateMessageForm,
32 PrivateMessageResponse,
33 PrivateMessagesResponse,
34 GetUserMentionsResponse,
39 } from '../interfaces';
41 export interface API {
46 function apiUrl(api: API) {
47 return `${api.url}/api/v1`;
50 export let alpha: API = {
51 url: 'http://localhost:8540',
54 export let beta: API = {
55 url: 'http://localhost:8550',
58 export let gamma: API = {
59 url: 'http://localhost:8560',
62 export async function setupLogins() {
63 let form: LoginForm = {
64 username_or_email: 'lemmy_alpha',
68 let resA: Promise<LoginResponse> = fetch(`${apiUrl(alpha)}/user/login`, {
71 'Content-Type': 'application/json',
74 }).then(d => d.json());
77 username_or_email: 'lemmy_beta',
81 let resB: Promise<LoginResponse> = fetch(`${apiUrl(beta)}/user/login`, {
84 'Content-Type': 'application/json',
87 }).then(d => d.json());
90 username_or_email: 'lemmy_gamma',
94 let resG: Promise<LoginResponse> = fetch(`${apiUrl(gamma)}/user/login`, {
97 'Content-Type': 'application/json',
100 }).then(d => d.json());
102 let res = await Promise.all([resA, resB, resG]);
103 alpha.auth = res[0].jwt;
104 beta.auth = res[1].jwt;
105 gamma.auth = res[2].jwt;
108 export async function createPost(
111 ): Promise<PostResponse> {
112 let name = 'A jest test post';
113 let postForm: PostForm = {
120 let createPostRes: PostResponse = await fetch(`${apiUrl(api)}/post`, {
123 'Content-Type': 'application/json',
125 body: wrapper(postForm),
126 }).then(d => d.json());
127 return createPostRes;
130 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
131 let name = 'A jest test federated post, updated';
132 let postForm: PostForm = {
139 let updateResponse: PostResponse = await fetch(`${apiUrl(api)}/post`, {
142 'Content-Type': 'application/json',
144 body: wrapper(postForm),
145 }).then(d => d.json());
146 return updateResponse;
149 export async function deletePost(
153 ): Promise<PostResponse> {
154 let deletePostForm: DeletePostForm = {
160 let deletePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/delete`, {
163 'Content-Type': 'application/json',
165 body: wrapper(deletePostForm),
166 }).then(d => d.json());
167 return deletePostRes;
170 export async function removePost(
174 ): Promise<PostResponse> {
175 let removePostForm: RemovePostForm = {
181 let removePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/remove`, {
184 'Content-Type': 'application/json',
186 body: wrapper(removePostForm),
187 }).then(d => d.json());
188 return removePostRes;
191 export async function stickyPost(
195 ): Promise<PostResponse> {
196 let stickyPostForm: StickyPostForm = {
202 let stickyRes: PostResponse = await fetch(`${apiUrl(api)}/post/sticky`, {
205 'Content-Type': 'application/json',
207 body: wrapper(stickyPostForm),
208 }).then(d => d.json());
213 export async function lockPost(
217 ): Promise<PostResponse> {
218 let lockPostForm: LockPostForm = {
224 let lockRes: PostResponse = await fetch(`${apiUrl(api)}/post/lock`, {
227 'Content-Type': 'application/json',
229 body: wrapper(lockPostForm),
230 }).then(d => d.json());
235 export async function searchPost(
238 ): Promise<SearchResponse> {
239 let searchUrl = `${apiUrl(api)}/search?q=${post.ap_id}&type_=All&sort=TopAll`;
240 let searchResponse: SearchResponse = await fetch(searchUrl, {
242 }).then(d => d.json());
243 return searchResponse;
246 export async function getPost(
249 ): Promise<GetPostResponse> {
250 let getPostUrl = `${apiUrl(api)}/post?id=${post_id}`;
251 let getPostRes: GetPostResponse = await fetch(getPostUrl, {
253 }).then(d => d.json());
258 export async function searchComment(
261 ): Promise<SearchResponse> {
262 let searchUrl = `${apiUrl(api)}/search?q=${
264 }&type_=All&sort=TopAll`;
265 let searchResponse: SearchResponse = await fetch(searchUrl, {
267 }).then(d => d.json());
268 return searchResponse;
271 export async function searchForBetaCommunity(
273 ): Promise<SearchResponse> {
274 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
275 // Use short-hand search url
276 let searchUrl = `${apiUrl(
278 )}/search?q=!main@lemmy-beta:8550&type_=All&sort=TopAll`;
280 let searchResponse: SearchResponse = await fetch(searchUrl, {
282 }).then(d => d.json());
283 return searchResponse;
286 export async function searchForUser(
289 ): Promise<SearchResponse> {
290 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
291 // Use short-hand search url
292 let searchUrl = `${apiUrl(
294 )}/search?q=${apShortname}&type_=All&sort=TopAll`;
296 let searchResponse: SearchResponse = await fetch(searchUrl, {
298 }).then(d => d.json());
299 return searchResponse;
302 export async function followCommunity(
306 ): Promise<CommunityResponse> {
307 let followForm: FollowCommunityForm = {
313 let followRes: CommunityResponse = await fetch(
314 `${apiUrl(api)}/community/follow`,
318 'Content-Type': 'application/json',
320 body: wrapper(followForm),
329 export async function checkFollowedCommunities(
331 ): Promise<GetFollowedCommunitiesResponse> {
332 let followedCommunitiesUrl = `${apiUrl(
334 )}/user/followed_communities?&auth=${api.auth}`;
335 let followedCommunitiesRes: GetFollowedCommunitiesResponse = await fetch(
336 followedCommunitiesUrl,
340 ).then(d => d.json());
341 return followedCommunitiesRes;
344 export async function likePost(
348 ): Promise<PostResponse> {
349 let likePostForm: CreatePostLikeForm = {
355 let likePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/like`, {
358 'Content-Type': 'application/json',
360 body: wrapper(likePostForm),
361 }).then(d => d.json());
366 export async function createComment(
370 content = 'a jest test comment'
371 ): Promise<CommentResponse> {
372 let commentForm: CommentForm = {
379 let createResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
382 'Content-Type': 'application/json',
384 body: wrapper(commentForm),
385 }).then(d => d.json());
386 return createResponse;
389 export async function updateComment(
392 content = 'A jest test federated comment update'
393 ): Promise<CommentResponse> {
394 let commentForm: CommentForm = {
400 let updateResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
403 'Content-Type': 'application/json',
405 body: wrapper(commentForm),
406 }).then(d => d.json());
407 return updateResponse;
410 export async function deleteComment(
414 ): Promise<CommentResponse> {
415 let deleteCommentForm: DeleteCommentForm = {
421 let deleteCommentRes: CommentResponse = await fetch(
422 `${apiUrl(api)}/comment/delete`,
426 'Content-Type': 'application/json',
428 body: wrapper(deleteCommentForm),
430 ).then(d => d.json());
431 return deleteCommentRes;
434 export async function removeComment(
438 ): Promise<CommentResponse> {
439 let removeCommentForm: RemoveCommentForm = {
445 let removeCommentRes: CommentResponse = await fetch(
446 `${apiUrl(api)}/comment/remove`,
450 'Content-Type': 'application/json',
452 body: wrapper(removeCommentForm),
454 ).then(d => d.json());
455 return removeCommentRes;
458 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
459 let getMentionUrl = `${apiUrl(
461 )}/user/mention?sort=New&unread_only=false&auth=${api.auth}`;
462 let getMentionsRes: GetUserMentionsResponse = await fetch(getMentionUrl, {
464 }).then(d => d.json());
465 return getMentionsRes;
468 export async function likeComment(
472 ): Promise<CommentResponse> {
473 let likeCommentForm: CommentLikeForm = {
474 comment_id: comment.id,
479 let likeCommentRes: CommentResponse = await fetch(
480 `${apiUrl(api)}/comment/like`,
484 'Content-Type': 'application/json',
486 body: wrapper(likeCommentForm),
488 ).then(d => d.json());
489 return likeCommentRes;
492 export async function createCommunity(
494 name_: string = randomString(5)
495 ): Promise<CommunityResponse> {
496 let communityForm: CommunityForm = {
504 let createCommunityRes: CommunityResponse = await fetch(
505 `${apiUrl(api)}/community`,
509 'Content-Type': 'application/json',
511 body: wrapper(communityForm),
513 ).then(d => d.json());
514 return createCommunityRes;
517 export async function deleteCommunity(
521 ): Promise<CommunityResponse> {
522 let deleteCommunityForm: DeleteCommunityForm = {
528 let deleteResponse: CommunityResponse = await fetch(
529 `${apiUrl(api)}/community/delete`,
533 'Content-Type': 'application/json',
535 body: wrapper(deleteCommunityForm),
537 ).then(d => d.json());
538 return deleteResponse;
541 export async function removeCommunity(
545 ): Promise<CommunityResponse> {
546 let removeCommunityForm: RemoveCommunityForm = {
552 let removeResponse: CommunityResponse = await fetch(
553 `${apiUrl(api)}/community/remove`,
557 'Content-Type': 'application/json',
559 body: wrapper(removeCommunityForm),
561 ).then(d => d.json());
562 return removeResponse;
565 export async function createPrivateMessage(
568 ): Promise<PrivateMessageResponse> {
569 let content = 'A jest test federated private message';
570 let privateMessageForm: PrivateMessageForm = {
576 let createRes: PrivateMessageResponse = await fetch(
577 `${apiUrl(api)}/private_message`,
581 'Content-Type': 'application/json',
583 body: wrapper(privateMessageForm),
585 ).then(d => d.json());
589 export async function updatePrivateMessage(
592 ): Promise<PrivateMessageResponse> {
593 let updatedContent = 'A jest test federated private message edited';
594 let updatePrivateMessageForm: EditPrivateMessageForm = {
595 content: updatedContent,
600 let updateRes: PrivateMessageResponse = await fetch(
601 `${apiUrl(api)}/private_message`,
605 'Content-Type': 'application/json',
607 body: wrapper(updatePrivateMessageForm),
609 ).then(d => d.json());
613 export async function deletePrivateMessage(
617 ): Promise<PrivateMessageResponse> {
618 let deletePrivateMessageForm: DeletePrivateMessageForm = {
624 let deleteRes: PrivateMessageResponse = await fetch(
625 `${apiUrl(api)}/private_message/delete`,
629 'Content-Type': 'application/json',
631 body: wrapper(deletePrivateMessageForm),
633 ).then(d => d.json());
638 export async function registerUser(
640 username: string = randomString(5)
641 ): Promise<LoginResponse> {
642 let registerForm: RegisterForm = {
645 password_verify: 'test',
650 let registerRes: Promise<LoginResponse> = fetch(
651 `${apiUrl(api)}/user/register`,
655 'Content-Type': 'application/json',
657 body: wrapper(registerForm),
659 ).then(d => d.json());
664 export async function saveUserSettingsBio(
667 ): Promise<LoginResponse> {
668 let form: UserSettingsForm = {
671 default_sort_type: SortType.Active,
672 default_listing_type: ListingType.All,
675 send_notifications_to_email: false,
676 bio: 'a changed bio',
680 let res: Promise<LoginResponse> = fetch(
681 `${apiUrl(api)}/user/save_user_settings`,
685 'Content-Type': 'application/json',
689 ).then(d => d.json());
693 export async function getSite(
696 ): Promise<GetSiteResponse> {
697 let siteUrl = `${apiUrl(api)}/site?auth=${auth}`;
699 let res: GetSiteResponse = await fetch(siteUrl, {
701 }).then(d => d.json());
705 export async function listPrivateMessages(
707 ): Promise<PrivateMessagesResponse> {
708 let getPrivateMessagesUrl = `${apiUrl(api)}/private_message/list?auth=${
710 }&unread_only=false&limit=999`;
712 let getPrivateMessagesRes: PrivateMessagesResponse = await fetch(
713 getPrivateMessagesUrl,
717 ).then(d => d.json());
718 return getPrivateMessagesRes;
721 export async function unfollowRemotes(
723 ): Promise<GetFollowedCommunitiesResponse> {
724 // Unfollow all remote communities
725 let followed = await checkFollowedCommunities(api);
726 let remoteFollowed = followed.communities.filter(
727 c => c.community_local == false
729 for (let cu of remoteFollowed) {
730 await followCommunity(api, false, cu.community_id);
732 let followed2 = await checkFollowedCommunities(api);
736 export async function followBeta(api: API): Promise<CommunityResponse> {
737 await unfollowRemotes(api);
740 let search = await searchForBetaCommunity(api);
741 let com = search.communities.filter(c => c.local == false);
743 let follow = await followCommunity(api, true, com[0].id);
748 export function wrapper(form: any): string {
749 return JSON.stringify(form);
752 function randomString(length: number): string {
754 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
755 var charactersLength = characters.length;
756 for (var i = 0; i < length; i++) {
757 result += characters.charAt(Math.floor(Math.random() * charactersLength));