1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
43 WebSocketJsonResponse,
44 } from '../interfaces';
45 import { UserService } from './';
46 import { i18n } from '../i18next';
47 import { toast } from '../utils';
48 import { Observable } from 'rxjs';
49 import { share } from 'rxjs/operators';
50 import ReconnectingWebSocket from 'reconnecting-websocket';
52 export class WebSocketService {
53 private static _instance: WebSocketService;
54 public ws: ReconnectingWebSocket;
55 public subject: Observable<any>;
58 public admins: Array<UserView>;
59 public banned: Array<UserView>;
61 private constructor() {
62 this.ws = new ReconnectingWebSocket(wsUri);
64 this.subject = Observable.create((obs: any) => {
65 this.ws.onmessage = e => {
66 obs.next(JSON.parse(e.data));
68 this.ws.onopen = () => {
69 console.log(`Connected to ${wsUri}`);
70 if (UserService.Instance.user) {
73 let res: WebSocketJsonResponse = {
81 public static get Instance() {
82 return this._instance || (this._instance = new this());
86 let form: UserJoinForm = { auth: UserService.Instance.auth };
87 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
90 public login(loginForm: LoginForm) {
91 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
94 public register(registerForm: RegisterForm) {
95 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
98 public createCommunity(communityForm: CommunityForm) {
99 this.setAuth(communityForm);
101 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
105 public editCommunity(communityForm: CommunityForm) {
106 this.setAuth(communityForm);
108 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
112 public followCommunity(followCommunityForm: FollowCommunityForm) {
113 this.setAuth(followCommunityForm);
115 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
119 public listCommunities(form: ListCommunitiesForm) {
120 this.setAuth(form, false);
121 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
124 public getFollowedCommunities() {
125 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
127 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
131 public listCategories() {
132 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
135 public createPost(postForm: PostForm) {
136 this.setAuth(postForm);
137 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
140 public getPost(form: GetPostForm) {
141 this.setAuth(form, false);
142 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
145 public getCommunity(form: GetCommunityForm) {
146 this.setAuth(form, false);
147 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
150 public createComment(commentForm: CommentForm) {
151 this.setAuth(commentForm);
152 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
155 public editComment(commentForm: CommentForm) {
156 this.setAuth(commentForm);
157 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
160 public likeComment(form: CommentLikeForm) {
162 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
165 public saveComment(form: SaveCommentForm) {
167 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
170 public getPosts(form: GetPostsForm) {
171 this.setAuth(form, false);
172 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
175 public likePost(form: CreatePostLikeForm) {
177 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
180 public editPost(postForm: PostForm) {
181 this.setAuth(postForm);
182 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
185 public savePost(form: SavePostForm) {
187 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
190 public banFromCommunity(form: BanFromCommunityForm) {
192 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
195 public addModToCommunity(form: AddModToCommunityForm) {
197 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
200 public transferCommunity(form: TransferCommunityForm) {
202 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
205 public transferSite(form: TransferSiteForm) {
207 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
210 public banUser(form: BanUserForm) {
212 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
215 public addAdmin(form: AddAdminForm) {
217 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
220 public getUserDetails(form: GetUserDetailsForm) {
221 this.setAuth(form, false);
222 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
225 public getReplies(form: GetRepliesForm) {
227 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
230 public getUserMentions(form: GetUserMentionsForm) {
232 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
235 public editUserMention(form: EditUserMentionForm) {
237 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
240 public getModlog(form: GetModlogForm) {
241 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
244 public createSite(siteForm: SiteForm) {
245 this.setAuth(siteForm);
246 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
249 public editSite(siteForm: SiteForm) {
250 this.setAuth(siteForm);
251 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
255 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
258 public search(form: SearchForm) {
259 this.setAuth(form, false);
260 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
263 public markAllAsRead() {
266 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
269 public saveUserSettings(userSettingsForm: UserSettingsForm) {
270 this.setAuth(userSettingsForm);
272 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
276 public deleteAccount(form: DeleteAccountForm) {
278 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
281 public passwordReset(form: PasswordResetForm) {
282 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
285 public passwordChange(form: PasswordChangeForm) {
286 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
289 public createPrivateMessage(form: PrivateMessageForm) {
291 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
294 public editPrivateMessage(form: EditPrivateMessageForm) {
296 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
299 public getPrivateMessages(form: GetPrivateMessagesForm) {
301 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
304 private wsSendWrapper(op: UserOperation, data: MessageType) {
305 let send = { op: UserOperation[op], data: data };
307 return JSON.stringify(send);
310 private setAuth(obj: any, throwErr: boolean = true) {
311 obj.auth = UserService.Instance.auth;
312 if (obj.auth == null && throwErr) {
313 toast(i18n.t('not_logged_in'), 'danger');
314 throw 'Not logged in';
319 window.onbeforeunload = () => {
320 WebSocketService.Instance.ws.close();