Adding an HTTP client. Cleaning up Websocket client.

This commit is contained in:
Dessalines 2020-08-19 23:59:45 -04:00
parent 6b938415a0
commit 46043334c8
6 changed files with 176 additions and 476 deletions

View file

@ -94,7 +94,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
web::post().to(route_post::<MarkCommentAsRead>), web::post().to(route_post::<MarkCommentAsRead>),
) )
.route("/like", web::post().to(route_post::<CreateCommentLike>)) .route("/like", web::post().to(route_post::<CreateCommentLike>))
.route("/save", web::put().to(route_post::<SaveComment>)), .route("/save", web::put().to(route_post::<SaveComment>))
.route("/list", web::get().to(route_get::<GetComments>)),
) )
// Private Message // Private Message
.service( .service(
@ -136,6 +137,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
"/followed_communities", "/followed_communities",
web::get().to(route_get::<GetFollowedCommunities>), web::get().to(route_get::<GetFollowedCommunities>),
) )
.route("/join", web::post().to(route_post::<UserJoin>))
// Admin action. I don't like that it's in /user // Admin action. I don't like that it's in /user
.route("/ban", web::post().to(route_post::<BanUser>)) .route("/ban", web::post().to(route_post::<BanUser>))
// Account actions. I don't like that they're in /user maybe /accounts // Account actions. I don't like that they're in /user maybe /accounts

2
ui/package.json vendored
View file

@ -37,7 +37,7 @@
"inferno-router": "^7.4.2", "inferno-router": "^7.4.2",
"js-cookie": "^2.2.0", "js-cookie": "^2.2.0",
"jwt-decode": "^2.2.0", "jwt-decode": "^2.2.0",
"lemmy-js-client": "^1.0.2", "lemmy-js-client": "^1.0.8",
"markdown-it": "^11.0.0", "markdown-it": "^11.0.0",
"markdown-it-container": "^3.0.0", "markdown-it-container": "^3.0.0",
"markdown-it-emoji": "^1.4.0", "markdown-it-emoji": "^1.4.0",

View file

@ -131,7 +131,7 @@ test('Remove a comment from admin and community on the same instance', async ()
test('Remove a comment from admin and community on different instance', async () => { test('Remove a comment from admin and community on different instance', async () => {
let alphaUser = await registerUser(alpha); let alphaUser = await registerUser(alpha);
let newAlphaApi: API = { let newAlphaApi: API = {
url: alpha.url, client: alpha.client,
auth: alphaUser.jwt, auth: alphaUser.jwt,
}; };

View file

@ -1,5 +1,3 @@
import fetch from 'node-fetch';
import { import {
LoginForm, LoginForm,
LoginResponse, LoginResponse,
@ -20,15 +18,21 @@ import {
CommentForm, CommentForm,
DeleteCommentForm, DeleteCommentForm,
RemoveCommentForm, RemoveCommentForm,
SearchForm,
CommentResponse, CommentResponse,
CommunityForm, CommunityForm,
DeleteCommunityForm, DeleteCommunityForm,
RemoveCommunityForm, RemoveCommunityForm,
GetUserMentionsForm,
CommentLikeForm, CommentLikeForm,
CreatePostLikeForm, CreatePostLikeForm,
PrivateMessageForm, PrivateMessageForm,
EditPrivateMessageForm, EditPrivateMessageForm,
DeletePrivateMessageForm, DeletePrivateMessageForm,
GetFollowedCommunitiesForm,
GetPrivateMessagesForm,
GetSiteForm,
GetPostForm,
PrivateMessageResponse, PrivateMessageResponse,
PrivateMessagesResponse, PrivateMessagesResponse,
GetUserMentionsResponse, GetUserMentionsResponse,
@ -36,27 +40,25 @@ import {
SortType, SortType,
ListingType, ListingType,
GetSiteResponse, GetSiteResponse,
SearchType,
LemmyHttp,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
export interface API { export interface API {
url: string; client: LemmyHttp;
auth?: string; auth?: string;
} }
function apiUrl(api: API) {
return `${api.url}/api/v1`;
}
export let alpha: API = { export let alpha: API = {
url: 'http://localhost:8540', client: new LemmyHttp('http://localhost:8540/api/v1'),
}; };
export let beta: API = { export let beta: API = {
url: 'http://localhost:8550', client: new LemmyHttp('http://localhost:8550/api/v1'),
}; };
export let gamma: API = { export let gamma: API = {
url: 'http://localhost:8560', client: new LemmyHttp('http://localhost:8560/api/v1'),
}; };
export async function setupLogins() { export async function setupLogins() {
@ -65,39 +67,21 @@ export async function setupLogins() {
password: 'lemmy', password: 'lemmy',
}; };
let resA: Promise<LoginResponse> = fetch(`${apiUrl(alpha)}/user/login`, { let resA = alpha.client.login(form);
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(form),
}).then(d => d.json());
let formB = { let formB = {
username_or_email: 'lemmy_beta', username_or_email: 'lemmy_beta',
password: 'lemmy', password: 'lemmy',
}; };
let resB: Promise<LoginResponse> = fetch(`${apiUrl(beta)}/user/login`, { let resB = beta.client.login(formB);
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(formB),
}).then(d => d.json());
let formC = { let formG = {
username_or_email: 'lemmy_gamma', username_or_email: 'lemmy_gamma',
password: 'lemmy', password: 'lemmy',
}; };
let resG: Promise<LoginResponse> = fetch(`${apiUrl(gamma)}/user/login`, { let resG = gamma.client.login(formG);
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(formC),
}).then(d => d.json());
let res = await Promise.all([resA, resB, resG]); let res = await Promise.all([resA, resB, resG]);
alpha.auth = res[0].jwt; alpha.auth = res[0].jwt;
@ -110,40 +94,24 @@ export async function createPost(
community_id: number community_id: number
): Promise<PostResponse> { ): Promise<PostResponse> {
let name = 'A jest test post'; let name = 'A jest test post';
let postForm: PostForm = { let form: PostForm = {
name, name,
auth: api.auth, auth: api.auth,
community_id, community_id,
nsfw: false, nsfw: false,
}; };
return api.client.createPost(form);
let createPostRes: PostResponse = await fetch(`${apiUrl(api)}/post`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(postForm),
}).then(d => d.json());
return createPostRes;
} }
export async function updatePost(api: API, post: Post): Promise<PostResponse> { export async function updatePost(api: API, post: Post): Promise<PostResponse> {
let name = 'A jest test federated post, updated'; let name = 'A jest test federated post, updated';
let postForm: PostForm = { let form: PostForm = {
name, name,
edit_id: post.id, edit_id: post.id,
auth: api.auth, auth: api.auth,
nsfw: false, nsfw: false,
}; };
return api.client.editPost(form);
let updateResponse: PostResponse = await fetch(`${apiUrl(api)}/post`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(postForm),
}).then(d => d.json());
return updateResponse;
} }
export async function deletePost( export async function deletePost(
@ -151,20 +119,12 @@ export async function deletePost(
deleted: boolean, deleted: boolean,
post: Post post: Post
): Promise<PostResponse> { ): Promise<PostResponse> {
let deletePostForm: DeletePostForm = { let form: DeletePostForm = {
edit_id: post.id, edit_id: post.id,
deleted: deleted, deleted: deleted,
auth: api.auth, auth: api.auth,
}; };
return api.client.deletePost(form);
let deletePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(deletePostForm),
}).then(d => d.json());
return deletePostRes;
} }
export async function removePost( export async function removePost(
@ -172,20 +132,12 @@ export async function removePost(
removed: boolean, removed: boolean,
post: Post post: Post
): Promise<PostResponse> { ): Promise<PostResponse> {
let removePostForm: RemovePostForm = { let form: RemovePostForm = {
edit_id: post.id, edit_id: post.id,
removed, removed,
auth: api.auth, auth: api.auth,
}; };
return api.client.removePost(form);
let removePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/remove`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(removePostForm),
}).then(d => d.json());
return removePostRes;
} }
export async function stickyPost( export async function stickyPost(
@ -193,21 +145,12 @@ export async function stickyPost(
stickied: boolean, stickied: boolean,
post: Post post: Post
): Promise<PostResponse> { ): Promise<PostResponse> {
let stickyPostForm: StickyPostForm = { let form: StickyPostForm = {
edit_id: post.id, edit_id: post.id,
stickied, stickied,
auth: api.auth, auth: api.auth,
}; };
return api.client.stickyPost(form);
let stickyRes: PostResponse = await fetch(`${apiUrl(api)}/post/sticky`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(stickyPostForm),
}).then(d => d.json());
return stickyRes;
} }
export async function lockPost( export async function lockPost(
@ -215,57 +158,46 @@ export async function lockPost(
locked: boolean, locked: boolean,
post: Post post: Post
): Promise<PostResponse> { ): Promise<PostResponse> {
let lockPostForm: LockPostForm = { let form: LockPostForm = {
edit_id: post.id, edit_id: post.id,
locked, locked,
auth: api.auth, auth: api.auth,
}; };
return api.client.lockPost(form);
let lockRes: PostResponse = await fetch(`${apiUrl(api)}/post/lock`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(lockPostForm),
}).then(d => d.json());
return lockRes;
} }
export async function searchPost( export async function searchPost(
api: API, api: API,
post: Post post: Post
): Promise<SearchResponse> { ): Promise<SearchResponse> {
let searchUrl = `${apiUrl(api)}/search?q=${post.ap_id}&type_=All&sort=TopAll`; let form: SearchForm = {
let searchResponse: SearchResponse = await fetch(searchUrl, { q: post.ap_id,
method: 'GET', type_: SearchType.All,
}).then(d => d.json()); sort: SortType.TopAll,
return searchResponse; };
return api.client.search(form);
} }
export async function getPost( export async function getPost(
api: API, api: API,
post_id: number post_id: number
): Promise<GetPostResponse> { ): Promise<GetPostResponse> {
let getPostUrl = `${apiUrl(api)}/post?id=${post_id}`; let form: GetPostForm = {
let getPostRes: GetPostResponse = await fetch(getPostUrl, { id: post_id,
method: 'GET', };
}).then(d => d.json()); return api.client.getPost(form);
return getPostRes;
} }
export async function searchComment( export async function searchComment(
api: API, api: API,
comment: Comment comment: Comment
): Promise<SearchResponse> { ): Promise<SearchResponse> {
let searchUrl = `${apiUrl(api)}/search?q=${ let form: SearchForm = {
comment.ap_id q: comment.ap_id,
}&type_=All&sort=TopAll`; type_: SearchType.All,
let searchResponse: SearchResponse = await fetch(searchUrl, { sort: SortType.TopAll,
method: 'GET', };
}).then(d => d.json()); return api.client.search(form);
return searchResponse;
} }
export async function searchForBetaCommunity( export async function searchForBetaCommunity(
@ -273,14 +205,12 @@ export async function searchForBetaCommunity(
): Promise<SearchResponse> { ): Promise<SearchResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha // Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url // Use short-hand search url
let searchUrl = `${apiUrl( let form: SearchForm = {
api q: '!main@lemmy-beta:8550',
)}/search?q=!main@lemmy-beta:8550&type_=All&sort=TopAll`; type_: SearchType.All,
sort: SortType.TopAll,
let searchResponse: SearchResponse = await fetch(searchUrl, { };
method: 'GET', return api.client.search(form);
}).then(d => d.json());
return searchResponse;
} }
export async function searchForUser( export async function searchForUser(
@ -289,14 +219,12 @@ export async function searchForUser(
): Promise<SearchResponse> { ): Promise<SearchResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha // Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url // Use short-hand search url
let searchUrl = `${apiUrl( let form: SearchForm = {
api q: apShortname,
)}/search?q=${apShortname}&type_=All&sort=TopAll`; type_: SearchType.All,
sort: SortType.TopAll,
let searchResponse: SearchResponse = await fetch(searchUrl, { };
method: 'GET', return api.client.search(form);
}).then(d => d.json());
return searchResponse;
} }
export async function followCommunity( export async function followCommunity(
@ -304,41 +232,21 @@ export async function followCommunity(
follow: boolean, follow: boolean,
community_id: number community_id: number
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let followForm: FollowCommunityForm = { let form: FollowCommunityForm = {
community_id, community_id,
follow, follow,
auth: api.auth, auth: api.auth,
}; };
return api.client.followCommunity(form);
let followRes: CommunityResponse = await fetch(
`${apiUrl(api)}/community/follow`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(followForm),
}
)
.then(d => d.json())
.catch(_e => {});
return followRes;
} }
export async function checkFollowedCommunities( export async function checkFollowedCommunities(
api: API api: API
): Promise<GetFollowedCommunitiesResponse> { ): Promise<GetFollowedCommunitiesResponse> {
let followedCommunitiesUrl = `${apiUrl( let form: GetFollowedCommunitiesForm = {
api auth: api.auth,
)}/user/followed_communities?&auth=${api.auth}`; };
let followedCommunitiesRes: GetFollowedCommunitiesResponse = await fetch( return api.client.getFollowedCommunities(form);
followedCommunitiesUrl,
{
method: 'GET',
}
).then(d => d.json());
return followedCommunitiesRes;
} }
export async function likePost( export async function likePost(
@ -346,21 +254,13 @@ export async function likePost(
score: number, score: number,
post: Post post: Post
): Promise<PostResponse> { ): Promise<PostResponse> {
let likePostForm: CreatePostLikeForm = { let form: CreatePostLikeForm = {
post_id: post.id, post_id: post.id,
score: score, score: score,
auth: api.auth, auth: api.auth,
}; };
let likePostRes: PostResponse = await fetch(`${apiUrl(api)}/post/like`, { return api.client.likePost(form);
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(likePostForm),
}).then(d => d.json());
return likePostRes;
} }
export async function createComment( export async function createComment(
@ -369,21 +269,13 @@ export async function createComment(
parent_id?: number, parent_id?: number,
content = 'a jest test comment' content = 'a jest test comment'
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let commentForm: CommentForm = { let form: CommentForm = {
content, content,
post_id, post_id,
parent_id, parent_id,
auth: api.auth, auth: api.auth,
}; };
return api.client.createComment(form);
let createResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(commentForm),
}).then(d => d.json());
return createResponse;
} }
export async function updateComment( export async function updateComment(
@ -391,20 +283,12 @@ export async function updateComment(
edit_id: number, edit_id: number,
content = 'A jest test federated comment update' content = 'A jest test federated comment update'
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let commentForm: CommentForm = { let form: CommentForm = {
content, content,
edit_id, edit_id,
auth: api.auth, auth: api.auth,
}; };
return api.client.editComment(form);
let updateResponse: CommentResponse = await fetch(`${apiUrl(api)}/comment`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(commentForm),
}).then(d => d.json());
return updateResponse;
} }
export async function deleteComment( export async function deleteComment(
@ -412,23 +296,12 @@ export async function deleteComment(
deleted: boolean, deleted: boolean,
edit_id: number edit_id: number
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let deleteCommentForm: DeleteCommentForm = { let form: DeleteCommentForm = {
edit_id, edit_id,
deleted, deleted,
auth: api.auth, auth: api.auth,
}; };
return api.client.deleteComment(form);
let deleteCommentRes: CommentResponse = await fetch(
`${apiUrl(api)}/comment/delete`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(deleteCommentForm),
}
).then(d => d.json());
return deleteCommentRes;
} }
export async function removeComment( export async function removeComment(
@ -436,33 +309,21 @@ export async function removeComment(
removed: boolean, removed: boolean,
edit_id: number edit_id: number
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let removeCommentForm: RemoveCommentForm = { let form: RemoveCommentForm = {
edit_id, edit_id,
removed, removed,
auth: api.auth, auth: api.auth,
}; };
return api.client.removeComment(form);
let removeCommentRes: CommentResponse = await fetch(
`${apiUrl(api)}/comment/remove`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(removeCommentForm),
}
).then(d => d.json());
return removeCommentRes;
} }
export async function getMentions(api: API): Promise<GetUserMentionsResponse> { export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
let getMentionUrl = `${apiUrl( let form: GetUserMentionsForm = {
api sort: SortType.New,
)}/user/mention?sort=New&unread_only=false&auth=${api.auth}`; unread_only: false,
let getMentionsRes: GetUserMentionsResponse = await fetch(getMentionUrl, { auth: api.auth,
method: 'GET', };
}).then(d => d.json()); return api.client.getUserMentions(form);
return getMentionsRes;
} }
export async function likeComment( export async function likeComment(
@ -470,48 +331,26 @@ export async function likeComment(
score: number, score: number,
comment: Comment comment: Comment
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let likeCommentForm: CommentLikeForm = { let form: CommentLikeForm = {
comment_id: comment.id, comment_id: comment.id,
score, score,
auth: api.auth, auth: api.auth,
}; };
return api.client.likeComment(form);
let likeCommentRes: CommentResponse = await fetch(
`${apiUrl(api)}/comment/like`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(likeCommentForm),
}
).then(d => d.json());
return likeCommentRes;
} }
export async function createCommunity( export async function createCommunity(
api: API, api: API,
name_: string = randomString(5) name_: string = randomString(5)
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let communityForm: CommunityForm = { let form: CommunityForm = {
name: name_, name: name_,
title: name_, title: name_,
category_id: 1, category_id: 1,
nsfw: false, nsfw: false,
auth: api.auth, auth: api.auth,
}; };
return api.client.createCommunity(form);
let createCommunityRes: CommunityResponse = await fetch(
`${apiUrl(api)}/community`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(communityForm),
}
).then(d => d.json());
return createCommunityRes;
} }
export async function deleteCommunity( export async function deleteCommunity(
@ -519,23 +358,12 @@ export async function deleteCommunity(
deleted: boolean, deleted: boolean,
edit_id: number edit_id: number
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let deleteCommunityForm: DeleteCommunityForm = { let form: DeleteCommunityForm = {
edit_id, edit_id,
deleted, deleted,
auth: api.auth, auth: api.auth,
}; };
return api.client.deleteCommunity(form);
let deleteResponse: CommunityResponse = await fetch(
`${apiUrl(api)}/community/delete`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(deleteCommunityForm),
}
).then(d => d.json());
return deleteResponse;
} }
export async function removeCommunity( export async function removeCommunity(
@ -543,23 +371,12 @@ export async function removeCommunity(
removed: boolean, removed: boolean,
edit_id: number edit_id: number
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let removeCommunityForm: RemoveCommunityForm = { let form: RemoveCommunityForm = {
edit_id, edit_id,
removed, removed,
auth: api.auth, auth: api.auth,
}; };
return api.client.removeCommunity(form);
let removeResponse: CommunityResponse = await fetch(
`${apiUrl(api)}/community/remove`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(removeCommunityForm),
}
).then(d => d.json());
return removeResponse;
} }
export async function createPrivateMessage( export async function createPrivateMessage(
@ -567,23 +384,12 @@ export async function createPrivateMessage(
recipient_id: number recipient_id: number
): Promise<PrivateMessageResponse> { ): Promise<PrivateMessageResponse> {
let content = 'A jest test federated private message'; let content = 'A jest test federated private message';
let privateMessageForm: PrivateMessageForm = { let form: PrivateMessageForm = {
content, content,
recipient_id, recipient_id,
auth: api.auth, auth: api.auth,
}; };
return api.client.createPrivateMessage(form);
let createRes: PrivateMessageResponse = await fetch(
`${apiUrl(api)}/private_message`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(privateMessageForm),
}
).then(d => d.json());
return createRes;
} }
export async function updatePrivateMessage( export async function updatePrivateMessage(
@ -591,23 +397,12 @@ export async function updatePrivateMessage(
edit_id: number edit_id: number
): Promise<PrivateMessageResponse> { ): Promise<PrivateMessageResponse> {
let updatedContent = 'A jest test federated private message edited'; let updatedContent = 'A jest test federated private message edited';
let updatePrivateMessageForm: EditPrivateMessageForm = { let form: EditPrivateMessageForm = {
content: updatedContent, content: updatedContent,
edit_id, edit_id,
auth: api.auth, auth: api.auth,
}; };
return api.client.editPrivateMessage(form);
let updateRes: PrivateMessageResponse = await fetch(
`${apiUrl(api)}/private_message`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(updatePrivateMessageForm),
}
).then(d => d.json());
return updateRes;
} }
export async function deletePrivateMessage( export async function deletePrivateMessage(
@ -615,50 +410,26 @@ export async function deletePrivateMessage(
deleted: boolean, deleted: boolean,
edit_id: number edit_id: number
): Promise<PrivateMessageResponse> { ): Promise<PrivateMessageResponse> {
let deletePrivateMessageForm: DeletePrivateMessageForm = { let form: DeletePrivateMessageForm = {
deleted, deleted,
edit_id, edit_id,
auth: api.auth, auth: api.auth,
}; };
return api.client.deletePrivateMessage(form);
let deleteRes: PrivateMessageResponse = await fetch(
`${apiUrl(api)}/private_message/delete`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(deletePrivateMessageForm),
}
).then(d => d.json());
return deleteRes;
} }
export async function registerUser( export async function registerUser(
api: API, api: API,
username: string = randomString(5) username: string = randomString(5)
): Promise<LoginResponse> { ): Promise<LoginResponse> {
let registerForm: RegisterForm = { let form: RegisterForm = {
username, username,
password: 'test', password: 'test',
password_verify: 'test', password_verify: 'test',
admin: false, admin: false,
show_nsfw: true, show_nsfw: true,
}; };
return api.client.register(form);
let registerRes: Promise<LoginResponse> = fetch(
`${apiUrl(api)}/user/register`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(registerForm),
}
).then(d => d.json());
return registerRes;
} }
export async function saveUserSettingsBio( export async function saveUserSettingsBio(
@ -676,46 +447,28 @@ export async function saveUserSettingsBio(
bio: 'a changed bio', bio: 'a changed bio',
auth, auth,
}; };
return api.client.saveUserSettings(form);
let res: Promise<LoginResponse> = fetch(
`${apiUrl(api)}/user/save_user_settings`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: wrapper(form),
}
).then(d => d.json());
return res;
} }
export async function getSite( export async function getSite(
api: API, api: API,
auth: string auth: string
): Promise<GetSiteResponse> { ): Promise<GetSiteResponse> {
let siteUrl = `${apiUrl(api)}/site?auth=${auth}`; let form: GetSiteForm = {
auth,
let res: GetSiteResponse = await fetch(siteUrl, { };
method: 'GET', return api.client.getSite(form);
}).then(d => d.json());
return res;
} }
export async function listPrivateMessages( export async function listPrivateMessages(
api: API api: API
): Promise<PrivateMessagesResponse> { ): Promise<PrivateMessagesResponse> {
let getPrivateMessagesUrl = `${apiUrl(api)}/private_message/list?auth=${ let form: GetPrivateMessagesForm = {
api.auth auth: api.auth,
}&unread_only=false&limit=999`; unread_only: false,
limit: 999,
let getPrivateMessagesRes: PrivateMessagesResponse = await fetch( };
getPrivateMessagesUrl, return api.client.getPrivateMessages(form);
{
method: 'GET',
}
).then(d => d.json());
return getPrivateMessagesRes;
} }
export async function unfollowRemotes( export async function unfollowRemotes(

View file

@ -1,63 +1,6 @@
import { wsUri } from '../env'; import { wsUri } from '../env';
import { import {
wsSendSearch, LemmyWebsocket,
wsSendGetPost,
wsSendBanUser,
wsSendGetSite,
wsSendUserJoin,
wsSendRegister,
wsSendLogin,
wsSendGetPosts,
wsSendLikePost,
wsSendEditPost,
wsSendLockPost,
wsSendSavePost,
wsSendAddAdmin,
wsSendEditSite,
wsSendGetModlog,
wsSendGetCaptcha,
wsSendCreatePost,
wsSendDeletePost,
wsSendRemovePost,
wsSendStickyPost,
wsSendGetReplies,
wsSendCreateSite,
wsSendEditComment,
wsSendLikeComment,
wsSendSaveComment,
wsSendGetComments,
wsSendGetCommunity,
wsSendTransferSite,
wsSendEditCommunity,
wsSendCreateComment,
wsSendDeleteComment,
wsSendRemoveComment,
wsSendGetSiteConfig,
wsSendMarkAllAsRead,
wsSendDeleteAccount,
wsSendPasswordReset,
wsSendPasswordChange,
wsSendListCategories,
wsSendGetUserDetails,
wsSendGetUserMentions,
wsSendSaveSiteConfig,
wsSendDeleteCommunity,
wsSendCreateCommunity,
wsSendRemoveCommunity,
wsSendFollowCommunity,
wsSendListCommunities,
wsSendBanFromCommunity,
wsSendSaveUserSettings,
wsSendMarkCommentAsRead,
wsSendGetFollowedCommunities,
wsSendAddModToCommunity,
wsSendTransferCommunity,
wsSendMarkUserMentionAsRead,
wsSendDeletePrivateMessage,
wsSendEditPrivateMessage,
wsSendGetPrivateMessages,
wsSendCreatePrivateMessage,
wsSendMarkPrivateMessageAsRead,
LoginForm, LoginForm,
RegisterForm, RegisterForm,
CommunityForm, CommunityForm,
@ -110,6 +53,7 @@ import {
GetSiteConfig, GetSiteConfig,
GetSiteForm, GetSiteForm,
SiteConfigForm, SiteConfigForm,
MarkAllAsReadForm,
WebSocketJsonResponse, WebSocketJsonResponse,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { UserService } from './'; import { UserService } from './';
@ -126,6 +70,7 @@ export class WebSocketService {
public admins: Array<UserView>; public admins: Array<UserView>;
public banned: Array<UserView>; public banned: Array<UserView>;
private client = new LemmyWebsocket();
private constructor() { private constructor() {
this.ws = new ReconnectingWebSocket(wsUri); this.ws = new ReconnectingWebSocket(wsUri);
@ -156,287 +101,287 @@ export class WebSocketService {
public userJoin() { public userJoin() {
let form: UserJoinForm = { auth: UserService.Instance.auth }; let form: UserJoinForm = { auth: UserService.Instance.auth };
this.ws.send(wsSendUserJoin(form)); this.ws.send(this.client.userJoin(form));
} }
public login(form: LoginForm) { public login(form: LoginForm) {
this.ws.send(wsSendLogin(form)); this.ws.send(this.client.login(form));
} }
public register(form: RegisterForm) { public register(form: RegisterForm) {
this.ws.send(wsSendRegister(form)); this.ws.send(this.client.register(form));
} }
public getCaptcha() { public getCaptcha() {
this.ws.send(wsSendGetCaptcha()); this.ws.send(this.client.getCaptcha());
} }
public createCommunity(form: CommunityForm) { public createCommunity(form: CommunityForm) {
this.setAuth(form); // TODO all these setauths at some point would be good to make required this.setAuth(form); // TODO all these setauths at some point would be good to make required
this.ws.send(wsSendCreateCommunity(form)); this.ws.send(this.client.createCommunity(form));
} }
public editCommunity(form: CommunityForm) { public editCommunity(form: CommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendEditCommunity(form)); this.ws.send(this.client.editCommunity(form));
} }
public deleteCommunity(form: DeleteCommunityForm) { public deleteCommunity(form: DeleteCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendDeleteCommunity(form)); this.ws.send(this.client.deleteCommunity(form));
} }
public removeCommunity(form: RemoveCommunityForm) { public removeCommunity(form: RemoveCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendRemoveCommunity(form)); this.ws.send(this.client.removeCommunity(form));
} }
public followCommunity(form: FollowCommunityForm) { public followCommunity(form: FollowCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendFollowCommunity(form)); this.ws.send(this.client.followCommunity(form));
} }
public listCommunities(form: ListCommunitiesForm) { public listCommunities(form: ListCommunitiesForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendListCommunities(form)); this.ws.send(this.client.listCommunities(form));
} }
public getFollowedCommunities() { public getFollowedCommunities() {
let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth }; let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
this.ws.send(wsSendGetFollowedCommunities(form)); this.ws.send(this.client.getFollowedCommunities(form));
} }
public listCategories() { public listCategories() {
this.ws.send(wsSendListCategories()); this.ws.send(this.client.listCategories());
} }
public createPost(form: PostForm) { public createPost(form: PostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendCreatePost(form)); this.ws.send(this.client.createPost(form));
} }
public getPost(form: GetPostForm) { public getPost(form: GetPostForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetPost(form)); this.ws.send(this.client.getPost(form));
} }
public getCommunity(form: GetCommunityForm) { public getCommunity(form: GetCommunityForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetCommunity(form)); this.ws.send(this.client.getCommunity(form));
} }
public createComment(form: CommentForm) { public createComment(form: CommentForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendCreateComment(form)); this.ws.send(this.client.createComment(form));
} }
public editComment(form: CommentForm) { public editComment(form: CommentForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendEditComment(form)); this.ws.send(this.client.editComment(form));
} }
public deleteComment(form: DeleteCommentForm) { public deleteComment(form: DeleteCommentForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendDeleteComment(form)); this.ws.send(this.client.deleteComment(form));
} }
public removeComment(form: RemoveCommentForm) { public removeComment(form: RemoveCommentForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendRemoveComment(form)); this.ws.send(this.client.removeComment(form));
} }
public markCommentAsRead(form: MarkCommentAsReadForm) { public markCommentAsRead(form: MarkCommentAsReadForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendMarkCommentAsRead(form)); this.ws.send(this.client.markCommentAsRead(form));
} }
public likeComment(form: CommentLikeForm) { public likeComment(form: CommentLikeForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendLikeComment(form)); this.ws.send(this.client.likeComment(form));
} }
public saveComment(form: SaveCommentForm) { public saveComment(form: SaveCommentForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendSaveComment(form)); this.ws.send(this.client.saveComment(form));
} }
public getPosts(form: GetPostsForm) { public getPosts(form: GetPostsForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetPosts(form)); this.ws.send(this.client.getPosts(form));
} }
public getComments(form: GetCommentsForm) { public getComments(form: GetCommentsForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetComments(form)); this.ws.send(this.client.getComments(form));
} }
public likePost(form: CreatePostLikeForm) { public likePost(form: CreatePostLikeForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendLikePost(form)); this.ws.send(this.client.likePost(form));
} }
public editPost(form: PostForm) { public editPost(form: PostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendEditPost(form)); this.ws.send(this.client.editPost(form));
} }
public deletePost(form: DeletePostForm) { public deletePost(form: DeletePostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendDeletePost(form)); this.ws.send(this.client.deletePost(form));
} }
public removePost(form: RemovePostForm) { public removePost(form: RemovePostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendRemovePost(form)); this.ws.send(this.client.removePost(form));
} }
public lockPost(form: LockPostForm) { public lockPost(form: LockPostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendLockPost(form)); this.ws.send(this.client.lockPost(form));
} }
public stickyPost(form: StickyPostForm) { public stickyPost(form: StickyPostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendStickyPost(form)); this.ws.send(this.client.stickyPost(form));
} }
public savePost(form: SavePostForm) { public savePost(form: SavePostForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendSavePost(form)); this.ws.send(this.client.savePost(form));
} }
public banFromCommunity(form: BanFromCommunityForm) { public banFromCommunity(form: BanFromCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendBanFromCommunity(form)); this.ws.send(this.client.banFromCommunity(form));
} }
public addModToCommunity(form: AddModToCommunityForm) { public addModToCommunity(form: AddModToCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendAddModToCommunity(form)); this.ws.send(this.client.addModToCommunity(form));
} }
public transferCommunity(form: TransferCommunityForm) { public transferCommunity(form: TransferCommunityForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendTransferCommunity(form)); this.ws.send(this.client.transferCommunity(form));
} }
public transferSite(form: TransferSiteForm) { public transferSite(form: TransferSiteForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendTransferSite(form)); this.ws.send(this.client.transferSite(form));
} }
public banUser(form: BanUserForm) { public banUser(form: BanUserForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendBanUser(form)); this.ws.send(this.client.banUser(form));
} }
public addAdmin(form: AddAdminForm) { public addAdmin(form: AddAdminForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendAddAdmin(form)); this.ws.send(this.client.addAdmin(form));
} }
public getUserDetails(form: GetUserDetailsForm) { public getUserDetails(form: GetUserDetailsForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetUserDetails(form)); this.ws.send(this.client.getUserDetails(form));
} }
public getReplies(form: GetRepliesForm) { public getReplies(form: GetRepliesForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendGetReplies(form)); this.ws.send(this.client.getReplies(form));
} }
public getUserMentions(form: GetUserMentionsForm) { public getUserMentions(form: GetUserMentionsForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendGetUserMentions(form)); this.ws.send(this.client.getUserMentions(form));
} }
public markUserMentionAsRead(form: MarkUserMentionAsReadForm) { public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendMarkUserMentionAsRead(form)); this.ws.send(this.client.markUserMentionAsRead(form));
} }
public getModlog(form: GetModlogForm) { public getModlog(form: GetModlogForm) {
this.ws.send(wsSendGetModlog(form)); this.ws.send(this.client.getModlog(form));
} }
public createSite(form: SiteForm) { public createSite(form: SiteForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendCreateSite(form)); this.ws.send(this.client.createSite(form));
} }
public editSite(form: SiteForm) { public editSite(form: SiteForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendEditSite(form)); this.ws.send(this.client.editSite(form));
} }
public getSite(form: GetSiteForm = {}) { public getSite(form: GetSiteForm = {}) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendGetSite(form)); this.ws.send(this.client.getSite(form));
} }
public getSiteConfig() { public getSiteConfig() {
let form: GetSiteConfig = {}; let form: GetSiteConfig = {};
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendGetSiteConfig(form)); this.ws.send(this.client.getSiteConfig(form));
} }
public search(form: SearchForm) { public search(form: SearchForm) {
this.setAuth(form, false); this.setAuth(form, false);
this.ws.send(wsSendSearch(form)); this.ws.send(this.client.search(form));
} }
public markAllAsRead() { public markAllAsRead() {
let form = {}; let form: MarkAllAsReadForm;
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendMarkAllAsRead(form)); this.ws.send(this.client.markAllAsRead(form));
} }
public saveUserSettings(form: UserSettingsForm) { public saveUserSettings(form: UserSettingsForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendSaveUserSettings(form)); this.ws.send(this.client.saveUserSettings(form));
} }
public deleteAccount(form: DeleteAccountForm) { public deleteAccount(form: DeleteAccountForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendDeleteAccount(form)); this.ws.send(this.client.deleteAccount(form));
} }
public passwordReset(form: PasswordResetForm) { public passwordReset(form: PasswordResetForm) {
this.ws.send(wsSendPasswordReset(form)); this.ws.send(this.client.passwordReset(form));
} }
public passwordChange(form: PasswordChangeForm) { public passwordChange(form: PasswordChangeForm) {
this.ws.send(wsSendPasswordChange(form)); this.ws.send(this.client.passwordChange(form));
} }
public createPrivateMessage(form: PrivateMessageForm) { public createPrivateMessage(form: PrivateMessageForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendCreatePrivateMessage(form)); this.ws.send(this.client.createPrivateMessage(form));
} }
public editPrivateMessage(form: EditPrivateMessageForm) { public editPrivateMessage(form: EditPrivateMessageForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendEditPrivateMessage(form)); this.ws.send(this.client.editPrivateMessage(form));
} }
public deletePrivateMessage(form: DeletePrivateMessageForm) { public deletePrivateMessage(form: DeletePrivateMessageForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendDeletePrivateMessage(form)); this.ws.send(this.client.deletePrivateMessage(form));
} }
public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) { public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendMarkPrivateMessageAsRead(form)); this.ws.send(this.client.markPrivateMessageAsRead(form));
} }
public getPrivateMessages(form: GetPrivateMessagesForm) { public getPrivateMessages(form: GetPrivateMessagesForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendGetPrivateMessages(form)); this.ws.send(this.client.getPrivateMessages(form));
} }
public saveSiteConfig(form: SiteConfigForm) { public saveSiteConfig(form: SiteConfigForm) {
this.setAuth(form); this.setAuth(form);
this.ws.send(wsSendSaveSiteConfig(form)); this.ws.send(this.client.saveSiteConfig(form));
} }
private setAuth(obj: any, throwErr: boolean = true) { private setAuth(obj: any, throwErr: boolean = true) {

8
ui/yarn.lock vendored
View file

@ -4938,10 +4938,10 @@ lego-api@^1.0.7:
dependencies: dependencies:
chain-able "^3.0.0" chain-able "^3.0.0"
lemmy-js-client@^1.0.2: lemmy-js-client@^1.0.8:
version "1.0.2" version "1.0.8"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-1.0.2.tgz#6345a40ebc9218d4ad9a32cdbafdd29140e3768a" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-1.0.8.tgz#98e34c8e3cd07427f883f60fad376dc4d6f46e7f"
integrity sha512-m4ZayBvyOF/v7M+MzPNROtrc54OHAL9+7qtxkF0lbsiGwx2vDcPDCd1Nw/tg+i3qheCvldYAdIGP4tD/zwiMgg== integrity sha512-YZxD3+8RGz7cRKdI8EIe5iQqQIMm5WzdNz6zZ7/CdkMtXUv6YuMOEv8HLTvBoGuaWIJwlMJ+23NIarxlT26IEw==
leven@^3.1.0: leven@^3.1.0:
version "3.1.0" version "3.1.0"