1 import { wsUri } from '../env';
19 AddModToCommunityForm,
20 TransferCommunityForm,
34 } from '../interfaces';
35 import { webSocket } from 'rxjs/webSocket';
36 import { Subject } from 'rxjs';
37 import { retryWhen, delay, take } from 'rxjs/operators';
38 import { UserService } from './';
39 import { i18n } from '../i18next';
41 export class WebSocketService {
42 private static _instance: WebSocketService;
43 public subject: Subject<any>;
46 public admins: Array<UserView>;
47 public banned: Array<UserView>;
49 private constructor() {
50 this.subject = webSocket(wsUri);
52 // Necessary to not keep reconnecting
64 console.log(`Connected to ${wsUri}`);
67 public static get Instance() {
68 return this._instance || (this._instance = new this());
71 public login(loginForm: LoginForm) {
72 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
75 public register(registerForm: RegisterForm) {
76 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
79 public createCommunity(communityForm: CommunityForm) {
80 this.setAuth(communityForm);
82 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
86 public editCommunity(communityForm: CommunityForm) {
87 this.setAuth(communityForm);
89 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
93 public followCommunity(followCommunityForm: FollowCommunityForm) {
94 this.setAuth(followCommunityForm);
96 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
100 public listCommunities(form: ListCommunitiesForm) {
101 this.setAuth(form, false);
102 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
105 public getFollowedCommunities() {
106 let data = { auth: UserService.Instance.auth };
108 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
112 public listCategories() {
114 this.wsSendWrapper(UserOperation.ListCategories, undefined)
118 public createPost(postForm: PostForm) {
119 this.setAuth(postForm);
120 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
123 public getPost(postId: number) {
124 let data = { id: postId, auth: UserService.Instance.auth };
125 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
128 public getCommunity(communityId: number) {
129 let data = { id: communityId, auth: UserService.Instance.auth };
130 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
133 public getCommunityByName(name: string) {
134 let data = { name: name, auth: UserService.Instance.auth };
135 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
138 public createComment(commentForm: CommentForm) {
139 this.setAuth(commentForm);
141 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
145 public editComment(commentForm: CommentForm) {
146 this.setAuth(commentForm);
148 this.wsSendWrapper(UserOperation.EditComment, commentForm)
152 public likeComment(form: CommentLikeForm) {
155 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
159 public saveComment(form: SaveCommentForm) {
161 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
164 public getPosts(form: GetPostsForm) {
165 this.setAuth(form, false);
166 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
169 public likePost(form: CreatePostLikeForm) {
171 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
174 public editPost(postForm: PostForm) {
175 this.setAuth(postForm);
176 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
179 public savePost(form: SavePostForm) {
181 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
184 public banFromCommunity(form: BanFromCommunityForm) {
186 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
189 public addModToCommunity(form: AddModToCommunityForm) {
192 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
196 public transferCommunity(form: TransferCommunityForm) {
199 this.wsSendWrapper(UserOperation.TransferCommunity, form)
203 public transferSite(form: TransferSiteForm) {
205 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
208 public banUser(form: BanUserForm) {
210 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
213 public addAdmin(form: AddAdminForm) {
215 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
218 public getUserDetails(form: GetUserDetailsForm) {
219 this.setAuth(form, false);
220 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
223 public getReplies(form: GetRepliesForm) {
225 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
228 public getUserMentions(form: GetUserMentionsForm) {
230 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
233 public editUserMention(form: EditUserMentionForm) {
235 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
238 public getModlog(form: GetModlogForm) {
239 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
242 public createSite(siteForm: SiteForm) {
243 this.setAuth(siteForm);
244 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
247 public editSite(siteForm: SiteForm) {
248 this.setAuth(siteForm);
249 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
253 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
256 public search(form: SearchForm) {
257 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
260 public markAllAsRead() {
263 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
266 public saveUserSettings(userSettingsForm: UserSettingsForm) {
267 this.setAuth(userSettingsForm);
269 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
273 public deleteAccount(form: DeleteAccountForm) {
275 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
278 public passwordReset(form: PasswordResetForm) {
279 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
282 private wsSendWrapper(op: UserOperation, data: any) {
283 let send = { op: UserOperation[op], data: data };
288 private setAuth(obj: any, throwErr: boolean = true) {
289 obj.auth = UserService.Instance.auth;
290 if (obj.auth == null && throwErr) {
291 alert(i18n.t('not_logged_in'));
292 throw 'Not logged in';
297 window.onbeforeunload = () => {
298 WebSocketService.Instance.subject.unsubscribe();
299 WebSocketService.Instance.subject = null;