1 import { wsUri } from '../env';
19 GetFollowedCommunitiesForm,
24 AddModToCommunityForm,
25 TransferCommunityForm,
33 MarkUserMentionAsReadForm,
40 EditPrivateMessageForm,
41 DeletePrivateMessageForm,
42 MarkPrivateMessageAsReadForm,
43 GetPrivateMessagesForm,
49 WebSocketJsonResponse,
50 } from '../interfaces';
51 import { UserService } from './';
52 import { i18n } from '../i18next';
53 import { toast } from '../utils';
54 import { Observable } from 'rxjs';
55 import { share } from 'rxjs/operators';
56 import ReconnectingWebSocket from 'reconnecting-websocket';
58 export class WebSocketService {
59 private static _instance: WebSocketService;
60 public ws: ReconnectingWebSocket;
61 public subject: Observable<any>;
63 public admins: Array<UserView>;
64 public banned: Array<UserView>;
66 private constructor() {
67 this.ws = new ReconnectingWebSocket(wsUri);
68 let firstConnect = true;
70 this.subject = Observable.create((obs: any) => {
71 this.ws.onmessage = e => {
72 obs.next(JSON.parse(e.data));
74 this.ws.onopen = () => {
75 console.log(`Connected to ${wsUri}`);
77 if (UserService.Instance.user) {
82 let res: WebSocketJsonResponse = {
93 public static get Instance() {
94 return this._instance || (this._instance = new this());
98 let form: UserJoinForm = { auth: UserService.Instance.auth };
99 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
102 public login(loginForm: LoginForm) {
103 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
106 public register(registerForm: RegisterForm) {
107 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
110 public createCommunity(form: CommunityForm) {
112 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
115 public editCommunity(form: CommunityForm) {
117 this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
120 public deleteCommunity(form: DeleteCommunityForm) {
122 this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
125 public removeCommunity(form: RemoveCommunityForm) {
127 this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
130 public followCommunity(followCommunityForm: FollowCommunityForm) {
131 this.setAuth(followCommunityForm);
133 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
137 public listCommunities(form: ListCommunitiesForm) {
138 this.setAuth(form, false);
139 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
142 public getFollowedCommunities() {
143 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
145 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
149 public listCategories() {
150 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
153 public createPost(postForm: PostForm) {
154 this.setAuth(postForm);
155 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
158 public getPost(form: GetPostForm) {
159 this.setAuth(form, false);
160 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
163 public getCommunity(form: GetCommunityForm) {
164 this.setAuth(form, false);
165 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
168 public createComment(commentForm: CommentForm) {
169 this.setAuth(commentForm);
170 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
173 public editComment(commentForm: CommentForm) {
174 this.setAuth(commentForm);
175 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
178 public likeComment(form: CommentLikeForm) {
180 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
183 public saveComment(form: SaveCommentForm) {
185 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
188 public getPosts(form: GetPostsForm) {
189 this.setAuth(form, false);
190 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
193 public getComments(form: GetCommentsForm) {
194 this.setAuth(form, false);
195 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
198 public likePost(form: CreatePostLikeForm) {
200 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
203 public editPost(postForm: PostForm) {
204 this.setAuth(postForm);
205 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
208 public savePost(form: SavePostForm) {
210 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
213 public banFromCommunity(form: BanFromCommunityForm) {
215 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
218 public addModToCommunity(form: AddModToCommunityForm) {
220 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
223 public transferCommunity(form: TransferCommunityForm) {
225 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
228 public transferSite(form: TransferSiteForm) {
230 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
233 public banUser(form: BanUserForm) {
235 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
238 public addAdmin(form: AddAdminForm) {
240 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
243 public getUserDetails(form: GetUserDetailsForm) {
244 this.setAuth(form, false);
245 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
248 public getReplies(form: GetRepliesForm) {
250 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
253 public getUserMentions(form: GetUserMentionsForm) {
255 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
258 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
260 this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
263 public getModlog(form: GetModlogForm) {
264 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
267 public createSite(siteForm: SiteForm) {
268 this.setAuth(siteForm);
269 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
272 public editSite(siteForm: SiteForm) {
273 this.setAuth(siteForm);
274 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
278 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
281 public getSiteConfig() {
282 let siteConfig: GetSiteConfig = {};
283 this.setAuth(siteConfig);
284 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
287 public search(form: SearchForm) {
288 this.setAuth(form, false);
289 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
292 public markAllAsRead() {
295 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
298 public saveUserSettings(userSettingsForm: UserSettingsForm) {
299 this.setAuth(userSettingsForm);
301 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
305 public deleteAccount(form: DeleteAccountForm) {
307 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
310 public passwordReset(form: PasswordResetForm) {
311 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
314 public passwordChange(form: PasswordChangeForm) {
315 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
318 public createPrivateMessage(form: PrivateMessageForm) {
320 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
323 public editPrivateMessage(form: EditPrivateMessageForm) {
325 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
328 public deletePrivateMessage(form: DeletePrivateMessageForm) {
330 this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
333 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
336 this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
340 public getPrivateMessages(form: GetPrivateMessagesForm) {
342 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
345 public saveSiteConfig(form: SiteConfigForm) {
347 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
350 private wsSendWrapper(op: UserOperation, data: MessageType) {
351 let send = { op: UserOperation[op], data: data };
353 return JSON.stringify(send);
356 private setAuth(obj: any, throwErr: boolean = true) {
357 obj.auth = UserService.Instance.auth;
358 if (obj.auth == null && throwErr) {
359 toast(i18n.t('not_logged_in'), 'danger');
360 throw 'Not logged in';
365 window.onbeforeunload = () => {
366 WebSocketService.Instance.ws.close();