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 createCommunity(form: CommunityForm) {
120 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
123 public editCommunity(form: CommunityForm) {
125 this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
128 public deleteCommunity(form: DeleteCommunityForm) {
130 this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
133 public removeCommunity(form: RemoveCommunityForm) {
135 this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
138 public followCommunity(followCommunityForm: FollowCommunityForm) {
139 this.setAuth(followCommunityForm);
141 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
145 public listCommunities(form: ListCommunitiesForm) {
146 this.setAuth(form, false);
147 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
150 public getFollowedCommunities() {
151 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
153 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
157 public listCategories() {
158 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
161 public createPost(form: PostForm) {
163 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, form));
166 public getPost(form: GetPostForm) {
167 this.setAuth(form, false);
168 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
171 public getCommunity(form: GetCommunityForm) {
172 this.setAuth(form, false);
173 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
176 public createComment(form: CommentForm) {
178 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, form));
181 public editComment(form: CommentForm) {
183 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, form));
186 public deleteComment(form: DeleteCommentForm) {
188 this.ws.send(this.wsSendWrapper(UserOperation.DeleteComment, form));
191 public removeComment(form: RemoveCommentForm) {
193 this.ws.send(this.wsSendWrapper(UserOperation.RemoveComment, form));
196 public markCommentAsRead(form: MarkCommentAsReadForm) {
198 this.ws.send(this.wsSendWrapper(UserOperation.MarkCommentAsRead, form));
201 public likeComment(form: CommentLikeForm) {
203 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
206 public saveComment(form: SaveCommentForm) {
208 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
211 public getPosts(form: GetPostsForm) {
212 this.setAuth(form, false);
213 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
216 public getComments(form: GetCommentsForm) {
217 this.setAuth(form, false);
218 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
221 public likePost(form: CreatePostLikeForm) {
223 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
226 public editPost(form: PostForm) {
228 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, form));
231 public deletePost(form: DeletePostForm) {
233 this.ws.send(this.wsSendWrapper(UserOperation.DeletePost, form));
236 public removePost(form: RemovePostForm) {
238 this.ws.send(this.wsSendWrapper(UserOperation.RemovePost, form));
241 public lockPost(form: LockPostForm) {
243 this.ws.send(this.wsSendWrapper(UserOperation.LockPost, form));
246 public stickyPost(form: StickyPostForm) {
248 this.ws.send(this.wsSendWrapper(UserOperation.StickyPost, form));
251 public savePost(form: SavePostForm) {
253 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
256 public banFromCommunity(form: BanFromCommunityForm) {
258 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
261 public addModToCommunity(form: AddModToCommunityForm) {
263 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
266 public transferCommunity(form: TransferCommunityForm) {
268 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
271 public transferSite(form: TransferSiteForm) {
273 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
276 public banUser(form: BanUserForm) {
278 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
281 public addAdmin(form: AddAdminForm) {
283 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
286 public getUserDetails(form: GetUserDetailsForm) {
287 this.setAuth(form, false);
288 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
291 public getReplies(form: GetRepliesForm) {
293 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
296 public getUserMentions(form: GetUserMentionsForm) {
298 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
301 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
303 this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
306 public getModlog(form: GetModlogForm) {
307 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
310 public createSite(siteForm: SiteForm) {
311 this.setAuth(siteForm);
312 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
315 public editSite(siteForm: SiteForm) {
316 this.setAuth(siteForm);
317 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
320 public getSite(form: GetSiteForm = {}) {
321 this.setAuth(form, false);
322 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, form));
325 public getSiteConfig() {
326 let siteConfig: GetSiteConfig = {};
327 this.setAuth(siteConfig);
328 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
331 public search(form: SearchForm) {
332 this.setAuth(form, false);
333 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
336 public markAllAsRead() {
339 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
342 public saveUserSettings(userSettingsForm: UserSettingsForm) {
343 this.setAuth(userSettingsForm);
345 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
349 public deleteAccount(form: DeleteAccountForm) {
351 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
354 public passwordReset(form: PasswordResetForm) {
355 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
358 public passwordChange(form: PasswordChangeForm) {
359 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
362 public createPrivateMessage(form: PrivateMessageForm) {
364 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
367 public editPrivateMessage(form: EditPrivateMessageForm) {
369 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
372 public deletePrivateMessage(form: DeletePrivateMessageForm) {
374 this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
377 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
380 this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
384 public getPrivateMessages(form: GetPrivateMessagesForm) {
386 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
389 public saveSiteConfig(form: SiteConfigForm) {
391 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
394 private wsSendWrapper(op: UserOperation, data: MessageType) {
395 let send = { op: UserOperation[op], data: data };
397 return JSON.stringify(send);
400 private setAuth(obj: any, throwErr: boolean = true) {
401 obj.auth = UserService.Instance.auth;
402 if (obj.auth == null && throwErr) {
403 toast(i18n.t('not_logged_in'), 'danger');
404 throw 'Not logged in';
409 window.onbeforeunload = () => {
410 WebSocketService.Instance.ws.close();