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}`);
72 public static get Instance() {
73 return this._instance || (this._instance = new this());
76 public login(loginForm: LoginForm) {
77 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
80 public register(registerForm: RegisterForm) {
81 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
84 public createCommunity(communityForm: CommunityForm) {
85 this.setAuth(communityForm);
87 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
91 public editCommunity(communityForm: CommunityForm) {
92 this.setAuth(communityForm);
94 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
98 public followCommunity(followCommunityForm: FollowCommunityForm) {
99 this.setAuth(followCommunityForm);
101 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
105 public listCommunities(form: ListCommunitiesForm) {
106 this.setAuth(form, false);
107 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
110 public getFollowedCommunities() {
111 let data = { auth: UserService.Instance.auth };
113 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
117 public listCategories() {
119 this.wsSendWrapper(UserOperation.ListCategories, undefined)
123 public createPost(postForm: PostForm) {
124 this.setAuth(postForm);
125 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
128 public getPost(postId: number) {
129 let data = { id: postId, auth: UserService.Instance.auth };
130 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
133 public getCommunity(communityId: number) {
134 let data = { id: communityId, auth: UserService.Instance.auth };
135 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
138 public getCommunityByName(name: string) {
139 let data = { name: name, auth: UserService.Instance.auth };
140 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
143 public createComment(commentForm: CommentForm) {
144 this.setAuth(commentForm);
146 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
150 public editComment(commentForm: CommentForm) {
151 this.setAuth(commentForm);
153 this.wsSendWrapper(UserOperation.EditComment, commentForm)
157 public likeComment(form: CommentLikeForm) {
160 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
164 public saveComment(form: SaveCommentForm) {
166 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
169 public getPosts(form: GetPostsForm) {
170 this.setAuth(form, false);
171 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
174 public likePost(form: CreatePostLikeForm) {
176 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
179 public editPost(postForm: PostForm) {
180 this.setAuth(postForm);
181 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
184 public savePost(form: SavePostForm) {
186 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
189 public banFromCommunity(form: BanFromCommunityForm) {
191 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
194 public addModToCommunity(form: AddModToCommunityForm) {
197 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
201 public transferCommunity(form: TransferCommunityForm) {
204 this.wsSendWrapper(UserOperation.TransferCommunity, form)
208 public transferSite(form: TransferSiteForm) {
210 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
213 public banUser(form: BanUserForm) {
215 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
218 public addAdmin(form: AddAdminForm) {
220 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
223 public getUserDetails(form: GetUserDetailsForm) {
224 this.setAuth(form, false);
225 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
228 public getReplies(form: GetRepliesForm) {
230 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
233 public getUserMentions(form: GetUserMentionsForm) {
235 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
238 public editUserMention(form: EditUserMentionForm) {
240 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
243 public getModlog(form: GetModlogForm) {
244 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
247 public createSite(siteForm: SiteForm) {
248 this.setAuth(siteForm);
249 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
252 public editSite(siteForm: SiteForm) {
253 this.setAuth(siteForm);
254 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
258 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
261 public search(form: SearchForm) {
262 this.setAuth(form, false);
263 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
266 public markAllAsRead() {
269 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
272 public saveUserSettings(userSettingsForm: UserSettingsForm) {
273 this.setAuth(userSettingsForm);
275 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
279 public deleteAccount(form: DeleteAccountForm) {
281 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
284 public passwordReset(form: PasswordResetForm) {
285 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
288 public passwordChange(form: PasswordChangeForm) {
289 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
292 public createPrivateMessage(form: PrivateMessageForm) {
295 this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
299 public editPrivateMessage(form: EditPrivateMessageForm) {
302 this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
306 public getPrivateMessages(form: GetPrivateMessagesForm) {
309 this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
313 private wsSendWrapper(op: UserOperation, data: any) {
314 let send = { op: UserOperation[op], data: data };
319 private setAuth(obj: any, throwErr: boolean = true) {
320 obj.auth = UserService.Instance.auth;
321 if (obj.auth == null && throwErr) {
322 toast(i18n.t('not_logged_in'), 'danger');
323 throw 'Not logged in';
328 window.onbeforeunload = () => {
329 WebSocketService.Instance.subject.unsubscribe();
330 WebSocketService.Instance.subject = null;