1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
38 EditPrivateMessageForm,
39 DeletePrivateMessageForm,
40 MarkPrivateMessageAsReadForm,
41 GetPrivateMessagesForm,
47 WebSocketJsonResponse,
48 } from '../interfaces';
49 import { UserService } from './';
50 import { i18n } from '../i18next';
51 import { toast } from '../utils';
52 import { Observable } from 'rxjs';
53 import { share } from 'rxjs/operators';
54 import ReconnectingWebSocket from 'reconnecting-websocket';
56 export class WebSocketService {
57 private static _instance: WebSocketService;
58 public ws: ReconnectingWebSocket;
59 public subject: Observable<any>;
61 public admins: Array<UserView>;
62 public banned: Array<UserView>;
64 private constructor() {
65 this.ws = new ReconnectingWebSocket(wsUri);
66 let firstConnect = true;
68 this.subject = Observable.create((obs: any) => {
69 this.ws.onmessage = e => {
70 obs.next(JSON.parse(e.data));
72 this.ws.onopen = () => {
73 console.log(`Connected to ${wsUri}`);
75 if (UserService.Instance.user) {
80 let res: WebSocketJsonResponse = {
91 public static get Instance() {
92 return this._instance || (this._instance = new this());
96 let form: UserJoinForm = { auth: UserService.Instance.auth };
97 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
100 public login(loginForm: LoginForm) {
101 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
104 public register(registerForm: RegisterForm) {
105 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
108 public createCommunity(communityForm: CommunityForm) {
109 this.setAuth(communityForm);
111 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
115 public editCommunity(communityForm: CommunityForm) {
116 this.setAuth(communityForm);
118 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
122 public followCommunity(followCommunityForm: FollowCommunityForm) {
123 this.setAuth(followCommunityForm);
125 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
129 public listCommunities(form: ListCommunitiesForm) {
130 this.setAuth(form, false);
131 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
134 public getFollowedCommunities() {
135 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
137 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
141 public listCategories() {
142 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
145 public createPost(postForm: PostForm) {
146 this.setAuth(postForm);
147 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
150 public getPost(form: GetPostForm) {
151 this.setAuth(form, false);
152 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
155 public getCommunity(form: GetCommunityForm) {
156 this.setAuth(form, false);
157 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
160 public createComment(commentForm: CommentForm) {
161 this.setAuth(commentForm);
162 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
165 public editComment(commentForm: CommentForm) {
166 this.setAuth(commentForm);
167 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
170 public likeComment(form: CommentLikeForm) {
172 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
175 public saveComment(form: SaveCommentForm) {
177 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
180 public getPosts(form: GetPostsForm) {
181 this.setAuth(form, false);
182 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
185 public getComments(form: GetCommentsForm) {
186 this.setAuth(form, false);
187 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
190 public likePost(form: CreatePostLikeForm) {
192 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
195 public editPost(postForm: PostForm) {
196 this.setAuth(postForm);
197 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
200 public savePost(form: SavePostForm) {
202 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
205 public banFromCommunity(form: BanFromCommunityForm) {
207 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
210 public addModToCommunity(form: AddModToCommunityForm) {
212 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
215 public transferCommunity(form: TransferCommunityForm) {
217 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
220 public transferSite(form: TransferSiteForm) {
222 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
225 public banUser(form: BanUserForm) {
227 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
230 public addAdmin(form: AddAdminForm) {
232 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
235 public getUserDetails(form: GetUserDetailsForm) {
236 this.setAuth(form, false);
237 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
240 public getReplies(form: GetRepliesForm) {
242 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
245 public getUserMentions(form: GetUserMentionsForm) {
247 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
250 public editUserMention(form: EditUserMentionForm) {
252 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
255 public getModlog(form: GetModlogForm) {
256 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
259 public createSite(siteForm: SiteForm) {
260 this.setAuth(siteForm);
261 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
264 public editSite(siteForm: SiteForm) {
265 this.setAuth(siteForm);
266 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
270 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
273 public getSiteConfig() {
274 let siteConfig: GetSiteConfig = {};
275 this.setAuth(siteConfig);
276 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
279 public search(form: SearchForm) {
280 this.setAuth(form, false);
281 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
284 public markAllAsRead() {
287 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
290 public saveUserSettings(userSettingsForm: UserSettingsForm) {
291 this.setAuth(userSettingsForm);
293 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
297 public deleteAccount(form: DeleteAccountForm) {
299 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
302 public passwordReset(form: PasswordResetForm) {
303 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
306 public passwordChange(form: PasswordChangeForm) {
307 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
310 public createPrivateMessage(form: PrivateMessageForm) {
312 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
315 public editPrivateMessage(form: EditPrivateMessageForm) {
317 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
320 public deletePrivateMessage(form: DeletePrivateMessageForm) {
322 this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
325 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
328 this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
332 public getPrivateMessages(form: GetPrivateMessagesForm) {
334 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
337 public saveSiteConfig(form: SiteConfigForm) {
339 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
342 private wsSendWrapper(op: UserOperation, data: MessageType) {
343 let send = { op: UserOperation[op], data: data };
345 return JSON.stringify(send);
348 private setAuth(obj: any, throwErr: boolean = true) {
349 obj.auth = UserService.Instance.auth;
350 if (obj.auth == null && throwErr) {
351 toast(i18n.t('not_logged_in'), 'danger');
352 throw 'Not logged in';
357 window.onbeforeunload = () => {
358 WebSocketService.Instance.ws.close();