]> Untitled Git - lemmy.git/commitdiff
Strictly typing websocket forms.
authorDessalines <tyhou13@gmx.com>
Tue, 28 Jan 2020 02:04:30 +0000 (21:04 -0500)
committerDessalines <tyhou13@gmx.com>
Tue, 28 Jan 2020 02:04:30 +0000 (21:04 -0500)
ui/src/components/community.tsx
ui/src/components/inbox.tsx
ui/src/components/post.tsx
ui/src/interfaces.ts
ui/src/services/WebSocketService.ts

index 221c9211eb6af81c90a17f78d340fba7424b2c76..3c5f68906ea1ec5d7a60379f4df7aeb6bdb62266 100644 (file)
@@ -11,6 +11,7 @@ import {
   SortType,
   Post,
   GetPostsForm,
+  GetCommunityForm,
   ListingType,
   GetPostsResponse,
   CreatePostLikeResponse,
@@ -98,11 +99,11 @@ export class Community extends Component<any, State> {
         () => console.log('complete')
       );
 
-    if (this.state.communityId) {
-      WebSocketService.Instance.getCommunity(this.state.communityId);
-    } else if (this.state.communityName) {
-      WebSocketService.Instance.getCommunityByName(this.state.communityName);
-    }
+    let form: GetCommunityForm = {
+      id: this.state.communityId ? this.state.communityId : null,
+      name: this.state.communityName ? this.state.communityName : null,
+    };
+    WebSocketService.Instance.getCommunity(form);
   }
 
   componentWillUnmount() {
index ba5cc6ad52fe26a9b65c6bded98036970a393df3..41c1ce60d730fd4029e3fe700eda4f91f4e39023 100644 (file)
@@ -38,6 +38,8 @@ enum UnreadType {
   Messages,
 }
 
+type ReplyType = Comment | PrivateMessageI;
+
 interface InboxState {
   unreadOrAll: UnreadOrAll;
   unreadType: UnreadType;
@@ -186,7 +188,7 @@ export class Inbox extends Component<any, InboxState> {
   }
 
   all() {
-    let combined: Array<Comment | PrivateMessageI> = [];
+    let combined: Array<ReplyType> = [];
 
     combined.push(...this.state.replies);
     combined.push(...this.state.mentions);
index f57d891376c6d10d54df92da23db414325153ee4..9ea275ce2615c5aaf4ff19a6dde4959756dbd002 100644 (file)
@@ -23,6 +23,7 @@ import {
   SearchType,
   SortType,
   SearchForm,
+  GetPostForm,
   SearchResponse,
   GetSiteResponse,
   GetCommunityResponse,
@@ -84,7 +85,10 @@ export class Post extends Component<any, PostState> {
         () => console.log('complete')
       );
 
-    WebSocketService.Instance.getPost(postId);
+    let form: GetPostForm = {
+      id: postId,
+    };
+    WebSocketService.Instance.getPost(form);
   }
 
   componentWillUnmount() {
index cd3961b5f7252f11b586bbd7d95f4647dc7f97f7..b0594e8de0282ca24659e9dee593a61116978459 100644 (file)
@@ -248,6 +248,10 @@ export interface FollowCommunityForm {
   auth?: string;
 }
 
+export interface GetFollowedCommunitiesForm {
+  auth: string;
+}
+
 export interface GetFollowedCommunitiesResponse {
   communities: Array<CommunityUser>;
 }
@@ -523,6 +527,12 @@ export interface CommunityForm {
   auth?: string;
 }
 
+export interface GetCommunityForm {
+  id?: number;
+  name?: string;
+  auth?: string;
+}
+
 export interface GetCommunityResponse {
   community: Community;
   moderators: Array<CommunityUser>;
@@ -572,6 +582,11 @@ export interface PostFormParams {
   community?: string;
 }
 
+export interface GetPostForm {
+  id: number;
+  auth?: string;
+}
+
 export interface GetPostResponse {
   post: Post;
   comments: Array<Comment>;
@@ -759,6 +774,45 @@ export interface PrivateMessageResponse {
   message: PrivateMessage;
 }
 
+export type MessageType =
+  | EditPrivateMessageForm
+  | LoginForm
+  | RegisterForm
+  | CommunityForm
+  | FollowCommunityForm
+  | ListCommunitiesForm
+  | GetFollowedCommunitiesForm
+  | PostForm
+  | GetPostForm
+  | GetPostsForm
+  | GetCommunityForm
+  | CommentForm
+  | CommentLikeForm
+  | SaveCommentForm
+  | CreatePostLikeForm
+  | BanFromCommunityForm
+  | AddAdminForm
+  | AddModToCommunityForm
+  | TransferCommunityForm
+  | TransferSiteForm
+  | SaveCommentForm
+  | BanUserForm
+  | AddAdminForm
+  | GetUserDetailsForm
+  | GetRepliesForm
+  | GetUserMentionsForm
+  | EditUserMentionForm
+  | GetModlogForm
+  | SiteForm
+  | SearchForm
+  | UserSettingsForm
+  | DeleteAccountForm
+  | PasswordResetForm
+  | PasswordChangeForm
+  | PrivateMessageForm
+  | EditPrivateMessageForm
+  | GetPrivateMessagesForm;
+
 type ResponseType =
   | SiteResponse
   | GetFollowedCommunitiesResponse
index e72a28716ffdc8d1d17876aa7f40dc1829e2acf0..9f09e56ae9f6b302ce15fd1fcd7b154f3c165128 100644 (file)
@@ -9,9 +9,12 @@ import {
   CommentForm,
   SaveCommentForm,
   CommentLikeForm,
+  GetPostForm,
   GetPostsForm,
   CreatePostLikeForm,
+  GetCommunityForm,
   FollowCommunityForm,
+  GetFollowedCommunitiesForm,
   GetUserDetailsForm,
   ListCommunitiesForm,
   GetModlogForm,
@@ -35,6 +38,7 @@ import {
   PrivateMessageForm,
   EditPrivateMessageForm,
   GetPrivateMessagesForm,
+  MessageType,
 } from '../interfaces';
 import { webSocket } from 'rxjs/webSocket';
 import { Subject } from 'rxjs';
@@ -108,9 +112,9 @@ export class WebSocketService {
   }
 
   public getFollowedCommunities() {
-    let data = { auth: UserService.Instance.auth };
+    let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
     this.subject.next(
-      this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
+      this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
     );
   }
 
@@ -125,19 +129,14 @@ export class WebSocketService {
     this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
   }
 
-  public getPost(postId: number) {
-    let data = { id: postId, auth: UserService.Instance.auth };
-    this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
-  }
-
-  public getCommunity(communityId: number) {
-    let data = { id: communityId, auth: UserService.Instance.auth };
-    this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
+  public getPost(form: GetPostForm) {
+    this.setAuth(form);
+    this.subject.next(this.wsSendWrapper(UserOperation.GetPost, form));
   }
 
-  public getCommunityByName(name: string) {
-    let data = { name: name, auth: UserService.Instance.auth };
-    this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
+  public getCommunity(form: GetCommunityForm) {
+    this.setAuth(form);
+    this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, form));
   }
 
   public createComment(commentForm: CommentForm) {
@@ -310,7 +309,7 @@ export class WebSocketService {
     );
   }
 
-  private wsSendWrapper(op: UserOperation, data: any) {
+  private wsSendWrapper(op: UserOperation, data: MessageType) {
     let send = { op: UserOperation[op], data: data };
     console.log(send);
     return send;