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';
45 export class WebSocketService {
46 private static _instance: WebSocketService;
47 public subject: Subject<any>;
50 public admins: Array<UserView>;
51 public banned: Array<UserView>;
53 private constructor() {
54 this.subject = webSocket(wsUri);
56 // Necessary to not keep reconnecting
68 console.log(`Connected to ${wsUri}`);
71 public static get Instance() {
72 return this._instance || (this._instance = new this());
75 public login(loginForm: LoginForm) {
76 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
79 public register(registerForm: RegisterForm) {
80 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
83 public createCommunity(communityForm: CommunityForm) {
84 this.setAuth(communityForm);
86 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
90 public editCommunity(communityForm: CommunityForm) {
91 this.setAuth(communityForm);
93 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
97 public followCommunity(followCommunityForm: FollowCommunityForm) {
98 this.setAuth(followCommunityForm);
100 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
104 public listCommunities(form: ListCommunitiesForm) {
105 this.setAuth(form, false);
106 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
109 public getFollowedCommunities() {
110 let data = { auth: UserService.Instance.auth };
112 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
116 public listCategories() {
118 this.wsSendWrapper(UserOperation.ListCategories, undefined)
122 public createPost(postForm: PostForm) {
123 this.setAuth(postForm);
124 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
127 public getPost(postId: number) {
128 let data = { id: postId, auth: UserService.Instance.auth };
129 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
132 public getCommunity(communityId: number) {
133 let data = { id: communityId, auth: UserService.Instance.auth };
134 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
137 public getCommunityByName(name: string) {
138 let data = { name: name, auth: UserService.Instance.auth };
139 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
142 public createComment(commentForm: CommentForm) {
143 this.setAuth(commentForm);
145 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
149 public editComment(commentForm: CommentForm) {
150 this.setAuth(commentForm);
152 this.wsSendWrapper(UserOperation.EditComment, commentForm)
156 public likeComment(form: CommentLikeForm) {
159 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
163 public saveComment(form: SaveCommentForm) {
165 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
168 public getPosts(form: GetPostsForm) {
169 this.setAuth(form, false);
170 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
173 public likePost(form: CreatePostLikeForm) {
175 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
178 public editPost(postForm: PostForm) {
179 this.setAuth(postForm);
180 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
183 public savePost(form: SavePostForm) {
185 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
188 public banFromCommunity(form: BanFromCommunityForm) {
190 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
193 public addModToCommunity(form: AddModToCommunityForm) {
196 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
200 public transferCommunity(form: TransferCommunityForm) {
203 this.wsSendWrapper(UserOperation.TransferCommunity, form)
207 public transferSite(form: TransferSiteForm) {
209 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
212 public banUser(form: BanUserForm) {
214 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
217 public addAdmin(form: AddAdminForm) {
219 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
222 public getUserDetails(form: GetUserDetailsForm) {
223 this.setAuth(form, false);
224 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
227 public getReplies(form: GetRepliesForm) {
229 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
232 public getUserMentions(form: GetUserMentionsForm) {
234 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
237 public editUserMention(form: EditUserMentionForm) {
239 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
242 public getModlog(form: GetModlogForm) {
243 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
246 public createSite(siteForm: SiteForm) {
247 this.setAuth(siteForm);
248 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
251 public editSite(siteForm: SiteForm) {
252 this.setAuth(siteForm);
253 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
257 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
260 public search(form: SearchForm) {
261 this.setAuth(form, false);
262 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
265 public markAllAsRead() {
268 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
271 public saveUserSettings(userSettingsForm: UserSettingsForm) {
272 this.setAuth(userSettingsForm);
274 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
278 public deleteAccount(form: DeleteAccountForm) {
280 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
283 public passwordReset(form: PasswordResetForm) {
284 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
287 public passwordChange(form: PasswordChangeForm) {
288 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
291 public createPrivateMessage(form: PrivateMessageForm) {
294 this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
298 public editPrivateMessage(form: EditPrivateMessageForm) {
301 this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
305 public getPrivateMessages(form: GetPrivateMessagesForm) {
308 this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
312 private wsSendWrapper(op: UserOperation, data: any) {
313 let send = { op: UserOperation[op], data: data };
318 private setAuth(obj: any, throwErr: boolean = true) {
319 obj.auth = UserService.Instance.auth;
320 if (obj.auth == null && throwErr) {
321 alert(i18n.t('not_logged_in'));
322 throw 'Not logged in';
327 window.onbeforeunload = () => {
328 WebSocketService.Instance.subject.unsubscribe();
329 WebSocketService.Instance.subject = null;