Merge branch 'main' into env-args

This commit is contained in:
SleeplessOne1917 2024-05-22 08:21:35 -04:00 committed by GitHub
commit 8927868f4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 317 additions and 346 deletions

26
Cargo.lock generated
View file

@ -2664,7 +2664,7 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "lemmy_api" name = "lemmy_api"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-web", "actix-web",
@ -2693,7 +2693,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_api_common" name = "lemmy_api_common"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-web", "actix-web",
@ -2731,7 +2731,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_api_crud" name = "lemmy_api_crud"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"accept-language", "accept-language",
"activitypub_federation", "activitypub_federation",
@ -2754,7 +2754,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_apub" name = "lemmy_apub"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-web", "actix-web",
@ -2792,7 +2792,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_db_perf" name = "lemmy_db_perf"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -2807,7 +2807,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_db_schema" name = "lemmy_db_schema"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"anyhow", "anyhow",
@ -2847,7 +2847,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_db_views" name = "lemmy_db_views"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"chrono", "chrono",
@ -2869,7 +2869,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_db_views_actor" name = "lemmy_db_views_actor"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"chrono", "chrono",
"diesel", "diesel",
@ -2889,7 +2889,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_db_views_moderator" name = "lemmy_db_views_moderator"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"diesel", "diesel",
"diesel-async", "diesel-async",
@ -2901,7 +2901,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_federate" name = "lemmy_federate"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"anyhow", "anyhow",
@ -2924,7 +2924,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_routes" name = "lemmy_routes"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-web", "actix-web",
@ -2949,7 +2949,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_server" name = "lemmy_server"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-cors", "actix-cors",
@ -2992,7 +2992,7 @@ dependencies = [
[[package]] [[package]]
name = "lemmy_utils" name = "lemmy_utils"
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"anyhow", "anyhow",

View file

@ -1,5 +1,5 @@
[workspace.package] [workspace.package]
version = "0.19.4-beta.7" version = "0.19.4-beta.8"
edition = "2021" edition = "2021"
description = "A link aggregator for the fediverse" description = "A link aggregator for the fediverse"
license = "AGPL-3.0" license = "AGPL-3.0"
@ -88,17 +88,17 @@ unused_self = "deny"
unwrap_used = "deny" unwrap_used = "deny"
[workspace.dependencies] [workspace.dependencies]
lemmy_api = { version = "=0.19.4-beta.7", path = "./crates/api" } lemmy_api = { version = "=0.19.4-beta.8", path = "./crates/api" }
lemmy_api_crud = { version = "=0.19.4-beta.7", path = "./crates/api_crud" } lemmy_api_crud = { version = "=0.19.4-beta.8", path = "./crates/api_crud" }
lemmy_apub = { version = "=0.19.4-beta.7", path = "./crates/apub" } lemmy_apub = { version = "=0.19.4-beta.8", path = "./crates/apub" }
lemmy_utils = { version = "=0.19.4-beta.7", path = "./crates/utils", default-features = false } lemmy_utils = { version = "=0.19.4-beta.8", path = "./crates/utils", default-features = false }
lemmy_db_schema = { version = "=0.19.4-beta.7", path = "./crates/db_schema" } lemmy_db_schema = { version = "=0.19.4-beta.8", path = "./crates/db_schema" }
lemmy_api_common = { version = "=0.19.4-beta.7", path = "./crates/api_common" } lemmy_api_common = { version = "=0.19.4-beta.8", path = "./crates/api_common" }
lemmy_routes = { version = "=0.19.4-beta.7", path = "./crates/routes" } lemmy_routes = { version = "=0.19.4-beta.8", path = "./crates/routes" }
lemmy_db_views = { version = "=0.19.4-beta.7", path = "./crates/db_views" } lemmy_db_views = { version = "=0.19.4-beta.8", path = "./crates/db_views" }
lemmy_db_views_actor = { version = "=0.19.4-beta.7", path = "./crates/db_views_actor" } lemmy_db_views_actor = { version = "=0.19.4-beta.8", path = "./crates/db_views_actor" }
lemmy_db_views_moderator = { version = "=0.19.4-beta.7", path = "./crates/db_views_moderator" } lemmy_db_views_moderator = { version = "=0.19.4-beta.8", path = "./crates/db_views_moderator" }
lemmy_federate = { version = "=0.19.4-beta.7", path = "./crates/federate" } lemmy_federate = { version = "=0.19.4-beta.8", path = "./crates/federate" }
activitypub_federation = { version = "0.5.6", default-features = false, features = [ activitypub_federation = { version = "0.5.6", default-features = false, features = [
"actix-web", "actix-web",
] } ] }

View file

@ -6,11 +6,11 @@
"repository": "https://github.com/LemmyNet/lemmy", "repository": "https://github.com/LemmyNet/lemmy",
"author": "Dessalines", "author": "Dessalines",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"packageManager": "pnpm@9.0.6", "packageManager": "pnpm@9.1.1+sha256.9551e803dcb7a1839fdf5416153a844060c7bce013218ce823410532504ac10b",
"scripts": { "scripts": {
"lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src && prettier --check 'src/**/*.ts'", "lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src && prettier --check 'src/**/*.ts'",
"fix": "prettier --write src && eslint --fix src", "fix": "prettier --write src && eslint --fix src",
"api-test": "jest -i follow.spec.ts && jest -i post.spec.ts && jest -i comment.spec.ts && jest -i private_message.spec.ts && jest -i user.spec.ts && jest -i community.spec.ts && jest -i image.spec.ts", "api-test": "jest -i follow.spec.ts && jest -i image.spec.ts && jest -i user.spec.ts && jest -i private_message.spec.ts && jest -i community.spec.ts && jest -i post.spec.ts && jest -i comment.spec.ts ",
"api-test-follow": "jest -i follow.spec.ts", "api-test-follow": "jest -i follow.spec.ts",
"api-test-comment": "jest -i comment.spec.ts", "api-test-comment": "jest -i comment.spec.ts",
"api-test-post": "jest -i post.spec.ts", "api-test-post": "jest -i post.spec.ts",

View file

@ -3,19 +3,19 @@
# it is expected that this script is called by run-federation-test.sh script. # it is expected that this script is called by run-federation-test.sh script.
set -e set -e
if [ -n "$LEMMY_LOG_LEVEL" ]; if [ -z "$LEMMY_LOG_LEVEL" ];
then then
LEMMY_LOG_LEVEL=warn LEMMY_LOG_LEVEL=info
fi fi
export RUST_BACKTRACE=1 export RUST_BACKTRACE=1
#export RUST_LOG="warn,lemmy_server=$LEMMY_LOG_LEVEL,lemmy_federate=$LEMMY_LOG_LEVEL,lemmy_api=$LEMMY_LOG_LEVEL,lemmy_api_common=$LEMMY_LOG_LEVEL,lemmy_api_crud=$LEMMY_LOG_LEVEL,lemmy_apub=$LEMMY_LOG_LEVEL,lemmy_db_schema=$LEMMY_LOG_LEVEL,lemmy_db_views=$LEMMY_LOG_LEVEL,lemmy_db_views_actor=$LEMMY_LOG_LEVEL,lemmy_db_views_moderator=$LEMMY_LOG_LEVEL,lemmy_routes=$LEMMY_LOG_LEVEL,lemmy_utils=$LEMMY_LOG_LEVEL,lemmy_websocket=$LEMMY_LOG_LEVEL" export RUST_LOG="warn,lemmy_server=$LEMMY_LOG_LEVEL,lemmy_federate=$LEMMY_LOG_LEVEL,lemmy_api=$LEMMY_LOG_LEVEL,lemmy_api_common=$LEMMY_LOG_LEVEL,lemmy_api_crud=$LEMMY_LOG_LEVEL,lemmy_apub=$LEMMY_LOG_LEVEL,lemmy_db_schema=$LEMMY_LOG_LEVEL,lemmy_db_views=$LEMMY_LOG_LEVEL,lemmy_db_views_actor=$LEMMY_LOG_LEVEL,lemmy_db_views_moderator=$LEMMY_LOG_LEVEL,lemmy_routes=$LEMMY_LOG_LEVEL,lemmy_utils=$LEMMY_LOG_LEVEL,lemmy_websocket=$LEMMY_LOG_LEVEL"
export LEMMY_TEST_FAST_FEDERATION=1 # by default, the persistent federation queue has delays in the scale of 30s-5min export LEMMY_TEST_FAST_FEDERATION=1 # by default, the persistent federation queue has delays in the scale of 30s-5min
# pictrs setup # pictrs setup
if [ ! -f "api_tests/pict-rs" ]; then if [ ! -f "api_tests/pict-rs" ]; then
curl "https://git.asonix.dog/asonix/pict-rs/releases/download/v0.5.0-beta.2/pict-rs-linux-amd64" -o api_tests/pict-rs curl "https://git.asonix.dog/asonix/pict-rs/releases/download/v0.5.13/pict-rs-linux-amd64" -o api_tests/pict-rs
chmod +x api_tests/pict-rs chmod +x api_tests/pict-rs
fi fi
./api_tests/pict-rs \ ./api_tests/pict-rs \

View file

@ -45,7 +45,6 @@ let postOnAlphaRes: PostResponse;
beforeAll(async () => { beforeAll(async () => {
await setupLogins(); await setupLogins();
await unfollows();
await Promise.all([followBeta(alpha), followBeta(gamma)]); await Promise.all([followBeta(alpha), followBeta(gamma)]);
betaCommunity = (await resolveBetaCommunity(alpha)).community; betaCommunity = (await resolveBetaCommunity(alpha)).community;
if (betaCommunity) { if (betaCommunity) {

View file

@ -380,8 +380,8 @@ test("User blocks instance, communities are hidden", async () => {
test("Community follower count is federated", async () => { test("Community follower count is federated", async () => {
// Follow the beta community from alpha // Follow the beta community from alpha
let community = await createCommunity(beta); let community = await createCommunity(beta);
let community_id = community.community_view.community.actor_id; let communityActorId = community.community_view.community.actor_id;
let resolved = await resolveCommunity(alpha, community_id); let resolved = await resolveCommunity(alpha, communityActorId);
if (!resolved.community) { if (!resolved.community) {
throw "Missing beta community"; throw "Missing beta community";
} }
@ -389,7 +389,7 @@ test("Community follower count is federated", async () => {
await followCommunity(alpha, true, resolved.community.community.id); await followCommunity(alpha, true, resolved.community.community.id);
let followed = ( let followed = (
await waitUntil( await waitUntil(
() => resolveCommunity(alpha, community_id), () => resolveCommunity(alpha, communityActorId),
c => c.community?.subscribed === "Subscribed", c => c.community?.subscribed === "Subscribed",
) )
).community; ).community;
@ -398,7 +398,7 @@ test("Community follower count is federated", async () => {
expect(followed?.counts.subscribers).toBe(1); expect(followed?.counts.subscribers).toBe(1);
// Follow the community from gamma // Follow the community from gamma
resolved = await resolveCommunity(gamma, community_id); resolved = await resolveCommunity(gamma, communityActorId);
if (!resolved.community) { if (!resolved.community) {
throw "Missing beta community"; throw "Missing beta community";
} }
@ -406,7 +406,7 @@ test("Community follower count is federated", async () => {
await followCommunity(gamma, true, resolved.community.community.id); await followCommunity(gamma, true, resolved.community.community.id);
followed = ( followed = (
await waitUntil( await waitUntil(
() => resolveCommunity(gamma, community_id), () => resolveCommunity(gamma, communityActorId),
c => c.community?.subscribed === "Subscribed", c => c.community?.subscribed === "Subscribed",
) )
).community; ).community;
@ -415,7 +415,7 @@ test("Community follower count is federated", async () => {
expect(followed?.counts?.subscribers).toBe(2); expect(followed?.counts?.subscribers).toBe(2);
// Follow the community from delta // Follow the community from delta
resolved = await resolveCommunity(delta, community_id); resolved = await resolveCommunity(delta, communityActorId);
if (!resolved.community) { if (!resolved.community) {
throw "Missing beta community"; throw "Missing beta community";
} }
@ -423,7 +423,7 @@ test("Community follower count is federated", async () => {
await followCommunity(delta, true, resolved.community.community.id); await followCommunity(delta, true, resolved.community.community.id);
followed = ( followed = (
await waitUntil( await waitUntil(
() => resolveCommunity(delta, community_id), () => resolveCommunity(delta, communityActorId),
c => c.community?.subscribed === "Subscribed", c => c.community?.subscribed === "Subscribed",
) )
).community; ).community;

View file

@ -29,14 +29,16 @@ import {
unfollows, unfollows,
getPost, getPost,
waitUntil, waitUntil,
randomString,
createPostWithThumbnail, createPostWithThumbnail,
sampleImage,
} from "./shared"; } from "./shared";
const downloadFileSync = require("download-file-sync"); const downloadFileSync = require("download-file-sync");
beforeAll(setupLogins); beforeAll(setupLogins);
afterAll(unfollows); afterAll(async () => {
await Promise.all([unfollows(), deleteAllImages(alpha)]);
});
test("Upload image and delete it", async () => { test("Upload image and delete it", async () => {
// Before running this test, you need to delete all previous images in the DB // Before running this test, you need to delete all previous images in the DB
@ -159,7 +161,6 @@ test("Purge post, linked image removed", async () => {
expect(post.post_view.post.url).toBe(upload.url); expect(post.post_view.post.url).toBe(upload.url);
// purge post // purge post
const purgeForm: PurgePost = { const purgeForm: PurgePost = {
post_id: post.post_view.post.id, post_id: post.post_view.post.id,
}; };
@ -183,13 +184,13 @@ test("Images in remote post are proxied if setting enabled", async () => {
gamma, gamma,
community.community_view.community.id, community.community_view.community.id,
upload.url, upload.url,
"![](http://example.com/image2.png)", `![](${sampleImage})`,
); );
expect(post.post_view.post).toBeDefined(); expect(post.post_view.post).toBeDefined();
// remote image gets proxied after upload // remote image gets proxied after upload
expect( expect(
post.post_view.post.url?.startsWith( post.post_view.post.thumbnail_url?.startsWith(
"http://lemmy-gamma:8561/api/v3/image_proxy?url", "http://lemmy-gamma:8561/api/v3/image_proxy?url",
), ),
).toBeTruthy(); ).toBeTruthy();
@ -202,14 +203,20 @@ test("Images in remote post are proxied if setting enabled", async () => {
let epsilonPost = await resolvePost(epsilon, post.post_view.post); let epsilonPost = await resolvePost(epsilon, post.post_view.post);
expect(epsilonPost.post).toBeDefined(); expect(epsilonPost.post).toBeDefined();
// remote image gets proxied after federation // Fetch the post again, the metadata should be backgrounded now
// Wait for the metadata to get fetched, since this is backgrounded now
let epsilonPost2 = await waitUntil(
() => getPost(epsilon, epsilonPost.post!.post.id),
p => p.post_view.post.thumbnail_url != undefined,
);
expect( expect(
epsilonPost.post!.post.url?.startsWith( epsilonPost2.post_view.post.thumbnail_url?.startsWith(
"http://lemmy-epsilon:8581/api/v3/image_proxy?url", "http://lemmy-epsilon:8581/api/v3/image_proxy?url",
), ),
).toBeTruthy(); ).toBeTruthy();
expect( expect(
epsilonPost.post!.post.body?.startsWith( epsilonPost2.post_view.post.body?.startsWith(
"![](http://lemmy-epsilon:8581/api/v3/image_proxy?url", "![](http://lemmy-epsilon:8581/api/v3/image_proxy?url",
), ),
).toBeTruthy(); ).toBeTruthy();
@ -232,7 +239,7 @@ test("No image proxying if setting is disabled", async () => {
alpha, alpha,
community.community_view.community.id, community.community_view.community.id,
upload.url, upload.url,
"![](http://example.com/image2.png)", `![](${sampleImage})`,
); );
expect(post.post_view.post).toBeDefined(); expect(post.post_view.post).toBeDefined();
@ -240,7 +247,7 @@ test("No image proxying if setting is disabled", async () => {
expect( expect(
post.post_view.post.url?.startsWith("http://127.0.0.1:8551/pictrs/image/"), post.post_view.post.url?.startsWith("http://127.0.0.1:8551/pictrs/image/"),
).toBeTruthy(); ).toBeTruthy();
expect(post.post_view.post.body).toBe("![](http://example.com/image2.png)"); expect(post.post_view.post.body).toBe(`![](${sampleImage})`);
let betaPost = await waitForPost( let betaPost = await waitForPost(
beta, beta,
@ -253,8 +260,7 @@ test("No image proxying if setting is disabled", async () => {
expect( expect(
betaPost.post.url?.startsWith("http://127.0.0.1:8551/pictrs/image/"), betaPost.post.url?.startsWith("http://127.0.0.1:8551/pictrs/image/"),
).toBeTruthy(); ).toBeTruthy();
expect(betaPost.post.body).toBe("![](http://example.com/image2.png)"); expect(betaPost.post.body).toBe(`![](${sampleImage})`);
// Make sure the alt text got federated // Make sure the alt text got federated
expect(post.post_view.post.alt_text).toBe(betaPost.post.alt_text); expect(post.post_view.post.alt_text).toBe(betaPost.post.alt_text);
}); });

View file

@ -48,7 +48,6 @@ beforeAll(async () => {
await setupLogins(); await setupLogins();
betaCommunity = (await resolveBetaCommunity(alpha)).community; betaCommunity = (await resolveBetaCommunity(alpha)).community;
expect(betaCommunity).toBeDefined(); expect(betaCommunity).toBeDefined();
await unfollows();
}); });
afterAll(unfollows); afterAll(unfollows);
@ -83,10 +82,7 @@ async function assertPostFederation(postOne: PostView, postTwo: PostView) {
test("Create a post", async () => { test("Create a post", async () => {
// Setup some allowlists and blocklists // Setup some allowlists and blocklists
let editSiteForm: EditSite = { const editSiteForm: EditSite = {};
allowed_instances: ["lemmy-beta"],
};
await delta.editSite(editSiteForm);
editSiteForm.allowed_instances = []; editSiteForm.allowed_instances = [];
editSiteForm.blocked_instances = ["lemmy-alpha"]; editSiteForm.blocked_instances = ["lemmy-alpha"];
@ -749,7 +745,7 @@ test("Block post that contains banned URL", async () => {
await epsilon.editSite(editSiteForm); await epsilon.editSite(editSiteForm);
await delay(500); await delay();
if (!betaCommunity) { if (!betaCommunity) {
throw "Missing beta community"; throw "Missing beta community";

View file

@ -81,6 +81,8 @@ import { ListingType } from "lemmy-js-client/dist/types/ListingType";
export const fetchFunction = fetch; export const fetchFunction = fetch;
export const imageFetchLimit = 50; export const imageFetchLimit = 50;
export const sampleImage =
"https://i.pinimg.com/originals/df/5f/5b/df5f5b1b174a2b4b6026cc6c8f9395c1.jpg";
export let alphaUrl = "http://127.0.0.1:8541"; export let alphaUrl = "http://127.0.0.1:8541";
export let betaUrl = "http://127.0.0.1:8551"; export let betaUrl = "http://127.0.0.1:8551";
@ -180,6 +182,10 @@ export async function setupLogins() {
]; ];
await gamma.editSite(editSiteForm); await gamma.editSite(editSiteForm);
// Setup delta allowed instance
editSiteForm.allowed_instances = ["lemmy-beta"];
await delta.editSite(editSiteForm);
// Create the main alpha/beta communities // Create the main alpha/beta communities
// Ignore thrown errors of duplicates // Ignore thrown errors of duplicates
try { try {
@ -693,8 +699,8 @@ export async function saveUserSettingsBio(
export async function saveUserSettingsFederated( export async function saveUserSettingsFederated(
api: LemmyHttp, api: LemmyHttp,
): Promise<SuccessResponse> { ): Promise<SuccessResponse> {
let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png"; let avatar = sampleImage;
let banner = "https://image.flaticon.com/icons/png/512/36/35896.png"; let banner = sampleImage;
let bio = "a changed bio"; let bio = "a changed bio";
let form: SaveUserSettings = { let form: SaveUserSettings = {
show_nsfw: false, show_nsfw: false,
@ -760,6 +766,7 @@ export async function unfollowRemotes(
await Promise.all( await Promise.all(
remoteFollowed.map(cu => 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;
} }
@ -889,14 +896,17 @@ export async function deleteAllImages(api: LemmyHttp) {
limit: imageFetchLimit, limit: imageFetchLimit,
}); });
imagesRes.images; imagesRes.images;
Promise.all(
for (const image of imagesRes.images) { imagesRes.images
const form: DeleteImage = { .map(image => {
token: image.local_image.pictrs_delete_token, const form: DeleteImage = {
filename: image.local_image.pictrs_alias, token: image.local_image.pictrs_delete_token,
}; filename: image.local_image.pictrs_alias,
await api.deleteImage(form); };
} return form;
})
.map(form => api.deleteImage(form)),
);
} }
export async function unfollows() { export async function unfollows() {
@ -907,6 +917,24 @@ export async function unfollows() {
unfollowRemotes(delta), unfollowRemotes(delta),
unfollowRemotes(epsilon), unfollowRemotes(epsilon),
]); ]);
await Promise.all([
purgeAllPosts(alpha),
purgeAllPosts(beta),
purgeAllPosts(gamma),
purgeAllPosts(delta),
purgeAllPosts(epsilon),
]);
}
export async function purgeAllPosts(api: LemmyHttp) {
// The best way to get all federated items, is to find the posts
let res = await api.getPosts({ type_: "All", limit: 50 });
await Promise.all(
Array.from(new Set(res.posts.map(p => p.post.id)))
.map(post_id => api.purgePost({ post_id }))
// Ignore errors
.map(p => p.catch(e => e)),
);
} }
export function getCommentParentId(comment: Comment): number | undefined { export function getCommentParentId(comment: Comment): number | undefined {

View file

@ -19,7 +19,7 @@ pub async fn change_password_after_reset(
) -> LemmyResult<Json<SuccessResponse>> { ) -> LemmyResult<Json<SuccessResponse>> {
// Fetch the user_id from the token // Fetch the user_id from the token
let token = data.token.clone(); let token = data.token.clone();
let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token) let local_user_id = PasswordResetRequest::read_and_delete(&mut context.pool(), &token)
.await? .await?
.ok_or(LemmyErrorType::TokenNotFound)? .ok_or(LemmyErrorType::TokenNotFound)?
.local_user_id; .local_user_id;

View file

@ -6,7 +6,6 @@ use lemmy_api_common::{
utils::send_password_reset_email, utils::send_password_reset_email,
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::source::password_reset_request::PasswordResetRequest;
use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::error::{LemmyErrorType, LemmyResult}; use lemmy_utils::error::{LemmyErrorType, LemmyResult};
@ -21,15 +20,6 @@ pub async fn reset_password(
.await? .await?
.ok_or(LemmyErrorType::IncorrectLogin)?; .ok_or(LemmyErrorType::IncorrectLogin)?;
// Check for too many attempts (to limit potential abuse)
let recent_resets_count = PasswordResetRequest::get_recent_password_resets_count(
&mut context.pool(),
local_user_view.local_user.id,
)
.await?;
if recent_resets_count >= 3 {
Err(LemmyErrorType::PasswordResetLimitReached)?
}
let site_view = SiteView::read_local(&mut context.pool()) let site_view = SiteView::read_local(&mut context.pool())
.await? .await?
.ok_or(LemmyErrorType::LocalSiteNotSetup)?; .ok_or(LemmyErrorType::LocalSiteNotSetup)?;

View file

@ -3,9 +3,10 @@ use crate::{
lemmy_db_schema::traits::Crud, lemmy_db_schema::traits::Crud,
post::{LinkMetadata, OpenGraphData}, post::{LinkMetadata, OpenGraphData},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{local_site_opt_to_sensitive, proxy_image_link, proxy_image_link_opt_apub}, utils::{local_site_opt_to_sensitive, proxy_image_link},
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use chrono::{DateTime, Utc};
use encoding_rs::{Encoding, UTF_8}; use encoding_rs::{Encoding, UTF_8};
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::DbUrl, newtypes::DbUrl,
@ -18,14 +19,13 @@ use lemmy_db_schema::{
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorType, LemmyResult},
settings::structs::{PictrsImageMode, Settings}, settings::structs::{PictrsImageMode, Settings},
spawn_try_task,
REQWEST_TIMEOUT, REQWEST_TIMEOUT,
VERSION, VERSION,
}; };
use mime::Mime; use mime::Mime;
use reqwest::{header::CONTENT_TYPE, Client, ClientBuilder}; use reqwest::{header::CONTENT_TYPE, Client, ClientBuilder};
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use tracing::info; use tracing::info;
use url::Url; use url::Url;
use urlencoding::encode; use urlencoding::encode;
@ -65,95 +65,70 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
}) })
} }
/// Generate post thumbnail in background task, because some sites can be very slow to respond. /// Generates and saves a post thumbnail and metadata.
/// ///
/// Takes a callback to generate a send activity task, so that post can be federated with metadata. /// Takes a callback to generate a send activity task, so that post can be federated with metadata.
/// ///
/// TODO: `federated_thumbnail` param can be removed once we federate full metadata and can /// TODO: `federated_thumbnail` param can be removed once we federate full metadata and can
/// write it to db directly, without calling this function. /// write it to db directly, without calling this function.
/// https://github.com/LemmyNet/lemmy/issues/4598 /// https://github.com/LemmyNet/lemmy/issues/4598
pub fn generate_post_link_metadata( pub async fn generate_post_link_metadata(
post: Post, post: Post,
custom_thumbnail: Option<Url>, custom_thumbnail: Option<Url>,
federated_thumbnail: Option<Url>,
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static, send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
local_site: Option<LocalSite>, local_site: Option<LocalSite>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
) { ) -> LemmyResult<()> {
spawn_try_task(async move { let metadata = match &post.url {
let metadata = match &post.url { Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(),
Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(), _ => Default::default(),
_ => Default::default(), };
};
let is_image_post = metadata let is_image_post = metadata
.content_type .content_type
.as_ref() .as_ref()
.is_some_and(|content_type| content_type.starts_with("image")); .is_some_and(|content_type| content_type.starts_with("image"));
// Decide if we are allowed to generate local thumbnail // Decide if we are allowed to generate local thumbnail
let allow_sensitive = local_site_opt_to_sensitive(&local_site); let allow_sensitive = local_site_opt_to_sensitive(&local_site);
let allow_generate_thumbnail = allow_sensitive || !post.nsfw; let allow_generate_thumbnail = allow_sensitive || !post.nsfw;
// Use custom thumbnail if available and its not an image post let image_url = if is_image_post {
let thumbnail_url = if !is_image_post && custom_thumbnail.is_some() { post.url
custom_thumbnail } else {
} metadata.opengraph_data.image.clone()
// Use federated thumbnail if available };
else if federated_thumbnail.is_some() {
federated_thumbnail
}
// Generate local thumbnail if allowed
else if allow_generate_thumbnail {
match post
.url
.filter(|_| is_image_post)
.or(metadata.opengraph_data.image)
{
Some(url) => generate_pictrs_thumbnail(&url, &context).await.ok(),
None => None,
}
}
// Otherwise use opengraph preview image directly
else {
metadata.opengraph_data.image.map(Into::into)
};
// Proxy the image fetch if necessary let thumbnail_url = if let (false, Some(url)) = (is_image_post, custom_thumbnail) {
let proxied_thumbnail_url = proxy_image_link_opt_apub(thumbnail_url, &context).await?; proxy_image_link(url, &context).await.ok()
} else if let (true, Some(url)) = (allow_generate_thumbnail, image_url) {
generate_pictrs_thumbnail(&url, &context)
.await
.ok()
.map(Into::into)
} else {
metadata.opengraph_data.image.clone()
};
let form = PostUpdateForm { let form = PostUpdateForm {
embed_title: Some(metadata.opengraph_data.title), embed_title: Some(metadata.opengraph_data.title),
embed_description: Some(metadata.opengraph_data.description), embed_description: Some(metadata.opengraph_data.description),
embed_video_url: Some(metadata.opengraph_data.embed_video_url), embed_video_url: Some(metadata.opengraph_data.embed_video_url),
thumbnail_url: Some(proxied_thumbnail_url), thumbnail_url: Some(thumbnail_url),
url_content_type: Some(metadata.content_type), url_content_type: Some(metadata.content_type),
..Default::default() ..Default::default()
}; };
let updated_post = Post::update(&mut context.pool(), post.id, &form).await?; let updated_post = Post::update(&mut context.pool(), post.id, &form).await?;
if let Some(send_activity) = send_activity(updated_post) { if let Some(send_activity) = send_activity(updated_post) {
ActivityChannel::submit_activity(send_activity, &context).await?; ActivityChannel::submit_activity(send_activity, &context).await?;
} }
Ok(()) Ok(())
});
} }
/// Extract site metadata from HTML Opengraph attributes. /// Extract site metadata from HTML Opengraph attributes.
fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> LemmyResult<OpenGraphData> { fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> LemmyResult<OpenGraphData> {
let html = String::from_utf8_lossy(html_bytes); let html = String::from_utf8_lossy(html_bytes);
// Make sure the first line is doctype html
let first_line = html
.trim_start()
.lines()
.next()
.ok_or(LemmyErrorType::NoLinesInHtml)?
.to_lowercase();
if !first_line.starts_with("<!doctype html") {
Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?
}
let mut page = HTML::from_string(html.to_string(), None)?; let mut page = HTML::from_string(html.to_string(), None)?;
// If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the // If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the
@ -201,19 +176,40 @@ fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> LemmyResult<OpenGraph
}) })
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct PictrsResponse { pub struct PictrsResponse {
files: Vec<PictrsFile>, pub files: Option<Vec<PictrsFile>>,
msg: String, pub msg: String,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct PictrsFile { pub struct PictrsFile {
file: String, pub file: String,
delete_token: String, pub delete_token: String,
pub details: PictrsFileDetails,
} }
#[derive(Deserialize, Debug)] impl PictrsFile {
pub fn thumbnail_url(&self, protocol_and_hostname: &str) -> Result<Url, url::ParseError> {
Url::parse(&format!(
"{protocol_and_hostname}/pictrs/image/{}",
self.file
))
}
}
/// Stores extra details about a Pictrs image.
#[derive(Deserialize, Serialize, Debug)]
pub struct PictrsFileDetails {
/// In pixels
pub width: u16,
/// In pixels
pub height: u16,
pub content_type: String,
pub created_at: DateTime<Utc>,
}
#[derive(Deserialize, Serialize, Debug)]
struct PictrsPurgeResponse { struct PictrsPurgeResponse {
msg: String, msg: String,
} }
@ -295,33 +291,34 @@ async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> L
encode(image_url.as_str()) encode(image_url.as_str())
); );
let response = context let res = context
.client() .client()
.get(&fetch_url) .get(&fetch_url)
.timeout(REQWEST_TIMEOUT) .timeout(REQWEST_TIMEOUT)
.send() .send()
.await?
.json::<PictrsResponse>()
.await?; .await?;
let response: PictrsResponse = response.json().await?; let files = res.files.unwrap_or_default();
if response.msg == "ok" { let image = files
let thumbnail_url = Url::parse(&format!( .first()
"{}/pictrs/image/{}", .ok_or(LemmyErrorType::PictrsResponseError(res.msg))?;
context.settings().get_protocol_and_hostname(),
response.files.first().expect("missing pictrs file").file let form = LocalImageForm {
))?; // This is none because its an internal request.
for uploaded_image in response.files { // IE, a local user shouldn't get to delete the thumbnails for their link posts
let form = LocalImageForm { local_user_id: None,
local_user_id: None, pictrs_alias: image.file.clone(),
pictrs_alias: uploaded_image.file.to_string(), pictrs_delete_token: image.delete_token.clone(),
pictrs_delete_token: uploaded_image.delete_token.to_string(), };
}; let protocol_and_hostname = context.settings().get_protocol_and_hostname();
LocalImage::create(&mut context.pool(), &form).await?; let thumbnail_url = image.thumbnail_url(&protocol_and_hostname)?;
}
Ok(thumbnail_url) LocalImage::create(&mut context.pool(), &form).await?;
} else {
Err(LemmyErrorType::PictrsResponseError(response.msg))? Ok(thumbnail_url)
}
} }
// TODO: get rid of this by reading content type from db // TODO: get rid of this by reading content type from db

View file

@ -440,7 +440,7 @@ pub async fn send_password_reset_email(
// Insert the row after successful send, to avoid using daily reset limit while // Insert the row after successful send, to avoid using daily reset limit while
// email sending is broken. // email sending is broken.
let local_user_id = user.local_user.id; let local_user_id = user.local_user.id;
PasswordResetRequest::create_token(pool, local_user_id, token.clone()).await?; PasswordResetRequest::create(pool, local_user_id, token.clone()).await?;
Ok(()) Ok(())
} }
@ -965,13 +965,10 @@ async fn proxy_image_link_internal(
if link.domain() == Some(&context.settings().hostname) { if link.domain() == Some(&context.settings().hostname) {
Ok(link.into()) Ok(link.into())
} else if image_mode == PictrsImageMode::ProxyAllImages { } else if image_mode == PictrsImageMode::ProxyAllImages {
let proxied = format!( let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?;
"{}/api/v3/image_proxy?url={}",
context.settings().get_protocol_and_hostname(),
encode(link.as_str())
);
RemoteImage::create(&mut context.pool(), vec![link]).await?; RemoteImage::create(&mut context.pool(), vec![link]).await?;
Ok(Url::parse(&proxied)?.into()) Ok(proxied.into())
} else { } else {
Ok(link.into()) Ok(link.into())
} }
@ -1025,6 +1022,17 @@ pub async fn proxy_image_link_opt_apub(
} }
} }
fn build_proxied_image_url(
link: &Url,
protocol_and_hostname: &str,
) -> Result<Url, url::ParseError> {
Url::parse(&format!(
"{}/api/v3/image_proxy?url={}",
protocol_and_hostname,
encode(link.as_str())
))
}
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)] #[allow(clippy::indexing_slicing)]

View file

@ -14,7 +14,6 @@ use lemmy_api_common::{
local_site_to_slur_regex, local_site_to_slur_regex,
mark_post_as_read, mark_post_as_read,
process_markdown_opt, process_markdown_opt,
proxy_image_link_opt_apub,
EndpointType, EndpointType,
}, },
}; };
@ -75,7 +74,6 @@ pub async fn create_post(
is_url_blocked(&url, &url_blocklist)?; is_url_blocked(&url, &url_blocklist)?;
check_url_scheme(&url)?; check_url_scheme(&url)?;
check_url_scheme(&custom_thumbnail)?; check_url_scheme(&custom_thumbnail)?;
let url = proxy_image_link_opt_apub(url, &context).await?;
check_community_user_action( check_community_user_action(
&local_user_view.person, &local_user_view.person,
@ -125,7 +123,7 @@ pub async fn create_post(
let post_form = PostInsertForm::builder() let post_form = PostInsertForm::builder()
.name(data.name.trim().to_string()) .name(data.name.trim().to_string())
.url(url) .url(url.map(Into::into))
.body(body) .body(body)
.alt_text(data.alt_text.clone()) .alt_text(data.alt_text.clone())
.community_id(data.community_id) .community_id(data.community_id)
@ -159,11 +157,11 @@ pub async fn create_post(
generate_post_link_metadata( generate_post_link_metadata(
updated_post.clone(), updated_post.clone(),
custom_thumbnail, custom_thumbnail,
None,
|post| Some(SendActivityData::CreatePost(post)), |post| Some(SendActivityData::CreatePost(post)),
Some(local_site), Some(local_site),
context.reset_request_count(), context.reset_request_count(),
); )
.await?;
// They like their own post by default // They like their own post by default
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;

View file

@ -11,7 +11,6 @@ use lemmy_api_common::{
get_url_blocklist, get_url_blocklist,
local_site_to_slur_regex, local_site_to_slur_regex,
process_markdown_opt, process_markdown_opt,
proxy_image_link_opt_apub,
}, },
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -86,11 +85,6 @@ pub async fn update_post(
Err(LemmyErrorType::NoPostEditAllowed)? Err(LemmyErrorType::NoPostEditAllowed)?
} }
let url = match url {
Some(url) => Some(proxy_image_link_opt_apub(Some(url), &context).await?),
_ => Default::default(),
};
let language_id = data.language_id; let language_id = data.language_id;
CommunityLanguage::is_allowed_community_language( CommunityLanguage::is_allowed_community_language(
&mut context.pool(), &mut context.pool(),
@ -101,7 +95,7 @@ pub async fn update_post(
let post_form = PostUpdateForm { let post_form = PostUpdateForm {
name: data.name.clone(), name: data.name.clone(),
url, url: Some(url.map(Into::into)),
body: diesel_option_overwrite(body), body: diesel_option_overwrite(body),
alt_text: diesel_option_overwrite(data.alt_text.clone()), alt_text: diesel_option_overwrite(data.alt_text.clone()),
nsfw: data.nsfw, nsfw: data.nsfw,
@ -118,11 +112,11 @@ pub async fn update_post(
generate_post_link_metadata( generate_post_link_metadata(
updated_post.clone(), updated_post.clone(),
custom_thumbnail, custom_thumbnail,
None,
|post| Some(SendActivityData::UpdatePost(post)), |post| Some(SendActivityData::UpdatePost(post)),
Some(local_site), Some(local_site),
context.reset_request_count(), context.reset_request_count(),
); )
.await?;
build_post_response( build_post_response(
context.deref(), context.deref(),

View file

@ -45,6 +45,7 @@ use lemmy_db_schema::{
use lemmy_db_views_actor::structs::CommunityModeratorView; use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorType, LemmyResult},
spawn_try_task,
utils::{markdown::markdown_to_html, slurs::check_slurs_opt, validation::check_url_scheme}, utils::{markdown::markdown_to_html, slurs::check_slurs_opt, validation::check_url_scheme},
}; };
use std::ops::Deref; use std::ops::Deref;
@ -255,15 +256,13 @@ impl Object for ApubPost {
let timestamp = page.updated.or(page.published).unwrap_or_else(naive_now); let timestamp = page.updated.or(page.published).unwrap_or_else(naive_now);
let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?; let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?;
let post_ = post.clone();
let context_ = context.reset_request_count();
generate_post_link_metadata( // Generates a post thumbnail in background task, because some sites can be very slow to respond.
post.clone(), spawn_try_task(async move {
None, generate_post_link_metadata(post_, None, |_| None, local_site, context_).await
page.image.map(|i| i.url), });
|_| None,
local_site,
context.reset_request_count(),
);
Ok(post.into()) Ok(post.into())
} }

View file

@ -1,49 +1,22 @@
use crate::{ use crate::{
diesel::OptionalExtension, diesel::OptionalExtension,
newtypes::LocalUserId, newtypes::LocalUserId,
schema::password_reset_request::dsl::{local_user_id, password_reset_request, published, token}, schema::password_reset_request::dsl::{password_reset_request, published, token},
source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm}, source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm},
traits::Crud,
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
use diesel::{ use diesel::{
delete,
dsl::{insert_into, now, IntervalDsl}, dsl::{insert_into, now, IntervalDsl},
result::Error, result::Error,
sql_types::Timestamptz, sql_types::Timestamptz,
ExpressionMethods, ExpressionMethods,
IntoSql, IntoSql,
QueryDsl,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
#[async_trait]
impl Crud for PasswordResetRequest {
type InsertForm = PasswordResetRequestForm;
type UpdateForm = PasswordResetRequestForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &PasswordResetRequestForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(password_reset_request)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
password_reset_request_id: i32,
form: &PasswordResetRequestForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(password_reset_request.find(password_reset_request_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
impl PasswordResetRequest { impl PasswordResetRequest {
pub async fn create_token( pub async fn create(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
from_local_user_id: LocalUserId, from_local_user_id: LocalUserId,
token_: String, token_: String,
@ -52,30 +25,21 @@ impl PasswordResetRequest {
local_user_id: from_local_user_id, local_user_id: from_local_user_id,
token: token_.into(), token: token_.into(),
}; };
Self::create(pool, &form).await
}
pub async fn read_from_token(pool: &mut DbPool<'_>, token_: &str) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
password_reset_request insert_into(password_reset_request)
.values(form)
.get_result::<Self>(conn)
.await
}
pub async fn read_and_delete(pool: &mut DbPool<'_>, token_: &str) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
delete(password_reset_request)
.filter(token.eq(token_)) .filter(token.eq(token_))
.filter(published.gt(now.into_sql::<Timestamptz>() - 1.days())) .filter(published.gt(now.into_sql::<Timestamptz>() - 1.days()))
.first(conn)
.await
.optional()
}
pub async fn get_recent_password_resets_count(
pool: &mut DbPool<'_>,
user_id: LocalUserId,
) -> Result<i64, Error> {
let conn = &mut get_conn(pool).await?;
password_reset_request
.filter(local_user_id.eq(user_id))
.filter(published.gt(now.into_sql::<Timestamptz>() - 1.days()))
.count()
.get_result(conn) .get_result(conn)
.await .await
.optional()
} }
} }
@ -94,62 +58,64 @@ mod tests {
traits::Crud, traits::Crud,
utils::build_db_pool_for_tests, utils::build_db_pool_for_tests,
}; };
use lemmy_utils::error::LemmyResult;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serial_test::serial; use serial_test::serial;
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_crud() { async fn test_password_reset() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await; let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into(); let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) // Setup
.await let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
.unwrap();
let new_person = PersonInsertForm::builder() let new_person = PersonInsertForm::builder()
.name("thommy prw".into()) .name("thommy prw".into())
.public_key("pubkey".to_string()) .public_key("pubkey".to_string())
.instance_id(inserted_instance.id) .instance_id(inserted_instance.id)
.build(); .build();
let inserted_person = Person::create(pool, &new_person).await?;
let inserted_person = Person::create(pool, &new_person).await.unwrap();
let new_local_user = LocalUserInsertForm::builder() let new_local_user = LocalUserInsertForm::builder()
.person_id(inserted_person.id) .person_id(inserted_person.id)
.password_encrypted("pass".to_string()) .password_encrypted("pass".to_string())
.build(); .build();
let inserted_local_user = LocalUser::create(pool, &new_local_user, vec![]).await?;
let inserted_local_user = LocalUser::create(pool, &new_local_user, vec![]) // Create password reset token
.await
.unwrap();
let token = "nope"; let token = "nope";
let inserted_password_reset_request = let inserted_password_reset_request =
PasswordResetRequest::create_token(pool, inserted_local_user.id, token.to_string()) PasswordResetRequest::create(pool, inserted_local_user.id, token.to_string()).await?;
.await
.unwrap();
let expected_password_reset_request = PasswordResetRequest { // Read it and verify
id: inserted_password_reset_request.id, let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token)
local_user_id: inserted_local_user.id, .await?
token: token.to_string().into(),
published: inserted_password_reset_request.published,
};
let read_password_reset_request = PasswordResetRequest::read_from_token(pool, token)
.await
.unwrap()
.unwrap(); .unwrap();
let num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
Instance::delete(pool, inserted_instance.id).await.unwrap();
assert_eq!(expected_password_reset_request, read_password_reset_request);
assert_eq!( assert_eq!(
expected_password_reset_request, inserted_password_reset_request.id,
inserted_password_reset_request read_password_reset_request.id
); );
assert_eq!(
inserted_password_reset_request.local_user_id,
read_password_reset_request.local_user_id
);
assert_eq!(
inserted_password_reset_request.token,
read_password_reset_request.token
);
assert_eq!(
inserted_password_reset_request.published,
read_password_reset_request.published
);
// Cannot reuse same token again
let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await?;
assert!(read_password_reset_request.is_none());
// Cleanup
let num_deleted = Person::delete(pool, inserted_person.id).await?;
Instance::delete(pool, inserted_instance.id).await?;
assert_eq!(1, num_deleted); assert_eq!(1, num_deleted);
Ok(())
} }
} }

View file

@ -22,7 +22,7 @@ use typed_builder::TypedBuilder;
diesel(belongs_to(crate::source::local_user::LocalUser)) diesel(belongs_to(crate::source::local_user::LocalUser))
)] )]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", diesel(primary_key(local_user_id)))] #[cfg_attr(feature = "full", diesel(primary_key(pictrs_alias)))]
pub struct LocalImage { pub struct LocalImage {
pub local_user_id: Option<LocalUserId>, pub local_user_id: Option<LocalUserId>,
pub pictrs_alias: String, pub pictrs_alias: String,
@ -41,9 +41,11 @@ pub struct LocalImageForm {
/// Stores all images which are hosted on remote domains. When attempting to proxy an image, it /// Stores all images which are hosted on remote domains. When attempting to proxy an image, it
/// is checked against this table to avoid Lemmy being used as a general purpose proxy. /// is checked against this table to avoid Lemmy being used as a general purpose proxy.
#[derive(PartialEq, Eq, Debug, Clone)] #[skip_serializing_none]
#[cfg_attr(feature = "full", derive(Queryable, Identifiable))] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = remote_image))] #[cfg_attr(feature = "full", diesel(table_name = remote_image))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
pub struct RemoteImage { pub struct RemoteImage {
pub id: i32, pub id: i32,
pub link: DbUrl, pub link: DbUrl,

View file

@ -216,6 +216,7 @@ pub struct VoteView {
pub score: i16, pub score: i16,
} }
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable))] #[cfg_attr(feature = "full", derive(TS, Queryable))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]

View file

@ -13,6 +13,7 @@ use tokio::{
time::sleep, time::sleep,
}; };
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::info;
mod util; mod util;
mod worker; mod worker;
@ -44,6 +45,10 @@ async fn start_stop_federation_workers(
let pool2 = &mut DbPool::Pool(&pool); let pool2 = &mut DbPool::Pool(&pool);
let process_index = opts.process_index - 1; let process_index = opts.process_index - 1;
let local_domain = federation_config.settings().get_hostname_without_port()?; let local_domain = federation_config.settings().get_hostname_without_port()?;
info!(
"Starting federation workers for process count {} and index {}",
opts.process_count, process_index
);
loop { loop {
let mut total_count = 0; let mut total_count = 0;
let mut dead_count = 0; let mut dead_count = 0;

View file

@ -34,6 +34,7 @@ use std::{
}; };
use tokio::{sync::mpsc::UnboundedSender, time::sleep}; use tokio::{sync::mpsc::UnboundedSender, time::sleep};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{debug, info, trace, warn};
/// Check whether to save state to db every n sends if there's no failures (during failures state is saved after every attempt) /// Check whether to save state to db every n sends if there's no failures (during failures state is saved after every attempt)
/// This determines the batch size for loop_batch. After a batch ends and SAVE_STATE_EVERY_TIME has passed, the federation_queue_state is updated in the DB. /// This determines the batch size for loop_batch. After a batch ends and SAVE_STATE_EVERY_TIME has passed, the federation_queue_state is updated in the DB.
@ -105,6 +106,7 @@ impl InstanceWorker {
&mut self, &mut self,
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
debug!("Starting federation worker for {}", self.instance.domain);
let save_state_every = chrono::Duration::from_std(SAVE_STATE_EVERY_TIME).expect("not negative"); let save_state_every = chrono::Duration::from_std(SAVE_STATE_EVERY_TIME).expect("not negative");
self.update_communities(pool).await?; self.update_communities(pool).await?;
@ -176,15 +178,14 @@ impl InstanceWorker {
.await .await
.context("failed reading activity from db")? .context("failed reading activity from db")?
else { else {
tracing::debug!("{}: {:?} does not exist", self.instance.domain, id); debug!("{}: {:?} does not exist", self.instance.domain, id);
self.state.last_successful_id = Some(id); self.state.last_successful_id = Some(id);
continue; continue;
}; };
if let Err(e) = self.send_retry_loop(pool, &ele.0, &ele.1).await { if let Err(e) = self.send_retry_loop(pool, &ele.0, &ele.1).await {
tracing::warn!( warn!(
"sending {} errored internally, skipping activity: {:?}", "sending {} errored internally, skipping activity: {:?}",
ele.0.ap_id, ele.0.ap_id, e
e
); );
} }
if self.stop.is_cancelled() { if self.stop.is_cancelled() {
@ -211,7 +212,7 @@ impl InstanceWorker {
.await .await
.context("failed figuring out inbox urls")?; .context("failed figuring out inbox urls")?;
if inbox_urls.is_empty() { if inbox_urls.is_empty() {
tracing::debug!("{}: {:?} no inboxes", self.instance.domain, activity.id); trace!("{}: {:?} no inboxes", self.instance.domain, activity.id);
self.state.last_successful_id = Some(activity.id); self.state.last_successful_id = Some(activity.id);
self.state.last_successful_published_time = Some(activity.published); self.state.last_successful_published_time = Some(activity.published);
return Ok(()); return Ok(());
@ -229,16 +230,14 @@ impl InstanceWorker {
SendActivityTask::prepare(&object, actor.as_ref(), inbox_urls, &self.context).await?; SendActivityTask::prepare(&object, actor.as_ref(), inbox_urls, &self.context).await?;
for task in requests { for task in requests {
// usually only one due to shared inbox // usually only one due to shared inbox
tracing::debug!("sending out {}", task); trace!("sending out {}", task);
while let Err(e) = task.sign_and_send(&self.context).await { while let Err(e) = task.sign_and_send(&self.context).await {
self.state.fail_count += 1; self.state.fail_count += 1;
self.state.last_retry = Some(Utc::now()); self.state.last_retry = Some(Utc::now());
let retry_delay: Duration = federate_retry_sleep_duration(self.state.fail_count); let retry_delay: Duration = federate_retry_sleep_duration(self.state.fail_count);
tracing::info!( info!(
"{}: retrying {:?} attempt {} with delay {retry_delay:.2?}. ({e})", "{}: retrying {:?} attempt {} with delay {retry_delay:.2?}. ({e})",
self.instance.domain, self.instance.domain, activity.id, self.state.fail_count
activity.id,
self.state.fail_count
); );
self.save_and_send_state(pool).await?; self.save_and_send_state(pool).await?;
tokio::select! { tokio::select! {

View file

@ -1,18 +1,16 @@
use actix_web::{ use actix_web::{
body::BodyStream, body::BodyStream,
error,
http::{ http::{
header::{HeaderName, ACCEPT_ENCODING, HOST}, header::{HeaderName, ACCEPT_ENCODING, HOST},
StatusCode, StatusCode,
}, },
web, web,
web::Query, web::Query,
Error,
HttpRequest, HttpRequest,
HttpResponse, HttpResponse,
}; };
use futures::stream::{Stream, StreamExt}; use futures::stream::{Stream, StreamExt};
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::{context::LemmyContext, request::PictrsResponse};
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
images::{LocalImage, LocalImageForm, RemoteImage}, images::{LocalImage, LocalImageForm, RemoteImage},
local_site::LocalSite, local_site::LocalSite,
@ -21,7 +19,7 @@ use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyResult, rate_limit::RateLimitCell, REQWEST_TIMEOUT}; use lemmy_utils::{error::LemmyResult, rate_limit::RateLimitCell, REQWEST_TIMEOUT};
use reqwest::Body; use reqwest::Body;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder}; use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
use serde::{Deserialize, Serialize}; use serde::Deserialize;
use std::time::Duration; use std::time::Duration;
use url::Url; use url::Url;
use urlencoding::decode; use urlencoding::decode;
@ -43,20 +41,8 @@ pub fn config(
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete))); .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
} }
#[derive(Debug, Serialize, Deserialize)]
struct Image {
file: String,
delete_token: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Images {
msg: String,
files: Option<Vec<Image>>,
}
#[derive(Deserialize)] #[derive(Deserialize)]
struct PictrsParams { struct PictrsGetParams {
format: Option<String>, format: Option<String>,
thumbnail: Option<i32>, thumbnail: Option<i32>,
} }
@ -92,7 +78,7 @@ async fn upload(
local_user_view: LocalUserView, local_user_view: LocalUserView,
client: web::Data<ClientWithMiddleware>, client: web::Data<ClientWithMiddleware>,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> { ) -> LemmyResult<HttpResponse> {
// TODO: check rate limit here // TODO: check rate limit here
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let image_url = format!("{}image", pictrs_config.url); let image_url = format!("{}image", pictrs_config.url);
@ -106,21 +92,18 @@ async fn upload(
.timeout(Duration::from_secs(pictrs_config.upload_timeout)) .timeout(Duration::from_secs(pictrs_config.upload_timeout))
.body(Body::wrap_stream(make_send(body))) .body(Body::wrap_stream(make_send(body)))
.send() .send()
.await .await?;
.map_err(error::ErrorBadRequest)?;
let status = res.status(); let status = res.status();
let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?; let images = res.json::<PictrsResponse>().await?;
if let Some(images) = &images.files { if let Some(images) = &images.files {
for uploaded_image in images { for image in images {
let form = LocalImageForm { let form = LocalImageForm {
local_user_id: Some(local_user_view.local_user.id), local_user_id: Some(local_user_view.local_user.id),
pictrs_alias: uploaded_image.file.to_string(), pictrs_alias: image.file.to_string(),
pictrs_delete_token: uploaded_image.delete_token.to_string(), pictrs_delete_token: image.delete_token.to_string(),
}; };
LocalImage::create(&mut context.pool(), &form) LocalImage::create(&mut context.pool(), &form).await?;
.await
.map_err(error::ErrorBadRequest)?;
} }
} }
@ -129,16 +112,14 @@ async fn upload(
async fn full_res( async fn full_res(
filename: web::Path<String>, filename: web::Path<String>,
web::Query(params): web::Query<PictrsParams>, web::Query(params): web::Query<PictrsGetParams>,
req: HttpRequest, req: HttpRequest,
client: web::Data<ClientWithMiddleware>, client: web::Data<ClientWithMiddleware>,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,
local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> Result<HttpResponse, Error> { ) -> LemmyResult<HttpResponse> {
// block access to images if instance is private and unauthorized, public // block access to images if instance is private and unauthorized, public
let local_site = LocalSite::read(&mut context.pool()) let local_site = LocalSite::read(&mut context.pool()).await?;
.await
.map_err(error::ErrorBadRequest)?;
if local_site.private_instance && local_user_view.is_none() { if local_site.private_instance && local_user_view.is_none() {
return Ok(HttpResponse::Unauthorized().finish()); return Ok(HttpResponse::Unauthorized().finish());
} }
@ -169,7 +150,7 @@ async fn image(
url: String, url: String,
req: HttpRequest, req: HttpRequest,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> Result<HttpResponse, Error> { ) -> LemmyResult<HttpResponse> {
let mut client_req = adapt_request(&req, client, url); let mut client_req = adapt_request(&req, client, url);
if let Some(addr) = req.head().peer_addr { if let Some(addr) = req.head().peer_addr {
@ -180,7 +161,7 @@ async fn image(
client_req = client_req.header("X-Forwarded-For", addr.to_string()); client_req = client_req.header("X-Forwarded-For", addr.to_string());
} }
let res = client_req.send().await.map_err(error::ErrorBadRequest)?; let res = client_req.send().await?;
if res.status() == StatusCode::NOT_FOUND { if res.status() == StatusCode::NOT_FOUND {
return Ok(HttpResponse::NotFound().finish()); return Ok(HttpResponse::NotFound().finish());
@ -202,7 +183,7 @@ async fn delete(
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,
// require login // require login
_local_user_view: LocalUserView, _local_user_view: LocalUserView,
) -> Result<HttpResponse, Error> { ) -> LemmyResult<HttpResponse> {
let (token, file) = components.into_inner(); let (token, file) = components.into_inner();
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
@ -214,11 +195,9 @@ async fn delete(
client_req = client_req.header("X-Forwarded-For", addr.to_string()); client_req = client_req.header("X-Forwarded-For", addr.to_string());
} }
let res = client_req.send().await.map_err(error::ErrorBadRequest)?; let res = client_req.send().await?;
LocalImage::delete_by_alias(&mut context.pool(), &file) LocalImage::delete_by_alias(&mut context.pool(), &file).await?;
.await
.map_err(error::ErrorBadRequest)?;
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream()))) Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
} }
@ -230,6 +209,8 @@ pub struct ImageProxyParams {
pub async fn image_proxy( pub async fn image_proxy(
Query(params): Query<ImageProxyParams>, Query(params): Query<ImageProxyParams>,
req: HttpRequest,
client: web::Data<ClientWithMiddleware>,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,
) -> LemmyResult<HttpResponse> { ) -> LemmyResult<HttpResponse> {
let url = Url::parse(&decode(&params.url)?)?; let url = Url::parse(&decode(&params.url)?)?;
@ -240,9 +221,8 @@ pub async fn image_proxy(
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let url = format!("{}image/original?proxy={}", pictrs_config.url, &params.url); let url = format!("{}image/original?proxy={}", pictrs_config.url, &params.url);
let image_response = context.client().get(url).send().await?;
Ok(HttpResponse::Ok().streaming(image_response.bytes_stream())) image(url, req, &client).await
} }
fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static

View file

@ -161,7 +161,6 @@ pub enum LemmyErrorType {
PermissiveRegex, PermissiveRegex,
InvalidRegex, InvalidRegex,
CaptchaIncorrect, CaptchaIncorrect,
PasswordResetLimitReached,
CouldntCreateAudioCaptcha, CouldntCreateAudioCaptcha,
InvalidUrlScheme, InvalidUrlScheme,
CouldntSendWebmention, CouldntSendWebmention,

@ -1 +1 @@
Subproject commit f0ab81deea347c433277a90ae752b10f68473719 Subproject commit 4e7c7ad4fcb4d8618d93ec17d72379367aa085b1

View file

@ -75,7 +75,7 @@ services:
init: true init: true
pictrs: pictrs:
image: asonix/pictrs:0.5.0-rc.2 image: asonix/pictrs:0.5.13
# this needs to match the pictrs url in lemmy.hjson # this needs to match the pictrs url in lemmy.hjson
hostname: pictrs hostname: pictrs
# we can set options to pictrs like this, here we set max. image size and forced format for conversion # we can set options to pictrs like this, here we set max. image size and forced format for conversion

View file

@ -49,7 +49,7 @@ services:
pictrs: pictrs:
restart: always restart: always
image: asonix/pictrs:0.5.0-rc.2 image: asonix/pictrs:0.5.13
user: 991:991 user: 991:991
volumes: volumes:
- ./volumes/pictrs_alpha:/mnt:Z - ./volumes/pictrs_alpha:/mnt:Z

View file

@ -278,6 +278,11 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.wrap(rate_limit.register()) .wrap(rate_limit.register())
.route(web::post().to(login)), .route(web::post().to(login)),
) )
.service(
web::resource("/user/password_reset")
.wrap(rate_limit.register())
.route(web::post().to(reset_password)),
)
.service( .service(
// Handle captcha separately // Handle captcha separately
web::resource("/user/get_captcha") web::resource("/user/get_captcha")
@ -318,7 +323,6 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
// TODO Account actions. I don't like that they're in /user maybe /accounts // TODO Account actions. I don't like that they're in /user maybe /accounts
.route("/logout", web::post().to(logout)) .route("/logout", web::post().to(logout))
.route("/delete_account", web::post().to(delete_account)) .route("/delete_account", web::post().to(delete_account))
.route("/password_reset", web::post().to(reset_password))
.route( .route(
"/password_change", "/password_change",
web::post().to(change_password_after_reset), web::post().to(change_password_after_reset),