1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
44 WebSocketJsonResponse,
45 } from '../interfaces';
46 import { UserService } from './';
47 import { i18n } from '../i18next';
48 import { toast } from '../utils';
49 import { Observable } from 'rxjs';
50 import { share } from 'rxjs/operators';
51 import ReconnectingWebSocket from 'reconnecting-websocket';
53 export class WebSocketService {
54 private static _instance: WebSocketService;
55 public ws: ReconnectingWebSocket;
56 public subject: Observable<any>;
59 public admins: Array<UserView>;
60 public banned: Array<UserView>;
62 private constructor() {
63 this.ws = new ReconnectingWebSocket(wsUri);
65 this.subject = Observable.create((obs: any) => {
66 this.ws.onmessage = e => {
67 obs.next(JSON.parse(e.data));
69 this.ws.onopen = () => {
70 console.log(`Connected to ${wsUri}`);
71 if (UserService.Instance.user) {
74 let res: WebSocketJsonResponse = {
82 public static get Instance() {
83 return this._instance || (this._instance = new this());
87 let form: UserJoinForm = { auth: UserService.Instance.auth };
88 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
91 public login(loginForm: LoginForm) {
92 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
95 public register(registerForm: RegisterForm) {
96 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
99 public createCommunity(communityForm: CommunityForm) {
100 this.setAuth(communityForm);
102 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
106 public editCommunity(communityForm: CommunityForm) {
107 this.setAuth(communityForm);
109 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
113 public followCommunity(followCommunityForm: FollowCommunityForm) {
114 this.setAuth(followCommunityForm);
116 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
120 public listCommunities(form: ListCommunitiesForm) {
121 this.setAuth(form, false);
122 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
125 public getFollowedCommunities() {
126 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
128 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
132 public listCategories() {
133 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
136 public createPost(postForm: PostForm) {
137 this.setAuth(postForm);
138 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
141 public getPost(form: GetPostForm) {
142 this.setAuth(form, false);
143 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
146 public getCommunity(form: GetCommunityForm) {
147 this.setAuth(form, false);
148 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
151 public createComment(commentForm: CommentForm) {
152 this.setAuth(commentForm);
153 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
156 public editComment(commentForm: CommentForm) {
157 this.setAuth(commentForm);
158 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
161 public likeComment(form: CommentLikeForm) {
163 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
166 public saveComment(form: SaveCommentForm) {
168 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
171 public getPosts(form: GetPostsForm) {
172 this.setAuth(form, false);
173 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
176 public getComments(form: GetCommentsForm) {
177 this.setAuth(form, false);
178 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
181 public likePost(form: CreatePostLikeForm) {
183 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
186 public editPost(postForm: PostForm) {
187 this.setAuth(postForm);
188 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
191 public savePost(form: SavePostForm) {
193 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
196 public banFromCommunity(form: BanFromCommunityForm) {
198 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
201 public addModToCommunity(form: AddModToCommunityForm) {
203 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
206 public transferCommunity(form: TransferCommunityForm) {
208 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
211 public transferSite(form: TransferSiteForm) {
213 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
216 public banUser(form: BanUserForm) {
218 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
221 public addAdmin(form: AddAdminForm) {
223 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
226 public getUserDetails(form: GetUserDetailsForm) {
227 this.setAuth(form, false);
228 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
231 public getReplies(form: GetRepliesForm) {
233 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
236 public getUserMentions(form: GetUserMentionsForm) {
238 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
241 public editUserMention(form: EditUserMentionForm) {
243 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
246 public getModlog(form: GetModlogForm) {
247 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
250 public createSite(siteForm: SiteForm) {
251 this.setAuth(siteForm);
252 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
255 public editSite(siteForm: SiteForm) {
256 this.setAuth(siteForm);
257 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
261 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
264 public search(form: SearchForm) {
265 this.setAuth(form, false);
266 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
269 public markAllAsRead() {
272 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
275 public saveUserSettings(userSettingsForm: UserSettingsForm) {
276 this.setAuth(userSettingsForm);
278 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
282 public deleteAccount(form: DeleteAccountForm) {
284 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
287 public passwordReset(form: PasswordResetForm) {
288 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
291 public passwordChange(form: PasswordChangeForm) {
292 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
295 public createPrivateMessage(form: PrivateMessageForm) {
297 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
300 public editPrivateMessage(form: EditPrivateMessageForm) {
302 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
305 public getPrivateMessages(form: GetPrivateMessagesForm) {
307 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
310 private wsSendWrapper(op: UserOperation, data: MessageType) {
311 let send = { op: UserOperation[op], data: data };
313 return JSON.stringify(send);
316 private setAuth(obj: any, throwErr: boolean = true) {
317 obj.auth = UserService.Instance.auth;
318 if (obj.auth == null && throwErr) {
319 toast(i18n.t('not_logged_in'), 'danger');
320 throw 'Not logged in';
325 window.onbeforeunload = () => {
326 WebSocketService.Instance.ws.close();