]> Untitled Git - lemmy.git/blobdiff - api_tests/src/shared.ts
First pass at adding comment trees. (#2362)
[lemmy.git] / api_tests / src / shared.ts
index 65effc6273f432e2b9fa14e89ba47781ff9ebdd3..1c0c01a8d05f8159287fe14d6d19722967db8ec3 100644 (file)
@@ -1,3 +1,4 @@
+import {None, Some, Option} from '@sniptt/monads';
 import {
   Login,
   LoginResponse,
@@ -58,65 +59,74 @@ import {
   ListCommentReports,
   ListCommentReportsResponse,
   DeleteAccount,
-  DeleteAccountResponse
+  DeleteAccountResponse,
+  EditSite,
+  CommentSortType,
+  GetComments,
+  GetCommentsResponse
 } from 'lemmy-js-client';
 
 export interface API {
   client: LemmyHttp;
-  auth?: string;
+  auth: Option<string>;
 }
 
 export let alpha: API = {
   client: new LemmyHttp('http://127.0.0.1:8541'),
+  auth: None,
 };
 
 export let beta: API = {
   client: new LemmyHttp('http://127.0.0.1:8551'),
+  auth: None,
 };
 
 export let gamma: API = {
   client: new LemmyHttp('http://127.0.0.1:8561'),
+  auth: None,
 };
 
 export let delta: API = {
   client: new LemmyHttp('http://127.0.0.1:8571'),
+  auth: None,
 };
 
 export let epsilon: API = {
   client: new LemmyHttp('http://127.0.0.1:8581'),
+  auth: None,
 };
 
 const password = 'lemmylemmy'
 
 export async function setupLogins() {
-  let formAlpha: Login = {
+  let formAlpha = new Login({
     username_or_email: 'lemmy_alpha',
     password,
-  };
+  });
   let resAlpha = alpha.client.login(formAlpha);
 
-  let formBeta = {
+  let formBeta = new Login({
     username_or_email: 'lemmy_beta',
     password,
-  };
+  });
   let resBeta = beta.client.login(formBeta);
 
-  let formGamma = {
+  let formGamma = new Login({
     username_or_email: 'lemmy_gamma',
     password,
-  };
+  });
   let resGamma = gamma.client.login(formGamma);
 
-  let formDelta = {
+  let formDelta = new Login({
     username_or_email: 'lemmy_delta',
     password,
-  };
+  });
   let resDelta = delta.client.login(formDelta);
 
-  let formEpsilon = {
+  let formEpsilon = new Login({
     username_or_email: 'lemmy_epsilon',
     password,
-  };
+  });
   let resEpsilon = epsilon.client.login(formEpsilon);
 
   let res = await Promise.all([
@@ -133,12 +143,36 @@ export async function setupLogins() {
   delta.auth = res[3].jwt;
   epsilon.auth = res[4].jwt;
 
-  // regstration applications are now enabled by default, need to disable them
-  await alpha.client.editSite({ require_application: false, auth: alpha.auth});
-  await beta.client.editSite({ require_application: false, auth: beta.auth});
-  await gamma.client.editSite({ require_application: false, auth: gamma.auth});
-  await delta.client.editSite({ require_application: false, auth: delta.auth});
-  await epsilon.client.editSite({ require_application: false, auth: epsilon.auth});
+  // Registration applications are now enabled by default, need to disable them
+  let editSiteForm = new EditSite({
+    name: None,
+    sidebar: None,
+    description: None,
+    icon: None,
+    banner: None,
+    enable_downvotes: None,
+    open_registration: None,
+    enable_nsfw: None,
+    community_creation_admin_only: None,
+    require_email_verification: None,
+    require_application: Some(false),
+    application_question: None,
+    private_instance: None,
+    default_theme: None,
+    legal_information: None,
+    default_post_listing_type: None,
+    auth: "",
+  });
+  editSiteForm.auth = alpha.auth.unwrap();
+  await alpha.client.editSite(editSiteForm);
+  editSiteForm.auth = beta.auth.unwrap();
+  await beta.client.editSite(editSiteForm);
+  editSiteForm.auth = gamma.auth.unwrap();
+  await gamma.client.editSite(editSiteForm);
+  editSiteForm.auth = delta.auth.unwrap();
+  await delta.client.editSite(editSiteForm);
+  editSiteForm.auth = epsilon.auth.unwrap();
+  await epsilon.client.editSite(editSiteForm);
 
   // Create the main beta community, follow it
   await createCommunity(beta, "main");
@@ -150,27 +184,30 @@ export async function createPost(
   community_id: number
 ): Promise<PostResponse> {
   let name = randomString(5);
-  let body = randomString(10);
-  let url = 'https://google.com/';
-  let form: CreatePost = {
+  let body = Some(randomString(10));
+  let url = Some('https://google.com/');
+  let form = new CreatePost({
     name,
     url,
     body,
-    auth: api.auth,
+    auth: api.auth.unwrap(),
     community_id,
-    nsfw: false,
-  };
+    nsfw: None,
+    honeypot: None,
+  });
   return api.client.createPost(form);
 }
 
 export async function editPost(api: API, post: Post): Promise<PostResponse> {
-  let name = 'A jest test federated post, updated';
-  let form: EditPost = {
+  let name = Some('A jest test federated post, updated');
+  let form = new EditPost({
     name,
     post_id: post.id,
-    auth: api.auth,
-    nsfw: false,
-  };
+    auth: api.auth.unwrap(),
+    nsfw: None,
+    url: None,
+    body: None,
+  });
   return api.client.editPost(form);
 }
 
@@ -179,11 +216,11 @@ export async function deletePost(
   deleted: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let form: DeletePost = {
+  let form = new DeletePost({
     post_id: post.id,
     deleted: deleted,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.deletePost(form);
 }
 
@@ -192,11 +229,12 @@ export async function removePost(
   removed: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let form: RemovePost = {
+  let form = new RemovePost({
     post_id: post.id,
     removed,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+    reason: None,
+  });
   return api.client.removePost(form);
 }
 
@@ -205,11 +243,11 @@ export async function stickyPost(
   stickied: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let form: StickyPost = {
+  let form = new StickyPost({
     post_id: post.id,
     stickied,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.stickyPost(form);
 }
 
@@ -218,11 +256,11 @@ export async function lockPost(
   locked: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let form: LockPost = {
+  let form = new LockPost({
     post_id: post.id,
     locked,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.lockPost(form);
 }
 
@@ -230,9 +268,10 @@ export async function resolvePost(
   api: API,
   post: Post
 ): Promise<ResolveObjectResponse> {
-  let form: ResolveObject = {
+  let form = new ResolveObject({
     q: post.ap_id,
-  };
+    auth: api.auth,
+  });
   return api.client.resolveObject(form);
 }
 
@@ -240,11 +279,18 @@ export async function searchPostLocal(
   api: API,
   post: Post
 ): Promise<SearchResponse> {
-  let form: Search = {
+  let form = new Search({
     q: post.name,
-    type_: SearchType.Posts,
-    sort: SortType.TopAll,
-  };
+    type_: Some(SearchType.Posts),
+    sort: Some(SortType.TopAll),
+    community_id: None,
+    community_name: None,
+    creator_id: None,
+    listing_type: None,
+    page: None,
+    limit: None,
+    auth: api.auth,
+  });
   return api.client.search(form);
 }
 
@@ -252,19 +298,42 @@ export async function getPost(
   api: API,
   post_id: number
 ): Promise<GetPostResponse> {
-  let form: GetPost = {
-    id: post_id,
-  };
+  let form = new GetPost({
+    id: Some(post_id),
+    comment_id: None,
+    auth: api.auth,
+  });
   return api.client.getPost(form);
 }
 
+export async function getComments(
+  api: API,
+  post_id: number
+): Promise<GetCommentsResponse> {
+  let form = new GetComments({
+    post_id: Some(post_id),
+    type_: Some(ListingType.All),
+    sort: Some(CommentSortType.New), // TODO this sort might be wrong
+    max_depth: None,
+    page: None,
+    limit: None,
+    community_id: None,
+    community_name: None,
+    saved_only: None,
+    parent_id: None,
+    auth: api.auth,
+  });
+  return api.client.getComments(form);
+}
+
 export async function resolveComment(
   api: API,
   comment: Comment
 ): Promise<ResolveObjectResponse> {
-  let form: ResolveObject = {
+  let form = new ResolveObject({
     q: comment.ap_id,
-  };
+    auth: api.auth,
+  });
   return api.client.resolveObject(form);
 }
 
@@ -272,9 +341,10 @@ export async function resolveBetaCommunity(
   api: API
 ): Promise<ResolveObjectResponse> {
   // Use short-hand search url
-  let form: ResolveObject = {
+  let form = new ResolveObject({
     q: '!main@lemmy-beta:8551',
-  };
+    auth: api.auth,
+  });
   return api.client.resolveObject(form);
 }
 
@@ -282,9 +352,10 @@ export async function resolveCommunity(
   api: API,
   q: string
 ): Promise<ResolveObjectResponse> {
-  let form: ResolveObject = {
+  let form = new ResolveObject({
     q,
-  };
+    auth: api.auth,
+  });
   return api.client.resolveObject(form);
 }
 
@@ -292,9 +363,10 @@ export async function resolvePerson(
   api: API,
   apShortname: string
 ): Promise<ResolveObjectResponse> {
-  let form: ResolveObject = {
+  let form = new ResolveObject({
     q: apShortname,
-  };
+    auth: api.auth,
+  });
   return api.client.resolveObject(form);
 }
 
@@ -305,12 +377,14 @@ export async function banPersonFromSite(
   remove_data: boolean
 ): Promise<BanPersonResponse> {
   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
-  let form: BanPerson = {
+  let form = new BanPerson({
     person_id,
     ban,
-    remove_data,
-    auth: api.auth,
-  };
+    remove_data: Some(remove_data),
+    auth: api.auth.unwrap(),
+    reason: None,
+    expires: None,
+  });
   return api.client.banPerson(form);
 }
 
@@ -321,13 +395,15 @@ export async function banPersonFromCommunity(
   remove_data: boolean,
   ban: boolean
 ): Promise<BanFromCommunityResponse> {
-  let form: BanFromCommunity = {
+  let form = new BanFromCommunity({
     person_id,
     community_id,
-    remove_data,
+    remove_data: Some(remove_data),
     ban,
-    auth: api.auth,
-  };
+    reason: None,
+    expires: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.banFromCommunity(form);
 }
 
@@ -336,11 +412,11 @@ export async function followCommunity(
   follow: boolean,
   community_id: number
 ): Promise<CommunityResponse> {
-  let form: FollowCommunity = {
+  let form = new FollowCommunity({
     community_id,
     follow,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap()
+  });
   return api.client.followCommunity(form);
 }
 
@@ -349,11 +425,11 @@ export async function likePost(
   score: number,
   post: Post
 ): Promise<PostResponse> {
-  let form: CreatePostLike = {
+  let form = new CreatePostLike({
     post_id: post.id,
     score: score,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap()
+  });
 
   return api.client.likePost(form);
 }
@@ -361,15 +437,16 @@ export async function likePost(
 export async function createComment(
   api: API,
   post_id: number,
-  parent_id?: number,
+  parent_id: Option<number>,
   content = 'a jest test comment'
 ): Promise<CommentResponse> {
-  let form: CreateComment = {
+  let form = new CreateComment({
     content,
     post_id,
     parent_id,
-    auth: api.auth,
-  };
+    form_id: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.createComment(form);
 }
 
@@ -378,11 +455,12 @@ export async function editComment(
   comment_id: number,
   content = 'A jest test federated comment update'
 ): Promise<CommentResponse> {
-  let form: EditComment = {
+  let form = new EditComment({
     content,
     comment_id,
-    auth: api.auth,
-  };
+    form_id: None,
+    auth: api.auth.unwrap()
+  });
   return api.client.editComment(form);
 }
 
@@ -391,11 +469,11 @@ export async function deleteComment(
   deleted: boolean,
   comment_id: number
 ): Promise<CommentResponse> {
-  let form: DeleteComment = {
+  let form = new DeleteComment({
     comment_id,
     deleted,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.deleteComment(form);
 }
 
@@ -404,20 +482,23 @@ export async function removeComment(
   removed: boolean,
   comment_id: number
 ): Promise<CommentResponse> {
-  let form: RemoveComment = {
+  let form = new RemoveComment({
     comment_id,
     removed,
-    auth: api.auth,
-  };
+    reason: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.removeComment(form);
 }
 
 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
-  let form: GetPersonMentions = {
-    sort: SortType.New,
-    unread_only: false,
-    auth: api.auth,
-  };
+  let form = new GetPersonMentions({
+    sort: Some(CommentSortType.New),
+    unread_only: Some(false),
+    auth: api.auth.unwrap(),
+    page: None,
+    limit: None,
+  });
   return api.client.getPersonMentions(form);
 }
 
@@ -426,11 +507,11 @@ export async function likeComment(
   score: number,
   comment: Comment
 ): Promise<CommentResponse> {
-  let form: CreateCommentLike = {
+  let form = new CreateCommentLike({
     comment_id: comment.id,
     score,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.likeComment(form);
 }
 
@@ -438,14 +519,17 @@ export async function createCommunity(
   api: API,
   name_: string = randomString(5)
 ): Promise<CommunityResponse> {
-  let description = 'a sample description';
-  let form: CreateCommunity = {
+  let description = Some('a sample description');
+  let form = new CreateCommunity({
     name: name_,
     title: name_,
     description,
-    nsfw: false,
-    auth: api.auth,
-  };
+    nsfw: None,
+    icon: None,
+    banner: None,
+    posting_restricted_to_mods: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.createCommunity(form);
 }
 
@@ -453,9 +537,11 @@ export async function getCommunity(
   api: API,
   id: number
 ): Promise<CommunityResponse> {
-  let form: GetCommunity = {
-    id,
-  };
+  let form = new GetCommunity({
+    id: Some(id),
+    name: None,
+    auth: api.auth,
+  });
   return api.client.getCommunity(form);
 }
 
@@ -464,11 +550,11 @@ export async function deleteCommunity(
   deleted: boolean,
   community_id: number
 ): Promise<CommunityResponse> {
-  let form: DeleteCommunity = {
+  let form = new DeleteCommunity({
     community_id,
     deleted,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.deleteCommunity(form);
 }
 
@@ -477,11 +563,13 @@ export async function removeCommunity(
   removed: boolean,
   community_id: number
 ): Promise<CommunityResponse> {
-  let form: RemoveCommunity = {
+  let form = new RemoveCommunity({
     community_id,
     removed,
-    auth: api.auth,
-  };
+    reason: None,
+    expires: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.removeCommunity(form);
 }
 
@@ -490,11 +578,11 @@ export async function createPrivateMessage(
   recipient_id: number
 ): Promise<PrivateMessageResponse> {
   let content = 'A jest test federated private message';
-  let form: CreatePrivateMessage = {
+  let form = new CreatePrivateMessage({
     content,
     recipient_id,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.createPrivateMessage(form);
 }
 
@@ -503,11 +591,11 @@ export async function editPrivateMessage(
   private_message_id: number
 ): Promise<PrivateMessageResponse> {
   let updatedContent = 'A jest test federated private message edited';
-  let form: EditPrivateMessage = {
+  let form = new EditPrivateMessage({
     content: updatedContent,
     private_message_id,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.editPrivateMessage(form);
 }
 
@@ -516,11 +604,11 @@ export async function deletePrivateMessage(
   deleted: boolean,
   private_message_id: number
 ): Promise<PrivateMessageResponse> {
-  let form: DeletePrivateMessage = {
+  let form = new DeletePrivateMessage({
     deleted,
     private_message_id,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.deletePrivateMessage(form);
 }
 
@@ -528,32 +616,77 @@ export async function registerUser(
   api: API,
   username: string = randomString(5)
 ): Promise<LoginResponse> {
-  let form: Register = {
+  let form = new Register({
     username,
     password,
     password_verify: password,
     show_nsfw: true,
-  };
+    email: None,
+    captcha_uuid: None,
+    captcha_answer: None,
+    honeypot: None,
+    answer: None,
+  });
   return api.client.register(form);
 }
 
 export async function saveUserSettingsBio(
   api: API
 ): Promise<LoginResponse> {
-  let form: SaveUserSettings = {
-    show_nsfw: true,
-    theme: 'darkly',
-    default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
-    default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
-    lang: 'en',
-    show_avatars: true,
-    send_notifications_to_email: false,
-    bio: 'a changed bio',
-    auth: api.auth,
-  };
+  let form = new SaveUserSettings({
+    show_nsfw: Some(true),
+    theme: Some('darkly'),
+    default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Active)),
+    default_listing_type: Some(Object.keys(ListingType).indexOf(ListingType.All)),
+    lang: Some('en'),
+    show_avatars: Some(true),
+    send_notifications_to_email: Some(false),
+    bio: Some('a changed bio'),
+    avatar: None,
+    banner: None,
+    display_name: None,
+    email: None,
+    matrix_user_id: None,
+    show_scores: None,
+    show_read_posts: None,
+    show_bot_accounts: None,
+    show_new_post_notifs: None,
+    bot_account: None,
+    auth: api.auth.unwrap(),
+  });
   return saveUserSettings(api, form);
 }
 
+export async function saveUserSettingsFederated(
+  api: API
+): Promise<LoginResponse> {
+  let avatar = Some('https://image.flaticon.com/icons/png/512/35/35896.png');
+  let banner = Some('https://image.flaticon.com/icons/png/512/36/35896.png');
+  let bio = Some('a changed bio');
+  let form = new SaveUserSettings({
+    show_nsfw: Some(false),
+    theme: Some(''),
+    default_sort_type: Some(Object.keys(SortType).indexOf(SortType.Hot)),
+    default_listing_type: Some(Object.keys(ListingType).indexOf(ListingType.All)),
+    lang: Some(''),
+    avatar,
+    banner,
+    display_name: Some('user321'),
+    show_avatars: Some(false),
+    send_notifications_to_email: Some(false),
+    bio,
+    email: None,
+    show_scores: None,
+    show_read_posts: None,
+    matrix_user_id: None,
+    bot_account: None,
+    show_bot_accounts: None,
+    show_new_post_notifs: None,
+    auth: api.auth.unwrap(),
+  });
+  return await saveUserSettings(alpha, form);
+}
+
 export async function saveUserSettings(
   api: API,
   form: SaveUserSettings
@@ -564,29 +697,31 @@ export async function saveUserSettings(
 export async function deleteUser(
   api: API
 ): Promise<DeleteAccountResponse> {
-  let form: DeleteAccount = {
-    auth: api.auth,
+  let form = new DeleteAccount({
+    auth: api.auth.unwrap(),
     password
-  };
+  });
   return api.client.deleteAccount(form);
 }
 
 export async function getSite(
   api: API
 ): Promise<GetSiteResponse> {
-  let form: GetSite = {
+  let form = new GetSite({
     auth: api.auth,
-  };
+  });
   return api.client.getSite(form);
 }
 
 export async function listPrivateMessages(
   api: API
 ): Promise<PrivateMessagesResponse> {
-  let form: GetPrivateMessages = {
-    auth: api.auth,
-    unread_only: false,
-  };
+  let form = new GetPrivateMessages({
+    auth: api.auth.unwrap(),
+    unread_only: Some(false),
+    page: None,
+    limit: None,
+  });
   return api.client.getPrivateMessages(form);
 }
 
@@ -595,7 +730,7 @@ export async function unfollowRemotes(
 ): Promise<GetSiteResponse> {
   // Unfollow all remote communities
   let site = await getSite(api);
-  let remoteFollowed = site.my_user.follows.filter(
+  let remoteFollowed = site.my_user.unwrap().follows.filter(
     c => c.community.local == false
   );
   for (let cu of remoteFollowed) {
@@ -607,9 +742,11 @@ export async function unfollowRemotes(
 
 export async function followBeta(api: API): Promise<CommunityResponse> {
   let betaCommunity = (await resolveBetaCommunity(api)).community;
-  if (betaCommunity) {
-    let follow = await followCommunity(api, true, betaCommunity.community.id);
+  if (betaCommunity.isSome()) {
+    let follow = await followCommunity(api, true, betaCommunity.unwrap().community.id);
     return follow;
+  } else {
+    return Promise.reject("no community worked");
   }
 }
 
@@ -618,18 +755,22 @@ export async function reportPost(
   post_id: number,
   reason: string
 ): Promise<PostReportResponse> {
-  let form: CreatePostReport = {
+  let form = new CreatePostReport({
     post_id,
     reason,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.createPostReport(form);
 }
 
 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
-  let form: ListPostReports = {
-    auth: api.auth,
-  };
+  let form = new ListPostReports({
+    auth: api.auth.unwrap(),
+    page: None,
+    limit: None,
+    community_id: None,
+    unresolved_only: None,
+  });
   return api.client.listPostReports(form);
 }
 
@@ -638,18 +779,22 @@ export async function reportComment(
   comment_id: number,
   reason: string
 ): Promise<CommentReportResponse> {
-  let form: CreateCommentReport = {
+  let form = new CreateCommentReport({
     comment_id,
     reason,
-    auth: api.auth,
-  };
+    auth: api.auth.unwrap(),
+  });
   return api.client.createCommentReport(form);
 }
 
 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
-  let form: ListCommentReports = {
-    auth: api.auth,
-  };
+  let form = new ListCommentReports({
+    page: None,
+    limit: None,
+    community_id: None,
+    unresolved_only: None,
+    auth: api.auth.unwrap(),
+  });
   return api.client.listCommentReports(form);
 }
 
@@ -681,3 +826,15 @@ export async function unfollows() {
   await unfollowRemotes(delta);
   await unfollowRemotes(epsilon);
 }
+
+export function getCommentParentId(comment: Comment): Option<number> {
+  let split = comment.path.split(".");
+  // remove the 0
+  split.shift();
+
+  if (split.length > 1) {
+    return Some(Number(split[split.length - 2]));
+  } else {
+    return None;
+  }
+}