1 import { wsUri } from '../env';
19 AddModToCommunityForm,
20 TransferCommunityForm,
31 } from '../interfaces';
32 import { webSocket } from 'rxjs/webSocket';
33 import { Subject } from 'rxjs';
34 import { retryWhen, delay, take } from 'rxjs/operators';
35 import { UserService } from './';
36 import { i18n } from '../i18next';
38 export class WebSocketService {
39 private static _instance: WebSocketService;
40 public subject: Subject<any>;
43 public admins: Array<UserView>;
44 public banned: Array<UserView>;
46 private constructor() {
47 this.subject = webSocket(wsUri);
49 // Necessary to not keep reconnecting
61 console.log(`Connected to ${wsUri}`);
64 public static get Instance() {
65 return this._instance || (this._instance = new this());
68 public login(loginForm: LoginForm) {
69 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
72 public register(registerForm: RegisterForm) {
73 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
76 public createCommunity(communityForm: CommunityForm) {
77 this.setAuth(communityForm);
79 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
83 public editCommunity(communityForm: CommunityForm) {
84 this.setAuth(communityForm);
86 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
90 public followCommunity(followCommunityForm: FollowCommunityForm) {
91 this.setAuth(followCommunityForm);
93 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
97 public listCommunities(form: ListCommunitiesForm) {
98 this.setAuth(form, false);
99 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
102 public getFollowedCommunities() {
103 let data = { auth: UserService.Instance.auth };
105 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
109 public listCategories() {
111 this.wsSendWrapper(UserOperation.ListCategories, undefined)
115 public createPost(postForm: PostForm) {
116 this.setAuth(postForm);
117 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
120 public getPost(postId: number) {
121 let data = { id: postId, auth: UserService.Instance.auth };
122 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
125 public getCommunity(communityId: number) {
126 let data = { id: communityId, auth: UserService.Instance.auth };
127 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
130 public getCommunityByName(name: string) {
131 let data = { name: name, auth: UserService.Instance.auth };
132 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
135 public createComment(commentForm: CommentForm) {
136 this.setAuth(commentForm);
138 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
142 public editComment(commentForm: CommentForm) {
143 this.setAuth(commentForm);
145 this.wsSendWrapper(UserOperation.EditComment, commentForm)
149 public likeComment(form: CommentLikeForm) {
152 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
156 public saveComment(form: SaveCommentForm) {
158 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
161 public getPosts(form: GetPostsForm) {
162 this.setAuth(form, false);
163 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
166 public likePost(form: CreatePostLikeForm) {
168 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
171 public editPost(postForm: PostForm) {
172 this.setAuth(postForm);
173 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
176 public savePost(form: SavePostForm) {
178 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
181 public banFromCommunity(form: BanFromCommunityForm) {
183 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
186 public addModToCommunity(form: AddModToCommunityForm) {
189 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
193 public transferCommunity(form: TransferCommunityForm) {
196 this.wsSendWrapper(UserOperation.TransferCommunity, form)
200 public transferSite(form: TransferSiteForm) {
202 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
205 public banUser(form: BanUserForm) {
207 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
210 public addAdmin(form: AddAdminForm) {
212 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
215 public getUserDetails(form: GetUserDetailsForm) {
216 this.setAuth(form, false);
217 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
220 public getReplies(form: GetRepliesForm) {
222 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
225 public getModlog(form: GetModlogForm) {
226 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
229 public createSite(siteForm: SiteForm) {
230 this.setAuth(siteForm);
231 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
234 public editSite(siteForm: SiteForm) {
235 this.setAuth(siteForm);
236 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
240 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
243 public search(form: SearchForm) {
244 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
247 public markAllAsRead() {
250 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
253 public saveUserSettings(userSettingsForm: UserSettingsForm) {
254 this.setAuth(userSettingsForm);
256 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
260 public deleteAccount(form: DeleteAccountForm) {
262 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
265 private wsSendWrapper(op: UserOperation, data: any) {
266 let send = { op: UserOperation[op], data: data };
271 private setAuth(obj: any, throwErr: boolean = true) {
272 obj.auth = UserService.Instance.auth;
273 if (obj.auth == null && throwErr) {
274 alert(i18n.t('not_logged_in'));
275 throw 'Not logged in';
280 window.onbeforeunload = () => {
281 WebSocketService.Instance.subject.unsubscribe();
282 WebSocketService.Instance.subject = null;