1 import { wsUri } from '../env';
19 AddModToCommunityForm,
20 TransferCommunityForm,
35 } from '../interfaces';
36 import { webSocket } from 'rxjs/webSocket';
37 import { Subject } from 'rxjs';
38 import { retryWhen, delay, take } from 'rxjs/operators';
39 import { UserService } from './';
40 import { i18n } from '../i18next';
42 export class WebSocketService {
43 private static _instance: WebSocketService;
44 public subject: Subject<any>;
47 public admins: Array<UserView>;
48 public banned: Array<UserView>;
50 private constructor() {
51 this.subject = webSocket(wsUri);
53 // Necessary to not keep reconnecting
65 console.log(`Connected to ${wsUri}`);
68 public static get Instance() {
69 return this._instance || (this._instance = new this());
72 public login(loginForm: LoginForm) {
73 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
76 public register(registerForm: RegisterForm) {
77 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
80 public createCommunity(communityForm: CommunityForm) {
81 this.setAuth(communityForm);
83 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
87 public editCommunity(communityForm: CommunityForm) {
88 this.setAuth(communityForm);
90 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
94 public followCommunity(followCommunityForm: FollowCommunityForm) {
95 this.setAuth(followCommunityForm);
97 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
101 public listCommunities(form: ListCommunitiesForm) {
102 this.setAuth(form, false);
103 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
106 public getFollowedCommunities() {
107 let data = { auth: UserService.Instance.auth };
109 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
113 public listCategories() {
115 this.wsSendWrapper(UserOperation.ListCategories, undefined)
119 public createPost(postForm: PostForm) {
120 this.setAuth(postForm);
121 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
124 public getPost(postId: number) {
125 let data = { id: postId, auth: UserService.Instance.auth };
126 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
129 public getCommunity(communityId: number) {
130 let data = { id: communityId, auth: UserService.Instance.auth };
131 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
134 public getCommunityByName(name: string) {
135 let data = { name: name, auth: UserService.Instance.auth };
136 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
139 public createComment(commentForm: CommentForm) {
140 this.setAuth(commentForm);
142 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
146 public editComment(commentForm: CommentForm) {
147 this.setAuth(commentForm);
149 this.wsSendWrapper(UserOperation.EditComment, commentForm)
153 public likeComment(form: CommentLikeForm) {
156 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
160 public saveComment(form: SaveCommentForm) {
162 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
165 public getPosts(form: GetPostsForm) {
166 this.setAuth(form, false);
167 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
170 public likePost(form: CreatePostLikeForm) {
172 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
175 public editPost(postForm: PostForm) {
176 this.setAuth(postForm);
177 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
180 public savePost(form: SavePostForm) {
182 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
185 public banFromCommunity(form: BanFromCommunityForm) {
187 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
190 public addModToCommunity(form: AddModToCommunityForm) {
193 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
197 public transferCommunity(form: TransferCommunityForm) {
200 this.wsSendWrapper(UserOperation.TransferCommunity, form)
204 public transferSite(form: TransferSiteForm) {
206 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
209 public banUser(form: BanUserForm) {
211 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
214 public addAdmin(form: AddAdminForm) {
216 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
219 public getUserDetails(form: GetUserDetailsForm) {
220 this.setAuth(form, false);
221 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
224 public getReplies(form: GetRepliesForm) {
226 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
229 public getUserMentions(form: GetUserMentionsForm) {
231 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
234 public editUserMention(form: EditUserMentionForm) {
236 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
239 public getModlog(form: GetModlogForm) {
240 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
243 public createSite(siteForm: SiteForm) {
244 this.setAuth(siteForm);
245 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
248 public editSite(siteForm: SiteForm) {
249 this.setAuth(siteForm);
250 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
254 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
257 public search(form: SearchForm) {
258 this.setAuth(form, false);
259 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
262 public markAllAsRead() {
265 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
268 public saveUserSettings(userSettingsForm: UserSettingsForm) {
269 this.setAuth(userSettingsForm);
271 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
275 public deleteAccount(form: DeleteAccountForm) {
277 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
280 public passwordReset(form: PasswordResetForm) {
281 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
284 public passwordChange(form: PasswordChangeForm) {
285 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
288 private wsSendWrapper(op: UserOperation, data: any) {
289 let send = { op: UserOperation[op], data: data };
294 private setAuth(obj: any, throwErr: boolean = true) {
295 obj.auth = UserService.Instance.auth;
296 if (obj.auth == null && throwErr) {
297 alert(i18n.t('not_logged_in'));
298 throw 'Not logged in';
303 window.onbeforeunload = () => {
304 WebSocketService.Instance.subject.unsubscribe();
305 WebSocketService.Instance.subject = null;