1 import { wsUri } from '../env';
18 MarkCommentAsReadForm,
26 GetFollowedCommunitiesForm,
31 AddModToCommunityForm,
32 TransferCommunityForm,
40 MarkUserMentionAsReadForm,
47 EditPrivateMessageForm,
48 DeletePrivateMessageForm,
49 MarkPrivateMessageAsReadForm,
50 GetPrivateMessagesForm,
57 WebSocketJsonResponse,
58 } from 'lemmy-js-client';
59 import { UserService } from './';
60 import { i18n } from '../i18next';
61 import { toast } from '../utils';
62 import { Observable } from 'rxjs';
63 import { share } from 'rxjs/operators';
64 import ReconnectingWebSocket from 'reconnecting-websocket';
66 export class WebSocketService {
67 private static _instance: WebSocketService;
68 public ws: ReconnectingWebSocket;
69 public subject: Observable<any>;
71 public admins: Array<UserView>;
72 public banned: Array<UserView>;
73 private client = new LemmyWebsocket();
75 private constructor() {
76 this.ws = new ReconnectingWebSocket(wsUri);
77 let firstConnect = true;
79 this.subject = Observable.create((obs: any) => {
80 this.ws.onmessage = e => {
81 obs.next(JSON.parse(e.data));
83 this.ws.onopen = () => {
84 console.log(`Connected to ${wsUri}`);
87 let res: WebSocketJsonResponse = {
98 public static get Instance() {
99 return this._instance || (this._instance = new this());
103 let form: UserJoinForm = { auth: UserService.Instance.auth };
104 this.ws.send(this.client.userJoin(form));
107 public login(form: LoginForm) {
108 this.ws.send(this.client.login(form));
111 public register(form: RegisterForm) {
112 this.ws.send(this.client.register(form));
115 public getCaptcha() {
116 this.ws.send(this.client.getCaptcha());
119 public createCommunity(form: CommunityForm) {
120 this.setAuth(form); // TODO all these setauths at some point would be good to make required
121 this.ws.send(this.client.createCommunity(form));
124 public editCommunity(form: CommunityForm) {
126 this.ws.send(this.client.editCommunity(form));
129 public deleteCommunity(form: DeleteCommunityForm) {
131 this.ws.send(this.client.deleteCommunity(form));
134 public removeCommunity(form: RemoveCommunityForm) {
136 this.ws.send(this.client.removeCommunity(form));
139 public followCommunity(form: FollowCommunityForm) {
141 this.ws.send(this.client.followCommunity(form));
144 public listCommunities(form: ListCommunitiesForm) {
145 this.setAuth(form, false);
146 this.ws.send(this.client.listCommunities(form));
149 public getFollowedCommunities() {
150 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
151 this.ws.send(this.client.getFollowedCommunities(form));
154 public listCategories() {
155 this.ws.send(this.client.listCategories());
158 public createPost(form: PostForm) {
160 this.ws.send(this.client.createPost(form));
163 public getPost(form: GetPostForm) {
164 this.setAuth(form, false);
165 this.ws.send(this.client.getPost(form));
168 public getCommunity(form: GetCommunityForm) {
169 this.setAuth(form, false);
170 this.ws.send(this.client.getCommunity(form));
173 public createComment(form: CommentForm) {
175 this.ws.send(this.client.createComment(form));
178 public editComment(form: CommentForm) {
180 this.ws.send(this.client.editComment(form));
183 public deleteComment(form: DeleteCommentForm) {
185 this.ws.send(this.client.deleteComment(form));
188 public removeComment(form: RemoveCommentForm) {
190 this.ws.send(this.client.removeComment(form));
193 public markCommentAsRead(form: MarkCommentAsReadForm) {
195 this.ws.send(this.client.markCommentAsRead(form));
198 public likeComment(form: CommentLikeForm) {
200 this.ws.send(this.client.likeComment(form));
203 public saveComment(form: SaveCommentForm) {
205 this.ws.send(this.client.saveComment(form));
208 public getPosts(form: GetPostsForm) {
209 this.setAuth(form, false);
210 this.ws.send(this.client.getPosts(form));
213 public getComments(form: GetCommentsForm) {
214 this.setAuth(form, false);
215 this.ws.send(this.client.getComments(form));
218 public likePost(form: CreatePostLikeForm) {
220 this.ws.send(this.client.likePost(form));
223 public editPost(form: PostForm) {
225 this.ws.send(this.client.editPost(form));
228 public deletePost(form: DeletePostForm) {
230 this.ws.send(this.client.deletePost(form));
233 public removePost(form: RemovePostForm) {
235 this.ws.send(this.client.removePost(form));
238 public lockPost(form: LockPostForm) {
240 this.ws.send(this.client.lockPost(form));
243 public stickyPost(form: StickyPostForm) {
245 this.ws.send(this.client.stickyPost(form));
248 public savePost(form: SavePostForm) {
250 this.ws.send(this.client.savePost(form));
253 public banFromCommunity(form: BanFromCommunityForm) {
255 this.ws.send(this.client.banFromCommunity(form));
258 public addModToCommunity(form: AddModToCommunityForm) {
260 this.ws.send(this.client.addModToCommunity(form));
263 public transferCommunity(form: TransferCommunityForm) {
265 this.ws.send(this.client.transferCommunity(form));
268 public transferSite(form: TransferSiteForm) {
270 this.ws.send(this.client.transferSite(form));
273 public banUser(form: BanUserForm) {
275 this.ws.send(this.client.banUser(form));
278 public addAdmin(form: AddAdminForm) {
280 this.ws.send(this.client.addAdmin(form));
283 public getUserDetails(form: GetUserDetailsForm) {
284 this.setAuth(form, false);
285 this.ws.send(this.client.getUserDetails(form));
288 public getReplies(form: GetRepliesForm) {
290 this.ws.send(this.client.getReplies(form));
293 public getUserMentions(form: GetUserMentionsForm) {
295 this.ws.send(this.client.getUserMentions(form));
298 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
300 this.ws.send(this.client.markUserMentionAsRead(form));
303 public getModlog(form: GetModlogForm) {
304 this.ws.send(this.client.getModlog(form));
307 public createSite(form: SiteForm) {
309 this.ws.send(this.client.createSite(form));
312 public editSite(form: SiteForm) {
314 this.ws.send(this.client.editSite(form));
317 public getSite(form: GetSiteForm = {}) {
318 this.setAuth(form, false);
319 this.ws.send(this.client.getSite(form));
322 public getSiteConfig() {
323 let form: GetSiteConfig = {};
325 this.ws.send(this.client.getSiteConfig(form));
328 public search(form: SearchForm) {
329 this.setAuth(form, false);
330 this.ws.send(this.client.search(form));
333 public markAllAsRead() {
334 let form: MarkAllAsReadForm;
336 this.ws.send(this.client.markAllAsRead(form));
339 public saveUserSettings(form: UserSettingsForm) {
341 this.ws.send(this.client.saveUserSettings(form));
344 public deleteAccount(form: DeleteAccountForm) {
346 this.ws.send(this.client.deleteAccount(form));
349 public passwordReset(form: PasswordResetForm) {
350 this.ws.send(this.client.passwordReset(form));
353 public passwordChange(form: PasswordChangeForm) {
354 this.ws.send(this.client.passwordChange(form));
357 public createPrivateMessage(form: PrivateMessageForm) {
359 this.ws.send(this.client.createPrivateMessage(form));
362 public editPrivateMessage(form: EditPrivateMessageForm) {
364 this.ws.send(this.client.editPrivateMessage(form));
367 public deletePrivateMessage(form: DeletePrivateMessageForm) {
369 this.ws.send(this.client.deletePrivateMessage(form));
372 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
374 this.ws.send(this.client.markPrivateMessageAsRead(form));
377 public getPrivateMessages(form: GetPrivateMessagesForm) {
379 this.ws.send(this.client.getPrivateMessages(form));
382 public saveSiteConfig(form: SiteConfigForm) {
384 this.ws.send(this.client.saveSiteConfig(form));
387 private setAuth(obj: any, throwErr: boolean = true) {
388 obj.auth = UserService.Instance.auth;
389 if (obj.auth == null && throwErr) {
390 toast(i18n.t('not_logged_in'), 'danger');
391 throw 'Not logged in';
396 window.onbeforeunload = () => {
397 WebSocketService.Instance.ws.close();