2020-08-20 03:51:57 +00:00
|
|
|
import fetch from 'node-fetch';
|
2020-08-20 02:25:21 +00:00
|
|
|
import {
|
2021-09-28 10:38:49 +00:00
|
|
|
CommentReportResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
CommentResponse,
|
|
|
|
CreateComment,
|
|
|
|
CreateCommentLike,
|
2021-09-28 10:38:49 +00:00
|
|
|
CreateCommentReport,
|
2020-12-20 17:33:14 +00:00
|
|
|
DeleteComment,
|
|
|
|
EditComment,
|
|
|
|
GetComments,
|
|
|
|
GetCommentsResponse,
|
2021-09-28 10:38:49 +00:00
|
|
|
ListCommentReports,
|
|
|
|
ListCommentReportsResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
MarkCommentAsRead,
|
|
|
|
RemoveComment,
|
2021-09-28 10:38:49 +00:00
|
|
|
ResolveCommentReport,
|
2020-12-20 17:33:14 +00:00
|
|
|
SaveComment,
|
|
|
|
} from './interfaces/api/comment';
|
|
|
|
import {
|
|
|
|
AddModToCommunity,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
BanFromCommunity,
|
|
|
|
BanFromCommunityResponse,
|
2021-08-20 00:49:15 +00:00
|
|
|
BlockCommunity,
|
|
|
|
BlockCommunityResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
CommunityResponse,
|
|
|
|
CreateCommunity,
|
|
|
|
DeleteCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
FollowCommunity,
|
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
|
|
|
ListCommunities,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
RemoveCommunity,
|
|
|
|
TransferCommunity,
|
|
|
|
} from './interfaces/api/community';
|
|
|
|
import {
|
|
|
|
CreatePost,
|
|
|
|
CreatePostLike,
|
|
|
|
DeletePost,
|
|
|
|
EditPost,
|
|
|
|
GetPost,
|
|
|
|
GetPostResponse,
|
|
|
|
GetPosts,
|
|
|
|
GetPostsResponse,
|
2021-08-19 15:14:39 +00:00
|
|
|
GetSiteMetadata,
|
|
|
|
GetSiteMetadataResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
LockPost,
|
2021-09-28 10:38:49 +00:00
|
|
|
CreatePostReport,
|
|
|
|
PostReportResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
PostResponse,
|
|
|
|
RemovePost,
|
|
|
|
SavePost,
|
|
|
|
StickyPost,
|
2021-09-28 10:38:49 +00:00
|
|
|
ResolvePostReport,
|
|
|
|
ListPostReports,
|
|
|
|
ListPostReportsResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
} from './interfaces/api/post';
|
|
|
|
import {
|
|
|
|
CreateSite,
|
|
|
|
EditSite,
|
|
|
|
GetModlog,
|
|
|
|
GetModlogResponse,
|
|
|
|
GetSite,
|
2020-08-20 02:25:21 +00:00
|
|
|
GetSiteConfig,
|
|
|
|
GetSiteConfigResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
GetSiteResponse,
|
2021-08-23 20:15:24 +00:00
|
|
|
ResolveObject,
|
|
|
|
ResolveObjectResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
SaveSiteConfig,
|
|
|
|
Search,
|
2020-08-20 02:25:21 +00:00
|
|
|
SearchResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
SiteResponse,
|
|
|
|
TransferSite,
|
|
|
|
} from './interfaces/api/site';
|
|
|
|
import {
|
|
|
|
AddAdmin,
|
2020-08-20 02:25:21 +00:00
|
|
|
AddAdminResponse,
|
2021-03-12 18:22:16 +00:00
|
|
|
BanPerson,
|
|
|
|
BanPersonResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
CreatePrivateMessage,
|
|
|
|
DeleteAccount,
|
|
|
|
DeletePrivateMessage,
|
|
|
|
EditPrivateMessage,
|
2020-08-20 02:25:21 +00:00
|
|
|
GetCaptchaResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
GetPrivateMessages,
|
|
|
|
GetReplies,
|
|
|
|
GetRepliesResponse,
|
2021-03-12 18:22:16 +00:00
|
|
|
GetPersonDetails,
|
|
|
|
GetPersonDetailsResponse,
|
|
|
|
GetPersonMentions,
|
|
|
|
GetPersonMentionsResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
Login,
|
|
|
|
LoginResponse,
|
|
|
|
MarkAllAsRead,
|
|
|
|
MarkPrivateMessageAsRead,
|
2021-03-12 18:22:16 +00:00
|
|
|
MarkPersonMentionAsRead,
|
2020-12-20 17:33:14 +00:00
|
|
|
PasswordChange,
|
|
|
|
PasswordReset,
|
|
|
|
PasswordResetResponse,
|
2020-08-20 02:25:21 +00:00
|
|
|
PrivateMessageResponse,
|
|
|
|
PrivateMessagesResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
Register,
|
|
|
|
SaveUserSettings,
|
2021-04-01 21:35:37 +00:00
|
|
|
ChangePassword,
|
2021-03-12 18:22:16 +00:00
|
|
|
PersonMentionResponse,
|
2021-08-20 00:49:15 +00:00
|
|
|
BlockPerson,
|
|
|
|
BlockPersonResponse,
|
2021-09-28 10:38:49 +00:00
|
|
|
GetReportCount,
|
|
|
|
GetReportCountResponse,
|
2021-10-17 17:42:40 +00:00
|
|
|
GetUnreadCount,
|
|
|
|
GetUnreadCountResponse,
|
2021-03-12 18:22:16 +00:00
|
|
|
} from './interfaces/api/person';
|
2020-08-20 02:25:21 +00:00
|
|
|
|
2021-03-29 14:29:27 +00:00
|
|
|
import { VERSION } from './interfaces/others';
|
|
|
|
|
2020-08-20 02:25:21 +00:00
|
|
|
enum HttpType {
|
|
|
|
Get = 'GET',
|
|
|
|
Post = 'POST',
|
|
|
|
Put = 'PUT',
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Helps build lemmy HTTP requests.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
export class LemmyHttp {
|
2021-03-29 14:29:27 +00:00
|
|
|
private apiUrl: string;
|
2020-11-12 19:42:31 +00:00
|
|
|
private headers: { [key: string]: string } = {};
|
2020-08-20 02:25:21 +00:00
|
|
|
|
2021-01-24 17:37:04 +00:00
|
|
|
/**
|
2021-08-23 01:01:40 +00:00
|
|
|
* Generates a new instance of LemmyHttp.
|
2021-03-29 14:29:27 +00:00
|
|
|
* @param baseUrl the base url, without the vX version: https://lemmy.ml -> goes to https://lemmy.ml/api/vX
|
2021-08-23 01:01:40 +00:00
|
|
|
* @param headers optional headers. Should contain `x-real-ip` and `x-forwarded-for` .
|
2021-01-24 17:37:04 +00:00
|
|
|
*/
|
2020-11-12 19:42:31 +00:00
|
|
|
constructor(baseUrl: string, headers?: { [key: string]: string }) {
|
2021-03-29 14:29:27 +00:00
|
|
|
this.apiUrl = `${baseUrl}/api/${VERSION}`;
|
2020-11-12 19:42:31 +00:00
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
2020-08-20 02:25:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Gets the site, and your user data.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getSite(form: GetSite): Promise<GetSiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/site', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Create your site.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async createSite(form: CreateSite): Promise<SiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/site', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Edit your site.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async editSite(form: EditSite): Promise<SiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/site', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Transfer your site to another user.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async transferSite(form: TransferSite): Promise<GetSiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/site/transfer', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get your site configuration.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async getSiteConfig(form: GetSiteConfig): Promise<GetSiteConfigResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/site/config', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Save your site config.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async saveSiteConfig(form: SaveSiteConfig): Promise<GetSiteConfigResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/site/config', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get the modlog.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getModlog(form: GetModlog): Promise<GetModlogResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/modlog', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Search lemmy.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async search(form: Search): Promise<SearchResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/search', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 20:15:24 +00:00
|
|
|
/**
|
|
|
|
* Fetch a non-local / federated object.
|
|
|
|
*/
|
|
|
|
async resolveObject(form: ResolveObject): Promise<ResolveObjectResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/resolve_object', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Create a new community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async createCommunity(form: CreateCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get / fetch a community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getCommunity(form: GetCommunity): Promise<GetCommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/community', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Edit a community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async editCommunity(form: EditCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/community', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* List communities, with various filters.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async listCommunities(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: ListCommunities
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<ListCommunitiesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/community/list', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Follow / subscribe to a community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async followCommunity(form: FollowCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/follow', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Block a community.
|
|
|
|
*/
|
2021-08-20 00:49:15 +00:00
|
|
|
async blockCommunity(form: BlockCommunity): Promise<BlockCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/block', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Delete a community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteCommunity(form: DeleteCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/delete', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* A moderator remove for a community.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async removeCommunity(form: RemoveCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/remove', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Transfer your community to an existing moderator.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async transferCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: TransferCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<GetCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/transfer', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Ban a user from a community.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async banFromCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: BanFromCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<BanFromCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/ban_user', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Add a moderator to your community.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async addModToCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: AddModToCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<AddModToCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/mod', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Create a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async createPost(form: CreatePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get / fetch a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getPost(form: GetPost): Promise<GetPostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/post', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Edit a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async editPost(form: EditPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/post', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Delete a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async deletePost(form: DeletePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/delete', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* A moderator remove for a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async removePost(form: RemovePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/remove', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* A moderator can lock a post ( IE disable new comments ).
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async lockPost(form: LockPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/lock', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* A moderator can sticky a post ( IE stick it to the top of a community ).
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async stickyPost(form: StickyPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/sticky', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get / fetch posts, with various filters.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getPosts(form: GetPosts): Promise<GetPostsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/post/list', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Like / vote on a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async likePost(form: CreatePostLike): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/like', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Save a post.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async savePost(form: SavePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/post/save', form);
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:38:49 +00:00
|
|
|
/**
|
|
|
|
* Report a post.
|
|
|
|
*/
|
|
|
|
async createPostReport(form: CreatePostReport): Promise<PostReportResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/post/report', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a post report. Only a mod can do this.
|
|
|
|
*/
|
|
|
|
async resolvePostReport(
|
|
|
|
form: ResolvePostReport
|
|
|
|
): Promise<PostReportResponse> {
|
|
|
|
return this.wrapper(HttpType.Put, '/post/report/resolve', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List post reports.
|
|
|
|
*/
|
|
|
|
async listPostReports(
|
|
|
|
form: ListPostReports
|
|
|
|
): Promise<ListPostReportsResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/post/report/list', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Fetch metadata for any given site.
|
|
|
|
*/
|
2021-08-19 15:14:39 +00:00
|
|
|
async getSiteMetadata(
|
|
|
|
form: GetSiteMetadata
|
|
|
|
): Promise<GetSiteMetadataResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/post/site_metadata', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Create a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async createComment(form: CreateComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Edit a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async editComment(form: EditComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/comment', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Delete a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteComment(form: DeleteComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/delete', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* A moderator remove for a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async removeComment(form: RemoveComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/remove', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Mark a comment as read.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async markCommentAsRead(form: MarkCommentAsRead): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Like / vote on a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async likeComment(form: CreateCommentLike): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/like', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Save a comment.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async saveComment(form: SaveComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/comment/save', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get / fetch comments.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getComments(form: GetComments): Promise<GetCommentsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/comment/list', form);
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:38:49 +00:00
|
|
|
/**
|
|
|
|
* Report a comment.
|
|
|
|
*/
|
|
|
|
async createCommentReport(
|
|
|
|
form: CreateCommentReport
|
|
|
|
): Promise<CommentReportResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/comment/report', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a comment report. Only a mod can do this.
|
|
|
|
*/
|
|
|
|
async resolveCommentReport(
|
|
|
|
form: ResolveCommentReport
|
|
|
|
): Promise<CommentReportResponse> {
|
|
|
|
return this.wrapper(HttpType.Put, '/comment/report/resolve', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List comment reports.
|
|
|
|
*/
|
|
|
|
async listCommentReports(
|
|
|
|
form: ListCommentReports
|
|
|
|
): Promise<ListCommentReportsResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/comment/report/list', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get / fetch private messages.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async getPrivateMessages(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: GetPrivateMessages
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessagesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/private_message/list', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Create a private message.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async createPrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: CreatePrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Edit a private message.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async editPrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: EditPrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Put, '/private_message', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Delete a private message.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async deletePrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: DeletePrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message/delete', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Mark a private message as read.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async markPrivateMessageAsRead(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: MarkPrivateMessageAsRead
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Register a new user.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async register(form: Register): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/register', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Log into lemmy.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async login(form: Login): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/login', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get the details for a person.
|
|
|
|
*/
|
2021-03-12 18:22:16 +00:00
|
|
|
async getPersonDetails(
|
|
|
|
form: GetPersonDetails
|
|
|
|
): Promise<GetPersonDetailsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/user', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get mentions for your user.
|
|
|
|
*/
|
2021-03-12 18:22:16 +00:00
|
|
|
async getPersonMentions(
|
|
|
|
form: GetPersonMentions
|
|
|
|
): Promise<GetPersonMentionsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/user/mention', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Mark a person mention as read.
|
|
|
|
*/
|
2021-03-12 18:22:16 +00:00
|
|
|
async markPersonMentionAsRead(
|
|
|
|
form: MarkPersonMentionAsRead
|
|
|
|
): Promise<PersonMentionResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/mention/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Get comment replies.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async getReplies(form: GetReplies): Promise<GetRepliesResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/user/replies', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Ban a person from your site.
|
|
|
|
*/
|
2021-03-12 18:22:16 +00:00
|
|
|
async banPerson(form: BanPerson): Promise<BanPersonResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/ban', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Block a person.
|
|
|
|
*/
|
2021-08-20 00:49:15 +00:00
|
|
|
async blockPerson(form: BlockPerson): Promise<BlockPersonResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/user/block', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Fetch a Captcha.
|
|
|
|
*/
|
2020-08-20 02:25:21 +00:00
|
|
|
async getCaptcha(): Promise<GetCaptchaResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/get_captcha', {});
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Delete your account.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteAccount(form: DeleteAccount): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/delete_account', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Reset your password.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async passwordReset(form: PasswordReset): Promise<PasswordResetResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/password_reset', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Change your password from an email / token based reset.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async passwordChange(form: PasswordChange): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/password_change', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Mark all replies as read.
|
|
|
|
*/
|
2021-03-21 19:51:40 +00:00
|
|
|
async markAllAsRead(form: MarkAllAsRead): Promise<GetRepliesResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/mark_all_as_read', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Save your user settings.
|
|
|
|
*/
|
2021-03-12 19:03:44 +00:00
|
|
|
async saveUserSettings(form: SaveUserSettings): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/user/save_user_settings', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Change your user password.
|
|
|
|
*/
|
2021-04-01 21:35:37 +00:00
|
|
|
async changePassword(form: ChangePassword): Promise<LoginResponse> {
|
|
|
|
return this.wrapper(HttpType.Put, '/user/change_password', form);
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:38:49 +00:00
|
|
|
/**
|
|
|
|
* Get counts for your reports
|
|
|
|
*/
|
|
|
|
async getReportCount(form: GetReportCount): Promise<GetReportCountResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/report_count', form);
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:42:40 +00:00
|
|
|
/**
|
|
|
|
* Get your unread counts
|
|
|
|
*/
|
|
|
|
async getUnreadCount(form: GetUnreadCount): Promise<GetUnreadCountResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/unread_count', form);
|
|
|
|
}
|
|
|
|
|
2021-08-23 01:01:40 +00:00
|
|
|
/**
|
|
|
|
* Add an admin to your site.
|
|
|
|
*/
|
2020-12-20 17:33:14 +00:00
|
|
|
async addAdmin(form: AddAdmin): Promise<AddAdminResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/admin/add', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildFullUrl(endpoint: string): string {
|
2021-03-29 14:29:27 +00:00
|
|
|
return `${this.apiUrl}${endpoint}`;
|
2020-08-20 02:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 19:21:26 +00:00
|
|
|
private async wrapper<ResponseType, MessageType>(
|
2020-08-20 02:25:21 +00:00
|
|
|
type_: HttpType,
|
|
|
|
endpoint: string,
|
|
|
|
form: MessageType
|
2020-12-20 19:21:26 +00:00
|
|
|
): Promise<ResponseType> {
|
2020-08-20 02:25:21 +00:00
|
|
|
if (type_ == HttpType.Get) {
|
2020-08-20 03:51:57 +00:00
|
|
|
let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
2020-08-20 02:25:21 +00:00
|
|
|
return fetch(getUrl, {
|
|
|
|
method: 'GET',
|
2020-11-12 19:42:31 +00:00
|
|
|
headers: this.headers,
|
2021-09-28 16:08:22 +00:00
|
|
|
}).then(d => d.json() as Promise<ResponseType>);
|
2020-08-20 02:25:21 +00:00
|
|
|
} else {
|
|
|
|
return fetch(this.buildFullUrl(endpoint), {
|
|
|
|
method: type_,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2020-11-12 19:42:31 +00:00
|
|
|
...this.headers,
|
2020-08-20 02:25:21 +00:00
|
|
|
},
|
|
|
|
body: JSON.stringify(form),
|
2021-09-28 16:08:22 +00:00
|
|
|
}).then(d => d.json() as Promise<ResponseType>);
|
2020-08-20 02:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeGetParams(p: any): string {
|
|
|
|
return Object.entries(p)
|
|
|
|
.map(kv => kv.map(encodeURIComponent).join('='))
|
|
|
|
.join('&');
|
|
|
|
}
|