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