1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
43 } from '../interfaces';
44 import { UserService } from './';
45 import { i18n } from '../i18next';
46 import { toast } from '../utils';
47 import { Observable } from 'rxjs';
48 import { share } from 'rxjs/operators';
49 import ReconnectingWebSocket from 'reconnecting-websocket';
51 export class WebSocketService {
52 private static _instance: WebSocketService;
53 public ws: ReconnectingWebSocket;
54 public subject: Observable<any>;
57 public admins: Array<UserView>;
58 public banned: Array<UserView>;
60 private constructor() {
61 this.ws = new ReconnectingWebSocket(wsUri);
62 this.ws.onopen = () => {
63 console.log(`Connected to ${wsUri}`);
64 if (UserService.Instance.user) {
69 this.subject = Observable.create((obs: any) => {
70 this.ws.onmessage = e => {
71 obs.next(JSON.parse(e.data));
76 public static get Instance() {
77 return this._instance || (this._instance = new this());
81 let form: UserJoinForm = { auth: UserService.Instance.auth };
82 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
85 public login(loginForm: LoginForm) {
86 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
89 public register(registerForm: RegisterForm) {
90 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
93 public createCommunity(communityForm: CommunityForm) {
94 this.setAuth(communityForm);
96 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
100 public editCommunity(communityForm: CommunityForm) {
101 this.setAuth(communityForm);
103 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
107 public followCommunity(followCommunityForm: FollowCommunityForm) {
108 this.setAuth(followCommunityForm);
110 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
114 public listCommunities(form: ListCommunitiesForm) {
115 this.setAuth(form, false);
116 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
119 public getFollowedCommunities() {
120 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
122 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
126 public listCategories() {
127 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
130 public createPost(postForm: PostForm) {
131 this.setAuth(postForm);
132 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
135 public getPost(form: GetPostForm) {
136 this.setAuth(form, false);
137 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
140 public getCommunity(form: GetCommunityForm) {
141 this.setAuth(form, false);
142 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
145 public createComment(commentForm: CommentForm) {
146 this.setAuth(commentForm);
147 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
150 public editComment(commentForm: CommentForm) {
151 this.setAuth(commentForm);
152 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
155 public likeComment(form: CommentLikeForm) {
157 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
160 public saveComment(form: SaveCommentForm) {
162 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
165 public getPosts(form: GetPostsForm) {
166 this.setAuth(form, false);
167 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
170 public likePost(form: CreatePostLikeForm) {
172 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
175 public editPost(postForm: PostForm) {
176 this.setAuth(postForm);
177 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
180 public savePost(form: SavePostForm) {
182 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
185 public banFromCommunity(form: BanFromCommunityForm) {
187 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
190 public addModToCommunity(form: AddModToCommunityForm) {
192 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
195 public transferCommunity(form: TransferCommunityForm) {
197 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
200 public transferSite(form: TransferSiteForm) {
202 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
205 public banUser(form: BanUserForm) {
207 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
210 public addAdmin(form: AddAdminForm) {
212 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
215 public getUserDetails(form: GetUserDetailsForm) {
216 this.setAuth(form, false);
217 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
220 public getReplies(form: GetRepliesForm) {
222 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
225 public getUserMentions(form: GetUserMentionsForm) {
227 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
230 public editUserMention(form: EditUserMentionForm) {
232 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
235 public getModlog(form: GetModlogForm) {
236 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
239 public createSite(siteForm: SiteForm) {
240 this.setAuth(siteForm);
241 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
244 public editSite(siteForm: SiteForm) {
245 this.setAuth(siteForm);
246 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
250 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
253 public search(form: SearchForm) {
254 this.setAuth(form, false);
255 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
258 public markAllAsRead() {
261 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
264 public saveUserSettings(userSettingsForm: UserSettingsForm) {
265 this.setAuth(userSettingsForm);
267 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
271 public deleteAccount(form: DeleteAccountForm) {
273 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
276 public passwordReset(form: PasswordResetForm) {
277 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
280 public passwordChange(form: PasswordChangeForm) {
281 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
284 public createPrivateMessage(form: PrivateMessageForm) {
286 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
289 public editPrivateMessage(form: EditPrivateMessageForm) {
291 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
294 public getPrivateMessages(form: GetPrivateMessagesForm) {
296 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
299 private wsSendWrapper(op: UserOperation, data: MessageType) {
300 let send = { op: UserOperation[op], data: data };
302 return JSON.stringify(send);
305 private setAuth(obj: any, throwErr: boolean = true) {
306 obj.auth = UserService.Instance.auth;
307 if (obj.auth == null && throwErr) {
308 toast(i18n.t('not_logged_in'), 'danger');
309 throw 'Not logged in';
314 window.onbeforeunload = () => {
315 WebSocketService.Instance.ws.close();