1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
42 } from '../interfaces';
43 import { webSocket } from 'rxjs/webSocket';
44 import { Subject } from 'rxjs';
45 import { retryWhen, delay } from 'rxjs/operators';
46 import { UserService } from './';
47 import { i18n } from '../i18next';
48 import { toast } from '../utils';
50 export class WebSocketService {
51 private static _instance: WebSocketService;
52 public subject: Subject<any>;
55 public admins: Array<UserView>;
56 public banned: Array<UserView>;
58 private constructor() {
59 this.subject = webSocket(wsUri);
61 // Necessary to not keep reconnecting
73 console.log(`Connected to ${wsUri}`);
75 if (UserService.Instance.user) {
79 public static get Instance() {
80 return this._instance || (this._instance = new this());
84 this.wsSendWrapper(UserOperation.ListCategories, undefined);
87 public login(loginForm: LoginForm) {
88 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
91 public register(registerForm: RegisterForm) {
92 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
95 public createCommunity(communityForm: CommunityForm) {
96 this.setAuth(communityForm);
98 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
102 public editCommunity(communityForm: CommunityForm) {
103 this.setAuth(communityForm);
105 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
109 public followCommunity(followCommunityForm: FollowCommunityForm) {
110 this.setAuth(followCommunityForm);
112 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
116 public listCommunities(form: ListCommunitiesForm) {
117 this.setAuth(form, false);
118 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
121 public getFollowedCommunities() {
122 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
124 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
128 public listCategories() {
130 this.wsSendWrapper(UserOperation.ListCategories, undefined)
134 public createPost(postForm: PostForm) {
135 this.setAuth(postForm);
136 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
139 public getPost(form: GetPostForm) {
140 this.setAuth(form, false);
141 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, form));
144 public getCommunity(form: GetCommunityForm) {
145 this.setAuth(form, false);
146 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, form));
149 public createComment(commentForm: CommentForm) {
150 this.setAuth(commentForm);
152 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
156 public editComment(commentForm: CommentForm) {
157 this.setAuth(commentForm);
159 this.wsSendWrapper(UserOperation.EditComment, commentForm)
163 public likeComment(form: CommentLikeForm) {
166 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
170 public saveComment(form: SaveCommentForm) {
172 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
175 public getPosts(form: GetPostsForm) {
176 this.setAuth(form, false);
177 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
180 public likePost(form: CreatePostLikeForm) {
182 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
185 public editPost(postForm: PostForm) {
186 this.setAuth(postForm);
187 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
190 public savePost(form: SavePostForm) {
192 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
195 public banFromCommunity(form: BanFromCommunityForm) {
197 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
200 public addModToCommunity(form: AddModToCommunityForm) {
203 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
207 public transferCommunity(form: TransferCommunityForm) {
210 this.wsSendWrapper(UserOperation.TransferCommunity, form)
214 public transferSite(form: TransferSiteForm) {
216 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
219 public banUser(form: BanUserForm) {
221 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
224 public addAdmin(form: AddAdminForm) {
226 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
229 public getUserDetails(form: GetUserDetailsForm) {
230 this.setAuth(form, false);
231 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
234 public getReplies(form: GetRepliesForm) {
236 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
239 public getUserMentions(form: GetUserMentionsForm) {
241 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
244 public editUserMention(form: EditUserMentionForm) {
246 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
249 public getModlog(form: GetModlogForm) {
250 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
253 public createSite(siteForm: SiteForm) {
254 this.setAuth(siteForm);
255 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
258 public editSite(siteForm: SiteForm) {
259 this.setAuth(siteForm);
260 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
264 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
267 public search(form: SearchForm) {
268 this.setAuth(form, false);
269 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
272 public markAllAsRead() {
275 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
278 public saveUserSettings(userSettingsForm: UserSettingsForm) {
279 this.setAuth(userSettingsForm);
281 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
285 public deleteAccount(form: DeleteAccountForm) {
287 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
290 public passwordReset(form: PasswordResetForm) {
291 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
294 public passwordChange(form: PasswordChangeForm) {
295 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
298 public createPrivateMessage(form: PrivateMessageForm) {
301 this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
305 public editPrivateMessage(form: EditPrivateMessageForm) {
308 this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
312 public getPrivateMessages(form: GetPrivateMessagesForm) {
315 this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
319 private wsSendWrapper(op: UserOperation, data: MessageType) {
320 let send = { op: UserOperation[op], data: data };
325 private setAuth(obj: any, throwErr: boolean = true) {
326 obj.auth = UserService.Instance.auth;
327 if (obj.auth == null && throwErr) {
328 toast(i18n.t('not_logged_in'), 'danger');
329 throw 'Not logged in';
334 window.onbeforeunload = () => {
335 WebSocketService.Instance.subject.unsubscribe();
336 WebSocketService.Instance.subject = null;