lemmy/ui/src/services/WebSocketService.ts

386 lines
11 KiB
TypeScript
Raw Normal View History

import { wsUri } from '../env';
import {
LoginForm,
RegisterForm,
UserOperation,
CommunityForm,
2020-07-20 13:37:39 -04:00
DeleteCommunityForm,
RemoveCommunityForm,
PostForm,
SavePostForm,
CommentForm,
2020-07-20 21:37:44 -04:00
DeleteCommentForm,
RemoveCommentForm,
MarkCommentAsReadForm,
SaveCommentForm,
CommentLikeForm,
2020-01-27 21:04:30 -05:00
GetPostForm,
GetPostsForm,
CreatePostLikeForm,
2020-01-27 21:04:30 -05:00
GetCommunityForm,
FollowCommunityForm,
2020-01-27 21:04:30 -05:00
GetFollowedCommunitiesForm,
GetUserDetailsForm,
ListCommunitiesForm,
GetModlogForm,
BanFromCommunityForm,
AddModToCommunityForm,
TransferCommunityForm,
AddAdminForm,
TransferSiteForm,
BanUserForm,
SiteForm,
UserView,
GetRepliesForm,
GetUserMentionsForm,
MarkUserMentionAsReadForm,
SearchForm,
UserSettingsForm,
DeleteAccountForm,
PasswordResetForm,
2019-11-02 02:41:57 -04:00
PasswordChangeForm,
PrivateMessageForm,
EditPrivateMessageForm,
DeletePrivateMessageForm,
MarkPrivateMessageAsReadForm,
GetPrivateMessagesForm,
GetCommentsForm,
UserJoinForm,
GetSiteConfig,
SiteConfigForm,
2020-01-27 21:04:30 -05:00
MessageType,
WebSocketJsonResponse,
} from '../interfaces';
import { UserService } from './';
import { i18n } from '../i18next';
import { toast } from '../utils';
import { Observable } from 'rxjs';
import { share } from 'rxjs/operators';
import ReconnectingWebSocket from 'reconnecting-websocket';
export class WebSocketService {
private static _instance: WebSocketService;
public ws: ReconnectingWebSocket;
public subject: Observable<any>;
2019-04-16 19:04:23 -04:00
public admins: Array<UserView>;
public banned: Array<UserView>;
private constructor() {
this.ws = new ReconnectingWebSocket(wsUri);
2020-03-04 22:35:55 -05:00
let firstConnect = true;
this.subject = Observable.create((obs: any) => {
this.ws.onmessage = e => {
obs.next(JSON.parse(e.data));
};
this.ws.onopen = () => {
console.log(`Connected to ${wsUri}`);
2020-03-04 22:35:55 -05:00
if (UserService.Instance.user) {
this.userJoin();
}
2020-03-04 22:35:55 -05:00
if (!firstConnect) {
let res: WebSocketJsonResponse = {
reconnect: true,
};
obs.next(res);
}
firstConnect = false;
};
}).pipe(share());
}
public static get Instance() {
return this._instance || (this._instance = new this());
}
2020-01-26 12:23:28 -05:00
public userJoin() {
let form: UserJoinForm = { auth: UserService.Instance.auth };
this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
2020-01-26 12:23:28 -05:00
}
public login(loginForm: LoginForm) {
this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
}
public register(registerForm: RegisterForm) {
this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
}
2020-07-20 13:37:39 -04:00
public createCommunity(form: CommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.CreateCommunity, form));
}
2020-07-20 13:37:39 -04:00
public editCommunity(form: CommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.EditCommunity, form));
}
public deleteCommunity(form: DeleteCommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.DeleteCommunity, form));
}
public removeCommunity(form: RemoveCommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.RemoveCommunity, form));
}
public followCommunity(followCommunityForm: FollowCommunityForm) {
this.setAuth(followCommunityForm);
this.ws.send(
this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
);
}
public listCommunities(form: ListCommunitiesForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
}
public getFollowedCommunities() {
2020-01-27 21:04:30 -05:00
let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
this.ws.send(
2020-01-27 21:04:30 -05:00
this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
);
}
public listCategories() {
this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
}
public createPost(postForm: PostForm) {
this.setAuth(postForm);
this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
}
2020-01-27 21:04:30 -05:00
public getPost(form: GetPostForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
}
2020-01-27 21:04:30 -05:00
public getCommunity(form: GetCommunityForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
}
2020-07-20 21:37:44 -04:00
public createComment(form: CommentForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, form));
}
public editComment(form: CommentForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.EditComment, form));
}
2020-07-20 21:37:44 -04:00
public deleteComment(form: DeleteCommentForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.DeleteComment, form));
}
public removeComment(form: RemoveCommentForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.RemoveComment, form));
}
public markCommentAsRead(form: MarkCommentAsReadForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.MarkCommentAsRead, form));
2019-03-29 00:56:23 -04:00
}
public likeComment(form: CommentLikeForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
}
public saveComment(form: SaveCommentForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
}
public getPosts(form: GetPostsForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
}
public getComments(form: GetCommentsForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
}
public likePost(form: CreatePostLikeForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
}
public editPost(postForm: PostForm) {
this.setAuth(postForm);
this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
}
public savePost(form: SavePostForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
}
public banFromCommunity(form: BanFromCommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
}
public addModToCommunity(form: AddModToCommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
}
public transferCommunity(form: TransferCommunityForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
}
public transferSite(form: TransferSiteForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
}
public banUser(form: BanUserForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
}
public addAdmin(form: AddAdminForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
}
public getUserDetails(form: GetUserDetailsForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
}
2019-04-20 14:17:00 -04:00
public getReplies(form: GetRepliesForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
2019-04-20 14:17:00 -04:00
}
public getUserMentions(form: GetUserMentionsForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
}
public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
}
public getModlog(form: GetModlogForm) {
this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
}
2019-04-16 19:04:23 -04:00
public createSite(siteForm: SiteForm) {
this.setAuth(siteForm);
this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
2019-04-16 19:04:23 -04:00
}
public editSite(siteForm: SiteForm) {
this.setAuth(siteForm);
this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
2019-04-16 19:04:23 -04:00
}
2019-04-23 18:05:50 -04:00
2019-04-16 19:04:23 -04:00
public getSite() {
this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
2019-04-16 19:04:23 -04:00
}
public getSiteConfig() {
let siteConfig: GetSiteConfig = {};
this.setAuth(siteConfig);
this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
}
2019-04-23 18:05:50 -04:00
public search(form: SearchForm) {
this.setAuth(form, false);
this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
2019-04-23 18:05:50 -04:00
}
2019-04-29 12:51:13 -04:00
public markAllAsRead() {
let form = {};
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
2019-04-29 12:51:13 -04:00
}
public saveUserSettings(userSettingsForm: UserSettingsForm) {
this.setAuth(userSettingsForm);
this.ws.send(
this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
);
}
public deleteAccount(form: DeleteAccountForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
}
public passwordReset(form: PasswordResetForm) {
this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
}
2019-11-02 02:41:57 -04:00
public passwordChange(form: PasswordChangeForm) {
this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
2019-11-02 02:41:57 -04:00
}
public createPrivateMessage(form: PrivateMessageForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
}
public editPrivateMessage(form: EditPrivateMessageForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
}
public deletePrivateMessage(form: DeletePrivateMessageForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.DeletePrivateMessage, form));
}
public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
this.setAuth(form);
this.ws.send(
this.wsSendWrapper(UserOperation.MarkPrivateMessageAsRead, form)
);
}
public getPrivateMessages(form: GetPrivateMessagesForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
}
public saveSiteConfig(form: SiteConfigForm) {
this.setAuth(form);
this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
}
2020-01-27 21:04:30 -05:00
private wsSendWrapper(op: UserOperation, data: MessageType) {
let send = { op: UserOperation[op], data: data };
console.log(send);
return JSON.stringify(send);
}
private setAuth(obj: any, throwErr: boolean = true) {
obj.auth = UserService.Instance.auth;
if (obj.auth == null && throwErr) {
toast(i18n.t('not_logged_in'), 'danger');
throw 'Not logged in';
}
}
}
window.onbeforeunload = () => {
WebSocketService.Instance.ws.close();
};