Merge branch 'main' into comment-fix

This commit is contained in:
Dessalines 2023-09-21 11:20:49 -04:00 committed by GitHub
commit b16661a17d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
109 changed files with 898 additions and 1141 deletions

1
Cargo.lock generated
View file

@ -2894,6 +2894,7 @@ dependencies = [
"rustls 0.21.3", "rustls 0.21.3",
"serde", "serde",
"serde_json", "serde_json",
"serial_test",
"tokio", "tokio",
"tokio-postgres", "tokio-postgres",
"tokio-postgres-rustls", "tokio-postgres-rustls",

View file

@ -166,5 +166,6 @@ tokio-postgres-rustls = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
prometheus = { version = "0.13.3", features = ["process"], optional = true } prometheus = { version = "0.13.3", features = ["process"], optional = true }
actix-web-prom = { version = "0.6.0", optional = true } actix-web-prom = { version = "0.6.0", optional = true }
serial_test = { workspace = true }
clap = { version = "4.3.19", features = ["derive"] } clap = { version = "4.3.19", features = ["derive"] }
lemmy_federate = { version = "0.18.4", path = "crates/federate" } lemmy_federate = { version = "0.18.4", path = "crates/federate" }

View file

@ -19,7 +19,7 @@
"eslint": "^8.40.0", "eslint": "^8.40.0",
"eslint-plugin-prettier": "^4.0.0", "eslint-plugin-prettier": "^4.0.0",
"jest": "^29.5.0", "jest": "^29.5.0",
"lemmy-js-client": "0.19.0-rc.5", "lemmy-js-client": "0.19.0-rc.12",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"ts-jest": "^29.1.0", "ts-jest": "^29.1.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"

View file

@ -6,6 +6,8 @@ set -e
export RUST_BACKTRACE=1 export RUST_BACKTRACE=1
export RUST_LOG="warn,lemmy_server=debug,lemmy_federate=debug,lemmy_api=debug,lemmy_api_common=debug,lemmy_api_crud=debug,lemmy_apub=debug,lemmy_db_schema=debug,lemmy_db_views=debug,lemmy_db_views_actor=debug,lemmy_db_views_moderator=debug,lemmy_routes=debug,lemmy_utils=debug,lemmy_websocket=debug" export RUST_LOG="warn,lemmy_server=debug,lemmy_federate=debug,lemmy_api=debug,lemmy_api_common=debug,lemmy_api_crud=debug,lemmy_apub=debug,lemmy_db_schema=debug,lemmy_db_views=debug,lemmy_db_views_actor=debug,lemmy_db_views_moderator=debug,lemmy_routes=debug,lemmy_utils=debug,lemmy_websocket=debug"
export LEMMY_TEST_FAST_FEDERATION=1 # by default, the persistent federation queue has delays in the scale of 30s-5min
for INSTANCE in lemmy_alpha lemmy_beta lemmy_gamma lemmy_delta lemmy_epsilon; do for INSTANCE in lemmy_alpha lemmy_beta lemmy_gamma lemmy_delta lemmy_epsilon; do
echo "DB URL: ${LEMMY_DATABASE_URL} INSTANCE: $INSTANCE" echo "DB URL: ${LEMMY_DATABASE_URL} INSTANCE: $INSTANCE"
psql "${LEMMY_DATABASE_URL}/lemmy" -c "DROP DATABASE IF EXISTS $INSTANCE" psql "${LEMMY_DATABASE_URL}/lemmy" -c "DROP DATABASE IF EXISTS $INSTANCE"
@ -34,30 +36,30 @@ echo "$PWD"
echo "start alpha" echo "start alpha"
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \
target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 & target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 &
echo "start beta" echo "start beta"
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_beta.hjson \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_beta.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_beta" \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_beta" \
target/lemmy_server >/tmp/lemmy_beta.out 2>&1 & target/lemmy_server >/tmp/lemmy_beta.out 2>&1 &
echo "start gamma" echo "start gamma"
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_gamma.hjson \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_gamma.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_gamma" \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_gamma" \
target/lemmy_server >/tmp/lemmy_gamma.out 2>&1 & target/lemmy_server >/tmp/lemmy_gamma.out 2>&1 &
echo "start delta" echo "start delta"
# An instance with only an allowlist for beta # An instance with only an allowlist for beta
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_delta.hjson \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_delta.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_delta" \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_delta" \
target/lemmy_server >/tmp/lemmy_delta.out 2>&1 & target/lemmy_server >/tmp/lemmy_delta.out 2>&1 &
echo "start epsilon" echo "start epsilon"
# An instance who has a blocklist, with lemmy-alpha blocked # An instance who has a blocklist, with lemmy-alpha blocked
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_epsilon" \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_epsilon" \
target/lemmy_server >/tmp/lemmy_epsilon.out 2>&1 & target/lemmy_server >/tmp/lemmy_epsilon.out 2>&1 &
echo "wait for all instances to start" echo "wait for all instances to start"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-alpha:8541/api/v3/site')" != "200" ]]; do sleep 1; done while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-alpha:8541/api/v3/site')" != "200" ]]; do sleep 1; done

View file

@ -24,7 +24,6 @@ import {
reportComment, reportComment,
listCommentReports, listCommentReports,
randomString, randomString,
API,
unfollows, unfollows,
getComments, getComments,
getCommentParentId, getCommentParentId,
@ -34,19 +33,21 @@ import {
getUnreadCount, getUnreadCount,
waitUntil, waitUntil,
delay, delay,
waitForPost,
alphaUrl,
} from "./shared"; } from "./shared";
import { CommentView } from "lemmy-js-client/dist/types/CommentView"; import { CommentView } from "lemmy-js-client/dist/types/CommentView";
import { CommunityView } from "lemmy-js-client";
import { LemmyHttp } from "lemmy-js-client";
let betaCommunity: CommunityView | undefined;
let postOnAlphaRes: PostResponse; let postOnAlphaRes: PostResponse;
beforeAll(async () => { beforeAll(async () => {
await setupLogins(); await setupLogins();
await unfollows(); await unfollows();
await followBeta(alpha); await Promise.all([followBeta(alpha), followBeta(gamma)]);
await followBeta(gamma); betaCommunity = (await resolveBetaCommunity(alpha)).community;
// wait for FOLLOW_ADDITIONS_RECHECK_DELAY
await delay(2000);
let betaCommunity = (await resolveBetaCommunity(alpha)).community;
if (betaCommunity) { if (betaCommunity) {
postOnAlphaRes = await createPost(alpha, betaCommunity.community.id); postOnAlphaRes = await createPost(alpha, betaCommunity.community.id);
} }
@ -227,10 +228,9 @@ test.skip("Remove a comment from admin and community on the same instance", asyn
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 alpha_user = await registerUser(alpha); let alpha_user = await registerUser(alpha);
let newAlphaApi: API = { let newAlphaApi = new LemmyHttp(alphaUrl, {
client: alpha.client, headers: { auth: alpha_user.jwt ?? "" },
auth: alpha_user.jwt ?? "", });
};
// New alpha user creates a community, post, and comment. // New alpha user creates a community, post, and comment.
let newCommunity = await createCommunity(newAlphaApi); let newCommunity = await createCommunity(newAlphaApi);
@ -343,6 +343,8 @@ test("Federated comment like", async () => {
}); });
test("Reply to a comment from another instance, get notification", async () => { test("Reply to a comment from another instance, get notification", async () => {
await alpha.markAllAsRead();
let betaCommunity = (await resolveBetaCommunity(alpha)).community; let betaCommunity = (await resolveBetaCommunity(alpha)).community;
if (!betaCommunity) { if (!betaCommunity) {
throw "Missing beta community"; throw "Missing beta community";
@ -375,16 +377,17 @@ test("Reply to a comment from another instance, get notification", async () => {
expect(replyRes.comment_view.counts.score).toBe(1); expect(replyRes.comment_view.counts.score).toBe(1);
// Make sure that reply comment is seen on alpha // Make sure that reply comment is seen on alpha
// TODO not sure why, but a searchComment back to alpha, for the ap_id of betas let commentSearch = await waitUntil(
// comment, isn't working. () => resolveComment(alpha, replyRes.comment_view.comment),
// let searchAlpha = await searchComment(alpha, replyRes.comment); c => c.comment?.counts.score === 1,
);
let alphaComment = commentSearch.comment!;
let postComments = await waitUntil( let postComments = await waitUntil(
() => getComments(alpha, postOnAlphaRes.post_view.post.id), () => getComments(alpha, postOnAlphaRes.post_view.post.id),
pc => pc.comments.length >= 2, pc => pc.comments.length >= 2,
); );
// Note: this test fails when run twice and this count will differ // Note: this test fails when run twice and this count will differ
expect(postComments.comments.length).toBeGreaterThanOrEqual(2); expect(postComments.comments.length).toBeGreaterThanOrEqual(2);
let alphaComment = postComments.comments[0];
expect(alphaComment.comment.content).toBeDefined(); expect(alphaComment.comment.content).toBeDefined();
expect(getCommentParentId(alphaComment.comment)).toBe( expect(getCommentParentId(alphaComment.comment)).toBe(
@ -400,23 +403,29 @@ test("Reply to a comment from another instance, get notification", async () => {
() => getUnreadCount(alpha), () => getUnreadCount(alpha),
e => e.replies >= 1, e => e.replies >= 1,
); );
expect(alphaUnreadCountRes.replies).toBe(1); expect(alphaUnreadCountRes.replies).toBeGreaterThanOrEqual(1);
// check inbox of replies on alpha, fetching read/unread both // check inbox of replies on alpha, fetching read/unread both
let alphaRepliesRes = await getReplies(alpha); let alphaRepliesRes = await getReplies(alpha);
expect(alphaRepliesRes.replies.length).toBe(1); const alphaReply = alphaRepliesRes.replies.find(
expect(alphaRepliesRes.replies[0].comment.content).toBeDefined(); r => r.comment.id === alphaComment.comment.id,
expect(alphaRepliesRes.replies[0].community.local).toBe(false); );
expect(alphaRepliesRes.replies[0].creator.local).toBe(false); expect(alphaReply).toBeDefined();
expect(alphaRepliesRes.replies[0].counts.score).toBe(1); if (!alphaReply) throw Error();
expect(alphaReply.comment.content).toBeDefined();
expect(alphaReply.community.local).toBe(false);
expect(alphaReply.creator.local).toBe(false);
expect(alphaReply.counts.score).toBe(1);
// ToDo: interesting alphaRepliesRes.replies[0].comment_reply.id is 1, meaning? how did that come about? // ToDo: interesting alphaRepliesRes.replies[0].comment_reply.id is 1, meaning? how did that come about?
expect(alphaRepliesRes.replies[0].comment.id).toBe(alphaComment.comment.id); expect(alphaReply.comment.id).toBe(alphaComment.comment.id);
// this is a new notification, getReplies fetch was for read/unread both, confirm it is unread. // this is a new notification, getReplies fetch was for read/unread both, confirm it is unread.
expect(alphaRepliesRes.replies[0].comment_reply.read).toBe(false); expect(alphaReply.comment_reply.read).toBe(false);
assertCommentFederation(alphaRepliesRes.replies[0], replyRes.comment_view); assertCommentFederation(alphaReply, replyRes.comment_view);
}); });
test("Mention beta from alpha", async () => { test("Mention beta from alpha", async () => {
if (!betaCommunity) throw Error("no community");
const postOnAlphaRes = await createPost(alpha, betaCommunity.community.id);
// Create a new branch, trunk-level comment branch, from alpha instance // Create a new branch, trunk-level comment branch, from alpha instance
let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Create a reply comment to previous comment, this has a mention in body // Create a reply comment to previous comment, this has a mention in body
@ -433,7 +442,7 @@ test("Mention beta from alpha", async () => {
expect(mentionRes.comment_view.counts.score).toBe(1); expect(mentionRes.comment_view.counts.score).toBe(1);
// get beta's localized copy of the alpha post // get beta's localized copy of the alpha post
let betaPost = (await resolvePost(beta, postOnAlphaRes.post_view.post)).post; let betaPost = await waitForPost(beta, postOnAlphaRes.post_view.post);
if (!betaPost) { if (!betaPost) {
throw "unable to locate post on beta"; throw "unable to locate post on beta";
} }
@ -443,9 +452,9 @@ test("Mention beta from alpha", async () => {
// Make sure that both new comments are seen on beta and have parent/child relationship // Make sure that both new comments are seen on beta and have parent/child relationship
let betaPostComments = await waitUntil( let betaPostComments = await waitUntil(
() => getComments(beta, betaPost!.post.id), () => getComments(beta, betaPost!.post.id),
c => c.comments[1].counts.score === 1, c => c.comments[1]?.counts.score === 1,
); );
expect(betaPostComments.comments.length).toBeGreaterThanOrEqual(2); expect(betaPostComments.comments.length).toEqual(2);
// the trunk-branch root comment will be older than the mention reply comment, so index 1 // the trunk-branch root comment will be older than the mention reply comment, so index 1
let betaRootComment = betaPostComments.comments[1]; let betaRootComment = betaPostComments.comments[1];
// the trunk-branch root comment should not have a parent // the trunk-branch root comment should not have a parent
@ -460,7 +469,10 @@ test("Mention beta from alpha", async () => {
expect(betaRootComment.counts.score).toBe(1); expect(betaRootComment.counts.score).toBe(1);
assertCommentFederation(betaRootComment, commentRes.comment_view); assertCommentFederation(betaRootComment, commentRes.comment_view);
let mentionsRes = await getMentions(beta); let mentionsRes = await waitUntil(
() => getMentions(beta),
m => !!m.mentions[0],
);
expect(mentionsRes.mentions[0].comment.content).toBeDefined(); expect(mentionsRes.mentions[0].comment.content).toBeDefined();
expect(mentionsRes.mentions[0].community.local).toBe(true); expect(mentionsRes.mentions[0].community.local).toBe(true);
expect(mentionsRes.mentions[0].creator.local).toBe(false); expect(mentionsRes.mentions[0].creator.local).toBe(false);
@ -492,7 +504,7 @@ test("A and G subscribe to B (center) A posts, G mentions B, it gets announced t
expect(alphaPost.post_view.community.local).toBe(true); expect(alphaPost.post_view.community.local).toBe(true);
// Make sure gamma sees it // Make sure gamma sees it
let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post; let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post))!.post;
if (!gammaPost) { if (!gammaPost) {
throw "Missing gamma post"; throw "Missing gamma post";
@ -514,7 +526,7 @@ test("A and G subscribe to B (center) A posts, G mentions B, it gets announced t
// Make sure alpha sees it // Make sure alpha sees it
let alphaPostComments2 = await waitUntil( let alphaPostComments2 = await waitUntil(
() => getComments(alpha, alphaPost.post_view.post.id), () => getComments(alpha, alphaPost.post_view.post.id),
e => !!e.comments[0], e => e.comments[0]?.counts.score === 1,
); );
expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent); expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
expect(alphaPostComments2.comments[0].community.local).toBe(true); expect(alphaPostComments2.comments[0].community.local).toBe(true);
@ -560,21 +572,19 @@ test("Check that activity from another instance is sent to third instance", asyn
() => resolveBetaCommunity(gamma), () => resolveBetaCommunity(gamma),
c => c.community?.subscribed === "Subscribed", c => c.community?.subscribed === "Subscribed",
); );
// FOLLOW_ADDITIONS_RECHECK_DELAY
await delay(2000);
// Create a post on beta // Create a post on beta
let betaPost = await createPost(beta, 2); let betaPost = await createPost(beta, 2);
expect(betaPost.post_view.community.local).toBe(true); expect(betaPost.post_view.community.local).toBe(true);
// Make sure gamma and alpha see it // Make sure gamma and alpha see it
let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post; let gammaPost = await waitForPost(gamma, betaPost.post_view.post);
if (!gammaPost) { if (!gammaPost) {
throw "Missing gamma post"; throw "Missing gamma post";
} }
expect(gammaPost.post).toBeDefined(); expect(gammaPost.post).toBeDefined();
let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post; let alphaPost = await waitForPost(alpha, betaPost.post_view.post);
if (!alphaPost) { if (!alphaPost) {
throw "Missing alpha post"; throw "Missing alpha post";
} }
@ -596,7 +606,7 @@ test("Check that activity from another instance is sent to third instance", asyn
// Make sure alpha sees it // Make sure alpha sees it
let alphaPostComments2 = await waitUntil( let alphaPostComments2 = await waitUntil(
() => getComments(alpha, alphaPost!.post.id), () => getComments(alpha, alphaPost!.post.id),
e => !!e.comments[0], e => e.comments[0]?.counts.score === 1,
); );
expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent); expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
expect(alphaPostComments2.comments[0].community.local).toBe(false); expect(alphaPostComments2.comments[0].community.local).toBe(false);
@ -607,8 +617,7 @@ test("Check that activity from another instance is sent to third instance", asyn
commentRes.comment_view, commentRes.comment_view,
); );
await unfollowRemotes(alpha); await Promise.all([unfollowRemotes(alpha), unfollowRemotes(gamma)]);
await unfollowRemotes(gamma);
}); });
test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => { test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => {
@ -660,8 +669,8 @@ test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent); expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
// Get the post from alpha // Get the post from alpha
let alphaPostB = (await resolvePost(alpha, postOnBetaRes.post_view.post)) let alphaPostB = await waitForPost(alpha, postOnBetaRes.post_view.post);
.post;
if (!alphaPostB) { if (!alphaPostB) {
throw "Missing alpha post B"; throw "Missing alpha post B";
} }
@ -671,7 +680,8 @@ test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
() => getComments(alpha, alphaPostB!.post.id), () => getComments(alpha, alphaPostB!.post.id),
c => c =>
c.comments[1]?.comment.content === c.comments[1]?.comment.content ===
parentCommentRes.comment_view.comment.content, parentCommentRes.comment_view.comment.content &&
c.comments[0]?.comment.content === updateRes.comment_view.comment.content,
); );
expect(alphaPost.post_view.post.name).toBeDefined(); expect(alphaPost.post_view.post.name).toBeDefined();
assertCommentFederation( assertCommentFederation(
@ -705,16 +715,17 @@ test("Report a comment", async () => {
throw "Missing alpha comment"; throw "Missing alpha comment";
} }
let alphaReport = ( const reason = randomString(10);
await reportComment(alpha, alphaComment.id, randomString(10)) let alphaReport = (await reportComment(alpha, alphaComment.id, reason))
).comment_report_view.comment_report; .comment_report_view.comment_report;
let betaReport = ( let betaReport = (await waitUntil(
await waitUntil( () =>
() => listCommentReports(beta), listCommentReports(beta).then(r =>
e => !!e.comment_reports[0], r.comment_reports.find(rep => rep.comment_report.reason === reason),
) ),
).comment_reports[0].comment_report; e => !!e,
))!.comment_report;
expect(betaReport).toBeDefined(); expect(betaReport).toBeDefined();
expect(betaReport.resolved).toBe(false); expect(betaReport.resolved).toBe(false);
expect(betaReport.original_comment_text).toBe( expect(betaReport.original_comment_text).toBe(

View file

@ -19,7 +19,6 @@ import {
getPost, getPost,
resolvePost, resolvePost,
registerUser, registerUser,
API,
getPosts, getPosts,
getComments, getComments,
createComment, createComment,
@ -27,7 +26,10 @@ import {
blockInstance, blockInstance,
waitUntil, waitUntil,
delay, delay,
waitForPost,
alphaUrl,
} from "./shared"; } from "./shared";
import { LemmyHttp } from "lemmy-js-client";
beforeAll(async () => { beforeAll(async () => {
await setupLogins(); await setupLogins();
@ -88,12 +90,6 @@ test("Delete community", async () => {
// Make sure the follow response went through // Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false); expect(follow.community_view.community.local).toBe(false);
await waitUntil(
() => resolveCommunity(alpha, searchShort),
g => g.community?.subscribed === "Subscribed",
);
// wait FOLLOW_ADDITIONS_RECHECK_DELAY
await delay(2000);
let deleteCommunityRes = await deleteCommunity( let deleteCommunityRes = await deleteCommunity(
beta, beta,
true, true,
@ -146,10 +142,6 @@ test("Remove community", async () => {
// Make sure the follow response went through // Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false); expect(follow.community_view.community.local).toBe(false);
await waitUntil(
() => resolveCommunity(alpha, searchShort),
g => g.community?.subscribed === "Subscribed",
);
let removeCommunityRes = await removeCommunity( let removeCommunityRes = await removeCommunity(
beta, beta,
true, true,
@ -258,11 +250,10 @@ test("Admin actions in remote community are not federated to origin", async () =
test("moderator view", async () => { test("moderator view", async () => {
// register a new user with their own community on alpha and post to it // register a new user with their own community on alpha and post to it
let otherUser: API = { let registerUserRes = await registerUser(alpha);
auth: (await registerUser(alpha)).jwt ?? "", let otherUser = new LemmyHttp(alphaUrl, {
client: alpha.client, headers: { auth: registerUserRes.jwt ?? "" },
}; });
expect(otherUser.auth).not.toBe("");
let otherCommunity = (await createCommunity(otherUser)).community_view; let otherCommunity = (await createCommunity(otherUser)).community_view;
expect(otherCommunity.community.name).toBeDefined(); expect(otherCommunity.community.name).toBeDefined();
@ -361,8 +352,8 @@ test("User blocks instance, communities are hidden", async () => {
expect(postRes.post_view.post.id).toBeDefined(); expect(postRes.post_view.post.id).toBeDefined();
// fetch post to alpha // fetch post to alpha
let alphaPost = await resolvePost(alpha, postRes.post_view.post); let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post!;
expect(alphaPost.post?.post).toBeDefined(); expect(alphaPost.post).toBeDefined();
// post should be included in listing // post should be included in listing
let listing = await getPosts(alpha, "All"); let listing = await getPosts(alpha, "All");
@ -370,7 +361,7 @@ test("User blocks instance, communities are hidden", async () => {
expect(listing_ids).toContain(postRes.post_view.post.ap_id); expect(listing_ids).toContain(postRes.post_view.post.ap_id);
// block the beta instance // block the beta instance
await blockInstance(alpha, alphaPost.post!.community.instance_id, true); await blockInstance(alpha, alphaPost.community.instance_id, true);
// after blocking, post should not be in listing // after blocking, post should not be in listing
let listing2 = await getPosts(alpha, "All"); let listing2 = await getPosts(alpha, "All");
@ -378,7 +369,7 @@ test("User blocks instance, communities are hidden", async () => {
expect(listing_ids2.indexOf(postRes.post_view.post.ap_id)).toBe(-1); expect(listing_ids2.indexOf(postRes.post_view.post.ap_id)).toBe(-1);
// unblock instance again // unblock instance again
await blockInstance(alpha, alphaPost.post!.community.instance_id, false); await blockInstance(alpha, alphaPost.community.instance_id, false);
// post should be included in listing // post should be included in listing
let listing3 = await getPosts(alpha, "All"); let listing3 = await getPosts(alpha, "All");

View file

@ -30,15 +30,16 @@ import {
listPostReports, listPostReports,
randomString, randomString,
registerUser, registerUser,
API,
getSite, getSite,
unfollows, unfollows,
resolveCommunity, resolveCommunity,
waitUntil, waitUntil,
delay, waitForPost,
alphaUrl,
} from "./shared"; } from "./shared";
import { PostView } from "lemmy-js-client/dist/types/PostView"; import { PostView } from "lemmy-js-client/dist/types/PostView";
import { CreatePost } from "lemmy-js-client/dist/types/CreatePost"; import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
import { LemmyHttp } from "lemmy-js-client";
let betaCommunity: CommunityView | undefined; let betaCommunity: CommunityView | undefined;
@ -82,11 +83,11 @@ test("Create a post", async () => {
expect(postRes.post_view.counts.score).toBe(1); expect(postRes.post_view.counts.score).toBe(1);
// Make sure that post is liked on beta // Make sure that post is liked on beta
const res = await waitUntil( const betaPost = await waitForPost(
() => resolvePost(beta, postRes.post_view.post), beta,
res => res.post?.counts.score === 1, postRes.post_view.post,
res => res?.counts.score === 1,
); );
let betaPost = res.post;
expect(betaPost).toBeDefined(); expect(betaPost).toBeDefined();
expect(betaPost?.community.local).toBe(true); expect(betaPost?.community.local).toBe(true);
@ -122,12 +123,12 @@ test("Unlike a post", async () => {
expect(unlike2.post_view.counts.score).toBe(0); expect(unlike2.post_view.counts.score).toBe(0);
// Make sure that post is unliked on beta // Make sure that post is unliked on beta
const betaPost = ( const betaPost = await waitForPost(
await waitUntil( beta,
() => resolvePost(beta, postRes.post_view.post), postRes.post_view.post,
b => b.post?.counts.score === 0, post => post?.counts.score === 0,
) );
).post;
expect(betaPost).toBeDefined(); expect(betaPost).toBeDefined();
expect(betaPost?.community.local).toBe(true); expect(betaPost?.community.local).toBe(true);
expect(betaPost?.creator.local).toBe(false); expect(betaPost?.creator.local).toBe(false);
@ -140,26 +141,16 @@ test("Update a post", async () => {
throw "Missing beta community"; throw "Missing beta community";
} }
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
await waitUntil( await waitForPost(beta, postRes.post_view.post);
() => resolvePost(beta, postRes.post_view.post),
res => !!res.post,
);
let updatedName = "A jest test federated post, updated"; let updatedName = "A jest test federated post, updated";
let updatedPost = await editPost(alpha, postRes.post_view.post); let updatedPost = await editPost(alpha, postRes.post_view.post);
await waitUntil(
() => resolvePost(beta, postRes.post_view.post),
res => res.post?.post.name === updatedName,
);
expect(updatedPost.post_view.post.name).toBe(updatedName); expect(updatedPost.post_view.post.name).toBe(updatedName);
expect(updatedPost.post_view.community.local).toBe(false); expect(updatedPost.post_view.community.local).toBe(false);
expect(updatedPost.post_view.creator.local).toBe(true); expect(updatedPost.post_view.creator.local).toBe(true);
// Make sure that post is updated on beta // Make sure that post is updated on beta
let betaPost = (await resolvePost(beta, postRes.post_view.post)).post; let betaPost = await waitForPost(beta, updatedPost.post_view.post);
if (!betaPost) {
throw "Missing beta post";
}
expect(betaPost.community.local).toBe(true); expect(betaPost.community.local).toBe(true);
expect(betaPost.creator.local).toBe(false); expect(betaPost.creator.local).toBe(false);
expect(betaPost.post.name).toBe(updatedName); expect(betaPost.post.name).toBe(updatedName);
@ -177,7 +168,7 @@ test("Sticky a post", async () => {
} }
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post; let betaPost1 = await waitForPost(beta, postRes.post_view.post);
if (!betaPost1) { if (!betaPost1) {
throw "Missing beta post1"; throw "Missing beta post1";
} }
@ -220,30 +211,19 @@ test("Lock a post", async () => {
() => resolveBetaCommunity(alpha), () => resolveBetaCommunity(alpha),
c => c.community?.subscribed === "Subscribed", c => c.community?.subscribed === "Subscribed",
); );
// wait FOLLOW_ADDITIONS_RECHECK_DELAY (there's no API to wait for this currently)
await delay(2_000);
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
// wait for federation let betaPost1 = await waitForPost(beta, postRes.post_view.post);
await waitUntil(
() => searchPostLocal(beta, postRes.post_view.post),
res => !!res.posts[0],
);
// Lock the post // Lock the post
let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
if (!betaPost1) {
throw "Missing beta post1";
}
let lockedPostRes = await lockPost(beta, true, betaPost1.post); let lockedPostRes = await lockPost(beta, true, betaPost1.post);
expect(lockedPostRes.post_view.post.locked).toBe(true); expect(lockedPostRes.post_view.post.locked).toBe(true);
// Make sure that post is locked on alpha // Make sure that post is locked on alpha
let searchAlpha = await waitUntil( let alphaPost1 = await waitForPost(
() => searchPostLocal(alpha, postRes.post_view.post), alpha,
res => res.posts[0]?.post.locked, postRes.post_view.post,
post => !!post && post.post.locked,
); );
let alphaPost1 = searchAlpha.posts[0];
expect(alphaPost1.post.locked).toBe(true);
// Try to make a new comment there, on alpha // Try to make a new comment there, on alpha
await expect(createComment(alpha, alphaPost1.post.id)).rejects.toBe("locked"); await expect(createComment(alpha, alphaPost1.post.id)).rejects.toBe("locked");
@ -253,11 +233,11 @@ test("Lock a post", async () => {
expect(unlockedPost.post_view.post.locked).toBe(false); expect(unlockedPost.post_view.post.locked).toBe(false);
// Make sure that post is unlocked on alpha // Make sure that post is unlocked on alpha
let searchAlpha2 = await waitUntil( let alphaPost2 = await waitForPost(
() => searchPostLocal(alpha, postRes.post_view.post), alpha,
res => !res.posts[0]?.post.locked, postRes.post_view.post,
post => !!post && !post.post.locked,
); );
let alphaPost2 = searchAlpha2.posts[0];
expect(alphaPost2.community.local).toBe(false); expect(alphaPost2.community.local).toBe(false);
expect(alphaPost2.creator.local).toBe(true); expect(alphaPost2.creator.local).toBe(true);
expect(alphaPost2.post.locked).toBe(false); expect(alphaPost2.post.locked).toBe(false);
@ -274,6 +254,7 @@ test("Delete a post", async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
expect(postRes.post_view.post).toBeDefined(); expect(postRes.post_view.post).toBeDefined();
await waitForPost(beta, postRes.post_view.post);
let deletedPost = await deletePost(alpha, true, postRes.post_view.post); let deletedPost = await deletePost(alpha, true, postRes.post_view.post);
expect(deletedPost.post_view.post.deleted).toBe(true); expect(deletedPost.post_view.post.deleted).toBe(true);
@ -281,16 +262,18 @@ test("Delete a post", async () => {
// Make sure lemmy beta sees post is deleted // Make sure lemmy beta sees post is deleted
// This will be undefined because of the tombstone // This will be undefined because of the tombstone
await expect(resolvePost(beta, postRes.post_view.post)).rejects.toBe( await waitForPost(beta, postRes.post_view.post, p => !p || p.post.deleted);
"couldnt_find_object",
);
// Undelete // Undelete
let undeletedPost = await deletePost(alpha, false, postRes.post_view.post); let undeletedPost = await deletePost(alpha, false, postRes.post_view.post);
expect(undeletedPost.post_view.post.deleted).toBe(false);
// Make sure lemmy beta sees post is undeleted // Make sure lemmy beta sees post is undeleted
let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post; let betaPost2 = await waitForPost(
beta,
postRes.post_view.post,
p => !!p && !p.post.deleted,
);
if (!betaPost2) { if (!betaPost2) {
throw "Missing beta post 2"; throw "Missing beta post 2";
} }
@ -349,11 +332,7 @@ test("Remove a post from admin and community on same instance", async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
expect(postRes.post_view.post).toBeDefined(); expect(postRes.post_view.post).toBeDefined();
// Get the id for beta // Get the id for beta
let searchBeta = await waitUntil( let betaPost = await waitForPost(beta, postRes.post_view.post);
() => searchPostLocal(beta, postRes.post_view.post),
res => !!res.posts[0],
);
let betaPost = searchBeta.posts[0];
expect(betaPost).toBeDefined(); expect(betaPost).toBeDefined();
// The beta admin removes it (the community lives on beta) // The beta admin removes it (the community lives on beta)
@ -361,18 +340,25 @@ test("Remove a post from admin and community on same instance", async () => {
expect(removePostRes.post_view.post.removed).toBe(true); expect(removePostRes.post_view.post.removed).toBe(true);
// Make sure lemmy alpha sees post is removed // Make sure lemmy alpha sees post is removed
// let alphaPost = await getPost(alpha, postRes.post_view.post.id); let alphaPost = await waitUntil(
// expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented () => getPost(alpha, postRes.post_view.post.id),
// assertPostFederation(alphaPost.post_view, removePostRes.post_view); p => p?.post_view.post.removed ?? false,
);
expect(alphaPost.post_view?.post.removed).toBe(true);
assertPostFederation(alphaPost.post_view, removePostRes.post_view);
// Undelete // Undelete
let undeletedPost = await removePost(beta, false, betaPost.post); let undeletedPost = await removePost(beta, false, betaPost.post);
expect(undeletedPost.post_view.post.removed).toBe(false); expect(undeletedPost.post_view.post.removed).toBe(false);
// Make sure lemmy alpha sees post is undeleted // Make sure lemmy alpha sees post is undeleted
let alphaPost2 = await getPost(alpha, postRes.post_view.post.id); let alphaPost2 = await waitForPost(
expect(alphaPost2.post_view.post.removed).toBe(false); alpha,
assertPostFederation(alphaPost2.post_view, undeletedPost.post_view); postRes.post_view.post,
p => !!p && !p.post.removed,
);
expect(alphaPost2.post.removed).toBe(false);
assertPostFederation(alphaPost2, undeletedPost.post_view);
await unfollowRemotes(alpha); await unfollowRemotes(alpha);
}); });
@ -384,7 +370,7 @@ test("Search for a post", async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
expect(postRes.post_view.post).toBeDefined(); expect(postRes.post_view.post).toBeDefined();
let betaPost = (await resolvePost(beta, postRes.post_view.post)).post; let betaPost = await waitForPost(beta, postRes.post_view.post);
expect(betaPost?.post.name).toBeDefined(); expect(betaPost?.post.name).toBeDefined();
}); });
@ -395,17 +381,16 @@ test("Enforce site ban for federated user", async () => {
// create a test user // create a test user
let alphaUserJwt = await registerUser(alpha); let alphaUserJwt = await registerUser(alpha);
expect(alphaUserJwt).toBeDefined(); expect(alphaUserJwt).toBeDefined();
let alpha_user: API = { let alpha_user = new LemmyHttp(alphaUrl, {
client: alpha.client, headers: { auth: alphaUserJwt.jwt ?? "" },
auth: alphaUserJwt.jwt ?? "", });
}; let alphaUserActorId = (await getSite(alpha_user)).my_user?.local_user_view
const alphaUserActorId = (await getSite(alpha_user)).my_user?.local_user_view
.person.actor_id; .person.actor_id;
if (!alphaUserActorId) { if (!alphaUserActorId) {
throw "Missing alpha user actor id"; throw "Missing alpha user actor id";
} }
expect(alphaUserActorId).toBeDefined(); expect(alphaUserActorId).toBeDefined();
let alphaPerson = (await resolvePerson(alpha_user, alphaUserActorId)).person; let alphaPerson = (await resolvePerson(alpha_user, alphaUserActorId!)).person;
if (!alphaPerson) { if (!alphaPerson) {
throw "Missing alpha person"; throw "Missing alpha person";
} }
@ -413,11 +398,7 @@ test("Enforce site ban for federated user", async () => {
// alpha makes post in beta community, it federates to beta instance // alpha makes post in beta community, it federates to beta instance
let postRes1 = await createPost(alpha_user, betaCommunity.community.id); let postRes1 = await createPost(alpha_user, betaCommunity.community.id);
let searchBeta1 = await waitUntil( let searchBeta1 = await waitForPost(beta, postRes1.post_view.post);
() => searchPostLocal(beta, postRes1.post_view.post),
res => !!res.posts[0],
);
expect(searchBeta1.posts[0]).toBeDefined();
// ban alpha from its instance // ban alpha from its instance
let banAlpha = await banPersonFromSite( let banAlpha = await banPersonFromSite(
@ -430,14 +411,16 @@ test("Enforce site ban for federated user", async () => {
// alpha ban should be federated to beta // alpha ban should be federated to beta
let alphaUserOnBeta1 = await waitUntil( let alphaUserOnBeta1 = await waitUntil(
() => resolvePerson(beta, alphaUserActorId), () => resolvePerson(beta, alphaUserActorId!),
res => res.person?.person.banned ?? false, res => res.person?.person.banned ?? false,
); );
expect(alphaUserOnBeta1.person?.person.banned).toBe(true); expect(alphaUserOnBeta1.person?.person.banned).toBe(true);
// existing alpha post should be removed on beta // existing alpha post should be removed on beta
let searchBeta2 = await getPost(beta, searchBeta1.posts[0].post.id); let searchBeta2 = await waitUntil(
expect(searchBeta2.post_view.post.removed).toBe(true); () => getPost(beta, searchBeta1.post.id),
s => s.post_view.post.removed,
);
// Unban alpha // Unban alpha
let unBanAlpha = await banPersonFromSite( let unBanAlpha = await banPersonFromSite(
@ -450,13 +433,9 @@ test("Enforce site ban for federated user", async () => {
// alpha makes new post in beta community, it federates // alpha makes new post in beta community, it federates
let postRes2 = await createPost(alpha_user, betaCommunity.community.id); let postRes2 = await createPost(alpha_user, betaCommunity.community.id);
let searchBeta3 = await waitUntil( let searchBeta3 = await waitForPost(beta, postRes2.post_view.post);
() => searchPostLocal(beta, postRes2.post_view.post),
e => !!e.posts[0],
);
expect(searchBeta3.posts[0]).toBeDefined();
let alphaUserOnBeta2 = await resolvePerson(beta, alphaUserActorId); let alphaUserOnBeta2 = await resolvePerson(beta, alphaUserActorId!);
expect(alphaUserOnBeta2.person?.person.banned).toBe(false); expect(alphaUserOnBeta2.person?.person.banned).toBe(false);
}); });
@ -544,12 +523,16 @@ test("Report a post", async () => {
await reportPost(alpha, alphaPost.post.id, randomString(10)) await reportPost(alpha, alphaPost.post.id, randomString(10))
).post_report_view.post_report; ).post_report_view.post_report;
let betaReport = ( let betaReport = (await waitUntil(
await waitUntil( () =>
() => listPostReports(beta), listPostReports(beta).then(p =>
res => !!res.post_reports[0], p.post_reports.find(
) r =>
).post_reports[0].post_report; r.post_report.original_post_name === alphaReport.original_post_name,
),
),
res => !!res,
))!.post_report;
expect(betaReport).toBeDefined(); expect(betaReport).toBeDefined();
expect(betaReport.resolved).toBe(false); expect(betaReport.resolved).toBe(false);
expect(betaReport.original_post_name).toBe(alphaReport.original_post_name); expect(betaReport.original_post_name).toBe(alphaReport.original_post_name);
@ -569,10 +552,9 @@ test("Sanitize HTML", async () => {
let form: CreatePost = { let form: CreatePost = {
name, name,
body, body,
auth: beta.auth,
community_id: betaCommunity.community.id, community_id: betaCommunity.community.id,
}; };
let post = await beta.client.createPost(form); let post = await beta.createPost(form);
// first escaping for the api // first escaping for the api
expect(post.post_view.post.body).toBe( expect(post.post_view.post.body).toBe(
"<script>alert('xss');</script> hello &"'", "<script>alert('xss');</script> hello &"'",

View file

@ -3,10 +3,10 @@ import {
BlockInstanceResponse, BlockInstanceResponse,
GetReplies, GetReplies,
GetRepliesResponse, GetRepliesResponse,
GetUnreadCount,
GetUnreadCountResponse, GetUnreadCountResponse,
InstanceId, InstanceId,
LemmyHttp, LemmyHttp,
PostView,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { CreatePost } from "lemmy-js-client/dist/types/CreatePost"; import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
import { DeletePost } from "lemmy-js-client/dist/types/DeletePost"; import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
@ -56,7 +56,6 @@ import { SaveUserSettings } from "lemmy-js-client/dist/types/SaveUserSettings";
import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount"; import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount";
import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse"; import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse";
import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse"; import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse";
import { GetSite } from "lemmy-js-client/dist/types/GetSite";
import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse"; import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse";
import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages"; import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages";
import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse"; import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse";
@ -73,35 +72,17 @@ import { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDe
import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails"; import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails";
import { ListingType } from "lemmy-js-client/dist/types/ListingType"; import { ListingType } from "lemmy-js-client/dist/types/ListingType";
export interface API { export let alphaUrl = "http://127.0.0.1:8541";
client: LemmyHttp; export let betaUrl = "http://127.0.0.1:8551";
auth: string; export let gammaUrl = "http://127.0.0.1:8561";
} export let deltaUrl = "http://127.0.0.1:8571";
export let epsilonUrl = "http://127.0.0.1:8581";
export let alpha: API = { export let alpha = new LemmyHttp(alphaUrl);
client: new LemmyHttp("http://127.0.0.1:8541"), export let beta = new LemmyHttp(betaUrl);
auth: "", export let gamma = new LemmyHttp(gammaUrl);
}; export let delta = new LemmyHttp(deltaUrl);
export let epsilon = new LemmyHttp(epsilonUrl);
export let beta: API = {
client: new LemmyHttp("http://127.0.0.1:8551"),
auth: "",
};
export let gamma: API = {
client: new LemmyHttp("http://127.0.0.1:8561"),
auth: "",
};
export let delta: API = {
client: new LemmyHttp("http://127.0.0.1:8571"),
auth: "",
};
export let epsilon: API = {
client: new LemmyHttp("http://127.0.0.1:8581"),
auth: "",
};
const password = "lemmylemmy"; const password = "lemmylemmy";
@ -110,31 +91,31 @@ export async function setupLogins() {
username_or_email: "lemmy_alpha", username_or_email: "lemmy_alpha",
password, password,
}; };
let resAlpha = alpha.client.login(formAlpha); let resAlpha = alpha.login(formAlpha);
let formBeta: Login = { let formBeta: Login = {
username_or_email: "lemmy_beta", username_or_email: "lemmy_beta",
password, password,
}; };
let resBeta = beta.client.login(formBeta); let resBeta = beta.login(formBeta);
let formGamma: Login = { let formGamma: Login = {
username_or_email: "lemmy_gamma", username_or_email: "lemmy_gamma",
password, password,
}; };
let resGamma = gamma.client.login(formGamma); let resGamma = gamma.login(formGamma);
let formDelta: Login = { let formDelta: Login = {
username_or_email: "lemmy_delta", username_or_email: "lemmy_delta",
password, password,
}; };
let resDelta = delta.client.login(formDelta); let resDelta = delta.login(formDelta);
let formEpsilon: Login = { let formEpsilon: Login = {
username_or_email: "lemmy_epsilon", username_or_email: "lemmy_epsilon",
password, password,
}; };
let resEpsilon = epsilon.client.login(formEpsilon); let resEpsilon = epsilon.login(formEpsilon);
let res = await Promise.all([ let res = await Promise.all([
resAlpha, resAlpha,
@ -143,12 +124,11 @@ export async function setupLogins() {
resDelta, resDelta,
resEpsilon, resEpsilon,
]); ]);
alpha.setHeaders({ auth: res[0].jwt ?? "" });
alpha.auth = res[0].jwt ?? ""; beta.setHeaders({ auth: res[1].jwt ?? "" });
beta.auth = res[1].jwt ?? ""; gamma.setHeaders({ auth: res[2].jwt ?? "" });
gamma.auth = res[2].jwt ?? ""; delta.setHeaders({ auth: res[3].jwt ?? "" });
delta.auth = res[3].jwt ?? ""; epsilon.setHeaders({ auth: res[4].jwt ?? "" });
epsilon.auth = res[4].jwt ?? "";
// Registration applications are now enabled by default, need to disable them // Registration applications are now enabled by default, need to disable them
let editSiteForm: EditSite = { let editSiteForm: EditSite = {
@ -159,45 +139,39 @@ export async function setupLogins() {
rate_limit_image: 999, rate_limit_image: 999,
rate_limit_comment: 999, rate_limit_comment: 999,
rate_limit_search: 999, rate_limit_search: 999,
auth: "",
}; };
// Set the blocks and auths for each // Set the blocks and auths for each
editSiteForm.auth = alpha.auth;
editSiteForm.allowed_instances = [ editSiteForm.allowed_instances = [
"lemmy-beta", "lemmy-beta",
"lemmy-gamma", "lemmy-gamma",
"lemmy-delta", "lemmy-delta",
"lemmy-epsilon", "lemmy-epsilon",
]; ];
await alpha.client.editSite(editSiteForm); await alpha.editSite(editSiteForm);
editSiteForm.auth = beta.auth;
editSiteForm.allowed_instances = [ editSiteForm.allowed_instances = [
"lemmy-alpha", "lemmy-alpha",
"lemmy-gamma", "lemmy-gamma",
"lemmy-delta", "lemmy-delta",
"lemmy-epsilon", "lemmy-epsilon",
]; ];
await beta.client.editSite(editSiteForm); await beta.editSite(editSiteForm);
editSiteForm.auth = gamma.auth;
editSiteForm.allowed_instances = [ editSiteForm.allowed_instances = [
"lemmy-alpha", "lemmy-alpha",
"lemmy-beta", "lemmy-beta",
"lemmy-delta", "lemmy-delta",
"lemmy-epsilon", "lemmy-epsilon",
]; ];
await gamma.client.editSite(editSiteForm); await gamma.editSite(editSiteForm);
editSiteForm.allowed_instances = ["lemmy-beta"]; editSiteForm.allowed_instances = ["lemmy-beta"];
editSiteForm.auth = delta.auth; await delta.editSite(editSiteForm);
await delta.client.editSite(editSiteForm);
editSiteForm.auth = epsilon.auth;
editSiteForm.allowed_instances = []; editSiteForm.allowed_instances = [];
editSiteForm.blocked_instances = ["lemmy-alpha"]; editSiteForm.blocked_instances = ["lemmy-alpha"];
await epsilon.client.editSite(editSiteForm); await epsilon.editSite(editSiteForm);
// Create the main alpha/beta communities // Create the main alpha/beta communities
// Ignore thrown errors of duplicates // Ignore thrown errors of duplicates
@ -208,14 +182,14 @@ export async function setupLogins() {
// otherwise the first few federated events may be missed // otherwise the first few federated events may be missed
// (because last_successful_id is set to current id when federation to an instance is first started) // (because last_successful_id is set to current id when federation to an instance is first started)
// only needed the first time so do in this try // only needed the first time so do in this try
await delay(6_000); await delay(10_000);
} catch (_) { } catch (_) {
console.log("Communities already exist"); console.log("Communities already exist");
} }
} }
export async function createPost( export async function createPost(
api: API, api: LemmyHttp,
community_id: number, community_id: number,
): Promise<PostResponse> { ): Promise<PostResponse> {
let name = randomString(5); let name = randomString(5);
@ -227,50 +201,49 @@ export async function createPost(
name, name,
url, url,
body, body,
auth: api.auth,
community_id, community_id,
}; };
return api.client.createPost(form); return api.createPost(form);
} }
export async function editPost(api: API, post: Post): Promise<PostResponse> { export async function editPost(
api: LemmyHttp,
post: Post,
): Promise<PostResponse> {
let name = "A jest test federated post, updated"; let name = "A jest test federated post, updated";
let form: EditPost = { let form: EditPost = {
name, name,
post_id: post.id, post_id: post.id,
auth: api.auth,
}; };
return api.client.editPost(form); return api.editPost(form);
} }
export async function deletePost( export async function deletePost(
api: API, api: LemmyHttp,
deleted: boolean, deleted: boolean,
post: Post, post: Post,
): Promise<PostResponse> { ): Promise<PostResponse> {
let form: DeletePost = { let form: DeletePost = {
post_id: post.id, post_id: post.id,
deleted: deleted, deleted: deleted,
auth: api.auth,
}; };
return api.client.deletePost(form); return api.deletePost(form);
} }
export async function removePost( export async function removePost(
api: API, api: LemmyHttp,
removed: boolean, removed: boolean,
post: Post, post: Post,
): Promise<PostResponse> { ): Promise<PostResponse> {
let form: RemovePost = { let form: RemovePost = {
post_id: post.id, post_id: post.id,
removed, removed,
auth: api.auth,
}; };
return api.client.removePost(form); return api.removePost(form);
} }
export async function featurePost( export async function featurePost(
api: API, api: LemmyHttp,
featured: boolean, featured: boolean,
post: Post, post: Post,
): Promise<PostResponse> { ): Promise<PostResponse> {
@ -278,61 +251,68 @@ export async function featurePost(
post_id: post.id, post_id: post.id,
featured, featured,
feature_type: "Community", feature_type: "Community",
auth: api.auth,
}; };
return api.client.featurePost(form); return api.featurePost(form);
} }
export async function lockPost( export async function lockPost(
api: API, api: LemmyHttp,
locked: boolean, locked: boolean,
post: Post, post: Post,
): Promise<PostResponse> { ): Promise<PostResponse> {
let form: LockPost = { let form: LockPost = {
post_id: post.id, post_id: post.id,
locked, locked,
auth: api.auth,
}; };
return api.client.lockPost(form); return api.lockPost(form);
} }
export async function resolvePost( export async function resolvePost(
api: API, api: LemmyHttp,
post: Post, post: Post,
): Promise<ResolveObjectResponse> { ): Promise<ResolveObjectResponse> {
let form: ResolveObject = { let form: ResolveObject = {
q: post.ap_id, q: post.ap_id,
auth: api.auth,
}; };
return api.client.resolveObject(form); return api.resolveObject(form);
} }
export async function searchPostLocal( export async function searchPostLocal(
api: API, api: LemmyHttp,
post: Post, post: Post,
): Promise<SearchResponse> { ): Promise<SearchResponse> {
let form: Search = { let form: Search = {
q: post.name, q: post.name,
type_: "Posts", type_: "Posts",
sort: "TopAll", sort: "TopAll",
auth: api.auth,
}; };
return api.client.search(form); return api.search(form);
}
/// wait for a post to appear locally without pulling it
export async function waitForPost(
api: LemmyHttp,
post: Post,
checker: (t: PostView | undefined) => boolean = p => !!p,
) {
return waitUntil<PostView>(
() => searchPostLocal(api, post).then(p => p.posts[0]),
checker,
);
} }
export async function getPost( export async function getPost(
api: API, api: LemmyHttp,
post_id: number, post_id: number,
): Promise<GetPostResponse> { ): Promise<GetPostResponse> {
let form: GetPost = { let form: GetPost = {
id: post_id, id: post_id,
auth: api.auth,
}; };
return api.client.getPost(form); return api.getPost(form);
} }
export async function getComments( export async function getComments(
api: API, api: LemmyHttp,
post_id?: number, post_id?: number,
listingType: ListingType = "All", listingType: ListingType = "All",
): Promise<GetCommentsResponse> { ): Promise<GetCommentsResponse> {
@ -340,75 +320,66 @@ export async function getComments(
post_id: post_id, post_id: post_id,
type_: listingType, type_: listingType,
sort: "New", sort: "New",
auth: api.auth,
}; };
return api.client.getComments(form); return api.getComments(form);
} }
export async function getUnreadCount( export async function getUnreadCount(
api: API, api: LemmyHttp,
): Promise<GetUnreadCountResponse> { ): Promise<GetUnreadCountResponse> {
let form: GetUnreadCount = { return api.getUnreadCount();
auth: api.auth,
};
return api.client.getUnreadCount(form);
} }
export async function getReplies(api: API): Promise<GetRepliesResponse> { export async function getReplies(api: LemmyHttp): Promise<GetRepliesResponse> {
let form: GetReplies = { let form: GetReplies = {
sort: "New", sort: "New",
unread_only: false, unread_only: false,
auth: api.auth,
}; };
return api.client.getReplies(form); return api.getReplies(form);
} }
export async function resolveComment( export async function resolveComment(
api: API, api: LemmyHttp,
comment: Comment, comment: Comment,
): Promise<ResolveObjectResponse> { ): Promise<ResolveObjectResponse> {
let form: ResolveObject = { let form: ResolveObject = {
q: comment.ap_id, q: comment.ap_id,
auth: api.auth,
}; };
return api.client.resolveObject(form); return api.resolveObject(form);
} }
export async function resolveBetaCommunity( export async function resolveBetaCommunity(
api: API, api: LemmyHttp,
): Promise<ResolveObjectResponse> { ): Promise<ResolveObjectResponse> {
// Use short-hand search url // Use short-hand search url
let form: ResolveObject = { let form: ResolveObject = {
q: "!main@lemmy-beta:8551", q: "!main@lemmy-beta:8551",
auth: api.auth,
}; };
return api.client.resolveObject(form); return api.resolveObject(form);
} }
export async function resolveCommunity( export async function resolveCommunity(
api: API, api: LemmyHttp,
q: string, q: string,
): Promise<ResolveObjectResponse> { ): Promise<ResolveObjectResponse> {
let form: ResolveObject = { let form: ResolveObject = {
q, q,
auth: api.auth,
}; };
return api.client.resolveObject(form); return api.resolveObject(form);
} }
export async function resolvePerson( export async function resolvePerson(
api: API, api: LemmyHttp,
apShortname: string, apShortname: string,
): Promise<ResolveObjectResponse> { ): Promise<ResolveObjectResponse> {
let form: ResolveObject = { let form: ResolveObject = {
q: apShortname, q: apShortname,
auth: api.auth,
}; };
return api.client.resolveObject(form); return api.resolveObject(form);
} }
export async function banPersonFromSite( export async function banPersonFromSite(
api: API, api: LemmyHttp,
person_id: number, person_id: number,
ban: boolean, ban: boolean,
remove_data: boolean, remove_data: boolean,
@ -418,13 +389,12 @@ export async function banPersonFromSite(
person_id, person_id,
ban, ban,
remove_data: remove_data, remove_data: remove_data,
auth: api.auth,
}; };
return api.client.banPerson(form); return api.banPerson(form);
} }
export async function banPersonFromCommunity( export async function banPersonFromCommunity(
api: API, api: LemmyHttp,
person_id: number, person_id: number,
community_id: number, community_id: number,
remove_data: boolean, remove_data: boolean,
@ -435,40 +405,44 @@ export async function banPersonFromCommunity(
community_id, community_id,
remove_data: remove_data, remove_data: remove_data,
ban, ban,
auth: api.auth,
}; };
return api.client.banFromCommunity(form); return api.banFromCommunity(form);
} }
export async function followCommunity( export async function followCommunity(
api: API, api: LemmyHttp,
follow: boolean, follow: boolean,
community_id: number, community_id: number,
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let form: FollowCommunity = { let form: FollowCommunity = {
community_id, community_id,
follow, follow,
auth: api.auth,
}; };
return api.client.followCommunity(form); const res = await api.followCommunity(form);
await waitUntil(
() => resolveCommunity(api, res.community_view.community.actor_id),
g => g.community?.subscribed === (follow ? "Subscribed" : "NotSubscribed"),
);
// wait FOLLOW_ADDITIONS_RECHECK_DELAY (there's no API to wait for this currently)
await delay(2000);
return res;
} }
export async function likePost( export async function likePost(
api: API, api: LemmyHttp,
score: number, score: number,
post: Post, post: Post,
): Promise<PostResponse> { ): Promise<PostResponse> {
let form: CreatePostLike = { let form: CreatePostLike = {
post_id: post.id, post_id: post.id,
score: score, score: score,
auth: api.auth,
}; };
return api.client.likePost(form); return api.likePost(form);
} }
export async function createComment( export async function createComment(
api: API, api: LemmyHttp,
post_id: number, post_id: number,
parent_id?: number, parent_id?: number,
content = "a jest test comment", content = "a jest test comment",
@ -477,76 +451,70 @@ export async function createComment(
content, content,
post_id, post_id,
parent_id, parent_id,
auth: api.auth,
}; };
return api.client.createComment(form); return api.createComment(form);
} }
export async function editComment( export async function editComment(
api: API, api: LemmyHttp,
comment_id: number, comment_id: number,
content = "A jest test federated comment update", content = "A jest test federated comment update",
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let form: EditComment = { let form: EditComment = {
content, content,
comment_id, comment_id,
auth: api.auth,
}; };
return api.client.editComment(form); return api.editComment(form);
} }
export async function deleteComment( export async function deleteComment(
api: API, api: LemmyHttp,
deleted: boolean, deleted: boolean,
comment_id: number, comment_id: number,
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let form: DeleteComment = { let form: DeleteComment = {
comment_id, comment_id,
deleted, deleted,
auth: api.auth,
}; };
return api.client.deleteComment(form); return api.deleteComment(form);
} }
export async function removeComment( export async function removeComment(
api: API, api: LemmyHttp,
removed: boolean, removed: boolean,
comment_id: number, comment_id: number,
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let form: RemoveComment = { let form: RemoveComment = {
comment_id, comment_id,
removed, removed,
auth: api.auth,
}; };
return api.client.removeComment(form); return api.removeComment(form);
} }
export async function getMentions( export async function getMentions(
api: API, api: LemmyHttp,
): Promise<GetPersonMentionsResponse> { ): Promise<GetPersonMentionsResponse> {
let form: GetPersonMentions = { let form: GetPersonMentions = {
sort: "New", sort: "New",
unread_only: false, unread_only: false,
auth: api.auth,
}; };
return api.client.getPersonMentions(form); return api.getPersonMentions(form);
} }
export async function likeComment( export async function likeComment(
api: API, api: LemmyHttp,
score: number, score: number,
comment: Comment, comment: Comment,
): Promise<CommentResponse> { ): Promise<CommentResponse> {
let form: CreateCommentLike = { let form: CreateCommentLike = {
comment_id: comment.id, comment_id: comment.id,
score, score,
auth: api.auth,
}; };
return api.client.likeComment(form); return api.likeComment(form);
} }
export async function createCommunity( export async function createCommunity(
api: API, api: LemmyHttp,
name_: string = randomString(5), name_: string = randomString(5),
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let description = "a sample description"; let description = "a sample description";
@ -554,100 +522,92 @@ export async function createCommunity(
name: name_, name: name_,
title: name_, title: name_,
description, description,
auth: api.auth,
}; };
return api.client.createCommunity(form); return api.createCommunity(form);
} }
export async function getCommunity( export async function getCommunity(
api: API, api: LemmyHttp,
id: number, id: number,
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let form: GetCommunity = { let form: GetCommunity = {
id, id,
auth: api.auth,
}; };
return api.client.getCommunity(form); return api.getCommunity(form);
} }
export async function getCommunityByName( export async function getCommunityByName(
api: API, api: LemmyHttp,
name: string, name: string,
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let form: GetCommunity = { let form: GetCommunity = {
name, name,
auth: api.auth,
}; };
return api.client.getCommunity(form); return api.getCommunity(form);
} }
export async function deleteCommunity( export async function deleteCommunity(
api: API, api: LemmyHttp,
deleted: boolean, deleted: boolean,
community_id: number, community_id: number,
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let form: DeleteCommunity = { let form: DeleteCommunity = {
community_id, community_id,
deleted, deleted,
auth: api.auth,
}; };
return api.client.deleteCommunity(form); return api.deleteCommunity(form);
} }
export async function removeCommunity( export async function removeCommunity(
api: API, api: LemmyHttp,
removed: boolean, removed: boolean,
community_id: number, community_id: number,
): Promise<CommunityResponse> { ): Promise<CommunityResponse> {
let form: RemoveCommunity = { let form: RemoveCommunity = {
community_id, community_id,
removed, removed,
auth: api.auth,
}; };
return api.client.removeCommunity(form); return api.removeCommunity(form);
} }
export async function createPrivateMessage( export async function createPrivateMessage(
api: API, api: LemmyHttp,
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 form: CreatePrivateMessage = { let form: CreatePrivateMessage = {
content, content,
recipient_id, recipient_id,
auth: api.auth,
}; };
return api.client.createPrivateMessage(form); return api.createPrivateMessage(form);
} }
export async function editPrivateMessage( export async function editPrivateMessage(
api: API, api: LemmyHttp,
private_message_id: number, private_message_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 form: EditPrivateMessage = { let form: EditPrivateMessage = {
content: updatedContent, content: updatedContent,
private_message_id, private_message_id,
auth: api.auth,
}; };
return api.client.editPrivateMessage(form); return api.editPrivateMessage(form);
} }
export async function deletePrivateMessage( export async function deletePrivateMessage(
api: API, api: LemmyHttp,
deleted: boolean, deleted: boolean,
private_message_id: number, private_message_id: number,
): Promise<PrivateMessageResponse> { ): Promise<PrivateMessageResponse> {
let form: DeletePrivateMessage = { let form: DeletePrivateMessage = {
deleted, deleted,
private_message_id, private_message_id,
auth: api.auth,
}; };
return api.client.deletePrivateMessage(form); return api.deletePrivateMessage(form);
} }
export async function registerUser( export async function registerUser(
api: API, api: LemmyHttp,
username: string = randomString(5), username: string = randomString(5),
): Promise<LoginResponse> { ): Promise<LoginResponse> {
let form: Register = { let form: Register = {
@ -656,10 +616,12 @@ export async function registerUser(
password_verify: password, password_verify: password,
show_nsfw: true, show_nsfw: true,
}; };
return api.client.register(form); return api.register(form);
} }
export async function saveUserSettingsBio(api: API): Promise<LoginResponse> { export async function saveUserSettingsBio(
api: LemmyHttp,
): Promise<LoginResponse> {
let form: SaveUserSettings = { let form: SaveUserSettings = {
show_nsfw: true, show_nsfw: true,
blur_nsfw: false, blur_nsfw: false,
@ -671,13 +633,12 @@ export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
show_avatars: true, show_avatars: true,
send_notifications_to_email: false, send_notifications_to_email: false,
bio: "a changed bio", bio: "a changed bio",
auth: api.auth,
}; };
return saveUserSettings(api, form); return saveUserSettings(api, form);
} }
export async function saveUserSettingsFederated( export async function saveUserSettingsFederated(
api: API, api: LemmyHttp,
): Promise<LoginResponse> { ): Promise<LoginResponse> {
let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png"; let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
let banner = "https://image.flaticon.com/icons/png/512/36/35896.png"; let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
@ -695,67 +656,64 @@ export async function saveUserSettingsFederated(
show_avatars: false, show_avatars: false,
send_notifications_to_email: false, send_notifications_to_email: false,
bio, bio,
auth: api.auth,
}; };
return await saveUserSettings(alpha, form); return await saveUserSettings(api, form);
} }
export async function saveUserSettings( export async function saveUserSettings(
api: API, api: LemmyHttp,
form: SaveUserSettings, form: SaveUserSettings,
): Promise<LoginResponse> { ): Promise<LoginResponse> {
return api.client.saveUserSettings(form); return api.saveUserSettings(form);
} }
export async function getPersonDetails( export async function getPersonDetails(
api: API, api: LemmyHttp,
person_id: number, person_id: number,
): Promise<GetPersonDetailsResponse> { ): Promise<GetPersonDetailsResponse> {
let form: GetPersonDetails = { let form: GetPersonDetails = {
auth: api.auth,
person_id: person_id, person_id: person_id,
}; };
return api.client.getPersonDetails(form); return api.getPersonDetails(form);
} }
export async function deleteUser(api: API): Promise<DeleteAccountResponse> { export async function deleteUser(
api: LemmyHttp,
): Promise<DeleteAccountResponse> {
let form: DeleteAccount = { let form: DeleteAccount = {
auth: api.auth,
delete_content: true, delete_content: true,
password, password,
}; };
return api.client.deleteAccount(form); return api.deleteAccount(form);
} }
export async function getSite(api: API): Promise<GetSiteResponse> { export async function getSite(api: LemmyHttp): Promise<GetSiteResponse> {
let form: GetSite = { return api.getSite();
auth: api.auth,
};
return api.client.getSite(form);
} }
export async function listPrivateMessages( export async function listPrivateMessages(
api: API, api: LemmyHttp,
): Promise<PrivateMessagesResponse> { ): Promise<PrivateMessagesResponse> {
let form: GetPrivateMessages = { let form: GetPrivateMessages = {
auth: api.auth,
unread_only: false, unread_only: false,
}; };
return api.client.getPrivateMessages(form); return api.getPrivateMessages(form);
} }
export async function unfollowRemotes(api: API): Promise<GetSiteResponse> { export async function unfollowRemotes(
api: LemmyHttp,
): Promise<GetSiteResponse> {
// Unfollow all remote communities // Unfollow all remote communities
let site = await getSite(api); let site = await getSite(api);
let remoteFollowed = let remoteFollowed =
site.my_user?.follows.filter(c => c.community.local == false) ?? []; site.my_user?.follows.filter(c => c.community.local == false) ?? [];
for (let cu of remoteFollowed) { await Promise.all(
await followCommunity(api, false, cu.community.id); remoteFollowed.map(cu => followCommunity(api, false, cu.community.id)),
} );
let siteRes = await getSite(api); let siteRes = await getSite(api);
return siteRes; return siteRes;
} }
export async function followBeta(api: API): Promise<CommunityResponse> { export async function followBeta(api: LemmyHttp): Promise<CommunityResponse> {
let betaCommunity = (await resolveBetaCommunity(api)).community; let betaCommunity = (await resolveBetaCommunity(api)).community;
if (betaCommunity) { if (betaCommunity) {
let follow = await followCommunity(api, true, betaCommunity.community.id); let follow = await followCommunity(api, true, betaCommunity.community.id);
@ -766,71 +724,63 @@ export async function followBeta(api: API): Promise<CommunityResponse> {
} }
export async function reportPost( export async function reportPost(
api: API, api: LemmyHttp,
post_id: number, post_id: number,
reason: string, reason: string,
): Promise<PostReportResponse> { ): Promise<PostReportResponse> {
let form: CreatePostReport = { let form: CreatePostReport = {
post_id, post_id,
reason, reason,
auth: api.auth,
}; };
return api.client.createPostReport(form); return api.createPostReport(form);
} }
export async function listPostReports( export async function listPostReports(
api: API, api: LemmyHttp,
): Promise<ListPostReportsResponse> { ): Promise<ListPostReportsResponse> {
let form: ListPostReports = { let form: ListPostReports = {};
auth: api.auth, return api.listPostReports(form);
};
return api.client.listPostReports(form);
} }
export async function reportComment( export async function reportComment(
api: API, api: LemmyHttp,
comment_id: number, comment_id: number,
reason: string, reason: string,
): Promise<CommentReportResponse> { ): Promise<CommentReportResponse> {
let form: CreateCommentReport = { let form: CreateCommentReport = {
comment_id, comment_id,
reason, reason,
auth: api.auth,
}; };
return api.client.createCommentReport(form); return api.createCommentReport(form);
} }
export async function listCommentReports( export async function listCommentReports(
api: API, api: LemmyHttp,
): Promise<ListCommentReportsResponse> { ): Promise<ListCommentReportsResponse> {
let form: ListCommentReports = { let form: ListCommentReports = {};
auth: api.auth, return api.listCommentReports(form);
};
return api.client.listCommentReports(form);
} }
export function getPosts( export function getPosts(
api: API, api: LemmyHttp,
listingType?: ListingType, listingType?: ListingType,
): Promise<GetPostsResponse> { ): Promise<GetPostsResponse> {
let form: GetPosts = { let form: GetPosts = {
auth: api.auth,
type_: listingType, type_: listingType,
}; };
return api.client.getPosts(form); return api.getPosts(form);
} }
export function blockInstance( export function blockInstance(
api: API, api: LemmyHttp,
instance_id: InstanceId, instance_id: InstanceId,
block: boolean, block: boolean,
): Promise<BlockInstanceResponse> { ): Promise<BlockInstanceResponse> {
let form: BlockInstance = { let form: BlockInstance = {
instance_id, instance_id,
block, block,
auth: api.auth,
}; };
return api.client.blockInstance(form); return api.blockInstance(form);
} }
export function delay(millis = 500) { export function delay(millis = 500) {
@ -857,10 +807,12 @@ export function randomString(length: number): string {
} }
export async function unfollows() { export async function unfollows() {
await unfollowRemotes(alpha); await Promise.all([
await unfollowRemotes(gamma); unfollowRemotes(alpha),
await unfollowRemotes(delta); unfollowRemotes(gamma),
await unfollowRemotes(epsilon); unfollowRemotes(delta),
unfollowRemotes(epsilon),
]);
} }
export function getCommentParentId(comment: Comment): number | undefined { export function getCommentParentId(comment: Comment): number | undefined {
@ -879,14 +831,18 @@ export async function waitUntil<T>(
fetcher: () => Promise<T>, fetcher: () => Promise<T>,
checker: (t: T) => boolean, checker: (t: T) => boolean,
retries = 10, retries = 10,
delaySeconds = 2, delaySeconds = [0.2, 0.5, 1, 2, 3],
) { ) {
let retry = 0; let retry = 0;
let result;
while (retry++ < retries) { while (retry++ < retries) {
const result = await fetcher(); result = await fetcher();
if (checker(result)) return result; if (checker(result)) return result;
await delay(delaySeconds * 1000); await delay(
delaySeconds[Math.min(retry - 1, delaySeconds.length - 1)] * 1000,
);
} }
console.error("result", result);
throw Error( throw Error(
`Failed "${fetcher}": "${checker}" did not return true after ${retries} retries (delayed ${delaySeconds}s each)`, `Failed "${fetcher}": "${checker}" did not return true after ${retries} retries (delayed ${delaySeconds}s each)`,
); );

View file

@ -13,10 +13,10 @@ import {
resolveBetaCommunity, resolveBetaCommunity,
deleteUser, deleteUser,
resolvePost, resolvePost,
API,
resolveComment, resolveComment,
saveUserSettingsFederated, saveUserSettingsFederated,
setupLogins, setupLogins,
alphaUrl,
} from "./shared"; } from "./shared";
import { LemmyHttp } from "lemmy-js-client"; import { LemmyHttp } from "lemmy-js-client";
import { GetPosts } from "lemmy-js-client/dist/types/GetPosts"; import { GetPosts } from "lemmy-js-client/dist/types/GetPosts";
@ -40,9 +40,11 @@ function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
test("Create user", async () => { test("Create user", async () => {
let userRes = await registerUser(alpha); let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined(); expect(userRes.jwt).toBeDefined();
alpha.auth = userRes.jwt ?? ""; let user = new LemmyHttp(alphaUrl, {
headers: { auth: userRes.jwt ?? "" },
});
let site = await getSite(alpha); let site = await getSite(user);
expect(site.my_user).toBeDefined(); expect(site.my_user).toBeDefined();
if (!site.my_user) { if (!site.my_user) {
throw "Missing site user"; throw "Missing site user";
@ -60,10 +62,9 @@ test("Set some user settings, check that they are federated", async () => {
test("Delete user", async () => { test("Delete user", async () => {
let userRes = await registerUser(alpha); let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined(); expect(userRes.jwt).toBeDefined();
let user: API = { let user = new LemmyHttp(alphaUrl, {
client: alpha.client, headers: { auth: userRes.jwt ?? "" },
auth: userRes.jwt ?? "", });
};
// make a local post and comment // make a local post and comment
let alphaCommunity = (await resolveCommunity(user, "!main@lemmy-alpha:8541")) let alphaCommunity = (await resolveCommunity(user, "!main@lemmy-alpha:8541"))
@ -107,17 +108,14 @@ test("Delete user", async () => {
}); });
test("Requests with invalid auth should be treated as unauthenticated", async () => { test("Requests with invalid auth should be treated as unauthenticated", async () => {
let invalid_auth: API = { let invalid_auth = new LemmyHttp(alphaUrl, {
client: new LemmyHttp("http://127.0.0.1:8541"), headers: { auth: "" },
auth: "invalid", });
};
let site = await getSite(invalid_auth); let site = await getSite(invalid_auth);
expect(site.my_user).toBeUndefined(); expect(site.my_user).toBeUndefined();
expect(site.site_view).toBeDefined(); expect(site.site_view).toBeDefined();
let form: GetPosts = { let form: GetPosts = {};
auth: "invalid", let posts = invalid_auth.getPosts(form);
};
let posts = invalid_auth.client.getPosts(form);
expect((await posts).posts).toBeDefined(); expect((await posts).posts).toBeDefined();
}); });

View file

@ -2174,10 +2174,10 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==
lemmy-js-client@0.19.0-rc.5: lemmy-js-client@0.19.0-rc.12:
version "0.19.0-rc.5" version "0.19.0-rc.12"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-rc.5.tgz#a0d3e0ac829b1e46124edcf3a0343ae04317d51a" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-rc.12.tgz#e3bd4e21b1966d583ab790ef70ece8394b012b48"
integrity sha512-Z1T95Ht1VZNvWlLH9XpVnO2oC7LhMT81jTiU5BhYPIkEtGhOwN91jk5uI1oyI6/d4v9lwbrsyzFYPsiuMmXTFQ== integrity sha512-1iu2fW9vlb3TrI+QR/ODP3+5pWZB0rUqL1wH09IzomDXohCqoQvfmXpwArmgF4Eq8GZgjkcfeMDC2gMrfw/i7Q==
dependencies: dependencies:
cross-fetch "^3.1.5" cross-fetch "^3.1.5"
form-data "^4.0.0" form-data "^4.0.0"

View file

@ -2,22 +2,21 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{CommentResponse, DistinguishComment}, comment::{CommentResponse, DistinguishComment},
context::LemmyContext, context::LemmyContext,
utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, utils::{check_community_ban, is_mod_or_admin},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::comment::{Comment, CommentUpdateForm}, source::comment::{Comment, CommentUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn distinguish_comment( pub async fn distinguish_comment(
data: Json<DistinguishComment>, data: Json<DistinguishComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let orig_comment = CommentView::read(&mut context.pool(), data.comment_id, None).await?; let orig_comment = CommentView::read(&mut context.pool(), data.comment_id, None).await?;
check_community_ban( check_community_ban(

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
comment::{CommentResponse, CreateCommentLike}, comment::{CommentResponse, CreateCommentLike},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, check_downvotes_enabled, local_user_view_from_jwt}, utils::{check_community_ban, check_downvotes_enabled},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::LocalUserId, newtypes::LocalUserId,
@ -24,9 +24,9 @@ use std::ops::Deref;
pub async fn like_comment( pub async fn like_comment(
data: Json<CreateCommentLike>, data: Json<CreateCommentLike>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let mut recipient_ids = Vec::<LocalUserId>::new(); let mut recipient_ids = Vec::<LocalUserId>::new();

View file

@ -2,22 +2,20 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{CommentResponse, SaveComment}, comment::{CommentResponse, SaveComment},
context::LemmyContext, context::LemmyContext,
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::comment::{CommentSaved, CommentSavedForm}, source::comment::{CommentSaved, CommentSavedForm},
traits::Saveable, traits::Saveable,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn save_comment( pub async fn save_comment(
data: Json<SaveComment>, data: Json<SaveComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_saved_form = CommentSavedForm { let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id, comment_id: data.comment_id,
person_id: local_user_view.person.id, person_id: local_user_view.person.id,

View file

@ -5,12 +5,7 @@ use lemmy_api_common::{
comment::{CommentReportResponse, CreateCommentReport}, comment::{CommentReportResponse, CreateCommentReport},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, sanitize_html_api, send_new_report_email_to_admins},
check_community_ban,
local_user_view_from_jwt,
sanitize_html_api,
send_new_report_email_to_admins,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -19,7 +14,7 @@ use lemmy_db_schema::{
}, },
traits::Reportable, traits::Reportable,
}; };
use lemmy_db_views::structs::{CommentReportView, CommentView}; use lemmy_db_views::structs::{CommentReportView, CommentView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
/// Creates a comment report and notifies the moderators of the community /// Creates a comment report and notifies the moderators of the community
@ -27,8 +22,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn create_comment_report( pub async fn create_comment_report(
data: Json<CreateCommentReport>, data: Json<CreateCommentReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentReportResponse>, LemmyError> { ) -> Result<Json<CommentReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let reason = sanitize_html_api(data.reason.trim()); let reason = sanitize_html_api(data.reason.trim());

View file

@ -2,9 +2,8 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{ListCommentReports, ListCommentReportsResponse}, comment::{ListCommentReports, ListCommentReportsResponse},
context::LemmyContext, context::LemmyContext,
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::comment_report_view::CommentReportQuery; use lemmy_db_views::{comment_report_view::CommentReportQuery, structs::LocalUserView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
/// Lists comment reports for a community if an id is supplied /// Lists comment reports for a community if an id is supplied
@ -13,9 +12,8 @@ use lemmy_utils::error::LemmyError;
pub async fn list_comment_reports( pub async fn list_comment_reports(
data: Query<ListCommentReports>, data: Query<ListCommentReports>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<ListCommentReportsResponse>, LemmyError> { ) -> Result<Json<ListCommentReportsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community_id = data.community_id; let community_id = data.community_id;
let unresolved_only = data.unresolved_only.unwrap_or_default(); let unresolved_only = data.unresolved_only.unwrap_or_default();

View file

@ -2,10 +2,10 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{CommentReportResponse, ResolveCommentReport}, comment::{CommentReportResponse, ResolveCommentReport},
context::LemmyContext, context::LemmyContext,
utils::{is_mod_or_admin, local_user_view_from_jwt}, utils::is_mod_or_admin,
}; };
use lemmy_db_schema::{source::comment_report::CommentReport, traits::Reportable}; use lemmy_db_schema::{source::comment_report::CommentReport, traits::Reportable};
use lemmy_db_views::structs::CommentReportView; use lemmy_db_views::structs::{CommentReportView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
/// Resolves or unresolves a comment report and notifies the moderators of the community /// Resolves or unresolves a comment report and notifies the moderators of the community
@ -13,9 +13,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn resolve_comment_report( pub async fn resolve_comment_report(
data: Json<ResolveCommentReport>, data: Json<ResolveCommentReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentReportResponse>, LemmyError> { ) -> Result<Json<CommentReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let report_id = data.report_id; let report_id = data.report_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?; let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?;

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
community::{AddModToCommunity, AddModToCommunityResponse}, community::{AddModToCommunity, AddModToCommunityResponse},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{is_mod_or_admin, local_user_view_from_jwt}, utils::is_mod_or_admin,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,6 +13,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Joinable}, traits::{Crud, Joinable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityModeratorView; use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -20,9 +21,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn add_mod_to_community( pub async fn add_mod_to_community(
data: Json<AddModToCommunity>, data: Json<AddModToCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<AddModToCommunityResponse>, LemmyError> { ) -> Result<Json<AddModToCommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community_id = data.community_id; let community_id = data.community_id;
// Verify that only mods or admins can add mod // Verify that only mods or admins can add mod

View file

@ -4,12 +4,7 @@ use lemmy_api_common::{
community::{BanFromCommunity, BanFromCommunityResponse}, community::{BanFromCommunity, BanFromCommunityResponse},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{is_mod_or_admin, remove_user_data_in_community, sanitize_html_api_opt},
is_mod_or_admin,
local_user_view_from_jwt,
remove_user_data_in_community,
sanitize_html_api_opt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -23,6 +18,7 @@ use lemmy_db_schema::{
}, },
traits::{Bannable, Crud, Followable}, traits::{Bannable, Crud, Followable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::PersonView; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -33,9 +29,8 @@ use lemmy_utils::{
pub async fn ban_from_community( pub async fn ban_from_community(
data: Json<BanFromCommunity>, data: Json<BanFromCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<BanFromCommunityResponse>, LemmyError> { ) -> Result<Json<BanFromCommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let banned_person_id = data.person_id; let banned_person_id = data.person_id;
let remove_data = data.remove_data.unwrap_or(false); let remove_data = data.remove_data.unwrap_or(false);
let expires = data.expires.map(naive_from_unix); let expires = data.expires.map(naive_from_unix);

View file

@ -4,7 +4,6 @@ use lemmy_api_common::{
community::{BlockCommunity, BlockCommunityResponse}, community::{BlockCommunity, BlockCommunityResponse},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,6 +12,7 @@ use lemmy_db_schema::{
}, },
traits::{Blockable, Followable}, traits::{Blockable, Followable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityView; use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -20,9 +20,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn block_community( pub async fn block_community(
data: Json<BlockCommunity>, data: Json<BlockCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<BlockCommunityResponse>, LemmyError> { ) -> Result<Json<BlockCommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community_id = data.community_id; let community_id = data.community_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let community_block_form = CommunityBlockForm { let community_block_form = CommunityBlockForm {

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
community::{CommunityResponse, FollowCommunity}, community::{CommunityResponse, FollowCommunity},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt}, utils::{check_community_ban, check_community_deleted_or_removed},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,6 +13,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Followable}, traits::{Crud, Followable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityView; use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -20,9 +21,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn follow_community( pub async fn follow_community(
data: Json<FollowCommunity>, data: Json<FollowCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community = Community::read(&mut context.pool(), data.community_id).await?; let community = Community::read(&mut context.pool(), data.community_id).await?;
let mut community_follower_form = CommunityFollowerForm { let mut community_follower_form = CommunityFollowerForm {
community_id: community.id, community_id: community.id,

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
community::{CommunityResponse, HideCommunity}, community::{CommunityResponse, HideCommunity},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api_opt}, utils::{is_admin, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -14,15 +14,16 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn hide_community( pub async fn hide_community(
data: Json<HideCommunity>, data: Json<HideCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
// Verify its a admin (only admin can hide or unhide it) // Verify its a admin (only admin can hide or unhide it)
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let community_form = CommunityUpdateForm { let community_form = CommunityUpdateForm {

View file

@ -3,7 +3,7 @@ use anyhow::Context;
use lemmy_api_common::{ use lemmy_api_common::{
community::{GetCommunityResponse, TransferCommunity}, community::{GetCommunityResponse, TransferCommunity},
context::LemmyContext, context::LemmyContext,
utils::{is_admin, is_top_mod, local_user_view_from_jwt}, utils::{is_admin, is_top_mod},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -12,6 +12,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Joinable}, traits::{Crud, Joinable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView}; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -24,9 +25,8 @@ use lemmy_utils::{
pub async fn transfer_community( pub async fn transfer_community(
data: Json<TransferCommunity>, data: Json<TransferCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetCommunityResponse>, LemmyError> { ) -> Result<Json<GetCommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Fetch the community mods // Fetch the community mods
let community_id = data.community_id; let community_id = data.community_id;
let mut community_mods = let mut community_mods =

View file

@ -126,68 +126,6 @@ mod tests {
#![allow(clippy::indexing_slicing)] #![allow(clippy::indexing_slicing)]
use super::*; use super::*;
use lemmy_api_common::utils::check_validator_time;
use lemmy_db_schema::{
source::{
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
secret::Secret,
},
traits::Crud,
utils::build_db_pool_for_tests,
};
use lemmy_utils::{claims::Claims, settings::SETTINGS};
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_should_not_validate_user_token_after_password_change() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let secret = Secret::init(pool).await.unwrap();
let settings = &SETTINGS.to_owned();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
.unwrap();
let new_person = PersonInsertForm::builder()
.name("Gerry9812".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(pool, &new_person).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
.password_encrypted("123456".to_string())
.build();
let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
let jwt = Claims::jwt(
inserted_local_user.id.0,
&secret.jwt_secret,
&settings.hostname,
)
.unwrap();
let claims = Claims::decode(&jwt, &secret.jwt_secret).unwrap().claims;
let check = check_validator_time(&inserted_local_user.validator_time, &claims);
assert!(check.is_ok());
// The check should fail, since the validator time is now newer than the jwt issue time
let updated_local_user =
LocalUser::update_password(pool, inserted_local_user.id, "password111")
.await
.unwrap();
let check_after = check_validator_time(&updated_local_user.validator_time, &claims);
assert!(check_after.is_err());
let num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
assert_eq!(1, num_deleted);
}
#[test] #[test]
fn test_build_totp() { fn test_build_totp() {

View file

@ -2,7 +2,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{AddAdmin, AddAdminResponse}, person::{AddAdmin, AddAdminResponse},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -19,9 +19,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn add_admin( pub async fn add_admin(
data: Json<AddAdmin>, data: Json<AddAdmin>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<AddAdminResponse>, LemmyError> { ) -> Result<Json<AddAdminResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{BanPerson, BanPersonResponse}, person::{BanPerson, BanPersonResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{is_admin, local_user_view_from_jwt, remove_user_data, sanitize_html_api_opt}, utils::{is_admin, remove_user_data, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,6 +13,7 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::PersonView; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -22,9 +23,8 @@ use lemmy_utils::{
pub async fn ban_from_site( pub async fn ban_from_site(
data: Json<BanPerson>, data: Json<BanPerson>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<BanPersonResponse>, LemmyError> { ) -> Result<Json<BanPersonResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -2,7 +2,6 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{BlockPerson, BlockPersonResponse}, person::{BlockPerson, BlockPersonResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::person_block::{PersonBlock, PersonBlockForm}, source::person_block::{PersonBlock, PersonBlockForm},
@ -16,9 +15,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn block_person( pub async fn block_person(
data: Json<BlockPerson>, data: Json<BlockPerson>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<BlockPersonResponse>, LemmyError> { ) -> Result<Json<BlockPersonResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let target_id = data.person_id; let target_id = data.person_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;

View file

@ -3,9 +3,10 @@ use bcrypt::verify;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{ChangePassword, LoginResponse}, person::{ChangePassword, LoginResponse},
utils::{local_user_view_from_jwt, password_length_check}, utils::password_length_check,
}; };
use lemmy_db_schema::source::local_user::LocalUser; use lemmy_db_schema::source::local_user::LocalUser;
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{ use lemmy_utils::{
claims::Claims, claims::Claims,
error::{LemmyError, LemmyErrorType}, error::{LemmyError, LemmyErrorType},
@ -15,9 +16,8 @@ use lemmy_utils::{
pub async fn change_password( pub async fn change_password(
data: Json<ChangePassword>, data: Json<ChangePassword>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<LoginResponse>, LemmyError> { ) -> Result<Json<LoginResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), &context).await?;
password_length_check(&data.new_password)?; password_length_check(&data.new_password)?;
// Make sure passwords match // Make sure passwords match

View file

@ -1,18 +1,13 @@
use actix_web::web::{Data, Json, Query}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, person::BannedPersonsResponse, utils::is_admin};
context::LemmyContext, use lemmy_db_views::structs::LocalUserView;
person::{BannedPersonsResponse, GetBannedPersons},
utils::{is_admin, local_user_view_from_jwt},
};
use lemmy_db_views_actor::structs::PersonView; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
pub async fn list_banned_users( pub async fn list_banned_users(
data: Query<GetBannedPersons>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<BannedPersonsResponse>, LemmyError> { ) -> Result<Json<BannedPersonsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -4,7 +4,13 @@ use bcrypt::verify;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{Login, LoginResponse}, person::{Login, LoginResponse},
utils::{check_registration_application, check_user_valid}, utils,
utils::check_user_valid,
};
use lemmy_db_schema::{
source::{local_site::LocalSite, registration_application::RegistrationApplication},
utils::DbPool,
RegistrationMode,
}; };
use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{ use lemmy_utils::{
@ -72,3 +78,29 @@ pub async fn login(
registration_created: false, registration_created: false,
})) }))
} }
async fn check_registration_application(
local_user_view: &LocalUserView,
local_site: &LocalSite,
pool: &mut DbPool<'_>,
) -> Result<(), LemmyError> {
if (local_site.registration_mode == RegistrationMode::RequireApplication
|| local_site.registration_mode == RegistrationMode::Closed)
&& !local_user_view.local_user.accepted_application
&& !local_user_view.local_user.admin
{
// Fetch the registration, see if its denied
let local_user_id = local_user_view.local_user.id;
let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id).await?;
if let Some(deny_reason) = registration.deny_reason {
let lang = utils::get_interface_language(local_user_view);
let registration_denied_message = format!("{}: {}", lang.registration_denied(), deny_reason);
Err(LemmyErrorType::RegistrationDenied(
registration_denied_message,
))?
} else {
Err(LemmyErrorType::RegistrationApplicationIsPending)?
}
}
Ok(())
}

View file

@ -2,8 +2,8 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{GetPersonMentions, GetPersonMentionsResponse}, person::{GetPersonMentions, GetPersonMentionsResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::person_mention_view::PersonMentionQuery; use lemmy_db_views_actor::person_mention_view::PersonMentionQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -11,9 +11,8 @@ use lemmy_utils::error::LemmyError;
pub async fn list_mentions( pub async fn list_mentions(
data: Query<GetPersonMentions>, data: Query<GetPersonMentions>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetPersonMentionsResponse>, LemmyError> { ) -> Result<Json<GetPersonMentionsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let sort = data.sort; let sort = data.sort;
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;

View file

@ -2,8 +2,8 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{GetReplies, GetRepliesResponse}, person::{GetReplies, GetRepliesResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::comment_reply_view::CommentReplyQuery; use lemmy_db_views_actor::comment_reply_view::CommentReplyQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -11,9 +11,8 @@ use lemmy_utils::error::LemmyError;
pub async fn list_replies( pub async fn list_replies(
data: Query<GetReplies>, data: Query<GetReplies>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetRepliesResponse>, LemmyError> { ) -> Result<Json<GetRepliesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let sort = data.sort; let sort = data.sort;
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;

View file

@ -1,22 +1,18 @@
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, person::GetRepliesResponse};
context::LemmyContext,
person::{GetRepliesResponse, MarkAllAsRead},
utils::local_user_view_from_jwt,
};
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
comment_reply::CommentReply, comment_reply::CommentReply,
person_mention::PersonMention, person_mention::PersonMention,
private_message::PrivateMessage, private_message::PrivateMessage,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn mark_all_notifications_read( pub async fn mark_all_notifications_read(
data: Json<MarkAllAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetRepliesResponse>, LemmyError> { ) -> Result<Json<GetRepliesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
// Mark all comment_replies as read // Mark all comment_replies as read

View file

@ -2,12 +2,12 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{MarkPersonMentionAsRead, PersonMentionResponse}, person::{MarkPersonMentionAsRead, PersonMentionResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::person_mention::{PersonMention, PersonMentionUpdateForm}, source::person_mention::{PersonMention, PersonMentionUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::PersonMentionView; use lemmy_db_views_actor::structs::PersonMentionView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -15,9 +15,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn mark_person_mention_as_read( pub async fn mark_person_mention_as_read(
data: Json<MarkPersonMentionAsRead>, data: Json<MarkPersonMentionAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PersonMentionResponse>, LemmyError> { ) -> Result<Json<PersonMentionResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let person_mention_id = data.person_mention_id; let person_mention_id = data.person_mention_id;
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?; let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?;

View file

@ -2,12 +2,12 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{CommentReplyResponse, MarkCommentReplyAsRead}, person::{CommentReplyResponse, MarkCommentReplyAsRead},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::comment_reply::{CommentReply, CommentReplyUpdateForm}, source::comment_reply::{CommentReply, CommentReplyUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommentReplyView; use lemmy_db_views_actor::structs::CommentReplyView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -15,9 +15,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn mark_reply_as_read( pub async fn mark_reply_as_read(
data: Json<MarkCommentReplyAsRead>, data: Json<MarkCommentReplyAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentReplyResponse>, LemmyError> { ) -> Result<Json<CommentReplyResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_reply_id = data.comment_reply_id; let comment_reply_id = data.comment_reply_id;
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?; let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;

View file

@ -1,20 +1,14 @@
use actix_web::web::{Data, Json, Query}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, person::GetUnreadCountResponse};
context::LemmyContext, use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
person::{GetUnreadCount, GetUnreadCountResponse},
utils::local_user_view_from_jwt,
};
use lemmy_db_views::structs::PrivateMessageView;
use lemmy_db_views_actor::structs::{CommentReplyView, PersonMentionView}; use lemmy_db_views_actor::structs::{CommentReplyView, PersonMentionView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn unread_count( pub async fn unread_count(
data: Query<GetUnreadCount>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetUnreadCountResponse>, LemmyError> { ) -> Result<Json<GetUnreadCountResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let replies = CommentReplyView::get_unread_replies(&mut context.pool(), person_id).await?; let replies = CommentReplyView::get_unread_replies(&mut context.pool(), person_id).await?;

View file

@ -2,18 +2,21 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{GetReportCount, GetReportCountResponse}, person::{GetReportCount, GetReportCountResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::structs::{CommentReportView, PostReportView, PrivateMessageReportView}; use lemmy_db_views::structs::{
CommentReportView,
LocalUserView,
PostReportView,
PrivateMessageReportView,
};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn report_count( pub async fn report_count(
data: Json<GetReportCount>, data: Json<GetReportCount>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetReportCountResponse>, LemmyError> { ) -> Result<Json<GetReportCountResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let admin = local_user_view.local_user.admin; let admin = local_user_view.local_user.admin;
let community_id = data.community_id; let community_id = data.community_id;

View file

@ -2,7 +2,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{LoginResponse, SaveUserSettings}, person::{LoginResponse, SaveUserSettings},
utils::{local_user_view_from_jwt, sanitize_html_api_opt, send_verification_email}, utils::{sanitize_html_api_opt, send_verification_email},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,7 +13,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url}, utils::{diesel_option_overwrite, diesel_option_overwrite_to_url},
}; };
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{ use lemmy_utils::{
claims::Claims, claims::Claims,
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -24,8 +24,8 @@ use lemmy_utils::{
pub async fn save_user_settings( pub async fn save_user_settings(
data: Json<SaveUserSettings>, data: Json<SaveUserSettings>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<LoginResponse>, LemmyError> { ) -> Result<Json<LoginResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let bio = sanitize_html_api_opt(&data.bio); let bio = sanitize_html_api_opt(&data.bio);

View file

@ -5,13 +5,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{FeaturePost, PostResponse}, post::{FeaturePost, PostResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, check_community_deleted_or_removed, is_admin, is_mod_or_admin},
check_community_ban,
check_community_deleted_or_removed,
is_admin,
is_mod_or_admin,
local_user_view_from_jwt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -21,15 +15,15 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
PostFeatureType, PostFeatureType,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn feature_post( pub async fn feature_post(
data: Json<FeaturePost>, data: Json<FeaturePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_id = data.post_id; let post_id = data.post_id;
let orig_post = Post::read(&mut context.pool(), post_id).await?; let orig_post = Post::read(&mut context.pool(), post_id).await?;

View file

@ -9,7 +9,6 @@ use lemmy_api_common::{
check_community_ban, check_community_ban,
check_community_deleted_or_removed, check_community_deleted_or_removed,
check_downvotes_enabled, check_downvotes_enabled,
local_user_view_from_jwt,
mark_post_as_read, mark_post_as_read,
}, },
}; };
@ -21,6 +20,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Likeable}, traits::{Crud, Likeable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
use std::ops::Deref; use std::ops::Deref;
@ -28,8 +28,8 @@ use std::ops::Deref;
pub async fn like_post( pub async fn like_post(
data: Json<CreatePostLike>, data: Json<CreatePostLike>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Don't do a downvote if site has downvotes disabled // Don't do a downvote if site has downvotes disabled

View file

@ -5,12 +5,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{LockPost, PostResponse}, post::{LockPost, PostResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, check_community_deleted_or_removed, is_mod_or_admin},
check_community_ban,
check_community_deleted_or_removed,
is_mod_or_admin,
local_user_view_from_jwt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -19,15 +14,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn lock_post( pub async fn lock_post(
data: Json<LockPost>, data: Json<LockPost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_id = data.post_id; let post_id = data.post_id;
let orig_post = Post::read(&mut context.pool(), post_id).await?; let orig_post = Post::read(&mut context.pool(), post_id).await?;

View file

@ -3,18 +3,16 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{MarkPostAsRead, PostResponse}, post::{MarkPostAsRead, PostResponse},
utils, utils,
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::structs::PostView; use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn mark_post_as_read( pub async fn mark_post_as_read(
data: Json<MarkPostAsRead>, data: Json<MarkPostAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_id = data.post_id; let post_id = data.post_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;

View file

@ -2,22 +2,21 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{PostResponse, SavePost}, post::{PostResponse, SavePost},
utils::{local_user_view_from_jwt, mark_post_as_read}, utils::mark_post_as_read,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::post::{PostSaved, PostSavedForm}, source::post::{PostSaved, PostSavedForm},
traits::Saveable, traits::Saveable,
}; };
use lemmy_db_views::structs::PostView; use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn save_post( pub async fn save_post(
data: Json<SavePost>, data: Json<SavePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_saved_form = PostSavedForm { let post_saved_form = PostSavedForm {
post_id: data.post_id, post_id: data.post_id,
person_id: local_user_view.person.id, person_id: local_user_view.person.id,

View file

@ -5,12 +5,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{CreatePostReport, PostReportResponse}, post::{CreatePostReport, PostReportResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, sanitize_html_api, send_new_report_email_to_admins},
check_community_ban,
local_user_view_from_jwt,
sanitize_html_api,
send_new_report_email_to_admins,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -19,7 +14,7 @@ use lemmy_db_schema::{
}, },
traits::Reportable, traits::Reportable,
}; };
use lemmy_db_views::structs::{PostReportView, PostView}; use lemmy_db_views::structs::{LocalUserView, PostReportView, PostView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
/// Creates a post report and notifies the moderators of the community /// Creates a post report and notifies the moderators of the community
@ -27,8 +22,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn create_post_report( pub async fn create_post_report(
data: Json<CreatePostReport>, data: Json<CreatePostReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostReportResponse>, LemmyError> { ) -> Result<Json<PostReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let reason = sanitize_html_api(data.reason.trim()); let reason = sanitize_html_api(data.reason.trim());

View file

@ -2,9 +2,8 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{ListPostReports, ListPostReportsResponse}, post::{ListPostReports, ListPostReportsResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::post_report_view::PostReportQuery; use lemmy_db_views::{post_report_view::PostReportQuery, structs::LocalUserView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
/// Lists post reports for a community if an id is supplied /// Lists post reports for a community if an id is supplied
@ -13,9 +12,8 @@ use lemmy_utils::error::LemmyError;
pub async fn list_post_reports( pub async fn list_post_reports(
data: Query<ListPostReports>, data: Query<ListPostReports>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<ListPostReportsResponse>, LemmyError> { ) -> Result<Json<ListPostReportsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community_id = data.community_id; let community_id = data.community_id;
let unresolved_only = data.unresolved_only.unwrap_or_default(); let unresolved_only = data.unresolved_only.unwrap_or_default();

View file

@ -2,10 +2,10 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{PostReportResponse, ResolvePostReport}, post::{PostReportResponse, ResolvePostReport},
utils::{is_mod_or_admin, local_user_view_from_jwt}, utils::is_mod_or_admin,
}; };
use lemmy_db_schema::{source::post_report::PostReport, traits::Reportable}; use lemmy_db_schema::{source::post_report::PostReport, traits::Reportable};
use lemmy_db_views::structs::PostReportView; use lemmy_db_views::structs::{LocalUserView, PostReportView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
/// Resolves or unresolves a post report and notifies the moderators of the community /// Resolves or unresolves a post report and notifies the moderators of the community
@ -13,9 +13,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn resolve_post_report( pub async fn resolve_post_report(
data: Json<ResolvePostReport>, data: Json<ResolvePostReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostReportResponse>, LemmyError> { ) -> Result<Json<PostReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let report_id = data.report_id; let report_id = data.report_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let report = PostReportView::read(&mut context.pool(), report_id, person_id).await?; let report = PostReportView::read(&mut context.pool(), report_id, person_id).await?;

View file

@ -2,22 +2,20 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse}, private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::private_message::{PrivateMessage, PrivateMessageUpdateForm}, source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::PrivateMessageView; use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn mark_pm_as_read( pub async fn mark_pm_as_read(
data: Json<MarkPrivateMessageAsRead>, data: Json<MarkPrivateMessageAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageResponse>, LemmyError> { ) -> Result<Json<PrivateMessageResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Checking permissions // Checking permissions
let private_message_id = data.private_message_id; let private_message_id = data.private_message_id;
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;

View file

@ -3,7 +3,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse}, private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
utils::{local_user_view_from_jwt, sanitize_html_api, send_new_report_email_to_admins}, utils::{sanitize_html_api, send_new_report_email_to_admins},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -13,15 +13,15 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Reportable}, traits::{Crud, Reportable},
}; };
use lemmy_db_views::structs::PrivateMessageReportView; use lemmy_db_views::structs::{LocalUserView, PrivateMessageReportView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn create_pm_report( pub async fn create_pm_report(
data: Json<CreatePrivateMessageReport>, data: Json<CreatePrivateMessageReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageReportResponse>, LemmyError> { ) -> Result<Json<PrivateMessageReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let reason = sanitize_html_api(data.reason.trim()); let reason = sanitize_html_api(data.reason.trim());

View file

@ -2,18 +2,20 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{ListPrivateMessageReports, ListPrivateMessageReportsResponse}, private_message::{ListPrivateMessageReports, ListPrivateMessageReportsResponse},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
};
use lemmy_db_views::{
private_message_report_view::PrivateMessageReportQuery,
structs::LocalUserView,
}; };
use lemmy_db_views::private_message_report_view::PrivateMessageReportQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn list_pm_reports( pub async fn list_pm_reports(
data: Query<ListPrivateMessageReports>, data: Query<ListPrivateMessageReports>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<ListPrivateMessageReportsResponse>, LemmyError> { ) -> Result<Json<ListPrivateMessageReportsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let unresolved_only = data.unresolved_only.unwrap_or_default(); let unresolved_only = data.unresolved_only.unwrap_or_default();

View file

@ -2,19 +2,18 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{PrivateMessageReportResponse, ResolvePrivateMessageReport}, private_message::{PrivateMessageReportResponse, ResolvePrivateMessageReport},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::{source::private_message_report::PrivateMessageReport, traits::Reportable}; use lemmy_db_schema::{source::private_message_report::PrivateMessageReport, traits::Reportable};
use lemmy_db_views::structs::PrivateMessageReportView; use lemmy_db_views::structs::{LocalUserView, PrivateMessageReportView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn resolve_pm_report( pub async fn resolve_pm_report(
data: Json<ResolvePrivateMessageReport>, data: Json<ResolvePrivateMessageReport>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageReportResponse>, LemmyError> { ) -> Result<Json<PrivateMessageReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let report_id = data.report_id; let report_id = data.report_id;

View file

@ -3,21 +3,20 @@ use actix_web::web::Json;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{BlockInstance, BlockInstanceResponse}, site::{BlockInstance, BlockInstanceResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::instance_block::{InstanceBlock, InstanceBlockForm}, source::instance_block::{InstanceBlock, InstanceBlockForm},
traits::Blockable, traits::Blockable,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn block_instance( pub async fn block_instance(
data: Json<BlockInstance>, data: Json<BlockInstance>,
local_user_view: LocalUserView,
context: Data<LemmyContext>, context: Data<LemmyContext>,
) -> Result<Json<BlockInstanceResponse>, LemmyError> { ) -> Result<Json<BlockInstanceResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let instance_id = data.instance_id; let instance_id = data.instance_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let instance_block_form = InstanceBlockForm { let instance_block_form = InstanceBlockForm {

View file

@ -1,9 +1,5 @@
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, site::GetSiteResponse, utils::is_admin};
context::LemmyContext,
site::{GetSiteResponse, LeaveAdmin},
utils::{is_admin, local_user_view_from_jwt},
};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
actor_language::SiteLanguage, actor_language::SiteLanguage,
@ -14,7 +10,7 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::{CustomEmojiView, SiteView}; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
use lemmy_db_views_actor::structs::PersonView; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType}, error::{LemmyError, LemmyErrorType},
@ -23,11 +19,9 @@ use lemmy_utils::{
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn leave_admin( pub async fn leave_admin(
data: Json<LeaveAdmin>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetSiteResponse>, LemmyError> { ) -> Result<Json<GetSiteResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
// Make sure there isn't just one admin (so if one leaves, there will still be one left) // Make sure there isn't just one admin (so if one leaves, there will still be one left)

View file

@ -2,13 +2,14 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{GetModlog, GetModlogResponse}, site::{GetModlog, GetModlogResponse},
utils::{check_private_instance, is_admin, is_mod_or_admin, local_user_view_from_jwt_opt}, utils::{check_private_instance, is_admin, is_mod_or_admin},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
source::local_site::LocalSite, source::local_site::LocalSite,
ModlogActionType, ModlogActionType,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_moderator::structs::{ use lemmy_db_views_moderator::structs::{
AdminPurgeCommentView, AdminPurgeCommentView,
AdminPurgeCommunityView, AdminPurgeCommunityView,
@ -34,8 +35,8 @@ use ModlogActionType::*;
pub async fn get_mod_log( pub async fn get_mod_log(
data: Query<GetModlog>, data: Query<GetModlog>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<GetModlogResponse>, LemmyError> { ) -> Result<Json<GetModlogResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -2,7 +2,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{PurgeComment, PurgeItemResponse}, site::{PurgeComment, PurgeItemResponse},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api_opt}, utils::{is_admin, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -11,15 +11,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn purge_comment( pub async fn purge_comment(
data: Json<PurgeComment>, data: Json<PurgeComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PurgeItemResponse>, LemmyError> { ) -> Result<Json<PurgeItemResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Only let admin purge an item // Only let admin purge an item
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -3,12 +3,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
request::purge_image_from_pictrs, request::purge_image_from_pictrs,
site::{PurgeCommunity, PurgeItemResponse}, site::{PurgeCommunity, PurgeItemResponse},
utils::{ utils::{is_admin, purge_image_posts_for_community, sanitize_html_api_opt},
is_admin,
local_user_view_from_jwt,
purge_image_posts_for_community,
sanitize_html_api_opt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -17,15 +12,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn purge_community( pub async fn purge_community(
data: Json<PurgeCommunity>, data: Json<PurgeCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PurgeItemResponse>, LemmyError> { ) -> Result<Json<PurgeItemResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Only let admin purge an item // Only let admin purge an item
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -3,7 +3,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
request::delete_image_from_pictrs, request::delete_image_from_pictrs,
site::{PurgeItemResponse, PurgePerson}, site::{PurgeItemResponse, PurgePerson},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api_opt}, utils::{is_admin, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -20,9 +20,8 @@ use lemmy_utils::error::LemmyError;
pub async fn purge_person( pub async fn purge_person(
data: Json<PurgePerson>, data: Json<PurgePerson>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PurgeItemResponse>, LemmyError> { ) -> Result<Json<PurgeItemResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Only let admin purge an item // Only let admin purge an item
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -3,7 +3,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
request::purge_image_from_pictrs, request::purge_image_from_pictrs,
site::{PurgeItemResponse, PurgePost}, site::{PurgeItemResponse, PurgePost},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api_opt}, utils::{is_admin, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -12,15 +12,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn purge_post( pub async fn purge_post(
data: Json<PurgePost>, data: Json<PurgePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PurgeItemResponse>, LemmyError> { ) -> Result<Json<PurgeItemResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Only let admin purge an item // Only let admin purge an item
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -2,7 +2,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{ApproveRegistrationApplication, RegistrationApplicationResponse}, site::{ApproveRegistrationApplication, RegistrationApplicationResponse},
utils::{is_admin, local_user_view_from_jwt, send_application_approved_email}, utils::{is_admin, send_application_approved_email},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -18,9 +18,8 @@ use lemmy_utils::error::LemmyError;
pub async fn approve_registration_application( pub async fn approve_registration_application(
data: Json<ApproveRegistrationApplication>, data: Json<ApproveRegistrationApplication>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<RegistrationApplicationResponse>, LemmyError> { ) -> Result<Json<RegistrationApplicationResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let app_id = data.id; let app_id = data.id;
// Only let admins do this // Only let admins do this

View file

@ -2,18 +2,21 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{ListRegistrationApplications, ListRegistrationApplicationsResponse}, site::{ListRegistrationApplications, ListRegistrationApplicationsResponse},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views::registration_application_view::RegistrationApplicationQuery; use lemmy_db_views::{
registration_application_view::RegistrationApplicationQuery,
structs::LocalUserView,
};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
/// Lists registration applications, filterable by undenied only. /// Lists registration applications, filterable by undenied only.
pub async fn list_registration_applications( pub async fn list_registration_applications(
data: Query<ListRegistrationApplications>, data: Query<ListRegistrationApplications>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<ListRegistrationApplicationsResponse>, LemmyError> { ) -> Result<Json<ListRegistrationApplicationsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin // Make sure user is an admin

View file

@ -1,18 +1,17 @@
use actix_web::web::{Data, Json, Query}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{GetUnreadRegistrationApplicationCount, GetUnreadRegistrationApplicationCountResponse}, site::GetUnreadRegistrationApplicationCountResponse,
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views::structs::RegistrationApplicationView; use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
pub async fn get_unread_registration_application_count( pub async fn get_unread_registration_application_count(
data: Query<GetUnreadRegistrationApplicationCount>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<GetUnreadRegistrationApplicationCountResponse>, LemmyError> { ) -> Result<Json<GetUnreadRegistrationApplicationCountResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Only let admins do this // Only let admins do this

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, CommentReportId, CommunityId, LanguageId, LocalUserId, PostId}, newtypes::{CommentId, CommentReportId, CommunityId, LanguageId, LocalUserId, PostId},
CommentSortType, CommentSortType,
@ -20,7 +19,6 @@ pub struct CreateComment {
pub post_id: PostId, pub post_id: PostId,
pub parent_id: Option<CommentId>, pub parent_id: Option<CommentId>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -30,7 +28,6 @@ pub struct CreateComment {
/// Fetch an individual comment. /// Fetch an individual comment.
pub struct GetComment { pub struct GetComment {
pub id: CommentId, pub id: CommentId,
pub auth: Option<Sensitive<String>>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -42,7 +39,6 @@ pub struct EditComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub content: Option<String>, pub content: Option<String>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -53,7 +49,6 @@ pub struct EditComment {
pub struct DistinguishComment { pub struct DistinguishComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub distinguished: bool, pub distinguished: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -64,7 +59,6 @@ pub struct DistinguishComment {
pub struct DeleteComment { pub struct DeleteComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub deleted: bool, pub deleted: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -76,7 +70,6 @@ pub struct RemoveComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub removed: bool, pub removed: bool,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -86,7 +79,6 @@ pub struct RemoveComment {
pub struct SaveComment { pub struct SaveComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub save: bool, pub save: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -107,7 +99,6 @@ pub struct CreateCommentLike {
pub comment_id: CommentId, pub comment_id: CommentId,
/// Must be -1, 0, or 1 . /// Must be -1, 0, or 1 .
pub score: i16, pub score: i16,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -128,7 +119,6 @@ pub struct GetComments {
pub saved_only: Option<bool>, pub saved_only: Option<bool>,
pub liked_only: Option<bool>, pub liked_only: Option<bool>,
pub disliked_only: Option<bool>, pub disliked_only: Option<bool>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -146,7 +136,6 @@ pub struct GetCommentsResponse {
pub struct CreateCommentReport { pub struct CreateCommentReport {
pub comment_id: CommentId, pub comment_id: CommentId,
pub reason: String, pub reason: String,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -164,7 +153,6 @@ pub struct CommentReportResponse {
pub struct ResolveCommentReport { pub struct ResolveCommentReport {
pub report_id: CommentReportId, pub report_id: CommentReportId,
pub resolved: bool, pub resolved: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -179,7 +167,6 @@ pub struct ListCommentReports {
pub unresolved_only: Option<bool>, pub unresolved_only: Option<bool>,
/// 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
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommunityId, LanguageId, PersonId}, newtypes::{CommunityId, LanguageId, PersonId},
source::site::Site, source::site::Site,
@ -20,7 +19,6 @@ pub struct GetCommunity {
pub id: Option<CommunityId>, pub id: Option<CommunityId>,
/// Example: star_trek , or star_trek@xyz.tld /// Example: star_trek , or star_trek@xyz.tld
pub name: Option<String>, pub name: Option<String>,
pub auth: Option<Sensitive<String>>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -56,7 +54,6 @@ pub struct CreateCommunity {
/// Whether to restrict posting only to moderators. /// Whether to restrict posting only to moderators.
pub posting_restricted_to_mods: Option<bool>, pub posting_restricted_to_mods: Option<bool>,
pub discussion_languages: Option<Vec<LanguageId>>, pub discussion_languages: Option<Vec<LanguageId>>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -79,7 +76,6 @@ pub struct ListCommunities {
pub show_nsfw: Option<bool>, pub show_nsfw: Option<bool>,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -102,7 +98,6 @@ pub struct BanFromCommunity {
pub remove_data: Option<bool>, pub remove_data: Option<bool>,
pub reason: Option<String>, pub reason: Option<String>,
pub expires: Option<i64>, pub expires: Option<i64>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -122,7 +117,6 @@ pub struct AddModToCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub person_id: PersonId, pub person_id: PersonId,
pub added: bool, pub added: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -153,7 +147,6 @@ pub struct EditCommunity {
/// Whether to restrict posting only to moderators. /// Whether to restrict posting only to moderators.
pub posting_restricted_to_mods: Option<bool>, pub posting_restricted_to_mods: Option<bool>,
pub discussion_languages: Option<Vec<LanguageId>>, pub discussion_languages: Option<Vec<LanguageId>>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -166,7 +159,6 @@ pub struct HideCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub hidden: bool, pub hidden: bool,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -177,7 +169,6 @@ pub struct HideCommunity {
pub struct DeleteCommunity { pub struct DeleteCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub deleted: bool, pub deleted: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -190,7 +181,6 @@ pub struct RemoveCommunity {
pub removed: bool, pub removed: bool,
pub reason: Option<String>, pub reason: Option<String>,
pub expires: Option<i64>, pub expires: Option<i64>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -200,7 +190,6 @@ pub struct RemoveCommunity {
pub struct FollowCommunity { pub struct FollowCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub follow: bool, pub follow: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -210,7 +199,6 @@ pub struct FollowCommunity {
pub struct BlockCommunity { pub struct BlockCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub block: bool, pub block: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -230,5 +218,4 @@ pub struct BlockCommunityResponse {
pub struct TransferCommunity { pub struct TransferCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub person_id: PersonId, pub person_id: PersonId,
pub auth: Sensitive<String>,
} }

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::newtypes::CustomEmojiId; use lemmy_db_schema::newtypes::CustomEmojiId;
use lemmy_db_views::structs::CustomEmojiView; use lemmy_db_views::structs::CustomEmojiView;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -17,7 +16,6 @@ pub struct CreateCustomEmoji {
pub image_url: Url, pub image_url: Url,
pub alt_text: String, pub alt_text: String,
pub keywords: Vec<String>, pub keywords: Vec<String>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -31,7 +29,6 @@ pub struct EditCustomEmoji {
pub image_url: Url, pub image_url: Url,
pub alt_text: String, pub alt_text: String,
pub keywords: Vec<String>, pub keywords: Vec<String>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -40,7 +37,6 @@ pub struct EditCustomEmoji {
/// Delete a custom emoji. /// Delete a custom emoji.
pub struct DeleteCustomEmoji { pub struct DeleteCustomEmoji {
pub id: CustomEmojiId, pub id: CustomEmojiId,
pub auth: Sensitive<String>,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]

View file

@ -119,7 +119,6 @@ pub struct SaveUserSettings {
pub show_new_post_notifs: Option<bool>, pub show_new_post_notifs: Option<bool>,
/// A list of languages you are able to see discussion in. /// A list of languages you are able to see discussion in.
pub discussion_languages: Option<Vec<LanguageId>>, pub discussion_languages: Option<Vec<LanguageId>>,
pub auth: Sensitive<String>,
/// Open links in a new tab /// Open links in a new tab
pub open_links_in_new_tab: Option<bool>, pub open_links_in_new_tab: Option<bool>,
/// Enable infinite scroll /// Enable infinite scroll
@ -134,7 +133,6 @@ pub struct ChangePassword {
pub new_password: Sensitive<String>, pub new_password: Sensitive<String>,
pub new_password_verify: Sensitive<String>, pub new_password_verify: Sensitive<String>,
pub old_password: Sensitive<String>, pub old_password: Sensitive<String>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -167,7 +165,6 @@ pub struct GetPersonDetails {
pub limit: Option<i64>, pub limit: Option<i64>,
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
pub saved_only: Option<bool>, pub saved_only: Option<bool>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -181,14 +178,6 @@ pub struct GetPersonDetailsResponse {
pub moderates: Vec<CommunityModeratorView>, pub moderates: Vec<CommunityModeratorView>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Marks all notifications as read.
pub struct MarkAllAsRead {
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -196,7 +185,6 @@ pub struct MarkAllAsRead {
pub struct AddAdmin { pub struct AddAdmin {
pub person_id: PersonId, pub person_id: PersonId,
pub added: bool, pub added: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -219,18 +207,9 @@ pub struct BanPerson {
pub remove_data: Option<bool>, pub remove_data: Option<bool>,
pub reason: Option<String>, pub reason: Option<String>,
pub expires: Option<i64>, pub expires: Option<i64>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Get a list of banned persons.
// TODO, this should be paged, since the list can be quite long. // TODO, this should be paged, since the list can be quite long.
pub struct GetBannedPersons {
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -255,7 +234,6 @@ pub struct BanPersonResponse {
pub struct BlockPerson { pub struct BlockPerson {
pub person_id: PersonId, pub person_id: PersonId,
pub block: bool, pub block: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -277,7 +255,6 @@ pub struct GetReplies {
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub unread_only: Option<bool>, pub unread_only: Option<bool>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -299,7 +276,6 @@ pub struct GetPersonMentions {
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub unread_only: Option<bool>, pub unread_only: Option<bool>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -317,7 +293,6 @@ pub struct GetPersonMentionsResponse {
pub struct MarkPersonMentionAsRead { pub struct MarkPersonMentionAsRead {
pub person_mention_id: PersonMentionId, pub person_mention_id: PersonMentionId,
pub read: bool, pub read: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -335,7 +310,6 @@ pub struct PersonMentionResponse {
pub struct MarkCommentReplyAsRead { pub struct MarkCommentReplyAsRead {
pub comment_reply_id: CommentReplyId, pub comment_reply_id: CommentReplyId,
pub read: bool, pub read: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -353,7 +327,6 @@ pub struct CommentReplyResponse {
pub struct DeleteAccount { pub struct DeleteAccount {
pub password: Sensitive<String>, pub password: Sensitive<String>,
pub delete_content: bool, pub delete_content: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -393,7 +366,6 @@ pub struct PasswordChangeAfterReset {
/// Get a count of the number of reports. /// Get a count of the number of reports.
pub struct GetReportCount { pub struct GetReportCount {
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -408,14 +380,6 @@ pub struct GetReportCountResponse {
pub private_message_reports: Option<i64>, pub private_message_reports: Option<i64>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Get a count of unread notifications.
pub struct GetUnreadCount {
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId}, newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId},
ListingType, ListingType,
@ -29,7 +28,6 @@ pub struct CreatePost {
pub honeypot: Option<String>, pub honeypot: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -47,7 +45,6 @@ pub struct PostResponse {
pub struct GetPost { pub struct GetPost {
pub id: Option<PostId>, pub id: Option<PostId>,
pub comment_id: Option<CommentId>, pub comment_id: Option<CommentId>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -78,7 +75,6 @@ pub struct GetPosts {
pub saved_only: Option<bool>, pub saved_only: Option<bool>,
pub liked_only: Option<bool>, pub liked_only: Option<bool>,
pub disliked_only: Option<bool>, pub disliked_only: Option<bool>,
pub auth: Option<Sensitive<String>>,
pub page_cursor: Option<PaginationCursor>, pub page_cursor: Option<PaginationCursor>,
} }
@ -101,7 +97,6 @@ pub struct CreatePostLike {
pub post_id: PostId, pub post_id: PostId,
/// Score must be -1, 0, or 1. /// Score must be -1, 0, or 1.
pub score: i16, pub score: i16,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -118,7 +113,6 @@ pub struct EditPost {
pub body: Option<String>, pub body: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -128,7 +122,6 @@ pub struct EditPost {
pub struct DeletePost { pub struct DeletePost {
pub post_id: PostId, pub post_id: PostId,
pub deleted: bool, pub deleted: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -140,7 +133,6 @@ pub struct RemovePost {
pub post_id: PostId, pub post_id: PostId,
pub removed: bool, pub removed: bool,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -150,7 +142,6 @@ pub struct RemovePost {
pub struct MarkPostAsRead { pub struct MarkPostAsRead {
pub post_id: PostId, pub post_id: PostId,
pub read: bool, pub read: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -160,7 +151,6 @@ pub struct MarkPostAsRead {
pub struct LockPost { pub struct LockPost {
pub post_id: PostId, pub post_id: PostId,
pub locked: bool, pub locked: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -171,7 +161,6 @@ pub struct FeaturePost {
pub post_id: PostId, pub post_id: PostId,
pub featured: bool, pub featured: bool,
pub feature_type: PostFeatureType, pub feature_type: PostFeatureType,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -181,7 +170,6 @@ pub struct FeaturePost {
pub struct SavePost { pub struct SavePost {
pub post_id: PostId, pub post_id: PostId,
pub save: bool, pub save: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -191,7 +179,6 @@ pub struct SavePost {
pub struct CreatePostReport { pub struct CreatePostReport {
pub post_id: PostId, pub post_id: PostId,
pub reason: String, pub reason: String,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -209,7 +196,6 @@ pub struct PostReportResponse {
pub struct ResolvePostReport { pub struct ResolvePostReport {
pub report_id: PostReportId, pub report_id: PostReportId,
pub resolved: bool, pub resolved: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -224,7 +210,6 @@ pub struct ListPostReports {
pub unresolved_only: Option<bool>, pub unresolved_only: Option<bool>,
/// 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
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::newtypes::{PersonId, PrivateMessageId, PrivateMessageReportId}; use lemmy_db_schema::newtypes::{PersonId, PrivateMessageId, PrivateMessageReportId};
use lemmy_db_views::structs::{PrivateMessageReportView, PrivateMessageView}; use lemmy_db_views::structs::{PrivateMessageReportView, PrivateMessageView};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -13,7 +12,6 @@ use ts_rs::TS;
pub struct CreatePrivateMessage { pub struct CreatePrivateMessage {
pub content: String, pub content: String,
pub recipient_id: PersonId, pub recipient_id: PersonId,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -23,7 +21,6 @@ pub struct CreatePrivateMessage {
pub struct EditPrivateMessage { pub struct EditPrivateMessage {
pub private_message_id: PrivateMessageId, pub private_message_id: PrivateMessageId,
pub content: String, pub content: String,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -33,7 +30,6 @@ pub struct EditPrivateMessage {
pub struct DeletePrivateMessage { pub struct DeletePrivateMessage {
pub private_message_id: PrivateMessageId, pub private_message_id: PrivateMessageId,
pub deleted: bool, pub deleted: bool,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -43,7 +39,6 @@ pub struct DeletePrivateMessage {
pub struct MarkPrivateMessageAsRead { pub struct MarkPrivateMessageAsRead {
pub private_message_id: PrivateMessageId, pub private_message_id: PrivateMessageId,
pub read: bool, pub read: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -56,7 +51,6 @@ pub struct GetPrivateMessages {
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub creator_id: Option<PersonId>, pub creator_id: Option<PersonId>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -82,7 +76,6 @@ pub struct PrivateMessageResponse {
pub struct CreatePrivateMessageReport { pub struct CreatePrivateMessageReport {
pub private_message_id: PrivateMessageId, pub private_message_id: PrivateMessageId,
pub reason: String, pub reason: String,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -100,7 +93,6 @@ pub struct PrivateMessageReportResponse {
pub struct ResolvePrivateMessageReport { pub struct ResolvePrivateMessageReport {
pub report_id: PrivateMessageReportId, pub report_id: PrivateMessageReportId,
pub resolved: bool, pub resolved: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -114,7 +106,6 @@ pub struct ListPrivateMessageReports {
pub limit: Option<i64>, pub limit: Option<i64>,
/// Only shows the unresolved reports /// Only shows the unresolved reports
pub unresolved_only: Option<bool>, pub unresolved_only: Option<bool>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -18,7 +18,15 @@ use lemmy_db_schema::{
}; };
use lemmy_db_views::structs::PrivateMessageView; use lemmy_db_views::structs::PrivateMessageView;
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;
use once_cell::sync::OnceCell; use once_cell::sync::{Lazy, OnceCell};
use tokio::{
sync::{
mpsc,
mpsc::{UnboundedReceiver, UnboundedSender, WeakUnboundedSender},
Mutex,
},
task::JoinHandle,
};
use url::Url; use url::Url;
type MatchOutgoingActivitiesBoxed = type MatchOutgoingActivitiesBoxed =
@ -54,16 +62,45 @@ pub enum SendActivityData {
CreateReport(Url, Person, Community, String), CreateReport(Url, Person, Community, String),
} }
pub struct ActivityChannel; // TODO: instead of static, move this into LemmyContext. make sure that stopping the process with
// ctrl+c still works.
static ACTIVITY_CHANNEL: Lazy<ActivityChannel> = Lazy::new(|| {
let (sender, receiver) = mpsc::unbounded_channel();
let weak_sender = sender.downgrade();
ActivityChannel {
weak_sender,
receiver: Mutex::new(receiver),
keepalive_sender: Mutex::new(Some(sender)),
}
});
pub struct ActivityChannel {
weak_sender: WeakUnboundedSender<SendActivityData>,
receiver: Mutex<UnboundedReceiver<SendActivityData>>,
keepalive_sender: Mutex<Option<UnboundedSender<SendActivityData>>>,
}
impl ActivityChannel { impl ActivityChannel {
pub async fn retrieve_activity() -> Option<SendActivityData> {
let mut lock = ACTIVITY_CHANNEL.receiver.lock().await;
lock.recv().await
}
pub async fn submit_activity( pub async fn submit_activity(
data: SendActivityData, data: SendActivityData,
context: &Data<LemmyContext>, _context: &Data<LemmyContext>,
) -> LemmyResult<()> { ) -> LemmyResult<()> {
MATCH_OUTGOING_ACTIVITIES // could do `ACTIVITY_CHANNEL.keepalive_sender.lock()` instead and get rid of weak_sender,
.get() // not sure which way is more efficient
.expect("retrieve function pointer")(data, context) if let Some(sender) = ACTIVITY_CHANNEL.weak_sender.upgrade() {
.await sender.send(data)?;
}
Ok(())
}
pub async fn close(outgoing_activities_task: JoinHandle<LemmyResult<()>>) -> LemmyResult<()> {
ACTIVITY_CHANNEL.keepalive_sender.lock().await.take();
outgoing_activities_task.await??;
Ok(())
} }
} }

View file

@ -1,4 +1,3 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, InstanceId, LanguageId, PersonId, PostId}, newtypes::{CommentId, CommunityId, InstanceId, LanguageId, PersonId, PostId},
source::{instance::Instance, language::Language, tagline::Tagline}, source::{instance::Instance, language::Language, tagline::Tagline},
@ -62,7 +61,6 @@ pub struct Search {
pub listing_type: Option<ListingType>, pub listing_type: Option<ListingType>,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -85,7 +83,6 @@ pub struct SearchResponse {
pub struct ResolveObject { pub struct ResolveObject {
/// Can be the full url, or a shortened version like: !fediverse@lemmy.ml /// Can be the full url, or a shortened version like: !fediverse@lemmy.ml
pub q: String, pub q: String,
pub auth: Option<Sensitive<String>>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -113,7 +110,6 @@ pub struct GetModlog {
pub limit: Option<i64>, pub limit: Option<i64>,
pub type_: Option<ModlogActionType>, pub type_: Option<ModlogActionType>,
pub other_person_id: Option<PersonId>, pub other_person_id: Option<PersonId>,
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -184,7 +180,6 @@ pub struct CreateSite {
pub blocked_instances: Option<Vec<String>>, pub blocked_instances: Option<Vec<String>>,
pub taglines: Option<Vec<String>>, pub taglines: Option<Vec<String>>,
pub registration_mode: Option<RegistrationMode>, pub registration_mode: Option<RegistrationMode>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -263,16 +258,6 @@ pub struct EditSite {
pub registration_mode: Option<RegistrationMode>, pub registration_mode: Option<RegistrationMode>,
/// Whether to email admins for new reports. /// Whether to email admins for new reports.
pub reports_email_admins: Option<bool>, pub reports_email_admins: Option<bool>,
pub auth: Sensitive<String>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Fetches the site.
pub struct GetSite {
pub auth: Option<Sensitive<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -326,14 +311,6 @@ pub struct MyUserInfo {
pub discussion_languages: Vec<LanguageId>, pub discussion_languages: Vec<LanguageId>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Leaves the admin team.
pub struct LeaveAdmin {
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -352,7 +329,6 @@ pub struct FederatedInstances {
pub struct PurgePerson { pub struct PurgePerson {
pub person_id: PersonId, pub person_id: PersonId,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -363,7 +339,6 @@ pub struct PurgePerson {
pub struct PurgeCommunity { pub struct PurgeCommunity {
pub community_id: CommunityId, pub community_id: CommunityId,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -374,7 +349,6 @@ pub struct PurgeCommunity {
pub struct PurgePost { pub struct PurgePost {
pub post_id: PostId, pub post_id: PostId,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]
@ -385,7 +359,6 @@ pub struct PurgePost {
pub struct PurgeComment { pub struct PurgeComment {
pub comment_id: CommentId, pub comment_id: CommentId,
pub reason: Option<String>, pub reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
@ -406,7 +379,6 @@ pub struct ListRegistrationApplications {
pub unread_only: Option<bool>, pub unread_only: Option<bool>,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -426,7 +398,6 @@ pub struct ApproveRegistrationApplication {
pub id: i32, pub id: i32,
pub approve: bool, pub approve: bool,
pub deny_reason: Option<String>, pub deny_reason: Option<String>,
pub auth: Sensitive<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -437,14 +408,6 @@ pub struct RegistrationApplicationResponse {
pub registration_application: RegistrationApplicationView, pub registration_application: RegistrationApplicationView,
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Gets a count of unread registration applications.
pub struct GetUnreadRegistrationApplicationCount {
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -460,7 +423,6 @@ pub struct GetUnreadRegistrationApplicationCountResponse {
pub struct BlockInstance { pub struct BlockInstance {
pub instance_id: InstanceId, pub instance_id: InstanceId,
pub block: bool, pub block: bool,
pub auth: Sensitive<String>,
} }
#[skip_serializing_none] #[skip_serializing_none]

View file

@ -1,14 +1,9 @@
use crate::{ use crate::{context::LemmyContext, request::purge_image_from_pictrs, site::FederatedInstances};
context::LemmyContext,
request::purge_image_from_pictrs,
sensitive::Sensitive,
site::FederatedInstances,
};
use anyhow::Context; use anyhow::Context;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use lemmy_db_schema::{ use lemmy_db_schema::{
impls::person::is_banned, impls::person::is_banned,
newtypes::{CommunityId, DbUrl, LocalUserId, PersonId, PostId}, newtypes::{CommunityId, DbUrl, PersonId, PostId},
source::{ source::{
comment::{Comment, CommentUpdateForm}, comment::{Comment, CommentUpdateForm},
community::{Community, CommunityModerator, CommunityUpdateForm}, community::{Community, CommunityModerator, CommunityUpdateForm},
@ -20,11 +15,9 @@ use lemmy_db_schema::{
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
person_block::PersonBlock, person_block::PersonBlock,
post::{Post, PostRead, PostReadForm}, post::{Post, PostRead, PostReadForm},
registration_application::RegistrationApplication,
}, },
traits::{Crud, Readable}, traits::{Crud, Readable},
utils::DbPool, utils::DbPool,
RegistrationMode,
}; };
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView}; use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
use lemmy_db_views_actor::structs::{ use lemmy_db_views_actor::structs::{
@ -33,9 +26,8 @@ use lemmy_db_views_actor::structs::{
CommunityView, CommunityView,
}; };
use lemmy_utils::{ use lemmy_utils::{
claims::Claims,
email::{send_email, translations::Lang}, email::{send_email, translations::Lang},
error::{LemmyError, LemmyErrorExt, LemmyErrorExt2, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
location_info, location_info,
rate_limit::RateLimitConfig, rate_limit::RateLimitConfig,
settings::structs::Settings, settings::structs::Settings,
@ -134,58 +126,6 @@ pub async fn mark_post_as_unread(
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead) .with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)
} }
#[tracing::instrument(skip_all)]
pub async fn local_user_view_from_jwt(
jwt: &str,
context: &LemmyContext,
) -> Result<LocalUserView, LemmyError> {
let claims = Claims::decode(jwt, &context.secret().jwt_secret)
.with_lemmy_type(LemmyErrorType::NotLoggedIn)?
.claims;
let local_user_id = LocalUserId(claims.sub);
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id).await?;
check_user_valid(
local_user_view.person.banned,
local_user_view.person.ban_expires,
local_user_view.person.deleted,
)?;
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
Ok(local_user_view)
}
#[tracing::instrument(skip_all)]
pub async fn local_user_view_from_jwt_opt(
jwt: Option<&Sensitive<String>>,
context: &LemmyContext,
) -> Option<LocalUserView> {
local_user_view_from_jwt(jwt?, context).await.ok()
}
#[tracing::instrument(skip_all)]
pub async fn local_user_view_from_jwt_opt_new(
local_user_view: &mut Option<LocalUserView>,
jwt: Option<&Sensitive<String>>,
context: &LemmyContext,
) {
if local_user_view.is_none() {
*local_user_view = local_user_view_from_jwt_opt(jwt, context).await;
}
}
/// Checks if user's token was issued before user's password reset.
pub fn check_validator_time(
validator_time: &DateTime<Utc>,
claims: &Claims,
) -> Result<(), LemmyError> {
let user_validation_time = validator_time.timestamp();
if user_validation_time > claims.iat {
Err(LemmyErrorType::NotLoggedIn)?
} else {
Ok(())
}
}
pub fn check_user_valid( pub fn check_user_valid(
banned: bool, banned: bool,
ban_expires: Option<DateTime<Utc>>, ban_expires: Option<DateTime<Utc>>,
@ -505,32 +445,6 @@ pub async fn send_new_report_email_to_admins(
Ok(()) Ok(())
} }
pub async fn check_registration_application(
local_user_view: &LocalUserView,
local_site: &LocalSite,
pool: &mut DbPool<'_>,
) -> Result<(), LemmyError> {
if (local_site.registration_mode == RegistrationMode::RequireApplication
|| local_site.registration_mode == RegistrationMode::Closed)
&& !local_user_view.local_user.accepted_application
&& !local_user_view.local_user.admin
{
// Fetch the registration, see if its denied
let local_user_id = local_user_view.local_user.id;
let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id).await?;
if let Some(deny_reason) = registration.deny_reason {
let lang = get_interface_language(local_user_view);
let registration_denied_message = format!("{}: {}", lang.registration_denied(), deny_reason);
Err(LemmyErrorType::RegistrationDenied(
registration_denied_message,
))?
} else {
Err(LemmyErrorType::RegistrationApplicationIsPending)?
}
}
Ok(())
}
pub fn check_private_instance_and_federation_enabled( pub fn check_private_instance_and_federation_enabled(
local_site: &LocalSite, local_site: &LocalSite,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {

View file

@ -12,7 +12,6 @@ use lemmy_api_common::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
get_post, get_post,
local_site_to_slur_regex, local_site_to_slur_regex,
local_user_view_from_jwt,
sanitize_html_api, sanitize_html_api,
EndpointType, EndpointType,
}, },
@ -28,6 +27,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Likeable}, traits::{Crud, Likeable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{ utils::{
@ -43,8 +43,8 @@ const MAX_COMMENT_DEPTH_LIMIT: usize = 100;
pub async fn create_comment( pub async fn create_comment(
data: Json<CreateComment>, data: Json<CreateComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let content = remove_slurs( let content = remove_slurs(

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
comment::{CommentResponse, DeleteComment}, comment::{CommentResponse, DeleteComment},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, local_user_view_from_jwt}, utils::check_community_ban,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -14,16 +14,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn delete_comment( pub async fn delete_comment(
data: Json<DeleteComment>, data: Json<DeleteComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_id = data.comment_id; let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;

View file

@ -3,17 +3,18 @@ use lemmy_api_common::{
build_response::build_comment_response, build_response::build_comment_response,
comment::{CommentResponse, GetComment}, comment::{CommentResponse, GetComment},
context::LemmyContext, context::LemmyContext,
utils::{check_private_instance, local_user_view_from_jwt_opt}, utils::check_private_instance,
}; };
use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn get_comment( pub async fn get_comment(
data: Query<GetComment>, data: Query<GetComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
comment::{CommentResponse, RemoveComment}, comment::{CommentResponse, RemoveComment},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, utils::{check_community_ban, is_mod_or_admin},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -15,16 +15,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn remove_comment( pub async fn remove_comment(
data: Json<RemoveComment>, data: Json<RemoveComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_id = data.comment_id; let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;

View file

@ -5,12 +5,7 @@ use lemmy_api_common::{
comment::{CommentResponse, EditComment}, comment::{CommentResponse, EditComment},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, local_site_to_slur_regex, sanitize_html_api_opt},
check_community_ban,
local_site_to_slur_regex,
local_user_view_from_jwt,
sanitize_html_api_opt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -21,7 +16,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::naive_now, utils::naive_now,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{ utils::{
@ -35,8 +30,8 @@ use lemmy_utils::{
pub async fn update_comment( pub async fn update_comment(
data: Json<EditComment>, data: Json<EditComment>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommentResponse>, LemmyError> { ) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let comment_id = data.comment_id; let comment_id = data.comment_id;

View file

@ -11,7 +11,6 @@ use lemmy_api_common::{
generate_shared_inbox_url, generate_shared_inbox_url,
is_admin, is_admin,
local_site_to_slur_regex, local_site_to_slur_regex,
local_user_view_from_jwt,
sanitize_html_api, sanitize_html_api,
sanitize_html_api_opt, sanitize_html_api_opt,
EndpointType, EndpointType,
@ -32,7 +31,7 @@ use lemmy_db_schema::{
traits::{ApubActor, Crud, Followable, Joinable}, traits::{ApubActor, Crud, Followable, Joinable},
utils::diesel_option_overwrite_to_url_create, utils::diesel_option_overwrite_to_url_create,
}; };
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{ utils::{
@ -45,8 +44,8 @@ use lemmy_utils::{
pub async fn create_community( pub async fn create_community(
data: Json<CreateCommunity>, data: Json<CreateCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_site = site_view.local_site; let local_site = site_view.local_site;

View file

@ -5,12 +5,13 @@ use lemmy_api_common::{
community::{CommunityResponse, DeleteCommunity}, community::{CommunityResponse, DeleteCommunity},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{is_top_mod, local_user_view_from_jwt}, utils::is_top_mod,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::community::{Community, CommunityUpdateForm}, source::community::{Community, CommunityUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityModeratorView; use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -18,9 +19,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn delete_community( pub async fn delete_community(
data: Json<DeleteCommunity>, data: Json<DeleteCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Fetch the community mods // Fetch the community mods
let community_id = data.community_id; let community_id = data.community_id;
let community_mods = let community_mods =

View file

@ -2,9 +2,10 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
community::{ListCommunities, ListCommunitiesResponse}, community::{ListCommunities, ListCommunitiesResponse},
context::LemmyContext, context::LemmyContext,
utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt}, utils::{check_private_instance, is_admin},
}; };
use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::community_view::CommunityQuery; use lemmy_db_views_actor::community_view::CommunityQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -12,8 +13,8 @@ use lemmy_utils::error::LemmyError;
pub async fn list_communities( pub async fn list_communities(
data: Query<ListCommunities>, data: Query<ListCommunities>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<ListCommunitiesResponse>, LemmyError> { ) -> Result<Json<ListCommunitiesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let is_admin = local_user_view let is_admin = local_user_view
.as_ref() .as_ref()

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
community::{CommunityResponse, RemoveCommunity}, community::{CommunityResponse, RemoveCommunity},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -14,6 +14,7 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::time::naive_from_unix, utils::time::naive_from_unix,
@ -23,9 +24,8 @@ use lemmy_utils::{
pub async fn remove_community( pub async fn remove_community(
data: Json<RemoveCommunity>, data: Json<RemoveCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Verify its an admin (only an admin can remove a community) // Verify its an admin (only an admin can remove a community)
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
community::{CommunityResponse, EditCommunity}, community::{CommunityResponse, EditCommunity},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{local_site_to_slur_regex, local_user_view_from_jwt, sanitize_html_api_opt}, utils::{local_site_to_slur_regex, sanitize_html_api_opt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
@ -17,6 +17,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now}, utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityModeratorView; use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -27,8 +28,8 @@ use lemmy_utils::{
pub async fn update_community( pub async fn update_community(
data: Json<EditCommunity>, data: Json<EditCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CommunityResponse>, LemmyError> { ) -> Result<Json<CommunityResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let slur_regex = local_site_to_slur_regex(&local_site); let slur_regex = local_site_to_slur_regex(&local_site);

View file

@ -3,23 +3,22 @@ use actix_web::web::Json;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
custom_emoji::{CreateCustomEmoji, CustomEmojiResponse}, custom_emoji::{CreateCustomEmoji, CustomEmojiResponse},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api}, utils::{is_admin, sanitize_html_api},
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, custom_emoji::{CustomEmoji, CustomEmojiInsertForm},
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
local_site::LocalSite, local_site::LocalSite,
}; };
use lemmy_db_views::structs::CustomEmojiView; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn create_custom_emoji( pub async fn create_custom_emoji(
data: Json<CreateCustomEmoji>, data: Json<CreateCustomEmoji>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CustomEmojiResponse>, LemmyError> { ) -> Result<Json<CustomEmojiResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -3,18 +3,18 @@ use actix_web::web::Json;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
custom_emoji::{DeleteCustomEmoji, DeleteCustomEmojiResponse}, custom_emoji::{DeleteCustomEmoji, DeleteCustomEmojiResponse},
utils::{is_admin, local_user_view_from_jwt}, utils::is_admin,
}; };
use lemmy_db_schema::source::custom_emoji::CustomEmoji; use lemmy_db_schema::source::custom_emoji::CustomEmoji;
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn delete_custom_emoji( pub async fn delete_custom_emoji(
data: Json<DeleteCustomEmoji>, data: Json<DeleteCustomEmoji>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<DeleteCustomEmojiResponse>, LemmyError> { ) -> Result<Json<DeleteCustomEmojiResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
CustomEmoji::delete(&mut context.pool(), data.id).await?; CustomEmoji::delete(&mut context.pool(), data.id).await?;

View file

@ -3,23 +3,22 @@ use actix_web::web::Json;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
custom_emoji::{CustomEmojiResponse, EditCustomEmoji}, custom_emoji::{CustomEmojiResponse, EditCustomEmoji},
utils::{is_admin, local_user_view_from_jwt, sanitize_html_api}, utils::{is_admin, sanitize_html_api},
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, custom_emoji::{CustomEmoji, CustomEmojiUpdateForm},
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
local_site::LocalSite, local_site::LocalSite,
}; };
use lemmy_db_views::structs::CustomEmojiView; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn update_custom_emoji( pub async fn update_custom_emoji(
data: Json<EditCustomEmoji>, data: Json<EditCustomEmoji>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CustomEmojiResponse>, LemmyError> { ) -> Result<Json<CustomEmojiResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;

View file

@ -12,7 +12,6 @@ use lemmy_api_common::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
honeypot_check, honeypot_check,
local_site_to_slur_regex, local_site_to_slur_regex,
local_user_view_from_jwt,
mark_post_as_read, mark_post_as_read,
sanitize_html_api, sanitize_html_api,
sanitize_html_api_opt, sanitize_html_api_opt,
@ -29,6 +28,7 @@ use lemmy_db_schema::{
}, },
traits::{Crud, Likeable}, traits::{Crud, Likeable},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::CommunityView; use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
@ -46,8 +46,8 @@ use webmention::{Webmention, WebmentionError};
pub async fn create_post( pub async fn create_post(
data: Json<CreatePost>, data: Json<CreatePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let slur_regex = local_site_to_slur_regex(&local_site); let slur_regex = local_site_to_slur_regex(&local_site);
@ -175,7 +175,7 @@ pub async fn create_post(
mark_post_as_read(person_id, post_id, &mut context.pool()).await?; mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
if let Some(url) = updated_post.url.clone() { if let Some(url) = updated_post.url.clone() {
let task = async move { spawn_try_task(async move {
let mut webmention = let mut webmention =
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?; Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
webmention.set_checked(true); webmention.set_checked(true);
@ -188,8 +188,7 @@ pub async fn create_post(
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention), Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention),
} }
}; });
spawn_try_task(task);
}; };
build_post_response(&context, community_id, person_id, post_id).await build_post_response(&context, community_id, person_id, post_id).await

View file

@ -5,21 +5,21 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{DeletePost, PostResponse}, post::{DeletePost, PostResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt}, utils::{check_community_ban, check_community_deleted_or_removed},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::post::{Post, PostUpdateForm}, source::post::{Post, PostUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn delete_post( pub async fn delete_post(
data: Json<DeletePost>, data: Json<DeletePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_id = data.post_id; let post_id = data.post_id;
let orig_post = Post::read(&mut context.pool(), post_id).await?; let orig_post = Post::read(&mut context.pool(), post_id).await?;

View file

@ -2,19 +2,17 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{GetPost, GetPostResponse}, post::{GetPost, GetPostResponse},
utils::{ utils::{check_private_instance, is_mod_or_admin_opt, mark_post_as_read},
check_private_instance,
is_mod_or_admin_opt,
local_user_view_from_jwt_opt,
mark_post_as_read,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm}, aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
source::{comment::Comment, local_site::LocalSite, post::Post}, source::{comment::Comment, local_site::LocalSite, post::Post},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::{post_view::PostQuery, structs::PostView}; use lemmy_db_views::{
post_view::PostQuery,
structs::{LocalUserView, PostView},
};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView}; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -22,8 +20,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn get_post( pub async fn get_post(
data: Query<GetPost>, data: Query<GetPost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<GetPostResponse>, LemmyError> { ) -> Result<Json<GetPostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{PostResponse, RemovePost}, post::{PostResponse, RemovePost},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, utils::{check_community_ban, is_mod_or_admin},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -14,15 +14,15 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn remove_post( pub async fn remove_post(
data: Json<RemovePost>, data: Json<RemovePost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let post_id = data.post_id; let post_id = data.post_id;
let orig_post = Post::read(&mut context.pool(), post_id).await?; let orig_post = Post::read(&mut context.pool(), post_id).await?;

View file

@ -6,12 +6,7 @@ use lemmy_api_common::{
post::{EditPost, PostResponse}, post::{EditPost, PostResponse},
request::fetch_site_data, request::fetch_site_data,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_ban, local_site_to_slur_regex, sanitize_html_api_opt},
check_community_ban,
local_site_to_slur_regex,
local_user_view_from_jwt,
sanitize_html_api_opt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -22,6 +17,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::{diesel_option_overwrite, naive_now}, utils::{diesel_option_overwrite, naive_now},
}; };
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{ utils::{
@ -35,8 +31,8 @@ use std::ops::Deref;
pub async fn update_post( pub async fn update_post(
data: Json<EditPost>, data: Json<EditPost>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let data_url = data.url.as_ref(); let data_url = data.url.as_ref();

View file

@ -9,7 +9,6 @@ use lemmy_api_common::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
get_interface_language, get_interface_language,
local_site_to_slur_regex, local_site_to_slur_regex,
local_user_view_from_jwt,
sanitize_html_api, sanitize_html_api,
send_email_to_user, send_email_to_user,
EndpointType, EndpointType,
@ -32,8 +31,8 @@ use lemmy_utils::{
pub async fn create_private_message( pub async fn create_private_message(
data: Json<CreatePrivateMessage>, data: Json<CreatePrivateMessage>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageResponse>, LemmyError> { ) -> Result<Json<PrivateMessageResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
let content = sanitize_html_api(&data.content); let content = sanitize_html_api(&data.content);

View file

@ -4,22 +4,20 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{DeletePrivateMessage, PrivateMessageResponse}, private_message::{DeletePrivateMessage, PrivateMessageResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::private_message::{PrivateMessage, PrivateMessageUpdateForm}, source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::PrivateMessageView; use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn delete_private_message( pub async fn delete_private_message(
data: Json<DeletePrivateMessage>, data: Json<DeletePrivateMessage>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageResponse>, LemmyError> { ) -> Result<Json<PrivateMessageResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
// Checking permissions // Checking permissions
let private_message_id = data.private_message_id; let private_message_id = data.private_message_id;
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;

View file

@ -2,17 +2,16 @@ use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{GetPrivateMessages, PrivateMessagesResponse}, private_message::{GetPrivateMessages, PrivateMessagesResponse},
utils::local_user_view_from_jwt,
}; };
use lemmy_db_views::private_message_view::PrivateMessageQuery; use lemmy_db_views::{private_message_view::PrivateMessageQuery, structs::LocalUserView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn get_private_message( pub async fn get_private_message(
data: Query<GetPrivateMessages>, data: Query<GetPrivateMessages>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessagesResponse>, LemmyError> { ) -> Result<Json<PrivateMessagesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), &context).await?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let page = data.page; let page = data.page;

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
private_message::{EditPrivateMessage, PrivateMessageResponse}, private_message::{EditPrivateMessage, PrivateMessageResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{local_site_to_slur_regex, local_user_view_from_jwt, sanitize_html_api}, utils::{local_site_to_slur_regex, sanitize_html_api},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -14,7 +14,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::naive_now, utils::naive_now,
}; };
use lemmy_db_views::structs::PrivateMessageView; use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{slurs::remove_slurs, validation::is_valid_body_field}, utils::{slurs::remove_slurs, validation::is_valid_body_field},
@ -24,8 +24,8 @@ use lemmy_utils::{
pub async fn update_private_message( pub async fn update_private_message(
data: Json<EditPrivateMessage>, data: Json<EditPrivateMessage>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<PrivateMessageResponse>, LemmyError> { ) -> Result<Json<PrivateMessageResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Checking permissions // Checking permissions

View file

@ -8,7 +8,6 @@ use lemmy_api_common::{
generate_site_inbox_url, generate_site_inbox_url,
is_admin, is_admin,
local_site_rate_limit_to_rate_limit_config, local_site_rate_limit_to_rate_limit_config,
local_user_view_from_jwt,
sanitize_html_api, sanitize_html_api,
sanitize_html_api_opt, sanitize_html_api_opt,
}, },
@ -24,7 +23,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now}, utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
}; };
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorType, LemmyResult},
utils::{ utils::{
@ -44,8 +43,8 @@ use url::Url;
pub async fn create_site( pub async fn create_site(
data: Json<CreateSite>, data: Json<CreateSite>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<SiteResponse>, LemmyError> { ) -> Result<Json<SiteResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin; other types of users should not create site data... // Make sure user is an admin; other types of users should not create site data...
@ -589,7 +588,6 @@ mod tests {
blocked_instances: None, blocked_instances: None,
taglines: None, taglines: None,
registration_mode: site_registration_mode, registration_mode: site_registration_mode,
auth: Default::default(),
} }
} }
} }

View file

@ -1,17 +1,12 @@
use actix_web::web::{Data, Json, Query}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
sensitive::Sensitive, site::{GetSiteResponse, MyUserInfo},
site::{GetSite, GetSiteResponse, MyUserInfo},
utils::{check_user_valid, check_validator_time},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::source::{
newtypes::LocalUserId, actor_language::{LocalUserLanguage, SiteLanguage},
source::{ language::Language,
actor_language::{LocalUserLanguage, SiteLanguage}, tagline::Tagline,
language::Language,
tagline::Tagline,
},
}; };
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
use lemmy_db_views_actor::structs::{ use lemmy_db_views_actor::structs::{
@ -23,24 +18,21 @@ use lemmy_db_views_actor::structs::{
PersonView, PersonView,
}; };
use lemmy_utils::{ use lemmy_utils::{
claims::Claims,
error::{LemmyError, LemmyErrorExt, LemmyErrorType}, error::{LemmyError, LemmyErrorExt, LemmyErrorType},
version, version,
}; };
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn get_site( pub async fn get_site(
data: Query<GetSite>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<GetSiteResponse>, LemmyError> { ) -> Result<Json<GetSiteResponse>, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let admins = PersonView::admins(&mut context.pool()).await?; let admins = PersonView::admins(&mut context.pool()).await?;
// Build the local user // Build the local user
let my_user = if let Some(local_user_view) = let my_user = if let Some(local_user_view) = local_user_view {
local_user_settings_view_from_jwt_opt(data.auth.as_ref(), &context).await
{
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id; let local_user_id = local_user_view.local_user.id;
@ -100,32 +92,3 @@ pub async fn get_site(
custom_emojis, custom_emojis,
})) }))
} }
#[tracing::instrument(skip_all)]
async fn local_user_settings_view_from_jwt_opt(
jwt: Option<&Sensitive<String>>,
context: &LemmyContext,
) -> Option<LocalUserView> {
match jwt {
Some(jwt) => {
let claims = Claims::decode(jwt.as_ref(), &context.secret().jwt_secret)
.ok()?
.claims;
let local_user_id = LocalUserId(claims.sub);
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id)
.await
.ok()?;
check_user_valid(
local_user_view.person.banned,
local_user_view.person.ban_expires,
local_user_view.person.deleted,
)
.ok()?;
check_validator_time(&local_user_view.local_user.validator_time, &claims).ok()?;
Some(local_user_view)
}
None => None,
}
}

View file

@ -3,12 +3,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{EditSite, SiteResponse}, site::{EditSite, SiteResponse},
utils::{ utils::{is_admin, local_site_rate_limit_to_rate_limit_config, sanitize_html_api_opt},
is_admin,
local_site_rate_limit_to_rate_limit_config,
local_user_view_from_jwt,
sanitize_html_api_opt,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -25,7 +20,7 @@ use lemmy_db_schema::{
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now}, utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
RegistrationMode, RegistrationMode,
}; };
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult},
utils::{ utils::{
@ -44,8 +39,8 @@ use lemmy_utils::{
pub async fn update_site( pub async fn update_site(
data: Json<EditSite>, data: Json<EditSite>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<SiteResponse>, LemmyError> { ) -> Result<Json<SiteResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_site = site_view.local_site; let local_site = site_view.local_site;
let site = site_view.site; let site = site_view.site;
@ -588,7 +583,6 @@ mod tests {
taglines: None, taglines: None,
registration_mode: site_registration_mode, registration_mode: site_registration_mode,
reports_email_admins: None, reports_email_admins: None,
auth: Default::default(),
} }
} }
} }

View file

@ -5,18 +5,18 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{DeleteAccount, DeleteAccountResponse}, person::{DeleteAccount, DeleteAccountResponse},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{local_user_view_from_jwt, purge_user_account}, utils::purge_user_account,
}; };
use lemmy_db_schema::source::person::Person; use lemmy_db_schema::source::person::Person;
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyError, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorType};
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn delete_account( pub async fn delete_account(
data: Json<DeleteAccount>, data: Json<DeleteAccount>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<DeleteAccountResponse>, LemmyError> { ) -> Result<Json<DeleteAccountResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), &context).await?;
// Verify the password // Verify the password
let valid: bool = verify( let valid: bool = verify(
&data.password, &data.password,

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
activities::{ activities::{
generate_activity_id, generate_activity_id,
generate_announce_activity_id,
send_lemmy_activity, send_lemmy_activity,
verify_is_public, verify_is_public,
verify_person_in_community, verify_person_in_community,
@ -75,16 +76,20 @@ impl AnnounceActivity {
community: &ApubCommunity, community: &ApubCommunity,
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
) -> Result<AnnounceActivity, LemmyError> { ) -> Result<AnnounceActivity, LemmyError> {
let inner_kind = object
.other
.get("type")
.and_then(serde_json::Value::as_str)
.unwrap_or("other");
let id =
generate_announce_activity_id(inner_kind, &context.settings().get_protocol_and_hostname())?;
Ok(AnnounceActivity { Ok(AnnounceActivity {
actor: community.id().into(), actor: community.id().into(),
to: vec![public()], to: vec![public()],
object: IdOrNestedObject::NestedObject(object), object: IdOrNestedObject::NestedObject(object),
cc: vec![community.followers_url.clone().into()], cc: vec![community.followers_url.clone().into()],
kind: AnnounceType::Announce, kind: AnnounceType::Announce,
id: generate_activity_id( id,
&AnnounceType::Announce,
&context.settings().get_protocol_and_hostname(),
)?,
}) })
} }

View file

@ -28,12 +28,15 @@ use crate::{
use activitypub_federation::{ use activitypub_federation::{
config::Data, config::Data,
fetch::object_id::ObjectId, fetch::object_id::ObjectId,
kinds::public, kinds::{activity::AnnounceType, public},
protocol::context::WithContext, protocol::context::WithContext,
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
}; };
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::{context::LemmyContext, send_activity::SendActivityData}; use lemmy_api_common::{
context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData},
};
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::CommunityId, newtypes::CommunityId,
source::{ source::{
@ -42,10 +45,7 @@ use lemmy_db_schema::{
}, },
}; };
use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView}; use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
use lemmy_utils::{ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult};
error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult},
spawn_try_task,
};
use serde::Serialize; use serde::Serialize;
use std::{ops::Deref, time::Duration}; use std::{ops::Deref, time::Duration};
use tracing::info; use tracing::info;
@ -181,6 +181,21 @@ where
Url::parse(&id) Url::parse(&id)
} }
/// like generate_activity_id but also add the inner kind for easier debugging
fn generate_announce_activity_id(
inner_kind: &str,
protocol_and_hostname: &str,
) -> Result<Url, ParseError> {
let id = format!(
"{}/activities/{}/{}/{}",
protocol_and_hostname,
AnnounceType::Announce.to_string().to_lowercase(),
inner_kind.to_lowercase(),
Uuid::new_v4()
);
Url::parse(&id)
}
pub(crate) trait GetActorType { pub(crate) trait GetActorType {
fn actor_type(&self) -> ActorType; fn actor_type(&self) -> ActorType;
} }
@ -198,12 +213,12 @@ where
ActorT: Actor + GetActorType, ActorT: Actor + GetActorType,
Activity: ActivityHandler<Error = LemmyError>, Activity: ActivityHandler<Error = LemmyError>,
{ {
info!("Sending activity {}", activity.id().to_string()); info!("Saving outgoing activity to queue {}", activity.id());
let activity = WithContext::new(activity, CONTEXT.deref().clone()); let activity = WithContext::new(activity, CONTEXT.deref().clone());
let form = SentActivityForm { let form = SentActivityForm {
ap_id: activity.id().clone().into(), ap_id: activity.id().clone().into(),
data: serde_json::to_value(activity.clone())?, data: serde_json::to_value(activity)?,
sensitive, sensitive,
send_inboxes: send_targets send_inboxes: send_targets
.inboxes .inboxes
@ -220,6 +235,13 @@ where
Ok(()) Ok(())
} }
pub async fn handle_outgoing_activities(context: Data<LemmyContext>) -> LemmyResult<()> {
while let Some(data) = ActivityChannel::retrieve_activity().await {
match_outgoing_activities(data, &context.reset_request_count()).await?
}
Ok(())
}
pub async fn match_outgoing_activities( pub async fn match_outgoing_activities(
data: SendActivityData, data: SendActivityData,
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
@ -324,6 +346,6 @@ pub async fn match_outgoing_activities(
} }
} }
}; };
spawn_try_task(fed_task); fed_task.await?;
Ok(()) Ok(())
} }

View file

@ -8,7 +8,7 @@ use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse}, comment::{GetComments, GetCommentsResponse},
context::LemmyContext, context::LemmyContext,
utils::{check_private_instance, local_user_view_from_jwt_opt_new}, utils::check_private_instance,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{comment::Comment, community::Community, local_site::LocalSite}, source::{comment::Comment, community::Community, local_site::LocalSite},
@ -21,9 +21,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn list_comments( pub async fn list_comments(
data: Query<GetComments>, data: Query<GetComments>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
mut local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> Result<Json<GetCommentsResponse>, LemmyError> { ) -> Result<Json<GetCommentsResponse>, LemmyError> {
local_user_view_from_jwt_opt_new(&mut local_user_view, data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -8,7 +8,7 @@ use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{GetPosts, GetPostsResponse}, post::{GetPosts, GetPostsResponse},
utils::{check_private_instance, local_user_view_from_jwt_opt_new}, utils::check_private_instance,
}; };
use lemmy_db_schema::source::{community::Community, local_site::LocalSite}; use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
use lemmy_db_views::{ use lemmy_db_views::{
@ -21,9 +21,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
pub async fn list_posts( pub async fn list_posts(
data: Query<GetPosts>, data: Query<GetPosts>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
mut local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> Result<Json<GetPostsResponse>, LemmyError> { ) -> Result<Json<GetPostsResponse>, LemmyError> {
local_user_view_from_jwt_opt_new(&mut local_user_view, data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -4,7 +4,7 @@ use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
community::{GetCommunity, GetCommunityResponse}, community::{GetCommunity, GetCommunityResponse},
context::LemmyContext, context::LemmyContext,
utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt_new}, utils::{check_private_instance, is_mod_or_admin_opt},
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
actor_language::CommunityLanguage, actor_language::CommunityLanguage,
@ -20,9 +20,8 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorExt2, LemmyErrorTy
pub async fn get_community( pub async fn get_community(
data: Query<GetCommunity>, data: Query<GetCommunity>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
mut local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> Result<Json<GetCommunityResponse>, LemmyError> { ) -> Result<Json<GetCommunityResponse>, LemmyError> {
local_user_view_from_jwt_opt_new(&mut local_user_view, data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
if data.name.is_none() && data.id.is_none() { if data.name.is_none() && data.id.is_none() {

View file

@ -4,7 +4,7 @@ use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{GetPersonDetails, GetPersonDetailsResponse}, person::{GetPersonDetails, GetPersonDetailsResponse},
utils::{check_private_instance, local_user_view_from_jwt_opt_new}, utils::check_private_instance,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{local_site::LocalSite, person::Person}, source::{local_site::LocalSite, person::Person},
@ -18,14 +18,13 @@ use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};
pub async fn read_person( pub async fn read_person(
data: Query<GetPersonDetails>, data: Query<GetPersonDetails>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
mut local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> Result<Json<GetPersonDetailsResponse>, LemmyError> { ) -> Result<Json<GetPersonDetailsResponse>, LemmyError> {
// Check to make sure a person name or an id is given // Check to make sure a person name or an id is given
if data.username.is_none() && data.person_id.is_none() { if data.username.is_none() && data.person_id.is_none() {
Err(LemmyErrorType::NoIdGiven)? Err(LemmyErrorType::NoIdGiven)?
} }
local_user_view_from_jwt_opt_new(&mut local_user_view, data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

Some files were not shown because too many files have changed in this diff Show more