1 import { wsUri } from '../env';
19 AddModToCommunityForm,
20 TransferCommunityForm,
33 } from '../interfaces';
34 import { webSocket } from 'rxjs/webSocket';
35 import { Subject } from 'rxjs';
36 import { retryWhen, delay, take } from 'rxjs/operators';
37 import { UserService } from './';
38 import { i18n } from '../i18next';
40 export class WebSocketService {
41 private static _instance: WebSocketService;
42 public subject: Subject<any>;
45 public admins: Array<UserView>;
46 public banned: Array<UserView>;
48 private constructor() {
49 this.subject = webSocket(wsUri);
51 // Necessary to not keep reconnecting
63 console.log(`Connected to ${wsUri}`);
66 public static get Instance() {
67 return this._instance || (this._instance = new this());
70 public login(loginForm: LoginForm) {
71 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
74 public register(registerForm: RegisterForm) {
75 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
78 public createCommunity(communityForm: CommunityForm) {
79 this.setAuth(communityForm);
81 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
85 public editCommunity(communityForm: CommunityForm) {
86 this.setAuth(communityForm);
88 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
92 public followCommunity(followCommunityForm: FollowCommunityForm) {
93 this.setAuth(followCommunityForm);
95 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
99 public listCommunities(form: ListCommunitiesForm) {
100 this.setAuth(form, false);
101 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
104 public getFollowedCommunities() {
105 let data = { auth: UserService.Instance.auth };
107 this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
111 public listCategories() {
113 this.wsSendWrapper(UserOperation.ListCategories, undefined)
117 public createPost(postForm: PostForm) {
118 this.setAuth(postForm);
119 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
122 public getPost(postId: number) {
123 let data = { id: postId, auth: UserService.Instance.auth };
124 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
127 public getCommunity(communityId: number) {
128 let data = { id: communityId, auth: UserService.Instance.auth };
129 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
132 public getCommunityByName(name: string) {
133 let data = { name: name, auth: UserService.Instance.auth };
134 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
137 public createComment(commentForm: CommentForm) {
138 this.setAuth(commentForm);
140 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
144 public editComment(commentForm: CommentForm) {
145 this.setAuth(commentForm);
147 this.wsSendWrapper(UserOperation.EditComment, commentForm)
151 public likeComment(form: CommentLikeForm) {
154 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
158 public saveComment(form: SaveCommentForm) {
160 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
163 public getPosts(form: GetPostsForm) {
164 this.setAuth(form, false);
165 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
168 public likePost(form: CreatePostLikeForm) {
170 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
173 public editPost(postForm: PostForm) {
174 this.setAuth(postForm);
175 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
178 public savePost(form: SavePostForm) {
180 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
183 public banFromCommunity(form: BanFromCommunityForm) {
185 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
188 public addModToCommunity(form: AddModToCommunityForm) {
191 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
195 public transferCommunity(form: TransferCommunityForm) {
198 this.wsSendWrapper(UserOperation.TransferCommunity, form)
202 public transferSite(form: TransferSiteForm) {
204 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
207 public banUser(form: BanUserForm) {
209 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
212 public addAdmin(form: AddAdminForm) {
214 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
217 public getUserDetails(form: GetUserDetailsForm) {
218 this.setAuth(form, false);
219 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
222 public getReplies(form: GetRepliesForm) {
224 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
227 public getUserMentions(form: GetUserMentionsForm) {
229 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
232 public editUserMention(form: EditUserMentionForm) {
234 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
237 public getModlog(form: GetModlogForm) {
238 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
241 public createSite(siteForm: SiteForm) {
242 this.setAuth(siteForm);
243 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
246 public editSite(siteForm: SiteForm) {
247 this.setAuth(siteForm);
248 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
252 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
255 public search(form: SearchForm) {
256 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
259 public markAllAsRead() {
262 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
265 public saveUserSettings(userSettingsForm: UserSettingsForm) {
266 this.setAuth(userSettingsForm);
268 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
272 public deleteAccount(form: DeleteAccountForm) {
274 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
277 private wsSendWrapper(op: UserOperation, data: any) {
278 let send = { op: UserOperation[op], data: data };
283 private setAuth(obj: any, throwErr: boolean = true) {
284 obj.auth = UserService.Instance.auth;
285 if (obj.auth == null && throwErr) {
286 alert(i18n.t('not_logged_in'));
287 throw 'Not logged in';
292 window.onbeforeunload = () => {
293 WebSocketService.Instance.subject.unsubscribe();
294 WebSocketService.Instance.subject = null;