1 import { wsUri } from '../env';
19 AddModToCommunityForm,
20 TransferCommunityForm,
36 EditPrivateMessageForm,
37 GetPrivateMessagesForm,
38 } from '../interfaces';
39 import { webSocket } from 'rxjs/webSocket';
40 import { Subject } from 'rxjs';
41 import { retryWhen, delay } from 'rxjs/operators';
42 import { UserService } from './';
43 import { i18n } from '../i18next';
44 import { toast } from '../utils';
46 export class WebSocketService {
47 private static _instance: WebSocketService;
48 public subject: Subject<any>;
51 public admins: Array<UserView>;
52 public banned: Array<UserView>;
54 private constructor() {
55 this.subject = webSocket(wsUri);
57 // Necessary to not keep reconnecting
69 console.log(`Connected to ${wsUri}`);
71 if (UserService.Instance.user) {
75 public static get Instance() {
76 return this._instance || (this._instance = new this());
80 this.wsSendWrapper(UserOperation.ListCategories, undefined);
83 public login(loginForm: LoginForm) {
84 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
87 public register(registerForm: RegisterForm) {
88 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
91 public createCommunity(communityForm: CommunityForm) {
92 this.setAuth(communityForm);
94 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
98 public editCommunity(communityForm: CommunityForm) {
99 this.setAuth(communityForm);
101 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
105 public followCommunity(followCommunityForm: FollowCommunityForm) {
106 this.setAuth(followCommunityForm);
108 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
112 public listCommunities(form: ListCommunitiesForm) {
113 this.setAuth(form, false);
114 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
117 public getFollowedCommunities() {
118 let data = { auth: UserService.Instance.auth };
120 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
124 public listCategories() {
126 this.wsSendWrapper(UserOperation.ListCategories, undefined)
130 public createPost(postForm: PostForm) {
131 this.setAuth(postForm);
132 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
135 // TODO strictly type these
136 public getPost(postId: number) {
137 let data = { id: postId, auth: UserService.Instance.auth };
138 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
141 public getCommunity(communityId: number) {
142 let data = { id: communityId, auth: UserService.Instance.auth };
143 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
146 public getCommunityByName(name: string) {
147 let data = { name: name, auth: UserService.Instance.auth };
148 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
151 public createComment(commentForm: CommentForm) {
152 this.setAuth(commentForm);
154 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
158 public editComment(commentForm: CommentForm) {
159 this.setAuth(commentForm);
161 this.wsSendWrapper(UserOperation.EditComment, commentForm)
165 public likeComment(form: CommentLikeForm) {
168 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
172 public saveComment(form: SaveCommentForm) {
174 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
177 public getPosts(form: GetPostsForm) {
178 this.setAuth(form, false);
179 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
182 public likePost(form: CreatePostLikeForm) {
184 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
187 public editPost(postForm: PostForm) {
188 this.setAuth(postForm);
189 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
192 public savePost(form: SavePostForm) {
194 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
197 public banFromCommunity(form: BanFromCommunityForm) {
199 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
202 public addModToCommunity(form: AddModToCommunityForm) {
205 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
209 public transferCommunity(form: TransferCommunityForm) {
212 this.wsSendWrapper(UserOperation.TransferCommunity, form)
216 public transferSite(form: TransferSiteForm) {
218 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
221 public banUser(form: BanUserForm) {
223 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
226 public addAdmin(form: AddAdminForm) {
228 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
231 public getUserDetails(form: GetUserDetailsForm) {
232 this.setAuth(form, false);
233 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
236 public getReplies(form: GetRepliesForm) {
238 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
241 public getUserMentions(form: GetUserMentionsForm) {
243 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
246 public editUserMention(form: EditUserMentionForm) {
248 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
251 public getModlog(form: GetModlogForm) {
252 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
255 public createSite(siteForm: SiteForm) {
256 this.setAuth(siteForm);
257 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
260 public editSite(siteForm: SiteForm) {
261 this.setAuth(siteForm);
262 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
266 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
269 public search(form: SearchForm) {
270 this.setAuth(form, false);
271 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
274 public markAllAsRead() {
277 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
280 public saveUserSettings(userSettingsForm: UserSettingsForm) {
281 this.setAuth(userSettingsForm);
283 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
287 public deleteAccount(form: DeleteAccountForm) {
289 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
292 public passwordReset(form: PasswordResetForm) {
293 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
296 public passwordChange(form: PasswordChangeForm) {
297 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
300 public createPrivateMessage(form: PrivateMessageForm) {
303 this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
307 public editPrivateMessage(form: EditPrivateMessageForm) {
310 this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
314 public getPrivateMessages(form: GetPrivateMessagesForm) {
317 this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
321 private wsSendWrapper(op: UserOperation, data: any) {
322 let send = { op: UserOperation[op], data: data };
327 private setAuth(obj: any, throwErr: boolean = true) {
328 obj.auth = UserService.Instance.auth;
329 if (obj.auth == null && throwErr) {
330 toast(i18n.t('not_logged_in'), 'danger');
331 throw 'Not logged in';
336 window.onbeforeunload = () => {
337 WebSocketService.Instance.subject.unsubscribe();
338 WebSocketService.Instance.subject = null;