From 368d4199a5426bfb65b4af5afe376b45d33ea310 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 24 Dec 2020 17:05:57 -0500 Subject: [PATCH] Actually sending ws requests, some other fixes. --- Dockerfile | 4 +- src/server/index.tsx | 4 +- src/shared/components/admin-settings.tsx | 18 ++++--- src/shared/components/app.tsx | 2 +- src/shared/components/comment-form.tsx | 18 ++++--- src/shared/components/comment-node.tsx | 54 ++++++++++--------- src/shared/components/communities.tsx | 21 +++++--- src/shared/components/community-form.tsx | 14 +++-- src/shared/components/community.tsx | 41 ++++++++------ src/shared/components/create-community.tsx | 3 +- src/shared/components/create-post.tsx | 11 ++-- .../components/create-private-message.tsx | 6 ++- src/shared/components/inbox.tsx | 24 +++++---- src/shared/components/login.tsx | 30 ++++++----- src/shared/components/main.tsx | 35 +++++++----- src/shared/components/modlog.tsx | 3 +- src/shared/components/navbar.tsx | 41 +++++++++----- src/shared/components/password_change.tsx | 5 +- src/shared/components/post-form.tsx | 18 ++++--- src/shared/components/post-listing.tsx | 54 ++++++++++--------- src/shared/components/post.tsx | 37 +++++++------ .../components/private-message-form.tsx | 14 ++--- src/shared/components/private-message.tsx | 10 ++-- src/shared/components/search.tsx | 11 ++-- src/shared/components/setup.tsx | 4 +- src/shared/components/sidebar.tsx | 22 ++++---- src/shared/components/site-form.tsx | 17 +++--- src/shared/components/user.tsx | 21 +++++--- src/shared/env.ts | 4 +- src/shared/services/UserService.ts | 11 ---- src/shared/services/WebSocketService.ts | 25 +++++---- src/shared/utils.ts | 30 +++++++++-- 32 files changed, 362 insertions(+), 250 deletions(-) diff --git a/Dockerfile b/Dockerfile index 745651b5..27814220 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM node:14-alpine as builder -RUN apk update && apk add yarn curl bash && rm -rf /var/cache/apk/* +RUN apk add yarn curl bash --no-cache RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin @@ -24,7 +24,7 @@ RUN yarn build:prod # Pruning # RUN npm prune --production -RUN /usr/local/bin/node-prune +RUN node-prune FROM node:14-alpine as runner COPY --from=builder /usr/src/app/dist /app/dist diff --git a/src/server/index.tsx b/src/server/index.tsx index a2bcd4ab..10e935d9 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -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[] = []; diff --git a/src/shared/components/admin-settings.tsx b/src/shared/components/admin-settings.tsx index b9f93f48..6b985a43 100644 --- a/src/shared/components/admin-settings.tsx +++ b/src/shared/components/admin-settings.tsx @@ -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 { 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 { 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 { 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); } diff --git a/src/shared/components/app.tsx b/src/shared/components/app.tsx index 26a28869..8c9fa9f9 100644 --- a/src/shared/components/app.tsx +++ b/src/shared/components/app.tsx @@ -28,7 +28,7 @@ export class App extends Component {
{siteRes && - siteRes.site_view.site && + siteRes.site_view && this.props.siteRes.site_view.site.icon && ( { {UserService.Instance.user ? ( { 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); } diff --git a/src/shared/components/comment-node.tsx b/src/shared/components/comment-node.tsx index 3d69812c..c08edc3a 100644 --- a/src/shared/components/comment-node.tsx +++ b/src/shared/components/comment-node.tsx @@ -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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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); } diff --git a/src/shared/components/communities.tsx b/src/shared/components/communities.tsx index 148a4136..236f777d 100644 --- a/src/shared/components/communities.tsx +++ b/src/shared/components/communities.tsx @@ -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 { 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 { 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[] { @@ -244,8 +249,8 @@ export class Communities extends Component { sort: SortType.TopAll, limit: communityLimit, page, - auth: req.auth, }; + setOptionalAuth(listCommunitiesForm, req.auth); return [req.client.listCommunities(listCommunitiesForm)]; } diff --git a/src/shared/components/community-form.tsx b/src/shared/components/community-form.tsx index 4a3dad4b..b40899af 100644 --- a/src/shared/components/community-form.tsx +++ b/src/shared/components/community-form.tsx @@ -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); } diff --git a/src/shared/components/community.tsx b/src/shared/components/community.tsx index aa0cbf02..6025e911 100644 --- a/src/shared/components/community.tsx +++ b/src/shared/components/community.tsx @@ -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 { } else { this.fetchCommunity(); this.fetchData(); - WebSocketService.Instance.client.listCategories(); + WebSocketService.Instance.send(wsClient.listCategories()); } setupTippy(); } @@ -135,9 +138,9 @@ export class Community extends Component { 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 { } 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 { 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 { 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 { } render() { - let cv = this.state.communityRes.community_view; + let cv = this.state.communityRes?.community_view; return (
{this.state.communityLoading ? ( @@ -418,9 +421,9 @@ export class Community extends Component { 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 { 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 { 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(msg).data; @@ -452,9 +457,11 @@ export class Community extends Component { 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 || diff --git a/src/shared/components/create-community.tsx b/src/shared/components/create-community.tsx index b9891838..aab54807 100644 --- a/src/shared/components/create-community.tsx +++ b/src/shared/components/create-community.tsx @@ -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 { this.state.categories = this.isoData.routeData[0].categories; this.state.loading = false; } else { - WebSocketService.Instance.client.listCategories(); + WebSocketService.Instance.send(wsClient.listCategories()); } } diff --git a/src/shared/components/create-post.tsx b/src/shared/components/create-post.tsx index 35fd6a8e..edb2f059 100644 --- a/src/shared/components/create-post.tsx +++ b/src/shared/components/create-post.tsx @@ -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 { 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 { let listCommunitiesForm: ListCommunities = { sort: SortType.TopAll, limit: 9999, - auth: req.auth, }; + setOptionalAuth(listCommunitiesForm, req.auth); return [req.client.listCommunities(listCommunitiesForm)]; } diff --git a/src/shared/components/create-private-message.tsx b/src/shared/components/create-private-message.tsx index 90e17c90..506e3eef 100644 --- a/src/shared/components/create-private-message.tsx +++ b/src/shared/components/create-private-message.tsx @@ -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[] { diff --git a/src/shared/components/inbox.tsx b/src/shared/components/inbox.tsx index 5d01feff..0a4c11ca 100644 --- a/src/shared/components/inbox.tsx +++ b/src/shared/components/inbox.tsx @@ -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 { 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 { } 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 = []; diff --git a/src/shared/components/login.tsx b/src/shared/components/login.tsx index 8ca93967..c650c38e 100644 --- a/src/shared/components/login.tsx +++ b/src/shared/components/login.tsx @@ -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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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(msg).data; diff --git a/src/shared/components/main.tsx b/src/shared/components/main.tsx index 0bfe08dc..fbccf51a 100644 --- a/src/shared/components/main.tsx +++ b/src/shared/components/main.tsx @@ -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 { 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 { 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 { 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 { 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 { 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 { 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 { 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(msg).data; diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index fcd18e59..cfdaef74 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -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 { page: this.state.page, limit: fetchLimit, }; - WebSocketService.Instance.client.getModlog(modlogForm); + WebSocketService.Instance.send(wsClient.getModlog(modlogForm)); } static fetchInitialData(req: InitialFetchRequest): Promise[] { diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx index e31d0d51..ea191b8f 100644 --- a/src/shared/components/navbar.tsx +++ b/src/shared/components/navbar.tsx @@ -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 { // 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 { // 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 { // 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 (