]> Untitled Git - lemmy-ui.git/commitdiff
Actually sending ws requests, some other fixes.
authorDessalines <tyhou13@gmx.com>
Thu, 24 Dec 2020 22:05:57 +0000 (17:05 -0500)
committerDessalines <tyhou13@gmx.com>
Thu, 24 Dec 2020 22:05:57 +0000 (17:05 -0500)
32 files changed:
Dockerfile
src/server/index.tsx
src/shared/components/admin-settings.tsx
src/shared/components/app.tsx
src/shared/components/comment-form.tsx
src/shared/components/comment-node.tsx
src/shared/components/communities.tsx
src/shared/components/community-form.tsx
src/shared/components/community.tsx
src/shared/components/create-community.tsx
src/shared/components/create-post.tsx
src/shared/components/create-private-message.tsx
src/shared/components/inbox.tsx
src/shared/components/login.tsx
src/shared/components/main.tsx
src/shared/components/modlog.tsx
src/shared/components/navbar.tsx
src/shared/components/password_change.tsx
src/shared/components/post-form.tsx
src/shared/components/post-listing.tsx
src/shared/components/post.tsx
src/shared/components/private-message-form.tsx
src/shared/components/private-message.tsx
src/shared/components/search.tsx
src/shared/components/setup.tsx
src/shared/components/sidebar.tsx
src/shared/components/site-form.tsx
src/shared/components/user.tsx
src/shared/env.ts
src/shared/services/UserService.ts
src/shared/services/WebSocketService.ts
src/shared/utils.ts

index 745651b54f7728b4054a320b2105d133ad816e23..278142202aeb2a7e0f0167875d9e3ec02eee0d29 100644 (file)
@@ -1,5 +1,5 @@
 FROM node:14-alpine as builder\r
-RUN apk update && apk add yarn curl bash && rm -rf /var/cache/apk/*\r
+RUN apk add yarn curl bash --no-cache\r
 \r
 RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin\r
 \r
@@ -24,7 +24,7 @@ RUN yarn build:prod
 \r
 # Pruning\r
 # RUN npm prune --production\r
-RUN /usr/local/bin/node-prune\r
+RUN node-prune\r
 \r
 FROM node:14-alpine as runner\r
 COPY --from=builder /usr/src/app/dist /app/dist\r
index a2bcd4abe9c0918497d524809404b7a801dc77c2..10e935d902415e6deb66565da34447ad4dfc3ec4 100644 (file)
@@ -14,6 +14,7 @@ import { Helmet } from 'inferno-helmet';
 import { initializeSite } from '../shared/initialize';
 import { httpUri } from '../shared/env';
 import { IncomingHttpHeaders } from 'http';
+import { setOptionalAuth } from '../shared/utils';
 
 const server = express();
 const port = 1234;
@@ -29,7 +30,8 @@ server.get('/*', async (req, res) => {
   const context = {} as any;
   let auth: string = IsomorphicCookie.load('jwt', req);
 
-  let getSiteForm: GetSite = { auth };
+  let getSiteForm: GetSite = {};
+  setOptionalAuth(getSiteForm, auth);
 
   let promises: Promise<any>[] = [];
 
index b9f93f489cd9381bdb5185ed8ce4516a59cb7d18..6b985a434a24b3ba9c343e3bacdafa2a194d58de 100644 (file)
@@ -8,7 +8,7 @@ import {
   GetSiteConfigResponse,
   GetSiteConfig,
 } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
+import { WebSocketService } from '../services';
 import {
   wsJsonToRes,
   capitalizeFirstLetter,
@@ -18,6 +18,8 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import autosize from 'autosize';
 import { SiteForm } from './site-form';
@@ -42,7 +44,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
     siteRes: this.isoData.site_res,
     siteConfigForm: {
       config_hjson: null,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     siteConfigRes: {
       config_hjson: null,
@@ -66,9 +68,11 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
       this.state.siteConfigLoading = false;
       this.state.loading = false;
     } else {
-      WebSocketService.Instance.client.getSiteConfig({
-        auth: UserService.Instance.authField(),
-      });
+      WebSocketService.Instance.send(
+        wsClient.getSiteConfig({
+          auth: authField(),
+        })
+      );
     }
   }
 
@@ -198,7 +202,9 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
   handleSiteConfigSubmit(i: AdminSettings, event: any) {
     event.preventDefault();
     i.state.siteConfigLoading = true;
-    WebSocketService.Instance.client.saveSiteConfig(i.state.siteConfigForm);
+    WebSocketService.Instance.send(
+      wsClient.saveSiteConfig(i.state.siteConfigForm)
+    );
     i.setState(i.state);
   }
 
index 26a2886950c9db6fded0ab7d4f63c139f0dc5061..8c9fa9f96af023f7fdf7eeebd5f37128479de177 100644 (file)
@@ -28,7 +28,7 @@ export class App extends Component<AppProps, any> {
           <div>
             <Theme user={siteRes.my_user} />
             {siteRes &&
-              siteRes.site_view.site &&
+              siteRes.site_view &&
               this.props.siteRes.site_view.site.icon && (
                 <Helmet>
                   <link
index 5a0d404a9c8d3cb61906ed596e32ddcc06311153..3e81d77525a66c023ce1752140cd26638b03e6d5 100644 (file)
@@ -9,7 +9,9 @@ import {
 } from 'lemmy-js-client';
 import { CommentNode as CommentNodeI } from '../interfaces';
 import {
+  authField,
   capitalizeFirstLetter,
+  wsClient,
   wsJsonToRes,
   wsSubscribe,
   wsUserOp,
@@ -21,11 +23,11 @@ import { MarkdownTextArea } from './markdown-textarea';
 
 interface CommentFormProps {
   postId?: number;
-  node?: CommentNodeI;
-  onReplyCancel?(): any;
+  node?: CommentNodeI; // Can either be the parent, or the editable comment
   edit?: boolean;
   disabled?: boolean;
   focus?: boolean;
+  onReplyCancel?(): any;
 }
 
 interface CommentFormState {
@@ -68,7 +70,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
         {UserService.Instance.user ? (
           <MarkdownTextArea
             initialContent={
-              this.props.node
+              this.props.edit
                 ? this.props.node.comment_view.comment.content
                 : null
             }
@@ -108,18 +110,18 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
         content,
         form_id: this.state.formId,
         edit_id: node.comment_view.comment.id,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.editComment(form);
+      WebSocketService.Instance.send(wsClient.editComment(form));
     } else {
       let form: CreateComment = {
         content,
         form_id: this.state.formId,
         post_id: node ? node.comment_view.post.id : this.props.postId,
-        parent_id: node ? node.comment_view.comment.parent_id : null,
-        auth: UserService.Instance.authField(),
+        parent_id: node ? node.comment_view.comment.id : null,
+        auth: authField(),
       };
-      WebSocketService.Instance.client.createComment(form);
+      WebSocketService.Instance.send(wsClient.createComment(form));
     }
     this.setState(this.state);
   }
index 3d69812c95581d0939f23022ef5926ebbbaef71b..c08edc3a73aa50c9df61e03afb9cbf62e65901fd 100644 (file)
@@ -32,6 +32,8 @@ import {
   isMod,
   setupTippy,
   colorList,
+  wsClient,
+  authField,
 } from '../utils';
 import moment from 'moment';
 import { MomentTime } from './moment-time';
@@ -849,9 +851,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let deleteForm: DeleteComment = {
       edit_id: comment.id,
       deleted: !comment.deleted,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.deleteComment(deleteForm);
+    WebSocketService.Instance.send(wsClient.deleteComment(deleteForm));
   }
 
   handleSaveCommentClick(i: CommentNode) {
@@ -860,10 +862,10 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let form: SaveComment = {
       comment_id: cv.comment.id,
       save,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.saveComment(form);
+    WebSocketService.Instance.send(wsClient.saveComment(form));
 
     i.state.saveLoading = true;
     i.setState(this.state);
@@ -895,10 +897,10 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let form: CreateCommentLike = {
       comment_id: i.comment_view.comment.id,
       score: this.state.my_vote,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.likeComment(form);
+    WebSocketService.Instance.send(wsClient.likeComment(form));
     this.setState(this.state);
     setupTippy();
   }
@@ -923,10 +925,10 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let form: CreateCommentLike = {
       comment_id: i.comment_view.comment.id,
       score: this.state.my_vote,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.likeComment(form);
+    WebSocketService.Instance.send(wsClient.likeComment(form));
     this.setState(this.state);
     setupTippy();
   }
@@ -952,9 +954,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
       edit_id: comment.id,
       removed: !comment.removed,
       reason: i.state.removeReason,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.removeComment(form);
+    WebSocketService.Instance.send(wsClient.removeComment(form));
 
     i.state.showRemoveDialog = false;
     i.setState(i.state);
@@ -971,16 +973,16 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
       let form: MarkUserMentionAsRead = {
         user_mention_id: i.props.node.comment_view.user_mention.id,
         read: !i.props.node.comment_view.user_mention.read,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.markUserMentionAsRead(form);
+      WebSocketService.Instance.send(wsClient.markUserMentionAsRead(form));
     } else {
       let form: MarkCommentAsRead = {
         comment_id: i.props.node.comment_view.comment.id,
         read: !i.props.node.comment_view.comment.read,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.markCommentAsRead(form);
+      WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
     }
 
     i.state.readLoading = true;
@@ -1037,9 +1039,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
         remove_data: i.state.removeData,
         reason: i.state.banReason,
         expires: getUnixTime(i.state.banExpires),
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.banFromCommunity(form);
+      WebSocketService.Instance.send(wsClient.banFromCommunity(form));
     } else {
       // If its an unban, restore all their data
       let ban = !cv.creator.banned;
@@ -1052,9 +1054,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
         remove_data: i.state.removeData,
         reason: i.state.banReason,
         expires: getUnixTime(i.state.banExpires),
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.banUser(form);
+      WebSocketService.Instance.send(wsClient.banUser(form));
     }
 
     i.state.showBanDialog = false;
@@ -1077,9 +1079,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
       user_id: cv.creator.id,
       community_id: cv.community.id,
       added: !i.isMod,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.addModToCommunity(form);
+    WebSocketService.Instance.send(wsClient.addModToCommunity(form));
     i.state.showConfirmAppointAsMod = false;
     i.setState(i.state);
   }
@@ -1098,9 +1100,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let form: AddAdmin = {
       user_id: i.props.node.comment_view.creator.id,
       added: !i.isAdmin,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.addAdmin(form);
+    WebSocketService.Instance.send(wsClient.addAdmin(form));
     i.state.showConfirmAppointAsAdmin = false;
     i.setState(i.state);
   }
@@ -1120,9 +1122,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
     let form: TransferCommunity = {
       community_id: cv.community.id,
       user_id: cv.creator.id,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.transferCommunity(form);
+    WebSocketService.Instance.send(wsClient.transferCommunity(form));
     i.state.showConfirmTransferCommunity = false;
     i.setState(i.state);
   }
@@ -1140,9 +1142,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
   handleTransferSite(i: CommentNode) {
     let form: TransferSite = {
       user_id: i.props.node.comment_view.creator.id,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.transferSite(form);
+    WebSocketService.Instance.send(wsClient.transferSite(form));
     i.state.showConfirmTransferSite = false;
     i.setState(i.state);
   }
index 148a41361c0c4c2bf4459b6bdbdfdb65fc901e8b..236f777d67ea98dc90e6cfb501722b6454a8f20f 100644 (file)
@@ -11,7 +11,7 @@ import {
   SortType,
   SiteView,
 } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
+import { WebSocketService } from '../services';
 import {
   wsJsonToRes,
   toast,
@@ -20,6 +20,9 @@ import {
   setIsoData,
   wsSubscribe,
   wsUserOp,
+  wsClient,
+  authField,
+  setOptionalAuth,
 } from '../utils';
 import { CommunityLink } from './community-link';
 import { i18n } from '../i18next';
@@ -212,18 +215,18 @@ export class Communities extends Component<any, CommunitiesState> {
     let form: FollowCommunity = {
       community_id: communityId,
       follow: false,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.followCommunity(form);
+    WebSocketService.Instance.send(wsClient.followCommunity(form));
   }
 
   handleSubscribe(communityId: number) {
     let form: FollowCommunity = {
       community_id: communityId,
       follow: true,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.followCommunity(form);
+    WebSocketService.Instance.send(wsClient.followCommunity(form));
   }
 
   refetch() {
@@ -231,10 +234,12 @@ export class Communities extends Component<any, CommunitiesState> {
       sort: SortType.TopAll,
       limit: communityLimit,
       page: this.state.page,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
 
-    WebSocketService.Instance.client.listCommunities(listCommunitiesForm);
+    WebSocketService.Instance.send(
+      wsClient.listCommunities(listCommunitiesForm)
+    );
   }
 
   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
@@ -244,8 +249,8 @@ export class Communities extends Component<any, CommunitiesState> {
       sort: SortType.TopAll,
       limit: communityLimit,
       page,
-      auth: req.auth,
     };
+    setOptionalAuth(listCommunitiesForm, req.auth);
 
     return [req.client.listCommunities(listCommunitiesForm)];
   }
index 4a3dad4b7bef18dbd7e876d84a80465081375378..b40899af45fe74fe6747ecfa1d4bff810c902335 100644 (file)
@@ -9,7 +9,7 @@ import {
   CommunityResponse,
   CommunityView,
 } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
+import { WebSocketService } from '../services';
 import {
   wsJsonToRes,
   capitalizeFirstLetter,
@@ -17,6 +17,8 @@ import {
   randomStr,
   wsSubscribe,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import { i18n } from '../i18next';
 
@@ -52,7 +54,7 @@ export class CommunityForm extends Component<
       nsfw: false,
       icon: null,
       banner: null,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     loading: false,
   };
@@ -82,7 +84,7 @@ export class CommunityForm extends Component<
         nsfw: cv.community.nsfw,
         icon: cv.community.icon,
         banner: cv.community.banner,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
     }
 
@@ -283,9 +285,11 @@ export class CommunityForm extends Component<
         ...i.state.communityForm,
         edit_id: i.props.community_view.community.id,
       };
-      WebSocketService.Instance.client.editCommunity(form);
+      WebSocketService.Instance.send(wsClient.editCommunity(form));
     } else {
-      WebSocketService.Instance.client.createCommunity(i.state.communityForm);
+      WebSocketService.Instance.send(
+        wsClient.createCommunity(i.state.communityForm)
+      );
     }
     i.setState(i.state);
   }
index aa0cbf027c1999f1a52558c643bb3bc2fa5f4d6d..6025e911e88339388aff151ba04ea1a8e2bcb952 100644 (file)
@@ -51,6 +51,9 @@ import {
   isBrowser,
   communityRSSUrl,
   wsUserOp,
+  wsClient,
+  authField,
+  setOptionalAuth,
 } from '../utils';
 import { i18n } from '../i18next';
 
@@ -126,7 +129,7 @@ export class Community extends Component<any, State> {
     } else {
       this.fetchCommunity();
       this.fetchData();
-      WebSocketService.Instance.client.listCategories();
+      WebSocketService.Instance.send(wsClient.listCategories());
     }
     setupTippy();
   }
@@ -135,9 +138,9 @@ export class Community extends Component<any, State> {
     let form: GetCommunity = {
       id: this.state.communityId ? this.state.communityId : null,
       name: this.state.communityName ? this.state.communityName : null,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.getCommunity(form);
+    WebSocketService.Instance.send(wsClient.getCommunity(form));
   }
 
   componentWillUnmount() {
@@ -170,7 +173,7 @@ export class Community extends Component<any, State> {
     }
 
     let communityForm: GetCommunity = id ? { id } : { name: name_ };
-    communityForm.auth = req.auth;
+    setOptionalAuth(communityForm, req.auth);
     promises.push(req.client.getCommunity(communityForm));
 
     let dataType: DataType = pathSplit[4]
@@ -191,8 +194,8 @@ export class Community extends Component<any, State> {
         limit: fetchLimit,
         sort,
         type_: ListingType.Community,
-        auth: req.auth,
       };
+      setOptionalAuth(getPostsForm, req.auth);
       this.setIdOrName(getPostsForm, id, name_);
       promises.push(req.client.getPosts(getPostsForm));
     } else {
@@ -201,8 +204,8 @@ export class Community extends Component<any, State> {
         limit: fetchLimit,
         sort,
         type_: ListingType.Community,
-        auth: req.auth,
       };
+      setOptionalAuth(getCommentsForm, req.auth);
       this.setIdOrName(getCommentsForm, id, name_);
       promises.push(req.client.getComments(getCommentsForm));
     }
@@ -236,7 +239,7 @@ export class Community extends Component<any, State> {
   }
 
   render() {
-    let cv = this.state.communityRes.community_view;
+    let cv = this.state.communityRes?.community_view;
     return (
       <div class="container">
         {this.state.communityLoading ? (
@@ -418,9 +421,9 @@ export class Community extends Component<any, State> {
         type_: ListingType.Community,
         community_id: this.state.communityId,
         community_name: this.state.communityName,
-        auth: UserService.Instance.authField(false),
+        auth: authField(false),
       };
-      WebSocketService.Instance.client.getPosts(form);
+      WebSocketService.Instance.send(wsClient.getPosts(form));
     } else {
       let form: GetComments = {
         page: this.state.page,
@@ -429,9 +432,9 @@ export class Community extends Component<any, State> {
         type_: ListingType.Community,
         community_id: this.state.communityId,
         community_name: this.state.communityName,
-        auth: UserService.Instance.authField(false),
+        auth: authField(false),
       };
-      WebSocketService.Instance.client.getComments(form);
+      WebSocketService.Instance.send(wsClient.getComments(form));
     }
   }
 
@@ -442,9 +445,11 @@ export class Community extends Component<any, State> {
       this.context.router.history.push('/');
       return;
     } else if (msg.reconnect) {
-      WebSocketService.Instance.client.communityJoin({
-        community_id: this.state.communityRes.community_view.community.id,
-      });
+      WebSocketService.Instance.send(
+        wsClient.communityJoin({
+          community_id: this.state.communityRes.community_view.community.id,
+        })
+      );
       this.fetchData();
     } else if (op == UserOperation.GetCommunity) {
       let data = wsJsonToRes<GetCommunityResponse>(msg).data;
@@ -452,9 +457,11 @@ export class Community extends Component<any, State> {
       this.state.communityLoading = false;
       this.setState(this.state);
       // TODO why is there no auth in this form?
-      WebSocketService.Instance.client.communityJoin({
-        community_id: data.community_view.community.id,
-      });
+      WebSocketService.Instance.send(
+        wsClient.communityJoin({
+          community_id: data.community_view.community.id,
+        })
+      );
     } else if (
       op == UserOperation.EditCommunity ||
       op == UserOperation.DeleteCommunity ||
index b9891838e2dc2eb6aaf98dc487b19a216846da16..aab548078220a565285a29b48d5177e115328a74 100644 (file)
@@ -16,6 +16,7 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
 } from '../utils';
 import { WebSocketService, UserService } from '../services';
 import { i18n } from '../i18next';
@@ -53,7 +54,7 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
       this.state.categories = this.isoData.routeData[0].categories;
       this.state.loading = false;
     } else {
-      WebSocketService.Instance.client.listCategories();
+      WebSocketService.Instance.send(wsClient.listCategories());
     }
   }
 
index 35fd6a8e29752476db2d8f3a0000a8e452ef5d3a..edb2f059c634146a444b005689144ff75f2cd6ad 100644 (file)
@@ -3,9 +3,12 @@ import { Subscription } from 'rxjs';
 import { PostForm } from './post-form';
 import { HtmlTags } from './html-tags';
 import {
+  authField,
   isBrowser,
   setIsoData,
+  setOptionalAuth,
   toast,
+  wsClient,
   wsJsonToRes,
   wsSubscribe,
   wsUserOp,
@@ -64,9 +67,11 @@ export class CreatePost extends Component<any, CreatePostState> {
     let listCommunitiesForm: ListCommunities = {
       sort: SortType.TopAll,
       limit: 9999,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.listCommunities(listCommunitiesForm);
+    WebSocketService.Instance.send(
+      wsClient.listCommunities(listCommunitiesForm)
+    );
   }
 
   componentWillUnmount() {
@@ -157,8 +162,8 @@ export class CreatePost extends Component<any, CreatePostState> {
     let listCommunitiesForm: ListCommunities = {
       sort: SortType.TopAll,
       limit: 9999,
-      auth: req.auth,
     };
+    setOptionalAuth(listCommunitiesForm, req.auth);
     return [req.client.listCommunities(listCommunitiesForm)];
   }
 
index 90e17c9048695c8eecb56185c668e231ca55167c..506e3eef26d93240969147a66bc5523d79108613 100644 (file)
@@ -12,10 +12,12 @@ import {
   GetUserDetails,
 } from 'lemmy-js-client';
 import {
+  authField,
   getRecipientIdFromProps,
   isBrowser,
   setIsoData,
   toast,
+  wsClient,
   wsJsonToRes,
   wsSubscribe,
   wsUserOp,
@@ -73,9 +75,9 @@ export class CreatePrivateMessage extends Component<
       user_id: this.state.recipient_id,
       sort: SortType.New,
       saved_only: false,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.getUserDetails(form);
+    WebSocketService.Instance.send(wsClient.getUserDetails(form));
   }
 
   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
index 5d01feff8eed00ed317420b69cbe95197311e593..0a4c11ca6d41a54f450c1339db2e5580a4612e54 100644 (file)
@@ -31,6 +31,8 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import { CommentNodes } from './comment-nodes';
 import { PrivateMessage } from './private-message';
@@ -498,26 +500,28 @@ export class Inbox extends Component<any, InboxState> {
       unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
       page: this.state.page,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.getReplies(repliesForm);
+    WebSocketService.Instance.send(wsClient.getReplies(repliesForm));
 
     let userMentionsForm: GetUserMentions = {
       sort: this.state.sort,
       unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
       page: this.state.page,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.getUserMentions(userMentionsForm);
+    WebSocketService.Instance.send(wsClient.getUserMentions(userMentionsForm));
 
     let privateMessagesForm: GetPrivateMessages = {
       unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
       page: this.state.page,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.getPrivateMessages(privateMessagesForm);
+    WebSocketService.Instance.send(
+      wsClient.getPrivateMessages(privateMessagesForm)
+    );
   }
 
   handleSortChange(val: SortType) {
@@ -528,9 +532,11 @@ export class Inbox extends Component<any, InboxState> {
   }
 
   markAllAsRead(i: Inbox) {
-    WebSocketService.Instance.client.markAllAsRead({
-      auth: UserService.Instance.authField(),
-    });
+    WebSocketService.Instance.send(
+      wsClient.markAllAsRead({
+        auth: authField(),
+      })
+    );
     i.state.replies = [];
     i.state.mentions = [];
     i.state.messages = [];
index 8ca93967ac7c26d81ce6e83a3630bf658e7a77c0..c650c38e15070b9b4be482a34e2b05bcaddb1c14 100644 (file)
@@ -19,6 +19,8 @@ import {
   isBrowser,
   setIsoData,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import { i18n } from '../i18next';
 import { HtmlTags } from './html-tags';
@@ -67,7 +69,7 @@ export class Login extends Component<any, State> {
     this.subscription = wsSubscribe(this.parseMessage);
 
     if (isBrowser()) {
-      WebSocketService.Instance.client.getCaptcha();
+      WebSocketService.Instance.send(wsClient.getCaptcha());
     }
   }
 
@@ -349,7 +351,7 @@ export class Login extends Component<any, State> {
     event.preventDefault();
     i.state.loginLoading = true;
     i.setState(i.state);
-    WebSocketService.Instance.client.login(i.state.loginForm);
+    WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
   }
 
   handleLoginUsernameChange(i: Login, event: any) {
@@ -366,7 +368,7 @@ export class Login extends Component<any, State> {
     event.preventDefault();
     i.state.registerLoading = true;
     i.setState(i.state);
-    WebSocketService.Instance.client.register(i.state.registerForm);
+    WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
   }
 
   handleRegisterUsernameChange(i: Login, event: any) {
@@ -404,7 +406,7 @@ export class Login extends Component<any, State> {
 
   handleRegenCaptcha(_i: Login, event: any) {
     event.preventDefault();
-    WebSocketService.Instance.client.getCaptcha();
+    WebSocketService.Instance.send(wsClient.getCaptcha());
   }
 
   handlePasswordReset(i: Login, event: any) {
@@ -412,7 +414,7 @@ export class Login extends Component<any, State> {
     let resetForm: PasswordReset = {
       email: i.state.loginForm.username_or_email,
     };
-    WebSocketService.Instance.client.passwordReset(resetForm);
+    WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
   }
 
   handleCaptchaPlay(i: Login, event: any) {
@@ -439,7 +441,7 @@ export class Login extends Component<any, State> {
       this.state = this.emptyState;
       this.state.registerForm.captcha_answer = undefined;
       // Refetch another captcha
-      WebSocketService.Instance.client.getCaptcha();
+      WebSocketService.Instance.send(wsClient.getCaptcha());
       this.setState(this.state);
       return;
     } else {
@@ -448,9 +450,11 @@ export class Login extends Component<any, State> {
         this.state = this.emptyState;
         this.setState(this.state);
         UserService.Instance.login(data);
-        WebSocketService.Instance.client.userJoin({
-          auth: UserService.Instance.authField(),
-        });
+        WebSocketService.Instance.send(
+          wsClient.userJoin({
+            auth: authField(),
+          })
+        );
         toast(i18n.t('logged_in'));
         this.props.history.push('/');
       } else if (op == UserOperation.Register) {
@@ -458,9 +462,11 @@ export class Login extends Component<any, State> {
         this.state = this.emptyState;
         this.setState(this.state);
         UserService.Instance.login(data);
-        WebSocketService.Instance.client.userJoin({
-          auth: UserService.Instance.authField(),
-        });
+        WebSocketService.Instance.send(
+          wsClient.userJoin({
+            auth: authField(),
+          })
+        );
         this.props.history.push('/communities');
       } else if (op == UserOperation.GetCaptcha) {
         let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
index 0bfe08dc43185737045ef002258837e581fbfed5..fbccf51a6922df2d4c154ae814b1ea8dfd7a6ac5 100644 (file)
@@ -55,6 +55,9 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  setOptionalAuth,
+  wsClient,
+  authField,
 } from '../utils';
 import { i18n } from '../i18next';
 import { T } from 'inferno-i18next';
@@ -133,9 +136,11 @@ export class Main extends Component<any, MainState> {
       this.fetchTrendingCommunities();
       this.fetchData();
       if (UserService.Instance.user) {
-        WebSocketService.Instance.client.getFollowedCommunities({
-          auth: UserService.Instance.authField(),
-        });
+        WebSocketService.Instance.send(
+          wsClient.getFollowedCommunities({
+            auth: authField(),
+          })
+        );
       }
     }
 
@@ -146,9 +151,11 @@ export class Main extends Component<any, MainState> {
     let listCommunitiesForm: ListCommunities = {
       sort: SortType.Hot,
       limit: 6,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.listCommunities(listCommunitiesForm);
+    WebSocketService.Instance.send(
+      wsClient.listCommunities(listCommunitiesForm)
+    );
   }
 
   componentDidMount() {
@@ -157,7 +164,7 @@ export class Main extends Component<any, MainState> {
       this.context.router.history.push('/setup');
     }
 
-    WebSocketService.Instance.client.communityJoin({ community_id: 0 });
+    WebSocketService.Instance.send(wsClient.communityJoin({ community_id: 0 }));
   }
 
   componentWillUnmount() {
@@ -206,8 +213,8 @@ export class Main extends Component<any, MainState> {
         limit: fetchLimit,
         sort,
         type_,
-        auth: req.auth,
       };
+      setOptionalAuth(getPostsForm, req.auth);
       promises.push(req.client.getPosts(getPostsForm));
     } else {
       let getCommentsForm: GetComments = {
@@ -215,8 +222,8 @@ export class Main extends Component<any, MainState> {
         limit: fetchLimit,
         sort,
         type_,
-        auth: req.auth,
       };
+      setOptionalAuth(getCommentsForm, req.auth);
       promises.push(req.client.getComments(getCommentsForm));
     }
 
@@ -661,18 +668,18 @@ export class Main extends Component<any, MainState> {
         limit: fetchLimit,
         sort: this.state.sort,
         type_: this.state.listingType,
-        auth: UserService.Instance.authField(false),
+        auth: authField(false),
       };
-      WebSocketService.Instance.client.getPosts(getPostsForm);
+      WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
     } else {
       let getCommentsForm: GetComments = {
         page: this.state.page,
         limit: fetchLimit,
         sort: this.state.sort,
         type_: this.state.listingType,
-        auth: UserService.Instance.authField(false),
+        auth: authField(false),
       };
-      WebSocketService.Instance.client.getComments(getCommentsForm);
+      WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
     }
   }
 
@@ -682,7 +689,9 @@ export class Main extends Component<any, MainState> {
       toast(i18n.t(msg.error), 'danger');
       return;
     } else if (msg.reconnect) {
-      WebSocketService.Instance.client.communityJoin({ community_id: 0 });
+      WebSocketService.Instance.send(
+        wsClient.communityJoin({ community_id: 0 })
+      );
       this.fetchData();
     } else if (op == UserOperation.GetFollowedCommunities) {
       let data = wsJsonToRes<GetFollowedCommunitiesResponse>(msg).data;
index fcd18e59f6d28f1b896ce166c7017b732e8d80ce..cfdaef74d7dd61660b22eaec885a0d15ec2b30cd 100644 (file)
@@ -25,6 +25,7 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
 } from '../utils';
 import { MomentTime } from './moment-time';
 import { HtmlTags } from './html-tags';
@@ -439,7 +440,7 @@ export class Modlog extends Component<any, ModlogState> {
       page: this.state.page,
       limit: fetchLimit,
     };
-    WebSocketService.Instance.client.getModlog(modlogForm);
+    WebSocketService.Instance.send(wsClient.getModlog(modlogForm));
   }
 
   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
index e31d0d51e4c2e4bb6e5321861fa84898271d01c7..ea191b8f422693a46b60ce21c7b9eb6d62050b14 100644 (file)
@@ -30,6 +30,8 @@ import {
   wsSubscribe,
   supportLemmyUrl,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import { i18n } from '../i18next';
 import { PictrsImage } from './pictrs-image';
@@ -88,9 +90,11 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
         // i18n.changeLanguage('de');
       } else {
         this.requestNotificationPermission();
-        WebSocketService.Instance.client.userJoin({
-          auth: UserService.Instance.authField(),
-        });
+        WebSocketService.Instance.send(
+          wsClient.userJoin({
+            auth: authField(),
+          })
+        );
         this.fetchUnreads();
       }
 
@@ -98,7 +102,9 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
         // A login
         if (res !== undefined) {
           this.requestNotificationPermission();
-          WebSocketService.Instance.client.getSite();
+          WebSocketService.Instance.send(
+            wsClient.getSite({ auth: authField() })
+          );
         } else {
           this.setState({ isLoggedIn: false });
         }
@@ -167,7 +173,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
 
   // TODO class active corresponding to current page
   navbar() {
-    let user = this.props.site_res.my_user;
+    let user = this.props.site_res.my_user || UserService.Instance.user;
     return (
       <nav class="navbar navbar-expand-lg navbar-light shadow-sm p-0 px-3">
         <div class="container">
@@ -376,9 +382,11 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       }
       return;
     } else if (msg.reconnect) {
-      WebSocketService.Instance.client.userJoin({
-        auth: UserService.Instance.authField(),
-      });
+      WebSocketService.Instance.send(
+        wsClient.userJoin({
+          auth: authField(),
+        })
+      );
       this.fetchUnreads();
     } else if (op == UserOperation.GetReplies) {
       let data = wsJsonToRes<GetRepliesResponse>(msg).data;
@@ -409,6 +417,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
     } else if (op == UserOperation.GetSite) {
       // This is only called on a successful login
       let data = wsJsonToRes<GetSiteResponse>(msg).data;
+      console.log(data.my_user);
       UserService.Instance.user = data.my_user;
       setTheme(UserService.Instance.user.theme);
       i18n.changeLanguage(getLanguage());
@@ -450,7 +459,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       unread_only: true,
       page: 1,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
     let userMentionsForm: GetUserMentions = {
@@ -458,20 +467,24 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       unread_only: true,
       page: 1,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
     let privateMessagesForm: GetPrivateMessages = {
       unread_only: true,
       page: 1,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
     if (this.currentLocation !== '/inbox') {
-      WebSocketService.Instance.client.getReplies(repliesForm);
-      WebSocketService.Instance.client.getUserMentions(userMentionsForm);
-      WebSocketService.Instance.client.getPrivateMessages(privateMessagesForm);
+      WebSocketService.Instance.send(wsClient.getReplies(repliesForm));
+      WebSocketService.Instance.send(
+        wsClient.getUserMentions(userMentionsForm)
+      );
+      WebSocketService.Instance.send(
+        wsClient.getPrivateMessages(privateMessagesForm)
+      );
     }
   }
 
index 4dcd6c65bd3404250a235d7ecf69333e9aced679..6ffa3e0775f50beac3614653a620c6e798b745e3 100644 (file)
@@ -15,6 +15,7 @@ import {
   isBrowser,
   wsSubscribe,
   wsUserOp,
+  wsClient,
 } from '../utils';
 import { i18n } from '../i18next';
 import { HtmlTags } from './html-tags';
@@ -138,7 +139,9 @@ export class PasswordChange extends Component<any, State> {
     i.state.loading = true;
     i.setState(i.state);
 
-    WebSocketService.Instance.client.passwordChange(i.state.passwordChangeForm);
+    WebSocketService.Instance.send(
+      wsClient.passwordChange(i.state.passwordChangeForm)
+    );
   }
 
   parseMessage(msg: any) {
index c64104b14709484b6ab6920de6743530804f1aa9..f4ff5ea66ed8a5e6d0c9d50a447d2c6bb79b6ed0 100644 (file)
@@ -34,6 +34,8 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 
 var Choices;
@@ -76,7 +78,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
       community_id: null,
       name: null,
       nsfw: false,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     loading: false,
     imageLoading: false,
@@ -102,7 +104,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
         community_id: this.props.post_view.community.id,
         url: this.props.post_view.post.url,
         nsfw: this.props.post_view.post.nsfw,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
     }
 
@@ -377,9 +379,9 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
         ...i.state.postForm,
         edit_id: i.props.post_view.post.id,
       };
-      WebSocketService.Instance.client.editPost(form);
+      WebSocketService.Instance.send(wsClient.editPost(form));
     } else {
-      WebSocketService.Instance.client.createPost(i.state.postForm);
+      WebSocketService.Instance.send(wsClient.createPost(i.state.postForm));
     }
     i.state.loading = true;
     i.setState(i.state);
@@ -408,10 +410,10 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
         sort: SortType.TopAll,
         page: 1,
         limit: 6,
-        auth: UserService.Instance.authField(false),
+        auth: authField(false),
       };
 
-      WebSocketService.Instance.client.search(form);
+      WebSocketService.Instance.send(wsClient.search(form));
 
       // Fetch the page title
       getPageTitle(this.state.postForm.url).then(d => {
@@ -438,11 +440,11 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
       community_id: this.state.postForm.community_id,
       page: 1,
       limit: 6,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
 
     if (this.state.postForm.name !== '') {
-      WebSocketService.Instance.client.search(form);
+      WebSocketService.Instance.send(wsClient.search(form));
     } else {
       this.state.suggestedPosts = [];
     }
index 881b9b08597eb61984a39aefc661a9f93601cbd5..7bf2174f3f044923f78ac6fb25bd6b9dda9a507f 100644 (file)
@@ -36,6 +36,8 @@ import {
   setupTippy,
   hostname,
   previewLines,
+  wsClient,
+  authField,
 } from '../utils';
 import { i18n } from '../i18next';
 import { externalHost } from '../env';
@@ -1175,10 +1177,10 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: CreatePostLike = {
       post_id: i.props.post_view.post.id,
       score: i.state.my_vote,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.likePost(form);
+    WebSocketService.Instance.send(wsClient.likePost(form));
     i.setState(i.state);
     setupTippy();
   }
@@ -1207,10 +1209,10 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: CreatePostLike = {
       post_id: i.props.post_view.post.id,
       score: i.state.my_vote,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.likePost(form);
+    WebSocketService.Instance.send(wsClient.likePost(form));
     i.setState(i.state);
     setupTippy();
   }
@@ -1235,9 +1237,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let deleteForm: DeletePost = {
       edit_id: i.props.post_view.post.id,
       deleted: !i.props.post_view.post.deleted,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.deletePost(deleteForm);
+    WebSocketService.Instance.send(wsClient.deletePost(deleteForm));
   }
 
   handleSavePostClick(i: PostListing) {
@@ -1246,10 +1248,10 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: SavePost = {
       post_id: i.props.post_view.post.id,
       save: saved,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
 
-    WebSocketService.Instance.client.savePost(form);
+    WebSocketService.Instance.send(wsClient.savePost(form));
   }
 
   get crossPostParams(): string {
@@ -1286,9 +1288,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
       edit_id: i.props.post_view.post.id,
       removed: !i.props.post_view.post.removed,
       reason: i.state.removeReason,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.removePost(form);
+    WebSocketService.Instance.send(wsClient.removePost(form));
 
     i.state.showRemoveDialog = false;
     i.setState(i.state);
@@ -1298,18 +1300,18 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: LockPost = {
       edit_id: i.props.post_view.post.id,
       locked: !i.props.post_view.post.locked,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.lockPost(form);
+    WebSocketService.Instance.send(wsClient.lockPost(form));
   }
 
   handleModSticky(i: PostListing) {
     let form: StickyPost = {
       edit_id: i.props.post_view.post.id,
       stickied: !i.props.post_view.post.stickied,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.stickyPost(form);
+    WebSocketService.Instance.send(wsClient.stickyPost(form));
   }
 
   handleModBanFromCommunityShow(i: PostListing) {
@@ -1362,9 +1364,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
         remove_data: i.state.removeData,
         reason: i.state.banReason,
         expires: getUnixTime(i.state.banExpires),
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.banFromCommunity(form);
+      WebSocketService.Instance.send(wsClient.banFromCommunity(form));
     } else {
       // If its an unban, restore all their data
       let ban = !i.props.post_view.creator.banned;
@@ -1377,9 +1379,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
         remove_data: i.state.removeData,
         reason: i.state.banReason,
         expires: getUnixTime(i.state.banExpires),
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.banUser(form);
+      WebSocketService.Instance.send(wsClient.banUser(form));
     }
 
     i.state.showBanDialog = false;
@@ -1391,9 +1393,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
       user_id: i.props.post_view.creator.id,
       community_id: i.props.post_view.community.id,
       added: !i.isMod,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.addModToCommunity(form);
+    WebSocketService.Instance.send(wsClient.addModToCommunity(form));
     i.setState(i.state);
   }
 
@@ -1401,9 +1403,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: AddAdmin = {
       user_id: i.props.post_view.creator.id,
       added: !i.isAdmin,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.addAdmin(form);
+    WebSocketService.Instance.send(wsClient.addAdmin(form));
     i.setState(i.state);
   }
 
@@ -1421,9 +1423,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
     let form: TransferCommunity = {
       community_id: i.props.post_view.community.id,
       user_id: i.props.post_view.creator.id,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.transferCommunity(form);
+    WebSocketService.Instance.send(wsClient.transferCommunity(form));
     i.state.showConfirmTransferCommunity = false;
     i.setState(i.state);
   }
@@ -1441,9 +1443,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
   handleTransferSite(i: PostListing) {
     let form: TransferSite = {
       user_id: i.props.post_view.creator.id,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.transferSite(form);
+    WebSocketService.Instance.send(wsClient.transferSite(form));
     i.state.showConfirmTransferSite = false;
     i.setState(i.state);
   }
index 1d800fc74f15e8bb7948c8c7dc07a35b6ec9b5ce..160dfccd74ba81efb18d0fc13c738f2b3f992e55 100644 (file)
@@ -47,6 +47,9 @@ import {
   previewLines,
   isImage,
   wsUserOp,
+  wsClient,
+  authField,
+  setOptionalAuth,
 } from '../utils';
 import { PostListing } from './post-listing';
 import { Sidebar } from './sidebar';
@@ -103,16 +106,16 @@ export class Post extends Component<any, PostState> {
       }
     } else {
       this.fetchPost();
-      WebSocketService.Instance.client.listCategories();
+      WebSocketService.Instance.send(wsClient.listCategories());
     }
   }
 
   fetchPost() {
     let form: GetPost = {
       id: this.state.postId,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.getPost(form);
+    WebSocketService.Instance.send(wsClient.getPost(form));
   }
 
   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
@@ -123,8 +126,8 @@ export class Post extends Component<any, PostState> {
 
     let postForm: GetPost = {
       id,
-      auth: req.auth,
     };
+    setOptionalAuth(postForm, req.auth);
 
     promises.push(req.client.getPost(postForm));
     promises.push(req.client.listCategories());
@@ -138,7 +141,9 @@ export class Post extends Component<any, PostState> {
   }
 
   componentDidMount() {
-    WebSocketService.Instance.client.postJoin({ post_id: this.state.postId });
+    WebSocketService.Instance.send(
+      wsClient.postJoin({ post_id: this.state.postId })
+    );
     autosize(document.querySelectorAll('textarea'));
   }
 
@@ -191,9 +196,9 @@ export class Post extends Component<any, PostState> {
       let form: MarkCommentAsRead = {
         comment_id: found.creator.id,
         read: true,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.markCommentAsRead(form);
+      WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
       UserService.Instance.unreadCountSub.next(
         UserService.Instance.unreadCountSub.value - 1
       );
@@ -218,7 +223,7 @@ export class Post extends Component<any, PostState> {
   }
 
   render() {
-    let pv = this.state.postRes.post_view;
+    let pv = this.state.postRes?.post_view;
     return (
       <div class="container">
         {this.state.loading ? (
@@ -444,11 +449,13 @@ export class Post extends Component<any, PostState> {
       return;
     } else if (msg.reconnect) {
       let postId = Number(this.props.match.params.id);
-      WebSocketService.Instance.client.postJoin({ post_id: postId });
-      WebSocketService.Instance.client.getPost({
-        id: postId,
-        auth: UserService.Instance.authField(false),
-      });
+      WebSocketService.Instance.send(wsClient.postJoin({ post_id: postId }));
+      WebSocketService.Instance.send(
+        wsClient.getPost({
+          id: postId,
+          auth: authField(false),
+        })
+      );
     } else if (op == UserOperation.GetPost) {
       let data = wsJsonToRes<GetPostResponse>(msg).data;
       this.state.postRes = data;
@@ -462,9 +469,9 @@ export class Post extends Component<any, PostState> {
           sort: SortType.TopAll,
           page: 1,
           limit: 6,
-          auth: UserService.Instance.authField(false),
+          auth: authField(false),
         };
-        WebSocketService.Instance.client.search(form);
+        WebSocketService.Instance.send(wsClient.search(form));
       }
 
       this.setState(this.state);
index a49f6ed9e4849472c76ef4e1c07c59c3cec9d53e..9c518aa07d588096d99325ca584dc7b416589e34 100644 (file)
@@ -9,7 +9,7 @@ import {
   UserSafe,
   UserOperation,
 } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
+import { WebSocketService } from '../services';
 import {
   capitalizeFirstLetter,
   wsJsonToRes,
@@ -18,6 +18,8 @@ import {
   wsSubscribe,
   isBrowser,
   wsUserOp,
+  wsClient,
+  authField,
 } from '../utils';
 import { UserListing } from './user-listing';
 import { MarkdownTextArea } from './markdown-textarea';
@@ -48,7 +50,7 @@ export class PrivateMessageForm extends Component<
     privateMessageForm: {
       content: null,
       recipient_id: this.props.recipient.id,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     loading: false,
     previewMode: false,
@@ -191,12 +193,12 @@ export class PrivateMessageForm extends Component<
       let form: EditPrivateMessage = {
         edit_id: i.props.privateMessage.private_message.id,
         content: i.state.privateMessageForm.content,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
-      WebSocketService.Instance.client.editPrivateMessage(form);
+      WebSocketService.Instance.send(wsClient.editPrivateMessage(form));
     } else {
-      WebSocketService.Instance.client.createPrivateMessage(
-        i.state.privateMessageForm
+      WebSocketService.Instance.send(
+        wsClient.createPrivateMessage(i.state.privateMessageForm)
       );
     }
     i.state.loading = true;
index ffad47f0333a2f73489e53d4d184d793a07f5f13..9e2fd76ccef384146b4d4b6a5f530afbb73c60f4 100644 (file)
@@ -6,7 +6,7 @@ import {
   UserSafe,
 } from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
-import { mdToHtml, toast } from '../utils';
+import { authField, mdToHtml, toast, wsClient } from '../utils';
 import { MomentTime } from './moment-time';
 import { PrivateMessageForm } from './private-message-form';
 import { UserListing } from './user-listing';
@@ -231,9 +231,9 @@ export class PrivateMessage extends Component<
     let form: DeletePrivateMessage = {
       edit_id: i.props.private_message_view.private_message.id,
       deleted: !i.props.private_message_view.private_message.deleted,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.deletePrivateMessage(form);
+    WebSocketService.Instance.send(wsClient.deletePrivateMessage(form));
   }
 
   handleReplyCancel() {
@@ -246,9 +246,9 @@ export class PrivateMessage extends Component<
     let form: MarkPrivateMessageAsRead = {
       edit_id: i.props.private_message_view.private_message.id,
       read: !i.props.private_message_view.private_message.read,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.markPrivateMessageAsRead(form);
+    WebSocketService.Instance.send(wsClient.markPrivateMessageAsRead(form));
   }
 
   handleMessageCollapse(i: PrivateMessage) {
index b4196f78f39dce5f17ee1cef1c5b2dc20349c0cd..9af800e26bc30edeeffe646c209e96415236c303 100644 (file)
@@ -14,7 +14,7 @@ import {
   CommentResponse,
   Site,
 } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
+import { WebSocketService } from '../services';
 import {
   wsJsonToRes,
   fetchLimit,
@@ -27,6 +27,9 @@ import {
   setIsoData,
   wsSubscribe,
   wsUserOp,
+  wsClient,
+  authField,
+  setOptionalAuth,
 } from '../utils';
 import { PostListing } from './post-listing';
 import { HtmlTags } from './html-tags';
@@ -141,8 +144,8 @@ export class Search extends Component<any, SearchState> {
       sort: this.getSortTypeFromProps(pathSplit[7]),
       page: this.getPageFromProps(pathSplit[9]),
       limit: fetchLimit,
-      auth: req.auth,
     };
+    setOptionalAuth(form, req.auth);
 
     if (form.q != '') {
       promises.push(req.client.search(form));
@@ -456,11 +459,11 @@ export class Search extends Component<any, SearchState> {
       sort: this.state.sort,
       page: this.state.page,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
 
     if (this.state.q != '') {
-      WebSocketService.Instance.client.search(form);
+      WebSocketService.Instance.send(wsClient.search(form));
     }
   }
 
index c139988149d6291c62871b523c289d83e472ce4b..bb30bc2c32ac5511eaffd2cda0915697096ee5ac 100644 (file)
@@ -4,7 +4,7 @@ import { Subscription } from 'rxjs';
 import { retryWhen, delay, take } from 'rxjs/operators';
 import { Register, LoginResponse, UserOperation } from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
-import { wsUserOp, wsJsonToRes, toast } from '../utils';
+import { wsUserOp, wsJsonToRes, toast, wsClient } from '../utils';
 import { SiteForm } from './site-form';
 import { i18n } from '../i18next';
 
@@ -163,7 +163,7 @@ export class Setup extends Component<any, State> {
     i.state.userLoading = true;
     i.setState(i.state);
     event.preventDefault();
-    WebSocketService.Instance.client.register(i.state.userForm);
+    WebSocketService.Instance.send(wsClient.register(i.state.userForm));
   }
 
   handleRegisterUsernameChange(i: Setup, event: any) {
index 09345357210e775b5d158a37ed2a11e5da7022a6..d44eb14ab8b81a6524ec6db3dfec6242a8129454 100644 (file)
@@ -11,7 +11,7 @@ import {
   Category,
 } from 'lemmy-js-client';
 import { WebSocketService, UserService } from '../services';
-import { mdToHtml, getUnixTime } from '../utils';
+import { mdToHtml, getUnixTime, wsClient, authField } from '../utils';
 import { CommunityForm } from './community-form';
 import { UserListing } from './user-listing';
 import { CommunityLink } from './community-link';
@@ -394,9 +394,9 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     let deleteForm: DeleteCommunity = {
       edit_id: i.props.community_view.community.id,
       deleted: !i.props.community_view.community.deleted,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.deleteCommunity(deleteForm);
+    WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
   }
 
   handleShowConfirmLeaveModTeamClick(i: Sidebar) {
@@ -409,9 +409,9 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
       user_id: UserService.Instance.user.id,
       community_id: i.props.community_view.community.id,
       added: false,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.addModToCommunity(form);
+    WebSocketService.Instance.send(wsClient.addModToCommunity(form));
     i.state.showConfirmLeaveModTeam = false;
     i.setState(i.state);
   }
@@ -426,9 +426,9 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     let form: FollowCommunity = {
       community_id: communityId,
       follow: false,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.followCommunity(form);
+    WebSocketService.Instance.send(wsClient.followCommunity(form));
   }
 
   handleSubscribe(communityId: number, event: any) {
@@ -436,9 +436,9 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     let form: FollowCommunity = {
       community_id: communityId,
       follow: true,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.followCommunity(form);
+    WebSocketService.Instance.send(wsClient.followCommunity(form));
   }
 
   private get amCreator(): boolean {
@@ -486,9 +486,9 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
       removed: !i.props.community_view.community.removed,
       reason: i.state.removeReason,
       expires: getUnixTime(i.state.removeExpires),
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     };
-    WebSocketService.Instance.client.removeCommunity(removeForm);
+    WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
 
     i.state.showRemoveDialog = false;
     i.setState(i.state);
index 6547edd749cd489bfe1ff411472f802fe4a6d1a5..2446b055df9dc3b02b1672f61c9d8a4aa9dee7d7 100644 (file)
@@ -3,8 +3,13 @@ import { Prompt } from 'inferno-router';
 import { MarkdownTextArea } from './markdown-textarea';
 import { ImageUploadForm } from './image-upload-form';
 import { Site, EditSite } from 'lemmy-js-client';
-import { UserService, WebSocketService } from '../services';
-import { capitalizeFirstLetter, randomStr } from '../utils';
+import { WebSocketService } from '../services';
+import {
+  authField,
+  capitalizeFirstLetter,
+  randomStr,
+  wsClient,
+} from '../utils';
 import { i18n } from '../i18next';
 
 interface SiteFormProps {
@@ -27,7 +32,7 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
       name: null,
       icon: null,
       banner: null,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     loading: false,
   };
@@ -55,7 +60,7 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
         enable_nsfw: this.props.site.enable_nsfw,
         icon: this.props.site.icon,
         banner: this.props.site.banner,
-        auth: UserService.Instance.authField(),
+        auth: authField(),
       };
     }
   }
@@ -244,9 +249,9 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
     event.preventDefault();
     i.state.loading = true;
     if (i.props.site) {
-      WebSocketService.Instance.client.editSite(i.state.siteForm);
+      WebSocketService.Instance.send(wsClient.editSite(i.state.siteForm));
     } else {
-      WebSocketService.Instance.client.createSite(i.state.siteForm);
+      WebSocketService.Instance.send(wsClient.createSite(i.state.siteForm));
     }
     i.setState(i.state);
   }
index 814e78969620f3590c8fe4ec717d5dce7c9fcfaf..9cf7d666df42377ffc03b7b51a8be2d4cfe8fbc0 100644 (file)
@@ -42,6 +42,9 @@ import {
   previewLines,
   editPostFindRes,
   wsUserOp,
+  wsClient,
+  authField,
+  setOptionalAuth,
 } from '../utils';
 import { UserListing } from './user-listing';
 import { HtmlTags } from './html-tags';
@@ -107,14 +110,14 @@ export class User extends Component<any, UserState> {
       send_notifications_to_email: null,
       bio: null,
       preferred_username: null,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     userSettingsLoading: null,
     deleteAccountLoading: null,
     deleteAccountShowConfirm: false,
     deleteAccountForm: {
       password: null,
-      auth: UserService.Instance.authField(),
+      auth: authField(),
     },
     siteRes: this.isoData.site_res,
   };
@@ -164,9 +167,9 @@ export class User extends Component<any, UserState> {
       saved_only: this.state.view === UserDetailsView.Saved,
       page: this.state.page,
       limit: fetchLimit,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
-    WebSocketService.Instance.client.getUserDetails(form);
+    WebSocketService.Instance.send(wsClient.getUserDetails(form));
   }
 
   get isCurrentUser() {
@@ -211,8 +214,8 @@ export class User extends Component<any, UserState> {
       saved_only: view === UserDetailsView.Saved,
       page,
       limit: fetchLimit,
-      auth: req.auth,
     };
+    setOptionalAuth(form, req.auth);
     this.setIdOrName(form, user_id, username);
     promises.push(req.client.getUserDetails(form));
     return promises;
@@ -1018,7 +1021,9 @@ export class User extends Component<any, UserState> {
     i.state.userSettingsLoading = true;
     i.setState(i.state);
 
-    WebSocketService.Instance.client.saveUserSettings(i.state.userSettingsForm);
+    WebSocketService.Instance.send(
+      wsClient.saveUserSettings(i.state.userSettingsForm)
+    );
   }
 
   handleDeleteAccountShowConfirmToggle(i: User, event: any) {
@@ -1042,7 +1047,9 @@ export class User extends Component<any, UserState> {
     i.state.deleteAccountLoading = true;
     i.setState(i.state);
 
-    WebSocketService.Instance.client.deleteAccount(i.state.deleteAccountForm);
+    WebSocketService.Instance.send(
+      wsClient.deleteAccount(i.state.deleteAccountForm)
+    );
     i.handleLogoutClick(i);
   }
 
index 59566a1b3f1b13b3ed55f60469ac8da60093873e..02d66e88447a0ea2ccf63c3f7f80626137bffe68 100644 (file)
@@ -25,8 +25,8 @@ const secure = isBrowser()
 const host = isBrowser() ? externalHost : internalHost;
 
 const httpBase = `http://${host}`; // Don't use secure here
-export const wsUri = `ws${secure}://${host}/api/v1/ws`;
-export const httpUri = `${httpBase}/api/v1`;
+export const wsUri = `ws${secure}://${host}/api/v2/ws`;
+export const httpUri = `${httpBase}/api/v2`;
 export const pictrsUri = `http${secure}://${host}/pictrs/image`;
 
 console.log(`httpbase: ${httpBase}`);
index 391a5ef2a7a78f57a3ef4d100fe7f3379cddb37b..cdcd63f3b5890094628e0a6c9132404c873406a6 100644 (file)
@@ -3,8 +3,6 @@ import IsomorphicCookie from 'isomorphic-cookie';
 import { User_, LoginResponse } from 'lemmy-js-client';
 import jwt_decode from 'jwt-decode';
 import { Subject, BehaviorSubject } from 'rxjs';
-import { i18n } from '../i18next';
-import { toast } from '../utils';
 
 interface Claims {
   id: number;
@@ -50,15 +48,6 @@ export class UserService {
     return IsomorphicCookie.load('jwt');
   }
 
-  public authField(throwErr: boolean = true): string {
-    if (this.auth == null && throwErr) {
-      toast(i18n.t('not_logged_in'), 'danger');
-      throw 'Not logged in';
-    } else {
-      return this.auth;
-    }
-  }
-
   private setClaims(jwt: string) {
     this.claims = jwt_decode(jwt);
     this.jwtSub.next(jwt);
index 868bd40b0a85699fb0c5c439972edbdcb8cb9380..3aab0e29f16218dd35083107f8a55161d647f0cf 100644 (file)
@@ -1,9 +1,5 @@
 import { wsUri } from '../env';
-import {
-  LemmyWebsocket,
-  UserViewSafe,
-  WebSocketJsonResponse,
-} from 'lemmy-js-client';
+import { UserViewSafe, WebSocketJsonResponse } from 'lemmy-js-client';
 import { isBrowser } from '../utils';
 import { Observable } from 'rxjs';
 import { share } from 'rxjs/operators';
@@ -14,7 +10,7 @@ import {
 
 export class WebSocketService {
   private static _instance: WebSocketService;
-  public ws: ReconnectingWebSocket;
+  private ws: ReconnectingWebSocket;
   public wsOptions: WSOptions = {
     connectionTimeout: 5000,
     maxRetries: 10,
@@ -23,7 +19,6 @@ export class WebSocketService {
 
   public admins: UserViewSafe[];
   public banned: UserViewSafe[];
-  public client = new LemmyWebsocket();
 
   private constructor() {
     this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
@@ -46,15 +41,19 @@ export class WebSocketService {
         firstConnect = false;
       };
     }).pipe(share());
+
+    if (isBrowser()) {
+      window.onbeforeunload = () => {
+        this.ws.close();
+      };
+    }
+  }
+
+  public send(data: string) {
+    this.ws.send(data);
   }
 
   public static get Instance() {
     return this._instance || (this._instance = new this());
   }
 }
-
-if (isBrowser()) {
-  window.onbeforeunload = () => {
-    WebSocketService.Instance.ws.close();
-  };
-}
index 603512b144d609eb54839230cec92dd1d0587f33..55856815dea2d1f2f26ba5ad4a37fadb635d8468 100644 (file)
@@ -41,6 +41,7 @@ import {
   SearchResponse,
   PostView,
   PrivateMessageView,
+  LemmyWebsocket,
 } from 'lemmy-js-client';
 
 import {
@@ -66,6 +67,9 @@ import tippy from 'tippy.js';
 import moment from 'moment';
 import { Subscription } from 'rxjs';
 import { retryWhen, delay, take } from 'rxjs/operators';
+import { i18n } from './i18next';
+
+export const wsClient = new LemmyWebsocket();
 
 export const favIconUrl = '/static/assets/favicon.svg';
 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
@@ -719,10 +723,10 @@ function userSearch(text: string, cb: any) {
       sort: SortType.TopAll,
       page: 1,
       limit: mentionDropdownFetchLimit,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
 
-    WebSocketService.Instance.client.search(form);
+    WebSocketService.Instance.send(wsClient.search(form));
 
     let userSub = WebSocketService.Instance.subject.subscribe(
       msg => {
@@ -757,10 +761,10 @@ function communitySearch(text: string, cb: any) {
       sort: SortType.TopAll,
       page: 1,
       limit: mentionDropdownFetchLimit,
-      auth: UserService.Instance.authField(false),
+      auth: authField(false),
     };
 
-    WebSocketService.Instance.client.search(form);
+    WebSocketService.Instance.send(wsClient.search(form));
 
     let communitySub = WebSocketService.Instance.subject.subscribe(
       msg => {
@@ -1111,6 +1115,24 @@ export function wsSubscribe(parseMessage: any): Subscription {
   }
 }
 
+export function setOptionalAuth(obj: any, auth = UserService.Instance.auth) {
+  if (auth) {
+    obj.auth = auth;
+  }
+}
+
+export function authField(
+  throwErr: boolean = true,
+  auth = UserService.Instance.auth
+): string {
+  if (auth == null && throwErr) {
+    toast(i18n.t('not_logged_in'), 'danger');
+    throw 'Not logged in';
+  } else {
+    return auth;
+  }
+}
+
 moment.updateLocale('en', {
   relativeTime: {
     future: 'in %s',