1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
42 } from '../interfaces';
43 import { UserService } from './';
44 import { i18n } from '../i18next';
45 import { toast } from '../utils';
46 import { Observable } from 'rxjs';
47 import { share } from 'rxjs/operators';
48 import ReconnectingWebSocket from 'reconnecting-websocket';
50 export class WebSocketService {
51 private static _instance: WebSocketService;
52 public ws: ReconnectingWebSocket;
53 public subject: Observable<any>;
56 public admins: Array<UserView>;
57 public banned: Array<UserView>;
59 private constructor() {
60 this.ws = new ReconnectingWebSocket(wsUri);
61 this.ws.onopen = () => {
62 console.log(`Connected to ${wsUri}`);
65 this.subject = Observable.create((obs: any) => {
66 this.ws.onmessage = e => {
67 obs.next(JSON.parse(e.data));
72 public static get Instance() {
73 return this._instance || (this._instance = new this());
76 public login(loginForm: LoginForm) {
77 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
80 public register(registerForm: RegisterForm) {
81 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
84 public createCommunity(communityForm: CommunityForm) {
85 this.setAuth(communityForm);
87 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
91 public editCommunity(communityForm: CommunityForm) {
92 this.setAuth(communityForm);
94 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
98 public followCommunity(followCommunityForm: FollowCommunityForm) {
99 this.setAuth(followCommunityForm);
101 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
105 public listCommunities(form: ListCommunitiesForm) {
106 this.setAuth(form, false);
107 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
110 public getFollowedCommunities() {
111 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
113 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
117 public listCategories() {
118 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
121 public createPost(postForm: PostForm) {
122 this.setAuth(postForm);
123 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
126 public getPost(form: GetPostForm) {
127 this.setAuth(form, false);
128 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
131 public getCommunity(form: GetCommunityForm) {
132 this.setAuth(form, false);
133 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
136 public createComment(commentForm: CommentForm) {
137 this.setAuth(commentForm);
138 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
141 public editComment(commentForm: CommentForm) {
142 this.setAuth(commentForm);
143 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
146 public likeComment(form: CommentLikeForm) {
148 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
151 public saveComment(form: SaveCommentForm) {
153 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
156 public getPosts(form: GetPostsForm) {
157 this.setAuth(form, false);
158 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
161 public likePost(form: CreatePostLikeForm) {
163 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
166 public editPost(postForm: PostForm) {
167 this.setAuth(postForm);
168 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
171 public savePost(form: SavePostForm) {
173 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
176 public banFromCommunity(form: BanFromCommunityForm) {
178 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
181 public addModToCommunity(form: AddModToCommunityForm) {
183 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
186 public transferCommunity(form: TransferCommunityForm) {
188 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
191 public transferSite(form: TransferSiteForm) {
193 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
196 public banUser(form: BanUserForm) {
198 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
201 public addAdmin(form: AddAdminForm) {
203 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
206 public getUserDetails(form: GetUserDetailsForm) {
207 this.setAuth(form, false);
208 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
211 public getReplies(form: GetRepliesForm) {
213 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
216 public getUserMentions(form: GetUserMentionsForm) {
218 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
221 public editUserMention(form: EditUserMentionForm) {
223 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
226 public getModlog(form: GetModlogForm) {
227 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
230 public createSite(siteForm: SiteForm) {
231 this.setAuth(siteForm);
232 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
235 public editSite(siteForm: SiteForm) {
236 this.setAuth(siteForm);
237 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
241 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
244 public search(form: SearchForm) {
245 this.setAuth(form, false);
246 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
249 public markAllAsRead() {
252 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
255 public saveUserSettings(userSettingsForm: UserSettingsForm) {
256 this.setAuth(userSettingsForm);
258 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
262 public deleteAccount(form: DeleteAccountForm) {
264 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
267 public passwordReset(form: PasswordResetForm) {
268 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
271 public passwordChange(form: PasswordChangeForm) {
272 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
275 public createPrivateMessage(form: PrivateMessageForm) {
277 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
280 public editPrivateMessage(form: EditPrivateMessageForm) {
282 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
285 public getPrivateMessages(form: GetPrivateMessagesForm) {
287 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
290 private wsSendWrapper(op: UserOperation, data: MessageType) {
291 let send = { op: UserOperation[op], data: data };
293 return JSON.stringify(send);
296 private setAuth(obj: any, throwErr: boolean = true) {
297 obj.auth = UserService.Instance.auth;
298 if (obj.auth == null && throwErr) {
299 toast(i18n.t('not_logged_in'), 'danger');
300 throw 'Not logged in';
305 window.onbeforeunload = () => {
306 WebSocketService.Instance.ws.close();