]> Untitled Git - lemmy.git/blobdiff - ui/src/services/WebSocketService.ts
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / ui / src / services / WebSocketService.ts
index 0b4def1c0946f7c42d032fa6a0906b8b8d336a08..93587c9901a6ed617e41ed95ea8f7a278a052e2f 100644 (file)
@@ -1,12 +1,21 @@
 import { wsUri } from '../env';
 import {
+  LemmyWebsocket,
   LoginForm,
   RegisterForm,
-  UserOperation,
   CommunityForm,
+  DeleteCommunityForm,
+  RemoveCommunityForm,
   PostForm,
+  DeletePostForm,
+  RemovePostForm,
+  LockPostForm,
+  StickyPostForm,
   SavePostForm,
   CommentForm,
+  DeleteCommentForm,
+  RemoveCommentForm,
+  MarkCommentAsReadForm,
   SaveCommentForm,
   CommentLikeForm,
   GetPostForm,
@@ -25,11 +34,10 @@ import {
   TransferSiteForm,
   BanUserForm,
   SiteForm,
-  Site,
   UserView,
   GetRepliesForm,
   GetUserMentionsForm,
-  EditUserMentionForm,
+  MarkUserMentionAsReadForm,
   SearchForm,
   UserSettingsForm,
   DeleteAccountForm,
@@ -37,10 +45,17 @@ import {
   PasswordChangeForm,
   PrivateMessageForm,
   EditPrivateMessageForm,
+  DeletePrivateMessageForm,
+  MarkPrivateMessageAsReadForm,
   GetPrivateMessagesForm,
+  GetCommentsForm,
   UserJoinForm,
-  MessageType,
-} from '../interfaces';
+  GetSiteConfig,
+  GetSiteForm,
+  SiteConfigForm,
+  MarkAllAsReadForm,
+  WebSocketJsonResponse,
+} from 'lemmy-js-client';
 import { UserService } from './';
 import { i18n } from '../i18next';
 import { toast } from '../utils';
@@ -53,25 +68,31 @@ export class WebSocketService {
   public ws: ReconnectingWebSocket;
   public subject: Observable<any>;
 
-  public site: Site;
   public admins: Array<UserView>;
   public banned: Array<UserView>;
+  private client = new LemmyWebsocket();
 
   private constructor() {
     this.ws = new ReconnectingWebSocket(wsUri);
-    this.ws.onopen = () => {
-      console.log(`Connected to ${wsUri}`);
-    };
+    let firstConnect = true;
 
     this.subject = Observable.create((obs: any) => {
       this.ws.onmessage = e => {
         obs.next(JSON.parse(e.data));
       };
-    }).pipe(share());
+      this.ws.onopen = () => {
+        console.log(`Connected to ${wsUri}`);
 
-    if (UserService.Instance.user) {
-      this.userJoin();
-    }
+        if (!firstConnect) {
+          let res: WebSocketJsonResponse = {
+            reconnect: true,
+          };
+          obs.next(res);
+        }
+
+        firstConnect = false;
+      };
+    }).pipe(share());
   }
 
   public static get Instance() {
@@ -80,227 +101,287 @@ export class WebSocketService {
 
   public userJoin() {
     let form: UserJoinForm = { auth: UserService.Instance.auth };
-    this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
+    this.ws.send(this.client.userJoin(form));
+  }
+
+  public login(form: LoginForm) {
+    this.ws.send(this.client.login(form));
   }
 
-  public login(loginForm: LoginForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
+  public register(form: RegisterForm) {
+    this.ws.send(this.client.register(form));
   }
 
-  public register(registerForm: RegisterForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
+  public getCaptcha() {
+    this.ws.send(this.client.getCaptcha());
   }
 
-  public createCommunity(communityForm: CommunityForm) {
-    this.setAuth(communityForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
-    );
+  public createCommunity(form: CommunityForm) {
+    this.setAuth(form); // TODO all these setauths at some point would be good to make required
+    this.ws.send(this.client.createCommunity(form));
   }
 
-  public editCommunity(communityForm: CommunityForm) {
-    this.setAuth(communityForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
-    );
+  public editCommunity(form: CommunityForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.editCommunity(form));
   }
 
-  public followCommunity(followCommunityForm: FollowCommunityForm) {
-    this.setAuth(followCommunityForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
-    );
+  public deleteCommunity(form: DeleteCommunityForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.deleteCommunity(form));
+  }
+
+  public removeCommunity(form: RemoveCommunityForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.removeCommunity(form));
+  }
+
+  public followCommunity(form: FollowCommunityForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.followCommunity(form));
   }
 
   public listCommunities(form: ListCommunitiesForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
+    this.ws.send(this.client.listCommunities(form));
   }
 
   public getFollowedCommunities() {
     let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
-    );
+    this.ws.send(this.client.getFollowedCommunities(form));
   }
 
   public listCategories() {
-    this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
+    this.ws.send(this.client.listCategories());
   }
 
-  public createPost(postForm: PostForm) {
-    this.setAuth(postForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
+  public createPost(form: PostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.createPost(form));
   }
 
   public getPost(form: GetPostForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
+    this.ws.send(this.client.getPost(form));
   }
 
   public getCommunity(form: GetCommunityForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
+    this.ws.send(this.client.getCommunity(form));
+  }
+
+  public createComment(form: CommentForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.createComment(form));
+  }
+
+  public editComment(form: CommentForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.editComment(form));
   }
 
-  public createComment(commentForm: CommentForm) {
-    this.setAuth(commentForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
+  public deleteComment(form: DeleteCommentForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.deleteComment(form));
   }
 
-  public editComment(commentForm: CommentForm) {
-    this.setAuth(commentForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
+  public removeComment(form: RemoveCommentForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.removeComment(form));
+  }
+
+  public markCommentAsRead(form: MarkCommentAsReadForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.markCommentAsRead(form));
   }
 
   public likeComment(form: CommentLikeForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
+    this.ws.send(this.client.likeComment(form));
   }
 
   public saveComment(form: SaveCommentForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
+    this.ws.send(this.client.saveComment(form));
   }
 
   public getPosts(form: GetPostsForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
+    this.ws.send(this.client.getPosts(form));
+  }
+
+  public getComments(form: GetCommentsForm) {
+    this.setAuth(form, false);
+    this.ws.send(this.client.getComments(form));
   }
 
   public likePost(form: CreatePostLikeForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
+    this.ws.send(this.client.likePost(form));
+  }
+
+  public editPost(form: PostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.editPost(form));
+  }
+
+  public deletePost(form: DeletePostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.deletePost(form));
   }
 
-  public editPost(postForm: PostForm) {
-    this.setAuth(postForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
+  public removePost(form: RemovePostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.removePost(form));
+  }
+
+  public lockPost(form: LockPostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.lockPost(form));
+  }
+
+  public stickyPost(form: StickyPostForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.stickyPost(form));
   }
 
   public savePost(form: SavePostForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
+    this.ws.send(this.client.savePost(form));
   }
 
   public banFromCommunity(form: BanFromCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
+    this.ws.send(this.client.banFromCommunity(form));
   }
 
   public addModToCommunity(form: AddModToCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
+    this.ws.send(this.client.addModToCommunity(form));
   }
 
   public transferCommunity(form: TransferCommunityForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
+    this.ws.send(this.client.transferCommunity(form));
   }
 
   public transferSite(form: TransferSiteForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
+    this.ws.send(this.client.transferSite(form));
   }
 
   public banUser(form: BanUserForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
+    this.ws.send(this.client.banUser(form));
   }
 
   public addAdmin(form: AddAdminForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
+    this.ws.send(this.client.addAdmin(form));
   }
 
   public getUserDetails(form: GetUserDetailsForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
+    this.ws.send(this.client.getUserDetails(form));
   }
 
   public getReplies(form: GetRepliesForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
+    this.ws.send(this.client.getReplies(form));
   }
 
   public getUserMentions(form: GetUserMentionsForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
+    this.ws.send(this.client.getUserMentions(form));
   }
 
-  public editUserMention(form: EditUserMentionForm) {
+  public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
+    this.ws.send(this.client.markUserMentionAsRead(form));
   }
 
   public getModlog(form: GetModlogForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
+    this.ws.send(this.client.getModlog(form));
   }
 
-  public createSite(siteForm: SiteForm) {
-    this.setAuth(siteForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
+  public createSite(form: SiteForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.createSite(form));
   }
 
-  public editSite(siteForm: SiteForm) {
-    this.setAuth(siteForm);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
+  public editSite(form: SiteForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.editSite(form));
   }
 
-  public getSite() {
-    this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
+  public getSite(form: GetSiteForm = {}) {
+    this.setAuth(form, false);
+    this.ws.send(this.client.getSite(form));
+  }
+
+  public getSiteConfig() {
+    let form: GetSiteConfig = {};
+    this.setAuth(form);
+    this.ws.send(this.client.getSiteConfig(form));
   }
 
   public search(form: SearchForm) {
     this.setAuth(form, false);
-    this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
+    this.ws.send(this.client.search(form));
   }
 
   public markAllAsRead() {
-    let form = {};
+    let form: MarkAllAsReadForm;
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
+    this.ws.send(this.client.markAllAsRead(form));
   }
 
-  public saveUserSettings(userSettingsForm: UserSettingsForm) {
-    this.setAuth(userSettingsForm);
-    this.ws.send(
-      this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
-    );
+  public saveUserSettings(form: UserSettingsForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.saveUserSettings(form));
   }
 
   public deleteAccount(form: DeleteAccountForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
+    this.ws.send(this.client.deleteAccount(form));
   }
 
   public passwordReset(form: PasswordResetForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
+    this.ws.send(this.client.passwordReset(form));
   }
 
   public passwordChange(form: PasswordChangeForm) {
-    this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
+    this.ws.send(this.client.passwordChange(form));
   }
 
   public createPrivateMessage(form: PrivateMessageForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
+    this.ws.send(this.client.createPrivateMessage(form));
   }
 
   public editPrivateMessage(form: EditPrivateMessageForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
+    this.ws.send(this.client.editPrivateMessage(form));
+  }
+
+  public deletePrivateMessage(form: DeletePrivateMessageForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.deletePrivateMessage(form));
+  }
+
+  public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.markPrivateMessageAsRead(form));
   }
 
   public getPrivateMessages(form: GetPrivateMessagesForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
+    this.ws.send(this.client.getPrivateMessages(form));
   }
 
-  private wsSendWrapper(op: UserOperation, data: MessageType) {
-    let send = { op: UserOperation[op], data: data };
-    console.log(send);
-    return JSON.stringify(send);
+  public saveSiteConfig(form: SiteConfigForm) {
+    this.setAuth(form);
+    this.ws.send(this.client.saveSiteConfig(form));
   }
 
   private setAuth(obj: any, throwErr: boolean = true) {