]> Untitled Git - lemmy.git/commitdiff
Switch front end to use lemmy-js-client. Fixes #1090 (#1097)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 20 Aug 2020 14:50:26 +0000 (10:50 -0400)
committerGitHub <noreply@github.com>
Thu, 20 Aug 2020 14:50:26 +0000 (10:50 -0400)
* Switch front end to use lemmy-js-client. Fixes #1090

* Adding an HTTP client. Cleaning up Websocket client.

46 files changed:
README.md
server/src/routes/api.rs
ui/package.json
ui/src/api_tests/comment.spec.ts
ui/src/api_tests/shared.ts
ui/src/components/admin-settings.tsx
ui/src/components/comment-form.tsx
ui/src/components/comment-node.tsx
ui/src/components/comment-nodes.tsx
ui/src/components/communities.tsx
ui/src/components/community-form.tsx
ui/src/components/community-link.tsx
ui/src/components/community.tsx
ui/src/components/create-community.tsx
ui/src/components/create-post.tsx
ui/src/components/create-private-message.tsx
ui/src/components/footer.tsx
ui/src/components/iframely-card.tsx
ui/src/components/inbox.tsx
ui/src/components/instances.tsx
ui/src/components/listing-type-select.tsx
ui/src/components/login.tsx
ui/src/components/main.tsx
ui/src/components/modlog.tsx
ui/src/components/navbar.tsx
ui/src/components/password_change.tsx
ui/src/components/post-form.tsx
ui/src/components/post-listing.tsx
ui/src/components/post-listings.tsx
ui/src/components/post.tsx
ui/src/components/private-message-form.tsx
ui/src/components/private-message.tsx
ui/src/components/search.tsx
ui/src/components/setup.tsx
ui/src/components/sidebar.tsx
ui/src/components/site-form.tsx
ui/src/components/sort-select.tsx
ui/src/components/sponsors.tsx
ui/src/components/user-details.tsx
ui/src/components/user-listing.tsx
ui/src/components/user.tsx
ui/src/interfaces.ts
ui/src/services/UserService.ts
ui/src/services/WebSocketService.ts
ui/src/utils.ts
ui/yarn.lock

index 8aa5fe38a92ffb3238f856cc67fc69848970abd6..57b3d63e54c59a7ace158d4ba6f3e4a05ba8fd71 100644 (file)
--- a/README.md
+++ b/README.md
@@ -112,6 +112,7 @@ Each Lemmy server can set its own moderation policy; appointing site-wide admins
 
 ### Libraries
 
+- [lemmy-js-client](https://github.com/LemmyNet/lemmy-js-client)
 - [Kotlin API ( under development )](https://github.com/eiknat/lemmy-client)
 
 ## Support / Donate
index 1c88ffa24104a2e000a7671e911410609db4d65a..201908623816cadf24acb73a8ac5d386f2c83514 100644 (file)
@@ -94,7 +94,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
             web::post().to(route_post::<MarkCommentAsRead>),
           )
           .route("/like", web::post().to(route_post::<CreateCommentLike>))
-          .route("/save", web::put().to(route_post::<SaveComment>)),
+          .route("/save", web::put().to(route_post::<SaveComment>))
+          .route("/list", web::get().to(route_get::<GetComments>)),
       )
       // Private Message
       .service(
@@ -136,6 +137,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
             "/followed_communities",
             web::get().to(route_get::<GetFollowedCommunities>),
           )
+          .route("/join", web::post().to(route_post::<UserJoin>))
           // Admin action. I don't like that it's in /user
           .route("/ban", web::post().to(route_post::<BanUser>))
           // Account actions. I don't like that they're in /user maybe /accounts
index 06c52807a7281ef015381846e696d39f66006ac8..74740b76f54c39c3c016f5df88e8b3c9752a3339 100644 (file)
@@ -37,6 +37,7 @@
     "inferno-router": "^7.4.2",
     "js-cookie": "^2.2.0",
     "jwt-decode": "^2.2.0",
+    "lemmy-js-client": "^1.0.8",
     "markdown-it": "^11.0.0",
     "markdown-it-container": "^3.0.0",
     "markdown-it-emoji": "^1.4.0",
index 24aca55f921cb3e1bf9b8563796ac9fd5cada4a2..747ec910907bfcdc81b7f8cf0b03ca29d7b3f692 100644 (file)
@@ -21,7 +21,7 @@ import {
   API,
 } from './shared';
 
-import { PostResponse } from '../interfaces';
+import { PostResponse } from 'lemmy-js-client';
 
 let postRes: PostResponse;
 
@@ -136,7 +136,7 @@ test('Remove a comment from admin and community on the same instance', async ()
 test('Remove a comment from admin and community on different instance', async () => {
   let alphaUser = await registerUser(alpha);
   let newAlphaApi: API = {
-    url: alpha.url,
+    client: alpha.client,
     auth: alphaUser.jwt,
   };
 
index 29aaff25f957c2eff31182e824ae3db74c45eb82..710671c0e089fe469ce2c3dd1d23b7863786a83a 100644 (file)
@@ -1,5 +1,3 @@
-import fetch from 'node-fetch';
-
 import {
   LoginForm,
   LoginResponse,
@@ -20,15 +18,21 @@ import {
   CommentForm,
   DeleteCommentForm,
   RemoveCommentForm,
+  SearchForm,
   CommentResponse,
   CommunityForm,
   DeleteCommunityForm,
   RemoveCommunityForm,
+  GetUserMentionsForm,
   CommentLikeForm,
   CreatePostLikeForm,
   PrivateMessageForm,
   EditPrivateMessageForm,
   DeletePrivateMessageForm,
+  GetFollowedCommunitiesForm,
+  GetPrivateMessagesForm,
+  GetSiteForm,
+  GetPostForm,
   PrivateMessageResponse,
   PrivateMessagesResponse,
   GetUserMentionsResponse,
@@ -36,35 +40,33 @@ import {
   SortType,
   ListingType,
   GetSiteResponse,
-} from '../interfaces';
+  SearchType,
+  LemmyHttp,
+} from 'lemmy-js-client';
 
 export interface API {
-  url: string;
+  client: LemmyHttp;
   auth?: string;
 }
 
-function apiUrl(api: API) {
-  return `${api.url}/api/v1`;
-}
-
 export let alpha: API = {
-  url: 'http://localhost:8540',
+  client: new LemmyHttp('http://localhost:8540/api/v1'),
 };
 
 export let beta: API = {
-  url: 'http://localhost:8550',
+  client: new LemmyHttp('http://localhost:8550/api/v1'),
 };
 
 export let gamma: API = {
-  url: 'http://localhost:8560',
+  client: new LemmyHttp('http://localhost:8560/api/v1'),
 };
 
 export let delta: API = {
-  url: 'http://localhost:8570',
+  client: new LemmyHttp('http://localhost:8570/api/v1'),
 };
 
 export let epsilon: API = {
-  url: 'http://localhost:8580',
+  client: new LemmyHttp('http://localhost:8580/api/v1'),
 };
 
 export async function setupLogins() {
@@ -72,69 +74,31 @@ export async function setupLogins() {
     username_or_email: 'lemmy_alpha',
     password: 'lemmy',
   };
-
-  let resAlpha: Promise<LoginResponse> = fetch(`${apiUrl(alpha)}/user/login`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(formAlpha),
-  }).then(d => d.json());
+  let resAlpha = alpha.client.login(formAlpha);
 
   let formBeta = {
     username_or_email: 'lemmy_beta',
     password: 'lemmy',
   };
-
-  let resBeta: Promise<LoginResponse> = fetch(`${apiUrl(beta)}/user/login`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(formBeta),
-  }).then(d => d.json());
+  let resBeta = beta.client.login(formBeta);
 
   let formGamma = {
     username_or_email: 'lemmy_gamma',
     password: 'lemmy',
   };
-
-  let resGamma: Promise<LoginResponse> = fetch(`${apiUrl(gamma)}/user/login`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(formGamma),
-  }).then(d => d.json());
+  let resGamma = gamma.client.login(formGamma);
 
   let formDelta = {
     username_or_email: 'lemmy_delta',
     password: 'lemmy',
   };
-
-  let resDelta: Promise<LoginResponse> = fetch(`${apiUrl(delta)}/user/login`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(formDelta),
-  }).then(d => d.json());
+  let resDelta = delta.client.login(formDelta);
 
   let formEpsilon = {
     username_or_email: 'lemmy_epsilon',
     password: 'lemmy',
   };
-
-  let resEpsilon: Promise<LoginResponse> = fetch(
-    `${apiUrl(epsilon)}/user/login`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(formEpsilon),
-    }
-  ).then(d => d.json());
+  let resEpsilon = epsilon.client.login(formEpsilon);
 
   let res = await Promise.all([
     resAlpha,
@@ -156,40 +120,24 @@ export async function createPost(
   community_id: number
 ): Promise<PostResponse> {
   let name = 'A jest test post';
-  let postForm: PostForm = {
+  let form: PostForm = {
     name,
     auth: api.auth,
     community_id,
     nsfw: false,
   };
-
-  let createPostRes: PostResponse = await fetch(`${apiUrl(api)}/post`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(postForm),
-  }).then(d => d.json());
-  return createPostRes;
+  return api.client.createPost(form);
 }
 
 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
   let name = 'A jest test federated post, updated';
-  let postForm: PostForm = {
+  let form: PostForm = {
     name,
     edit_id: post.id,
     auth: api.auth,
     nsfw: false,
   };
-
-  let updateResponse: PostResponse = await fetch(`${apiUrl(api)}/post`, {
-    method: 'PUT',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(postForm),
-  }).then(d => d.json());
-  return updateResponse;
+  return api.client.editPost(form);
 }
 
 export async function deletePost(
@@ -197,20 +145,12 @@ export async function deletePost(
   deleted: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let deletePostForm: DeletePostForm = {
+  let form: DeletePostForm = {
     edit_id: post.id,
     deleted: deleted,
     auth: api.auth,
   };
-
-  let deletePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/delete`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(deletePostForm),
-  }).then(d => d.json());
-  return deletePostRes;
+  return api.client.deletePost(form);
 }
 
 export async function removePost(
@@ -218,20 +158,12 @@ export async function removePost(
   removed: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let removePostForm: RemovePostForm = {
+  let form: RemovePostForm = {
     edit_id: post.id,
     removed,
     auth: api.auth,
   };
-
-  let removePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/remove`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(removePostForm),
-  }).then(d => d.json());
-  return removePostRes;
+  return api.client.removePost(form);
 }
 
 export async function stickyPost(
@@ -239,21 +171,12 @@ export async function stickyPost(
   stickied: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let stickyPostForm: StickyPostForm = {
+  let form: StickyPostForm = {
     edit_id: post.id,
     stickied,
     auth: api.auth,
   };
-
-  let stickyRes: PostResponse = await fetch(`${apiUrl(api)}/post/sticky`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(stickyPostForm),
-  }).then(d => d.json());
-
-  return stickyRes;
+  return api.client.stickyPost(form);
 }
 
 export async function lockPost(
@@ -261,57 +184,46 @@ export async function lockPost(
   locked: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let lockPostForm: LockPostForm = {
+  let form: LockPostForm = {
     edit_id: post.id,
     locked,
     auth: api.auth,
   };
-
-  let lockRes: PostResponse = await fetch(`${apiUrl(api)}/post/lock`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(lockPostForm),
-  }).then(d => d.json());
-
-  return lockRes;
+  return api.client.lockPost(form);
 }
 
 export async function searchPost(
   api: API,
   post: Post
 ): Promise<SearchResponse> {
-  let searchUrl = `${apiUrl(api)}/search?q=${post.ap_id}&type_=All&sort=TopAll`;
-  let searchResponse: SearchResponse = await fetch(searchUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return searchResponse;
+  let form: SearchForm = {
+    q: post.ap_id,
+    type_: SearchType.All,
+    sort: SortType.TopAll,
+  };
+  return api.client.search(form);
 }
 
 export async function getPost(
   api: API,
   post_id: number
 ): Promise<GetPostResponse> {
-  let getPostUrl = `${apiUrl(api)}/post?id=${post_id}`;
-  let getPostRes: GetPostResponse = await fetch(getPostUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-
-  return getPostRes;
+  let form: GetPostForm = {
+    id: post_id,
+  };
+  return api.client.getPost(form);
 }
 
 export async function searchComment(
   api: API,
   comment: Comment
 ): Promise<SearchResponse> {
-  let searchUrl = `${apiUrl(api)}/search?q=${
-    comment.ap_id
-  }&type_=All&sort=TopAll`;
-  let searchResponse: SearchResponse = await fetch(searchUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return searchResponse;
+  let form: SearchForm = {
+    q: comment.ap_id,
+    type_: SearchType.All,
+    sort: SortType.TopAll,
+  };
+  return api.client.search(form);
 }
 
 export async function searchForBetaCommunity(
@@ -319,14 +231,12 @@ export async function searchForBetaCommunity(
 ): Promise<SearchResponse> {
   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
   // Use short-hand search url
-  let searchUrl = `${apiUrl(
-    api
-  )}/search?q=!main@lemmy-beta:8550&type_=All&sort=TopAll`;
-
-  let searchResponse: SearchResponse = await fetch(searchUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return searchResponse;
+  let form: SearchForm = {
+    q: '!main@lemmy-beta:8550',
+    type_: SearchType.All,
+    sort: SortType.TopAll,
+  };
+  return api.client.search(form);
 }
 
 export async function searchForUser(
@@ -335,14 +245,12 @@ export async function searchForUser(
 ): Promise<SearchResponse> {
   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
   // Use short-hand search url
-  let searchUrl = `${apiUrl(
-    api
-  )}/search?q=${apShortname}&type_=All&sort=TopAll`;
-
-  let searchResponse: SearchResponse = await fetch(searchUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return searchResponse;
+  let form: SearchForm = {
+    q: apShortname,
+    type_: SearchType.All,
+    sort: SortType.TopAll,
+  };
+  return api.client.search(form);
 }
 
 export async function followCommunity(
@@ -350,41 +258,21 @@ export async function followCommunity(
   follow: boolean,
   community_id: number
 ): Promise<CommunityResponse> {
-  let followForm: FollowCommunityForm = {
+  let form: FollowCommunityForm = {
     community_id,
     follow,
     auth: api.auth,
   };
-
-  let followRes: CommunityResponse = await fetch(
-    `${apiUrl(api)}/community/follow`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(followForm),
-    }
-  )
-    .then(d => d.json())
-    .catch(_e => {});
-
-  return followRes;
+  return api.client.followCommunity(form);
 }
 
 export async function checkFollowedCommunities(
   api: API
 ): Promise<GetFollowedCommunitiesResponse> {
-  let followedCommunitiesUrl = `${apiUrl(
-    api
-  )}/user/followed_communities?&auth=${api.auth}`;
-  let followedCommunitiesRes: GetFollowedCommunitiesResponse = await fetch(
-    followedCommunitiesUrl,
-    {
-      method: 'GET',
-    }
-  ).then(d => d.json());
-  return followedCommunitiesRes;
+  let form: GetFollowedCommunitiesForm = {
+    auth: api.auth,
+  };
+  return api.client.getFollowedCommunities(form);
 }
 
 export async function likePost(
@@ -392,21 +280,13 @@ export async function likePost(
   score: number,
   post: Post
 ): Promise<PostResponse> {
-  let likePostForm: CreatePostLikeForm = {
+  let form: CreatePostLikeForm = {
     post_id: post.id,
     score: score,
     auth: api.auth,
   };
 
-  let likePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/like`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(likePostForm),
-  }).then(d => d.json());
-
-  return likePostRes;
+  return api.client.likePost(form);
 }
 
 export async function createComment(
@@ -415,21 +295,13 @@ export async function createComment(
   parent_id?: number,
   content = 'a jest test comment'
 ): Promise<CommentResponse> {
-  let commentForm: CommentForm = {
+  let form: CommentForm = {
     content,
     post_id,
     parent_id,
     auth: api.auth,
   };
-
-  let createResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(commentForm),
-  }).then(d => d.json());
-  return createResponse;
+  return api.client.createComment(form);
 }
 
 export async function updateComment(
@@ -437,20 +309,12 @@ export async function updateComment(
   edit_id: number,
   content = 'A jest test federated comment update'
 ): Promise<CommentResponse> {
-  let commentForm: CommentForm = {
+  let form: CommentForm = {
     content,
     edit_id,
     auth: api.auth,
   };
-
-  let updateResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
-    method: 'PUT',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: wrapper(commentForm),
-  }).then(d => d.json());
-  return updateResponse;
+  return api.client.editComment(form);
 }
 
 export async function deleteComment(
@@ -458,23 +322,12 @@ export async function deleteComment(
   deleted: boolean,
   edit_id: number
 ): Promise<CommentResponse> {
-  let deleteCommentForm: DeleteCommentForm = {
+  let form: DeleteCommentForm = {
     edit_id,
     deleted,
     auth: api.auth,
   };
-
-  let deleteCommentRes: CommentResponse = await fetch(
-    `${apiUrl(api)}/comment/delete`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(deleteCommentForm),
-    }
-  ).then(d => d.json());
-  return deleteCommentRes;
+  return api.client.deleteComment(form);
 }
 
 export async function removeComment(
@@ -482,33 +335,21 @@ export async function removeComment(
   removed: boolean,
   edit_id: number
 ): Promise<CommentResponse> {
-  let removeCommentForm: RemoveCommentForm = {
+  let form: RemoveCommentForm = {
     edit_id,
     removed,
     auth: api.auth,
   };
-
-  let removeCommentRes: CommentResponse = await fetch(
-    `${apiUrl(api)}/comment/remove`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(removeCommentForm),
-    }
-  ).then(d => d.json());
-  return removeCommentRes;
+  return api.client.removeComment(form);
 }
 
 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
-  let getMentionUrl = `${apiUrl(
-    api
-  )}/user/mention?sort=New&unread_only=false&auth=${api.auth}`;
-  let getMentionsRes: GetUserMentionsResponse = await fetch(getMentionUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return getMentionsRes;
+  let form: GetUserMentionsForm = {
+    sort: SortType.New,
+    unread_only: false,
+    auth: api.auth,
+  };
+  return api.client.getUserMentions(form);
 }
 
 export async function likeComment(
@@ -516,48 +357,26 @@ export async function likeComment(
   score: number,
   comment: Comment
 ): Promise<CommentResponse> {
-  let likeCommentForm: CommentLikeForm = {
+  let form: CommentLikeForm = {
     comment_id: comment.id,
     score,
     auth: api.auth,
   };
-
-  let likeCommentRes: CommentResponse = await fetch(
-    `${apiUrl(api)}/comment/like`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(likeCommentForm),
-    }
-  ).then(d => d.json());
-  return likeCommentRes;
+  return api.client.likeComment(form);
 }
 
 export async function createCommunity(
   api: API,
   name_: string = randomString(5)
 ): Promise<CommunityResponse> {
-  let communityForm: CommunityForm = {
+  let form: CommunityForm = {
     name: name_,
     title: name_,
     category_id: 1,
     nsfw: false,
     auth: api.auth,
   };
-
-  let createCommunityRes: CommunityResponse = await fetch(
-    `${apiUrl(api)}/community`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(communityForm),
-    }
-  ).then(d => d.json());
-  return createCommunityRes;
+  return api.client.createCommunity(form);
 }
 
 export async function deleteCommunity(
@@ -565,23 +384,12 @@ export async function deleteCommunity(
   deleted: boolean,
   edit_id: number
 ): Promise<CommunityResponse> {
-  let deleteCommunityForm: DeleteCommunityForm = {
+  let form: DeleteCommunityForm = {
     edit_id,
     deleted,
     auth: api.auth,
   };
-
-  let deleteResponse: CommunityResponse = await fetch(
-    `${apiUrl(api)}/community/delete`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(deleteCommunityForm),
-    }
-  ).then(d => d.json());
-  return deleteResponse;
+  return api.client.deleteCommunity(form);
 }
 
 export async function removeCommunity(
@@ -589,23 +397,12 @@ export async function removeCommunity(
   removed: boolean,
   edit_id: number
 ): Promise<CommunityResponse> {
-  let removeCommunityForm: RemoveCommunityForm = {
+  let form: RemoveCommunityForm = {
     edit_id,
     removed,
     auth: api.auth,
   };
-
-  let removeResponse: CommunityResponse = await fetch(
-    `${apiUrl(api)}/community/remove`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(removeCommunityForm),
-    }
-  ).then(d => d.json());
-  return removeResponse;
+  return api.client.removeCommunity(form);
 }
 
 export async function createPrivateMessage(
@@ -613,23 +410,12 @@ export async function createPrivateMessage(
   recipient_id: number
 ): Promise<PrivateMessageResponse> {
   let content = 'A jest test federated private message';
-  let privateMessageForm: PrivateMessageForm = {
+  let form: PrivateMessageForm = {
     content,
     recipient_id,
     auth: api.auth,
   };
-
-  let createRes: PrivateMessageResponse = await fetch(
-    `${apiUrl(api)}/private_message`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(privateMessageForm),
-    }
-  ).then(d => d.json());
-  return createRes;
+  return api.client.createPrivateMessage(form);
 }
 
 export async function updatePrivateMessage(
@@ -637,23 +423,12 @@ export async function updatePrivateMessage(
   edit_id: number
 ): Promise<PrivateMessageResponse> {
   let updatedContent = 'A jest test federated private message edited';
-  let updatePrivateMessageForm: EditPrivateMessageForm = {
+  let form: EditPrivateMessageForm = {
     content: updatedContent,
     edit_id,
     auth: api.auth,
   };
-
-  let updateRes: PrivateMessageResponse = await fetch(
-    `${apiUrl(api)}/private_message`,
-    {
-      method: 'PUT',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(updatePrivateMessageForm),
-    }
-  ).then(d => d.json());
-  return updateRes;
+  return api.client.editPrivateMessage(form);
 }
 
 export async function deletePrivateMessage(
@@ -661,50 +436,26 @@ export async function deletePrivateMessage(
   deleted: boolean,
   edit_id: number
 ): Promise<PrivateMessageResponse> {
-  let deletePrivateMessageForm: DeletePrivateMessageForm = {
+  let form: DeletePrivateMessageForm = {
     deleted,
     edit_id,
     auth: api.auth,
   };
-
-  let deleteRes: PrivateMessageResponse = await fetch(
-    `${apiUrl(api)}/private_message/delete`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(deletePrivateMessageForm),
-    }
-  ).then(d => d.json());
-
-  return deleteRes;
+  return api.client.deletePrivateMessage(form);
 }
 
 export async function registerUser(
   api: API,
   username: string = randomString(5)
 ): Promise<LoginResponse> {
-  let registerForm: RegisterForm = {
+  let form: RegisterForm = {
     username,
     password: 'test',
     password_verify: 'test',
     admin: false,
     show_nsfw: true,
   };
-
-  let registerRes: Promise<LoginResponse> = fetch(
-    `${apiUrl(api)}/user/register`,
-    {
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(registerForm),
-    }
-  ).then(d => d.json());
-
-  return registerRes;
+  return api.client.register(form);
 }
 
 export async function saveUserSettingsBio(
@@ -714,54 +465,36 @@ export async function saveUserSettingsBio(
   let form: UserSettingsForm = {
     show_nsfw: true,
     theme: 'darkly',
-    default_sort_type: SortType.Active,
-    default_listing_type: ListingType.All,
+    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,
   };
-
-  let res: Promise<LoginResponse> = fetch(
-    `${apiUrl(api)}/user/save_user_settings`,
-    {
-      method: 'PUT',
-      headers: {
-        'Content-Type': 'application/json',
-      },
-      body: wrapper(form),
-    }
-  ).then(d => d.json());
-  return res;
+  return api.client.saveUserSettings(form);
 }
 
 export async function getSite(
   api: API,
   auth: string
 ): Promise<GetSiteResponse> {
-  let siteUrl = `${apiUrl(api)}/site?auth=${auth}`;
-
-  let res: GetSiteResponse = await fetch(siteUrl, {
-    method: 'GET',
-  }).then(d => d.json());
-  return res;
+  let form: GetSiteForm = {
+    auth,
+  };
+  return api.client.getSite(form);
 }
 
 export async function listPrivateMessages(
   api: API
 ): Promise<PrivateMessagesResponse> {
-  let getPrivateMessagesUrl = `${apiUrl(api)}/private_message/list?auth=${
-    api.auth
-  }&unread_only=false&limit=999`;
-
-  let getPrivateMessagesRes: PrivateMessagesResponse = await fetch(
-    getPrivateMessagesUrl,
-    {
-      method: 'GET',
-    }
-  ).then(d => d.json());
-  return getPrivateMessagesRes;
+  let form: GetPrivateMessagesForm = {
+    auth: api.auth,
+    unread_only: false,
+    limit: 999,
+  };
+  return api.client.getPrivateMessages(form);
 }
 
 export async function unfollowRemotes(
index fe50b1e94714a914d54f1b6be09fe0818d62899d..a3bfdd81341d6489d1ab69f1742ad911c9d75363 100644 (file)
@@ -9,7 +9,7 @@ import {
   SiteConfigForm,
   GetSiteConfigResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { wsJsonToRes, capitalizeFirstLetter, toast, randomStr } from '../utils';
 import autosize from 'autosize';
index 5597f58ee5f3db89dcd8f5be47bc99dacbc1c796..dbd14dc76adc1ff82939d23f10714a2a60970331 100644 (file)
@@ -8,7 +8,7 @@ import {
   WebSocketJsonResponse,
   UserOperation,
   CommentResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { capitalizeFirstLetter, wsJsonToRes } from '../utils';
 import { WebSocketService, UserService } from '../services';
 import { i18n } from '../i18next';
index 13263b8221b1492322d0b4bc413b9c61b69f5200..1992c4fc846bb9c6d4b1b656c5d8cd90aac2eda1 100644 (file)
@@ -16,10 +16,9 @@ import {
   AddAdminForm,
   TransferCommunityForm,
   TransferSiteForm,
-  BanType,
-  CommentSortType,
   SortType,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { CommentSortType, BanType } from '../interfaces';
 import { WebSocketService, UserService } from '../services';
 import {
   mdToHtml,
index 62693f7662a45a0bf04271f7f3a6c9817b69b4ff..bdb8a545e589ceacd1d305ef9445a137c891955e 100644 (file)
@@ -1,11 +1,11 @@
 import { Component } from 'inferno';
+import { CommentSortType } from '../interfaces';
 import {
   CommentNode as CommentNodeI,
   CommunityUser,
   UserView,
-  CommentSortType,
   SortType,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { commentSort, commentSortSortType } from '../utils';
 import { CommentNode } from './comment-node';
 
index 038e4517fdae54bb913c8423a6f57cc36442915a..5be032c5e47f3790742cca62acc0df0ee9d3548e 100644 (file)
@@ -13,7 +13,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { wsJsonToRes, toast, getPageFromProps } from '../utils';
 import { CommunityLink } from './community-link';
@@ -218,7 +218,7 @@ export class Communities extends Component<any, CommunitiesState> {
 
   refetch() {
     let listCommunitiesForm: ListCommunitiesForm = {
-      sort: SortType[SortType.TopAll],
+      sort: SortType.TopAll,
       limit: communityLimit,
       page: this.state.page,
     };
index 1ae96ac8b1e2687ce483ca2d68b3717c189bfcb9..7b8c379ba043a9ccd617f359c28fe6fb06e6379f 100644 (file)
@@ -9,12 +9,12 @@ import {
   ListCategoriesResponse,
   CommunityResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+  Community,
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { wsJsonToRes, capitalizeFirstLetter, toast, randomStr } from '../utils';
 import { i18n } from '../i18next';
 
-import { Community } from '../interfaces';
 import { MarkdownTextArea } from './markdown-textarea';
 import { ImageUploadForm } from './image-upload-form';
 
index 293ded0464b097162d4d9bc8c773b8dd8b544f19..003f61e14464b357b97500ef40315a453ed50f7b 100644 (file)
@@ -1,6 +1,6 @@
 import { Component } from 'inferno';
 import { Link } from 'inferno-router';
-import { Community } from '../interfaces';
+import { Community } from 'lemmy-js-client';
 import { hostname, pictrsAvatarThumbnail, showAvatars } from '../utils';
 
 interface CommunityOther {
index 1fe75c598ed497d67895f33cb0aaadac2d92c105..f86562f8c23286fe84bb38f92b5a708fb8d028eb 100644 (file)
@@ -2,6 +2,7 @@ import { Component, linkEvent } from 'inferno';
 import { Helmet } from 'inferno-helmet';
 import { Subscription } from 'rxjs';
 import { retryWhen, delay, take } from 'rxjs/operators';
+import { DataType } from '../interfaces';
 import {
   UserOperation,
   Community as CommunityI,
@@ -14,7 +15,6 @@ import {
   GetPostsForm,
   GetCommunityForm,
   ListingType,
-  DataType,
   GetPostsResponse,
   PostResponse,
   AddModToCommunityResponse,
@@ -26,7 +26,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { PostListings } from './post-listings';
 import { CommentNodes } from './comment-nodes';
@@ -78,7 +78,7 @@ interface CommunityProps {
 
 interface UrlParams {
   dataType?: string;
-  sort?: string;
+  sort?: SortType;
   page?: number;
 }
 
@@ -287,9 +287,7 @@ export class Community extends Component<any, State> {
           <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
         </span>
         <a
-          href={`/feeds/c/${this.state.communityName}.xml?sort=${
-            SortType[this.state.sort]
-          }`}
+          href={`/feeds/c/${this.state.communityName}.xml?sort=${this.state.sort}`}
           target="_blank"
           title="RSS"
           rel="noopener"
@@ -336,20 +334,18 @@ export class Community extends Component<any, State> {
   }
 
   handleSortChange(val: SortType) {
-    this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ sort: val, page: 1 });
     window.scrollTo(0, 0);
   }
 
   handleDataTypeChange(val: DataType) {
-    this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ dataType: DataType[val], page: 1 });
     window.scrollTo(0, 0);
   }
 
   updateUrl(paramUpdates: UrlParams) {
-    const dataTypeStr =
-      paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
-    const sortStr =
-      paramUpdates.sort || SortType[this.state.sort].toLowerCase();
+    const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
+    const sortStr = paramUpdates.sort || this.state.sort;
     const page = paramUpdates.page || this.state.page;
     this.props.history.push(
       `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
@@ -361,8 +357,8 @@ export class Community extends Component<any, State> {
       let getPostsForm: GetPostsForm = {
         page: this.state.page,
         limit: fetchLimit,
-        sort: SortType[this.state.sort],
-        type_: ListingType[ListingType.Community],
+        sort: this.state.sort,
+        type_: ListingType.Community,
         community_id: this.state.community.id,
       };
       WebSocketService.Instance.getPosts(getPostsForm);
@@ -370,8 +366,8 @@ export class Community extends Component<any, State> {
       let getCommentsForm: GetCommentsForm = {
         page: this.state.page,
         limit: fetchLimit,
-        sort: SortType[this.state.sort],
-        type_: ListingType[ListingType.Community],
+        sort: this.state.sort,
+        type_: ListingType.Community,
         community_id: this.state.community.id,
       };
       WebSocketService.Instance.getComments(getCommentsForm);
index 8317ffbe863b92f34754ca3c0d647c749aa0eb49..6f156211d34be49fb5bc1c80b30ef781f8f14410 100644 (file)
@@ -9,7 +9,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { toast, wsJsonToRes } from '../utils';
 import { WebSocketService, UserService } from '../services';
 import { i18n } from '../i18next';
index eb86d8f88868e14dd4085be848627eac2caebf0b..f4c03b653ecd72659137a86fe03b8398f5381e14 100644 (file)
@@ -11,7 +11,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { i18n } from '../i18next';
 
 interface CreatePostState {
index ed06a66ae89f86f85234e55ed5679830ac404191..98c69d5b74bfa15b30799ca024138e062fa7d341 100644 (file)
@@ -10,7 +10,7 @@ import {
   GetSiteResponse,
   Site,
   PrivateMessageFormParams,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { toast, wsJsonToRes } from '../utils';
 import { i18n } from '../i18next';
 
index 6e7acb7a0ac4a770bbf996615a46982584670d15..62585ff30bb9b4e40693b4dc8c436035abfe6dfc 100644 (file)
@@ -9,7 +9,7 @@ import {
   UserOperation,
   WebSocketJsonResponse,
   GetSiteResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 
 interface FooterState {
   version: string;
index 1a47f377a6dac9bf182ed8637aea43f5ea57c2fa..6a604f7c5ffbe8cabc88083c8c14da410634b5ae 100644 (file)
@@ -1,5 +1,5 @@
 import { Component, linkEvent } from 'inferno';
-import { Post } from '../interfaces';
+import { Post } from 'lemmy-js-client';
 import { mdToHtml } from '../utils';
 import { i18n } from '../i18next';
 
index ecc9223c3c0ac10503d4dde026f277eb05432b50..4da86e351a35c6b7f4a45c3840dc3190ef78703b 100644 (file)
@@ -19,7 +19,7 @@ import {
   PrivateMessageResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import {
   wsJsonToRes,
@@ -399,7 +399,7 @@ export class Inbox extends Component<any, InboxState> {
 
   refetch() {
     let repliesForm: GetRepliesForm = {
-      sort: SortType[this.state.sort],
+      sort: this.state.sort,
       unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
       page: this.state.page,
       limit: fetchLimit,
@@ -407,7 +407,7 @@ export class Inbox extends Component<any, InboxState> {
     WebSocketService.Instance.getReplies(repliesForm);
 
     let userMentionsForm: GetUserMentionsForm = {
-      sort: SortType[this.state.sort],
+      sort: this.state.sort,
       unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
       page: this.state.page,
       limit: fetchLimit,
index ae4e3f13b65f8dca677dbc437087794c2b4a86b2..bcc02480bfb9a31edb5e504c018f93dae7fe3a08 100644 (file)
@@ -6,7 +6,7 @@ import {
   UserOperation,
   WebSocketJsonResponse,
   GetSiteResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { wsJsonToRes, toast } from '../utils';
 import { i18n } from '../i18next';
index 6bdf457c402a487c80475fe1e46b18e295863d0c..3d12d43439e661b5e841f0f886657362562fd511 100644 (file)
@@ -1,7 +1,7 @@
 import { Component, linkEvent } from 'inferno';
-import { ListingType } from '../interfaces';
+import { ListingType } from 'lemmy-js-client';
 import { UserService } from '../services';
-
+import { randomStr } from '../utils';
 import { i18n } from '../i18next';
 
 interface ListingTypeSelectProps {
@@ -17,6 +17,8 @@ export class ListingTypeSelect extends Component<
   ListingTypeSelectProps,
   ListingTypeSelectState
 > {
+  private id = `listing-type-input-${randomStr()}`;
+
   private emptyState: ListingTypeSelectState = {
     type_: this.props.type_,
   };
@@ -42,6 +44,7 @@ export class ListingTypeSelect extends Component<
           `}
         >
           <input
+            id={`${this.id}-subscribed`}
             type="radio"
             value={ListingType.Subscribed}
             checked={this.state.type_ == ListingType.Subscribed}
@@ -56,6 +59,7 @@ export class ListingTypeSelect extends Component<
           }`}
         >
           <input
+            id={`${this.id}-all`}
             type="radio"
             value={ListingType.All}
             checked={this.state.type_ == ListingType.All}
@@ -68,6 +72,6 @@ export class ListingTypeSelect extends Component<
   }
 
   handleTypeChange(i: ListingTypeSelect, event: any) {
-    i.props.onChange(Number(event.target.value));
+    i.props.onChange(event.target.value);
   }
 }
index bdee3a97e956da80e9a3e96edd7b79b15784866f..caf8c9cfb48c9463f04d149b7f0700c07231a94e 100644 (file)
@@ -12,7 +12,7 @@ import {
   GetCaptchaResponse,
   WebSocketJsonResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import { wsJsonToRes, validEmail, toast } from '../utils';
 import { i18n } from '../i18next';
index d80c77251493017fca6aeb248064b5a397343f16..0f4b7da02d213052ad869743395b9f1a3c584af6 100644 (file)
@@ -13,7 +13,6 @@ import {
   SortType,
   GetSiteResponse,
   ListingType,
-  DataType,
   SiteResponse,
   GetPostsResponse,
   PostResponse,
@@ -26,7 +25,8 @@ import {
   AddAdminResponse,
   BanUserResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { DataType } from '../interfaces';
 import { WebSocketService, UserService } from '../services';
 import { PostListings } from './post-listings';
 import { CommentNodes } from './comment-nodes';
@@ -82,9 +82,9 @@ interface MainProps {
 }
 
 interface UrlParams {
-  listingType?: string;
+  listingType?: ListingType;
   dataType?: string;
-  sort?: string;
+  sort?: SortType;
   page?: number;
 }
 
@@ -151,7 +151,7 @@ export class Main extends Component<any, MainState> {
     }
 
     let listCommunitiesForm: ListCommunitiesForm = {
-      sort: SortType[SortType.Hot],
+      sort: SortType.Hot,
       limit: 6,
     };
 
@@ -334,13 +334,9 @@ export class Main extends Component<any, MainState> {
   }
 
   updateUrl(paramUpdates: UrlParams) {
-    const listingTypeStr =
-      paramUpdates.listingType ||
-      ListingType[this.state.listingType].toLowerCase();
-    const dataTypeStr =
-      paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
-    const sortStr =
-      paramUpdates.sort || SortType[this.state.sort].toLowerCase();
+    const listingTypeStr = paramUpdates.listingType || this.state.listingType;
+    const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
+    const sortStr = paramUpdates.sort || this.state.sort;
     const page = paramUpdates.page || this.state.page;
     this.props.history.push(
       `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
@@ -549,7 +545,7 @@ export class Main extends Component<any, MainState> {
         </span>
         {this.state.listingType == ListingType.All && (
           <a
-            href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
+            href={`/feeds/all.xml?sort=${this.state.sort}`}
             target="_blank"
             rel="noopener"
             title="RSS"
@@ -562,9 +558,7 @@ export class Main extends Component<any, MainState> {
         {UserService.Instance.user &&
           this.state.listingType == ListingType.Subscribed && (
             <a
-              href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
-                SortType[this.state.sort]
-              }`}
+              href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${this.state.sort}`}
               target="_blank"
               title="RSS"
               rel="noopener"
@@ -631,17 +625,17 @@ export class Main extends Component<any, MainState> {
   }
 
   handleSortChange(val: SortType) {
-    this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ sort: val, page: 1 });
     window.scrollTo(0, 0);
   }
 
   handleListingTypeChange(val: ListingType) {
-    this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ listingType: val, page: 1 });
     window.scrollTo(0, 0);
   }
 
   handleDataTypeChange(val: DataType) {
-    this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ dataType: DataType[val], page: 1 });
     window.scrollTo(0, 0);
   }
 
@@ -650,16 +644,16 @@ export class Main extends Component<any, MainState> {
       let getPostsForm: GetPostsForm = {
         page: this.state.page,
         limit: fetchLimit,
-        sort: SortType[this.state.sort],
-        type_: ListingType[this.state.listingType],
+        sort: this.state.sort,
+        type_: this.state.listingType,
       };
       WebSocketService.Instance.getPosts(getPostsForm);
     } else {
       let getCommentsForm: GetCommentsForm = {
         page: this.state.page,
         limit: fetchLimit,
-        sort: SortType[this.state.sort],
-        type_: ListingType[this.state.listingType],
+        sort: this.state.sort,
+        type_: this.state.listingType,
       };
       WebSocketService.Instance.getComments(getCommentsForm);
     }
index 0cc78da73ad82100d332bbd517a8ef9869e8653f..106015a4ab74550d01f5a4810916de2ca4373382 100644 (file)
@@ -19,7 +19,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { wsJsonToRes, addTypeInfo, fetchLimit, toast } from '../utils';
 import { MomentTime } from './moment-time';
index 0ddcbb4cdf3aaa33d6faa036a520c888b3e5c0e3..4ef5276c36c9a72f030483f0a19c3aafe5ece433 100644 (file)
@@ -18,7 +18,7 @@ import {
   PrivateMessage,
   PrivateMessageResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import {
   wsJsonToRes,
   pictrsAvatarThumbnail,
@@ -137,7 +137,7 @@ export class Navbar extends Component<any, NavbarState> {
       this.context.router.history.push(`/search/`);
     } else {
       this.context.router.history.push(
-        `/search/q/${searchParam}/type/all/sort/topall/page/1`
+        `/search/q/${searchParam}/type/All/sort/TopAll/page/1`
       );
     }
   }
@@ -477,14 +477,14 @@ export class Navbar extends Component<any, NavbarState> {
   fetchUnreads() {
     console.log('Fetching unreads...');
     let repliesForm: GetRepliesForm = {
-      sort: SortType[SortType.New],
+      sort: SortType.New,
       unread_only: true,
       page: 1,
       limit: fetchLimit,
     };
 
     let userMentionsForm: GetUserMentionsForm = {
-      sort: SortType[SortType.New],
+      sort: SortType.New,
       unread_only: true,
       page: 1,
       limit: fetchLimit,
index 5b157f7f6245367304774622a1df827cbe4497aa..527f21e045e463362746789269c16b9deeed1a3b 100644 (file)
@@ -9,7 +9,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils';
 import { i18n } from '../i18next';
index 854cff6e9a8d438ccd802cfd89dfc270c4b4bca7..97b44f5fac584cac79b4fed24de9f17c1c73f096 100644 (file)
@@ -18,7 +18,7 @@ import {
   SearchType,
   SearchResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import {
   wsJsonToRes,
@@ -121,7 +121,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
       );
 
     let listCommunitiesForm: ListCommunitiesForm = {
-      sort: SortType[SortType.TopAll],
+      sort: SortType.TopAll,
       limit: 9999,
     };
 
@@ -405,8 +405,8 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
     if (validURL(this.state.postForm.url)) {
       let form: SearchForm = {
         q: this.state.postForm.url,
-        type_: SearchType[SearchType.Url],
-        sort: SortType[SortType.TopAll],
+        type_: SearchType.Url,
+        sort: SortType.TopAll,
         page: 1,
         limit: 6,
       };
@@ -433,8 +433,8 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
   fetchSimilarPosts() {
     let form: SearchForm = {
       q: this.state.postForm.name,
-      type_: SearchType[SearchType.Posts],
-      sort: SortType[SortType.TopAll],
+      type_: SearchType.Posts,
+      sort: SortType.TopAll,
       community_id: this.state.postForm.community_id,
       page: 1,
       limit: 6,
index e3e19e99cbe52c27e5862e0265c7d5ab0a850746..fa4bf391177359df89f823244b21e72c6a925c5e 100644 (file)
@@ -11,14 +11,14 @@ import {
   SavePostForm,
   CommunityUser,
   UserView,
-  BanType,
   BanFromCommunityForm,
   BanUserForm,
   AddModToCommunityForm,
   AddAdminForm,
   TransferSiteForm,
   TransferCommunityForm,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { BanType } from '../interfaces';
 import { MomentTime } from './moment-time';
 import { PostForm } from './post-form';
 import { IFramelyCard } from './iframely-card';
index cd65d9340dec38d2eec22119f870d0483331ac2f..2c9b4a882663e4e154c6e406fbf11fd807c775aa 100644 (file)
@@ -1,6 +1,6 @@
 import { Component } from 'inferno';
 import { Link } from 'inferno-router';
-import { Post, SortType } from '../interfaces';
+import { Post, SortType } from 'lemmy-js-client';
 import { postSort } from '../utils';
 import { PostListing } from './post-listing';
 import { i18n } from '../i18next';
index 06f461f3aa794e6d74ee757f45990fa06a9fe9a9..e9427a5eb32f17243ebc05743db7866c616ebd1a 100644 (file)
@@ -11,8 +11,6 @@ import {
   Comment,
   MarkCommentAsReadForm,
   CommentResponse,
-  CommentSortType,
-  CommentViewType,
   CommunityUser,
   CommunityResponse,
   CommentNode as CommentNodeI,
@@ -28,7 +26,8 @@ import {
   GetSiteResponse,
   GetCommunityResponse,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { CommentSortType, CommentViewType } from '../interfaces';
 import { WebSocketService, UserService } from '../services';
 import {
   wsJsonToRes,
@@ -439,8 +438,8 @@ export class Post extends Component<any, PostState> {
       if (this.state.post.url) {
         let form: SearchForm = {
           q: this.state.post.url,
-          type_: SearchType[SearchType.Url],
-          sort: SortType[SortType.TopAll],
+          type_: SearchType.Url,
+          sort: SortType.TopAll,
           page: 1,
           limit: 6,
         };
index ff889c240c9cc55e786df1fadff4d159403a0ddc..6d7825cd0c08c6455c9edbbd56d4e280222edb40 100644 (file)
@@ -14,7 +14,7 @@ import {
   GetUserDetailsForm,
   SortType,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import {
   capitalizeFirstLetter,
@@ -77,7 +77,7 @@ export class PrivateMessageForm extends Component<
       this.state.privateMessageForm.recipient_id = this.props.params.recipient_id;
       let form: GetUserDetailsForm = {
         user_id: this.state.privateMessageForm.recipient_id,
-        sort: SortType[SortType.New],
+        sort: SortType.New,
         saved_only: false,
       };
       WebSocketService.Instance.getUserDetails(form);
index bb6aca4c11ae43061ae1eab6e52a686b00a56019..243d12e579eac09c1ff2691010af7aad17ee2fa7 100644 (file)
@@ -4,7 +4,7 @@ import {
   PrivateMessage as PrivateMessageI,
   DeletePrivateMessageForm,
   MarkPrivateMessageAsReadForm,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import { mdToHtml, pictrsAvatarThumbnail, showAvatars, toast } from '../utils';
 import { MomentTime } from './moment-time';
index fc19cab9797b5389e5aa88cf44453f7bba0b6bf6..a18cc2d8e757ed219c8cd690bcea5e0c0603ff84 100644 (file)
@@ -17,7 +17,7 @@ import {
   WebSocketJsonResponse,
   GetSiteResponse,
   Site,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import {
   wsJsonToRes,
@@ -57,8 +57,8 @@ interface SearchProps {
 
 interface UrlParams {
   q?: string;
-  type_?: string;
-  sort?: string;
+  type_?: SearchType;
+  sort?: SortType;
   page?: number;
 }
 
@@ -461,8 +461,8 @@ export class Search extends Component<any, SearchState> {
   search() {
     let form: SearchForm = {
       q: this.state.q,
-      type_: SearchType[this.state.type_],
-      sort: SortType[this.state.sort],
+      type_: this.state.type_,
+      sort: this.state.sort,
       page: this.state.page,
       limit: fetchLimit,
     };
@@ -473,12 +473,12 @@ export class Search extends Component<any, SearchState> {
   }
 
   handleSortChange(val: SortType) {
-    this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ sort: val, page: 1 });
   }
 
   handleTypeChange(i: Search, event: any) {
     i.updateUrl({
-      type_: SearchType[Number(event.target.value)].toLowerCase(),
+      type_: SearchType[event.target.value],
       page: 1,
     });
   }
@@ -487,8 +487,8 @@ export class Search extends Component<any, SearchState> {
     event.preventDefault();
     i.updateUrl({
       q: i.state.searchText,
-      type_: SearchType[i.state.type_].toLowerCase(),
-      sort: SortType[i.state.sort].toLowerCase(),
+      type_: i.state.type_,
+      sort: i.state.sort,
       page: i.state.page,
     });
   }
@@ -499,10 +499,8 @@ export class Search extends Component<any, SearchState> {
 
   updateUrl(paramUpdates: UrlParams) {
     const qStr = paramUpdates.q || this.state.q;
-    const typeStr =
-      paramUpdates.type_ || SearchType[this.state.type_].toLowerCase();
-    const sortStr =
-      paramUpdates.sort || SortType[this.state.sort].toLowerCase();
+    const typeStr = paramUpdates.type_ || this.state.type_;
+    const sortStr = paramUpdates.sort || this.state.sort;
     const page = paramUpdates.page || this.state.page;
     this.props.history.push(
       `/search/q/${qStr}/type/${typeStr}/sort/${sortStr}/page/${page}`
index 7da143791e1dbd49ba2547c9aef487fd96be8f23..6360ec5a36022e4fcb7d21aabc748c1b479dbc50 100644 (file)
@@ -7,7 +7,7 @@ import {
   LoginResponse,
   UserOperation,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import { wsJsonToRes, toast } from '../utils';
 import { SiteForm } from './site-form';
index b434bb874537158ccfe50d137217fff5fa956ccc..25cbd79722b65866309624119b3225ae9ac6bee7 100644 (file)
@@ -8,7 +8,7 @@ import {
   RemoveCommunityForm,
   UserView,
   AddModToCommunityForm,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
 import { mdToHtml, getUnixTime } from '../utils';
 import { CommunityForm } from './community-form';
index 98f1259b1b0f72576e2f9a38d76201279777e6b4..9b572f57ecd0ed406dbf4498d3198c58e047e5fa 100644 (file)
@@ -2,7 +2,7 @@ import { Component, linkEvent } from 'inferno';
 import { Prompt } from 'inferno-router';
 import { MarkdownTextArea } from './markdown-textarea';
 import { ImageUploadForm } from './image-upload-form';
-import { Site, SiteForm as SiteFormI } from '../interfaces';
+import { Site, SiteForm as SiteFormI } from 'lemmy-js-client';
 import { WebSocketService } from '../services';
 import { capitalizeFirstLetter, randomStr } from '../utils';
 import { i18n } from '../i18next';
index 778ed65ce55782051d8bc5ba4f8d05ff124e213c..1f0fb055716a0d2202c24a2345ee9e014355875a 100644 (file)
@@ -1,6 +1,6 @@
 import { Component, linkEvent } from 'inferno';
-import { SortType } from '../interfaces';
-import { sortingHelpUrl } from '../utils';
+import { SortType } from 'lemmy-js-client';
+import { sortingHelpUrl, randomStr } from '../utils';
 import { i18n } from '../i18next';
 
 interface SortSelectProps {
@@ -14,6 +14,7 @@ interface SortSelectState {
 }
 
 export class SortSelect extends Component<SortSelectProps, SortSelectState> {
+  private id = `sort-select-${randomStr()}`;
   private emptyState: SortSelectState = {
     sort: this.props.sort,
   };
@@ -33,6 +34,8 @@ export class SortSelect extends Component<SortSelectProps, SortSelectState> {
     return (
       <>
         <select
+          id={this.id}
+          name={this.id}
           value={this.state.sort}
           onChange={linkEvent(this, this.handleSortChange)}
           class="custom-select w-auto mr-2 mb-2"
@@ -68,6 +71,6 @@ export class SortSelect extends Component<SortSelectProps, SortSelectState> {
   }
 
   handleSortChange(i: SortSelect, event: any) {
-    i.props.onChange(Number(event.target.value));
+    i.props.onChange(event.target.value);
   }
 }
index 509ba96c17ed34b435b0ab9b377cd4693a7d34e0..3da9fc2be9f830f1bef602694375a84b316f1aaa 100644 (file)
@@ -8,7 +8,7 @@ import {
   Site,
   WebSocketJsonResponse,
   UserOperation,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { i18n } from '../i18next';
 import { T } from 'inferno-i18next';
 import { repoUrl, wsJsonToRes, toast } from '../utils';
index b3ce294f32651e1a1276f951c5104c7178e354ac..8b6247ca359cbe812d81108ac2199a7ebb1e3856 100644 (file)
@@ -12,11 +12,11 @@ import {
   UserDetailsResponse,
   UserView,
   WebSocketJsonResponse,
-  UserDetailsView,
   CommentResponse,
   BanUserResponse,
   PostResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { UserDetailsView } from '../interfaces';
 import {
   wsJsonToRes,
   toast,
@@ -35,7 +35,7 @@ interface UserDetailsProps {
   user_id?: number;
   page: number;
   limit: number;
-  sort: string;
+  sort: SortType;
   enableDownvotes: boolean;
   enableNsfw: boolean;
   view: UserDetailsView;
@@ -137,7 +137,7 @@ export class UserDetails extends Component<UserDetailsProps, UserDetailsState> {
     ];
 
     // Sort it
-    if (SortType[this.props.sort] === SortType.New) {
+    if (this.props.sort === SortType.New) {
       combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
     } else {
       combined.sort((a, b) => b.data.score - a.data.score);
index 6ab29d0b322ace532d1642c09d47002666a8d3d1..fd296faddce8a2b019a830ed0c8769810404b3a9 100644 (file)
@@ -1,6 +1,6 @@
 import { Component } from 'inferno';
 import { Link } from 'inferno-router';
-import { UserView } from '../interfaces';
+import { UserView } from 'lemmy-js-client';
 import {
   pictrsAvatarThumbnail,
   showAvatars,
index 13cc90acae15fc7095981d92e44df2a48186f318..e4b6c750b570a74c6f5dc0032a5a7f3e78b44f89 100644 (file)
@@ -14,10 +14,10 @@ import {
   DeleteAccountForm,
   WebSocketJsonResponse,
   GetSiteResponse,
-  UserDetailsView,
   UserDetailsResponse,
   AddAdminResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
+import { UserDetailsView } from '../interfaces';
 import { WebSocketService, UserService } from '../services';
 import {
   wsJsonToRes,
@@ -73,7 +73,7 @@ interface UserProps {
 
 interface UrlParams {
   view?: string;
-  sort?: string;
+  sort?: SortType;
   page?: number;
 }
 
@@ -190,17 +190,15 @@ export class User extends Component<any, UserState> {
     );
   }
 
-  static getViewFromProps(view: any): UserDetailsView {
-    return view
-      ? UserDetailsView[capitalizeFirstLetter(view)]
-      : UserDetailsView.Overview;
+  static getViewFromProps(view: string): UserDetailsView {
+    return view ? UserDetailsView[view] : UserDetailsView.Overview;
   }
 
-  static getSortTypeFromProps(sort: any): SortType {
+  static getSortTypeFromProps(sort: string): SortType {
     return sort ? routeSortTypeToEnum(sort) : SortType.New;
   }
 
-  static getPageFromProps(page: any): number {
+  static getPageFromProps(page: number): number {
     return page ? Number(page) : 1;
   }
 
@@ -272,7 +270,7 @@ export class User extends Component<any, UserState> {
             <UserDetails
               user_id={this.state.user_id}
               username={this.state.username}
-              sort={SortType[this.state.sort]}
+              sort={this.state.sort}
               page={this.state.page}
               limit={fetchLimit}
               enableDownvotes={this.state.siteRes.site.enable_downvotes}
@@ -364,9 +362,7 @@ export class User extends Component<any, UserState> {
           hideHot
         />
         <a
-          href={`/feeds/u/${this.state.username}.xml?sort=${
-            SortType[this.state.sort]
-          }`}
+          href={`/feeds/u/${this.state.username}.xml?sort=${this.state.sort}`}
           target="_blank"
           rel="noopener"
           title="RSS"
@@ -538,7 +534,11 @@ export class User extends Component<any, UserState> {
                   <div class="mr-2">{i18n.t('sort_type')}</div>
                 </label>
                 <ListingTypeSelect
-                  type_={this.state.userSettingsForm.default_listing_type}
+                  type_={
+                    Object.values(ListingType)[
+                      this.state.userSettingsForm.default_listing_type
+                    ]
+                  }
                   onChange={this.handleUserSettingsListingTypeChange}
                 />
               </form>
@@ -547,7 +547,11 @@ export class User extends Component<any, UserState> {
                   <div class="mr-2">{i18n.t('type')}</div>
                 </label>
                 <SortSelect
-                  sort={this.state.userSettingsForm.default_sort_type}
+                  sort={
+                    Object.values(SortType)[
+                      this.state.userSettingsForm.default_sort_type
+                    ]
+                  }
                   onChange={this.handleUserSettingsSortTypeChange}
                 />
               </form>
@@ -859,10 +863,8 @@ export class User extends Component<any, UserState> {
 
   updateUrl(paramUpdates: UrlParams) {
     const page = paramUpdates.page || this.state.page;
-    const viewStr =
-      paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
-    const sortStr =
-      paramUpdates.sort || SortType[this.state.sort].toLowerCase();
+    const viewStr = paramUpdates.view || UserDetailsView[this.state.view];
+    const sortStr = paramUpdates.sort || this.state.sort;
     this.props.history.push(
       `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
     );
@@ -873,12 +875,12 @@ export class User extends Component<any, UserState> {
   }
 
   handleSortChange(val: SortType) {
-    this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
+    this.updateUrl({ sort: val, page: 1 });
   }
 
   handleViewChange(i: User, event: any) {
     i.updateUrl({
-      view: UserDetailsView[Number(event.target.value)].toLowerCase(),
+      view: UserDetailsView[Number(event.target.value)],
       page: 1,
     });
   }
@@ -912,12 +914,16 @@ export class User extends Component<any, UserState> {
   }
 
   handleUserSettingsSortTypeChange(val: SortType) {
-    this.state.userSettingsForm.default_sort_type = val;
+    this.state.userSettingsForm.default_sort_type = Object.keys(
+      SortType
+    ).indexOf(val);
     this.setState(this.state);
   }
 
   handleUserSettingsListingTypeChange(val: ListingType) {
-    this.state.userSettingsForm.default_listing_type = val;
+    this.state.userSettingsForm.default_listing_type = Object.keys(
+      ListingType
+    ).indexOf(val);
     this.setState(this.state);
   }
 
index b2995926cb3bb7d1e1d2fa7be16fbba679b8c3c9..3b081183d7f16a4425bce3b9944206492bab3e3d 100644 (file)
@@ -1,64 +1,3 @@
-export enum UserOperation {
-  Login,
-  Register,
-  GetCaptcha,
-  CreateCommunity,
-  CreatePost,
-  ListCommunities,
-  ListCategories,
-  GetPost,
-  GetCommunity,
-  CreateComment,
-  EditComment,
-  DeleteComment,
-  RemoveComment,
-  MarkCommentAsRead,
-  SaveComment,
-  CreateCommentLike,
-  GetPosts,
-  CreatePostLike,
-  EditPost,
-  DeletePost,
-  RemovePost,
-  LockPost,
-  StickyPost,
-  SavePost,
-  EditCommunity,
-  DeleteCommunity,
-  RemoveCommunity,
-  FollowCommunity,
-  GetFollowedCommunities,
-  GetUserDetails,
-  GetReplies,
-  GetUserMentions,
-  MarkUserMentionAsRead,
-  GetModlog,
-  BanFromCommunity,
-  AddModToCommunity,
-  CreateSite,
-  EditSite,
-  GetSite,
-  AddAdmin,
-  BanUser,
-  Search,
-  MarkAllAsRead,
-  SaveUserSettings,
-  TransferCommunity,
-  TransferSite,
-  DeleteAccount,
-  PasswordReset,
-  PasswordChange,
-  CreatePrivateMessage,
-  EditPrivateMessage,
-  DeletePrivateMessage,
-  MarkPrivateMessageAsRead,
-  GetPrivateMessages,
-  UserJoin,
-  GetComments,
-  GetSiteConfig,
-  SaveSiteConfig,
-}
-
 export enum CommentSortType {
   Hot,
   Top,
@@ -71,1007 +10,16 @@ export enum CommentViewType {
   Chat,
 }
 
-export enum ListingType {
-  All,
-  Subscribed,
-  Community,
-}
-
 export enum DataType {
   Post,
   Comment,
 }
 
-export enum SortType {
-  Active,
-  Hot,
-  New,
-  TopDay,
-  TopWeek,
-  TopMonth,
-  TopYear,
-  TopAll,
-}
-
-export enum SearchType {
-  All,
-  Comments,
-  Posts,
-  Communities,
-  Users,
-  Url,
-}
-
-export interface Claims {
-  id: number;
-  iss: string;
-}
-
-export interface User {
-  id: number;
-  name: string;
-  preferred_username?: string;
-  email?: string;
-  avatar?: string;
-  banner?: string;
-  admin: boolean;
-  banned: boolean;
-  published: string;
-  updated?: string;
-  show_nsfw: boolean;
-  theme: string;
-  default_sort_type: SortType;
-  default_listing_type: ListingType;
-  lang: string;
-  show_avatars: boolean;
-  send_notifications_to_email: boolean;
-  matrix_user_id?: string;
-  actor_id: string;
-  bio?: string;
-  local: boolean;
-  last_refreshed_at: string;
-}
-
-export interface UserView {
-  id: number;
-  actor_id: string;
-  name: string;
-  preferred_username?: string;
-  avatar?: string;
-  banner?: string;
-  matrix_user_id?: string;
-  bio?: string;
-  local: boolean;
-  published: string;
-  number_of_posts: number;
-  post_score: number;
-  number_of_comments: number;
-  comment_score: number;
-  banned: boolean;
-}
-
-export interface CommunityUser {
-  id: number;
-  user_id: number;
-  user_actor_id: string;
-  user_local: boolean;
-  user_name: string;
-  user_preferred_username?: string;
-  avatar?: string;
-  community_id: number;
-  community_actor_id: string;
-  community_local: boolean;
-  community_name: string;
-  community_icon?: string;
-  published: string;
-}
-
-export interface Community {
-  id: number;
-  actor_id: string;
-  local: boolean;
-  name: string;
-  title: string;
-  icon?: string;
-  banner?: string;
-  description?: string;
-  category_id: number;
-  creator_id: number;
-  removed: boolean;
-  deleted: boolean;
-  nsfw: boolean;
-  published: string;
-  updated?: string;
-  creator_actor_id: string;
-  creator_local: boolean;
-  last_refreshed_at: string;
-  creator_name: string;
-  creator_preferred_username?: string;
-  creator_avatar?: string;
-  category_name: string;
-  number_of_subscribers: number;
-  number_of_posts: number;
-  number_of_comments: number;
-  user_id?: number;
-  subscribed?: boolean;
-}
-
-export interface Post {
-  id: number;
-  name: string;
-  url?: string;
-  body?: string;
-  creator_id: number;
-  community_id: number;
-  removed: boolean;
-  deleted: boolean;
-  locked: boolean;
-  stickied: boolean;
-  embed_title?: string;
-  embed_description?: string;
-  embed_html?: string;
-  thumbnail_url?: string;
-  ap_id: string;
-  local: boolean;
-  nsfw: boolean;
-  banned: boolean;
-  banned_from_community: boolean;
-  published: string;
-  updated?: string;
-  creator_actor_id: string;
-  creator_local: boolean;
-  creator_name: string;
-  creator_preferred_username?: string;
-  creator_published: string;
-  creator_avatar?: string;
-  community_actor_id: string;
-  community_local: boolean;
-  community_name: string;
-  community_icon?: string;
-  community_removed: boolean;
-  community_deleted: boolean;
-  community_nsfw: boolean;
-  number_of_comments: number;
-  score: number;
-  upvotes: number;
-  downvotes: number;
-  hot_rank: number;
-  hot_rank_active: number;
-  newest_activity_time: string;
-  user_id?: number;
-  my_vote?: number;
-  subscribed?: boolean;
-  read?: boolean;
-  saved?: boolean;
-  duplicates?: Array<Post>;
-}
-
-export interface Comment {
-  id: number;
-  ap_id: string;
-  local: boolean;
-  creator_id: number;
-  post_id: number;
-  post_name: string;
-  parent_id?: number;
-  content: string;
-  removed: boolean;
-  deleted: boolean;
-  read: boolean;
-  published: string;
-  updated?: string;
-  community_id: number;
-  community_actor_id: string;
-  community_local: boolean;
-  community_name: string;
-  community_icon?: string;
-  banned: boolean;
-  banned_from_community: boolean;
-  creator_actor_id: string;
-  creator_local: boolean;
-  creator_name: string;
-  creator_preferred_username?: string;
-  creator_avatar?: string;
-  creator_published: string;
-  score: number;
-  upvotes: number;
-  downvotes: number;
-  hot_rank: number;
-  hot_rank_active: number;
-  user_id?: number;
-  my_vote?: number;
-  subscribed?: number;
-  saved?: boolean;
-  user_mention_id?: number; // For mention type
-  recipient_id?: number;
-  recipient_actor_id?: string;
-  recipient_local?: boolean;
-  depth?: number;
-}
-
-export interface Category {
-  id: number;
-  name: string;
-}
-
-export interface Site {
-  id: number;
-  name: string;
-  description?: string;
-  creator_id: number;
-  published: string;
-  updated?: string;
-  creator_name: string;
-  creator_preferred_username?: string;
-  number_of_users: number;
-  number_of_posts: number;
-  number_of_comments: number;
-  number_of_communities: number;
-  enable_downvotes: boolean;
-  open_registration: boolean;
-  enable_nsfw: boolean;
-  icon?: string;
-  banner?: string;
-}
-
-export interface PrivateMessage {
-  id: number;
-  creator_id: number;
-  recipient_id: number;
-  content: string;
-  deleted: boolean;
-  read: boolean;
-  published: string;
-  updated?: string;
-  ap_id: string;
-  local: boolean;
-  creator_name: string;
-  creator_preferred_username?: string;
-  creator_avatar?: string;
-  creator_actor_id: string;
-  creator_local: boolean;
-  recipient_name: string;
-  recipient_preferred_username?: string;
-  recipient_avatar?: string;
-  recipient_actor_id: string;
-  recipient_local: boolean;
-}
-
 export enum BanType {
   Community,
   Site,
 }
 
-export interface FollowCommunityForm {
-  community_id: number;
-  follow: boolean;
-  auth?: string;
-}
-
-export interface GetFollowedCommunitiesForm {
-  auth: string;
-}
-
-export interface GetFollowedCommunitiesResponse {
-  communities: Array<CommunityUser>;
-}
-
-export interface GetUserDetailsForm {
-  user_id?: number;
-  username?: string;
-  sort: string;
-  page?: number;
-  limit?: number;
-  community_id?: number;
-  saved_only: boolean;
-}
-
-export interface UserDetailsResponse {
-  user: UserView;
-  follows: Array<CommunityUser>;
-  moderates: Array<CommunityUser>;
-  comments: Array<Comment>;
-  posts: Array<Post>;
-  admins: Array<UserView>;
-}
-
-export interface GetRepliesForm {
-  sort: string;
-  page?: number;
-  limit?: number;
-  unread_only: boolean;
-  auth?: string;
-}
-
-export interface GetRepliesResponse {
-  replies: Array<Comment>;
-}
-
-export interface GetUserMentionsForm {
-  sort: string;
-  page?: number;
-  limit?: number;
-  unread_only: boolean;
-  auth?: string;
-}
-
-export interface GetUserMentionsResponse {
-  mentions: Array<Comment>;
-}
-
-export interface MarkUserMentionAsReadForm {
-  user_mention_id: number;
-  read: boolean;
-  auth?: string;
-}
-
-export interface UserMentionResponse {
-  mention: Comment;
-}
-
-export interface BanFromCommunityForm {
-  community_id: number;
-  user_id: number;
-  ban: boolean;
-  remove_data?: boolean;
-  reason?: string;
-  expires?: number;
-  auth?: string;
-}
-
-export interface BanFromCommunityResponse {
-  user: UserView;
-  banned: boolean;
-}
-
-export interface AddModToCommunityForm {
-  community_id: number;
-  user_id: number;
-  added: boolean;
-  auth?: string;
-}
-
-export interface TransferCommunityForm {
-  community_id: number;
-  user_id: number;
-  auth?: string;
-}
-
-export interface TransferSiteForm {
-  user_id: number;
-  auth?: string;
-}
-
-export interface AddModToCommunityResponse {
-  moderators: Array<CommunityUser>;
-}
-
-export interface GetModlogForm {
-  mod_user_id?: number;
-  community_id?: number;
-  page?: number;
-  limit?: number;
-}
-
-export interface GetModlogResponse {
-  removed_posts: Array<ModRemovePost>;
-  locked_posts: Array<ModLockPost>;
-  stickied_posts: Array<ModStickyPost>;
-  removed_comments: Array<ModRemoveComment>;
-  removed_communities: Array<ModRemoveCommunity>;
-  banned_from_community: Array<ModBanFromCommunity>;
-  banned: Array<ModBan>;
-  added_to_community: Array<ModAddCommunity>;
-  added: Array<ModAdd>;
-}
-
-export interface ModRemovePost {
-  id: number;
-  mod_user_id: number;
-  post_id: number;
-  reason?: string;
-  removed?: boolean;
-  when_: string;
-  mod_user_name: string;
-  post_name: string;
-  community_id: number;
-  community_name: string;
-}
-
-export interface ModLockPost {
-  id: number;
-  mod_user_id: number;
-  post_id: number;
-  locked?: boolean;
-  when_: string;
-  mod_user_name: string;
-  post_name: string;
-  community_id: number;
-  community_name: string;
-}
-
-export interface ModStickyPost {
-  id: number;
-  mod_user_id: number;
-  post_id: number;
-  stickied?: boolean;
-  when_: string;
-  mod_user_name: string;
-  post_name: string;
-  community_id: number;
-  community_name: string;
-}
-
-export interface ModRemoveComment {
-  id: number;
-  mod_user_id: number;
-  comment_id: number;
-  reason?: string;
-  removed?: boolean;
-  when_: string;
-  mod_user_name: string;
-  comment_user_id: number;
-  comment_user_name: string;
-  comment_content: string;
-  post_id: number;
-  post_name: string;
-  community_id: number;
-  community_name: string;
-}
-
-export interface ModRemoveCommunity {
-  id: number;
-  mod_user_id: number;
-  community_id: number;
-  reason?: string;
-  removed?: boolean;
-  expires?: number;
-  when_: string;
-  mod_user_name: string;
-  community_name: string;
-}
-
-export interface ModBanFromCommunity {
-  id: number;
-  mod_user_id: number;
-  other_user_id: number;
-  community_id: number;
-  reason?: string;
-  banned?: boolean;
-  expires?: number;
-  when_: string;
-  mod_user_name: string;
-  other_user_name: string;
-  community_name: string;
-}
-
-export interface ModBan {
-  id: number;
-  mod_user_id: number;
-  other_user_id: number;
-  reason?: string;
-  banned?: boolean;
-  expires?: number;
-  when_: string;
-  mod_user_name: string;
-  other_user_name: string;
-}
-
-export interface ModAddCommunity {
-  id: number;
-  mod_user_id: number;
-  other_user_id: number;
-  community_id: number;
-  removed?: boolean;
-  when_: string;
-  mod_user_name: string;
-  other_user_name: string;
-  community_name: string;
-}
-
-export interface ModAdd {
-  id: number;
-  mod_user_id: number;
-  other_user_id: number;
-  removed?: boolean;
-  when_: string;
-  mod_user_name: string;
-  other_user_name: string;
-}
-
-export interface LoginForm {
-  username_or_email: string;
-  password: string;
-}
-
-export interface RegisterForm {
-  username: string;
-  email?: string;
-  password: string;
-  password_verify: string;
-  admin: boolean;
-  show_nsfw: boolean;
-  captcha_uuid?: string;
-  captcha_answer?: string;
-}
-
-export interface GetCaptchaResponse {
-  ok?: {
-    png: string;
-    wav?: string;
-    uuid: string;
-  };
-}
-
-export interface LoginResponse {
-  jwt: string;
-}
-
-export interface UserSettingsForm {
-  show_nsfw: boolean;
-  theme: string;
-  default_sort_type: SortType;
-  default_listing_type: ListingType;
-  lang: string;
-  avatar?: string;
-  banner?: string;
-  preferred_username?: string;
-  email?: string;
-  bio?: string;
-  matrix_user_id?: string;
-  new_password?: string;
-  new_password_verify?: string;
-  old_password?: string;
-  show_avatars: boolean;
-  send_notifications_to_email: boolean;
-  auth: string;
-}
-
-export interface CommunityForm {
-  name: string;
-  edit_id?: number;
-  title: string;
-  description?: string;
-  icon?: string;
-  banner?: string;
-  category_id: number;
-  nsfw: boolean;
-  auth?: string;
-}
-
-export interface DeleteCommunityForm {
-  edit_id: number;
-  deleted: boolean;
-  auth?: string;
-}
-
-export interface RemoveCommunityForm {
-  edit_id: number;
-  removed: boolean;
-  reason?: string;
-  expires?: number;
-  auth?: string;
-}
-
-export interface GetCommunityForm {
-  id?: number;
-  name?: string;
-  auth?: string;
-}
-
-export interface GetCommunityResponse {
-  community: Community;
-  moderators: Array<CommunityUser>;
-  online: number;
-}
-
-export interface CommunityResponse {
-  community: Community;
-}
-
-export interface ListCommunitiesForm {
-  sort: string;
-  page?: number;
-  limit?: number;
-  auth?: string;
-}
-
-export interface ListCommunitiesResponse {
-  communities: Array<Community>;
-}
-
-export interface ListCategoriesResponse {
-  categories: Array<Category>;
-}
-
-export interface PostForm {
-  name: string;
-  url?: string;
-  body?: string;
-  community_id?: number;
-  edit_id?: number;
-  nsfw: boolean;
-  auth: string;
-}
-
-export interface DeletePostForm {
-  edit_id: number;
-  deleted: boolean;
-  auth: string;
-}
-
-export interface RemovePostForm {
-  edit_id: number;
-  removed: boolean;
-  reason?: string;
-  auth: string;
-}
-
-export interface LockPostForm {
-  edit_id: number;
-  locked: boolean;
-  auth: string;
-}
-
-export interface StickyPostForm {
-  edit_id: number;
-  stickied: boolean;
-  auth: string;
-}
-
-export interface PostFormParams {
-  name: string;
-  url?: string;
-  body?: string;
-  community?: string;
-}
-
-export interface GetPostForm {
-  id: number;
-  auth?: string;
-}
-
-export interface GetPostResponse {
-  post: Post;
-  comments: Array<Comment>;
-  community: Community;
-  moderators: Array<CommunityUser>;
-  online: number;
-}
-
-export interface SavePostForm {
-  post_id: number;
-  save: boolean;
-  auth?: string;
-}
-
-export interface PostResponse {
-  post: Post;
-}
-
-export interface CommentForm {
-  content: string;
-  post_id?: number;
-  parent_id?: number;
-  edit_id?: number;
-  creator_id?: number;
-  form_id?: string;
-  auth: string;
-}
-
-export interface DeleteCommentForm {
-  edit_id: number;
-  deleted: boolean;
-  auth: string;
-}
-
-export interface RemoveCommentForm {
-  edit_id: number;
-  removed: boolean;
-  reason?: string;
-  auth: string;
-}
-
-export interface MarkCommentAsReadForm {
-  edit_id: number;
-  read: boolean;
-  auth: string;
-}
-
-export interface SaveCommentForm {
-  comment_id: number;
-  save: boolean;
-  auth?: string;
-}
-
-export interface CommentResponse {
-  comment: Comment;
-  recipient_ids: Array<number>;
-  form_id?: string;
-}
-
-export interface CommentLikeForm {
-  comment_id: number;
-  score: number;
-  auth?: string;
-}
-
-export interface CommentNode {
-  comment: Comment;
-  children?: Array<CommentNode>;
-}
-
-export interface GetPostsForm {
-  type_: string;
-  sort: string;
-  page?: number;
-  limit?: number;
-  community_id?: number;
-  auth?: string;
-}
-
-export interface GetPostsResponse {
-  posts: Array<Post>;
-}
-
-export interface GetCommentsForm {
-  type_: string;
-  sort: string;
-  page?: number;
-  limit: number;
-  community_id?: number;
-  auth?: string;
-}
-
-export interface GetCommentsResponse {
-  comments: Array<Comment>;
-}
-
-export interface CreatePostLikeForm {
-  post_id: number;
-  score: number;
-  auth?: string;
-}
-
-export interface SiteForm {
-  name: string;
-  description?: string;
-  icon?: string;
-  banner?: string;
-  enable_downvotes: boolean;
-  open_registration: boolean;
-  enable_nsfw: boolean;
-  auth?: string;
-}
-
-export interface GetSiteConfig {
-  auth?: string;
-}
-
-export interface GetSiteForm {
-  auth?: string;
-}
-
-export interface GetSiteConfigResponse {
-  config_hjson: string;
-}
-
-export interface SiteConfigForm {
-  config_hjson: string;
-  auth?: string;
-}
-
-export interface GetSiteResponse {
-  site: Site;
-  admins: Array<UserView>;
-  banned: Array<UserView>;
-  online: number;
-  version: string;
-  my_user?: User;
-  federated_instances: Array<string>;
-}
-
-export interface SiteResponse {
-  site: Site;
-}
-
-export interface BanUserForm {
-  user_id: number;
-  ban: boolean;
-  remove_data?: boolean;
-  reason?: string;
-  expires?: number;
-  auth?: string;
-}
-
-export interface BanUserResponse {
-  user: UserView;
-  banned: boolean;
-}
-
-export interface AddAdminForm {
-  user_id: number;
-  added: boolean;
-  auth?: string;
-}
-
-export interface AddAdminResponse {
-  admins: Array<UserView>;
-}
-
-export interface SearchForm {
-  q: string;
-  type_: string;
-  community_id?: number;
-  sort: string;
-  page?: number;
-  limit?: number;
-  auth?: string;
-}
-
-export interface SearchResponse {
-  type_: string;
-  posts?: Array<Post>;
-  comments?: Array<Comment>;
-  communities: Array<Community>;
-  users: Array<UserView>;
-}
-
-export interface DeleteAccountForm {
-  password: string;
-}
-
-export interface PasswordResetForm {
-  email: string;
-}
-
-// export interface PasswordResetResponse {
-// }
-
-export interface PasswordChangeForm {
-  token: string;
-  password: string;
-  password_verify: string;
-}
-
-export interface PrivateMessageForm {
-  content: string;
-  recipient_id: number;
-  auth?: string;
-}
-
-export interface PrivateMessageFormParams {
-  recipient_id: number;
-}
-
-export interface EditPrivateMessageForm {
-  edit_id: number;
-  content: string;
-  auth?: string;
-}
-
-export interface DeletePrivateMessageForm {
-  edit_id: number;
-  deleted: boolean;
-  auth?: string;
-}
-
-export interface MarkPrivateMessageAsReadForm {
-  edit_id: number;
-  read: boolean;
-  auth?: string;
-}
-
-export interface GetPrivateMessagesForm {
-  unread_only: boolean;
-  page?: number;
-  limit?: number;
-  auth?: string;
-}
-
-export interface PrivateMessagesResponse {
-  messages: Array<PrivateMessage>;
-}
-
-export interface PrivateMessageResponse {
-  message: PrivateMessage;
-}
-
-export interface UserJoinForm {
-  auth: string;
-}
-
-export interface UserJoinResponse {
-  user_id: number;
-}
-
-export type MessageType =
-  | LoginForm
-  | RegisterForm
-  | CommunityForm
-  | DeleteCommunityForm
-  | RemoveCommunityForm
-  | FollowCommunityForm
-  | ListCommunitiesForm
-  | GetFollowedCommunitiesForm
-  | PostForm
-  | DeletePostForm
-  | RemovePostForm
-  | LockPostForm
-  | StickyPostForm
-  | GetPostForm
-  | GetPostsForm
-  | GetCommunityForm
-  | CommentForm
-  | DeleteCommentForm
-  | RemoveCommentForm
-  | MarkCommentAsReadForm
-  | CommentLikeForm
-  | SaveCommentForm
-  | CreatePostLikeForm
-  | BanFromCommunityForm
-  | AddAdminForm
-  | AddModToCommunityForm
-  | TransferCommunityForm
-  | TransferSiteForm
-  | SaveCommentForm
-  | BanUserForm
-  | AddAdminForm
-  | GetUserDetailsForm
-  | GetRepliesForm
-  | GetUserMentionsForm
-  | MarkUserMentionAsReadForm
-  | GetModlogForm
-  | SiteForm
-  | SearchForm
-  | UserSettingsForm
-  | DeleteAccountForm
-  | PasswordResetForm
-  | PasswordChangeForm
-  | PrivateMessageForm
-  | EditPrivateMessageForm
-  | DeletePrivateMessageForm
-  | MarkPrivateMessageAsReadForm
-  | GetPrivateMessagesForm
-  | SiteConfigForm;
-
-type ResponseType =
-  | SiteResponse
-  | GetFollowedCommunitiesResponse
-  | ListCommunitiesResponse
-  | GetPostsResponse
-  | PostResponse
-  | GetRepliesResponse
-  | GetUserMentionsResponse
-  | ListCategoriesResponse
-  | CommunityResponse
-  | CommentResponse
-  | UserMentionResponse
-  | LoginResponse
-  | GetCaptchaResponse
-  | GetModlogResponse
-  | SearchResponse
-  | BanFromCommunityResponse
-  | AddModToCommunityResponse
-  | BanUserResponse
-  | AddAdminResponse
-  | PrivateMessageResponse
-  | PrivateMessagesResponse
-  | GetSiteConfigResponse
-  | GetSiteResponse;
-
-export interface WebSocketResponse {
-  op: UserOperation;
-  data: ResponseType;
-}
-
-export interface WebSocketJsonResponse {
-  op?: string;
-  data?: ResponseType;
-  error?: string;
-  reconnect?: boolean;
-}
-
 export enum UserDetailsView {
   Overview,
   Comments,
index 124393fd94038b5f24f39ee23c575843e918abc8..513ba608079ea29e115b562141cb0d8e32d84879 100644 (file)
@@ -1,9 +1,14 @@
 import Cookies from 'js-cookie';
-import { User, Claims, LoginResponse } from '../interfaces';
+import { User, LoginResponse } from 'lemmy-js-client';
 import { setTheme } from '../utils';
 import jwt_decode from 'jwt-decode';
 import { Subject, BehaviorSubject } from 'rxjs';
 
+interface Claims {
+  id: number;
+  iss: string;
+}
+
 export class UserService {
   private static _instance: UserService;
   public user: User;
index c73de21023a2a4319c932802521b19054fab16ad..93587c9901a6ed617e41ed95ea8f7a278a052e2f 100644 (file)
@@ -1,8 +1,8 @@
 import { wsUri } from '../env';
 import {
+  LemmyWebsocket,
   LoginForm,
   RegisterForm,
-  UserOperation,
   CommunityForm,
   DeleteCommunityForm,
   RemoveCommunityForm,
@@ -53,9 +53,9 @@ import {
   GetSiteConfig,
   GetSiteForm,
   SiteConfigForm,
-  MessageType,
+  MarkAllAsReadForm,
   WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
 import { UserService } from './';
 import { i18n } from '../i18next';
 import { toast } from '../utils';
@@ -70,6 +70,7 @@ export class WebSocketService {
 
   public admins: Array<UserView>;
   public banned: Array<UserView>;
+  private client = new LemmyWebsocket();
 
   private constructor() {
     this.ws = new ReconnectingWebSocket(wsUri);
@@ -100,301 +101,287 @@ export class WebSocketService {
 
   public userJoin() {
     let form: UserJoinForm = { auth: UserService.Instance.auth };
-    this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
+    this.ws.send(this.client.userJoin(form));
   }
 
-  public login(loginForm: LoginForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
+  public login(form: LoginForm) {
+    this.ws.send(this.client.login(form));
   }
 
-  public register(registerForm: RegisterForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
+  public register(form: RegisterForm) {
+    this.ws.send(this.client.register(form));
   }
 
   public getCaptcha() {
-    this.ws.send(this.wsSendWrapper(UserOperation.GetCaptcha, {}));
+    this.ws.send(this.client.getCaptcha());
   }
 
   public createCommunity(form: CommunityForm) {
-    this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
+    this.setAuth(form); // TODO all these setauths at some point would be good to make required
+    this.ws.send(this.client.createCommunity(form));
   }
 
   public editCommunity(form: CommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
+    this.ws.send(this.client.editCommunity(form));
   }
 
   public deleteCommunity(form: DeleteCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
+    this.ws.send(this.client.deleteCommunity(form));
   }
 
   public removeCommunity(form: RemoveCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
+    this.ws.send(this.client.removeCommunity(form));
   }
 
-  public followCommunity(followCommunityForm: FollowCommunityForm) {
-    this.setAuth(followCommunityForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
-    );
+  public followCommunity(form: FollowCommunityForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.followCommunity(form));
   }
 
   public listCommunities(form: ListCommunitiesForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
+    this.ws.send(this.client.listCommunities(form));
   }
 
   public getFollowedCommunities() {
     let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
-    );
+    this.ws.send(this.client.getFollowedCommunities(form));
   }
 
   public listCategories() {
-    this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
+    this.ws.send(this.client.listCategories());
   }
 
   public createPost(form: PostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, form));
+    this.ws.send(this.client.createPost(form));
   }
 
   public getPost(form: GetPostForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
+    this.ws.send(this.client.getPost(form));
   }
 
   public getCommunity(form: GetCommunityForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
+    this.ws.send(this.client.getCommunity(form));
   }
 
   public createComment(form: CommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, form));
+    this.ws.send(this.client.createComment(form));
   }
 
   public editComment(form: CommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditComment, form));
+    this.ws.send(this.client.editComment(form));
   }
 
   public deleteComment(form: DeleteCommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeleteComment, form));
+    this.ws.send(this.client.deleteComment(form));
   }
 
   public removeComment(form: RemoveCommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.RemoveComment, form));
+    this.ws.send(this.client.removeComment(form));
   }
 
   public markCommentAsRead(form: MarkCommentAsReadForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.MarkCommentAsRead, form));
+    this.ws.send(this.client.markCommentAsRead(form));
   }
 
   public likeComment(form: CommentLikeForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
+    this.ws.send(this.client.likeComment(form));
   }
 
   public saveComment(form: SaveCommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
+    this.ws.send(this.client.saveComment(form));
   }
 
   public getPosts(form: GetPostsForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
+    this.ws.send(this.client.getPosts(form));
   }
 
   public getComments(form: GetCommentsForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
+    this.ws.send(this.client.getComments(form));
   }
 
   public likePost(form: CreatePostLikeForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
+    this.ws.send(this.client.likePost(form));
   }
 
   public editPost(form: PostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditPost, form));
+    this.ws.send(this.client.editPost(form));
   }
 
   public deletePost(form: DeletePostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeletePost, form));
+    this.ws.send(this.client.deletePost(form));
   }
 
   public removePost(form: RemovePostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.RemovePost, form));
+    this.ws.send(this.client.removePost(form));
   }
 
   public lockPost(form: LockPostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.LockPost, form));
+    this.ws.send(this.client.lockPost(form));
   }
 
   public stickyPost(form: StickyPostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.StickyPost, form));
+    this.ws.send(this.client.stickyPost(form));
   }
 
   public savePost(form: SavePostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
+    this.ws.send(this.client.savePost(form));
   }
 
   public banFromCommunity(form: BanFromCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
+    this.ws.send(this.client.banFromCommunity(form));
   }
 
   public addModToCommunity(form: AddModToCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
+    this.ws.send(this.client.addModToCommunity(form));
   }
 
   public transferCommunity(form: TransferCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
+    this.ws.send(this.client.transferCommunity(form));
   }
 
   public transferSite(form: TransferSiteForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
+    this.ws.send(this.client.transferSite(form));
   }
 
   public banUser(form: BanUserForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
+    this.ws.send(this.client.banUser(form));
   }
 
   public addAdmin(form: AddAdminForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
+    this.ws.send(this.client.addAdmin(form));
   }
 
   public getUserDetails(form: GetUserDetailsForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
+    this.ws.send(this.client.getUserDetails(form));
   }
 
   public getReplies(form: GetRepliesForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
+    this.ws.send(this.client.getReplies(form));
   }
 
   public getUserMentions(form: GetUserMentionsForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
+    this.ws.send(this.client.getUserMentions(form));
   }
 
   public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
+    this.ws.send(this.client.markUserMentionAsRead(form));
   }
 
   public getModlog(form: GetModlogForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
+    this.ws.send(this.client.getModlog(form));
   }
 
-  public createSite(siteForm: SiteForm) {
-    this.setAuth(siteForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
+  public createSite(form: SiteForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.createSite(form));
   }
 
-  public editSite(siteForm: SiteForm) {
-    this.setAuth(siteForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
+  public editSite(form: SiteForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.editSite(form));
   }
 
   public getSite(form: GetSiteForm = {}) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetSite, form));
+    this.ws.send(this.client.getSite(form));
   }
 
   public getSiteConfig() {
-    let siteConfig: GetSiteConfig = {};
-    this.setAuth(siteConfig);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
+    let form: GetSiteConfig = {};
+    this.setAuth(form);
+    this.ws.send(this.client.getSiteConfig(form));
   }
 
   public search(form: SearchForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
+    this.ws.send(this.client.search(form));
   }
 
   public markAllAsRead() {
-    let form = {};
+    let form: MarkAllAsReadForm;
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
+    this.ws.send(this.client.markAllAsRead(form));
   }
 
-  public saveUserSettings(userSettingsForm: UserSettingsForm) {
-    this.setAuth(userSettingsForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
-    );
+  public saveUserSettings(form: UserSettingsForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.saveUserSettings(form));
   }
 
   public deleteAccount(form: DeleteAccountForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
+    this.ws.send(this.client.deleteAccount(form));
   }
 
   public passwordReset(form: PasswordResetForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
+    this.ws.send(this.client.passwordReset(form));
   }
 
   public passwordChange(form: PasswordChangeForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
+    this.ws.send(this.client.passwordChange(form));
   }
 
   public createPrivateMessage(form: PrivateMessageForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
+    this.ws.send(this.client.createPrivateMessage(form));
   }
 
   public editPrivateMessage(form: EditPrivateMessageForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
+    this.ws.send(this.client.editPrivateMessage(form));
   }
 
   public deletePrivateMessage(form: DeletePrivateMessageForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
+    this.ws.send(this.client.deletePrivateMessage(form));
   }
 
   public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
     this.setAuth(form);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
-    );
+    this.ws.send(this.client.markPrivateMessageAsRead(form));
   }
 
   public getPrivateMessages(form: GetPrivateMessagesForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
+    this.ws.send(this.client.getPrivateMessages(form));
   }
 
   public saveSiteConfig(form: SiteConfigForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
-  }
-
-  private wsSendWrapper(op: UserOperation, data: MessageType) {
-    let send = { op: UserOperation[op], data: data };
-    console.log(send);
-    return JSON.stringify(send);
+    this.ws.send(this.client.saveSiteConfig(form));
   }
 
   private setAuth(obj: any, throwErr: boolean = true) {
index ae2785a6bbeafd2b94e4925a755cd919038c33aa..f00a095ac23d66630a105b20ca2026e38e81369b 100644 (file)
@@ -34,9 +34,7 @@ import {
   PrivateMessage,
   User,
   SortType,
-  CommentSortType,
   ListingType,
-  DataType,
   SearchType,
   WebSocketResponse,
   WebSocketJsonResponse,
@@ -44,7 +42,9 @@ import {
   SearchResponse,
   CommentResponse,
   PostResponse,
-} from './interfaces';
+} from 'lemmy-js-client';
+
+import { CommentSortType, DataType } from './interfaces';
 import { UserService, WebSocketService } from './services';
 
 import Tribute from 'tributejs/src/Tribute.js';
@@ -273,27 +273,11 @@ export function capitalizeFirstLetter(str: string): string {
 }
 
 export function routeSortTypeToEnum(sort: string): SortType {
-  if (sort == 'new') {
-    return SortType.New;
-  } else if (sort == 'hot') {
-    return SortType.Hot;
-  } else if (sort == 'active') {
-    return SortType.Active;
-  } else if (sort == 'topday') {
-    return SortType.TopDay;
-  } else if (sort == 'topweek') {
-    return SortType.TopWeek;
-  } else if (sort == 'topmonth') {
-    return SortType.TopMonth;
-  } else if (sort == 'topyear') {
-    return SortType.TopYear;
-  } else if (sort == 'topall') {
-    return SortType.TopAll;
-  }
+  return SortType[sort];
 }
 
 export function routeListingTypeToEnum(type: string): ListingType {
-  return ListingType[capitalizeFirstLetter(type)];
+  return ListingType[type];
 }
 
 export function routeDataTypeToEnum(type: string): DataType {
@@ -730,8 +714,8 @@ function userSearch(text: string, cb: any) {
   if (text) {
     let form: SearchForm = {
       q: text,
-      type_: SearchType[SearchType.Users],
-      sort: SortType[SortType.TopAll],
+      type_: SearchType.Users,
+      sort: SortType.TopAll,
       page: 1,
       limit: mentionDropdownFetchLimit,
     };
@@ -767,8 +751,8 @@ function communitySearch(text: string, cb: any) {
   if (text) {
     let form: SearchForm = {
       q: text,
-      type_: SearchType[SearchType.Communities],
-      sort: SortType[SortType.TopAll],
+      type_: SearchType.Communities,
+      sort: SortType.TopAll,
       page: 1,
       limit: mentionDropdownFetchLimit,
     };
@@ -804,7 +788,7 @@ export function getListingTypeFromProps(props: any): ListingType {
   return props.match.params.listing_type
     ? routeListingTypeToEnum(props.match.params.listing_type)
     : UserService.Instance.user
-    ? UserService.Instance.user.default_listing_type
+    ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
     : ListingType.All;
 }
 
@@ -819,7 +803,7 @@ export function getSortTypeFromProps(props: any): SortType {
   return props.match.params.sort
     ? routeSortTypeToEnum(props.match.params.sort)
     : UserService.Instance.user
-    ? UserService.Instance.user.default_sort_type
+    ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
     : SortType.Active;
 }
 
index c4f77395992e2c28fe3dd1692722d7b5d586ab50..a474636ed71510010d8c383dbec6f77355e151fe 100644 (file)
@@ -4938,6 +4938,11 @@ lego-api@^1.0.7:
   dependencies:
     chain-able "^3.0.0"
 
+lemmy-js-client@^1.0.8:
+  version "1.0.8"
+  resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-1.0.8.tgz#98e34c8e3cd07427f883f60fad376dc4d6f46e7f"
+  integrity sha512-YZxD3+8RGz7cRKdI8EIe5iQqQIMm5WzdNz6zZ7/CdkMtXUv6YuMOEv8HLTvBoGuaWIJwlMJ+23NIarxlT26IEw==
+
 leven@^3.1.0:
   version "3.1.0"
   resolved "https://registry.yarnpkg.com/leven/-/leven-3.1.0.tgz#77891de834064cccba82ae7842bb6b14a13ed7f2"