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, isBrowser } from '../utils';
62 import { Observable } from 'rxjs';
63 import { share } from 'rxjs/operators';
64 import WebSocket from 'isomorphic-ws';
67 default as ReconnectingWebSocket,
68 } from 'reconnecting-websocket';
70 export class WebSocketService {
71 private static _instance: WebSocketService;
72 public ws: ReconnectingWebSocket;
73 public wsOptions: WSOptions = {
75 connectionTimeout: 1000,
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 = Observable.create((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 login(form: LoginForm) {
117 this.ws.send(this.client.login(form));
120 public register(form: RegisterForm) {
121 this.ws.send(this.client.register(form));
124 public getCaptcha() {
125 this.ws.send(this.client.getCaptcha());
128 public createCommunity(form: CommunityForm) {
129 this.setAuth(form); // TODO all these setauths at some point would be good to make required
130 this.ws.send(this.client.createCommunity(form));
133 public editCommunity(form: CommunityForm) {
135 this.ws.send(this.client.editCommunity(form));
138 public deleteCommunity(form: DeleteCommunityForm) {
140 this.ws.send(this.client.deleteCommunity(form));
143 public removeCommunity(form: RemoveCommunityForm) {
145 this.ws.send(this.client.removeCommunity(form));
148 public followCommunity(form: FollowCommunityForm) {
150 this.ws.send(this.client.followCommunity(form));
153 public listCommunities(form: ListCommunitiesForm) {
154 this.setAuth(form, false);
155 this.ws.send(this.client.listCommunities(form));
158 public getFollowedCommunities() {
159 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
160 this.ws.send(this.client.getFollowedCommunities(form));
163 public listCategories() {
164 this.ws.send(this.client.listCategories());
167 public createPost(form: PostForm) {
169 this.ws.send(this.client.createPost(form));
172 public getPost(form: GetPostForm) {
173 this.setAuth(form, false);
174 this.ws.send(this.client.getPost(form));
177 public getCommunity(form: GetCommunityForm) {
178 this.setAuth(form, false);
179 this.ws.send(this.client.getCommunity(form));
182 public createComment(form: CommentForm) {
184 this.ws.send(this.client.createComment(form));
187 public editComment(form: CommentForm) {
189 this.ws.send(this.client.editComment(form));
192 public deleteComment(form: DeleteCommentForm) {
194 this.ws.send(this.client.deleteComment(form));
197 public removeComment(form: RemoveCommentForm) {
199 this.ws.send(this.client.removeComment(form));
202 public markCommentAsRead(form: MarkCommentAsReadForm) {
204 this.ws.send(this.client.markCommentAsRead(form));
207 public likeComment(form: CommentLikeForm) {
209 this.ws.send(this.client.likeComment(form));
212 public saveComment(form: SaveCommentForm) {
214 this.ws.send(this.client.saveComment(form));
217 public getPosts(form: GetPostsForm) {
218 this.setAuth(form, false);
219 this.ws.send(this.client.getPosts(form));
222 public getComments(form: GetCommentsForm) {
223 this.setAuth(form, false);
224 this.ws.send(this.client.getComments(form));
227 public likePost(form: CreatePostLikeForm) {
229 this.ws.send(this.client.likePost(form));
232 public editPost(form: PostForm) {
234 this.ws.send(this.client.editPost(form));
237 public deletePost(form: DeletePostForm) {
239 this.ws.send(this.client.deletePost(form));
242 public removePost(form: RemovePostForm) {
244 this.ws.send(this.client.removePost(form));
247 public lockPost(form: LockPostForm) {
249 this.ws.send(this.client.lockPost(form));
252 public stickyPost(form: StickyPostForm) {
254 this.ws.send(this.client.stickyPost(form));
257 public savePost(form: SavePostForm) {
259 this.ws.send(this.client.savePost(form));
262 public banFromCommunity(form: BanFromCommunityForm) {
264 this.ws.send(this.client.banFromCommunity(form));
267 public addModToCommunity(form: AddModToCommunityForm) {
269 this.ws.send(this.client.addModToCommunity(form));
272 public transferCommunity(form: TransferCommunityForm) {
274 this.ws.send(this.client.transferCommunity(form));
277 public transferSite(form: TransferSiteForm) {
279 this.ws.send(this.client.transferSite(form));
282 public banUser(form: BanUserForm) {
284 this.ws.send(this.client.banUser(form));
287 public addAdmin(form: AddAdminForm) {
289 this.ws.send(this.client.addAdmin(form));
292 public getUserDetails(form: GetUserDetailsForm) {
293 this.setAuth(form, false);
294 this.ws.send(this.client.getUserDetails(form));
297 public getReplies(form: GetRepliesForm) {
299 this.ws.send(this.client.getReplies(form));
302 public getUserMentions(form: GetUserMentionsForm) {
304 this.ws.send(this.client.getUserMentions(form));
307 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
309 this.ws.send(this.client.markUserMentionAsRead(form));
312 public getModlog(form: GetModlogForm) {
313 this.ws.send(this.client.getModlog(form));
316 public createSite(form: SiteForm) {
318 this.ws.send(this.client.createSite(form));
321 public editSite(form: SiteForm) {
323 this.ws.send(this.client.editSite(form));
326 public getSite(form: GetSiteForm = {}) {
327 this.setAuth(form, false);
328 this.ws.send(this.client.getSite(form));
331 public getSiteConfig() {
332 let form: GetSiteConfig = {};
334 this.ws.send(this.client.getSiteConfig(form));
337 public search(form: SearchForm) {
338 this.setAuth(form, false);
339 this.ws.send(this.client.search(form));
342 public markAllAsRead() {
343 let form: MarkAllAsReadForm;
345 this.ws.send(this.client.markAllAsRead(form));
348 public saveUserSettings(form: UserSettingsForm) {
350 this.ws.send(this.client.saveUserSettings(form));
353 public deleteAccount(form: DeleteAccountForm) {
355 this.ws.send(this.client.deleteAccount(form));
358 public passwordReset(form: PasswordResetForm) {
359 this.ws.send(this.client.passwordReset(form));
362 public passwordChange(form: PasswordChangeForm) {
363 this.ws.send(this.client.passwordChange(form));
366 public createPrivateMessage(form: PrivateMessageForm) {
368 this.ws.send(this.client.createPrivateMessage(form));
371 public editPrivateMessage(form: EditPrivateMessageForm) {
373 this.ws.send(this.client.editPrivateMessage(form));
376 public deletePrivateMessage(form: DeletePrivateMessageForm) {
378 this.ws.send(this.client.deletePrivateMessage(form));
381 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
383 this.ws.send(this.client.markPrivateMessageAsRead(form));
386 public getPrivateMessages(form: GetPrivateMessagesForm) {
388 this.ws.send(this.client.getPrivateMessages(form));
391 public saveSiteConfig(form: SiteConfigForm) {
393 this.ws.send(this.client.saveSiteConfig(form));
396 private setAuth(obj: any, throwErr: boolean = true) {
397 obj.auth = UserService.Instance.auth;
398 if (obj.auth == null && throwErr) {
399 toast(i18n.t('not_logged_in'), 'danger');
400 throw 'Not logged in';
406 window.onbeforeunload = () => {
407 WebSocketService.Instance.ws.close();