Merge remote-tracking branch 'upstream/main' into migration-runner

This commit is contained in:
Dull Bananas 2024-12-19 14:35:16 -07:00
commit f044ef3321
132 changed files with 3021 additions and 1690 deletions

View file

@ -2,6 +2,10 @@
# See https://github.com/woodpecker-ci/woodpecker/issues/1677
variables:
# When updating the rust version here, be sure to update versions in `docker/Dockerfile`
# as well. Otherwise release builds can fail if Lemmy or dependencies rely on new Rust
# features. In particular the ARM builder image needs to be updated manually in the repo below:
# https://github.com/raskyld/lemmy-cross-toolchains
- &rust_image "rust:1.81"
- &rust_nightly_image "rustlang/rust:nightly"
- &install_pnpm "corepack enable pnpm"
@ -268,13 +272,15 @@ steps:
# using https://github.com/pksunkara/cargo-workspaces
publish_to_crates_io:
image: *rust_image
environment:
CARGO_API_TOKEN:
from_secret: cargo_api_token
commands:
- *install_binstall
# Install cargo-workspaces
- cargo binstall -y cargo-workspaces
- cp -r migrations crates/db_schema/
- cargo workspaces publish --token "$CARGO_API_TOKEN" --from-git --allow-dirty --no-verify --allow-branch "${CI_COMMIT_TAG}" --yes custom "${CI_COMMIT_TAG}"
secrets: [cargo_api_token]
when:
- event: tag

32
Cargo.lock generated
View file

@ -779,6 +779,17 @@ dependencies = [
"nom",
]
[[package]]
name = "cfb"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
dependencies = [
"byteorder",
"fnv",
"uuid",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -1239,9 +1250,9 @@ dependencies = [
[[package]]
name = "diesel"
version = "2.2.4"
version = "2.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "158fe8e2e68695bd615d7e4f3227c0727b151330d3e253b525086c348d055d5e"
checksum = "cbf9649c05e0a9dbd6d0b0b8301db5182b972d0fd02f0a7c6736cf632d7c0fd5"
dependencies = [
"bitflags 2.6.0",
"byteorder",
@ -1255,9 +1266,9 @@ dependencies = [
[[package]]
name = "diesel-async"
version = "0.5.1"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c5c6ec8d5c7b8444d19a47161797cbe361e0fb1ee40c6a8124ec915b64a4125"
checksum = "51a307ac00f7c23f526a04a77761a0519b9f0eb2838ebf5b905a58580095bdcb"
dependencies = [
"async-trait",
"deadpool",
@ -2360,6 +2371,15 @@ dependencies = [
"serde",
]
[[package]]
name = "infer"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc150e5ce2330295b8616ce0e3f53250e53af31759a9dbedad1621ba29151847"
dependencies = [
"cfb",
]
[[package]]
name = "inout"
version = "0.1.3"
@ -2520,6 +2540,7 @@ dependencies = [
"encoding_rs",
"enum-map",
"futures",
"infer",
"jsonwebtoken",
"lemmy_db_schema",
"lemmy_db_views",
@ -2562,6 +2583,7 @@ dependencies = [
"lemmy_db_views",
"lemmy_db_views_actor",
"lemmy_utils",
"regex",
"serde",
"serde_json",
"serde_with",
@ -2677,8 +2699,10 @@ dependencies = [
"lemmy_utils",
"pretty_assertions",
"serde",
"serde_json",
"serde_with",
"serial_test",
"test-context",
"tokio",
"tracing",
"ts-rs",

View file

@ -28,7 +28,7 @@
"eslint": "^9.14.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.5.0",
"lemmy-js-client": "0.20.0-alpha.18",
"lemmy-js-client": "0.20.0-api-v4.16",
"prettier": "^3.2.5",
"ts-jest": "^29.1.0",
"typescript": "^5.5.4",

View file

@ -30,8 +30,8 @@ importers:
specifier: ^29.5.0
version: 29.7.0(@types/node@22.9.0)
lemmy-js-client:
specifier: 0.20.0-alpha.18
version: 0.20.0-alpha.18
specifier: 0.20.0-api-v4.16
version: 0.20.0-api-v4.16
prettier:
specifier: ^3.2.5
version: 3.3.3
@ -1167,8 +1167,8 @@ packages:
resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==}
engines: {node: '>=6'}
lemmy-js-client@0.20.0-alpha.18:
resolution: {integrity: sha512-oZy8DboTWfUar4mPWpi7SYrOEjTBJxkvd1e6QaVwoA5UhqQV1WhxEYbzrpi/gXnEokaVQ0i5sjtL/Y2PHMO3MQ==}
lemmy-js-client@0.20.0-api-v4.16:
resolution: {integrity: sha512-9Wn7b8YT2KnEA286+RV1B3mLmecAynvAERoC0ZZiccfSgkEvd3rG9A5X9ejiPqp+JzDZJeisO57+Ut4QHr5oTw==}
leven@3.1.0:
resolution: {integrity: sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==}
@ -3077,7 +3077,7 @@ snapshots:
kleur@3.0.3: {}
lemmy-js-client@0.20.0-alpha.18: {}
lemmy-js-client@0.20.0-api-v4.16: {}
leven@3.1.0: {}

View file

@ -82,13 +82,13 @@ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \
target/lemmy_server >$LOG_DIR/lemmy_epsilon.out 2>&1 &
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/v4/site')" != "200" ]]; do sleep 1; done
echo "alpha started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-beta:8551/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-beta:8551/api/v4/site')" != "200" ]]; do sleep 1; done
echo "beta started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-gamma:8561/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-gamma:8561/api/v4/site')" != "200" ]]; do sleep 1; done
echo "gamma started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-delta:8571/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-delta:8571/api/v4/site')" != "200" ]]; do sleep 1; done
echo "delta started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-epsilon:8581/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-epsilon:8581/api/v4/site')" != "200" ]]; do sleep 1; done
echo "epsilon started. All started"

View file

@ -156,7 +156,6 @@ test("Delete a comment", async () => {
commentRes.comment_view.comment.id,
);
expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
expect(deleteCommentRes.comment_view.comment.content).toBe("");
// Make sure that comment is deleted on beta
await waitUntil(
@ -254,7 +253,6 @@ test("Remove a comment from admin and community on different instance", async ()
betaComment.comment.id,
);
expect(removeCommentRes.comment_view.comment.removed).toBe(true);
expect(removeCommentRes.comment_view.comment.content).toBe("");
// Comment text is also hidden from list
let listComments = await getComments(
@ -263,7 +261,6 @@ test("Remove a comment from admin and community on different instance", async ()
);
expect(listComments.comments.length).toBe(1);
expect(listComments.comments[0].comment.removed).toBe(true);
expect(listComments.comments[0].comment.content).toBe("");
// Make sure its not removed on alpha
let refetchedPostComments = await getComments(
@ -702,10 +699,10 @@ test("Check that activity from another instance is sent to third instance", asyn
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 () => {
// Unfollow all remote communities
let site = await unfollowRemotes(alpha);
expect(
site.my_user?.follows.filter(c => c.community.local == false).length,
).toBe(0);
let my_user = await unfollowRemotes(alpha);
expect(my_user.follows.filter(c => c.community.local == false).length).toBe(
0,
);
// B creates a post, and two comments, should be invisible to A
let postOnBetaRes = await createPost(beta, 2);

View file

@ -25,17 +25,18 @@ import {
getComments,
createComment,
getCommunityByName,
blockInstance,
waitUntil,
alphaUrl,
delta,
betaAllowedInstances,
searchPostLocal,
longDelay,
editCommunity,
unfollows,
getMyUser,
userBlockInstance,
} from "./shared";
import { EditCommunity, EditSite } from "lemmy-js-client";
import { AdminAllowInstanceParams } from "lemmy-js-client/dist/types/AdminAllowInstanceParams";
import { EditCommunity, EditSite, GetPosts } from "lemmy-js-client";
beforeAll(setupLogins);
afterAll(unfollows);
@ -226,7 +227,7 @@ test("Admin actions in remote community are not federated to origin", async () =
if (!betaCommunity) {
throw "Missing beta community";
}
let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
let bannedUserInfo1 = (await getMyUser(gamma)).local_user_view.person;
if (!bannedUserInfo1) {
throw "Missing banned user 1";
}
@ -363,7 +364,7 @@ test("User blocks instance, communities are hidden", async () => {
expect(listing_ids).toContain(postRes.post_view.post.ap_id);
// block the beta instance
await blockInstance(alpha, alphaPost.community.instance_id, true);
await userBlockInstance(alpha, alphaPost.community.instance_id, true);
// after blocking, post should not be in listing
let listing2 = await getPosts(alpha, "All");
@ -371,7 +372,7 @@ test("User blocks instance, communities are hidden", async () => {
expect(listing_ids2.indexOf(postRes.post_view.post.ap_id)).toBe(-1);
// unblock instance again
await blockInstance(alpha, alphaPost.community.instance_id, false);
await userBlockInstance(alpha, alphaPost.community.instance_id, false);
// post should be included in listing
let listing3 = await getPosts(alpha, "All");
@ -455,9 +456,12 @@ test("Dont receive community activities after unsubscribe", async () => {
expect(communityRes1.community_view.counts.subscribers).toBe(2);
// temporarily block alpha, so that it doesn't know about unfollow
let editSiteForm: EditSite = {};
editSiteForm.allowed_instances = ["lemmy-epsilon"];
await beta.editSite(editSiteForm);
var allow_instance_params: AdminAllowInstanceParams = {
instance: "lemmy-alpha",
allow: false,
reason: undefined,
};
await beta.adminAllowInstance(allow_instance_params);
await longDelay();
// unfollow
@ -471,8 +475,8 @@ test("Dont receive community activities after unsubscribe", async () => {
expect(communityRes2.community_view.counts.subscribers).toBe(2);
// unblock alpha
editSiteForm.allowed_instances = betaAllowedInstances;
await beta.editSite(editSiteForm);
allow_instance_params.allow = true;
await beta.adminAllowInstance(allow_instance_params);
await longDelay();
// create a post, it shouldnt reach beta
@ -573,3 +577,29 @@ test("Remote mods can edit communities", async () => {
"Example description",
);
});
test("Community name with non-ascii chars", async () => {
const name = овае_ядосва" + Math.random().toString().slice(2, 6);
let communityRes = await createCommunity(alpha, name);
let betaCommunity1 = await resolveCommunity(
beta,
communityRes.community_view.community.actor_id,
);
expect(betaCommunity1.community!.community.name).toBe(name);
let alphaCommunity2 = await getCommunityByName(alpha, name);
expect(alphaCommunity2.community_view.community.name).toBe(name);
let fediName = `${communityRes.community_view.community.name}@LEMMY-ALPHA:8541`;
let betaCommunity2 = await getCommunityByName(beta, fediName);
expect(betaCommunity2.community_view.community.name).toBe(name);
let postRes = await createPost(beta, betaCommunity1.community!.community.id);
let form: GetPosts = {
community_name: fediName,
};
let posts = await beta.getPosts(form);
expect(posts.posts[0].post.name).toBe(postRes.post_view.post.name);
});

View file

@ -12,6 +12,7 @@ import {
registerUser,
unfollows,
delay,
getMyUser,
} from "./shared";
beforeAll(setupLogins);
@ -85,8 +86,8 @@ test("Follow federated community", async () => {
);
// Check it from local
let site = await getSite(alpha);
let remoteCommunityId = site.my_user?.follows.find(
let my_user = await getMyUser(alpha);
let remoteCommunityId = my_user?.follows.find(
c =>
c.community.local == false &&
c.community.id === betaCommunityInitial.community.id,
@ -102,9 +103,9 @@ test("Follow federated community", async () => {
expect(unfollow.community_view.subscribed).toBe("NotSubscribed");
// Make sure you are unsubbed locally
let siteUnfollowCheck = await getSite(alpha);
let siteUnfollowCheck = await getMyUser(alpha);
expect(
siteUnfollowCheck.my_user?.follows.find(
siteUnfollowCheck.follows.find(
c => c.community.id === betaCommunityInitial.community.id,
),
).toBe(undefined);

View file

@ -32,6 +32,7 @@ import {
createPostWithThumbnail,
sampleImage,
sampleSite,
getMyUser,
} from "./shared";
beforeAll(setupLogins);
@ -129,9 +130,9 @@ test("Purge user, uploaded image removed", async () => {
expect(content.length).toBeGreaterThan(0);
// purge user
let site = await getSite(user);
let my_user = await getMyUser(user);
const purgeForm: PurgePerson = {
person_id: site.my_user!.local_user_view.person.id,
person_id: my_user.local_user_view.person.id,
};
const delete_ = await alphaImage.purgePerson(purgeForm);
expect(delete_.success).toBe(true);
@ -199,11 +200,11 @@ test("Images in remote image post are proxied if setting enabled", async () => {
// remote image gets proxied after upload
expect(
post.thumbnail_url?.startsWith(
"http://lemmy-gamma:8561/api/v3/image_proxy?url",
"http://lemmy-gamma:8561/api/v4/image_proxy?url",
),
).toBeTruthy();
expect(
post.body?.startsWith("![](http://lemmy-gamma:8561/api/v3/image_proxy?url"),
post.body?.startsWith("![](http://lemmy-gamma:8561/api/v4/image_proxy?url"),
).toBeTruthy();
// Make sure that it ends with jpg, to be sure its an image
@ -222,12 +223,12 @@ test("Images in remote image post are proxied if setting enabled", async () => {
expect(
epsilonPost.thumbnail_url?.startsWith(
"http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();
expect(
epsilonPost.body?.startsWith(
"![](http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"![](http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();
@ -249,7 +250,7 @@ test("Thumbnail of remote image link is proxied if setting enabled", async () =>
// remote image gets proxied after upload
expect(
post.thumbnail_url?.startsWith(
"http://lemmy-gamma:8561/api/v3/image_proxy?url",
"http://lemmy-gamma:8561/api/v4/image_proxy?url",
),
).toBeTruthy();
@ -267,7 +268,7 @@ test("Thumbnail of remote image link is proxied if setting enabled", async () =>
expect(
epsilonPost.thumbnail_url?.startsWith(
"http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();

View file

@ -38,8 +38,10 @@ import {
alphaUrl,
loginUser,
createCommunity,
getMyUser,
} from "./shared";
import { PostView } from "lemmy-js-client/dist/types/PostView";
import { AdminBlockInstanceParams } from "lemmy-js-client/dist/types/AdminBlockInstanceParams";
import { EditSite, ResolveObject } from "lemmy-js-client";
let betaCommunity: CommunityView | undefined;
@ -87,12 +89,12 @@ async function assertPostFederation(
}
test("Create a post", async () => {
// Setup some allowlists and blocklists
const editSiteForm: EditSite = {};
editSiteForm.allowed_instances = [];
editSiteForm.blocked_instances = ["lemmy-alpha"];
await epsilon.editSite(editSiteForm);
// Block alpha
var block_instance_params: AdminBlockInstanceParams = {
instance: "lemmy-alpha",
block: true,
};
await epsilon.adminBlockInstance(block_instance_params);
if (!betaCommunity) {
throw "Missing beta community";
@ -132,11 +134,9 @@ test("Create a post", async () => {
resolvePost(epsilon, postRes.post_view.post),
).rejects.toStrictEqual(Error("not_found"));
// remove added allow/blocklists
editSiteForm.allowed_instances = [];
editSiteForm.blocked_instances = [];
await delta.editSite(editSiteForm);
await epsilon.editSite(editSiteForm);
// remove blocked instance
block_instance_params.block = false;
await epsilon.adminBlockInstance(block_instance_params);
});
test("Create a post in a non-existent community", async () => {
@ -452,8 +452,7 @@ test("Enforce site ban federation for local user", async () => {
// create a test user
let alphaUserHttp = await registerUser(alpha, alphaUrl);
let alphaUserPerson = (await getSite(alphaUserHttp)).my_user?.local_user_view
.person;
let alphaUserPerson = (await getMyUser(alphaUserHttp)).local_user_view.person;
let alphaUserActorId = alphaUserPerson?.actor_id;
if (!alphaUserActorId) {
throw "Missing alpha user actor id";
@ -533,8 +532,7 @@ test("Enforce site ban federation for federated user", async () => {
// create a test user
let alphaUserHttp = await registerUser(alpha, alphaUrl);
let alphaUserPerson = (await getSite(alphaUserHttp)).my_user?.local_user_view
.person;
let alphaUserPerson = (await getMyUser(alphaUserHttp)).local_user_view.person;
let alphaUserActorId = alphaUserPerson?.actor_id;
if (!alphaUserActorId) {
throw "Missing alpha user actor id";
@ -564,8 +562,7 @@ test("Enforce site ban federation for federated user", async () => {
expect(banAlphaOnBeta.banned).toBe(true);
// The beta site ban should NOT be federated to alpha
let alphaPerson2 = (await getSite(alphaUserHttp)).my_user!.local_user_view
.person;
let alphaPerson2 = (await getMyUser(alphaUserHttp)).local_user_view.person;
expect(alphaPerson2.banned).toBe(false);
// existing alpha post should be removed on beta

View file

@ -1,9 +1,8 @@
import {
AdminBlockInstanceParams,
ApproveCommunityPendingFollower,
BlockCommunity,
BlockCommunityResponse,
BlockInstance,
BlockInstanceResponse,
CommunityId,
CommunityVisibility,
CreatePrivateMessageReport,
@ -17,15 +16,18 @@ import {
LemmyHttp,
ListCommunityPendingFollows,
ListCommunityPendingFollowsResponse,
MyUserInfo,
PersonId,
PostView,
PrivateMessageReportResponse,
SuccessResponse,
UserBlockInstanceParams,
} from "lemmy-js-client";
import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
import { EditPost } from "lemmy-js-client/dist/types/EditPost";
import { EditSite } from "lemmy-js-client/dist/types/EditSite";
import { AdminAllowInstanceParams } from "lemmy-js-client/dist/types/AdminAllowInstanceParams";
import { FeaturePost } from "lemmy-js-client/dist/types/FeaturePost";
import { GetComments } from "lemmy-js-client/dist/types/GetComments";
import { GetCommentsResponse } from "lemmy-js-client/dist/types/GetCommentsResponse";
@ -104,13 +106,6 @@ export const gamma = new LemmyHttp(gammaUrl, { fetchFunction });
export const delta = new LemmyHttp(deltaUrl, { fetchFunction });
export const epsilon = new LemmyHttp(epsilonUrl, { fetchFunction });
export const betaAllowedInstances = [
"lemmy-alpha",
"lemmy-gamma",
"lemmy-delta",
"lemmy-epsilon",
];
const password = "lemmylemmy";
export async function setupLogins() {
@ -168,30 +163,29 @@ export async function setupLogins() {
rate_limit_comment: 999,
rate_limit_search: 999,
};
// Set the blocks and auths for each
editSiteForm.allowed_instances = [
"lemmy-beta",
"lemmy-gamma",
"lemmy-delta",
"lemmy-epsilon",
];
await alpha.editSite(editSiteForm);
editSiteForm.allowed_instances = betaAllowedInstances;
await beta.editSite(editSiteForm);
editSiteForm.allowed_instances = [
"lemmy-alpha",
"lemmy-beta",
"lemmy-delta",
"lemmy-epsilon",
];
await gamma.editSite(editSiteForm);
// Setup delta allowed instance
editSiteForm.allowed_instances = ["lemmy-beta"];
await delta.editSite(editSiteForm);
await epsilon.editSite(editSiteForm);
// Set the blocks for each
await allowInstance(alpha, "lemmy-beta");
await allowInstance(alpha, "lemmy-gamma");
await allowInstance(alpha, "lemmy-delta");
await allowInstance(alpha, "lemmy-epsilon");
await allowInstance(beta, "lemmy-alpha");
await allowInstance(beta, "lemmy-gamma");
await allowInstance(beta, "lemmy-delta");
await allowInstance(beta, "lemmy-epsilon");
await allowInstance(gamma, "lemmy-alpha");
await allowInstance(gamma, "lemmy-beta");
await allowInstance(gamma, "lemmy-delta");
await allowInstance(gamma, "lemmy-epsilon");
await allowInstance(delta, "lemmy-beta");
// Create the main alpha/beta communities
// Ignore thrown errors of duplicates
@ -208,6 +202,17 @@ export async function setupLogins() {
}
}
async function allowInstance(api: LemmyHttp, instance: string) {
const params: AdminAllowInstanceParams = {
instance,
allow: true,
};
// Ignore errors from duplicate allows (because setup gets called for each test file)
try {
await api.adminAllowInstance(params);
} catch {}
}
export async function createPost(
api: LemmyHttp,
community_id: number,
@ -757,6 +762,10 @@ export async function getSite(api: LemmyHttp): Promise<GetSiteResponse> {
return api.getSite();
}
export async function getMyUser(api: LemmyHttp): Promise<MyUserInfo> {
return api.getMyUser();
}
export async function listPrivateMessages(
api: LemmyHttp,
): Promise<PrivateMessagesResponse> {
@ -766,19 +775,16 @@ export async function listPrivateMessages(
return api.getPrivateMessages(form);
}
export async function unfollowRemotes(
api: LemmyHttp,
): Promise<GetSiteResponse> {
export async function unfollowRemotes(api: LemmyHttp): Promise<MyUserInfo> {
// Unfollow all remote communities
let site = await getSite(api);
let my_user = await getMyUser(api);
let remoteFollowed =
site.my_user?.follows.filter(c => c.community.local == false) ?? [];
my_user.follows.filter(c => c.community.local == false) ?? [];
await Promise.all(
remoteFollowed.map(cu => followCommunity(api, false, cu.community.id)),
);
let siteRes = await getSite(api);
return siteRes;
return await getMyUser(api);
}
export async function followBeta(api: LemmyHttp): Promise<CommunityResponse> {
@ -854,16 +860,16 @@ export function getPosts(
return api.getPosts(form);
}
export function blockInstance(
export function userBlockInstance(
api: LemmyHttp,
instance_id: InstanceId,
block: boolean,
): Promise<BlockInstanceResponse> {
let form: BlockInstance = {
): Promise<SuccessResponse> {
let form: UserBlockInstanceParams = {
instance_id,
block,
};
return api.blockInstance(form);
return api.userBlockInstance(form);
}
export function blockCommunity(

View file

@ -22,8 +22,15 @@ import {
alphaImage,
unfollows,
saveUserSettingsBio,
getMyUser,
getPersonDetails,
} from "./shared";
import { LemmyHttp, SaveUserSettings, UploadImage } from "lemmy-js-client";
import {
EditSite,
LemmyHttp,
SaveUserSettings,
UploadImage,
} from "lemmy-js-client";
import { GetPosts } from "lemmy-js-client/dist/types/GetPosts";
beforeAll(setupLogins);
@ -44,12 +51,9 @@ function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
test("Create user", async () => {
let user = await registerUser(alpha, alphaUrl);
let site = await getSite(user);
expect(site.my_user).toBeDefined();
if (!site.my_user) {
throw "Missing site user";
}
apShortname = `${site.my_user.local_user_view.person.name}@lemmy-alpha:8541`;
let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
apShortname = `${my_user.local_user_view.person.name}@lemmy-alpha:8541`;
});
test("Set some user settings, check that they are federated", async () => {
@ -64,12 +68,15 @@ test("Set some user settings, check that they are federated", async () => {
};
await saveUserSettings(beta, form);
let site = await getSite(beta);
expect(site.my_user?.local_user_view.local_user.theme).toBe("test");
let my_user = await getMyUser(beta);
expect(my_user.local_user_view.local_user.theme).toBe("test");
});
test("Delete user", async () => {
let user = await registerUser(alpha, alphaUrl);
let user_profile = await getMyUser(user);
let person_id = user_profile.local_user_view.person.id;
let actor_id = user_profile.local_user_view.person.actor_id;
// make a local post and comment
let alphaCommunity = (await resolveCommunity(user, "main@lemmy-alpha:8541"))
@ -97,6 +104,10 @@ test("Delete user", async () => {
expect(remoteComment).toBeDefined();
await deleteUser(user);
await expect(getMyUser(user)).rejects.toStrictEqual(Error("incorrect_login"));
await expect(getPersonDetails(user, person_id)).rejects.toStrictEqual(
Error("not_found"),
);
// check that posts and comments are marked as deleted on other instances.
// use get methods to avoid refetching from origin instance
@ -114,6 +125,9 @@ test("Delete user", async () => {
(await getComments(alpha, remoteComment.post_id)).comments[0].comment
.deleted,
).toBe(true);
await expect(
getPersonDetails(user, remoteComment.creator_id),
).rejects.toStrictEqual(Error("not_found"));
});
test("Requests with invalid auth should be treated as unauthenticated", async () => {
@ -121,8 +135,10 @@ test("Requests with invalid auth should be treated as unauthenticated", async ()
headers: { Authorization: "Bearer foobar" },
fetchFunction,
});
await expect(getMyUser(invalid_auth)).rejects.toStrictEqual(
Error("incorrect_login"),
);
let site = await getSite(invalid_auth);
expect(site.my_user).toBeUndefined();
expect(site.site_view).toBeDefined();
let form: GetPosts = {};
@ -131,37 +147,39 @@ test("Requests with invalid auth should be treated as unauthenticated", async ()
});
test("Create user with Arabic name", async () => {
let user = await registerUser(
alpha,
alphaUrl,
"تجريب" + Math.random().toString().slice(2, 10), // less than actor_name_max_length
);
// less than actor_name_max_length
const name = "تجريب" + Math.random().toString().slice(2, 10);
let user = await registerUser(alpha, alphaUrl, name);
let site = await getSite(user);
expect(site.my_user).toBeDefined();
if (!site.my_user) {
throw "Missing site user";
}
apShortname = `${site.my_user.local_user_view.person.name}@lemmy-alpha:8541`;
let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
apShortname = `${my_user.local_user_view.person.name}@lemmy-alpha:8541`;
let alphaPerson = (await resolvePerson(alpha, apShortname)).person;
expect(alphaPerson).toBeDefined();
let betaPerson1 = (await resolvePerson(beta, apShortname)).person;
expect(betaPerson1!.person.name).toBe(name);
let betaPerson2 = await getPersonDetails(beta, betaPerson1!.person.id);
expect(betaPerson2!.person_view.person.name).toBe(name);
});
test("Create user with accept-language", async () => {
const edit: EditSite = {
discussion_languages: [32],
};
await alpha.editSite(edit);
let lemmy_http = new LemmyHttp(alphaUrl, {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#syntax
headers: { "Accept-Language": "fr-CH, en;q=0.8, de;q=0.7, *;q=0.5" },
headers: { "Accept-Language": "fr-CH, en;q=0.8, *;q=0.5" },
});
let user = await registerUser(lemmy_http, alphaUrl);
let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
expect(my_user?.local_user_view.local_user.interface_language).toBe("fr");
let site = await getSite(user);
expect(site.my_user).toBeDefined();
expect(site.my_user?.local_user_view.local_user.interface_language).toBe(
"fr",
);
let langs = site.all_languages
.filter(a => site.my_user?.discussion_languages.includes(a.id))
.filter(a => my_user.discussion_languages.includes(a.id))
.map(l => l.code);
// should have languages from accept header, as well as "undetermined"
// which is automatically enabled by backend
@ -207,8 +225,8 @@ test("Set a new avatar, old avatar is deleted", async () => {
// Now try to save a user settings, with the icon missing,
// and make sure it doesn't clear the data, or delete the image
await saveUserSettingsBio(alpha);
let site = await getSite(alpha);
expect(site.my_user?.local_user_view.person.avatar).toBe(upload2.url);
let my_user = await getMyUser(alpha);
expect(my_user.local_user_view.person.avatar).toBe(upload2.url);
// make sure only the new avatar is kept
const listMediaRes4 = await alphaImage.listMedia();

View file

@ -1,11 +1,7 @@
{
# settings related to the postgresql database
database: {
# Configure the database by specifying a URI
#
# This is the preferred method to specify database connection details since
# it is the most flexible.
# Connection URI pointing to a postgres instance
# Configure the database by specifying URI pointing to a postgres instance
#
# This example uses peer authentication to obviate the need for creating,
# configuring, and managing passwords.
@ -14,25 +10,7 @@
# PostgreSQL's documentation.
#
# [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
uri: "postgresql:///lemmy?user=lemmy&host=/var/run/postgresql"
# or
# Configure the database by specifying parts of a URI
#
# Note that specifying the `uri` field should be preferred since it provides
# greater control over how the connection is made. This merely exists for
# backwards-compatibility.
# Username to connect to postgres
user: "string"
# Password to connect to postgres
password: "string"
# Host where postgres is running
host: "string"
# Port where postgres can be accessed
port: 123
# Name of the postgres database for lemmy
database: "string"
connection: "postgres://lemmy:password@localhost:5432/lemmy"
# Maximum number of active sql connections
pool_size: 30
}
@ -66,13 +44,22 @@
# or
# If enabled, all images from remote domains are rewritten to pass through
# `/api/v3/image_proxy`, including embedded images in markdown. Images are stored temporarily
# `/api/v4/image_proxy`, including embedded images in markdown. Images are stored temporarily
# in pict-rs for caching. This improves privacy as users don't expose their IP to untrusted
# servers, and decreases load on other servers. However it increases bandwidth use for the
# local server.
#
# Requires pict-rs 0.5
"ProxyAllImages"
# Allows bypassing proxy for specific image hosts when using ProxyAllImages.
#
# imgur.com is bypassed by default to avoid rate limit errors. When specifying any bypass
# in the config, this default is ignored and you need to list imgur explicitly. To proxy imgur
# requests, specify a noop bypass list, eg `proxy_bypass_domains ["example.org"]`.
proxy_bypass_domains: [
"i.imgur.com"
/* ... */
]
# Timeout for uploading images to pictrs (in seconds)
upload_timeout: 30
# Resize post thumbnails to this maximum width/height.

View file

@ -10,7 +10,7 @@ use lemmy_db_schema::{
source::{
community::{Community, CommunityModerator, CommunityModeratorForm},
local_user::LocalUser,
moderator::{ModAddCommunity, ModAddCommunityForm},
mod_log::moderator::{ModAddCommunity, ModAddCommunityForm},
},
traits::{Crud, Joinable},
};

View file

@ -20,7 +20,7 @@ use lemmy_db_schema::{
CommunityPersonBanForm,
},
local_user::LocalUser,
moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
mod_log::moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
},
traits::{Bannable, Crud, Followable},
};
@ -110,7 +110,7 @@ pub async fn ban_from_community(
ModBanFromCommunity::create(&mut context.pool(), &form).await?;
let person_view = PersonView::read(&mut context.pool(), data.person_id).await?;
let person_view = PersonView::read(&mut context.pool(), data.person_id, false).await?;
ActivityChannel::submit_activity(
SendActivityData::BanFromCommunity {

View file

@ -17,7 +17,7 @@ use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
#[tracing::instrument(skip(context))]
pub async fn block_community(
pub async fn user_block_community(
data: Json<BlockCommunity>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,

View file

@ -10,7 +10,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
community::{Community, CommunityUpdateForm},
moderator::{ModHideCommunity, ModHideCommunityForm},
mod_log::moderator::{ModHideCommunity, ModHideCommunityForm},
},
traits::Crud,
};

View file

@ -8,7 +8,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
community::{Community, CommunityModerator, CommunityModeratorForm},
moderator::{ModTransferCommunity, ModTransferCommunityForm},
mod_log::moderator::{ModTransferCommunity, ModTransferCommunityForm},
},
traits::{Crud, Joinable},
};

View file

@ -19,7 +19,7 @@ use lemmy_db_schema::{
CommunityPersonBanForm,
},
local_site::LocalSite,
moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
mod_log::moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
person::Person,
},
traits::{Bannable, Crud, Followable},

View file

@ -7,7 +7,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
local_user::{LocalUser, LocalUserUpdateForm},
moderator::{ModAdd, ModAddForm},
mod_log::moderator::{ModAdd, ModAddForm},
},
traits::Crud,
};

View file

@ -11,7 +11,7 @@ use lemmy_db_schema::{
source::{
local_user::LocalUser,
login_token::LoginToken,
moderator::{ModBan, ModBanForm},
mod_log::moderator::{ModBan, ModBanForm},
person::{Person, PersonUpdateForm},
},
traits::Crud,
@ -88,7 +88,7 @@ pub async fn ban_from_site(
ModBan::create(&mut context.pool(), &form).await?;
let person_view = PersonView::read(&mut context.pool(), person.id).await?;
let person_view = PersonView::read(&mut context.pool(), person.id, false).await?;
ban_nonlocal_user_from_local_communities(
&local_user_view,

View file

@ -12,7 +12,7 @@ use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
#[tracing::instrument(skip(context))]
pub async fn block_person(
pub async fn user_block_person(
data: Json<BlockPerson>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
@ -48,7 +48,7 @@ pub async fn block_person(
.with_lemmy_type(LemmyErrorType::PersonBlockAlreadyExists)?;
}
let person_view = PersonView::read(&mut context.pool(), target_id).await?;
let person_view = PersonView::read(&mut context.pool(), target_id, false).await?;
Ok(Json(BlockPersonResponse {
person_view,
blocked: data.block,

View file

@ -15,5 +15,6 @@ pub mod report_count;
pub mod reset_password;
pub mod save_settings;
pub mod update_totp;
pub mod user_block_instance;
pub mod validate_auth;
pub mod verify_email;

View file

@ -1,9 +1,6 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
site::{BlockInstance, BlockInstanceResponse},
};
use lemmy_api_common::{context::LemmyContext, site::UserBlockInstanceParams, SuccessResponse};
use lemmy_db_schema::{
source::instance_block::{InstanceBlock, InstanceBlockForm},
traits::Blockable,
@ -12,11 +9,11 @@ use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
#[tracing::instrument(skip(context))]
pub async fn block_instance(
data: Json<BlockInstance>,
pub async fn user_block_instance(
data: Json<UserBlockInstanceParams>,
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<BlockInstanceResponse>> {
) -> LemmyResult<Json<SuccessResponse>> {
let instance_id = data.instance_id;
let person_id = local_user_view.person.id;
if local_user_view.person.instance_id == instance_id {
@ -38,7 +35,5 @@ pub async fn block_instance(
.with_lemmy_type(LemmyErrorType::InstanceBlockAlreadyExists)?;
}
Ok(Json(BlockInstanceResponse {
blocked: data.block,
}))
Ok(Json(SuccessResponse::default()))
}

View file

@ -10,7 +10,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
community::Community,
moderator::{ModFeaturePost, ModFeaturePostForm},
mod_log::moderator::{ModFeaturePost, ModFeaturePostForm},
post::{Post, PostUpdateForm},
},
traits::Crud,

View file

@ -9,7 +9,7 @@ use lemmy_api_common::{
};
use lemmy_db_schema::{
source::{
moderator::{ModLockPost, ModLockPostForm},
mod_log::moderator::{ModLockPost, ModLockPostForm},
post::{Post, PostUpdateForm},
},
traits::Crud,

View file

@ -0,0 +1,53 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
site::AdminAllowInstanceParams,
utils::is_admin,
LemmyErrorType,
SuccessResponse,
};
use lemmy_db_schema::source::{
federation_allowlist::{FederationAllowList, FederationAllowListForm},
instance::Instance,
mod_log::admin::{AdminAllowInstance, AdminAllowInstanceForm},
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyResult;
#[tracing::instrument(skip(context))]
pub async fn admin_allow_instance(
data: Json<AdminAllowInstanceParams>,
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
is_admin(&local_user_view)?;
let blocklist = Instance::blocklist(&mut context.pool()).await?;
if !blocklist.is_empty() {
Err(LemmyErrorType::CannotCombineFederationBlocklistAndAllowlist)?;
}
let instance_id = Instance::read_or_create(&mut context.pool(), data.instance.clone())
.await?
.id;
let form = FederationAllowListForm {
instance_id,
updated: None,
};
if data.allow {
FederationAllowList::allow(&mut context.pool(), &form).await?;
} else {
FederationAllowList::unallow(&mut context.pool(), instance_id).await?;
}
let mod_log_form = AdminAllowInstanceForm {
instance_id,
admin_person_id: local_user_view.person.id,
reason: data.reason.clone(),
allowed: data.allow,
};
AdminAllowInstance::insert(&mut context.pool(), &mod_log_form).await?;
Ok(Json(SuccessResponse::default()))
}

View file

@ -0,0 +1,56 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
site::AdminBlockInstanceParams,
utils::is_admin,
LemmyErrorType,
SuccessResponse,
};
use lemmy_db_schema::source::{
federation_blocklist::{FederationBlockList, FederationBlockListForm},
instance::Instance,
mod_log::admin::{AdminBlockInstance, AdminBlockInstanceForm},
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyResult;
#[tracing::instrument(skip(context))]
pub async fn admin_block_instance(
data: Json<AdminBlockInstanceParams>,
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
is_admin(&local_user_view)?;
let allowlist = Instance::allowlist(&mut context.pool()).await?;
if !allowlist.is_empty() {
Err(LemmyErrorType::CannotCombineFederationBlocklistAndAllowlist)?;
}
let instance_id = Instance::read_or_create(&mut context.pool(), data.instance.clone())
.await?
.id;
let form = FederationBlockListForm {
instance_id,
expires: data.expires,
updated: None,
};
if data.block {
FederationBlockList::block(&mut context.pool(), &form).await?;
} else {
FederationBlockList::unblock(&mut context.pool(), instance_id).await?;
}
let mod_log_form = AdminBlockInstanceForm {
instance_id,
admin_person_id: local_user_view.person.id,
blocked: data.block,
reason: data.reason.clone(),
when_: data.expires,
};
AdminBlockInstance::insert(&mut context.pool(), &mod_log_form).await?;
Ok(Json(SuccessResponse::default()))
}

View file

@ -6,7 +6,7 @@ use lemmy_db_schema::{
language::Language,
local_site_url_blocklist::LocalSiteUrlBlocklist,
local_user::{LocalUser, LocalUserUpdateForm},
moderator::{ModAdd, ModAddForm},
mod_log::moderator::{ModAdd, ModAddForm},
oauth_provider::OAuthProvider,
tagline::Tagline,
},
@ -69,14 +69,12 @@ pub async fn leave_admin(
site_view,
admins,
version: VERSION.to_string(),
my_user: None,
all_languages,
discussion_languages,
oauth_providers: Some(oauth_providers),
admin_oauth_providers: None,
blocked_urls,
tagline,
taglines: vec![],
custom_emojis: vec![],
my_user: None,
}))
}

View file

@ -1,4 +1,5 @@
pub mod block;
pub mod admin_allow_instance;
pub mod admin_block_instance;
pub mod federated_instances;
pub mod leave_admin;
pub mod list_all_media;

View file

@ -7,6 +7,8 @@ use lemmy_api_common::{
use lemmy_db_schema::{source::local_site::LocalSite, ModlogActionType};
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_moderator::structs::{
AdminAllowInstanceView,
AdminBlockInstanceView,
AdminPurgeCommentView,
AdminPurgeCommunityView,
AdminPurgePersonView,
@ -121,6 +123,8 @@ pub async fn get_mod_log(
admin_purged_communities,
admin_purged_posts,
admin_purged_comments,
admin_block_instance,
admin_allow_instance,
) = if data.community_id.is_none() {
(
match type_ {
@ -161,6 +165,18 @@ pub async fn get_mod_log(
}
_ => Default::default(),
},
match type_ {
All | AdminBlockInstance if other_person_id.is_none() => {
AdminBlockInstanceView::list(&mut context.pool(), params).await?
}
_ => Default::default(),
},
match type_ {
All | AdminAllowInstance if other_person_id.is_none() => {
AdminAllowInstanceView::list(&mut context.pool(), params).await?
}
_ => Default::default(),
},
)
} else {
Default::default()
@ -183,5 +199,7 @@ pub async fn get_mod_log(
admin_purged_posts,
admin_purged_comments,
hidden_communities,
admin_block_instance,
admin_allow_instance,
}))
}

View file

@ -11,7 +11,7 @@ use lemmy_db_schema::{
source::{
comment::Comment,
local_user::LocalUser,
moderator::{AdminPurgeComment, AdminPurgeCommentForm},
mod_log::admin::{AdminPurgeComment, AdminPurgeCommentForm},
},
traits::Crud,
};

View file

@ -13,7 +13,7 @@ use lemmy_db_schema::{
source::{
community::Community,
local_user::LocalUser,
moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm},
mod_log::admin::{AdminPurgeCommunity, AdminPurgeCommunityForm},
},
traits::Crud,
};

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
local_user::LocalUser,
moderator::{AdminPurgePerson, AdminPurgePersonForm},
mod_log::admin::{AdminPurgePerson, AdminPurgePersonForm},
person::{Person, PersonUpdateForm},
},
traits::Crud,

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
local_user::LocalUser,
moderator::{AdminPurgePost, AdminPurgePostForm},
mod_log::admin::{AdminPurgePost, AdminPurgePostForm},
post::Post,
},
traits::Crud,

View file

@ -66,6 +66,7 @@ enum-map = { workspace = true }
urlencoding = { workspace = true }
mime = { version = "0.3.17", optional = true }
mime_guess = "2.0.5"
infer = "0.16.0"
webpage = { version = "2.0", default-features = false, features = [
"serde",
], optional = true }

View file

@ -11,7 +11,7 @@ Here is an example using [reqwest](https://crates.io/crates/reqwest):
};
let client = Client::new();
let response = client
.get("https://lemmy.ml/api/v3/post/list")
.get("https://lemmy.ml/api/v4/post/list")
.query(&params)
.send()
.await?;

View file

@ -25,6 +25,8 @@ pub struct CreateOAuthProvider {
#[cfg_attr(feature = "full", ts(optional))]
pub account_linking_enabled: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub use_pkce: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub enabled: Option<bool>,
}
@ -54,6 +56,8 @@ pub struct EditOAuthProvider {
#[cfg_attr(feature = "full", ts(optional))]
pub account_linking_enabled: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub use_pkce: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub enabled: Option<bool>,
}
@ -82,4 +86,6 @@ pub struct AuthenticateWithOauth {
/// An answer is mandatory if require application is enabled on the server
#[cfg_attr(feature = "full", ts(optional))]
pub answer: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub pkce_code_verifier: Option<String>,
}

View file

@ -1,5 +1,5 @@
use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId},
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId, TagId},
ListingType,
PostFeatureType,
PostSortType,
@ -37,6 +37,8 @@ pub struct CreatePost {
/// Instead of fetching a thumbnail, use a custom one.
#[cfg_attr(feature = "full", ts(optional))]
pub custom_thumbnail: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub tags: Option<Vec<TagId>>,
/// Time when this post should be scheduled. Null means publish immediately.
#[cfg_attr(feature = "full", ts(optional))]
pub scheduled_publish_time: Option<i64>,
@ -164,6 +166,8 @@ pub struct EditPost {
/// Instead of fetching a thumbnail, use a custom one.
#[cfg_attr(feature = "full", ts(optional))]
pub custom_thumbnail: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub tags: Option<Vec<TagId>>,
/// Time when this post should be scheduled. Null means publish immediately.
#[cfg_attr(feature = "full", ts(optional))]
pub scheduled_publish_time: Option<i64>,

View file

@ -23,6 +23,7 @@ use lemmy_utils::{
REQWEST_TIMEOUT,
VERSION,
};
use mime::{Mime, TEXT_HTML};
use reqwest::{
header::{CONTENT_TYPE, RANGE},
Client,
@ -50,9 +51,11 @@ pub fn client_builder(settings: &Settings) -> ClientBuilder {
#[tracing::instrument(skip_all)]
pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResult<LinkMetadata> {
info!("Fetching site metadata for url: {}", url);
// We only fetch the first 64kB of data in order to not waste bandwidth especially for large
// binary files
let bytes_to_fetch = 64 * 1024;
// We only fetch the first MB of data in order to not waste bandwidth especially for large
// binary files. This high limit is particularly needed for youtube, which includes a lot of
// javascript code before the opengraph tags. Mastodon also uses a 1 MB limit:
// https://github.com/mastodon/mastodon/blob/295ad6f19a016b3f16e1201ffcbb1b3ad6b455a2/app/lib/request.rb#L213
let bytes_to_fetch = 1024 * 1024;
let response = context
.client()
.get(url.as_str())
@ -63,47 +66,54 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
.await?
.error_for_status()?;
// In some cases servers send a wrong mime type for images, which prevents thumbnail
// generation. To avoid this we also try to guess the mime type from file extension.
let content_type = mime_guess::from_path(url.path())
.first()
// If you can guess that its an image type, then return that first.
.filter(|guess| guess.type_() == mime::IMAGE)
// Otherwise, get the content type from the headers
.or(
response
.headers()
.get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok())
.and_then(|h| h.parse().ok()),
);
let mut content_type: Option<Mime> = response
.headers()
.get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok())
.and_then(|h| h.parse().ok())
// If we don't get a content_type from the response (e.g. if the server is down),
// then try to infer the content_type from the file extension.
.or(mime_guess::from_path(url.path()).first());
let opengraph_data = {
// if the content type is not text/html, we don't need to parse it
let is_html = content_type
.as_ref()
.map(|c| {
(c.type_() == mime::TEXT && c.subtype() == mime::HTML)
||
// application/xhtml+xml is a subset of HTML
(c.type_() == mime::APPLICATION && c.subtype() == "xhtml")
// application/xhtml+xml is a subset of HTML
let application_xhtml: Mime = "application/xhtml+xml".parse::<Mime>().unwrap_or(TEXT_HTML);
let allowed_mime_types = [TEXT_HTML.essence_str(), application_xhtml.essence_str()];
allowed_mime_types.contains(&c.essence_str())
})
.unwrap_or(false);
if !is_html {
Default::default()
} else {
.unwrap_or_default();
if is_html {
// Can't use .text() here, because it only checks the content header, not the actual bytes
// https://github.com/LemmyNet/lemmy/issues/1964
// So we want to do deep inspection of the actually returned bytes but need to be careful not
// spend too much time parsing binary data as HTML
// So we want to do deep inspection of the actually returned bytes but need to be careful
// not spend too much time parsing binary data as HTML
// only take first bytes regardless of how many bytes the server returns
let html_bytes = collect_bytes_until_limit(response, bytes_to_fetch).await?;
extract_opengraph_data(&html_bytes, url)
.map_err(|e| info!("{e}"))
.unwrap_or_default()
} else {
let is_octet_type = content_type
.as_ref()
.map(|c| c.subtype() == "octet-stream")
.unwrap_or_default();
// Overwrite the content type if its an octet type
if is_octet_type {
// Don't need to fetch as much data for this as we do with opengraph
let octet_bytes = collect_bytes_until_limit(response, 512).await?;
content_type =
infer::get(&octet_bytes).map_or(content_type, |t| t.mime_type().parse().ok());
}
Default::default()
}
};
Ok(LinkMetadata {
opengraph_data,
content_type: content_type.map(|c| c.to_string()),

View file

@ -43,6 +43,8 @@ use lemmy_db_views_actor::structs::{
PersonView,
};
use lemmy_db_views_moderator::structs::{
AdminAllowInstanceView,
AdminBlockInstanceView,
AdminPurgeCommentView,
AdminPurgeCommunityView,
AdminPurgePersonView,
@ -183,6 +185,8 @@ pub struct GetModlogResponse {
pub admin_purged_posts: Vec<AdminPurgePostView>,
pub admin_purged_comments: Vec<AdminPurgeCommentView>,
pub hidden_communities: Vec<ModHideCommunityView>,
pub admin_block_instance: Vec<AdminBlockInstanceView>,
pub admin_allow_instance: Vec<AdminAllowInstanceView>,
}
#[skip_serializing_none]
@ -265,10 +269,6 @@ pub struct CreateSite {
#[cfg_attr(feature = "full", ts(optional))]
pub captcha_difficulty: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub allowed_instances: Option<Vec<String>>,
#[cfg_attr(feature = "full", ts(optional))]
pub blocked_instances: Option<Vec<String>>,
#[cfg_attr(feature = "full", ts(optional))]
pub registration_mode: Option<RegistrationMode>,
#[cfg_attr(feature = "full", ts(optional))]
pub oauth_registration: Option<bool>,
@ -394,12 +394,6 @@ pub struct EditSite {
/// The captcha difficulty. Can be easy, medium, or hard
#[cfg_attr(feature = "full", ts(optional))]
pub captcha_difficulty: Option<String>,
/// A list of allowed instances. If none are set, federation is open.
#[cfg_attr(feature = "full", ts(optional))]
pub allowed_instances: Option<Vec<String>>,
/// A list of blocked instances.
#[cfg_attr(feature = "full", ts(optional))]
pub blocked_instances: Option<Vec<String>>,
/// A list of blocked URLs
#[cfg_attr(feature = "full", ts(optional))]
pub blocked_urls: Option<Vec<String>>,
@ -435,7 +429,7 @@ pub struct EditSite {
/// The response for a site.
pub struct SiteResponse {
pub site_view: SiteView,
/// deprecated, use field `tagline` or /api/v3/tagline/list
/// deprecated, use field `tagline` or /api/v4/tagline/list
pub taglines: Vec<()>,
}
@ -448,14 +442,10 @@ pub struct GetSiteResponse {
pub site_view: SiteView,
pub admins: Vec<PersonView>,
pub version: String,
#[cfg_attr(feature = "full", ts(optional))]
#[cfg_attr(feature = "full", ts(skip))]
pub my_user: Option<MyUserInfo>,
pub all_languages: Vec<Language>,
pub discussion_languages: Vec<LanguageId>,
/// deprecated, use field `tagline` or /api/v3/tagline/list
pub taglines: Vec<()>,
/// deprecated, use /api/v3/custom_emoji/list
pub custom_emojis: Vec<()>,
/// If the site has any taglines, a random one is included here for displaying
#[cfg_attr(feature = "full", ts(optional))]
pub tagline: Option<Tagline>,
@ -648,15 +638,29 @@ pub struct GetUnreadRegistrationApplicationCountResponse {
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Block an instance as user
pub struct BlockInstance {
pub struct UserBlockInstanceParams {
pub instance_id: InstanceId,
pub block: bool,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct BlockInstanceResponse {
pub blocked: bool,
pub struct AdminBlockInstanceParams {
pub instance: String,
pub block: bool,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub expires: Option<DateTime<Utc>>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct AdminAllowInstanceParams {
pub instance: String,
pub allow: bool,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
}

View file

@ -23,7 +23,12 @@ use lemmy_db_schema::{
local_site::LocalSite,
local_site_rate_limit::LocalSiteRateLimit,
local_site_url_blocklist::LocalSiteUrlBlocklist,
moderator::{ModRemoveComment, ModRemoveCommentForm, ModRemovePost, ModRemovePostForm},
mod_log::moderator::{
ModRemoveComment,
ModRemoveCommentForm,
ModRemovePost,
ModRemovePostForm,
},
oauth_account::OAuthAccount,
password_reset_request::PasswordResetRequest,
person::{Person, PersonUpdateForm},
@ -71,7 +76,7 @@ use tracing::warn;
use url::{ParseError, Url};
use urlencoding::encode;
pub static AUTH_COOKIE_NAME: &str = "jwt";
pub const AUTH_COOKIE_NAME: &str = "jwt";
#[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin(
@ -118,8 +123,6 @@ pub fn is_admin(local_user_view: &LocalUserView) -> LemmyResult<()> {
check_user_valid(&local_user_view.person)?;
if !local_user_view.local_user.admin {
Err(LemmyErrorType::NotAnAdmin)?
} else if local_user_view.person.banned {
Err(LemmyErrorType::Banned)?
} else {
Ok(())
}
@ -1120,7 +1123,7 @@ async fn proxy_image_link_internal(
}
}
/// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and
/// Rewrite a link to go through `/api/v4/image_proxy` endpoint. This is only for remote urls and
/// if image_proxy setting is enabled.
pub async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
proxy_image_link_internal(
@ -1172,7 +1175,7 @@ fn build_proxied_image_url(
protocol_and_hostname: &str,
) -> Result<Url, url::ParseError> {
Url::parse(&format!(
"{}/api/v3/image_proxy?url={}",
"{}/api/v4/image_proxy?url={}",
protocol_and_hostname,
encode(link.as_str())
))
@ -1251,7 +1254,7 @@ mod tests {
)
.await?;
assert_eq!(
"https://lemmy-alpha/api/v3/image_proxy?url=http%3A%2F%2Flemmy-beta%2Fimage.png",
"https://lemmy-alpha/api/v4/image_proxy?url=http%3A%2F%2Flemmy-beta%2Fimage.png",
proxied.as_str()
);

View file

@ -29,6 +29,7 @@ anyhow.workspace = true
chrono.workspace = true
webmention = "0.6.0"
accept-language = "3.1.0"
regex = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
serde_with = { workspace = true }

View file

@ -12,7 +12,7 @@ use lemmy_db_schema::{
comment::{Comment, CommentUpdateForm},
comment_report::CommentReport,
local_user::LocalUser,
moderator::{ModRemoveComment, ModRemoveCommentForm},
mod_log::moderator::{ModRemoveComment, ModRemoveCommentForm},
},
traits::{Crud, Reportable},
};

View file

@ -10,7 +10,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
community::{Community, CommunityUpdateForm},
moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
mod_log::moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
},
traits::Crud,
};

View file

@ -35,6 +35,7 @@ pub async fn create_oauth_provider(
scopes: data.scopes.to_string(),
auto_verify_email: data.auto_verify_email,
account_linking_enabled: data.account_linking_enabled,
use_pkce: data.use_pkce,
enabled: data.enabled,
};
let oauth_provider = OAuthProvider::create(&mut context.pool(), &oauth_provider_form).await?;

View file

@ -33,6 +33,7 @@ pub async fn update_oauth_provider(
auto_verify_email: data.auto_verify_email,
account_linking_enabled: data.account_linking_enabled,
enabled: data.enabled,
use_pkce: data.use_pkce,
updated: Some(Some(Utc::now())),
};

View file

@ -11,7 +11,7 @@ use lemmy_db_schema::{
source::{
community::Community,
local_user::LocalUser,
moderator::{ModRemovePost, ModRemovePostForm},
mod_log::moderator::{ModRemovePost, ModRemovePostForm},
post::{Post, PostUpdateForm},
post_report::PostReport,
},

View file

@ -1,30 +1,32 @@
use crate::user::my_user::get_my_user;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
site::{GetSiteResponse, MyUserInfo},
};
use lemmy_api_common::{context::LemmyContext, site::GetSiteResponse};
use lemmy_db_schema::source::{
actor_language::{LocalUserLanguage, SiteLanguage},
community_block::CommunityBlock,
instance_block::InstanceBlock,
actor_language::SiteLanguage,
language::Language,
local_site_url_blocklist::LocalSiteUrlBlocklist,
oauth_provider::OAuthProvider,
person_block::PersonBlock,
tagline::Tagline,
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView, PersonView};
use lemmy_utils::{
build_cache,
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
CacheLock,
VERSION,
};
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{build_cache, error::LemmyResult, CacheLock, VERSION};
use std::sync::LazyLock;
#[tracing::instrument(skip(context))]
pub async fn get_site(
pub async fn get_site_v3(
local_user_view: Option<LocalUserView>,
context: Data<LemmyContext>,
) -> LemmyResult<Json<GetSiteResponse>> {
let mut site = get_site_v4(local_user_view.clone(), context.clone()).await?;
if let Some(local_user_view) = local_user_view {
site.my_user = Some(get_my_user(local_user_view, context).await?.0);
}
Ok(site)
}
#[tracing::instrument(skip(context))]
pub async fn get_site_v4(
local_user_view: Option<LocalUserView>,
context: Data<LemmyContext>,
) -> LemmyResult<Json<GetSiteResponse>> {
@ -35,42 +37,6 @@ pub async fn get_site(
.await
.map_err(|e| anyhow::anyhow!("Failed to construct site response: {e}"))?;
// Build the local user with parallel queries and add it to site response
site_response.my_user = if let Some(ref local_user_view) = local_user_view {
let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id;
let pool = &mut context.pool();
let (
follows,
community_blocks,
instance_blocks,
person_blocks,
moderates,
discussion_languages,
) = lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Some(MyUserInfo {
local_user_view: local_user_view.clone(),
follows,
moderates,
community_blocks,
instance_blocks,
person_blocks,
discussion_languages,
})
} else {
None
};
// filter oauth_providers for public access
if !local_user_view
.map(|l| l.local_user.admin)
@ -103,7 +69,5 @@ async fn read_site(context: &LemmyContext) -> LemmyResult<GetSiteResponse> {
tagline,
oauth_providers: Some(oauth_providers),
admin_oauth_providers: Some(admin_oauth_providers),
taglines: vec![],
custom_emojis: vec![],
})
}

View file

@ -19,8 +19,6 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
actor_language::SiteLanguage,
federation_allowlist::FederationAllowList,
federation_blocklist::FederationBlockList,
local_site::{LocalSite, LocalSiteUpdateForm},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
local_site_url_blocklist::LocalSiteUrlBlocklist,
@ -152,12 +150,6 @@ pub async fn update_site(
.await
.ok();
// Replace the blocked and allowed instances
let allowed = data.allowed_instances.clone();
FederationAllowList::replace(&mut context.pool(), allowed).await?;
let blocked = data.blocked_instances.clone();
FederationBlockList::replace(&mut context.pool(), blocked).await?;
if let Some(url_blocklist) = data.blocked_urls.clone() {
let parsed_urls = check_urls_are_valid(&url_blocklist)?;
LocalSiteUrlBlocklist::replace(&mut context.pool(), parsed_urls).await?;

View file

@ -21,8 +21,9 @@ use lemmy_api_common::{
};
use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
newtypes::{InstanceId, OAuthProviderId},
newtypes::{InstanceId, OAuthProviderId, SiteId},
source::{
actor_language::SiteLanguage,
captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
language::Language,
local_site::LocalSite,
@ -44,9 +45,10 @@ use lemmy_utils::{
validation::is_valid_actor_name,
},
};
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::collections::HashSet;
use std::{collections::HashSet, sync::LazyLock};
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -145,7 +147,13 @@ pub async fn register(
..LocalUserInsertForm::new(inserted_person.id, Some(data.password.to_string()))
};
let inserted_local_user = create_local_user(&context, language_tags, &local_user_form).await?;
let inserted_local_user = create_local_user(
&context,
language_tags,
&local_user_form,
local_site.site_id,
)
.await?;
if local_site.site_setup && require_registration_application {
if let Some(answer) = data.answer.clone() {
@ -218,6 +226,11 @@ pub async fn authenticate_with_oauth(
Err(LemmyErrorType::OauthAuthorizationInvalid)?
}
// validate the PKCE challenge
if let Some(code_verifier) = &data.pkce_code_verifier {
check_code_verifier(code_verifier)?;
}
// Fetch the OAUTH provider and make sure it's enabled
let oauth_provider_id = data.oauth_provider_id;
let oauth_provider = OAuthProvider::read(&mut context.pool(), oauth_provider_id)
@ -229,9 +242,14 @@ pub async fn authenticate_with_oauth(
return Err(LemmyErrorType::OauthAuthorizationInvalid)?;
}
let token_response =
oauth_request_access_token(&context, &oauth_provider, &data.code, redirect_uri.as_str())
.await?;
let token_response = oauth_request_access_token(
&context,
&oauth_provider,
&data.code,
data.pkce_code_verifier.as_deref(),
redirect_uri.as_str(),
)
.await?;
let user_info = oidc_get_user_info(
&context,
@ -358,7 +376,13 @@ pub async fn authenticate_with_oauth(
..LocalUserInsertForm::new(person.id, None)
};
local_user = create_local_user(&context, language_tags, &local_user_form).await?;
local_user = create_local_user(
&context,
language_tags,
&local_user_form,
local_site.site_id,
)
.await?;
// Create the oauth account
let oauth_account_form =
@ -449,15 +473,23 @@ async fn create_local_user(
context: &Data<LemmyContext>,
language_tags: Vec<String>,
local_user_form: &LocalUserInsertForm,
local_site_id: SiteId,
) -> Result<LocalUser, LemmyError> {
let all_languages = Language::read_all(&mut context.pool()).await?;
// use hashset to avoid duplicates
let mut language_ids = HashSet::new();
// Enable languages from `Accept-Language` header
for l in language_tags {
if let Some(found) = all_languages.iter().find(|all| all.code == l) {
language_ids.insert(found.id);
}
}
// Enable site languages. Ignored if all languages are enabled.
let discussion_languages = SiteLanguage::read(&mut context.pool(), local_site_id).await?;
language_ids.extend(discussion_languages);
let language_ids = language_ids.into_iter().collect();
let inserted_local_user =
@ -512,20 +544,27 @@ async fn oauth_request_access_token(
context: &Data<LemmyContext>,
oauth_provider: &OAuthProvider,
code: &str,
pkce_code_verifier: Option<&str>,
redirect_uri: &str,
) -> LemmyResult<TokenResponse> {
let mut form = vec![
("client_id", &*oauth_provider.client_id),
("client_secret", &*oauth_provider.client_secret),
("code", code),
("grant_type", "authorization_code"),
("redirect_uri", redirect_uri),
];
if let Some(code_verifier) = pkce_code_verifier {
form.push(("code_verifier", code_verifier));
}
// Request an Access Token from the OAUTH provider
let response = context
.client()
.post(oauth_provider.token_endpoint.as_str())
.header("Accept", "application/json")
.form(&[
("grant_type", "authorization_code"),
("code", code),
("redirect_uri", redirect_uri),
("client_id", &oauth_provider.client_id),
("client_secret", &oauth_provider.client_secret),
])
.form(&form[..])
.send()
.await
.with_lemmy_type(LemmyErrorType::OauthLoginFailed)?
@ -575,3 +614,17 @@ fn read_user_info(user_info: &serde_json::Value, key: &str) -> LemmyResult<Strin
}
Err(LemmyErrorType::OauthLoginFailed)?
}
#[allow(clippy::expect_used)]
fn check_code_verifier(code_verifier: &str) -> LemmyResult<()> {
static VALID_CODE_VERIFIER_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9\-._~]{43,128}$").expect("compile regex"));
let check = VALID_CODE_VERIFIER_REGEX.is_match(code_verifier);
if check {
Ok(())
} else {
Err(LemmyErrorType::InvalidCodeVerifier.into())
}
}

View file

@ -1,2 +1,3 @@
pub mod create;
pub mod delete;
pub mod my_user;

View file

@ -0,0 +1,45 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::{context::LemmyContext, site::MyUserInfo, utils::check_user_valid};
use lemmy_db_schema::source::{
actor_language::LocalUserLanguage,
community_block::CommunityBlock,
instance_block::InstanceBlock,
person_block::PersonBlock,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView};
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
#[tracing::instrument(skip(context))]
pub async fn get_my_user(
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<MyUserInfo>> {
check_user_valid(&local_user_view.person)?;
// Build the local user with parallel queries and add it to site response
let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id;
let pool = &mut context.pool();
let (follows, community_blocks, instance_blocks, person_blocks, moderates, discussion_languages) =
lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Ok(Json(MyUserInfo {
local_user_view: local_user_view.clone(),
follows,
moderates,
community_blocks,
instance_blocks,
person_blocks,
discussion_languages,
}))
}

View file

@ -0,0 +1,15 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://queer.hacktivis.me/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"attributedTo": "https://queer.hacktivis.me/users/lanodan",
"content": "Hi!",
"id": "https://queer.hacktivis.me/objects/2",
"published": "2020-02-12T14:08:20Z",
"to": ["https://enterprise.lemmy.ml/u/picard"],
"type": "ChatMessage"
}

View file

@ -36,7 +36,7 @@ use lemmy_db_schema::{
CommunityPersonBan,
CommunityPersonBanForm,
},
moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
mod_log::moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
person::{Person, PersonUpdateForm},
},
traits::{Bannable, Crud, Followable},

View file

@ -27,7 +27,7 @@ use lemmy_db_schema::{
source::{
activity::ActivitySendTargets,
community::{CommunityPersonBan, CommunityPersonBanForm},
moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
mod_log::moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
person::{Person, PersonUpdateForm},
},
traits::{Bannable, Crud},

View file

@ -31,7 +31,7 @@ use lemmy_db_schema::{
source::{
activity::ActivitySendTargets,
community::{Community, CommunityModerator, CommunityModeratorForm},
moderator::{ModAddCommunity, ModAddCommunityForm},
mod_log::moderator::{ModAddCommunity, ModAddCommunityForm},
person::Person,
post::{Post, PostUpdateForm},
},

View file

@ -27,7 +27,7 @@ use lemmy_db_schema::{
source::{
activity::ActivitySendTargets,
community::{Community, CommunityModerator, CommunityModeratorForm},
moderator::{ModAddCommunity, ModAddCommunityForm},
mod_log::moderator::{ModAddCommunity, ModAddCommunityForm},
post::{Post, PostUpdateForm},
},
traits::{Crud, Joinable},

View file

@ -27,7 +27,7 @@ use lemmy_db_schema::{
source::{
activity::ActivitySendTargets,
community::Community,
moderator::{ModLockPost, ModLockPostForm},
mod_log::moderator::{ModLockPost, ModLockPostForm},
person::Person,
post::{Post, PostUpdateForm},
},

View file

@ -99,8 +99,11 @@ impl CreateOrUpdateNote {
inboxes.add_inbox(person.shared_inbox_or_inbox());
}
let activity =
AnnouncableActivities::CreateOrUpdateNoteWrapper(from_value(to_value(create_or_update)?)?);
// AnnouncableActivities doesnt contain Comment activity but only NoteWrapper,
// to be able to handle both comment and private message. So to send this out we need
// to convert this to NoteWrapper, by serializing and then deserializing again.
let converted = from_value(to_value(create_or_update)?)?;
let activity = AnnouncableActivities::CreateOrUpdateNoteWrapper(converted);
send_activity_in_community(activity, &person, &community, inboxes, false, &context).await
}
}

View file

@ -1,5 +1,5 @@
use crate::{
objects::{community::ApubCommunity, note_wrapper::is_public},
objects::community::ApubCommunity,
protocol::{
activities::create_or_update::{
note::CreateOrUpdateNote,
@ -11,10 +11,13 @@ use crate::{
};
use activitypub_federation::{config::Data, traits::ActivityHandler};
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::{FederationError, LemmyError, LemmyResult};
use lemmy_utils::error::{LemmyError, LemmyResult};
use serde_json::{from_value, to_value};
use url::Url;
/// In Activitypub, both private messages and comments are represented by `type: Note` which
/// makes it difficult to distinguish them. This wrapper handles receiving of both types, and
/// routes them to the correct handler.
#[async_trait::async_trait]
impl ActivityHandler for CreateOrUpdateNoteWrapper {
type DataType = LemmyContext;
@ -29,38 +32,43 @@ impl ActivityHandler for CreateOrUpdateNoteWrapper {
}
#[tracing::instrument(skip_all)]
async fn verify(&self, context: &Data<Self::DataType>) -> LemmyResult<()> {
let val = to_value(self)?;
if is_public(&self.to, &self.cc) {
CreateOrUpdateNote::verify(&from_value(val)?, context).await?;
} else {
CreateOrUpdatePrivateMessage::verify(&from_value(val)?, context).await?;
}
async fn verify(&self, _context: &Data<Self::DataType>) -> LemmyResult<()> {
// Do everything in receive to avoid extra checks.
Ok(())
}
#[tracing::instrument(skip_all)]
async fn receive(self, context: &Data<Self::DataType>) -> LemmyResult<()> {
let is_public = is_public(&self.to, &self.cc);
// Use serde to convert NoteWrapper either into Comment or PrivateMessage,
// depending on conditions below. This works because NoteWrapper keeps all
// additional data in field `other: Map<String, Value>`.
let val = to_value(self)?;
if is_public {
CreateOrUpdateNote::receive(from_value(val)?, context).await?;
} else {
CreateOrUpdatePrivateMessage::receive(from_value(val)?, context).await?;
// Convert self to a comment and get the community. If the conversion is
// successful and a community is returned, this is a comment.
let comment = from_value::<CreateOrUpdateNote>(val.clone());
if let Ok(comment) = comment {
if comment.community(context).await.is_ok() {
CreateOrUpdateNote::verify(&comment, context).await?;
CreateOrUpdateNote::receive(comment, context).await?;
return Ok(());
}
}
// If any of the previous checks failed, we are dealing with a private message.
let private_message = from_value(val)?;
CreateOrUpdatePrivateMessage::verify(&private_message, context).await?;
CreateOrUpdatePrivateMessage::receive(private_message, context).await?;
Ok(())
}
}
#[async_trait::async_trait]
impl InCommunity for CreateOrUpdateNoteWrapper {
#[tracing::instrument(skip(self, context))]
async fn community(&self, context: &Data<LemmyContext>) -> LemmyResult<ApubCommunity> {
if is_public(&self.to, &self.cc) {
let comment: CreateOrUpdateNote = from_value(to_value(self)?)?;
comment.community(context).await
} else {
Err(FederationError::ObjectIsNotPublic.into())
}
// Same logic as in receive. In case this is a private message, an error is returned.
let val = to_value(self)?;
let comment: CreateOrUpdateNote = from_value(val.clone())?;
comment.community(context).await
}
}

View file

@ -14,7 +14,7 @@ use lemmy_db_schema::{
comment::{Comment, CommentUpdateForm},
comment_report::CommentReport,
community::{Community, CommunityUpdateForm},
moderator::{
mod_log::moderator::{
ModRemoveComment,
ModRemoveCommentForm,
ModRemoveCommunity,

View file

@ -13,7 +13,7 @@ use lemmy_db_schema::{
source::{
comment::{Comment, CommentUpdateForm},
community::{Community, CommunityUpdateForm},
moderator::{
mod_log::moderator::{
ModRemoveComment,
ModRemoveCommentForm,
ModRemoveCommunity,

View file

@ -4,7 +4,7 @@ use actix_web::web::{Json, Query};
use lemmy_api_common::{
context::LemmyContext,
person::{GetPersonDetails, GetPersonDetailsResponse},
utils::{check_private_instance, read_site_for_actor},
utils::{check_private_instance, is_admin, read_site_for_actor},
};
use lemmy_db_schema::{source::person::Person, utils::post_to_comment_sort_type};
use lemmy_db_views::{
@ -45,7 +45,11 @@ pub async fn read_person(
// You don't need to return settings for the user, since this comes back with GetSite
// `my_user`
let person_view = PersonView::read(&mut context.pool(), person_details_id).await?;
let is_admin = local_user_view
.as_ref()
.map(|l| is_admin(l).is_ok())
.unwrap_or_default();
let person_view = PersonView::read(&mut context.pool(), person_details_id, is_admin).await?;
let sort = data.sort;
let page = data.page;

View file

@ -60,7 +60,7 @@ async fn convert_response(
}
},
SearchableObjects::PersonOrCommunity(pc) => match *pc {
UserOrCommunity::User(u) => res.person = Some(PersonView::read(pool, u.id).await?),
UserOrCommunity::User(u) => res.person = Some(PersonView::read(pool, u.id, is_admin).await?),
UserOrCommunity::Community(c) => {
res.community = Some(CommunityView::read(pool, c.id, local_user.as_ref(), is_admin).await?)
}

View file

@ -322,7 +322,7 @@ pub(crate) mod tests {
CommunityFollowerState,
CommunityInsertForm,
},
local_user::LocalUser,
person::Person,
},
traits::{Crud, Followable},
};
@ -376,8 +376,8 @@ pub(crate) mod tests {
assert_eq!(follows.len(), 1);
assert_eq!(follows[0].community.actor_id, community.actor_id);
LocalUser::delete(pool, export_user.local_user.id).await?;
LocalUser::delete(pool, import_user.local_user.id).await?;
Person::delete(pool, export_user.person.id).await?;
Person::delete(pool, import_user.person.id).await?;
Ok(())
}
@ -412,8 +412,8 @@ pub(crate) mod tests {
Some(LemmyErrorType::TooManyItems)
);
LocalUser::delete(pool, export_user.local_user.id).await?;
LocalUser::delete(pool, import_user.local_user.id).await?;
Person::delete(pool, export_user.person.id).await?;
Person::delete(pool, import_user.person.id).await?;
Ok(())
}

View file

@ -42,7 +42,8 @@ pub async fn markdown_rewrite_remote_links(
let mut local_url = local_url.to_string();
// restore title
if let Some(extra) = extra {
local_url = format!("{local_url} {extra}");
local_url.push(' ');
local_url.push_str(extra);
}
src.replace_range(start..end, local_url.as_str());
}

View file

@ -15,7 +15,6 @@ use std::fmt::Debug;
pub mod comment;
pub mod community;
pub mod instance;
pub mod note_wrapper;
pub mod person;
pub mod post;
pub mod private_message;

View file

@ -1,85 +0,0 @@
use super::comment::ApubComment;
use crate::{
objects::private_message::ApubPrivateMessage,
protocol::objects::note_wrapper::NoteWrapper,
};
use activitypub_federation::{config::Data, kinds::public, traits::Object};
use chrono::{DateTime, Utc};
use lemmy_api_common::{context::LemmyContext, LemmyErrorType};
use lemmy_utils::error::{LemmyError, LemmyResult};
use serde_json::{from_value, to_value};
use url::Url;
/// Private messages and public comments are quite awkward in Activitypub, because the json
/// format looks identical. They only way to differentiate them is to check for the presence
/// or absence of `https://www.w3.org/ns/activitystreams#Public` in `to` or `cc` which this
/// wrapper does.
#[derive(Debug)]
pub(crate) struct ApubNote {}
#[async_trait::async_trait]
impl Object for ApubNote {
type DataType = LemmyContext;
type Kind = NoteWrapper;
type Error = LemmyError;
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
None
}
#[tracing::instrument(skip_all)]
async fn read_from_id(
_object_id: Url,
_context: &Data<Self::DataType>,
) -> LemmyResult<Option<Self>> {
Err(LemmyErrorType::Unknown("not implemented".to_string()).into())
}
#[tracing::instrument(skip_all)]
async fn delete(self, _context: &Data<Self::DataType>) -> LemmyResult<()> {
Err(LemmyErrorType::Unknown("not implemented".to_string()).into())
}
async fn verify(
note: &NoteWrapper,
expected_domain: &Url,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let val = to_value(note)?;
if is_public(&note.to, &note.cc) {
ApubComment::verify(&from_value(val)?, expected_domain, context).await?;
} else {
ApubPrivateMessage::verify(&from_value(val)?, expected_domain, context).await?;
}
Ok(())
}
async fn from_json(note: NoteWrapper, context: &Data<LemmyContext>) -> LemmyResult<ApubNote> {
let is_public = is_public(&note.to, &note.cc);
let val = to_value(note)?;
if is_public {
ApubComment::from_json(from_value(val)?, context).await?;
} else {
ApubPrivateMessage::from_json(from_value(val)?, context).await?;
}
Ok(ApubNote {})
}
async fn into_json(self, _context: &Data<Self::DataType>) -> LemmyResult<NoteWrapper> {
Err(LemmyErrorType::Unknown("not implemented".to_string()).into())
}
}
pub(crate) fn is_public(to: &Option<Vec<Url>>, cc: &Option<Vec<Url>>) -> bool {
if let Some(to) = to {
if to.contains(&public()) {
return true;
}
}
if let Some(cc) = cc {
if cc.contains(&public()) {
return true;
}
}
false
}

View file

@ -242,4 +242,24 @@ mod tests {
cleanup(data, &context).await?;
Ok(())
}
#[tokio::test]
#[serial]
async fn test_parse_pleroma_pm() -> LemmyResult<()> {
let context = LemmyContext::init_test_context().await;
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621")?;
let data = prepare_comment_test(&url, &context).await?;
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2")?;
let json = file_to_json_object("assets/pleroma/objects/chat_message.json")?;
ApubPrivateMessage::verify(&json, &pleroma_url, &context).await?;
let pm = ApubPrivateMessage::from_json(json, &context).await?;
assert_eq!(pm.ap_id, pleroma_url.into());
assert_eq!(pm.content.len(), 3);
assert_eq!(context.request_count(), 0);
DbPrivateMessage::delete(&mut context.pool(), pm.id).await?;
cleanup(data, &context).await?;
Ok(())
}
}

View file

@ -1,4 +1,4 @@
use crate::protocol::objects::note_wrapper::NoteWrapper;
use activitypub_federation::kinds::object::NoteType;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use url::Url;
@ -6,11 +6,21 @@ use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdateNoteWrapper {
object: NoteWrapper,
pub(crate) object: NoteWrapper,
pub(crate) id: Url,
#[serde(default)]
pub(crate) to: Vec<Url>,
#[serde(default)]
pub(crate) cc: Vec<Url>,
pub(crate) actor: Url,
pub(crate) to: Option<Vec<Url>>,
pub(crate) cc: Option<Vec<Url>>,
#[serde(flatten)]
other: Map<String, Value>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct NoteWrapper {
pub(crate) r#type: NoteType,
#[serde(flatten)]
other: Map<String, Value>,
}

View file

@ -116,7 +116,6 @@ pub(crate) mod tests {
// parse file into hashmap, which ensures that every field is included
let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
// assert that all fields are identical, otherwise print diff
//dbg!(&parsed, &raw);
assert_json_include!(actual: &parsed, expected: raw);
Ok(parsed)
}

View file

@ -11,7 +11,6 @@ use url::Url;
pub(crate) mod group;
pub(crate) mod instance;
pub(crate) mod note;
pub(crate) mod note_wrapper;
pub(crate) mod page;
pub(crate) mod person;
pub(crate) mod private_message;
@ -102,8 +101,8 @@ impl LanguageTag {
#[cfg(test)]
mod tests {
use super::note_wrapper::NoteWrapper;
use crate::protocol::{
activities::create_or_update::note_wrapper::NoteWrapper,
objects::{
group::Group,
instance::Instance,

View file

@ -1,14 +0,0 @@
use activitypub_federation::kinds::object::NoteType;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct NoteWrapper {
pub(crate) r#type: NoteType,
pub(crate) to: Option<Vec<Url>>,
pub(crate) cc: Option<Vec<Url>>,
#[serde(flatten)]
other: Map<String, Value>,
}

View file

@ -34,7 +34,7 @@ pub struct PrivateMessage {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PrivateMessageType {
/// For compatibility with Lemmy 0.19 and earlier
/// Deprecated, for compatibility with Lemmy 0.19 and earlier
/// https://docs.pleroma.social/backend/development/ap_extensions/#chatmessages
ChatMessage,
Note,

View file

@ -1,60 +1,51 @@
use crate::{
schema::federation_allowlist,
newtypes::InstanceId,
schema::{admin_allow_instance, federation_allowlist},
source::{
federation_allowlist::{FederationAllowList, FederationAllowListForm},
instance::Instance,
mod_log::admin::{AdminAllowInstance, AdminAllowInstanceForm},
},
utils::{get_conn, DbPool},
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel::{delete, dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
impl FederationAllowList {
pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option<Vec<String>>) -> Result<(), Error> {
impl AdminAllowInstance {
pub async fn insert(pool: &mut DbPool<'_>, form: &AdminAllowInstanceForm) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
if let Some(list) = list_opt {
Self::clear(conn).await?;
for domain in list {
// Upsert all of these as instances
let instance = Instance::read_or_create(&mut conn.into(), domain).await?;
let form = FederationAllowListForm {
instance_id: instance.id,
updated: None,
};
insert_into(federation_allowlist::table)
.values(form)
.get_result::<Self>(conn)
.await?;
}
Ok(())
} else {
Ok(())
}
}) as _
})
.await
}
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
diesel::delete(federation_allowlist::table)
insert_into(admin_allow_instance::table)
.values(form)
.execute(conn)
.await
.await?;
Ok(())
}
}
impl FederationAllowList {
pub async fn allow(pool: &mut DbPool<'_>, form: &FederationAllowListForm) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
insert_into(federation_allowlist::table)
.values(form)
.execute(conn)
.await?;
Ok(())
}
pub async fn unallow(pool: &mut DbPool<'_>, instance_id_: InstanceId) -> Result<(), Error> {
use federation_allowlist::dsl::instance_id;
let conn = &mut get_conn(pool).await?;
delete(federation_allowlist::table.filter(instance_id.eq(instance_id_)))
.execute(conn)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{
source::{federation_allowlist::FederationAllowList, instance::Instance},
utils::build_db_pool_for_tests,
};
use diesel::result::Error;
use super::*;
use crate::{source::instance::Instance, utils::build_db_pool_for_tests};
use pretty_assertions::assert_eq;
use serial_test::serial;
@ -63,31 +54,33 @@ mod tests {
async fn test_allowlist_insert_and_clear() -> Result<(), Error> {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();
let domains = vec![
"tld1.xyz".to_string(),
"tld2.xyz".to_string(),
"tld3.xyz".to_string(),
let instances = vec![
Instance::read_or_create(pool, "tld1.xyz".to_string()).await?,
Instance::read_or_create(pool, "tld2.xyz".to_string()).await?,
Instance::read_or_create(pool, "tld3.xyz".to_string()).await?,
];
let forms: Vec<_> = instances
.iter()
.map(|i| FederationAllowListForm {
instance_id: i.id,
updated: None,
})
.collect();
let allowed = Some(domains.clone());
FederationAllowList::replace(pool, allowed).await?;
for f in &forms {
FederationAllowList::allow(pool, f).await?;
}
let allows = Instance::allowlist(pool).await?;
let allows_domains = allows
.iter()
.map(|i| i.domain.clone())
.collect::<Vec<String>>();
assert_eq!(3, allows.len());
assert_eq!(domains, allows_domains);
assert_eq!(instances, allows);
// Now test clearing them via Some(empty vec)
let clear_allows = Some(Vec::new());
FederationAllowList::replace(pool, clear_allows).await?;
// Now test clearing them
for f in forms {
FederationAllowList::unallow(pool, f.instance_id).await?;
}
let allows = Instance::allowlist(pool).await?;
assert_eq!(0, allows.len());
Instance::delete_all(pool).await?;

View file

@ -1,49 +1,42 @@
use crate::{
schema::federation_blocklist,
newtypes::InstanceId,
schema::{admin_block_instance, federation_blocklist},
source::{
federation_blocklist::{FederationBlockList, FederationBlockListForm},
instance::Instance,
mod_log::admin::{AdminBlockInstance, AdminBlockInstanceForm},
},
utils::{get_conn, DbPool},
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel::{delete, dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
impl FederationBlockList {
pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option<Vec<String>>) -> Result<(), Error> {
impl AdminBlockInstance {
pub async fn insert(pool: &mut DbPool<'_>, form: &AdminBlockInstanceForm) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
if let Some(list) = list_opt {
Self::clear(conn).await?;
for domain in list {
// Upsert all of these as instances
let instance = Instance::read_or_create(&mut conn.into(), domain).await?;
let form = FederationBlockListForm {
instance_id: instance.id,
updated: None,
};
insert_into(federation_blocklist::table)
.values(form)
.get_result::<Self>(conn)
.await?;
}
Ok(())
} else {
Ok(())
}
}) as _
})
.await
}
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
diesel::delete(federation_blocklist::table)
insert_into(admin_block_instance::table)
.values(form)
.execute(conn)
.await
.await?;
Ok(())
}
}
impl FederationBlockList {
pub async fn block(pool: &mut DbPool<'_>, form: &FederationBlockListForm) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
insert_into(federation_blocklist::table)
.values(form)
.execute(conn)
.await?;
Ok(())
}
pub async fn unblock(pool: &mut DbPool<'_>, instance_id_: InstanceId) -> Result<(), Error> {
use federation_blocklist::dsl::instance_id;
let conn = &mut get_conn(pool).await?;
delete(federation_blocklist::table.filter(instance_id.eq(instance_id_)))
.execute(conn)
.await?;
Ok(())
}
}

View file

@ -21,7 +21,7 @@ pub mod local_site_url_blocklist;
pub mod local_user;
pub mod local_user_vote_display_mode;
pub mod login_token;
pub mod moderator;
pub mod mod_log;
pub mod oauth_account;
pub mod oauth_provider;
pub mod password_reset_request;
@ -35,4 +35,5 @@ pub mod private_message_report;
pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tag;
pub mod tagline;

View file

@ -0,0 +1,132 @@
use crate::{
source::mod_log::admin::{
AdminPurgeComment,
AdminPurgeCommentForm,
AdminPurgeCommunity,
AdminPurgeCommunityForm,
AdminPurgePerson,
AdminPurgePersonForm,
AdminPurgePost,
AdminPurgePostForm,
},
traits::Crud,
utils::{get_conn, DbPool},
};
use diesel::{dsl::insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;
#[async_trait]
impl Crud for AdminPurgePerson {
type InsertForm = AdminPurgePersonForm;
type UpdateForm = AdminPurgePersonForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_person)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_person.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgeCommunity {
type InsertForm = AdminPurgeCommunityForm;
type UpdateForm = AdminPurgeCommunityForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_community)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_community.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgePost {
type InsertForm = AdminPurgePostForm;
type UpdateForm = AdminPurgePostForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_post)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_post.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgeComment {
type InsertForm = AdminPurgeCommentForm;
type UpdateForm = AdminPurgeCommentForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_comment)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_comment.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}

View file

@ -0,0 +1,2 @@
pub mod admin;
pub mod moderator;

View file

@ -1,13 +1,5 @@
use crate::{
source::moderator::{
AdminPurgeComment,
AdminPurgeCommentForm,
AdminPurgeCommunity,
AdminPurgeCommunityForm,
AdminPurgePerson,
AdminPurgePersonForm,
AdminPurgePost,
AdminPurgePostForm,
source::mod_log::moderator::{
ModAdd,
ModAddCommunity,
ModAddCommunityForm,
@ -376,157 +368,20 @@ impl Crud for ModAdd {
}
}
#[async_trait]
impl Crud for AdminPurgePerson {
type InsertForm = AdminPurgePersonForm;
type UpdateForm = AdminPurgePersonForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_person)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_person.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgeCommunity {
type InsertForm = AdminPurgeCommunityForm;
type UpdateForm = AdminPurgeCommunityForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_community)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_community.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgePost {
type InsertForm = AdminPurgePostForm;
type UpdateForm = AdminPurgePostForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_post)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_post.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[async_trait]
impl Crud for AdminPurgeComment {
type InsertForm = AdminPurgeCommentForm;
type UpdateForm = AdminPurgeCommentForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
let conn = &mut get_conn(pool).await?;
insert_into(admin_purge_comment)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
let conn = &mut get_conn(pool).await?;
diesel::update(admin_purge_comment.find(from_id))
.set(form)
.get_result::<Self>(conn)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityInsertForm},
instance::Instance,
moderator::{
ModAdd,
ModAddCommunity,
ModAddCommunityForm,
ModAddForm,
ModBan,
ModBanForm,
ModBanFromCommunity,
ModBanFromCommunityForm,
ModFeaturePost,
ModFeaturePostForm,
ModLockPost,
ModLockPostForm,
ModRemoveComment,
ModRemoveCommentForm,
ModRemoveCommunity,
ModRemoveCommunityForm,
ModRemovePost,
ModRemovePostForm,
},
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm},
},
traits::Crud,
utils::build_db_pool_for_tests,
};
use diesel::result::Error;
use pretty_assertions::assert_eq;
use serial_test::serial;

View file

@ -0,0 +1,53 @@
use crate::{
newtypes::TagId,
schema::{post_tag, tag},
source::tag::{PostTagInsertForm, Tag, TagInsertForm},
traits::Crud,
utils::{get_conn, DbPool},
};
use diesel::{insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_utils::error::LemmyResult;
#[async_trait]
impl Crud for Tag {
type InsertForm = TagInsertForm;
type UpdateForm = TagInsertForm;
type IdType = TagId;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(tag::table)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
pid: TagId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(tag::table.find(pid))
.set(form)
.get_result::<Self>(conn)
.await
}
}
impl PostTagInsertForm {
pub async fn insert_tag_associations(
pool: &mut DbPool<'_>,
tags: &[PostTagInsertForm],
) -> LemmyResult<()> {
let conn = &mut get_conn(pool).await?;
insert_into(post_tag::table)
.values(tags)
.execute(conn)
.await?;
Ok(())
}
}

View file

@ -210,6 +210,8 @@ pub enum ModlogActionType {
AdminPurgeCommunity,
AdminPurgePost,
AdminPurgeComment,
AdminBlockInstance,
AdminAllowInstance,
}
#[derive(

View file

@ -283,3 +283,9 @@ impl InstanceId {
self.0
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The internal tag id.
pub struct TagId(pub i32);

View file

@ -42,6 +42,29 @@ pub mod sql_types {
pub struct RegistrationModeEnum;
}
diesel::table! {
admin_allow_instance (id) {
id -> Int4,
instance_id -> Int4,
admin_person_id -> Int4,
allowed -> Bool,
reason -> Nullable<Text>,
when_ -> Timestamptz,
}
}
diesel::table! {
admin_block_instance (id) {
id -> Int4,
instance_id -> Int4,
admin_person_id -> Int4,
blocked -> Bool,
reason -> Nullable<Text>,
expires -> Nullable<Timestamptz>,
when_ -> Timestamptz,
}
}
diesel::table! {
admin_purge_comment (id) {
id -> Int4,
@ -284,6 +307,7 @@ diesel::table! {
instance_id -> Int4,
published -> Timestamptz,
updated -> Nullable<Timestamptz>,
expires -> Nullable<Timestamptz>,
}
}
@ -636,6 +660,7 @@ diesel::table! {
enabled -> Bool,
published -> Timestamptz,
updated -> Nullable<Timestamptz>,
use_pkce -> Bool,
}
}
@ -801,6 +826,14 @@ diesel::table! {
}
}
diesel::table! {
post_tag (post_id, tag_id) {
post_id -> Int4,
tag_id -> Int4,
published -> Timestamptz,
}
}
diesel::table! {
previously_run_sql (id) {
id -> Bool,
@ -933,6 +966,18 @@ diesel::table! {
}
}
diesel::table! {
tag (id) {
id -> Int4,
ap_id -> Text,
name -> Text,
community_id -> Int4,
published -> Timestamptz,
updated -> Nullable<Timestamptz>,
deleted -> Bool,
}
}
diesel::table! {
tagline (id) {
id -> Int4,
@ -942,6 +987,10 @@ diesel::table! {
}
}
diesel::joinable!(admin_allow_instance -> instance (instance_id));
diesel::joinable!(admin_allow_instance -> person (admin_person_id));
diesel::joinable!(admin_block_instance -> instance (instance_id));
diesel::joinable!(admin_block_instance -> person (admin_person_id));
diesel::joinable!(admin_purge_comment -> person (admin_person_id));
diesel::joinable!(admin_purge_comment -> post (post_id));
diesel::joinable!(admin_purge_community -> person (admin_person_id));
@ -1010,6 +1059,8 @@ diesel::joinable!(post_aggregates -> instance (instance_id));
diesel::joinable!(post_aggregates -> person (creator_id));
diesel::joinable!(post_aggregates -> post (post_id));
diesel::joinable!(post_report -> post (post_id));
diesel::joinable!(post_tag -> post (post_id));
diesel::joinable!(post_tag -> tag (tag_id));
diesel::joinable!(private_message_report -> private_message (private_message_id));
diesel::joinable!(registration_application -> local_user (local_user_id));
diesel::joinable!(registration_application -> person (admin_id));
@ -1017,8 +1068,11 @@ diesel::joinable!(site -> instance (instance_id));
diesel::joinable!(site_aggregates -> site (site_id));
diesel::joinable!(site_language -> language (language_id));
diesel::joinable!(site_language -> site (site_id));
diesel::joinable!(tag -> community (community_id));
diesel::allow_tables_to_appear_in_same_query!(
admin_allow_instance,
admin_block_instance,
admin_purge_comment,
admin_purge_community,
admin_purge_person,
@ -1074,6 +1128,7 @@ diesel::allow_tables_to_appear_in_same_query!(
post_actions,
post_aggregates,
post_report,
post_tag,
previously_run_sql,
private_message,
private_message_report,
@ -1085,5 +1140,6 @@ diesel::allow_tables_to_appear_in_same_query!(
site,
site_aggregates,
site_language,
tag,
tagline,
);

View file

@ -1,14 +1,14 @@
use crate::newtypes::InstanceId;
#[cfg(feature = "full")]
use crate::schema::federation_blocklist;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
#[cfg(feature = "full")]
use {crate::schema::federation_blocklist, ts_rs::TS};
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(Queryable, Selectable, Associations, Identifiable)
derive(TS, Queryable, Selectable, Associations, Identifiable)
)]
#[cfg_attr(
feature = "full",
@ -17,10 +17,14 @@ use std::fmt::Debug;
#[cfg_attr(feature = "full", diesel(table_name = federation_blocklist))]
#[cfg_attr(feature = "full", diesel(primary_key(instance_id)))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct FederationBlockList {
pub instance_id: InstanceId,
pub published: DateTime<Utc>,
#[cfg_attr(feature = "full", ts(optional))]
pub updated: Option<DateTime<Utc>>,
#[cfg_attr(feature = "full", ts(optional))]
pub expires: Option<DateTime<Utc>>,
}
#[derive(Clone, Default)]
@ -29,4 +33,5 @@ pub struct FederationBlockList {
pub struct FederationBlockListForm {
pub instance_id: InstanceId,
pub updated: Option<DateTime<Utc>>,
pub expires: Option<DateTime<Utc>>,
}

View file

@ -26,7 +26,7 @@ pub mod local_site_url_blocklist;
pub mod local_user;
pub mod local_user_vote_display_mode;
pub mod login_token;
pub mod moderator;
pub mod mod_log;
pub mod oauth_account;
pub mod oauth_provider;
pub mod password_reset_request;
@ -40,6 +40,7 @@ pub mod private_message_report;
pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tag;
pub mod tagline;
/// Default value for columns like [community::Community.inbox_url] which are marked as serde(skip).

View file

@ -0,0 +1,176 @@
use crate::newtypes::{CommunityId, InstanceId, PersonId, PostId};
#[cfg(feature = "full")]
use crate::schema::{
admin_allow_instance,
admin_block_instance,
admin_purge_comment,
admin_purge_community,
admin_purge_person,
admin_purge_post,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_person))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a person.
pub struct AdminPurgePerson {
pub id: i32,
pub admin_person_id: PersonId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_person))]
pub struct AdminPurgePersonForm {
pub admin_person_id: PersonId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_community))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a community.
pub struct AdminPurgeCommunity {
pub id: i32,
pub admin_person_id: PersonId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_community))]
pub struct AdminPurgeCommunityForm {
pub admin_person_id: PersonId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_post))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a post.
pub struct AdminPurgePost {
pub id: i32,
pub admin_person_id: PersonId,
pub community_id: CommunityId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_post))]
pub struct AdminPurgePostForm {
pub admin_person_id: PersonId,
pub community_id: CommunityId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_comment))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a comment.
pub struct AdminPurgeComment {
pub id: i32,
pub admin_person_id: PersonId,
pub post_id: PostId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_comment))]
pub struct AdminPurgeCommentForm {
pub admin_person_id: PersonId,
pub post_id: PostId,
pub reason: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(TS, Queryable, Selectable, Associations, Identifiable)
)]
#[cfg_attr(
feature = "full",
diesel(belongs_to(crate::source::instance::Instance))
)]
#[cfg_attr(feature = "full", diesel(table_name = admin_allow_instance))]
#[cfg_attr(feature = "full", diesel(primary_key(instance_id)))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct AdminAllowInstance {
pub id: i32,
pub instance_id: InstanceId,
pub admin_person_id: PersonId,
pub allowed: bool,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[derive(Clone, Default)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_allow_instance))]
pub struct AdminAllowInstanceForm {
pub instance_id: InstanceId,
pub admin_person_id: PersonId,
pub allowed: bool,
pub reason: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(TS, Queryable, Selectable, Associations, Identifiable)
)]
#[cfg_attr(
feature = "full",
diesel(belongs_to(crate::source::instance::Instance))
)]
#[cfg_attr(feature = "full", diesel(table_name = admin_block_instance))]
#[cfg_attr(feature = "full", diesel(primary_key(instance_id)))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct AdminBlockInstance {
pub id: i32,
pub instance_id: InstanceId,
pub admin_person_id: PersonId,
pub blocked: bool,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
#[cfg_attr(feature = "full", ts(optional))]
pub expires: Option<DateTime<Utc>>,
pub when_: DateTime<Utc>,
}
#[derive(Clone, Default)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_block_instance))]
pub struct AdminBlockInstanceForm {
pub instance_id: InstanceId,
pub admin_person_id: PersonId,
pub blocked: bool,
pub reason: Option<String>,
pub when_: Option<DateTime<Utc>>,
}

View file

@ -0,0 +1,2 @@
pub mod admin;
pub mod moderator;

View file

@ -1,10 +1,6 @@
use crate::newtypes::{CommentId, CommunityId, PersonId, PostId};
#[cfg(feature = "full")]
use crate::schema::{
admin_purge_comment,
admin_purge_community,
admin_purge_person,
admin_purge_post,
mod_add,
mod_add_community,
mod_ban,
@ -300,95 +296,3 @@ pub struct ModAddForm {
pub other_person_id: PersonId,
pub removed: Option<bool>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_person))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a person.
pub struct AdminPurgePerson {
pub id: i32,
pub admin_person_id: PersonId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_person))]
pub struct AdminPurgePersonForm {
pub admin_person_id: PersonId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_community))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a community.
pub struct AdminPurgeCommunity {
pub id: i32,
pub admin_person_id: PersonId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_community))]
pub struct AdminPurgeCommunityForm {
pub admin_person_id: PersonId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_post))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a post.
pub struct AdminPurgePost {
pub id: i32,
pub admin_person_id: PersonId,
pub community_id: CommunityId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_post))]
pub struct AdminPurgePostForm {
pub admin_person_id: PersonId,
pub community_id: CommunityId,
pub reason: Option<String>,
}
#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_comment))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// When an admin purges a comment.
pub struct AdminPurgeComment {
pub id: i32,
pub admin_person_id: PersonId,
pub post_id: PostId,
#[cfg_attr(feature = "full", ts(optional))]
pub reason: Option<String>,
pub when_: DateTime<Utc>,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = admin_purge_comment))]
pub struct AdminPurgeCommentForm {
pub admin_person_id: PersonId,
pub post_id: PostId,
pub reason: Option<String>,
}

View file

@ -62,6 +62,8 @@ pub struct OAuthProvider {
pub published: DateTime<Utc>,
#[cfg_attr(feature = "full", ts(optional))]
pub updated: Option<DateTime<Utc>>,
/// switch to enable or disable PKCE
pub use_pkce: bool,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize)]
@ -83,6 +85,7 @@ impl Serialize for PublicOAuthProvider {
state.serialize_field("authorization_endpoint", &self.0.authorization_endpoint)?;
state.serialize_field("client_id", &self.0.client_id)?;
state.serialize_field("scopes", &self.0.scopes)?;
state.serialize_field("use_pkce", &self.0.use_pkce)?;
state.end()
}
}
@ -102,6 +105,7 @@ pub struct OAuthProviderInsertForm {
pub scopes: String,
pub auto_verify_email: Option<bool>,
pub account_linking_enabled: Option<bool>,
pub use_pkce: Option<bool>,
pub enabled: Option<bool>,
}
@ -118,6 +122,7 @@ pub struct OAuthProviderUpdateForm {
pub scopes: Option<String>,
pub auto_verify_email: Option<bool>,
pub account_linking_enabled: Option<bool>,
pub use_pkce: Option<bool>,
pub enabled: Option<bool>,
pub updated: Option<Option<DateTime<Utc>>>,
}

View file

@ -0,0 +1,57 @@
use crate::newtypes::{CommunityId, DbUrl, PostId, TagId};
#[cfg(feature = "full")]
use crate::schema::{post_tag, tag};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;
/// A tag that can be assigned to a post within a community.
/// The tag object is created by the community moderators.
/// The assignment happens by the post creator and can be updated by the community moderators.
///
/// A tag is a federatable object that gives additional context to another object, which can be
/// displayed and filtered on currently, we only have community post tags, which is a tag that is
/// created by post authors as well as mods of a community, to categorize a post. in the future we
/// may add more tag types, depending on the requirements, this will lead to either expansion of
/// this table (community_id optional, addition of tag_type enum) or split of this table / creation
/// of new tables.
#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable, Selectable, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = tag))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct Tag {
pub id: TagId,
pub ap_id: DbUrl,
pub name: String,
/// the community that owns this tag
pub community_id: CommunityId,
pub published: DateTime<Utc>,
#[cfg_attr(feature = "full", ts(optional))]
pub updated: Option<DateTime<Utc>>,
pub deleted: bool,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = tag))]
pub struct TagInsertForm {
pub ap_id: DbUrl,
pub name: String,
pub community_id: CommunityId,
// default now
pub published: Option<DateTime<Utc>>,
pub updated: Option<DateTime<Utc>>,
pub deleted: bool,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_tag))]
pub struct PostTagInsertForm {
pub post_id: PostId,
pub tag_id: TagId,
}

View file

@ -547,6 +547,11 @@ pub mod functions {
// really this function is variadic, this just adds the two-argument version
define_sql_function!(fn coalesce<T: diesel::sql_types::SqlType + diesel::sql_types::SingleValue>(x: diesel::sql_types::Nullable<T>, y: T) -> T);
define_sql_function! {
#[aggregate]
fn json_agg<T: diesel::sql_types::SqlType + diesel::sql_types::SingleValue>(obj: T) -> Json
}
}
pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";

View file

@ -35,6 +35,7 @@ diesel-async = { workspace = true, optional = true }
diesel_ltree = { workspace = true, optional = true }
serde = { workspace = true }
serde_with = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true, optional = true }
ts-rs = { workspace = true, optional = true }
actix-web = { workspace = true, optional = true }
@ -46,3 +47,4 @@ serial_test = { workspace = true }
tokio = { workspace = true }
pretty_assertions = { workspace = true }
url = { workspace = true }
test-context = "0.3.0"

View file

@ -316,17 +316,14 @@ impl CommentView {
comment_id: CommentId,
my_local_user: Option<&'_ LocalUser>,
) -> Result<Self, Error> {
let is_admin = my_local_user.map(|u| u.admin).unwrap_or(false);
// If a person is given, then my_vote (res.9), if None, should be 0, not null
// Necessary to differentiate between other person's votes
let res = queries().read(pool, (comment_id, my_local_user)).await?;
let mut new_view = res.clone();
let mut res = queries().read(pool, (comment_id, my_local_user)).await?;
if my_local_user.is_some() && res.my_vote.is_none() {
new_view.my_vote = Some(0);
res.my_vote = Some(0);
}
if res.comment.deleted || res.comment.removed {
new_view.comment.content = String::new();
}
Ok(new_view)
Ok(handle_deleted(res, is_admin))
}
}
@ -350,22 +347,25 @@ pub struct CommentQuery<'a> {
impl CommentQuery<'_> {
pub async fn list(self, site: &Site, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
let is_admin = self.local_user.map(|u| u.admin).unwrap_or(false);
Ok(
queries()
.list(pool, (self, site))
.await?
.into_iter()
.map(|mut c| {
if c.comment.deleted || c.comment.removed {
c.comment.content = String::new();
}
c
})
.map(|c| handle_deleted(c, is_admin))
.collect(),
)
}
}
fn handle_deleted(mut c: CommentView, is_admin: bool) -> CommentView {
if !is_admin && (c.comment.deleted || c.comment.removed) {
c.comment.content = String::new();
}
c
}
#[cfg(test)]
#[expect(clippy::indexing_slicing)]
mod tests {
@ -1301,4 +1301,65 @@ mod tests {
cleanup(data, pool).await
}
#[tokio::test]
#[serial]
async fn comment_removed() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();
let mut data = init_data(pool).await?;
// Mark a comment as removed
let form = CommentUpdateForm {
removed: Some(true),
..Default::default()
};
Comment::update(pool, data.inserted_comment_0.id, &form).await?;
// Read as normal user, content is cleared
data.timmy_local_user_view.local_user.admin = false;
let comment_view = CommentView::read(
pool,
data.inserted_comment_0.id,
Some(&data.timmy_local_user_view.local_user),
)
.await?;
assert_eq!("", comment_view.comment.content);
let comment_listing = CommentQuery {
community_id: Some(data.inserted_community.id),
local_user: Some(&data.timmy_local_user_view.local_user),
sort: Some(CommentSortType::Old),
..Default::default()
}
.list(&data.site, pool)
.await?;
assert_eq!("", comment_listing[0].comment.content);
// Read as admin, content is returned
data.timmy_local_user_view.local_user.admin = true;
let comment_view = CommentView::read(
pool,
data.inserted_comment_0.id,
Some(&data.timmy_local_user_view.local_user),
)
.await?;
assert_eq!(
data.inserted_comment_0.content,
comment_view.comment.content
);
let comment_listing = CommentQuery {
community_id: Some(data.inserted_community.id),
local_user: Some(&data.timmy_local_user_view.local_user),
sort: Some(CommentSortType::Old),
..Default::default()
}
.list(&data.site, pool)
.await?;
assert_eq!(
data.inserted_comment_0.content,
comment_listing[0].comment.content
);
cleanup(data, pool).await
}
}

View file

@ -14,6 +14,8 @@ pub mod local_user_view;
#[cfg(feature = "full")]
pub mod post_report_view;
#[cfg(feature = "full")]
pub mod post_tags_view;
#[cfg(feature = "full")]
pub mod post_view;
#[cfg(feature = "full")]
pub mod private_message_report_view;

View file

@ -0,0 +1,30 @@
//! see post_view.rs for the reason for this json decoding
use crate::structs::PostTags;
use diesel::{
deserialize::FromSql,
pg::{Pg, PgValue},
serialize::ToSql,
sql_types::{self, Nullable},
};
impl FromSql<Nullable<sql_types::Json>, Pg> for PostTags {
fn from_sql(bytes: PgValue) -> diesel::deserialize::Result<Self> {
let value = <serde_json::Value as FromSql<sql_types::Json, Pg>>::from_sql(bytes)?;
Ok(serde_json::from_value::<PostTags>(value)?)
}
fn from_nullable_sql(
bytes: Option<<Pg as diesel::backend::Backend>::RawValue<'_>>,
) -> diesel::deserialize::Result<Self> {
match bytes {
Some(bytes) => Self::from_sql(bytes),
None => Ok(Self { tags: vec![] }),
}
}
}
impl ToSql<Nullable<sql_types::Json>, Pg> for PostTags {
fn to_sql(&self, out: &mut diesel::serialize::Output<Pg>) -> diesel::serialize::Result {
let value = serde_json::to_value(self)?;
<serde_json::Value as ToSql<sql_types::Json, Pg>>::to_sql(&value, &mut out.reborrow())
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,7 @@
#[cfg(feature = "full")]
use diesel::Queryable;
#[cfg(feature = "full")]
use diesel::{deserialize::FromSqlRow, expression::AsExpression, sql_types};
use lemmy_db_schema::{
aggregates::structs::{CommentAggregates, PersonAggregates, PostAggregates, SiteAggregates},
source::{
@ -20,6 +22,7 @@ use lemmy_db_schema::{
private_message_report::PrivateMessageReport,
registration_application::RegistrationApplication,
site::Site,
tag::Tag,
},
SubscribedType,
};
@ -151,6 +154,7 @@ pub struct PostView {
#[cfg_attr(feature = "full", ts(optional))]
pub my_vote: Option<i16>,
pub unread_comments: i64,
pub tags: PostTags,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
@ -237,3 +241,12 @@ pub struct LocalImageView {
pub local_image: LocalImage,
pub person: Person,
}
#[derive(Clone, serde::Serialize, serde::Deserialize, Debug, PartialEq, Default)]
#[cfg_attr(feature = "full", derive(TS, FromSqlRow, AsExpression))]
#[serde(transparent)]
#[cfg_attr(feature = "full", diesel(sql_type = Nullable<sql_types::Json>))]
/// we wrap this in a struct so we can implement FromSqlRow<Json> for it
pub struct PostTags {
pub tags: Vec<Tag>,
}

View file

@ -188,7 +188,7 @@ impl CommunityView {
let is_mod =
CommunityModeratorView::check_is_community_moderator(pool, community_id, person_id).await;
if is_mod.is_ok()
|| PersonView::read(pool, person_id)
|| PersonView::read(pool, person_id, false)
.await
.is_ok_and(|t| t.is_admin)
{
@ -206,7 +206,7 @@ impl CommunityView {
let is_mod_of_any =
CommunityModeratorView::is_community_moderator_of_any(pool, person_id).await;
if is_mod_of_any.is_ok()
|| PersonView::read(pool, person_id)
|| PersonView::read(pool, person_id, false)
.await
.is_ok_and(|t| t.is_admin)
{

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