mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04:00
Merge remote-tracking branch 'origin/main' into reject_federated_votes
This commit is contained in:
commit
a3b7a46e93
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2538,6 +2538,7 @@ dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"bcrypt",
|
||||
"chrono",
|
||||
"futures",
|
||||
"lemmy_api_common",
|
||||
"lemmy_db_schema",
|
||||
|
@ -21,16 +21,16 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^22.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
||||
"@typescript-eslint/parser": "^8.0.0",
|
||||
"eslint": "^9.8.0",
|
||||
"@types/node": "^22.3.0",
|
||||
"@typescript-eslint/eslint-plugin": "^8.1.0",
|
||||
"@typescript-eslint/parser": "^8.1.0",
|
||||
"eslint": "^9.9.0",
|
||||
"eslint-plugin-prettier": "^5.1.3",
|
||||
"jest": "^29.5.0",
|
||||
"lemmy-js-client": "0.20.0-alpha.4",
|
||||
"lemmy-js-client": "0.20.0-alpha.11",
|
||||
"prettier": "^3.2.5",
|
||||
"ts-jest": "^29.1.0",
|
||||
"typescript": "^5.5.4",
|
||||
"typescript-eslint": "^8.0.0"
|
||||
"typescript-eslint": "^8.1.0"
|
||||
}
|
||||
}
|
||||
|
@ -12,16 +12,16 @@ importers:
|
||||
specifier: ^29.5.12
|
||||
version: 29.5.12
|
||||
'@types/node':
|
||||
specifier: ^22.0.2
|
||||
specifier: ^22.3.0
|
||||
version: 22.3.0
|
||||
'@typescript-eslint/eslint-plugin':
|
||||
specifier: ^8.0.0
|
||||
specifier: ^8.1.0
|
||||
version: 8.1.0(@typescript-eslint/parser@8.1.0(eslint@9.9.0)(typescript@5.5.4))(eslint@9.9.0)(typescript@5.5.4)
|
||||
'@typescript-eslint/parser':
|
||||
specifier: ^8.0.0
|
||||
specifier: ^8.1.0
|
||||
version: 8.1.0(eslint@9.9.0)(typescript@5.5.4)
|
||||
eslint:
|
||||
specifier: ^9.8.0
|
||||
specifier: ^9.9.0
|
||||
version: 9.9.0
|
||||
eslint-plugin-prettier:
|
||||
specifier: ^5.1.3
|
||||
@ -30,8 +30,8 @@ importers:
|
||||
specifier: ^29.5.0
|
||||
version: 29.7.0(@types/node@22.3.0)
|
||||
lemmy-js-client:
|
||||
specifier: 0.20.0-alpha.4
|
||||
version: 0.20.0-alpha.4
|
||||
specifier: 0.20.0-alpha.11
|
||||
version: 0.20.0-alpha.11
|
||||
prettier:
|
||||
specifier: ^3.2.5
|
||||
version: 3.3.3
|
||||
@ -42,7 +42,7 @@ importers:
|
||||
specifier: ^5.5.4
|
||||
version: 5.5.4
|
||||
typescript-eslint:
|
||||
specifier: ^8.0.0
|
||||
specifier: ^8.1.0
|
||||
version: 8.1.0(eslint@9.9.0)(typescript@5.5.4)
|
||||
|
||||
packages:
|
||||
@ -1175,8 +1175,8 @@ packages:
|
||||
resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
lemmy-js-client@0.20.0-alpha.4:
|
||||
resolution: {integrity: sha512-MuVE8u/IFz59ks2vxOUXMRU8x6SIz3Keu4zRhmhNt+n+gI25JAggsTDrtxVCBtrqBhFEe+a3PdXdwm+fXbs0Dw==}
|
||||
lemmy-js-client@0.20.0-alpha.11:
|
||||
resolution: {integrity: sha512-iRSG4xHMjPDIreQqVIoJ5JrMY71uk07G0Zbgyf068xKbib22J3+i1x/XgCTs6tiHlqTnw1Ig/KRq7p7qJoA4uw==}
|
||||
|
||||
leven@3.1.0:
|
||||
resolution: {integrity: sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==}
|
||||
@ -3107,7 +3107,7 @@ snapshots:
|
||||
|
||||
kleur@3.0.3: {}
|
||||
|
||||
lemmy-js-client@0.20.0-alpha.4: {}
|
||||
lemmy-js-client@0.20.0-alpha.11: {}
|
||||
|
||||
leven@3.1.0: {}
|
||||
|
||||
|
@ -92,7 +92,7 @@ test("Create a comment", async () => {
|
||||
|
||||
test("Create a comment in a non-existent post", async () => {
|
||||
await expect(createComment(alpha, -1)).rejects.toStrictEqual(
|
||||
Error("couldnt_find_post"),
|
||||
Error("not_found"),
|
||||
);
|
||||
});
|
||||
|
||||
@ -143,7 +143,7 @@ test("Delete a comment", async () => {
|
||||
await waitUntil(
|
||||
() =>
|
||||
resolveComment(gamma, commentRes.comment_view.comment).catch(e => e),
|
||||
r => r.message !== "couldnt_find_object",
|
||||
r => r.message !== "not_found",
|
||||
)
|
||||
).comment;
|
||||
if (!gammaComment) {
|
||||
@ -161,13 +161,13 @@ test("Delete a comment", async () => {
|
||||
// Make sure that comment is undefined on beta
|
||||
await waitUntil(
|
||||
() => resolveComment(beta, commentRes.comment_view.comment).catch(e => e),
|
||||
e => e.message == "couldnt_find_object",
|
||||
e => e.message == "not_found",
|
||||
);
|
||||
|
||||
// Make sure that comment is undefined on gamma after delete
|
||||
await waitUntil(
|
||||
() => resolveComment(gamma, commentRes.comment_view.comment).catch(e => e),
|
||||
e => e.message === "couldnt_find_object",
|
||||
e => e.message === "not_found",
|
||||
);
|
||||
|
||||
// Test undeleting the comment
|
||||
@ -182,7 +182,7 @@ test("Delete a comment", async () => {
|
||||
let betaComment2 = (
|
||||
await waitUntil(
|
||||
() => resolveComment(beta, commentRes.comment_view.comment).catch(e => e),
|
||||
e => e.message !== "couldnt_find_object",
|
||||
e => e.message !== "not_found",
|
||||
)
|
||||
).comment;
|
||||
expect(betaComment2?.comment.deleted).toBe(false);
|
||||
|
@ -527,12 +527,12 @@ test("Content in local-only community doesn't federate", async () => {
|
||||
// cant resolve the community from another instance
|
||||
await expect(
|
||||
resolveCommunity(beta, communityRes.actor_id),
|
||||
).rejects.toStrictEqual(Error("couldnt_find_object"));
|
||||
).rejects.toStrictEqual(Error("not_found"));
|
||||
|
||||
// create a post, also cant resolve it
|
||||
let postRes = await createPost(alpha, communityRes.id);
|
||||
await expect(resolvePost(beta, postRes.post_view.post)).rejects.toStrictEqual(
|
||||
Error("couldnt_find_object"),
|
||||
Error("not_found"),
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -125,12 +125,12 @@ test("Create a post", async () => {
|
||||
// Delta only follows beta, so it should not see an alpha ap_id
|
||||
await expect(
|
||||
resolvePost(delta, postRes.post_view.post),
|
||||
).rejects.toStrictEqual(Error("couldnt_find_object"));
|
||||
).rejects.toStrictEqual(Error("not_found"));
|
||||
|
||||
// Epsilon has alpha blocked, it should not see the alpha post
|
||||
await expect(
|
||||
resolvePost(epsilon, postRes.post_view.post),
|
||||
).rejects.toStrictEqual(Error("couldnt_find_object"));
|
||||
).rejects.toStrictEqual(Error("not_found"));
|
||||
|
||||
// remove added allow/blocklists
|
||||
editSiteForm.allowed_instances = [];
|
||||
@ -140,9 +140,7 @@ test("Create a post", async () => {
|
||||
});
|
||||
|
||||
test("Create a post in a non-existent community", async () => {
|
||||
await expect(createPost(alpha, -2)).rejects.toStrictEqual(
|
||||
Error("couldnt_find_community"),
|
||||
);
|
||||
await expect(createPost(alpha, -2)).rejects.toStrictEqual(Error("not_found"));
|
||||
});
|
||||
|
||||
test("Unlike a post", async () => {
|
||||
@ -630,7 +628,7 @@ test("Enforce community ban for federated user", async () => {
|
||||
// Alpha tries to make post on beta, but it fails because of ban
|
||||
await expect(
|
||||
createPost(alpha, betaCommunity.community.id),
|
||||
).rejects.toStrictEqual(Error("banned_from_community"));
|
||||
).rejects.toStrictEqual(Error("person_is_banned_from_community"));
|
||||
|
||||
// Unban alpha
|
||||
let unBanAlpha = await banPersonFromCommunity(
|
||||
|
@ -690,7 +690,7 @@ export async function saveUserSettingsBio(
|
||||
blur_nsfw: false,
|
||||
auto_expand: true,
|
||||
theme: "darkly",
|
||||
default_sort_type: "Active",
|
||||
default_post_sort_type: "Active",
|
||||
default_listing_type: "All",
|
||||
interface_language: "en",
|
||||
show_avatars: true,
|
||||
@ -710,7 +710,7 @@ export async function saveUserSettingsFederated(
|
||||
show_nsfw: false,
|
||||
blur_nsfw: true,
|
||||
auto_expand: false,
|
||||
default_sort_type: "Hot",
|
||||
default_post_sort_type: "Hot",
|
||||
default_listing_type: "All",
|
||||
interface_language: "",
|
||||
avatar,
|
||||
|
@ -22,8 +22,7 @@ pub async fn distinguish_comment(
|
||||
data.comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -60,8 +59,7 @@ pub async fn distinguish_comment(
|
||||
data.comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
Ok(Json(CommentResponse {
|
||||
comment_view,
|
||||
|
@ -40,8 +40,7 @@ pub async fn like_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -54,8 +53,7 @@ pub async fn like_comment(
|
||||
let comment_reply = CommentReply::read_by_comment(&mut context.pool(), comment_id).await;
|
||||
if let Ok(Some(reply)) = comment_reply {
|
||||
let recipient_id = reply.recipient_id;
|
||||
if let Ok(Some(local_recipient)) =
|
||||
LocalUserView::read_person(&mut context.pool(), recipient_id).await
|
||||
if let Ok(local_recipient) = LocalUserView::read_person(&mut context.pool(), recipient_id).await
|
||||
{
|
||||
recipient_ids.push(local_recipient.local_user.id);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use lemmy_api_common::{
|
||||
utils::is_mod_or_admin,
|
||||
};
|
||||
use lemmy_db_views::structs::{CommentView, LocalUserView, VoteView};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
/// Lists likes for a comment
|
||||
#[tracing::instrument(skip(context))]
|
||||
@ -19,8 +19,7 @@ pub async fn list_comment_likes(
|
||||
data.comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
|
@ -37,8 +37,7 @@ pub async fn save_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
Ok(Json(CommentResponse {
|
||||
comment_view,
|
||||
|
@ -40,8 +40,7 @@ pub async fn create_comment_report(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -64,9 +63,8 @@ pub async fn create_comment_report(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
|
||||
|
||||
let comment_report_view = CommentReportView::read(&mut context.pool(), report.id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
|
||||
let comment_report_view =
|
||||
CommentReportView::read(&mut context.pool(), report.id, person_id).await?;
|
||||
|
||||
// Email the admins
|
||||
if local_site.reports_email_admins {
|
||||
|
@ -17,9 +17,7 @@ pub async fn resolve_comment_report(
|
||||
) -> LemmyResult<Json<CommentReportResponse>> {
|
||||
let report_id = data.report_id;
|
||||
let person_id = local_user_view.person.id;
|
||||
let report = CommentReportView::read(&mut context.pool(), report_id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
|
||||
let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?;
|
||||
|
||||
let person_id = local_user_view.person.id;
|
||||
check_community_mod_action(
|
||||
@ -41,9 +39,8 @@ pub async fn resolve_comment_report(
|
||||
}
|
||||
|
||||
let report_id = data.report_id;
|
||||
let comment_report_view = CommentReportView::read(&mut context.pool(), report_id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
|
||||
let comment_report_view =
|
||||
CommentReportView::read(&mut context.pool(), report_id, person_id).await?;
|
||||
|
||||
Ok(Json(CommentReportResponse {
|
||||
comment_report_view,
|
||||
|
@ -46,23 +46,18 @@ pub async fn add_mod_to_community(
|
||||
.await?;
|
||||
}
|
||||
|
||||
let community = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), community_id).await?;
|
||||
|
||||
// If user is admin and community is remote, explicitly check that he is a
|
||||
// moderator. This is necessary because otherwise the action would be rejected
|
||||
// by the community's home instance.
|
||||
if local_user_view.local_user.admin && !community.local {
|
||||
let is_mod = CommunityModeratorView::is_community_moderator(
|
||||
CommunityModeratorView::check_is_community_moderator(
|
||||
&mut context.pool(),
|
||||
community.id,
|
||||
local_user_view.person.id,
|
||||
)
|
||||
.await?;
|
||||
if !is_mod {
|
||||
Err(LemmyErrorType::NotAModerator)?
|
||||
}
|
||||
}
|
||||
|
||||
// Update in local database
|
||||
|
@ -111,9 +111,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?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person_view = PersonView::read(&mut context.pool(), data.person_id).await?;
|
||||
|
||||
ActivityChannel::submit_activity(
|
||||
SendActivityData::BanFromCommunity {
|
||||
|
@ -56,8 +56,7 @@ pub async fn block_community(
|
||||
Some(&local_user_view.local_user),
|
||||
false,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
|
||||
ActivityChannel::submit_activity(
|
||||
SendActivityData::FollowCommunity(
|
||||
|
@ -23,9 +23,7 @@ pub async fn follow_community(
|
||||
context: Data<LemmyContext>,
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<CommunityResponse>> {
|
||||
let community = Community::read(&mut context.pool(), data.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), data.community_id).await?;
|
||||
let mut community_follower_form = CommunityFollowerForm {
|
||||
community_id: community.id,
|
||||
person_id: local_user_view.person.id,
|
||||
@ -68,8 +66,7 @@ pub async fn follow_community(
|
||||
Some(&local_user_view.local_user),
|
||||
false,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
|
||||
let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
|
||||
|
||||
|
@ -82,13 +82,10 @@ pub async fn transfer_community(
|
||||
Some(&local_user_view.local_user),
|
||||
false,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
|
||||
let community_id = data.community_id;
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(Json(GetCommunityResponse {
|
||||
|
@ -258,17 +258,13 @@ pub async fn local_user_view_from_jwt(
|
||||
let local_user_id = Claims::validate(jwt, context)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::NotLoggedIn)?;
|
||||
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindLocalUser)?;
|
||||
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id).await?;
|
||||
check_user_valid(&local_user_view.person)?;
|
||||
|
||||
Ok(local_user_view)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
@ -36,8 +36,8 @@ pub async fn add_admin(
|
||||
|
||||
// Make sure that the person_id added is local
|
||||
let added_local_user = LocalUserView::read_person(&mut context.pool(), data.person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::ObjectNotLocal)?;
|
||||
.await
|
||||
.map_err(|_| LemmyErrorType::ObjectNotLocal)?;
|
||||
|
||||
LocalUser::update(
|
||||
&mut context.pool(),
|
||||
|
@ -60,7 +60,7 @@ pub async fn ban_from_site(
|
||||
|
||||
// if its a local user, invalidate logins
|
||||
let local_user = LocalUserView::read_person(&mut context.pool(), person.id).await;
|
||||
if let Ok(Some(local_user)) = local_user {
|
||||
if let Ok(local_user) = local_user {
|
||||
LoginToken::invalidate_all(&mut context.pool(), local_user.local_user.id).await?;
|
||||
}
|
||||
|
||||
@ -84,9 +84,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?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person_view = PersonView::read(&mut context.pool(), person.id).await?;
|
||||
|
||||
ban_nonlocal_user_from_local_communities(
|
||||
&local_user_view,
|
||||
|
@ -32,8 +32,7 @@ pub async fn block_person(
|
||||
|
||||
let target_user = LocalUserView::read_person(&mut context.pool(), target_id)
|
||||
.await
|
||||
.ok()
|
||||
.flatten();
|
||||
.ok();
|
||||
|
||||
if target_user.is_some_and(|t| t.local_user.admin) {
|
||||
Err(LemmyErrorType::CantBlockAdmin)?
|
||||
@ -49,9 +48,7 @@ pub async fn block_person(
|
||||
.with_lemmy_type(LemmyErrorType::PersonBlockAlreadyExists)?;
|
||||
}
|
||||
|
||||
let person_view = PersonView::read(&mut context.pool(), target_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person_view = PersonView::read(&mut context.pool(), target_id).await?;
|
||||
Ok(Json(BlockPersonResponse {
|
||||
person_view,
|
||||
blocked: data.block,
|
||||
|
@ -21,7 +21,6 @@ pub async fn change_password_after_reset(
|
||||
let token = data.token.clone();
|
||||
let local_user_id = PasswordResetRequest::read_and_delete(&mut context.pool(), &token)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::TokenNotFound)?
|
||||
.local_user_id;
|
||||
|
||||
password_length_check(&data.password)?;
|
||||
|
@ -24,9 +24,7 @@ pub async fn login(
|
||||
// Fetch that username / email
|
||||
let username_or_email = data.username_or_email.clone();
|
||||
let local_user_view =
|
||||
LocalUserView::find_by_email_or_name(&mut context.pool(), &username_or_email)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::IncorrectLogin)?;
|
||||
LocalUserView::find_by_email_or_name(&mut context.pool(), &username_or_email).await?;
|
||||
|
||||
// Verify the password
|
||||
let valid: bool = local_user_view
|
||||
|
@ -18,9 +18,7 @@ pub async fn mark_person_mention_as_read(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<PersonMentionResponse>> {
|
||||
let person_mention_id = data.person_mention_id;
|
||||
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPersonMention)?;
|
||||
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?;
|
||||
|
||||
if local_user_view.person.id != read_person_mention.recipient_id {
|
||||
Err(LemmyErrorType::CouldntUpdateComment)?
|
||||
@ -39,9 +37,7 @@ pub async fn mark_person_mention_as_read(
|
||||
let person_mention_id = read_person_mention.id;
|
||||
let person_id = local_user_view.person.id;
|
||||
let person_mention_view =
|
||||
PersonMentionView::read(&mut context.pool(), person_mention_id, Some(person_id))
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPersonMention)?;
|
||||
PersonMentionView::read(&mut context.pool(), person_mention_id, Some(person_id)).await?;
|
||||
|
||||
Ok(Json(PersonMentionResponse {
|
||||
person_mention_view,
|
||||
|
@ -18,9 +18,7 @@ pub async fn mark_reply_as_read(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<CommentReplyResponse>> {
|
||||
let comment_reply_id = data.comment_reply_id;
|
||||
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommentReply)?;
|
||||
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;
|
||||
|
||||
if local_user_view.person.id != read_comment_reply.recipient_id {
|
||||
Err(LemmyErrorType::CouldntUpdateComment)?
|
||||
@ -40,9 +38,7 @@ pub async fn mark_reply_as_read(
|
||||
let comment_reply_id = read_comment_reply.id;
|
||||
let person_id = local_user_view.person.id;
|
||||
let comment_reply_view =
|
||||
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id))
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommentReply)?;
|
||||
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id)).await?;
|
||||
|
||||
Ok(Json(CommentReplyResponse { comment_reply_view }))
|
||||
}
|
||||
|
@ -16,8 +16,8 @@ pub async fn reset_password(
|
||||
// Fetch that email
|
||||
let email = data.email.to_lowercase();
|
||||
let local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::IncorrectLogin)?;
|
||||
.await
|
||||
.map_err(|_| LemmyErrorType::IncorrectLogin)?;
|
||||
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
check_email_verified(&local_user_view, &site_view)?;
|
||||
|
@ -63,9 +63,7 @@ pub async fn save_user_settings(
|
||||
let previous_email = local_user_view.local_user.email.clone().unwrap_or_default();
|
||||
// if email was changed, check that it is not taken and send verification mail
|
||||
if previous_email.deref() != email {
|
||||
if LocalUser::is_email_taken(&mut context.pool(), email).await? {
|
||||
return Err(LemmyErrorType::EmailAlreadyExists)?;
|
||||
}
|
||||
LocalUser::check_is_email_taken(&mut context.pool(), email).await?;
|
||||
send_verification_email(
|
||||
&local_user_view,
|
||||
email,
|
||||
@ -132,7 +130,6 @@ pub async fn save_user_settings(
|
||||
send_notifications_to_email: data.send_notifications_to_email,
|
||||
show_nsfw: data.show_nsfw,
|
||||
blur_nsfw: data.blur_nsfw,
|
||||
auto_expand: data.auto_expand,
|
||||
show_bot_accounts: data.show_bot_accounts,
|
||||
default_post_sort_type,
|
||||
default_comment_sort_type,
|
||||
|
@ -10,7 +10,7 @@ use lemmy_db_schema::source::{
|
||||
local_user::{LocalUser, LocalUserUpdateForm},
|
||||
};
|
||||
use lemmy_db_views::structs::{LocalUserView, SiteView};
|
||||
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
pub async fn verify_email(
|
||||
data: Json<VerifyEmail>,
|
||||
@ -18,9 +18,7 @@ pub async fn verify_email(
|
||||
) -> LemmyResult<Json<SuccessResponse>> {
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
let token = data.token.clone();
|
||||
let verification = EmailVerification::read_for_token(&mut context.pool(), &token)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::TokenNotFound)?;
|
||||
let verification = EmailVerification::read_for_token(&mut context.pool(), &token).await?;
|
||||
|
||||
let form = LocalUserUpdateForm {
|
||||
// necessary in case this is a new signup
|
||||
@ -37,9 +35,7 @@ pub async fn verify_email(
|
||||
|
||||
// send out notification about registration application to admins if enabled
|
||||
if site_view.local_site.application_email_admins {
|
||||
let local_user = LocalUserView::read(&mut context.pool(), local_user_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let local_user = LocalUserView::read(&mut context.pool(), local_user_id).await?;
|
||||
|
||||
send_new_applicant_email_to_admins(
|
||||
&local_user.person.name,
|
||||
|
@ -16,7 +16,7 @@ use lemmy_db_schema::{
|
||||
PostFeatureType,
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn feature_post(
|
||||
@ -25,9 +25,7 @@ pub async fn feature_post(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<PostResponse>> {
|
||||
let post_id = data.post_id;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
check_community_mod_action(
|
||||
&local_user_view.person,
|
||||
|
@ -38,9 +38,7 @@ pub async fn like_post(
|
||||
|
||||
// Check for a community ban
|
||||
let post_id = data.post_id;
|
||||
let post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -70,9 +68,7 @@ pub async fn like_post(
|
||||
|
||||
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
|
||||
|
||||
let community = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), post.community_id).await?;
|
||||
|
||||
ActivityChannel::submit_activity(
|
||||
SendActivityData::LikePostOrComment {
|
||||
|
@ -6,7 +6,7 @@ use lemmy_api_common::{
|
||||
};
|
||||
use lemmy_db_schema::{source::post::Post, traits::Crud};
|
||||
use lemmy_db_views::structs::{LocalUserView, VoteView};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
/// Lists likes for a post
|
||||
#[tracing::instrument(skip(context))]
|
||||
@ -15,9 +15,7 @@ pub async fn list_post_likes(
|
||||
context: Data<LemmyContext>,
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<ListPostLikesResponse>> {
|
||||
let post = Post::read(&mut context.pool(), data.post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let post = Post::read(&mut context.pool(), data.post_id).await?;
|
||||
is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
&local_user_view.person,
|
||||
|
@ -15,7 +15,7 @@ use lemmy_db_schema::{
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn lock_post(
|
||||
@ -24,9 +24,7 @@ pub async fn lock_post(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<PostResponse>> {
|
||||
let post_id = data.post_id;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
check_community_mod_action(
|
||||
&local_user_view.person,
|
||||
|
@ -40,8 +40,7 @@ pub async fn save_post(
|
||||
Some(&local_user_view.local_user),
|
||||
false,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
.await?;
|
||||
|
||||
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
|
||||
|
||||
|
@ -35,9 +35,7 @@ pub async fn create_post_report(
|
||||
|
||||
let person_id = local_user_view.person.id;
|
||||
let post_id = data.post_id;
|
||||
let post_view = PostView::read(&mut context.pool(), post_id, None, false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let post_view = PostView::read(&mut context.pool(), post_id, None, false).await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -61,9 +59,7 @@ pub async fn create_post_report(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
|
||||
|
||||
let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPostReport)?;
|
||||
let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?;
|
||||
|
||||
// Email the admins
|
||||
if local_site.reports_email_admins {
|
||||
|
@ -17,9 +17,7 @@ pub async fn resolve_post_report(
|
||||
) -> LemmyResult<Json<PostReportResponse>> {
|
||||
let report_id = data.report_id;
|
||||
let person_id = local_user_view.person.id;
|
||||
let report = PostReportView::read(&mut context.pool(), report_id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPostReport)?;
|
||||
let report = PostReportView::read(&mut context.pool(), report_id, person_id).await?;
|
||||
|
||||
let person_id = local_user_view.person.id;
|
||||
check_community_mod_action(
|
||||
@ -40,9 +38,7 @@ pub async fn resolve_post_report(
|
||||
.with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
|
||||
}
|
||||
|
||||
let post_report_view = PostReportView::read(&mut context.pool(), report_id, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPostReport)?;
|
||||
let post_report_view = PostReportView::read(&mut context.pool(), report_id, person_id).await?;
|
||||
|
||||
Ok(Json(PostReportResponse { post_report_view }))
|
||||
}
|
||||
|
@ -18,9 +18,7 @@ pub async fn mark_pm_as_read(
|
||||
) -> LemmyResult<Json<PrivateMessageResponse>> {
|
||||
// Checking permissions
|
||||
let private_message_id = data.private_message_id;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
||||
if local_user_view.person.id != orig_private_message.recipient_id {
|
||||
Err(LemmyErrorType::CouldntUpdatePrivateMessage)?
|
||||
}
|
||||
@ -39,9 +37,7 @@ pub async fn mark_pm_as_read(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
|
||||
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
|
||||
Ok(Json(PrivateMessageResponse {
|
||||
private_message_view: view,
|
||||
}))
|
||||
|
@ -29,9 +29,7 @@ pub async fn create_pm_report(
|
||||
|
||||
let person_id = local_user_view.person.id;
|
||||
let private_message_id = data.private_message_id;
|
||||
let private_message = PrivateMessage::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
||||
|
||||
// Make sure that only the recipient of the private message can create a report
|
||||
if person_id != private_message.recipient_id {
|
||||
@ -49,9 +47,8 @@ pub async fn create_pm_report(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
|
||||
|
||||
let private_message_report_view = PrivateMessageReportView::read(&mut context.pool(), report.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessageReport)?;
|
||||
let private_message_report_view =
|
||||
PrivateMessageReportView::read(&mut context.pool(), report.id).await?;
|
||||
|
||||
// Email the admins
|
||||
if local_site.reports_email_admins {
|
||||
|
@ -28,9 +28,8 @@ pub async fn resolve_pm_report(
|
||||
.with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
|
||||
}
|
||||
|
||||
let private_message_report_view = PrivateMessageReportView::read(&mut context.pool(), report_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessageReport)?;
|
||||
let private_message_report_view =
|
||||
PrivateMessageReportView::read(&mut context.pool(), report_id).await?;
|
||||
|
||||
Ok(Json(PrivateMessageReportResponse {
|
||||
private_message_report_view,
|
||||
|
@ -16,7 +16,7 @@ use lemmy_db_schema::{
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views::structs::{CommentView, LocalUserView};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn purge_comment(
|
||||
@ -35,8 +35,7 @@ pub async fn purge_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
// Also check that you're a higher admin
|
||||
LocalUser::is_higher_admin_check(
|
||||
|
@ -19,7 +19,7 @@ use lemmy_db_schema::{
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_db_views_actor::structs::CommunityModeratorView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn purge_community(
|
||||
@ -31,9 +31,7 @@ pub async fn purge_community(
|
||||
is_admin(&local_user_view)?;
|
||||
|
||||
// Read the community to get its images
|
||||
let community = Community::read(&mut context.pool(), data.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), data.community_id).await?;
|
||||
|
||||
// Also check that you're a higher admin than all the mods
|
||||
let community_mod_person_ids =
|
||||
|
@ -17,7 +17,7 @@ use lemmy_db_schema::{
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn purge_person(
|
||||
@ -36,9 +36,7 @@ pub async fn purge_person(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let person = Person::read(&mut context.pool(), data.person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person = Person::read(&mut context.pool(), data.person_id).await?;
|
||||
|
||||
ban_nonlocal_user_from_local_communities(
|
||||
&local_user_view,
|
||||
|
@ -17,7 +17,7 @@ use lemmy_db_schema::{
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn purge_post(
|
||||
@ -29,9 +29,7 @@ pub async fn purge_post(
|
||||
is_admin(&local_user_view)?;
|
||||
|
||||
// Read the post to get the community_id
|
||||
let post = Post::read(&mut context.pool(), data.post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let post = Post::read(&mut context.pool(), data.post_id).await?;
|
||||
|
||||
// Also check that you're a higher admin
|
||||
LocalUser::is_higher_admin_check(
|
||||
|
@ -14,10 +14,7 @@ use lemmy_db_schema::{
|
||||
utils::{diesel_string_update, get_conn},
|
||||
};
|
||||
use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
|
||||
pub async fn approve_registration_application(
|
||||
data: Json<ApproveRegistrationApplication>,
|
||||
@ -61,9 +58,8 @@ pub async fn approve_registration_application(
|
||||
.await?;
|
||||
|
||||
if data.approve {
|
||||
let approved_local_user_view = LocalUserView::read(&mut context.pool(), approved_user_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindLocalUser)?;
|
||||
let approved_local_user_view =
|
||||
LocalUserView::read(&mut context.pool(), approved_user_id).await?;
|
||||
if approved_local_user_view.local_user.email.is_some() {
|
||||
// Email sending may fail, but this won't revert the application approval
|
||||
send_application_approved_email(&approved_local_user_view, context.settings()).await?;
|
||||
@ -71,9 +67,8 @@ pub async fn approve_registration_application(
|
||||
};
|
||||
|
||||
// Read the view
|
||||
let registration_application = RegistrationApplicationView::read(&mut context.pool(), app_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?;
|
||||
let registration_application =
|
||||
RegistrationApplicationView::read(&mut context.pool(), app_id).await?;
|
||||
|
||||
Ok(Json(RegistrationApplicationResponse {
|
||||
registration_application,
|
||||
|
@ -5,7 +5,7 @@ use lemmy_api_common::{
|
||||
utils::is_admin,
|
||||
};
|
||||
use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
/// Lists registration applications, filterable by undenied only.
|
||||
pub async fn get_registration_application(
|
||||
@ -18,9 +18,7 @@ pub async fn get_registration_application(
|
||||
|
||||
// Read the view
|
||||
let registration_application =
|
||||
RegistrationApplicationView::read_by_person(&mut context.pool(), data.person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?;
|
||||
RegistrationApplicationView::read_by_person(&mut context.pool(), data.person_id).await?;
|
||||
|
||||
Ok(Json(RegistrationApplicationResponse {
|
||||
registration_application,
|
||||
|
@ -34,7 +34,7 @@ use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType, CACHE_DURATION_API};
|
||||
use serial_test::serial;
|
||||
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
async fn create_test_site(context: &Data<LemmyContext>) -> LemmyResult<(Instance, LocalUserView)> {
|
||||
let pool = &mut context.pool();
|
||||
|
||||
@ -54,20 +54,20 @@ async fn create_test_site(context: &Data<LemmyContext>) -> LemmyResult<(Instance
|
||||
)
|
||||
.await?;
|
||||
|
||||
let admin_local_user_view = LocalUserView::read_person(pool, admin_person.id)
|
||||
.await?
|
||||
.unwrap();
|
||||
let admin_local_user_view = LocalUserView::read_person(pool, admin_person.id).await?;
|
||||
|
||||
let site_form = SiteInsertForm::new("test site".to_string(), inserted_instance.id);
|
||||
let site = Site::create(pool, &site_form).await.unwrap();
|
||||
|
||||
// Create a local site, since this is necessary for determining if email verification is
|
||||
// required
|
||||
let mut local_site_form = LocalSiteInsertForm::new(site.id);
|
||||
local_site_form.require_email_verification = Some(true);
|
||||
local_site_form.application_question = Some(".".to_string());
|
||||
local_site_form.registration_mode = Some(RegistrationMode::RequireApplication);
|
||||
local_site_form.site_setup = Some(true);
|
||||
let local_site_form = LocalSiteInsertForm {
|
||||
require_email_verification: Some(true),
|
||||
application_question: Some(".".to_string()),
|
||||
registration_mode: Some(RegistrationMode::RequireApplication),
|
||||
site_setup: Some(true),
|
||||
..LocalSiteInsertForm::new(site.id)
|
||||
};
|
||||
let local_site = LocalSite::create(pool, &local_site_form).await.unwrap();
|
||||
|
||||
// Required to have a working local SiteView when updating the site to change email verification
|
||||
@ -109,7 +109,7 @@ async fn signup(
|
||||
Ok((local_user, application))
|
||||
}
|
||||
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
async fn get_application_statuses(
|
||||
context: &Data<LemmyContext>,
|
||||
admin: LocalUserView,
|
||||
@ -138,10 +138,9 @@ async fn get_application_statuses(
|
||||
Ok((application_count, unread_applications, all_applications))
|
||||
}
|
||||
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
#[tokio::test]
|
||||
#[expect(clippy::indexing_slicing)]
|
||||
async fn test_application_approval() -> LemmyResult<()> {
|
||||
let context = LemmyContext::init_test_context().await;
|
||||
let pool = &mut context.pool();
|
||||
|
@ -42,7 +42,7 @@ pub async fn get_sitemap(context: Data<LemmyContext>) -> LemmyResult<HttpRespons
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
pub(crate) mod tests {
|
||||
|
||||
use crate::sitemap::generate_urlset;
|
||||
|
@ -27,7 +27,6 @@ use lemmy_db_views_actor::structs::CommunityView;
|
||||
use lemmy_utils::{
|
||||
error::LemmyResult,
|
||||
utils::{markdown::markdown_to_html, mention::MentionData},
|
||||
LemmyErrorType,
|
||||
};
|
||||
|
||||
pub async fn build_comment_response(
|
||||
@ -37,9 +36,8 @@ pub async fn build_comment_response(
|
||||
recipient_ids: Vec<LocalUserId>,
|
||||
) -> LemmyResult<CommentResponse> {
|
||||
let local_user = local_user_view.map(|l| l.local_user);
|
||||
let comment_view = CommentView::read(&mut context.pool(), comment_id, local_user.as_ref())
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
let comment_view =
|
||||
CommentView::read(&mut context.pool(), comment_id, local_user.as_ref()).await?;
|
||||
Ok(CommentResponse {
|
||||
comment_view,
|
||||
recipient_ids,
|
||||
@ -61,8 +59,7 @@ pub async fn build_community_response(
|
||||
Some(&local_user),
|
||||
is_mod_or_admin,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
|
||||
|
||||
Ok(Json(CommunityResponse {
|
||||
@ -87,8 +84,7 @@ pub async fn build_post_response(
|
||||
Some(&local_user),
|
||||
is_mod_or_admin,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
.await?;
|
||||
Ok(Json(PostResponse { post_view }))
|
||||
}
|
||||
|
||||
@ -112,8 +108,7 @@ pub async fn send_local_notifs(
|
||||
comment_id,
|
||||
local_user_view.map(|view| &view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
let comment = comment_view.comment;
|
||||
let post = comment_view.post;
|
||||
let community = comment_view.community;
|
||||
@ -125,7 +120,7 @@ pub async fn send_local_notifs(
|
||||
{
|
||||
let mention_name = mention.name.clone();
|
||||
let user_view = LocalUserView::read_from_name(&mut context.pool(), &mention_name).await;
|
||||
if let Ok(Some(mention_user_view)) = user_view {
|
||||
if let Ok(mention_user_view) = user_view {
|
||||
// TODO
|
||||
// At some point, make it so you can't tag the parent creator either
|
||||
// Potential duplication of notifications, one for reply and the other for mention, is handled
|
||||
@ -161,9 +156,7 @@ pub async fn send_local_notifs(
|
||||
|
||||
// Send comment_reply to the parent commenter / poster
|
||||
if let Some(parent_comment_id) = comment.parent_comment_id() {
|
||||
let parent_comment = Comment::read(&mut context.pool(), parent_comment_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
let parent_comment = Comment::read(&mut context.pool(), parent_comment_id).await?;
|
||||
|
||||
// Get the parent commenter local_user
|
||||
let parent_creator_id = parent_comment.creator_id;
|
||||
@ -182,7 +175,7 @@ pub async fn send_local_notifs(
|
||||
// Don't send a notif to yourself
|
||||
if parent_comment.creator_id != person.id && !check_blocks {
|
||||
let user_view = LocalUserView::read_person(&mut context.pool(), parent_creator_id).await;
|
||||
if let Ok(Some(parent_user_view)) = user_view {
|
||||
if let Ok(parent_user_view) = user_view {
|
||||
// Don't duplicate notif if already mentioned by checking recipient ids
|
||||
if !recipient_ids.contains(&parent_user_view.local_user.id) {
|
||||
recipient_ids.push(parent_user_view.local_user.id);
|
||||
@ -229,7 +222,7 @@ pub async fn send_local_notifs(
|
||||
if post.creator_id != person.id && !check_blocks {
|
||||
let creator_id = post.creator_id;
|
||||
let parent_user = LocalUserView::read_person(&mut context.pool(), creator_id).await;
|
||||
if let Ok(Some(parent_user_view)) = parent_user {
|
||||
if let Ok(parent_user_view) = parent_user {
|
||||
if !recipient_ids.contains(&parent_user_view.local_user.id) {
|
||||
recipient_ids.push(parent_user_view.local_user.id);
|
||||
|
||||
|
@ -29,13 +29,9 @@ impl Claims {
|
||||
let claims =
|
||||
decode::<Claims>(jwt, &key, &validation).with_lemmy_type(LemmyErrorType::NotLoggedIn)?;
|
||||
let user_id = LocalUserId(claims.claims.sub.parse()?);
|
||||
let is_valid = LoginToken::validate(&mut context.pool(), user_id, jwt).await?;
|
||||
if !is_valid {
|
||||
Err(LemmyErrorType::NotLoggedIn)?
|
||||
} else {
|
||||
LoginToken::validate(&mut context.pool(), user_id, jwt).await?;
|
||||
Ok(user_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn generate(
|
||||
user_id: LocalUserId,
|
||||
@ -73,8 +69,7 @@ impl Claims {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
|
||||
use crate::{claims::Claims, context::LemmyContext};
|
||||
|
@ -84,8 +84,8 @@ pub struct CaptchaResponse {
|
||||
pub struct SaveUserSettings {
|
||||
/// Show nsfw posts.
|
||||
pub show_nsfw: Option<bool>,
|
||||
/// Blur nsfw posts.
|
||||
pub blur_nsfw: Option<bool>,
|
||||
pub auto_expand: Option<bool>,
|
||||
/// Your user's theme.
|
||||
pub theme: Option<String>,
|
||||
/// The default post listing type, usually "local"
|
||||
|
@ -30,6 +30,8 @@ pub struct CreatePost {
|
||||
pub language_id: Option<LanguageId>,
|
||||
/// Instead of fetching a thumbnail, use a custom one.
|
||||
pub custom_thumbnail: Option<String>,
|
||||
/// Time when this post should be scheduled. Null means publish immediately.
|
||||
pub scheduled_publish_time: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@ -124,6 +126,8 @@ pub struct EditPost {
|
||||
pub language_id: Option<LanguageId>,
|
||||
/// Instead of fetching a thumbnail, use a custom one.
|
||||
pub custom_thumbnail: Option<String>,
|
||||
/// Time when this post should be scheduled. Null means publish immediately.
|
||||
pub scheduled_publish_time: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
|
||||
|
@ -471,8 +471,7 @@ pub async fn replace_image(
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
|
||||
use crate::{
|
||||
|
@ -78,7 +78,11 @@ pub struct Search {
|
||||
pub listing_type: Option<ListingType>,
|
||||
pub page: Option<i64>,
|
||||
pub limit: Option<i64>,
|
||||
pub post_title_only: Option<bool>,
|
||||
pub title_only: Option<bool>,
|
||||
pub post_url_only: Option<bool>,
|
||||
pub saved_only: Option<bool>,
|
||||
pub liked_only: Option<bool>,
|
||||
pub disliked_only: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
|
@ -17,7 +17,7 @@ use lemmy_db_schema::{
|
||||
community::{Community, CommunityModerator, CommunityUpdateForm},
|
||||
community_block::CommunityBlock,
|
||||
email_verification::{EmailVerification, EmailVerificationForm},
|
||||
images::RemoteImage,
|
||||
images::{ImageDetails, RemoteImage},
|
||||
instance::Instance,
|
||||
instance_block::InstanceBlock,
|
||||
local_site::LocalSite,
|
||||
@ -73,13 +73,7 @@ pub async fn is_mod_or_admin(
|
||||
community_id: CommunityId,
|
||||
) -> LemmyResult<()> {
|
||||
check_user_valid(person)?;
|
||||
|
||||
let is_mod_or_admin = CommunityView::is_mod_or_admin(pool, person.id, community_id).await?;
|
||||
if !is_mod_or_admin {
|
||||
Err(LemmyErrorType::NotAModOrAdmin)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
CommunityView::check_is_mod_or_admin(pool, person.id, community_id).await
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
@ -110,13 +104,7 @@ pub async fn check_community_mod_of_any_or_admin_action(
|
||||
let person = &local_user_view.person;
|
||||
|
||||
check_user_valid(person)?;
|
||||
|
||||
let is_mod_of_any_or_admin = CommunityView::is_mod_of_any_or_admin(pool, person.id).await?;
|
||||
if !is_mod_of_any_or_admin {
|
||||
Err(LemmyErrorType::NotAModOrAdmin)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
CommunityView::check_is_mod_of_any_or_admin(pool, person.id).await
|
||||
}
|
||||
|
||||
pub fn is_admin(local_user_view: &LocalUserView) -> LemmyResult<()> {
|
||||
@ -175,9 +163,7 @@ pub async fn update_read_comments(
|
||||
..PersonPostAggregatesForm::default()
|
||||
};
|
||||
|
||||
PersonPostAggregates::upsert(pool, &person_post_agg_form)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
|
||||
PersonPostAggregates::upsert(pool, &person_post_agg_form).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -223,9 +209,7 @@ pub async fn check_registration_application(
|
||||
// Fetch the registration application. If no admin id is present its still pending. Otherwise it
|
||||
// was processed (either accepted or denied).
|
||||
let local_user_id = local_user_view.local_user.id;
|
||||
let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?;
|
||||
let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id).await?;
|
||||
if registration.admin_id.is_some() {
|
||||
Err(LemmyErrorType::RegistrationDenied(registration.deny_reason))?
|
||||
} else {
|
||||
@ -246,7 +230,7 @@ pub async fn check_community_user_action(
|
||||
) -> LemmyResult<()> {
|
||||
check_user_valid(person)?;
|
||||
check_community_deleted_removed(community_id, pool).await?;
|
||||
check_community_ban(person, community_id, pool).await?;
|
||||
CommunityPersonBanView::check(pool, person.id, community_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -254,28 +238,13 @@ async fn check_community_deleted_removed(
|
||||
community_id: CommunityId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
let community = Community::read(pool, community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(pool, community_id).await?;
|
||||
if community.deleted || community.removed {
|
||||
Err(LemmyErrorType::Deleted)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn check_community_ban(
|
||||
person: &Person,
|
||||
community_id: CommunityId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
// check if user was banned from site or community
|
||||
let is_banned = CommunityPersonBanView::get(pool, person.id, community_id).await?;
|
||||
if is_banned {
|
||||
Err(LemmyErrorType::BannedFromCommunity)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check that the given user can perform a mod action in the community.
|
||||
///
|
||||
/// In particular it checks that he is an admin or mod, wasn't banned and the community isn't
|
||||
@ -287,7 +256,7 @@ pub async fn check_community_mod_action(
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
is_mod_or_admin(pool, person, community_id).await?;
|
||||
check_community_ban(person, community_id, pool).await?;
|
||||
CommunityPersonBanView::check(pool, person.id, community_id).await?;
|
||||
|
||||
// it must be possible to restore deleted community
|
||||
if !allow_deleted {
|
||||
@ -313,51 +282,6 @@ pub fn check_comment_deleted_or_removed(comment: &Comment) -> LemmyResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Throws an error if a recipient has blocked a person.
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn check_person_block(
|
||||
my_id: PersonId,
|
||||
potential_blocker_id: PersonId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
let is_blocked = PersonBlock::read(pool, potential_blocker_id, my_id).await?;
|
||||
if is_blocked {
|
||||
Err(LemmyErrorType::PersonIsBlocked)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Throws an error if a recipient has blocked a community.
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn check_community_block(
|
||||
community_id: CommunityId,
|
||||
person_id: PersonId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
let is_blocked = CommunityBlock::read(pool, person_id, community_id).await?;
|
||||
if is_blocked {
|
||||
Err(LemmyErrorType::CommunityIsBlocked)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Throws an error if a recipient has blocked an instance.
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn check_instance_block(
|
||||
instance_id: InstanceId,
|
||||
person_id: PersonId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
let is_blocked = InstanceBlock::read(pool, person_id, instance_id).await?;
|
||||
if is_blocked {
|
||||
Err(LemmyErrorType::InstanceIsBlocked)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn check_person_instance_community_block(
|
||||
my_id: PersonId,
|
||||
@ -366,9 +290,9 @@ pub async fn check_person_instance_community_block(
|
||||
community_id: CommunityId,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> LemmyResult<()> {
|
||||
check_person_block(my_id, potential_blocker_id, pool).await?;
|
||||
check_instance_block(community_instance_id, potential_blocker_id, pool).await?;
|
||||
check_community_block(community_id, potential_blocker_id, pool).await?;
|
||||
PersonBlock::read(pool, potential_blocker_id, my_id).await?;
|
||||
InstanceBlock::read(pool, potential_blocker_id, community_instance_id).await?;
|
||||
CommunityBlock::read(pool, potential_blocker_id, community_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -704,7 +628,7 @@ pub async fn purge_image_posts_for_person(
|
||||
|
||||
/// Delete a local_user's images
|
||||
async fn delete_local_user_images(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> {
|
||||
if let Ok(Some(local_user)) = LocalUserView::read_person(&mut context.pool(), person_id).await {
|
||||
if let Ok(local_user) = LocalUserView::read_person(&mut context.pool(), person_id).await {
|
||||
let pictrs_uploads =
|
||||
LocalImageView::get_all_by_local_user_id(&mut context.pool(), local_user.local_user.id)
|
||||
.await?;
|
||||
@ -749,9 +673,7 @@ pub async fn remove_user_data(
|
||||
) -> LemmyResult<()> {
|
||||
let pool = &mut context.pool();
|
||||
// Purge user images
|
||||
let person = Person::read(pool, banned_person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person = Person::read(pool, banned_person_id).await?;
|
||||
if let Some(avatar) = person.avatar {
|
||||
purge_image_from_pictrs(&avatar, context).await.ok();
|
||||
}
|
||||
@ -854,12 +776,13 @@ pub async fn remove_or_restore_user_data_in_community(
|
||||
|
||||
// Comments
|
||||
// TODO Diesel doesn't allow updates with joins, so this has to be a loop
|
||||
let site = Site::read_local(pool).await?;
|
||||
let comments = CommentQuery {
|
||||
creator_id: Some(banned_person_id),
|
||||
community_id: Some(community_id),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
.list(&site, pool)
|
||||
.await?;
|
||||
|
||||
for comment_view in &comments {
|
||||
@ -881,9 +804,7 @@ pub async fn remove_or_restore_user_data_in_community(
|
||||
pub async fn purge_user_account(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> {
|
||||
let pool = &mut context.pool();
|
||||
|
||||
let person = Person::read(pool, person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person = Person::read(pool, person_id).await?;
|
||||
|
||||
// Delete their local images, if they're a local user
|
||||
delete_local_user_images(person_id, context).await.ok();
|
||||
@ -913,7 +834,7 @@ pub async fn purge_user_account(person_id: PersonId, context: &LemmyContext) ->
|
||||
CommunityModerator::leave_all_communities(pool, person_id).await?;
|
||||
|
||||
// Delete the oauth accounts linked to the local user
|
||||
if let Ok(Some(local_user)) = LocalUserView::read_person(pool, person_id).await {
|
||||
if let Ok(local_user) = LocalUserView::read_person(pool, person_id).await {
|
||||
OAuthAccount::delete_user_accounts(pool, local_user.local_user.id).await?;
|
||||
}
|
||||
|
||||
@ -999,6 +920,18 @@ fn limit_expire_time(expires: DateTime<Utc>) -> LemmyResult<Option<DateTime<Utc>
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn check_conflicting_like_filters(
|
||||
liked_only: Option<bool>,
|
||||
disliked_only: Option<bool>,
|
||||
) -> LemmyResult<()> {
|
||||
if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() {
|
||||
Err(LemmyErrorType::ContradictingFilters)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn process_markdown(
|
||||
text: &str,
|
||||
slur_regex: &Option<Regex>,
|
||||
@ -1012,6 +945,7 @@ pub async fn process_markdown(
|
||||
|
||||
if context.settings().pictrs_config()?.image_mode() == PictrsImageMode::ProxyAllImages {
|
||||
let (text, links) = markdown_rewrite_image_links(text);
|
||||
RemoteImage::create(&mut context.pool(), links.clone()).await?;
|
||||
|
||||
// Create images and image detail rows
|
||||
for link in links {
|
||||
@ -1021,7 +955,7 @@ pub async fn process_markdown(
|
||||
let proxied =
|
||||
build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?;
|
||||
let details_form = details.build_image_details_form(&proxied);
|
||||
RemoteImage::create(&mut context.pool(), &details_form).await?;
|
||||
ImageDetails::create(&mut context.pool(), &details_form).await?;
|
||||
}
|
||||
}
|
||||
Ok(text)
|
||||
@ -1057,13 +991,15 @@ async fn proxy_image_link_internal(
|
||||
if link.domain() == Some(&context.settings().hostname) {
|
||||
Ok(link.into())
|
||||
} else if image_mode == PictrsImageMode::ProxyAllImages {
|
||||
RemoteImage::create(&mut context.pool(), vec![link.clone()]).await?;
|
||||
|
||||
let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?;
|
||||
// This should fail softly, since pictrs might not even be running
|
||||
let details_res = fetch_pictrs_proxied_image_details(&link, context).await;
|
||||
|
||||
if let Ok(details) = details_res {
|
||||
let details_form = details.build_image_details_form(&proxied);
|
||||
RemoteImage::create(&mut context.pool(), &details_form).await?;
|
||||
ImageDetails::create(&mut context.pool(), &details_form).await?;
|
||||
};
|
||||
|
||||
Ok(proxied.into())
|
||||
@ -1131,8 +1067,7 @@ fn build_proxied_image_url(
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
@ -1209,7 +1144,7 @@ mod tests {
|
||||
assert!(
|
||||
RemoteImage::validate(&mut context.pool(), remote_image.into())
|
||||
.await
|
||||
.is_err()
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ futures.workspace = true
|
||||
uuid = { workspace = true }
|
||||
moka.workspace = true
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
webmention = "0.6.0"
|
||||
accept-language = "3.1.0"
|
||||
serde_json = { workspace = true }
|
||||
|
@ -57,8 +57,7 @@ pub async fn create_comment(
|
||||
Some(&local_user_view.local_user),
|
||||
true,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
.await?;
|
||||
|
||||
let post = post_view.post;
|
||||
let community_id = post_view.community.id;
|
||||
@ -79,8 +78,7 @@ pub async fn create_comment(
|
||||
Comment::read(&mut context.pool(), parent_id).await.ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.flatten();
|
||||
};
|
||||
|
||||
// If there's a parent_id, check to make sure that comment is in that post
|
||||
// Strange issue where sometimes the post ID of the parent comment is incorrect
|
||||
@ -111,9 +109,10 @@ pub async fn create_comment(
|
||||
}
|
||||
};
|
||||
|
||||
let mut comment_form =
|
||||
CommentInsertForm::new(local_user_view.person.id, data.post_id, content.clone());
|
||||
comment_form.language_id = language_id;
|
||||
let comment_form = CommentInsertForm {
|
||||
language_id,
|
||||
..CommentInsertForm::new(local_user_view.person.id, data.post_id, content.clone())
|
||||
};
|
||||
|
||||
// Create the comment
|
||||
let parent_path = parent_opt.clone().map(|t| t.path);
|
||||
|
@ -26,8 +26,7 @@ pub async fn delete_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
// Dont delete it if its already been deleted.
|
||||
if orig_comment.comment.deleted == data.deleted {
|
||||
|
@ -31,8 +31,7 @@ pub async fn remove_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
check_community_mod_action(
|
||||
&local_user_view.person,
|
||||
|
@ -41,8 +41,7 @@ pub async fn update_comment(
|
||||
comment_id,
|
||||
Some(&local_user_view.local_user),
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
||||
.await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
|
@ -88,23 +88,25 @@ pub async fn create_community(
|
||||
// When you create a community, make sure the user becomes a moderator and a follower
|
||||
let keypair = generate_actor_keypair()?;
|
||||
|
||||
let mut community_form = CommunityInsertForm::new(
|
||||
let community_form = CommunityInsertForm {
|
||||
description,
|
||||
icon,
|
||||
banner,
|
||||
nsfw: data.nsfw,
|
||||
actor_id: Some(community_actor_id.clone()),
|
||||
private_key: Some(keypair.private_key),
|
||||
followers_url: Some(generate_followers_url(&community_actor_id)?),
|
||||
inbox_url: Some(generate_inbox_url(&community_actor_id)?),
|
||||
shared_inbox_url: Some(generate_shared_inbox_url(context.settings())?),
|
||||
posting_restricted_to_mods: data.posting_restricted_to_mods,
|
||||
visibility: data.visibility,
|
||||
..CommunityInsertForm::new(
|
||||
site_view.site.instance_id,
|
||||
data.name.clone(),
|
||||
data.title.clone(),
|
||||
keypair.public_key,
|
||||
);
|
||||
community_form.description = description;
|
||||
community_form.icon = icon;
|
||||
community_form.banner = banner;
|
||||
community_form.nsfw = data.nsfw;
|
||||
community_form.actor_id = Some(community_actor_id.clone());
|
||||
community_form.private_key = Some(keypair.private_key);
|
||||
community_form.followers_url = Some(generate_followers_url(&community_actor_id)?);
|
||||
community_form.inbox_url = Some(generate_inbox_url(&community_actor_id)?);
|
||||
community_form.shared_inbox_url = Some(generate_shared_inbox_url(context.settings())?);
|
||||
community_form.posting_restricted_to_mods = data.posting_restricted_to_mods;
|
||||
community_form.visibility = data.visibility;
|
||||
)
|
||||
};
|
||||
|
||||
let inserted_community = Community::create(&mut context.pool(), &community_form)
|
||||
.await
|
||||
|
@ -51,9 +51,7 @@ pub async fn update_community(
|
||||
is_valid_body_field(desc, false)?;
|
||||
}
|
||||
|
||||
let old_community = Community::read(&mut context.pool(), data.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let old_community = Community::read(&mut context.pool(), data.community_id).await?;
|
||||
|
||||
let icon = diesel_url_update(data.icon.as_deref())?;
|
||||
replace_image(&icon, &old_community.icon, &context).await?;
|
||||
|
@ -7,7 +7,7 @@ use lemmy_db_schema::{
|
||||
utils::{diesel_required_string_update, diesel_required_url_update, naive_now},
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyError, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyError;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn update_oauth_provider(
|
||||
@ -37,8 +37,6 @@ pub async fn update_oauth_provider(
|
||||
|
||||
let update_result =
|
||||
OAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form).await?;
|
||||
let oauth_provider = OAuthProvider::read(&mut context.pool(), update_result.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindOauthProvider)?;
|
||||
let oauth_provider = OAuthProvider::read(&mut context.pool(), update_result.id).await?;
|
||||
Ok(Json(oauth_provider))
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use super::convert_published_time;
|
||||
use activitypub_federation::config::Data;
|
||||
use actix_web::web::Json;
|
||||
use lemmy_api_common::{
|
||||
@ -92,20 +93,15 @@ pub async fn create_post(
|
||||
.await?;
|
||||
|
||||
let community_id = data.community_id;
|
||||
let community = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), community_id).await?;
|
||||
if community.posting_restricted_to_mods {
|
||||
let community_id = data.community_id;
|
||||
let is_mod = CommunityModeratorView::is_community_moderator(
|
||||
CommunityModeratorView::check_is_community_moderator(
|
||||
&mut context.pool(),
|
||||
community_id,
|
||||
local_user_view.local_user.person_id,
|
||||
)
|
||||
.await?;
|
||||
if !is_mod {
|
||||
Err(LemmyErrorType::OnlyModsCanPostInCommunity)?
|
||||
}
|
||||
}
|
||||
|
||||
// Only need to check if language is allowed in case user set it explicitly. When using default
|
||||
@ -130,25 +126,36 @@ pub async fn create_post(
|
||||
}
|
||||
};
|
||||
|
||||
let mut post_form = PostInsertForm::new(
|
||||
let scheduled_publish_time =
|
||||
convert_published_time(data.scheduled_publish_time, &local_user_view, &context).await?;
|
||||
let post_form = PostInsertForm {
|
||||
url: url.map(Into::into),
|
||||
body,
|
||||
alt_text: data.alt_text.clone(),
|
||||
nsfw: data.nsfw,
|
||||
language_id,
|
||||
scheduled_publish_time,
|
||||
..PostInsertForm::new(
|
||||
data.name.trim().to_string(),
|
||||
local_user_view.person.id,
|
||||
data.community_id,
|
||||
);
|
||||
post_form.url = url.map(Into::into);
|
||||
post_form.body = body;
|
||||
post_form.alt_text = data.alt_text.clone();
|
||||
post_form.nsfw = data.nsfw;
|
||||
post_form.language_id = language_id;
|
||||
)
|
||||
};
|
||||
|
||||
let inserted_post = Post::create(&mut context.pool(), &post_form)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntCreatePost)?;
|
||||
|
||||
let federate_post = if scheduled_publish_time.is_none() {
|
||||
send_webmention(inserted_post.clone(), community);
|
||||
|post| Some(SendActivityData::CreatePost(post))
|
||||
} else {
|
||||
|_| None
|
||||
};
|
||||
generate_post_link_metadata(
|
||||
inserted_post.clone(),
|
||||
custom_thumbnail.map(Into::into),
|
||||
|post| Some(SendActivityData::CreatePost(post)),
|
||||
federate_post,
|
||||
context.reset_request_count(),
|
||||
)
|
||||
.await?;
|
||||
@ -168,11 +175,14 @@ pub async fn create_post(
|
||||
|
||||
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
|
||||
|
||||
if let Some(url) = inserted_post.url.clone() {
|
||||
build_post_response(&context, community_id, local_user_view, post_id).await
|
||||
}
|
||||
|
||||
pub fn send_webmention(post: Post, community: Community) {
|
||||
if let Some(url) = post.url.clone() {
|
||||
if community.visibility == CommunityVisibility::Public {
|
||||
spawn_try_task(async move {
|
||||
let mut webmention =
|
||||
Webmention::new::<Url>(inserted_post.ap_id.clone().into(), url.clone().into())?;
|
||||
let mut webmention = Webmention::new::<Url>(post.ap_id.clone().into(), url.clone().into())?;
|
||||
webmention.set_checked(true);
|
||||
match webmention
|
||||
.send()
|
||||
@ -186,6 +196,4 @@ pub async fn create_post(
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
build_post_response(&context, community_id, local_user_view, post_id).await
|
||||
}
|
||||
|
@ -21,9 +21,7 @@ pub async fn delete_post(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<PostResponse>> {
|
||||
let post_id = data.post_id;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
// Dont delete it if its already been deleted.
|
||||
if orig_post.deleted == data.deleted {
|
||||
|
@ -1,5 +1,38 @@
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use lemmy_api_common::context::LemmyContext;
|
||||
use lemmy_db_schema::source::post::Post;
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
|
||||
pub mod create;
|
||||
pub mod delete;
|
||||
pub mod read;
|
||||
pub mod remove;
|
||||
pub mod update;
|
||||
|
||||
async fn convert_published_time(
|
||||
scheduled_publish_time: Option<i64>,
|
||||
local_user_view: &LocalUserView,
|
||||
context: &LemmyContext,
|
||||
) -> LemmyResult<Option<DateTime<Utc>>> {
|
||||
const MAX_SCHEDULED_POSTS: i64 = 10;
|
||||
if let Some(scheduled_publish_time) = scheduled_publish_time {
|
||||
let converted = Utc
|
||||
.timestamp_opt(scheduled_publish_time, 0)
|
||||
.single()
|
||||
.ok_or(LemmyErrorType::InvalidUnixTime)?;
|
||||
if converted < Utc::now() {
|
||||
Err(LemmyErrorType::PostScheduleTimeMustBeInFuture)?;
|
||||
}
|
||||
if !local_user_view.local_user.admin {
|
||||
let count =
|
||||
Post::user_scheduled_post_count(local_user_view.person.id, &mut context.pool()).await?;
|
||||
if count >= MAX_SCHEDULED_POSTS {
|
||||
Err(LemmyErrorType::TooManyScheduledPosts)?;
|
||||
}
|
||||
}
|
||||
Ok(Some(converted))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
@ -33,16 +33,14 @@ pub async fn get_post(
|
||||
} else if let Some(comment_id) = data.comment_id {
|
||||
Comment::read(&mut context.pool(), comment_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?
|
||||
.post_id
|
||||
} else {
|
||||
Err(LemmyErrorType::CouldntFindPost)?
|
||||
Err(LemmyErrorType::NotFound)?
|
||||
};
|
||||
|
||||
// Check to see if the person is a mod or admin, to show deleted / removed
|
||||
let community_id = Post::read(&mut context.pool(), post_id)
|
||||
let community_id = Post::read_xx(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?
|
||||
.community_id;
|
||||
|
||||
let is_mod_or_admin = is_mod_or_admin_opt(
|
||||
@ -60,8 +58,7 @@ pub async fn get_post(
|
||||
local_user.as_ref(),
|
||||
is_mod_or_admin,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
.await?;
|
||||
|
||||
let post_id = post_view.post.id;
|
||||
if let Some(person_id) = person_id {
|
||||
@ -83,15 +80,15 @@ pub async fn get_post(
|
||||
local_user.as_ref(),
|
||||
is_mod_or_admin,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
||||
|
||||
// Fetch the cross_posts
|
||||
let cross_posts = if let Some(url) = &post_view.post.url {
|
||||
let mut x_posts = PostQuery {
|
||||
url_search: Some(url.inner().as_str().into()),
|
||||
url_only: Some(true),
|
||||
search_term: Some(url.inner().as_str().into()),
|
||||
local_user: local_user.as_ref(),
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use lemmy_db_schema::{
|
||||
traits::{Crud, Reportable},
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn remove_post(
|
||||
@ -26,9 +26,7 @@ pub async fn remove_post(
|
||||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<PostResponse>> {
|
||||
let post_id = data.post_id;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
check_community_mod_action(
|
||||
&local_user_view.person,
|
||||
|
@ -1,3 +1,4 @@
|
||||
use super::{convert_published_time, create::send_webmention};
|
||||
use activitypub_federation::config::Data;
|
||||
use actix_web::web::Json;
|
||||
use lemmy_api_common::{
|
||||
@ -16,6 +17,7 @@ use lemmy_api_common::{
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
actor_language::CommunityLanguage,
|
||||
community::Community,
|
||||
local_site::LocalSite,
|
||||
post::{Post, PostUpdateForm},
|
||||
},
|
||||
@ -85,9 +87,7 @@ pub async fn update_post(
|
||||
}
|
||||
|
||||
let post_id = data.post_id;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let orig_post = Post::read(&mut context.pool(), post_id).await?;
|
||||
|
||||
check_community_user_action(
|
||||
&local_user_view.person,
|
||||
@ -109,6 +109,21 @@ pub async fn update_post(
|
||||
)
|
||||
.await?;
|
||||
|
||||
// handle changes to scheduled_publish_time
|
||||
let scheduled_publish_time = match (
|
||||
orig_post.scheduled_publish_time,
|
||||
data.scheduled_publish_time,
|
||||
) {
|
||||
// schedule time can be changed if post is still scheduled (and not published yet)
|
||||
(Some(_), Some(_)) => {
|
||||
Some(convert_published_time(data.scheduled_publish_time, &local_user_view, &context).await?)
|
||||
}
|
||||
// post was scheduled, gets changed to publish immediately
|
||||
(Some(_), None) => Some(None),
|
||||
// unchanged
|
||||
(_, _) => None,
|
||||
};
|
||||
|
||||
let post_form = PostUpdateForm {
|
||||
name: data.name.clone(),
|
||||
url,
|
||||
@ -117,6 +132,7 @@ pub async fn update_post(
|
||||
nsfw: data.nsfw,
|
||||
language_id: data.language_id,
|
||||
updated: Some(Some(naive_now())),
|
||||
scheduled_publish_time,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@ -125,13 +141,36 @@ pub async fn update_post(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntUpdatePost)?;
|
||||
|
||||
// send out federation/webmention if necessary
|
||||
match (
|
||||
orig_post.scheduled_publish_time,
|
||||
data.scheduled_publish_time,
|
||||
) {
|
||||
// schedule was removed, send create activity and webmention
|
||||
(Some(_), None) => {
|
||||
let community = Community::read(&mut context.pool(), orig_post.community_id).await?;
|
||||
send_webmention(updated_post.clone(), community);
|
||||
generate_post_link_metadata(
|
||||
updated_post.clone(),
|
||||
custom_thumbnail.flatten().map(Into::into),
|
||||
|post| Some(SendActivityData::CreatePost(post)),
|
||||
context.reset_request_count(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
// post was already public, send update
|
||||
(None, _) => {
|
||||
generate_post_link_metadata(
|
||||
updated_post.clone(),
|
||||
custom_thumbnail.flatten().map(Into::into),
|
||||
|post| Some(SendActivityData::UpdatePost(post)),
|
||||
context.reset_request_count(),
|
||||
)
|
||||
.await?;
|
||||
.await?
|
||||
}
|
||||
// schedule was changed, do nothing
|
||||
(Some(_), Some(_)) => {}
|
||||
};
|
||||
|
||||
build_post_response(
|
||||
context.deref(),
|
||||
|
@ -5,7 +5,6 @@ use lemmy_api_common::{
|
||||
private_message::{CreatePrivateMessage, PrivateMessageResponse},
|
||||
send_activity::{ActivityChannel, SendActivityData},
|
||||
utils::{
|
||||
check_person_block,
|
||||
get_interface_language,
|
||||
get_url_blocklist,
|
||||
local_site_to_slur_regex,
|
||||
@ -16,6 +15,7 @@ use lemmy_api_common::{
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
local_site::LocalSite,
|
||||
person_block::PersonBlock,
|
||||
private_message::{PrivateMessage, PrivateMessageInsertForm},
|
||||
},
|
||||
traits::Crud,
|
||||
@ -39,10 +39,10 @@ pub async fn create_private_message(
|
||||
let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?;
|
||||
is_valid_body_field(&content, false)?;
|
||||
|
||||
check_person_block(
|
||||
local_user_view.person.id,
|
||||
data.recipient_id,
|
||||
PersonBlock::read(
|
||||
&mut context.pool(),
|
||||
data.recipient_id,
|
||||
local_user_view.person.id,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -56,16 +56,12 @@ pub async fn create_private_message(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?;
|
||||
|
||||
let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id).await?;
|
||||
|
||||
// Send email to the local recipient, if one exists
|
||||
if view.recipient.local {
|
||||
let recipient_id = data.recipient_id;
|
||||
let local_recipient = LocalUserView::read_person(&mut context.pool(), recipient_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let local_recipient = LocalUserView::read_person(&mut context.pool(), recipient_id).await?;
|
||||
let lang = get_interface_language(&local_recipient);
|
||||
let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
|
||||
let sender_name = &local_user_view.person.name;
|
||||
|
@ -20,9 +20,7 @@ pub async fn delete_private_message(
|
||||
) -> LemmyResult<Json<PrivateMessageResponse>> {
|
||||
// Checking permissions
|
||||
let private_message_id = data.private_message_id;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
||||
if local_user_view.person.id != orig_private_message.creator_id {
|
||||
Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
|
||||
}
|
||||
@ -47,9 +45,7 @@ pub async fn delete_private_message(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
|
||||
Ok(Json(PrivateMessageResponse {
|
||||
private_message_view: view,
|
||||
}))
|
||||
|
@ -30,9 +30,7 @@ pub async fn update_private_message(
|
||||
|
||||
// Checking permissions
|
||||
let private_message_id = data.private_message_id;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
||||
if local_user_view.person.id != orig_private_message.creator_id {
|
||||
Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
|
||||
}
|
||||
@ -56,9 +54,7 @@ pub async fn update_private_message(
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
|
||||
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPrivateMessage)?;
|
||||
let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
|
||||
|
||||
ActivityChannel::submit_activity(
|
||||
SendActivityData::UpdatePrivateMessage(view.clone()),
|
||||
|
@ -191,8 +191,6 @@ fn validate_create_payload(local_site: &LocalSite, create_site: &CreateSite) ->
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use crate::site::create::validate_create_payload;
|
||||
|
@ -48,8 +48,6 @@ fn not_zero(val: Option<i32>) -> Option<i32> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use crate::site::{application_question_check, not_zero, site_default_post_listing_type_check};
|
||||
|
@ -243,8 +243,6 @@ fn validate_update_payload(local_site: &LocalSite, edit_site: &EditSite) -> Lemm
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use crate::site::update::validate_update_payload;
|
||||
|
@ -92,9 +92,8 @@ pub async fn register(
|
||||
}
|
||||
|
||||
if local_site.site_setup && local_site.captcha_enabled {
|
||||
if let Some(captcha_uuid) = &data.captcha_uuid {
|
||||
let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
|
||||
let check = CaptchaAnswer::check_captcha(
|
||||
let uuid = uuid::Uuid::parse_str(&data.captcha_uuid.clone().unwrap_or_default())?;
|
||||
CaptchaAnswer::check_captcha(
|
||||
&mut context.pool(),
|
||||
CheckCaptchaAnswer {
|
||||
uuid,
|
||||
@ -102,26 +101,16 @@ pub async fn register(
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if !check {
|
||||
Err(LemmyErrorType::CaptchaIncorrect)?
|
||||
}
|
||||
} else {
|
||||
Err(LemmyErrorType::CaptchaIncorrect)?
|
||||
}
|
||||
}
|
||||
|
||||
let slur_regex = local_site_to_slur_regex(&local_site);
|
||||
check_slurs(&data.username, &slur_regex)?;
|
||||
check_slurs_opt(&data.answer, &slur_regex)?;
|
||||
|
||||
if Person::is_username_taken(&mut context.pool(), &data.username).await? {
|
||||
return Err(LemmyErrorType::UsernameAlreadyExists)?;
|
||||
}
|
||||
Person::check_username_taken(&mut context.pool(), &data.username).await?;
|
||||
|
||||
if let Some(email) = &data.email {
|
||||
if LocalUser::is_email_taken(&mut context.pool(), email).await? {
|
||||
Err(LemmyErrorType::EmailAlreadyExists)?
|
||||
}
|
||||
LocalUser::check_is_email_taken(&mut context.pool(), email).await?;
|
||||
}
|
||||
|
||||
// We have to create both a person, and local_user
|
||||
@ -234,7 +223,6 @@ pub async fn authenticate_with_oauth(
|
||||
let oauth_provider = OAuthProvider::read(&mut context.pool(), oauth_provider_id)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.ok_or(LemmyErrorType::OauthAuthorizationInvalid)?;
|
||||
|
||||
if !oauth_provider.enabled {
|
||||
@ -262,10 +250,10 @@ pub async fn authenticate_with_oauth(
|
||||
|
||||
// Lookup user by oauth_user_id
|
||||
let mut local_user_view =
|
||||
LocalUserView::find_by_oauth_id(&mut context.pool(), oauth_provider.id, &oauth_user_id).await?;
|
||||
LocalUserView::find_by_oauth_id(&mut context.pool(), oauth_provider.id, &oauth_user_id).await;
|
||||
|
||||
let local_user: LocalUser;
|
||||
if let Some(user_view) = local_user_view {
|
||||
if let Ok(user_view) = local_user_view {
|
||||
// user found by oauth_user_id => Login user
|
||||
local_user = user_view.clone().local_user;
|
||||
|
||||
@ -292,10 +280,10 @@ pub async fn authenticate_with_oauth(
|
||||
local_site.registration_mode == RegistrationMode::RequireApplication;
|
||||
|
||||
// Lookup user by OAUTH email and link accounts
|
||||
local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email).await?;
|
||||
local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email).await;
|
||||
|
||||
let person;
|
||||
if let Some(user_view) = local_user_view {
|
||||
if let Ok(user_view) = local_user_view {
|
||||
// user found by email => link and login if linking is allowed
|
||||
|
||||
// we only allow linking by email when email_verification is required otherwise emails cannot
|
||||
@ -339,9 +327,7 @@ pub async fn authenticate_with_oauth(
|
||||
check_slurs(username, &slur_regex)?;
|
||||
check_slurs_opt(&data.answer, &slur_regex)?;
|
||||
|
||||
if Person::is_username_taken(&mut context.pool(), username).await? {
|
||||
return Err(LemmyErrorType::UsernameAlreadyExists)?;
|
||||
}
|
||||
Person::check_username_taken(&mut context.pool(), username).await?;
|
||||
|
||||
// We have to create a person, a local_user, and an oauth_account
|
||||
person = create_person(
|
||||
|
@ -22,10 +22,7 @@ use lemmy_db_schema::{
|
||||
traits::Crud,
|
||||
utils::DbPool,
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use serde::Deserialize;
|
||||
use url::Url;
|
||||
|
||||
@ -182,7 +179,6 @@ pub(crate) async fn send_ban_from_community(
|
||||
) -> LemmyResult<()> {
|
||||
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
let expires = check_expire_time(data.expires)?;
|
||||
|
||||
|
@ -213,14 +213,12 @@ async fn can_accept_activity_in_community(
|
||||
context: &Data<LemmyContext>,
|
||||
) -> LemmyResult<()> {
|
||||
if let Some(community) = community {
|
||||
if !community.local
|
||||
&& !CommunityFollower::has_local_followers(&mut context.pool(), community.id).await?
|
||||
{
|
||||
Err(LemmyErrorType::CommunityHasNoFollowers)?
|
||||
}
|
||||
// Local only community can't federate
|
||||
if community.visibility != CommunityVisibility::Public {
|
||||
return Err(LemmyErrorType::CouldntFindCommunity.into());
|
||||
return Err(LemmyErrorType::NotFound.into());
|
||||
}
|
||||
if !community.local {
|
||||
CommunityFollower::check_has_local_followers(&mut context.pool(), community.id).await?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -36,10 +36,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::{Crud, Joinable},
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use url::Url;
|
||||
|
||||
impl CollectionAdd {
|
||||
@ -129,9 +126,7 @@ impl ActivityHandler for CollectionAdd {
|
||||
async fn receive(self, context: &Data<Self::DataType>) -> LemmyResult<()> {
|
||||
insert_received_activity(&self.id, context).await?;
|
||||
let (community, collection_type) =
|
||||
Community::get_by_collection_url(&mut context.pool(), &self.target.into())
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?;
|
||||
match collection_type {
|
||||
CollectionType::Moderators => {
|
||||
let new_mod = ObjectId::<ApubPerson>::from(self.object)
|
||||
@ -188,11 +183,9 @@ pub(crate) async fn send_add_mod_to_community(
|
||||
let actor: ApubPerson = actor.into();
|
||||
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
let updated_mod: ApubPerson = Person::read(&mut context.pool(), updated_mod_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?
|
||||
.into();
|
||||
if added {
|
||||
CollectionAdd::send_add_mod(&community, &updated_mod, &actor, &context).await
|
||||
@ -211,7 +204,6 @@ pub(crate) async fn send_feature_post(
|
||||
let post: ApubPost = post.into();
|
||||
let community = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
if featured {
|
||||
CollectionAdd::send_add_featured_post(&community, &post, &actor, &context).await
|
||||
|
@ -31,10 +31,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::{Crud, Joinable},
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use url::Url;
|
||||
|
||||
impl CollectionRemove {
|
||||
@ -124,9 +121,7 @@ impl ActivityHandler for CollectionRemove {
|
||||
async fn receive(self, context: &Data<Self::DataType>) -> LemmyResult<()> {
|
||||
insert_received_activity(&self.id, context).await?;
|
||||
let (community, collection_type) =
|
||||
Community::get_by_collection_url(&mut context.pool(), &self.target.into())
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?;
|
||||
match collection_type {
|
||||
CollectionType::Moderators => {
|
||||
let remove_mod = ObjectId::<ApubPerson>::from(self.object)
|
||||
|
@ -32,10 +32,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use url::Url;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@ -132,7 +129,6 @@ pub(crate) async fn send_lock_post(
|
||||
) -> LemmyResult<()> {
|
||||
let community: ApubCommunity = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
let id = generate_activity_id(
|
||||
LockType::Lock,
|
||||
|
@ -29,10 +29,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::{Crud, Reportable},
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use url::Url;
|
||||
|
||||
impl Report {
|
||||
@ -70,9 +67,7 @@ impl Report {
|
||||
PostOrComment::Post(p) => p.creator_id,
|
||||
PostOrComment::Comment(c) => c.creator_id,
|
||||
};
|
||||
let object_creator = Person::read(&mut context.pool(), object_creator_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let object_creator = Person::read(&mut context.pool(), object_creator_id).await?;
|
||||
let object_creator_site: Option<ApubSite> =
|
||||
Site::read_from_instance_id(&mut context.pool(), object_creator.instance_id)
|
||||
.await?
|
||||
|
@ -42,7 +42,6 @@ use lemmy_db_schema::{
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
utils::mention::scrape_text_for_mentions,
|
||||
LemmyErrorType,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
@ -56,17 +55,11 @@ impl CreateOrUpdateNote {
|
||||
) -> LemmyResult<()> {
|
||||
// TODO: might be helpful to add a comment method to retrieve community directly
|
||||
let post_id = comment.post_id;
|
||||
let post = Post::read(&mut context.pool(), post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let post = Post::read(&mut context.pool(), post_id).await?;
|
||||
let community_id = post.community_id;
|
||||
let person: ApubPerson = Person::read(&mut context.pool(), person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?
|
||||
.into();
|
||||
let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
|
||||
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
|
||||
let id = generate_activity_id(
|
||||
|
@ -32,7 +32,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::{Crud, Likeable},
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use url::Url;
|
||||
|
||||
impl CreateOrUpdatePage {
|
||||
@ -66,13 +66,9 @@ impl CreateOrUpdatePage {
|
||||
context: Data<LemmyContext>,
|
||||
) -> LemmyResult<()> {
|
||||
let community_id = post.community_id;
|
||||
let person: ApubPerson = Person::read(&mut context.pool(), person_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?
|
||||
.into();
|
||||
let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
|
||||
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into();
|
||||
|
||||
let create_or_update =
|
||||
|
@ -176,8 +176,8 @@ pub(in crate::activities) async fn receive_remove_action(
|
||||
.await?;
|
||||
}
|
||||
// TODO these need to be implemented yet, for now, return errors
|
||||
DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::CouldntFindPrivateMessage)?,
|
||||
DeletableObjects::Person(_) => Err(LemmyErrorType::CouldntFindPerson)?,
|
||||
DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::NotFound)?,
|
||||
DeletableObjects::Person(_) => Err(LemmyErrorType::NotFound)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ use lemmy_db_schema::{
|
||||
},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
use std::ops::Deref;
|
||||
use url::Url;
|
||||
|
||||
@ -87,7 +87,6 @@ pub(crate) async fn send_apub_delete_private_message(
|
||||
let recipient_id = pm.recipient_id;
|
||||
let recipient: ApubPerson = Person::read(&mut context.pool(), recipient_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?
|
||||
.into();
|
||||
|
||||
let deletable = DeletableObjects::PrivateMessage(pm.into());
|
||||
|
@ -156,8 +156,8 @@ impl UndoDelete {
|
||||
.await?;
|
||||
}
|
||||
// TODO these need to be implemented yet, for now, return errors
|
||||
DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::CouldntFindPrivateMessage)?,
|
||||
DeletableObjects::Person(_) => Err(LemmyErrorType::CouldntFindPerson)?,
|
||||
DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::NotFound)?,
|
||||
DeletableObjects::Person(_) => Err(LemmyErrorType::NotFound)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ impl ActivityHandler for Follow {
|
||||
UserOrCommunity::Community(c) => {
|
||||
// Dont allow following local-only community via federation.
|
||||
if c.visibility != CommunityVisibility::Public {
|
||||
return Err(LemmyErrorType::CouldntFindCommunity.into());
|
||||
return Err(LemmyErrorType::NotFound.into());
|
||||
}
|
||||
let form = CommunityFollowerForm {
|
||||
community_id: c.id,
|
||||
|
@ -87,12 +87,7 @@ pub(crate) async fn verify_person_in_community(
|
||||
}
|
||||
let person_id = person.id;
|
||||
let community_id = community.id;
|
||||
let is_banned = CommunityPersonBanView::get(&mut context.pool(), person_id, community_id).await?;
|
||||
if is_banned {
|
||||
Err(LemmyErrorType::PersonIsBannedFromCommunity)?
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
CommunityPersonBanView::check(&mut context.pool(), person_id, community_id).await
|
||||
}
|
||||
|
||||
/// Verify that mod action in community was performed by a moderator.
|
||||
@ -106,14 +101,6 @@ pub(crate) async fn verify_mod_action(
|
||||
community: &Community,
|
||||
context: &Data<LemmyContext>,
|
||||
) -> LemmyResult<()> {
|
||||
let mod_ = mod_id.dereference(context).await?;
|
||||
|
||||
let is_mod_or_admin =
|
||||
CommunityView::is_mod_or_admin(&mut context.pool(), mod_.id, community.id).await?;
|
||||
if is_mod_or_admin {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// mod action comes from the same instance as the community, so it was presumably done
|
||||
// by an instance admin.
|
||||
// TODO: federate instance admin status and check it here
|
||||
@ -121,7 +108,8 @@ pub(crate) async fn verify_mod_action(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(LemmyErrorType::NotAModerator)?
|
||||
let mod_ = mod_id.dereference(context).await?;
|
||||
CommunityView::check_is_mod_or_admin(&mut context.pool(), mod_.id, community.id).await
|
||||
}
|
||||
|
||||
pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> LemmyResult<()> {
|
||||
@ -245,9 +233,7 @@ pub async fn match_outgoing_activities(
|
||||
CreateOrUpdatePage::send(post, creator_id, CreateOrUpdateType::Update, context).await
|
||||
}
|
||||
DeletePost(post, person, data) => {
|
||||
let community = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), post.community_id).await?;
|
||||
send_apub_delete_in_community(
|
||||
person,
|
||||
community,
|
||||
@ -264,9 +250,7 @@ pub async fn match_outgoing_activities(
|
||||
reason,
|
||||
removed,
|
||||
} => {
|
||||
let community = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let community = Community::read(&mut context.pool(), post.community_id).await?;
|
||||
send_apub_delete_in_community(
|
||||
moderator,
|
||||
community,
|
||||
|
@ -117,13 +117,12 @@ impl InCommunity for AnnouncableActivities {
|
||||
CollectionRemove(a) => a.community(context).await,
|
||||
LockPost(a) => a.community(context).await,
|
||||
UndoLockPost(a) => a.community(context).await,
|
||||
Page(_) => Err(LemmyErrorType::CouldntFindPost.into()),
|
||||
Page(_) => Err(LemmyErrorType::NotFound.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use crate::{
|
||||
|
@ -12,10 +12,13 @@ use lemmy_api_common::{
|
||||
utils::check_private_instance,
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{comment::Comment, community::Community, local_site::LocalSite},
|
||||
source::{comment::Comment, community::Community},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
|
||||
use lemmy_db_views::{
|
||||
comment_view::CommentQuery,
|
||||
structs::{LocalUserView, SiteView},
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult};
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
@ -24,8 +27,8 @@ pub async fn list_comments(
|
||||
context: Data<LemmyContext>,
|
||||
local_user_view: Option<LocalUserView>,
|
||||
) -> LemmyResult<Json<GetCommentsResponse>> {
|
||||
let local_site = LocalSite::read(&mut context.pool()).await?;
|
||||
check_private_instance(&local_user_view, &local_site)?;
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
check_private_instance(&local_user_view, &site_view.local_site)?;
|
||||
|
||||
let community_id = if let Some(name) = &data.community_name {
|
||||
Some(
|
||||
@ -40,7 +43,7 @@ pub async fn list_comments(
|
||||
let sort = Some(comment_sort_type_with_default(
|
||||
data.sort,
|
||||
local_user_ref,
|
||||
&local_site,
|
||||
&site_view.local_site,
|
||||
));
|
||||
let max_depth = data.max_depth;
|
||||
let saved_only = data.saved_only;
|
||||
@ -58,18 +61,13 @@ pub async fn list_comments(
|
||||
let listing_type = Some(listing_type_with_default(
|
||||
data.type_,
|
||||
local_user_view.as_ref().map(|u| &u.local_user),
|
||||
&local_site,
|
||||
&site_view.local_site,
|
||||
community_id,
|
||||
));
|
||||
|
||||
// If a parent_id is given, fetch the comment to get the path
|
||||
let parent_path = if let Some(parent_id) = parent_id {
|
||||
Some(
|
||||
Comment::read(&mut context.pool(), parent_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?
|
||||
.path,
|
||||
)
|
||||
Some(Comment::read(&mut context.pool(), parent_id).await?.path)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@ -93,7 +91,7 @@ pub async fn list_comments(
|
||||
limit,
|
||||
..Default::default()
|
||||
}
|
||||
.list(&mut context.pool())
|
||||
.list(&site_view.site, &mut context.pool())
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
|
||||
|
||||
|
@ -8,14 +8,14 @@ use actix_web::web::{Json, Query};
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
post::{GetPosts, GetPostsResponse},
|
||||
utils::check_private_instance,
|
||||
utils::{check_conflicting_like_filters, check_private_instance},
|
||||
};
|
||||
use lemmy_db_schema::source::community::Community;
|
||||
use lemmy_db_views::{
|
||||
post_view::PostQuery,
|
||||
structs::{LocalUserView, PaginationCursor, SiteView},
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult};
|
||||
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn list_posts(
|
||||
@ -45,9 +45,7 @@ pub async fn list_posts(
|
||||
|
||||
let liked_only = data.liked_only;
|
||||
let disliked_only = data.disliked_only;
|
||||
if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() {
|
||||
return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
|
||||
}
|
||||
check_conflicting_like_filters(liked_only, disliked_only)?;
|
||||
|
||||
let local_user = local_user_view.as_ref().map(|u| &u.local_user);
|
||||
let listing_type = Some(listing_type_with_default(
|
||||
|
@ -13,7 +13,7 @@ use lemmy_db_schema::source::{
|
||||
};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
|
||||
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorExt2, LemmyErrorType, LemmyResult};
|
||||
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn get_community(
|
||||
@ -36,8 +36,7 @@ pub async fn get_community(
|
||||
None => {
|
||||
let name = data.name.clone().unwrap_or_else(|| "main".to_string());
|
||||
resolve_actor_identifier::<ApubCommunity, Community>(&name, &context, &local_user_view, true)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?
|
||||
.await?
|
||||
.id
|
||||
}
|
||||
};
|
||||
@ -56,12 +55,9 @@ pub async fn get_community(
|
||||
local_user,
|
||||
is_mod_or_admin,
|
||||
)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.await?;
|
||||
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
||||
|
||||
let site = read_site_for_actor(community_view.community.actor_id.clone(), &context).await?;
|
||||
|
||||
|
@ -13,7 +13,7 @@ use lemmy_db_views::{
|
||||
structs::{LocalUserView, SiteView},
|
||||
};
|
||||
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView};
|
||||
use lemmy_utils::error::{LemmyErrorExt2, LemmyErrorType, LemmyResult};
|
||||
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn read_person(
|
||||
@ -35,20 +35,17 @@ pub async fn read_person(
|
||||
None => {
|
||||
if let Some(username) = &data.username {
|
||||
resolve_actor_identifier::<ApubPerson, Person>(username, &context, &local_user_view, true)
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindPerson)?
|
||||
.await?
|
||||
.id
|
||||
} else {
|
||||
Err(LemmyErrorType::CouldntFindPerson)?
|
||||
Err(LemmyErrorType::NotFound)?
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 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?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
||||
let person_view = PersonView::read(&mut context.pool(), person_details_id).await?;
|
||||
|
||||
let sort = data.sort;
|
||||
let page = data.page;
|
||||
@ -88,7 +85,7 @@ pub async fn read_person(
|
||||
creator_id,
|
||||
..Default::default()
|
||||
}
|
||||
.list(&mut context.pool())
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
|
||||
let moderates = CommunityModeratorView::for_person(
|
||||
|
@ -34,11 +34,11 @@ pub async fn resolve_object(
|
||||
// user isn't authenticated only allow a local search.
|
||||
search_query_to_object_id_local(&data.q, &context).await
|
||||
}
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindObject)?;
|
||||
.with_lemmy_type(LemmyErrorType::NotFound)?;
|
||||
|
||||
convert_response(res, local_user_view, &mut context.pool())
|
||||
.await
|
||||
.with_lemmy_type(LemmyErrorType::CouldntFindObject)
|
||||
.with_lemmy_type(LemmyErrorType::NotFound)
|
||||
}
|
||||
|
||||
async fn convert_response(
|
||||
@ -54,36 +54,20 @@ async fn convert_response(
|
||||
match object {
|
||||
Post(p) => {
|
||||
removed_or_deleted = p.deleted || p.removed;
|
||||
res.post = Some(
|
||||
PostView::read(pool, p.id, local_user.as_ref(), false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?,
|
||||
)
|
||||
res.post = Some(PostView::read(pool, p.id, local_user.as_ref(), false).await?)
|
||||
}
|
||||
Comment(c) => {
|
||||
removed_or_deleted = c.deleted || c.removed;
|
||||
res.comment = Some(
|
||||
CommentView::read(pool, c.id, local_user.as_ref())
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?,
|
||||
)
|
||||
res.comment = Some(CommentView::read(pool, c.id, local_user.as_ref()).await?)
|
||||
}
|
||||
PersonOrCommunity(p) => match *p {
|
||||
UserOrCommunity::User(u) => {
|
||||
removed_or_deleted = u.deleted;
|
||||
res.person = Some(
|
||||
PersonView::read(pool, u.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPerson)?,
|
||||
)
|
||||
res.person = Some(PersonView::read(pool, u.id).await?)
|
||||
}
|
||||
UserOrCommunity::Community(c) => {
|
||||
removed_or_deleted = c.deleted || c.removed;
|
||||
res.community = Some(
|
||||
CommunityView::read(pool, c.id, local_user.as_ref(), false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?,
|
||||
)
|
||||
res.community = Some(CommunityView::read(pool, c.id, local_user.as_ref(), false).await?)
|
||||
}
|
||||
},
|
||||
};
|
||||
|
@ -4,7 +4,7 @@ use actix_web::web::{Json, Query};
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
site::{Search, SearchResponse},
|
||||
utils::{check_private_instance, is_admin},
|
||||
utils::{check_conflicting_like_filters, check_private_instance, is_admin},
|
||||
};
|
||||
use lemmy_db_schema::{source::community::Community, utils::post_to_comment_sort_type, SearchType};
|
||||
use lemmy_db_views::{
|
||||
@ -37,67 +37,88 @@ pub async fn search(
|
||||
|
||||
// TODO no clean / non-nsfw searching rn
|
||||
|
||||
let q = data.q.clone();
|
||||
let page = data.page;
|
||||
let limit = data.limit;
|
||||
let sort = data.sort;
|
||||
let listing_type = data.listing_type;
|
||||
let search_type = data.type_.unwrap_or(SearchType::All);
|
||||
let community_id = if let Some(name) = &data.community_name {
|
||||
let Query(Search {
|
||||
q,
|
||||
community_id,
|
||||
community_name,
|
||||
creator_id,
|
||||
type_,
|
||||
sort,
|
||||
listing_type,
|
||||
page,
|
||||
limit,
|
||||
title_only,
|
||||
post_url_only,
|
||||
saved_only,
|
||||
liked_only,
|
||||
disliked_only,
|
||||
}) = data;
|
||||
|
||||
let q = q.clone();
|
||||
let search_type = type_.unwrap_or(SearchType::All);
|
||||
let community_id = if let Some(name) = &community_name {
|
||||
Some(
|
||||
resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
|
||||
.await?,
|
||||
)
|
||||
.map(|c| c.id)
|
||||
} else {
|
||||
data.community_id
|
||||
community_id
|
||||
};
|
||||
let creator_id = data.creator_id;
|
||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
||||
let post_title_only = data.post_title_only;
|
||||
|
||||
check_conflicting_like_filters(liked_only, disliked_only)?;
|
||||
|
||||
let posts_query = PostQuery {
|
||||
sort: (sort),
|
||||
listing_type: (listing_type),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
sort,
|
||||
listing_type,
|
||||
community_id,
|
||||
creator_id,
|
||||
local_user,
|
||||
search_term: (Some(q.clone())),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
title_only: (post_title_only),
|
||||
search_term: Some(q.clone()),
|
||||
page,
|
||||
limit,
|
||||
title_only,
|
||||
url_only: post_url_only,
|
||||
liked_only,
|
||||
disliked_only,
|
||||
saved_only,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let comment_query = CommentQuery {
|
||||
sort: (sort.map(post_to_comment_sort_type)),
|
||||
listing_type: (listing_type),
|
||||
search_term: (Some(q.clone())),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
sort: sort.map(post_to_comment_sort_type),
|
||||
listing_type,
|
||||
search_term: Some(q.clone()),
|
||||
community_id,
|
||||
creator_id,
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
page,
|
||||
limit,
|
||||
liked_only,
|
||||
disliked_only,
|
||||
saved_only,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let community_query = CommunityQuery {
|
||||
sort: (sort),
|
||||
listing_type: (listing_type),
|
||||
search_term: (Some(q.clone())),
|
||||
sort,
|
||||
listing_type,
|
||||
search_term: Some(q.clone()),
|
||||
title_only,
|
||||
local_user,
|
||||
is_mod_or_admin: (is_admin),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
is_mod_or_admin: is_admin,
|
||||
page,
|
||||
limit,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let person_query = PersonQuery {
|
||||
sort,
|
||||
search_term: (Some(q.clone())),
|
||||
listing_type: (listing_type),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
search_term: Some(q.clone()),
|
||||
listing_type,
|
||||
page,
|
||||
limit,
|
||||
};
|
||||
|
||||
match search_type {
|
||||
@ -107,7 +128,9 @@ pub async fn search(
|
||||
.await?;
|
||||
}
|
||||
SearchType::Comments => {
|
||||
comments = comment_query.list(&mut context.pool()).await?;
|
||||
comments = comment_query
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
}
|
||||
SearchType::Communities => {
|
||||
communities = community_query
|
||||
@ -120,13 +143,15 @@ pub async fn search(
|
||||
SearchType::All => {
|
||||
// If the community or creator is included, dont search communities or users
|
||||
let community_or_creator_included =
|
||||
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
|
||||
community_id.is_some() || community_name.is_some() || creator_id.is_some();
|
||||
|
||||
posts = posts_query
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
|
||||
comments = comment_query.list(&mut context.pool()).await?;
|
||||
comments = comment_query
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
|
||||
communities = if community_or_creator_included {
|
||||
vec![]
|
||||
@ -142,21 +167,6 @@ pub async fn search(
|
||||
person_query.list(&mut context.pool()).await?
|
||||
};
|
||||
}
|
||||
SearchType::Url => {
|
||||
posts = PostQuery {
|
||||
sort: (sort),
|
||||
listing_type: (listing_type),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
url_search: (Some(q)),
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
..Default::default()
|
||||
}
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
|
||||
// Return the jwt
|
||||
|
@ -127,7 +127,6 @@ pub async fn import_settings(
|
||||
show_read_posts: data.settings.as_ref().map(|s| s.show_read_posts),
|
||||
open_links_in_new_tab: data.settings.as_ref().map(|s| s.open_links_in_new_tab),
|
||||
blur_nsfw: data.settings.as_ref().map(|s| s.blur_nsfw),
|
||||
auto_expand: data.settings.as_ref().map(|s| s.auto_expand),
|
||||
infinite_scroll_enabled: data.settings.as_ref().map(|s| s.infinite_scroll_enabled),
|
||||
post_listing_mode: data.settings.as_ref().map(|s| s.post_listing_mode),
|
||||
..Default::default()
|
||||
@ -308,8 +307,9 @@ where
|
||||
});
|
||||
Ok(failed_items.into_iter().join(","))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use crate::api::user_settings_backup::{export_settings, import_settings, UserSettingsBackup};
|
||||
@ -348,11 +348,7 @@ mod tests {
|
||||
let user_form = LocalUserInsertForm::test_form(person.id);
|
||||
let local_user = LocalUser::create(&mut context.pool(), &user_form, vec![]).await?;
|
||||
|
||||
Ok(
|
||||
LocalUserView::read(&mut context.pool(), local_user.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindLocalUser)?,
|
||||
)
|
||||
Ok(LocalUserView::read(&mut context.pool(), local_user.id).await?)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@ -386,9 +382,8 @@ mod tests {
|
||||
// wait for background task to finish
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
|
||||
let import_user_updated = LocalUserView::read(&mut context.pool(), import_user.local_user.id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindLocalUser)?;
|
||||
let import_user_updated =
|
||||
LocalUserView::read(&mut context.pool(), import_user.local_user.id).await?;
|
||||
|
||||
assert_eq!(
|
||||
export_user.person.display_name,
|
||||
|
@ -98,7 +98,7 @@ impl Collection for ApubCommunityModerators {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
@ -12,10 +12,7 @@ use lemmy_db_schema::{
|
||||
source::{community::Community, post::Post},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::{LemmyError, LemmyResult},
|
||||
LemmyErrorType,
|
||||
};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
use serde::Deserialize;
|
||||
use url::Url;
|
||||
|
||||
@ -97,15 +94,9 @@ impl InCommunity for PostOrComment {
|
||||
PostOrComment::Comment(c) => {
|
||||
Post::read(&mut context.pool(), c.post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?
|
||||
.community_id
|
||||
}
|
||||
};
|
||||
Ok(
|
||||
Community::read(&mut context.pool(), cid)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.into(),
|
||||
)
|
||||
Ok(Community::read(&mut context.pool(), cid).await?.into())
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ use lemmy_db_schema::{
|
||||
source::{comment::Comment, community::Community, post::Post},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -31,16 +31,9 @@ pub(crate) async fn get_apub_comment(
|
||||
) -> LemmyResult<HttpResponse> {
|
||||
let id = CommentId(info.comment_id.parse::<i32>()?);
|
||||
// Can't use CommentView here because it excludes deleted/removed/local-only items
|
||||
let comment: ApubComment = Comment::read(&mut context.pool(), id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindComment)?
|
||||
.into();
|
||||
let post = Post::read(&mut context.pool(), comment.post_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
||||
let community = Community::read(&mut context.pool(), post.community_id)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
let comment: ApubComment = Comment::read(&mut context.pool(), id).await?.into();
|
||||
let post = Post::read(&mut context.pool(), comment.post_id).await?;
|
||||
let community = Community::read(&mut context.pool(), post.community_id).await?;
|
||||
check_community_public(&community)?;
|
||||
|
||||
if !comment.local {
|
||||
|
@ -35,7 +35,7 @@ pub(crate) async fn get_apub_community_http(
|
||||
let community: ApubCommunity =
|
||||
Community::read_from_name(&mut context.pool(), &info.community_name, true)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.ok_or(LemmyErrorType::NotFound)?
|
||||
.into();
|
||||
|
||||
if community.deleted || community.removed {
|
||||
@ -67,7 +67,7 @@ pub(crate) async fn get_apub_community_followers(
|
||||
) -> LemmyResult<HttpResponse> {
|
||||
let community = Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
.ok_or(LemmyErrorType::NotFound)?;
|
||||
check_community_public(&community)?;
|
||||
let followers = ApubCommunityFollower::read_local(&community.into(), &context).await?;
|
||||
create_apub_response(&followers)
|
||||
@ -82,7 +82,7 @@ pub(crate) async fn get_apub_community_outbox(
|
||||
let community: ApubCommunity =
|
||||
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.ok_or(LemmyErrorType::NotFound)?
|
||||
.into();
|
||||
check_community_public(&community)?;
|
||||
let outbox = ApubCommunityOutbox::read_local(&community, &context).await?;
|
||||
@ -97,7 +97,7 @@ pub(crate) async fn get_apub_community_moderators(
|
||||
let community: ApubCommunity =
|
||||
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.ok_or(LemmyErrorType::NotFound)?
|
||||
.into();
|
||||
check_community_public(&community)?;
|
||||
let moderators = ApubCommunityModerators::read_local(&community, &context).await?;
|
||||
@ -112,7 +112,7 @@ pub(crate) async fn get_apub_community_featured(
|
||||
let community: ApubCommunity =
|
||||
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.ok_or(LemmyErrorType::NotFound)?
|
||||
.into();
|
||||
check_community_public(&community)?;
|
||||
let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
|
||||
@ -120,8 +120,7 @@ pub(crate) async fn get_apub_community_featured(
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
pub(crate) mod tests {
|
||||
|
||||
use super::*;
|
||||
@ -151,14 +150,16 @@ pub(crate) mod tests {
|
||||
Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
|
||||
create_local_site(context, instance.id).await?;
|
||||
|
||||
let mut community_form = CommunityInsertForm::new(
|
||||
let community_form = CommunityInsertForm {
|
||||
deleted: Some(deleted),
|
||||
visibility: Some(visibility),
|
||||
..CommunityInsertForm::new(
|
||||
instance.id,
|
||||
"testcom6".to_string(),
|
||||
"nada".to_owned(),
|
||||
"pubkey".to_string(),
|
||||
);
|
||||
community_form.deleted = Some(deleted);
|
||||
community_form.visibility = Some(visibility);
|
||||
)
|
||||
};
|
||||
let community = Community::create(&mut context.pool(), &community_form).await?;
|
||||
Ok((instance, community))
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user