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';
66 import WebSocket from 'isomorphic-ws';
69 default as ReconnectingWebSocket,
70 } from 'reconnecting-websocket';
72 export class WebSocketService {
73 private static _instance: WebSocketService;
74 public ws: ReconnectingWebSocket;
75 public wsOptions: WSOptions = {
77 connectionTimeout: 1000,
80 public subject: Observable<any>;
82 public admins: UserView[];
83 public banned: UserView[];
84 private client = new LemmyWebsocket();
86 private constructor() {
87 this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
88 let firstConnect = true;
90 this.subject = new Observable((obs: any) => {
91 this.ws.onmessage = e => {
92 obs.next(JSON.parse(e.data));
94 this.ws.onopen = () => {
95 console.log(`Connected to ${wsUri}`);
98 let res: WebSocketJsonResponse = {
104 firstConnect = false;
109 public static get Instance() {
110 return this._instance || (this._instance = new this());
114 let form: UserJoinForm = { auth: UserService.Instance.auth };
115 this.ws.send(this.client.userJoin(form));
118 public postJoin(form: PostJoinForm) {
119 this.ws.send(this.client.postJoin(form));
122 public communityJoin(form: CommunityJoinForm) {
123 this.ws.send(this.client.communityJoin(form));
126 public login(form: LoginForm) {
127 this.ws.send(this.client.login(form));
130 public register(form: RegisterForm) {
131 this.ws.send(this.client.register(form));
134 public getCaptcha() {
135 this.ws.send(this.client.getCaptcha());
138 public createCommunity(form: CommunityForm) {
139 this.setAuth(form); // TODO all these setauths at some point would be good to make required
140 this.ws.send(this.client.createCommunity(form));
143 public editCommunity(form: CommunityForm) {
145 this.ws.send(this.client.editCommunity(form));
148 public deleteCommunity(form: DeleteCommunityForm) {
150 this.ws.send(this.client.deleteCommunity(form));
153 public removeCommunity(form: RemoveCommunityForm) {
155 this.ws.send(this.client.removeCommunity(form));
158 public followCommunity(form: FollowCommunityForm) {
160 this.ws.send(this.client.followCommunity(form));
163 public listCommunities(form: ListCommunitiesForm) {
164 this.setAuth(form, false);
165 this.ws.send(this.client.listCommunities(form));
168 public getFollowedCommunities() {
169 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
170 this.ws.send(this.client.getFollowedCommunities(form));
173 public listCategories() {
174 this.ws.send(this.client.listCategories());
177 public createPost(form: PostForm) {
179 this.ws.send(this.client.createPost(form));
182 public getPost(form: GetPostForm) {
183 this.setAuth(form, false);
184 this.ws.send(this.client.getPost(form));
187 public getCommunity(form: GetCommunityForm) {
188 this.setAuth(form, false);
189 this.ws.send(this.client.getCommunity(form));
192 public createComment(form: CommentForm) {
194 this.ws.send(this.client.createComment(form));
197 public editComment(form: CommentForm) {
199 this.ws.send(this.client.editComment(form));
202 public deleteComment(form: DeleteCommentForm) {
204 this.ws.send(this.client.deleteComment(form));
207 public removeComment(form: RemoveCommentForm) {
209 this.ws.send(this.client.removeComment(form));
212 public markCommentAsRead(form: MarkCommentAsReadForm) {
214 this.ws.send(this.client.markCommentAsRead(form));
217 public likeComment(form: CommentLikeForm) {
219 this.ws.send(this.client.likeComment(form));
222 public saveComment(form: SaveCommentForm) {
224 this.ws.send(this.client.saveComment(form));
227 public getPosts(form: GetPostsForm) {
228 this.setAuth(form, false);
229 this.ws.send(this.client.getPosts(form));
232 public getComments(form: GetCommentsForm) {
233 this.setAuth(form, false);
234 this.ws.send(this.client.getComments(form));
237 public likePost(form: CreatePostLikeForm) {
239 this.ws.send(this.client.likePost(form));
242 public editPost(form: PostForm) {
244 this.ws.send(this.client.editPost(form));
247 public deletePost(form: DeletePostForm) {
249 this.ws.send(this.client.deletePost(form));
252 public removePost(form: RemovePostForm) {
254 this.ws.send(this.client.removePost(form));
257 public lockPost(form: LockPostForm) {
259 this.ws.send(this.client.lockPost(form));
262 public stickyPost(form: StickyPostForm) {
264 this.ws.send(this.client.stickyPost(form));
267 public savePost(form: SavePostForm) {
269 this.ws.send(this.client.savePost(form));
272 public banFromCommunity(form: BanFromCommunityForm) {
274 this.ws.send(this.client.banFromCommunity(form));
277 public addModToCommunity(form: AddModToCommunityForm) {
279 this.ws.send(this.client.addModToCommunity(form));
282 public transferCommunity(form: TransferCommunityForm) {
284 this.ws.send(this.client.transferCommunity(form));
287 public transferSite(form: TransferSiteForm) {
289 this.ws.send(this.client.transferSite(form));
292 public banUser(form: BanUserForm) {
294 this.ws.send(this.client.banUser(form));
297 public addAdmin(form: AddAdminForm) {
299 this.ws.send(this.client.addAdmin(form));
302 public getUserDetails(form: GetUserDetailsForm) {
303 this.setAuth(form, false);
304 this.ws.send(this.client.getUserDetails(form));
307 public getReplies(form: GetRepliesForm) {
309 this.ws.send(this.client.getReplies(form));
312 public getUserMentions(form: GetUserMentionsForm) {
314 this.ws.send(this.client.getUserMentions(form));
317 public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
319 this.ws.send(this.client.markUserMentionAsRead(form));
322 public getModlog(form: GetModlogForm) {
323 this.ws.send(this.client.getModlog(form));
326 public createSite(form: SiteForm) {
328 this.ws.send(this.client.createSite(form));
331 public editSite(form: SiteForm) {
333 this.ws.send(this.client.editSite(form));
336 public getSite(form: GetSiteForm = {}) {
337 this.setAuth(form, false);
338 this.ws.send(this.client.getSite(form));
341 public getSiteConfig() {
342 let form: GetSiteConfig = {};
344 this.ws.send(this.client.getSiteConfig(form));
347 public search(form: SearchForm) {
348 this.setAuth(form, false);
349 this.ws.send(this.client.search(form));
352 public markAllAsRead() {
353 let form: MarkAllAsReadForm;
355 this.ws.send(this.client.markAllAsRead(form));
358 public saveUserSettings(form: UserSettingsForm) {
360 this.ws.send(this.client.saveUserSettings(form));
363 public deleteAccount(form: DeleteAccountForm) {
365 this.ws.send(this.client.deleteAccount(form));
368 public passwordReset(form: PasswordResetForm) {
369 this.ws.send(this.client.passwordReset(form));
372 public passwordChange(form: PasswordChangeForm) {
373 this.ws.send(this.client.passwordChange(form));
376 public createPrivateMessage(form: PrivateMessageForm) {
378 this.ws.send(this.client.createPrivateMessage(form));
381 public editPrivateMessage(form: EditPrivateMessageForm) {
383 this.ws.send(this.client.editPrivateMessage(form));
386 public deletePrivateMessage(form: DeletePrivateMessageForm) {
388 this.ws.send(this.client.deletePrivateMessage(form));
391 public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
393 this.ws.send(this.client.markPrivateMessageAsRead(form));
396 public getPrivateMessages(form: GetPrivateMessagesForm) {
398 this.ws.send(this.client.getPrivateMessages(form));
401 public saveSiteConfig(form: SiteConfigForm) {
403 this.ws.send(this.client.saveSiteConfig(form));
406 public setAuth(obj: any, throwErr: boolean = true) {
407 obj.auth = UserService.Instance.auth;
408 if (obj.auth == null && throwErr) {
409 toast(i18n.t('not_logged_in'), 'danger');
410 throw 'Not logged in';
416 window.onbeforeunload = () => {
417 WebSocketService.Instance.ws.close();