parent
a0b3f7606b
commit
8f35e63b3a
13 changed files with 759 additions and 44 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
dist
|
||||
package
|
||||
node_modules
|
||||
docs
|
||||
|
|
20
README.md
20
README.md
|
@ -16,20 +16,28 @@ A javascript / typescript http and websocket client and type system for [Lemmy](
|
|||
|
||||
## Usage
|
||||
|
||||
Check out the [Lemmy HTTP / websocket API](https://dev.lemmy.ml/docs/contributing_websocket_http_api.html) for all the commands.
|
||||
### Websocket Client
|
||||
|
||||
### Websocket
|
||||
[LemmyWebsocket docs](classes/LemmyWebsocket.html)
|
||||
|
||||
```js
|
||||
import { LoginForm, LemmyWebsocket } from 'lemmy-js-client';
|
||||
```ts
|
||||
import { Login, LemmyWebsocket } from 'lemmy-js-client';
|
||||
|
||||
let client: LemmyWebsocket = new LemmyWebsocket();
|
||||
|
||||
let form: Login {
|
||||
username_or_email: "my_email@email.tld",
|
||||
password: "my_pass",
|
||||
};
|
||||
|
||||
this.ws.send(client.login(form));
|
||||
```
|
||||
|
||||
### HTTP
|
||||
### HTTP Client
|
||||
|
||||
```js
|
||||
[LemmyHttp docs](classes/LemmyHttp.html)
|
||||
|
||||
```ts
|
||||
import { LemmyHttp } from 'lemmy-js-client';
|
||||
|
||||
let baseUrl = 'https://lemmy.ml';
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
],
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"docs": "typedoc src/index.ts",
|
||||
"lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src",
|
||||
"prepare": "yarn run build"
|
||||
},
|
||||
|
@ -23,6 +24,7 @@
|
|||
"node-fetch": "^2.6.1",
|
||||
"prettier": "^2.3.2",
|
||||
"sortpack": "^2.2.0",
|
||||
"typedoc": "^0.21.6",
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
|
|
184
src/http.ts
184
src/http.ts
|
@ -105,14 +105,17 @@ enum HttpType {
|
|||
Put = 'PUT',
|
||||
}
|
||||
|
||||
/**
|
||||
* Helps build lemmy HTTP requests.
|
||||
*/
|
||||
export class LemmyHttp {
|
||||
private apiUrl: string;
|
||||
private headers: { [key: string]: string } = {};
|
||||
|
||||
/**
|
||||
* Generates a new instance of LemmyHttp
|
||||
* Generates a new instance of LemmyHttp.
|
||||
* @param baseUrl the base url, without the vX version: https://lemmy.ml -> goes to https://lemmy.ml/api/vX
|
||||
* @param headers optional headers. Should contain x-real-ip and x-forwarded-for
|
||||
* @param headers optional headers. Should contain `x-real-ip` and `x-forwarded-for` .
|
||||
*/
|
||||
constructor(baseUrl: string, headers?: { [key: string]: string }) {
|
||||
this.apiUrl = `${baseUrl}/api/${VERSION}`;
|
||||
|
@ -122,264 +125,441 @@ export class LemmyHttp {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the site, and your user data.
|
||||
*/
|
||||
async getSite(form: GetSite): Promise<GetSiteResponse> {
|
||||
return this.wrapper(HttpType.Get, '/site', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create your site.
|
||||
*/
|
||||
async createSite(form: CreateSite): Promise<SiteResponse> {
|
||||
return this.wrapper(HttpType.Post, '/site', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit your site.
|
||||
*/
|
||||
async editSite(form: EditSite): Promise<SiteResponse> {
|
||||
return this.wrapper(HttpType.Put, '/site', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer your site to another user.
|
||||
*/
|
||||
async transferSite(form: TransferSite): Promise<GetSiteResponse> {
|
||||
return this.wrapper(HttpType.Post, '/site/transfer', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get your site configuration.
|
||||
*/
|
||||
async getSiteConfig(form: GetSiteConfig): Promise<GetSiteConfigResponse> {
|
||||
return this.wrapper(HttpType.Get, '/site/config', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save your site config.
|
||||
*/
|
||||
async saveSiteConfig(form: SaveSiteConfig): Promise<GetSiteConfigResponse> {
|
||||
return this.wrapper(HttpType.Put, '/site/config', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the modlog.
|
||||
*/
|
||||
async getModlog(form: GetModlog): Promise<GetModlogResponse> {
|
||||
return this.wrapper(HttpType.Get, '/modlog', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search lemmy.
|
||||
*/
|
||||
async search(form: Search): Promise<SearchResponse> {
|
||||
return this.wrapper(HttpType.Get, '/search', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new community.
|
||||
*/
|
||||
async createCommunity(form: CreateCommunity): Promise<CommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch a community.
|
||||
*/
|
||||
async getCommunity(form: GetCommunity): Promise<GetCommunityResponse> {
|
||||
return this.wrapper(HttpType.Get, '/community', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a community.
|
||||
*/
|
||||
async editCommunity(form: EditCommunity): Promise<CommunityResponse> {
|
||||
return this.wrapper(HttpType.Put, '/community', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* List communities, with various filters.
|
||||
*/
|
||||
async listCommunities(
|
||||
form: ListCommunities
|
||||
): Promise<ListCommunitiesResponse> {
|
||||
return this.wrapper(HttpType.Get, '/community/list', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Follow / subscribe to a community.
|
||||
*/
|
||||
async followCommunity(form: FollowCommunity): Promise<CommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/follow', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a community.
|
||||
*/
|
||||
async blockCommunity(form: BlockCommunity): Promise<BlockCommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/block', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a community.
|
||||
*/
|
||||
async deleteCommunity(form: DeleteCommunity): Promise<CommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/delete', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a community.
|
||||
*/
|
||||
async removeCommunity(form: RemoveCommunity): Promise<CommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/remove', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer your community to an existing moderator.
|
||||
*/
|
||||
async transferCommunity(
|
||||
form: TransferCommunity
|
||||
): Promise<GetCommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/transfer', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban a user from a community.
|
||||
*/
|
||||
async banFromCommunity(
|
||||
form: BanFromCommunity
|
||||
): Promise<BanFromCommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/ban_user', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a moderator to your community.
|
||||
*/
|
||||
async addModToCommunity(
|
||||
form: AddModToCommunity
|
||||
): Promise<AddModToCommunityResponse> {
|
||||
return this.wrapper(HttpType.Post, '/community/mod', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a post.
|
||||
*/
|
||||
async createPost(form: CreatePost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch a post.
|
||||
*/
|
||||
async getPost(form: GetPost): Promise<GetPostResponse> {
|
||||
return this.wrapper(HttpType.Get, '/post', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a post.
|
||||
*/
|
||||
async editPost(form: EditPost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Put, '/post', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a post.
|
||||
*/
|
||||
async deletePost(form: DeletePost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post/delete', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a post.
|
||||
*/
|
||||
async removePost(form: RemovePost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post/remove', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator can lock a post ( IE disable new comments ).
|
||||
*/
|
||||
async lockPost(form: LockPost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post/lock', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator can sticky a post ( IE stick it to the top of a community ).
|
||||
*/
|
||||
async stickyPost(form: StickyPost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post/sticky', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch posts, with various filters.
|
||||
*/
|
||||
async getPosts(form: GetPosts): Promise<GetPostsResponse> {
|
||||
return this.wrapper(HttpType.Get, '/post/list', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like / vote on a post.
|
||||
*/
|
||||
async likePost(form: CreatePostLike): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Post, '/post/like', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a post.
|
||||
*/
|
||||
async savePost(form: SavePost): Promise<PostResponse> {
|
||||
return this.wrapper(HttpType.Put, '/post/save', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch metadata for any given site.
|
||||
*/
|
||||
async getSiteMetadata(
|
||||
form: GetSiteMetadata
|
||||
): Promise<GetSiteMetadataResponse> {
|
||||
return this.wrapper(HttpType.Get, '/post/site_metadata', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a comment.
|
||||
*/
|
||||
async createComment(form: CreateComment): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Post, '/comment', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a comment.
|
||||
*/
|
||||
async editComment(form: EditComment): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Put, '/comment', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a comment.
|
||||
*/
|
||||
async deleteComment(form: DeleteComment): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Post, '/comment/delete', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a comment.
|
||||
*/
|
||||
async removeComment(form: RemoveComment): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Post, '/comment/remove', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a comment as read.
|
||||
*/
|
||||
async markCommentAsRead(form: MarkCommentAsRead): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Post, '/comment/mark_as_read', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like / vote on a comment.
|
||||
*/
|
||||
async likeComment(form: CreateCommentLike): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Post, '/comment/like', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a comment.
|
||||
*/
|
||||
async saveComment(form: SaveComment): Promise<CommentResponse> {
|
||||
return this.wrapper(HttpType.Put, '/comment/save', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch comments.
|
||||
*/
|
||||
async getComments(form: GetComments): Promise<GetCommentsResponse> {
|
||||
return this.wrapper(HttpType.Get, '/comment/list', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch private messages.
|
||||
*/
|
||||
async getPrivateMessages(
|
||||
form: GetPrivateMessages
|
||||
): Promise<PrivateMessagesResponse> {
|
||||
return this.wrapper(HttpType.Get, '/private_message/list', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a private message.
|
||||
*/
|
||||
async createPrivateMessage(
|
||||
form: CreatePrivateMessage
|
||||
): Promise<PrivateMessageResponse> {
|
||||
return this.wrapper(HttpType.Post, '/private_message', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a private message.
|
||||
*/
|
||||
async editPrivateMessage(
|
||||
form: EditPrivateMessage
|
||||
): Promise<PrivateMessageResponse> {
|
||||
return this.wrapper(HttpType.Put, '/private_message', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a private message.
|
||||
*/
|
||||
async deletePrivateMessage(
|
||||
form: DeletePrivateMessage
|
||||
): Promise<PrivateMessageResponse> {
|
||||
return this.wrapper(HttpType.Post, '/private_message/delete', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a private message as read.
|
||||
*/
|
||||
async markPrivateMessageAsRead(
|
||||
form: MarkPrivateMessageAsRead
|
||||
): Promise<PrivateMessageResponse> {
|
||||
return this.wrapper(HttpType.Post, '/private_message/mark_as_read', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user.
|
||||
*/
|
||||
async register(form: Register): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/register', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log into lemmy.
|
||||
*/
|
||||
async login(form: Login): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/login', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the details for a person.
|
||||
*/
|
||||
async getPersonDetails(
|
||||
form: GetPersonDetails
|
||||
): Promise<GetPersonDetailsResponse> {
|
||||
return this.wrapper(HttpType.Get, '/user', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mentions for your user.
|
||||
*/
|
||||
async getPersonMentions(
|
||||
form: GetPersonMentions
|
||||
): Promise<GetPersonMentionsResponse> {
|
||||
return this.wrapper(HttpType.Get, '/user/mention', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a person mention as read.
|
||||
*/
|
||||
async markPersonMentionAsRead(
|
||||
form: MarkPersonMentionAsRead
|
||||
): Promise<PersonMentionResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/mention/mark_as_read', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comment replies.
|
||||
*/
|
||||
async getReplies(form: GetReplies): Promise<GetRepliesResponse> {
|
||||
return this.wrapper(HttpType.Get, '/user/replies', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban a person from your site.
|
||||
*/
|
||||
async banPerson(form: BanPerson): Promise<BanPersonResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/ban', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a person.
|
||||
*/
|
||||
async blockPerson(form: BlockPerson): Promise<BlockPersonResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/block', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a Captcha.
|
||||
*/
|
||||
async getCaptcha(): Promise<GetCaptchaResponse> {
|
||||
return this.wrapper(HttpType.Get, '/user/get_captcha', {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete your account.
|
||||
*/
|
||||
async deleteAccount(form: DeleteAccount): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/delete_account', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset your password.
|
||||
*/
|
||||
async passwordReset(form: PasswordReset): Promise<PasswordResetResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/password_reset', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change your password from an email / token based reset.
|
||||
*/
|
||||
async passwordChange(form: PasswordChange): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/password_change', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all replies as read.
|
||||
*/
|
||||
async markAllAsRead(form: MarkAllAsRead): Promise<GetRepliesResponse> {
|
||||
return this.wrapper(HttpType.Post, '/user/mark_all_as_read', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save your user settings.
|
||||
*/
|
||||
async saveUserSettings(form: SaveUserSettings): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Put, '/user/save_user_settings', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change your user password.
|
||||
*/
|
||||
async changePassword(form: ChangePassword): Promise<LoginResponse> {
|
||||
return this.wrapper(HttpType.Put, '/user/change_password', form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an admin to your site.
|
||||
*/
|
||||
async addAdmin(form: AddAdmin): Promise<AddAdminResponse> {
|
||||
return this.wrapper(HttpType.Post, '/admin/add', form);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
/**
|
||||
* Aggregate data for a person.
|
||||
*/
|
||||
export interface PersonAggregates {
|
||||
id: number;
|
||||
person_id: number;
|
||||
|
@ -7,6 +10,9 @@ export interface PersonAggregates {
|
|||
comment_score: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate data for your site.
|
||||
*/
|
||||
export interface SiteAggregates {
|
||||
id: number;
|
||||
site_id: number;
|
||||
|
@ -14,12 +20,27 @@ export interface SiteAggregates {
|
|||
posts: number;
|
||||
comments: number;
|
||||
communities: number;
|
||||
/**
|
||||
* Active users per day.
|
||||
*/
|
||||
users_active_day: number;
|
||||
/**
|
||||
* Active users per week.
|
||||
*/
|
||||
users_active_week: number;
|
||||
/**
|
||||
* Active users per month.
|
||||
*/
|
||||
users_active_month: number;
|
||||
/**
|
||||
* Active users per year.
|
||||
*/
|
||||
users_active_half_year: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate data for your post.
|
||||
*/
|
||||
export interface PostAggregates {
|
||||
id: number;
|
||||
post_id: number;
|
||||
|
@ -27,22 +48,43 @@ export interface PostAggregates {
|
|||
score: number;
|
||||
upvotes: number;
|
||||
downvotes: number;
|
||||
/**
|
||||
* Newest comment time, limited to 2 days, to prevent necrobumping.
|
||||
*/
|
||||
newest_comment_time_necro: string;
|
||||
newest_comment_time: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate data for your community.
|
||||
*/
|
||||
export interface CommunityAggregates {
|
||||
id: number;
|
||||
community_id: number;
|
||||
subscribers: number;
|
||||
posts: number;
|
||||
comments: number;
|
||||
/**
|
||||
* Active users per day.
|
||||
*/
|
||||
users_active_day: number;
|
||||
/**
|
||||
* Active users per week.
|
||||
*/
|
||||
users_active_week: number;
|
||||
/**
|
||||
* Active users per month.
|
||||
*/
|
||||
users_active_month: number;
|
||||
/**
|
||||
* Active users per year.
|
||||
*/
|
||||
users_active_half_year: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate data for your comment.
|
||||
*/
|
||||
export interface CommentAggregates {
|
||||
id: number;
|
||||
comment_id: number;
|
||||
|
|
|
@ -4,13 +4,19 @@ export interface CreateComment {
|
|||
content: string;
|
||||
parent_id?: number;
|
||||
post_id: number;
|
||||
form_id?: string; // An optional front end ID, to tell which is coming back
|
||||
/**
|
||||
* An optional front end ID, to tell which is comment is coming back.
|
||||
*/
|
||||
form_id?: string;
|
||||
auth: string;
|
||||
}
|
||||
|
||||
export interface EditComment {
|
||||
content: string;
|
||||
comment_id: number;
|
||||
/**
|
||||
* An optional front end ID, to tell which is comment is coming back.
|
||||
*/
|
||||
form_id?: string;
|
||||
auth: string;
|
||||
}
|
||||
|
@ -52,7 +58,10 @@ export interface SaveComment {
|
|||
export interface CommentResponse {
|
||||
comment_view: CommentView;
|
||||
recipient_ids: number[];
|
||||
form_id?: string; // An optional front end ID, to tell which is coming back
|
||||
/**
|
||||
* An optional front end ID, to tell which is comment is coming back.
|
||||
*/
|
||||
form_id?: string;
|
||||
}
|
||||
|
||||
export interface CreateCommentLike {
|
||||
|
@ -64,10 +73,17 @@ export interface CreateCommentLike {
|
|||
/**
|
||||
* Comment listing types are `All, Subscribed, Community`
|
||||
*
|
||||
* `community_name` can only be used for local communities. To get posts for a federated community, pass `community_id` instead.
|
||||
* You can use either `community_id` or `community_name` as an id.
|
||||
* To get posts for a federated community by name, use `name@instance.tld` .
|
||||
*/
|
||||
export interface GetComments {
|
||||
/**
|
||||
* The [[ListingType]].
|
||||
*/
|
||||
type_?: string;
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
|
@ -106,7 +122,9 @@ export interface ResolveCommentReportResponse {
|
|||
export interface ListCommentReports {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
// if no community is given, it returns reports for all communities moderated by the auth user
|
||||
/**
|
||||
* if no community is given, it returns reports for all communities moderated by the auth user.
|
||||
*/
|
||||
community?: number;
|
||||
auth: string;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,11 @@ import {
|
|||
PersonViewSafe,
|
||||
} from '../views';
|
||||
|
||||
/**
|
||||
* You can use either `id` or `name` as an id.
|
||||
*
|
||||
* To get a federated community by name, use `name@instance.tld` .
|
||||
*/
|
||||
export interface GetCommunity {
|
||||
id?: number;
|
||||
name?: string;
|
||||
|
@ -31,7 +36,14 @@ export interface CommunityResponse {
|
|||
}
|
||||
|
||||
export interface ListCommunities {
|
||||
/**
|
||||
* The [[ListingType]].
|
||||
*/
|
||||
type_?: string;
|
||||
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
|
@ -46,7 +58,11 @@ export interface BanFromCommunity {
|
|||
community_id: number;
|
||||
person_id: number;
|
||||
ban: boolean;
|
||||
remove_data?: boolean; // Removes/Restores their comments and posts for that community
|
||||
|
||||
/**
|
||||
* Removes/Restores their comments and posts for that community.
|
||||
*/
|
||||
remove_data?: boolean;
|
||||
reason?: string;
|
||||
expires?: number;
|
||||
auth: string;
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
import {
|
||||
CommentView,
|
||||
CommunityFollowerView,
|
||||
CommunityModeratorView,
|
||||
PostView,
|
||||
PrivateMessageView,
|
||||
PersonMentionView,
|
||||
PersonViewSafe,
|
||||
CommunityBlockView,
|
||||
PersonBlockView,
|
||||
} from '../views';
|
||||
|
||||
export interface Login {
|
||||
|
@ -16,7 +13,9 @@ export interface Login {
|
|||
}
|
||||
|
||||
/**
|
||||
* Only the first user will be able to be the admin.
|
||||
* Register a new user.
|
||||
*
|
||||
* Only the first user to register will be able to be the admin.
|
||||
*/
|
||||
export interface Register {
|
||||
username: string;
|
||||
|
@ -24,27 +23,60 @@ export interface Register {
|
|||
password: string;
|
||||
password_verify: string;
|
||||
show_nsfw: boolean;
|
||||
captcha_uuid?: string; // Only checked if these are enabled in the server
|
||||
/**
|
||||
* Captcha is only checked if these are enabled in the server.
|
||||
*/
|
||||
captcha_uuid?: string;
|
||||
captcha_answer?: string;
|
||||
}
|
||||
|
||||
export interface GetCaptcha {}
|
||||
|
||||
export interface GetCaptchaResponse {
|
||||
ok?: CaptchaResponse; // Will be undefined if captchas are disabled
|
||||
/**
|
||||
* Will be undefined if captchas are disabled.
|
||||
*/
|
||||
ok?: CaptchaResponse;
|
||||
}
|
||||
|
||||
export interface CaptchaResponse {
|
||||
png: string; // A Base64 encoded png
|
||||
wav?: string; // A Base64 encoded wav aud,
|
||||
/**
|
||||
* A Base64 encoded png.
|
||||
*/
|
||||
png: string;
|
||||
|
||||
/**
|
||||
* A Base64 encoded wav file.
|
||||
*/
|
||||
wav?: string;
|
||||
|
||||
/**
|
||||
* A UUID to match the one given on request.
|
||||
*/
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
export interface SaveUserSettings {
|
||||
show_nsfw?: boolean;
|
||||
theme?: string; // Default 'browser'
|
||||
default_sort_type?: number; // The Sort types from above, zero indexed as a number
|
||||
default_listing_type?: number; // Post listing types are `All, Subscribed, Community`
|
||||
|
||||
/**
|
||||
* Default for this is `browser`.
|
||||
*/
|
||||
theme?: string;
|
||||
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*
|
||||
* The Sort types from above, zero indexed as a number
|
||||
*/
|
||||
default_sort_type?: number;
|
||||
|
||||
/**
|
||||
* The [[ListingType]].
|
||||
*
|
||||
* Post listing types are `All, Subscribed, Community`
|
||||
*/
|
||||
default_listing_type?: number;
|
||||
lang?: string;
|
||||
avatar?: string;
|
||||
banner?: string;
|
||||
|
@ -76,11 +108,11 @@ export interface LoginResponse {
|
|||
jwt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* `username` can only be used for local users. To get details for a federated user, pass `user_id` instead.
|
||||
*/
|
||||
export interface GetPersonDetails {
|
||||
person_id?: number;
|
||||
/**
|
||||
* To get details for a federated user, use `person@instance.tld`.
|
||||
*/
|
||||
username?: string;
|
||||
sort?: string;
|
||||
page?: number;
|
||||
|
@ -122,7 +154,11 @@ export interface AddAdminResponse {
|
|||
export interface BanPerson {
|
||||
person_id: number;
|
||||
ban: boolean;
|
||||
remove_data?: boolean; // Removes/Restores their comments, posts, and communities
|
||||
|
||||
/**
|
||||
* Removes/Restores their comments, posts, and communities
|
||||
*/
|
||||
remove_data?: boolean;
|
||||
reason?: string;
|
||||
expires?: number;
|
||||
auth: string;
|
||||
|
@ -134,6 +170,9 @@ export interface BanPersonResponse {
|
|||
}
|
||||
|
||||
export interface GetReplies {
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
|
@ -142,6 +181,9 @@ export interface GetReplies {
|
|||
}
|
||||
|
||||
export interface GetPersonMentions {
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
|
|
|
@ -33,17 +33,23 @@ export interface GetPostResponse {
|
|||
online: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post listing types are `All, Subscribed, Community`
|
||||
*
|
||||
* `community_name` can only be used for local communities. To get posts for a federated community, pass `community_id` instead.
|
||||
*/
|
||||
export interface GetPosts {
|
||||
/**
|
||||
* The [[ListingType]].
|
||||
*
|
||||
* Post listing types are `All, Subscribed, Community`
|
||||
*/
|
||||
type_?: string;
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
community_id?: number;
|
||||
/**
|
||||
* To get posts for a federated community by name, use `name@instance.tld` .
|
||||
*/
|
||||
community_name?: string;
|
||||
saved_only?: boolean;
|
||||
auth?: string;
|
||||
|
@ -53,11 +59,12 @@ export interface GetPostsResponse {
|
|||
posts: PostView[];
|
||||
}
|
||||
|
||||
/**
|
||||
* `score` can be 0, -1, or 1
|
||||
*/
|
||||
export interface CreatePostLike {
|
||||
post_id: number;
|
||||
|
||||
/**
|
||||
* `score` can be 0, -1, or 1. Anything else will be rejected.
|
||||
*/
|
||||
score: number;
|
||||
auth: string;
|
||||
}
|
||||
|
|
|
@ -24,15 +24,28 @@ import {
|
|||
} from '../views';
|
||||
|
||||
/**
|
||||
* Search types are `All, Comments, Posts, Communities, Users, Url`
|
||||
* Search lemmy for different types of data.
|
||||
*/
|
||||
export interface Search {
|
||||
/**
|
||||
* The search query string.
|
||||
*/
|
||||
q: string;
|
||||
|
||||
/**
|
||||
* The [[SearchType]].
|
||||
*/
|
||||
type_?: string;
|
||||
community_id?: number;
|
||||
community_name?: string;
|
||||
creator_id?: number;
|
||||
/**
|
||||
* The [[SortType]].
|
||||
*/
|
||||
sort?: string;
|
||||
/**
|
||||
* The [[ListingType]].
|
||||
*/
|
||||
listing_type?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
|
@ -40,6 +53,9 @@ export interface Search {
|
|||
}
|
||||
|
||||
export interface SearchResponse {
|
||||
/**
|
||||
* The [[SearchType]].
|
||||
*/
|
||||
type_: string;
|
||||
comments: CommentView[];
|
||||
posts: PostView[];
|
||||
|
@ -102,15 +118,24 @@ export interface SiteResponse {
|
|||
}
|
||||
|
||||
export interface GetSiteResponse {
|
||||
site_view?: SiteView; // Because the site might not be set up yet
|
||||
/**
|
||||
* Optional, because the site might not be set up yet.
|
||||
*/
|
||||
site_view?: SiteView;
|
||||
admins: PersonViewSafe[];
|
||||
banned: PersonViewSafe[];
|
||||
online: number;
|
||||
version: string;
|
||||
my_user?: MyUserInfo; // Gives back your local user, settings, follows, and blocks if logged in
|
||||
/**
|
||||
* If you're logged in, you'll get back extra user info.
|
||||
*/
|
||||
my_user?: MyUserInfo;
|
||||
federated_instances?: FederatedInstances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Your user info, such as blocks, follows, etc.
|
||||
*/
|
||||
export interface MyUserInfo {
|
||||
local_user_view: LocalUserSettingsView;
|
||||
follows: CommunityFollowerView[];
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
export const VERSION = 'v3';
|
||||
|
||||
/**
|
||||
* All of the websocket operations available
|
||||
* All of the websocket operations available.
|
||||
*/
|
||||
export enum UserOperation {
|
||||
Login,
|
||||
|
@ -68,19 +68,52 @@ export enum UserOperation {
|
|||
BlockPerson,
|
||||
}
|
||||
|
||||
/**
|
||||
* Different sort types used in lemmy.
|
||||
*/
|
||||
export enum SortType {
|
||||
/**
|
||||
* Posts sorted by the most recent comment.
|
||||
*/
|
||||
Active = 'Active',
|
||||
/**
|
||||
* Posts sorted by the published time.
|
||||
*/
|
||||
Hot = 'Hot',
|
||||
New = 'New',
|
||||
/**
|
||||
* The top posts for this last day.
|
||||
*/
|
||||
TopDay = 'TopDay',
|
||||
/**
|
||||
* The top posts for this last week.
|
||||
*/
|
||||
TopWeek = 'TopWeek',
|
||||
/**
|
||||
* The top posts for this last month.
|
||||
*/
|
||||
TopMonth = 'TopMonth',
|
||||
/**
|
||||
* The top posts for this last year.
|
||||
*/
|
||||
TopYear = 'TopYear',
|
||||
/**
|
||||
* The top posts of all time.
|
||||
*/
|
||||
TopAll = 'TopAll',
|
||||
/**
|
||||
* Posts sorted by the most comments.
|
||||
*/
|
||||
MostComments = 'MostComments',
|
||||
/**
|
||||
* Posts sorted by the newest comments, with no necrobumping. IE a forum sort.
|
||||
*/
|
||||
NewComments = 'NewComments',
|
||||
}
|
||||
|
||||
/**
|
||||
* The different listing types for post and comment fetches.
|
||||
*/
|
||||
export enum ListingType {
|
||||
All = 'All',
|
||||
Local = 'Local',
|
||||
|
@ -88,6 +121,9 @@ export enum ListingType {
|
|||
Community = 'Community',
|
||||
}
|
||||
|
||||
/**
|
||||
* Search types for lemmy's search.
|
||||
*/
|
||||
export enum SearchType {
|
||||
All = 'All',
|
||||
Comments = 'Comments',
|
||||
|
@ -97,18 +133,45 @@ export enum SearchType {
|
|||
Url = 'Url',
|
||||
}
|
||||
|
||||
/**
|
||||
* A websocket response. Includes the return type.
|
||||
* Can be used like:
|
||||
*
|
||||
* ```ts
|
||||
* if (op == UserOperation.Search) {
|
||||
* let data = wsJsonToRes<SearchResponse>(msg).data;
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export interface WebSocketResponse<ResponseType> {
|
||||
op: UserOperation;
|
||||
/**
|
||||
* This contains the data for a websocket response.
|
||||
*
|
||||
* The correct response type if given is in [[LemmyHttp]].
|
||||
*/
|
||||
data: ResponseType;
|
||||
}
|
||||
|
||||
/**
|
||||
* A websocket JSON response that includes the errors.
|
||||
*/
|
||||
export interface WebSocketJsonResponse<ResponseType> {
|
||||
op?: string;
|
||||
|
||||
/**
|
||||
* This contains the data for a websocket response.
|
||||
*
|
||||
* The correct response type if given is in [[LemmyHttp]].
|
||||
*/
|
||||
data?: ResponseType;
|
||||
error?: string;
|
||||
reconnect?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A holder for a site's metadata ( such as opengraph tags ), used for post links.
|
||||
*/
|
||||
export interface SiteMetadata {
|
||||
title?: string;
|
||||
description?: string;
|
||||
|
|
200
src/websocket.ts
200
src/websocket.ts
|
@ -70,255 +70,453 @@ import { UserJoin, PostJoin, CommunityJoin } from './interfaces/api/websocket';
|
|||
import { UserOperation } from './interfaces/others';
|
||||
|
||||
/**
|
||||
* Helps build lemmy websocket message requests, that you can use in your Websocket sends
|
||||
* Helps build lemmy websocket message requests, that you can use in your Websocket sends.
|
||||
*
|
||||
* You'll receive back a [[WebsocketResponse]].
|
||||
*
|
||||
* The return types for these are given in [[LemmyHttp]]
|
||||
*/
|
||||
export class LemmyWebsocket {
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Log into lemmy.
|
||||
*/
|
||||
login(form: Login): string {
|
||||
return wrapper(UserOperation.Login, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A websocket join for your user.
|
||||
*
|
||||
* Allows your user to receive private messages and notifications.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
communityJoin(form: CommunityJoin): string {
|
||||
return wrapper(UserOperation.CommunityJoin, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user.
|
||||
*/
|
||||
register(register: Register) {
|
||||
return wrapper(UserOperation.Register, register);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a Captcha.
|
||||
*/
|
||||
getCaptcha() {
|
||||
return wrapper(UserOperation.GetCaptcha, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new community.
|
||||
*/
|
||||
createCommunity(form: CreateCommunity) {
|
||||
return wrapper(UserOperation.CreateCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a community.
|
||||
*/
|
||||
editCommunity(form: EditCommunity) {
|
||||
return wrapper(UserOperation.EditCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a community.
|
||||
*/
|
||||
deleteCommunity(form: DeleteCommunity) {
|
||||
return wrapper(UserOperation.DeleteCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a community.
|
||||
*/
|
||||
removeCommunity(form: RemoveCommunity) {
|
||||
return wrapper(UserOperation.RemoveCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Follow / subscribe to a community.
|
||||
*/
|
||||
followCommunity(form: FollowCommunity) {
|
||||
return wrapper(UserOperation.FollowCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* List communities, with various filters.
|
||||
*/
|
||||
listCommunities(form: ListCommunities) {
|
||||
return wrapper(UserOperation.ListCommunities, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a post.
|
||||
*/
|
||||
createPost(form: CreatePost) {
|
||||
return wrapper(UserOperation.CreatePost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch a post.
|
||||
*/
|
||||
getPost(form: GetPost) {
|
||||
return wrapper(UserOperation.GetPost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch a community.
|
||||
*/
|
||||
getCommunity(form: GetCommunity) {
|
||||
return wrapper(UserOperation.GetCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a comment.
|
||||
*/
|
||||
createComment(form: CreateComment) {
|
||||
return wrapper(UserOperation.CreateComment, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a comment.
|
||||
*/
|
||||
editComment(form: EditComment) {
|
||||
return wrapper(UserOperation.EditComment, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a comment.
|
||||
*/
|
||||
deleteComment(form: DeleteComment) {
|
||||
return wrapper(UserOperation.DeleteComment, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a comment.
|
||||
*/
|
||||
removeComment(form: RemoveComment) {
|
||||
return wrapper(UserOperation.RemoveComment, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a comment as read.
|
||||
*/
|
||||
markCommentAsRead(form: MarkCommentAsRead) {
|
||||
return wrapper(UserOperation.MarkCommentAsRead, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like / vote on a comment.
|
||||
*/
|
||||
likeComment(form: CreateCommentLike) {
|
||||
return wrapper(UserOperation.CreateCommentLike, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a comment.
|
||||
*/
|
||||
saveComment(form: SaveComment) {
|
||||
return wrapper(UserOperation.SaveComment, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch posts, with various filters.
|
||||
*/
|
||||
getPosts(form: GetPosts) {
|
||||
return wrapper(UserOperation.GetPosts, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / fetch comments.
|
||||
*/
|
||||
getComments(form: GetComments) {
|
||||
return wrapper(UserOperation.GetComments, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like / vote on a post.
|
||||
*/
|
||||
likePost(form: CreatePostLike) {
|
||||
return wrapper(UserOperation.CreatePostLike, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a post.
|
||||
*/
|
||||
editPost(form: EditPost) {
|
||||
return wrapper(UserOperation.EditPost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a post.
|
||||
*/
|
||||
deletePost(form: DeletePost) {
|
||||
return wrapper(UserOperation.DeletePost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator remove for a post.
|
||||
*/
|
||||
removePost(form: RemovePost) {
|
||||
return wrapper(UserOperation.RemovePost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator can lock a post ( IE disable new comments ).
|
||||
*/
|
||||
lockPost(form: LockPost) {
|
||||
return wrapper(UserOperation.LockPost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* A moderator can sticky a post ( IE stick it to the top of a community ).
|
||||
*/
|
||||
stickyPost(form: StickyPost) {
|
||||
return wrapper(UserOperation.StickyPost, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a post.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
banFromCommunity(form: BanFromCommunity) {
|
||||
return wrapper(UserOperation.BanFromCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a moderator to your community.
|
||||
*/
|
||||
addModToCommunity(form: AddModToCommunity) {
|
||||
return wrapper(UserOperation.AddModToCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer your community to an existing moderator.
|
||||
*/
|
||||
transferCommunity(form: TransferCommunity) {
|
||||
return wrapper(UserOperation.TransferCommunity, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer your site to another user.
|
||||
*/
|
||||
transferSite(form: TransferSite) {
|
||||
return wrapper(UserOperation.TransferSite, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban a person from your site.
|
||||
*/
|
||||
banPerson(form: BanPerson) {
|
||||
return wrapper(UserOperation.BanPerson, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an admin to your site.
|
||||
*/
|
||||
addAdmin(form: AddAdmin) {
|
||||
return wrapper(UserOperation.AddAdmin, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the details for a person.
|
||||
*/
|
||||
getPersonDetails(form: GetPersonDetails) {
|
||||
return wrapper(UserOperation.GetPersonDetails, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comment replies.
|
||||
*/
|
||||
getReplies(form: GetReplies) {
|
||||
return wrapper(UserOperation.GetReplies, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mentions for your user.
|
||||
*/
|
||||
getPersonMentions(form: GetPersonMentions) {
|
||||
return wrapper(UserOperation.GetPersonMentions, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a person mention as read.
|
||||
*/
|
||||
markPersonMentionAsRead(form: MarkPersonMentionAsRead) {
|
||||
return wrapper(UserOperation.MarkPersonMentionAsRead, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the modlog.
|
||||
*/
|
||||
getModlog(form: GetModlog) {
|
||||
return wrapper(UserOperation.GetModlog, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create your site.
|
||||
*/
|
||||
createSite(form: CreateSite) {
|
||||