lemmy-js-client/src/websocket.ts

538 lines
11 KiB
TypeScript
Raw Normal View History

2020-12-20 17:38:55 +00:00
import {
CreateComment,
CreateCommentLike,
DeleteComment,
EditComment,
GetComments,
MarkCommentAsRead,
RemoveComment,
SaveComment,
} from './interfaces/api/comment';
import {
AddModToCommunity,
BanFromCommunity,
BlockCommunity,
2020-12-20 17:38:55 +00:00
CreateCommunity,
DeleteCommunity,
EditCommunity,
FollowCommunity,
GetCommunity,
ListCommunities,
RemoveCommunity,
TransferCommunity,
} from './interfaces/api/community';
import {
CreatePost,
CreatePostLike,
DeletePost,
EditPost,
GetPost,
GetPosts,
GetSiteMetadata,
2020-12-20 17:38:55 +00:00
LockPost,
RemovePost,
SavePost,
StickyPost,
} from './interfaces/api/post';
import {
CreateSite,
EditSite,
GetModlog,
GetSite,
GetSiteConfig,
ResolveObject,
2020-12-20 17:38:55 +00:00
SaveSiteConfig,
Search,
TransferSite,
} from './interfaces/api/site';
import {
AddAdmin,
BanPerson,
2020-12-20 17:38:55 +00:00
CreatePrivateMessage,
DeleteAccount,
DeletePrivateMessage,
EditPrivateMessage,
GetPrivateMessages,
GetReplies,
GetPersonDetails,
GetPersonMentions,
2020-12-20 17:38:55 +00:00
Login,
MarkAllAsRead,
MarkPrivateMessageAsRead,
MarkPersonMentionAsRead,
2020-12-20 17:38:55 +00:00
PasswordChange,
PasswordReset,
Register,
SaveUserSettings,
2021-04-01 21:35:37 +00:00
ChangePassword,
BlockPerson,
} from './interfaces/api/person';
2021-01-18 21:45:43 +00:00
import { UserJoin, PostJoin, CommunityJoin } from './interfaces/api/websocket';
import { UserOperation } from './interfaces/others';
2020-12-20 17:38:55 +00:00
2021-01-24 17:37:04 +00:00
/**
* Helps build lemmy websocket message requests, that you can use in your Websocket sends.
*
2021-08-23 15:18:08 +00:00
* You'll receive back a [[WebSocketResponse]].
*
* The return types for these are given in [[LemmyHttp]]
2021-01-24 17:37:04 +00:00
*/
2020-12-20 17:38:55 +00:00
export class LemmyWebsocket {
constructor() {}
/**
* Log into lemmy.
*/
2020-12-20 17:38:55 +00:00
login(form: Login): string {
return wrapper(UserOperation.Login, form);
}
/**
* A websocket join for your user.
*
* Allows your user to receive private messages and notifications.
*/
2020-12-20 17:38:55 +00:00
userJoin(form: UserJoin): string {
return wrapper(UserOperation.UserJoin, form);
}
/**
* A websocket join for the current post room.
*
* Allows your user to receive new comments and updates for that post.
*/
2020-12-20 17:38:55 +00:00
postJoin(form: PostJoin): string {
return wrapper(UserOperation.PostJoin, form);
}
/**
* A websocket join for a given community.
*
* Allows your user to receive community updates.
*
* Note: community_id: 0, is your front page.
*/
2020-12-20 17:38:55 +00:00
communityJoin(form: CommunityJoin): string {
return wrapper(UserOperation.CommunityJoin, form);
}
/**
* Register a new user.
*/
2020-12-20 17:38:55 +00:00
register(register: Register) {
return wrapper(UserOperation.Register, register);
}
/**
* Fetch a Captcha.
*/
2020-12-20 17:38:55 +00:00
getCaptcha() {
return wrapper(UserOperation.GetCaptcha, {});
}
/**
* Create a new community.
*/
2020-12-20 17:38:55 +00:00
createCommunity(form: CreateCommunity) {
return wrapper(UserOperation.CreateCommunity, form);
}
/**
* Edit a community.
*/
2020-12-20 17:38:55 +00:00
editCommunity(form: EditCommunity) {
return wrapper(UserOperation.EditCommunity, form);
}
/**
* Delete a community.
*/
2020-12-20 17:38:55 +00:00
deleteCommunity(form: DeleteCommunity) {
return wrapper(UserOperation.DeleteCommunity, form);
}
/**
* A moderator remove for a community.
*/
2020-12-20 17:38:55 +00:00
removeCommunity(form: RemoveCommunity) {
return wrapper(UserOperation.RemoveCommunity, form);
}
/**
* Follow / subscribe to a community.
*/
2020-12-20 17:38:55 +00:00
followCommunity(form: FollowCommunity) {
return wrapper(UserOperation.FollowCommunity, form);
}
/**
* List communities, with various filters.
*/
2020-12-20 17:38:55 +00:00
listCommunities(form: ListCommunities) {
return wrapper(UserOperation.ListCommunities, form);
}
/**
* Create a post.
*/
2020-12-20 17:38:55 +00:00
createPost(form: CreatePost) {
return wrapper(UserOperation.CreatePost, form);
}
/**
* Get / fetch a post.
*/
2020-12-20 17:38:55 +00:00
getPost(form: GetPost) {
return wrapper(UserOperation.GetPost, form);
}
/**
* Get / fetch a community.
*/
2020-12-20 17:38:55 +00:00
getCommunity(form: GetCommunity) {
return wrapper(UserOperation.GetCommunity, form);
}
/**
* Create a comment.
*/
2020-12-20 17:38:55 +00:00
createComment(form: CreateComment) {
return wrapper(UserOperation.CreateComment, form);
}
/**
* Edit a comment.
*/
2020-12-20 17:38:55 +00:00
editComment(form: EditComment) {
return wrapper(UserOperation.EditComment, form);
}
/**
* Delete a comment.
*/
2020-12-20 17:38:55 +00:00
deleteComment(form: DeleteComment) {
return wrapper(UserOperation.DeleteComment, form);
}
/**
* A moderator remove for a comment.
*/
2020-12-20 17:38:55 +00:00
removeComment(form: RemoveComment) {
return wrapper(UserOperation.RemoveComment, form);
}
/**
* Mark a comment as read.
*/
2020-12-20 17:38:55 +00:00
markCommentAsRead(form: MarkCommentAsRead) {
return wrapper(UserOperation.MarkCommentAsRead, form);
}
/**
* Like / vote on a comment.
*/
2020-12-20 17:38:55 +00:00
likeComment(form: CreateCommentLike) {
return wrapper(UserOperation.CreateCommentLike, form);
}
/**
* Save a comment.
*/
2020-12-20 17:38:55 +00:00
saveComment(form: SaveComment) {
return wrapper(UserOperation.SaveComment, form);
}
/**
* Get / fetch posts, with various filters.
*/
2020-12-20 17:38:55 +00:00
getPosts(form: GetPosts) {
return wrapper(UserOperation.GetPosts, form);
}
/**
* Get / fetch comments.
*/
2020-12-20 17:38:55 +00:00
getComments(form: GetComments) {
return wrapper(UserOperation.GetComments, form);
}
/**
* Like / vote on a post.
*/
2020-12-20 17:38:55 +00:00
likePost(form: CreatePostLike) {
return wrapper(UserOperation.CreatePostLike, form);
}
/**
* Edit a post.
*/
2020-12-20 17:38:55 +00:00
editPost(form: EditPost) {
return wrapper(UserOperation.EditPost, form);
}
/**
* Delete a post.
*/
2020-12-20 17:38:55 +00:00
deletePost(form: DeletePost) {
return wrapper(UserOperation.DeletePost, form);
}
/**
* A moderator remove for a post.
*/
2020-12-20 17:38:55 +00:00
removePost(form: RemovePost) {
return wrapper(UserOperation.RemovePost, form);
}
/**
* A moderator can lock a post ( IE disable new comments ).
*/
2020-12-20 17:38:55 +00:00
lockPost(form: LockPost) {
return wrapper(UserOperation.LockPost, form);
}
/**
* A moderator can sticky a post ( IE stick it to the top of a community ).
*/
2020-12-20 17:38:55 +00:00
stickyPost(form: StickyPost) {
return wrapper(UserOperation.StickyPost, form);
}
/**
* Save a post.
*/
2020-12-20 17:38:55 +00:00
savePost(form: SavePost) {
return wrapper(UserOperation.SavePost, form);
}
/**
* Fetch metadata for any given site.
*/
getSiteMetadata(form: GetSiteMetadata) {
return wrapper(UserOperation.GetSiteMetadata, form);
}
/**
* Ban a user from a community.
*/
2020-12-20 17:38:55 +00:00
banFromCommunity(form: BanFromCommunity) {
return wrapper(UserOperation.BanFromCommunity, form);
}
/**
* Add a moderator to your community.
*/
2020-12-20 17:38:55 +00:00
addModToCommunity(form: AddModToCommunity) {
return wrapper(UserOperation.AddModToCommunity, form);
}
/**
* Transfer your community to an existing moderator.
*/
2020-12-20 17:38:55 +00:00
transferCommunity(form: TransferCommunity) {
return wrapper(UserOperation.TransferCommunity, form);
}
/**
* Transfer your site to another user.
*/
2020-12-20 17:38:55 +00:00
transferSite(form: TransferSite) {
return wrapper(UserOperation.TransferSite, form);
}
/**
* Ban a person from your site.
*/
banPerson(form: BanPerson) {
return wrapper(UserOperation.BanPerson, form);
2020-12-20 17:38:55 +00:00
}
/**
* Add an admin to your site.
*/
2020-12-20 17:38:55 +00:00
addAdmin(form: AddAdmin) {
return wrapper(UserOperation.AddAdmin, form);
}
/**
* Get the details for a person.
*/
getPersonDetails(form: GetPersonDetails) {
return wrapper(UserOperation.GetPersonDetails, form);
2020-12-20 17:38:55 +00:00
}
/**
* Get comment replies.
*/
2020-12-20 17:38:55 +00:00
getReplies(form: GetReplies) {
return wrapper(UserOperation.GetReplies, form);
}
/**
* Get mentions for your user.
*/
getPersonMentions(form: GetPersonMentions) {
return wrapper(UserOperation.GetPersonMentions, form);
2020-12-20 17:38:55 +00:00
}
/**
* Mark a person mention as read.
*/
markPersonMentionAsRead(form: MarkPersonMentionAsRead) {
return wrapper(UserOperation.MarkPersonMentionAsRead, form);
2020-12-20 17:38:55 +00:00
}
/**
* Get the modlog.
*/
2020-12-20 17:38:55 +00:00
getModlog(form: GetModlog) {
return wrapper(UserOperation.GetModlog, form);
}
/**
* Create your site.
*/
2020-12-20 17:38:55 +00:00
createSite(form: CreateSite) {
return wrapper(UserOperation.CreateSite, form);
}
/**
* Edit your site.
*/
2020-12-20 17:38:55 +00:00
editSite(form: EditSite) {
return wrapper(UserOperation.EditSite, form);
}
/**
* Gets the site, and your user data.
*/
2020-12-20 17:38:55 +00:00
getSite(form: GetSite = {}) {
return wrapper(UserOperation.GetSite, form);
}
/**
* Get your site configuration.
*/
2020-12-20 17:38:55 +00:00
getSiteConfig(form: GetSiteConfig) {
return wrapper(UserOperation.GetSiteConfig, form);
}
/**
* Search lemmy.
*/
2020-12-20 17:38:55 +00:00
search(form: Search) {
return wrapper(UserOperation.Search, form);
}
/**
* Fetch a non-local / federated object.
*/
resolveObject(form: ResolveObject) {
return wrapper(UserOperation.ResolveObject, form);
}
/**
* Mark all replies as read.
*/
2020-12-20 17:38:55 +00:00
markAllAsRead(form: MarkAllAsRead) {
return wrapper(UserOperation.MarkAllAsRead, form);
}
/**
* Save your user settings.
*/
2020-12-20 17:38:55 +00:00
saveUserSettings(form: SaveUserSettings) {
return wrapper(UserOperation.SaveUserSettings, form);
}
/**
* Change your user password.
*/
2021-04-01 21:35:37 +00:00
changePassword(form: ChangePassword) {
return wrapper(UserOperation.ChangePassword, form);
}
/**
* Delete your account.
*/
2020-12-20 17:38:55 +00:00
deleteAccount(form: DeleteAccount) {
return wrapper(UserOperation.DeleteAccount, form);
}
/**
* Reset your password.
*/
2020-12-20 17:38:55 +00:00
passwordReset(form: PasswordReset) {
return wrapper(UserOperation.PasswordReset, form);
}
/**
* Change your password from an email / token based reset.
*/
2020-12-20 17:38:55 +00:00
passwordChange(form: PasswordChange) {
return wrapper(UserOperation.PasswordChange, form);
}
/**
* Create a private message.
*/
2020-12-20 17:38:55 +00:00
createPrivateMessage(form: CreatePrivateMessage) {
return wrapper(UserOperation.CreatePrivateMessage, form);
}
/**
* Edit a private message.
*/
2020-12-20 17:38:55 +00:00
editPrivateMessage(form: EditPrivateMessage) {
return wrapper(UserOperation.EditPrivateMessage, form);
}
/**
* Delete a private message.
*/
2020-12-20 17:38:55 +00:00
deletePrivateMessage(form: DeletePrivateMessage) {
return wrapper(UserOperation.DeletePrivateMessage, form);
}
/**
* Mark a private message as read.
*/
2020-12-20 17:38:55 +00:00
markPrivateMessageAsRead(form: MarkPrivateMessageAsRead) {
return wrapper(UserOperation.MarkPrivateMessageAsRead, form);
}
/**
* Get / fetch private messages.
*/
2020-12-20 17:38:55 +00:00
getPrivateMessages(form: GetPrivateMessages) {
return wrapper(UserOperation.GetPrivateMessages, form);
}
/**
* Save your site config.
*/
2020-12-20 17:38:55 +00:00
saveSiteConfig(form: SaveSiteConfig) {
return wrapper(UserOperation.SaveSiteConfig, form);
}
/**
* Block a person.
*/
blockPerson(form: BlockPerson) {
return wrapper(UserOperation.BlockPerson, form);
}
/**
* Block a community.
*/
blockCommunity(form: BlockCommunity) {
return wrapper(UserOperation.BlockCommunity, form);
}
2020-12-20 17:38:55 +00:00
}
function wrapper<MessageType>(op: UserOperation, data: MessageType) {
2020-12-20 17:38:55 +00:00
let send = { op: UserOperation[op], data: data };
console.log(send);
return JSON.stringify(send);
}