1 import { wsUri } from '../env';
14 MarkCommentAsReadForm,
22 GetFollowedCommunitiesForm,
27 AddModToCommunityForm,
28 TransferCommunityForm,
36 MarkUserMentionAsReadForm,
43 EditPrivateMessageForm,
44 DeletePrivateMessageForm,
45 MarkPrivateMessageAsReadForm,
46 GetPrivateMessagesForm,
52 WebSocketJsonResponse,
53 } from '../interfaces';
54 import { UserService } from './';
55 import { i18n } from '../i18next';
56 import { toast } from '../utils';
57 import { Observable } from 'rxjs';
58 import { share } from 'rxjs/operators';
59 import ReconnectingWebSocket from 'reconnecting-websocket';
61 export class WebSocketService {
62 private static _instance: WebSocketService;
63 public ws: ReconnectingWebSocket;
64 public subject: Observable<any>;
66 public admins: Array<UserView>;
67 public banned: Array<UserView>;
69 private constructor() {
70 this.ws = new ReconnectingWebSocket(wsUri);
71 let firstConnect = true;
73 this.subject = Observable.create((obs: any) => {
74 this.ws.onmessage = e => {
75 obs.next(JSON.parse(e.data));
77 this.ws.onopen = () => {
78 console.log(`Connected to ${wsUri}`);
80 if (UserService.Instance.user) {
85 let res: WebSocketJsonResponse = {
96 public static get Instance() {
97 return this._instance || (this._instance = new this());
101 let form: UserJoinForm = { auth: UserService.Instance.auth };
102 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
105 public login(loginForm: LoginForm) {
106 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
109 public register(registerForm: RegisterForm) {
110 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
113 public createCommunity(form: CommunityForm) {
115 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
118 public editCommunity(form: CommunityForm) {
120 this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
123 public deleteCommunity(form: DeleteCommunityForm) {
125 this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
128 public removeCommunity(form: RemoveCommunityForm) {
130 this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
133 public followCommunity(followCommunityForm: FollowCommunityForm) {
134 this.setAuth(followCommunityForm);
136 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
140 public listCommunities(form: ListCommunitiesForm) {
141 this.setAuth(form, false);
142 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
145 public getFollowedCommunities() {
146 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
148 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
152 public listCategories() {
153 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
156 public createPost(postForm: PostForm) {
157 this.setAuth(postForm);
158 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
161 public getPost(form: GetPostForm) {
162 this.setAuth(form, false);
163 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
166 public getCommunity(form: GetCommunityForm) {
167 this.setAuth(form, false);
168 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
171 public createComment(form: CommentForm) {
173 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, form));
176 public editComment(form: CommentForm) {
178 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, form));
181 public deleteComment(form: DeleteCommentForm) {
183 this.ws.send(this.wsSendWrapper(UserOperation.DeleteComment, form));
186 public removeComment(form: RemoveCommentForm) {
188 this.ws.send(this.wsSendWrapper(UserOperation.RemoveComment, form));
191 public markCommentAsRead(form: MarkCommentAsReadForm) {
193 this.ws.send(this.wsSendWrapper(UserOperation.MarkCommentAsRead, form));
196 public likeComment(form: CommentLikeForm) {
198 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
201 public saveComment(form: SaveCommentForm) {
203 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
206 public getPosts(form: GetPostsForm) {
207 this.setAuth(form, false);
208 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
211 public getComments(form: GetCommentsForm) {
212 this.setAuth(form, false);
213 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
216 public likePost(form: CreatePostLikeForm) {
218 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
221 public editPost(postForm: PostForm) {
222 this.setAuth(postForm);
223 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
226 public savePost(form: SavePostForm) {
228 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
231 public banFromCommunity(form: BanFromCommunityForm) {
233 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
236 public addModToCommunity(form: AddModToCommunityForm) {
238 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
241 public transferCommunity(form: TransferCommunityForm) {
243 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
246 public transferSite(form: TransferSiteForm) {
248 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
251 public banUser(form: BanUserForm) {
253 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
256 public addAdmin(form: AddAdminForm) {
258 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
261 public getUserDetails(form: GetUserDetailsForm) {
262 this.setAuth(form, false);
263 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
266 public getReplies(form: GetRepliesForm) {
268 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
271 public getUserMentions(form: GetUserMentionsForm) {
273 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
276 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
278 this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
281 public getModlog(form: GetModlogForm) {
282 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
285 public createSite(siteForm: SiteForm) {
286 this.setAuth(siteForm);
287 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
290 public editSite(siteForm: SiteForm) {
291 this.setAuth(siteForm);
292 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
296 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
299 public getSiteConfig() {
300 let siteConfig: GetSiteConfig = {};
301 this.setAuth(siteConfig);
302 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
305 public search(form: SearchForm) {
306 this.setAuth(form, false);
307 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
310 public markAllAsRead() {
313 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
316 public saveUserSettings(userSettingsForm: UserSettingsForm) {
317 this.setAuth(userSettingsForm);
319 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
323 public deleteAccount(form: DeleteAccountForm) {
325 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
328 public passwordReset(form: PasswordResetForm) {
329 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
332 public passwordChange(form: PasswordChangeForm) {
333 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
336 public createPrivateMessage(form: PrivateMessageForm) {
338 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
341 public editPrivateMessage(form: EditPrivateMessageForm) {
343 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
346 public deletePrivateMessage(form: DeletePrivateMessageForm) {
348 this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
351 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
354 this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
358 public getPrivateMessages(form: GetPrivateMessagesForm) {
360 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
363 public saveSiteConfig(form: SiteConfigForm) {
365 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
368 private wsSendWrapper(op: UserOperation, data: MessageType) {
369 let send = { op: UserOperation[op], data: data };
371 return JSON.stringify(send);
374 private setAuth(obj: any, throwErr: boolean = true) {
375 obj.auth = UserService.Instance.auth;
376 if (obj.auth == null && throwErr) {
377 toast(i18n.t('not_logged_in'), 'danger');
378 throw 'Not logged in';
383 window.onbeforeunload = () => {
384 WebSocketService.Instance.ws.close();