1 import { wsUri } from '../env';
2 import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, SavePostForm, CommentForm, SaveCommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm, GetModlogForm, BanFromCommunityForm, AddModToCommunityForm, AddAdminForm, BanUserForm, SiteForm, Site, UserView, GetRepliesForm, SearchForm, UserSettingsForm } from '../interfaces';
3 import { webSocket } from 'rxjs/webSocket';
4 import { Subject } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
6 import { UserService } from './';
7 import { i18n } from '../i18next';
9 export class WebSocketService {
10 private static _instance: WebSocketService;
11 public subject: Subject<any>;
14 public admins: Array<UserView>;
15 public banned: Array<UserView>;
17 private constructor() {
18 this.subject = webSocket(wsUri);
20 // Necessary to not keep reconnecting
22 .pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
25 console.log(`Connected to ${wsUri}`);
28 public static get Instance(){
29 return this._instance || (this._instance = new this());
32 public login(loginForm: LoginForm) {
33 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
36 public register(registerForm: RegisterForm) {
37 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
40 public createCommunity(communityForm: CommunityForm) {
41 this.setAuth(communityForm);
42 this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
45 public editCommunity(communityForm: CommunityForm) {
46 this.setAuth(communityForm);
47 this.subject.next(this.wsSendWrapper(UserOperation.EditCommunity, communityForm));
50 public followCommunity(followCommunityForm: FollowCommunityForm) {
51 this.setAuth(followCommunityForm);
52 this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
55 public listCommunities(form: ListCommunitiesForm) {
56 this.setAuth(form, false);
57 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
60 public getFollowedCommunities() {
61 let data = {auth: UserService.Instance.auth };
62 this.subject.next(this.wsSendWrapper(UserOperation.GetFollowedCommunities, data));
65 public listCategories() {
66 this.subject.next(this.wsSendWrapper(UserOperation.ListCategories, undefined));
69 public createPost(postForm: PostForm) {
70 this.setAuth(postForm);
71 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
74 public getPost(postId: number) {
75 let data = {id: postId, auth: UserService.Instance.auth };
76 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
79 public getCommunity(communityId: number) {
80 let data = {id: communityId, auth: UserService.Instance.auth };
81 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
84 public getCommunityByName(name: string) {
85 let data = {name: name, auth: UserService.Instance.auth };
86 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
89 public createComment(commentForm: CommentForm) {
90 this.setAuth(commentForm);
91 this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
94 public editComment(commentForm: CommentForm) {
95 this.setAuth(commentForm);
96 this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
99 public likeComment(form: CommentLikeForm) {
101 this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
104 public saveComment(form: SaveCommentForm) {
106 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
109 public getPosts(form: GetPostsForm) {
110 this.setAuth(form, false);
111 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
114 public likePost(form: CreatePostLikeForm) {
116 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
119 public editPost(postForm: PostForm) {
120 this.setAuth(postForm);
121 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
124 public savePost(form: SavePostForm) {
126 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
129 public banFromCommunity(form: BanFromCommunityForm) {
131 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
134 public addModToCommunity(form: AddModToCommunityForm) {
136 this.subject.next(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
139 public banUser(form: BanUserForm) {
141 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
144 public addAdmin(form: AddAdminForm) {
146 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
149 public getUserDetails(form: GetUserDetailsForm) {
150 this.setAuth(form, false);
151 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
154 public getReplies(form: GetRepliesForm) {
156 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
159 public getModlog(form: GetModlogForm) {
160 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
163 public createSite(siteForm: SiteForm) {
164 this.setAuth(siteForm);
165 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
168 public editSite(siteForm: SiteForm) {
169 this.setAuth(siteForm);
170 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
174 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
177 public search(form: SearchForm) {
178 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
181 public markAllAsRead() {
184 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
187 public saveUserSettings(userSettingsForm: UserSettingsForm) {
188 this.setAuth(userSettingsForm);
189 this.subject.next(this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm));
192 private wsSendWrapper(op: UserOperation, data: any) {
193 let send = { op: UserOperation[op], data: data };
198 private setAuth(obj: any, throwErr: boolean = true) {
199 obj.auth = UserService.Instance.auth;
200 if (obj.auth == null && throwErr) {
201 alert(i18n.t('not_logged_in'));
202 throw "Not logged in";
207 window.onbeforeunload = (() => {
208 WebSocketService.Instance.subject.unsubscribe();
209 WebSocketService.Instance.subject = null;