1 import { wsUri } from '../env';
18 MarkCommentAsReadForm,
26 GetFollowedCommunitiesForm,
31 AddModToCommunityForm,
32 TransferCommunityForm,
40 MarkUserMentionAsReadForm,
47 EditPrivateMessageForm,
48 DeletePrivateMessageForm,
49 MarkPrivateMessageAsReadForm,
50 GetPrivateMessagesForm,
56 WebSocketJsonResponse,
57 } from '../interfaces';
58 import { UserService } from './';
59 import { i18n } from '../i18next';
60 import { toast } from '../utils';
61 import { Observable } from 'rxjs';
62 import { share } from 'rxjs/operators';
63 import ReconnectingWebSocket from 'reconnecting-websocket';
65 export class WebSocketService {
66 private static _instance: WebSocketService;
67 public ws: ReconnectingWebSocket;
68 public subject: Observable<any>;
70 public admins: Array<UserView>;
71 public banned: Array<UserView>;
73 private constructor() {
74 this.ws = new ReconnectingWebSocket(wsUri);
75 let firstConnect = true;
77 this.subject = Observable.create((obs: any) => {
78 this.ws.onmessage = e => {
79 obs.next(JSON.parse(e.data));
81 this.ws.onopen = () => {
82 console.log(`Connected to ${wsUri}`);
84 if (UserService.Instance.user) {
89 let res: WebSocketJsonResponse = {
100 public static get Instance() {
101 return this._instance || (this._instance = new this());
105 let form: UserJoinForm = { auth: UserService.Instance.auth };
106 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
109 public login(loginForm: LoginForm) {
110 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
113 public register(registerForm: RegisterForm) {
114 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
117 public createCommunity(form: CommunityForm) {
119 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
122 public editCommunity(form: CommunityForm) {
124 this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
127 public deleteCommunity(form: DeleteCommunityForm) {
129 this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
132 public removeCommunity(form: RemoveCommunityForm) {
134 this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
137 public followCommunity(followCommunityForm: FollowCommunityForm) {
138 this.setAuth(followCommunityForm);
140 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
144 public listCommunities(form: ListCommunitiesForm) {
145 this.setAuth(form, false);
146 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
149 public getFollowedCommunities() {
150 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
152 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
156 public listCategories() {
157 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
160 public createPost(form: PostForm) {
162 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, form));
165 public getPost(form: GetPostForm) {
166 this.setAuth(form, false);
167 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
170 public getCommunity(form: GetCommunityForm) {
171 this.setAuth(form, false);
172 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
175 public createComment(form: CommentForm) {
177 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, form));
180 public editComment(form: CommentForm) {
182 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, form));
185 public deleteComment(form: DeleteCommentForm) {
187 this.ws.send(this.wsSendWrapper(UserOperation.DeleteComment, form));
190 public removeComment(form: RemoveCommentForm) {
192 this.ws.send(this.wsSendWrapper(UserOperation.RemoveComment, form));
195 public markCommentAsRead(form: MarkCommentAsReadForm) {
197 this.ws.send(this.wsSendWrapper(UserOperation.MarkCommentAsRead, form));
200 public likeComment(form: CommentLikeForm) {
202 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
205 public saveComment(form: SaveCommentForm) {
207 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
210 public getPosts(form: GetPostsForm) {
211 this.setAuth(form, false);
212 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
215 public getComments(form: GetCommentsForm) {
216 this.setAuth(form, false);
217 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
220 public likePost(form: CreatePostLikeForm) {
222 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
225 public editPost(form: PostForm) {
227 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, form));
230 public deletePost(form: DeletePostForm) {
232 this.ws.send(this.wsSendWrapper(UserOperation.DeletePost, form));
235 public removePost(form: RemovePostForm) {
237 this.ws.send(this.wsSendWrapper(UserOperation.RemovePost, form));
240 public lockPost(form: LockPostForm) {
242 this.ws.send(this.wsSendWrapper(UserOperation.LockPost, form));
245 public stickyPost(form: StickyPostForm) {
247 this.ws.send(this.wsSendWrapper(UserOperation.StickyPost, form));
250 public savePost(form: SavePostForm) {
252 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
255 public banFromCommunity(form: BanFromCommunityForm) {
257 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
260 public addModToCommunity(form: AddModToCommunityForm) {
262 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
265 public transferCommunity(form: TransferCommunityForm) {
267 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
270 public transferSite(form: TransferSiteForm) {
272 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
275 public banUser(form: BanUserForm) {
277 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
280 public addAdmin(form: AddAdminForm) {
282 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
285 public getUserDetails(form: GetUserDetailsForm) {
286 this.setAuth(form, false);
287 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
290 public getReplies(form: GetRepliesForm) {
292 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
295 public getUserMentions(form: GetUserMentionsForm) {
297 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
300 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
302 this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
305 public getModlog(form: GetModlogForm) {
306 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
309 public createSite(siteForm: SiteForm) {
310 this.setAuth(siteForm);
311 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
314 public editSite(siteForm: SiteForm) {
315 this.setAuth(siteForm);
316 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
320 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
323 public getSiteConfig() {
324 let siteConfig: GetSiteConfig = {};
325 this.setAuth(siteConfig);
326 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
329 public search(form: SearchForm) {
330 this.setAuth(form, false);
331 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
334 public markAllAsRead() {
337 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
340 public saveUserSettings(userSettingsForm: UserSettingsForm) {
341 this.setAuth(userSettingsForm);
343 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
347 public deleteAccount(form: DeleteAccountForm) {
349 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
352 public passwordReset(form: PasswordResetForm) {
353 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
356 public passwordChange(form: PasswordChangeForm) {
357 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
360 public createPrivateMessage(form: PrivateMessageForm) {
362 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
365 public editPrivateMessage(form: EditPrivateMessageForm) {
367 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
370 public deletePrivateMessage(form: DeletePrivateMessageForm) {
372 this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
375 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
378 this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
382 public getPrivateMessages(form: GetPrivateMessagesForm) {
384 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
387 public saveSiteConfig(form: SiteConfigForm) {
389 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
392 private wsSendWrapper(op: UserOperation, data: MessageType) {
393 let send = { op: UserOperation[op], data: data };
395 return JSON.stringify(send);
398 private setAuth(obj: any, throwErr: boolean = true) {
399 obj.auth = UserService.Instance.auth;
400 if (obj.auth == null && throwErr) {
401 toast(i18n.t('not_logged_in'), 'danger');
402 throw 'Not logged in';
407 window.onbeforeunload = () => {
408 WebSocketService.Instance.ws.close();