Skip to content

Commit

Permalink
Merge branch 'LemmyNet:main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Sep 23, 2024
2 parents 1a84cae + 62e1790 commit 635ee99
Show file tree
Hide file tree
Showing 134 changed files with 504 additions and 880 deletions.
2 changes: 1 addition & 1 deletion api_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"eslint": "^9.8.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",
Expand Down
10 changes: 5 additions & 5 deletions api_tests/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions api_tests/src/comment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
);
});

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions api_tests/src/community.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
);
});

Expand Down
8 changes: 3 additions & 5 deletions api_tests/src/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions api_tests/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/comment/distinguish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/comment/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions crates/api/src/comment/list_comment_likes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions crates/api/src/comment/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions crates/api/src/comment_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions crates/api/src/comment_report/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ 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
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions crates/api/src/community/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/community/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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?;

Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/community/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ 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)
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/add_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/local_user/ban_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
}

Expand All @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/local_user/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/local_user/change_password_after_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
Loading

0 comments on commit 635ee99

Please sign in to comment.