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,
60 } from 'lemmy-js-client';
61 import { UserService } from './';
62 import { i18n } from '../i18next';
63 import { toast, isBrowser } from '../utils';
64 import { Observable } from 'rxjs';
65 import { share } from 'rxjs/operators';
68 default as ReconnectingWebSocket,
69 } from 'reconnecting-websocket';
71 export class WebSocketService {
72 private static _instance: WebSocketService;
73 public ws: ReconnectingWebSocket;
74 public wsOptions: WSOptions = {
75 connectionTimeout: 5000,
78 public subject: Observable<any>;
80 public admins: UserView[];
81 public banned: UserView[];
82 private client = new LemmyWebsocket();
84 private constructor() {
85 this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
86 let firstConnect = true;
88 this.subject = new Observable((obs: any) => {
89 this.ws.onmessage = e => {
90 obs.next(JSON.parse(e.data));
92 this.ws.onopen = () => {
93 console.log(`Connected to ${wsUri}`);
96 let res: WebSocketJsonResponse = {
102 firstConnect = false;
107 public static get Instance() {
108 return this._instance || (this._instance = new this());
112 let form: UserJoinForm = { auth: UserService.Instance.auth };
113 this.ws.send(this.client.userJoin(form));
116 public postJoin(form: PostJoinForm) {
117 this.ws.send(this.client.postJoin(form));
120 public communityJoin(form: CommunityJoinForm) {
121 this.ws.send(this.client.communityJoin(form));
124 public login(form: LoginForm) {
125 this.ws.send(this.client.login(form));
128 public register(form: RegisterForm) {
129 this.ws.send(this.client.register(form));
132 public getCaptcha() {
133 this.ws.send(this.client.getCaptcha());
136 public createCommunity(form: CommunityForm) {
137 this.setAuth(form); // TODO all these setauths at some point would be good to make required
138 this.ws.send(this.client.createCommunity(form));
141 public editCommunity(form: CommunityForm) {
143 this.ws.send(this.client.editCommunity(form));
146 public deleteCommunity(form: DeleteCommunityForm) {
148 this.ws.send(this.client.deleteCommunity(form));
151 public removeCommunity(form: RemoveCommunityForm) {
153 this.ws.send(this.client.removeCommunity(form));
156 public followCommunity(form: FollowCommunityForm) {
158 this.ws.send(this.client.followCommunity(form));
161 public listCommunities(form: ListCommunitiesForm) {
162 this.setAuth(form, false);
163 this.ws.send(this.client.listCommunities(form));
166 public getFollowedCommunities() {
167 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
168 this.ws.send(this.client.getFollowedCommunities(form));
171 public listCategories() {
172 this.ws.send(this.client.listCategories());
175 public createPost(form: PostForm) {
177 this.ws.send(this.client.createPost(form));
180 public getPost(form: GetPostForm) {
181 this.setAuth(form, false);
182 this.ws.send(this.client.getPost(form));
185 public getCommunity(form: GetCommunityForm) {
186 this.setAuth(form, false);
187 this.ws.send(this.client.getCommunity(form));
190 public createComment(form: CommentForm) {
192 this.ws.send(this.client.createComment(form));
195 public editComment(form: CommentForm) {
197 this.ws.send(this.client.editComment(form));
200 public deleteComment(form: DeleteCommentForm) {
202 this.ws.send(this.client.deleteComment(form));
205 public removeComment(form: RemoveCommentForm) {
207 this.ws.send(this.client.removeComment(form));
210 public markCommentAsRead(form: MarkCommentAsReadForm) {
212 this.ws.send(this.client.markCommentAsRead(form));
215 public likeComment(form: CommentLikeForm) {
217 this.ws.send(this.client.likeComment(form));
220 public saveComment(form: SaveCommentForm) {
222 this.ws.send(this.client.saveComment(form));
225 public getPosts(form: GetPostsForm) {
226 this.setAuth(form, false);
227 this.ws.send(this.client.getPosts(form));
230 public getComments(form: GetCommentsForm) {
231 this.setAuth(form, false);
232 this.ws.send(this.client.getComments(form));
235 public likePost(form: CreatePostLikeForm) {
237 this.ws.send(this.client.likePost(form));
240 public editPost(form: PostForm) {
242 this.ws.send(this.client.editPost(form));
245 public deletePost(form: DeletePostForm) {
247 this.ws.send(this.client.deletePost(form));
250 public removePost(form: RemovePostForm) {
252 this.ws.send(this.client.removePost(form));
255 public lockPost(form: LockPostForm) {
257 this.ws.send(this.client.lockPost(form));
260 public stickyPost(form: StickyPostForm) {
262 this.ws.send(this.client.stickyPost(form));
265 public savePost(form: SavePostForm) {
267 this.ws.send(this.client.savePost(form));
270 public banFromCommunity(form: BanFromCommunityForm) {
272 this.ws.send(this.client.banFromCommunity(form));
275 public addModToCommunity(form: AddModToCommunityForm) {
277 this.ws.send(this.client.addModToCommunity(form));
280 public transferCommunity(form: TransferCommunityForm) {
282 this.ws.send(this.client.transferCommunity(form));
285 public transferSite(form: TransferSiteForm) {
287 this.ws.send(this.client.transferSite(form));
290 public banUser(form: BanUserForm) {
292 this.ws.send(this.client.banUser(form));
295 public addAdmin(form: AddAdminForm) {
297 this.ws.send(this.client.addAdmin(form));
300 public getUserDetails(form: GetUserDetailsForm) {
301 this.setAuth(form, false);
302 this.ws.send(this.client.getUserDetails(form));
305 public getReplies(form: GetRepliesForm) {
307 this.ws.send(this.client.getReplies(form));
310 public getUserMentions(form: GetUserMentionsForm) {
312 this.ws.send(this.client.getUserMentions(form));
315 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
317 this.ws.send(this.client.markUserMentionAsRead(form));
320 public getModlog(form: GetModlogForm) {
321 this.ws.send(this.client.getModlog(form));
324 public createSite(form: SiteForm) {
326 this.ws.send(this.client.createSite(form));
329 public editSite(form: SiteForm) {
331 this.ws.send(this.client.editSite(form));
334 public getSite(form: GetSiteForm = {}) {
335 this.setAuth(form, false);
336 this.ws.send(this.client.getSite(form));
339 public getSiteConfig() {
340 let form: GetSiteConfig = {};
342 this.ws.send(this.client.getSiteConfig(form));
345 public search(form: SearchForm) {
346 this.setAuth(form, false);
347 this.ws.send(this.client.search(form));
350 public markAllAsRead() {
351 let form: MarkAllAsReadForm = { auth: null };
353 this.ws.send(this.client.markAllAsRead(form));
356 public saveUserSettings(form: UserSettingsForm) {
358 this.ws.send(this.client.saveUserSettings(form));
361 public deleteAccount(form: DeleteAccountForm) {
363 this.ws.send(this.client.deleteAccount(form));
366 public passwordReset(form: PasswordResetForm) {
367 this.ws.send(this.client.passwordReset(form));
370 public passwordChange(form: PasswordChangeForm) {
371 this.ws.send(this.client.passwordChange(form));
374 public createPrivateMessage(form: PrivateMessageForm) {
376 this.ws.send(this.client.createPrivateMessage(form));
379 public editPrivateMessage(form: EditPrivateMessageForm) {
381 this.ws.send(this.client.editPrivateMessage(form));
384 public deletePrivateMessage(form: DeletePrivateMessageForm) {
386 this.ws.send(this.client.deletePrivateMessage(form));
389 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
391 this.ws.send(this.client.markPrivateMessageAsRead(form));
394 public getPrivateMessages(form: GetPrivateMessagesForm) {
396 this.ws.send(this.client.getPrivateMessages(form));
399 public saveSiteConfig(form: SiteConfigForm) {
401 this.ws.send(this.client.saveSiteConfig(form));
404 public setAuth(obj: any, throwErr: boolean = true) {
405 obj.auth = UserService.Instance.auth;
406 if (obj.auth == null && throwErr) {
407 toast(i18n.t('not_logged_in'), 'danger');
408 throw 'Not logged in';
414 window.onbeforeunload = () => {
415 WebSocketService.Instance.ws.close();