Skip to content

Commit

Permalink
Adding a post_view mode. Fixes #3730 (#3731)
Browse files Browse the repository at this point in the history
* Adding a post_view mode. Fixes #3730

* Fix test.

* Addressing PR comments.

* Adding a post_view mode. Fixes #3730

* Fix test.

* Addressing PR comments.

* Fixing column order.

* Fix default Ok returns.

* Removing return Err(... where feasible.
  • Loading branch information
dessalines authored Aug 31, 2023
1 parent fed6542 commit 56e26fc
Show file tree
Hide file tree
Showing 53 changed files with 199 additions and 156 deletions.
2 changes: 1 addition & 1 deletion crates/api/src/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn add_mod_to_community(
is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
let community = Community::read(&mut context.pool(), community_id).await?;
if local_user_view.local_user.admin && !community.local {
return Err(LemmyErrorType::NotAModerator)?;
Err(LemmyErrorType::NotAModerator)?
}

// Update in local database
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/community/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Perform for TransferCommunity {
if !(is_top_mod(&local_user_view, &community_mods).is_ok()
|| is_admin(&local_user_view).is_ok())
{
return Err(LemmyErrorType::NotAnAdmin)?;
Err(LemmyErrorType::NotAnAdmin)?
}

// You have to re-do the community_moderator table, reordering it.
Expand Down
36 changes: 18 additions & 18 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, LemmyEr
if let Some(samples16) = samples.as_sixteen() {
concat_samples.extend(samples16);
} else {
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?;
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?
}
}

// Encode the concatenated result as a wav file
let mut output_buffer = Cursor::new(vec![]);
let header = match any_header {
Some(header) => header,
None => return Err(LemmyErrorType::CouldntCreateAudioCaptcha)?,
};
wav::write(
header,
&wav::BitDepth::Sixteen(concat_samples),
&mut output_buffer,
)
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;

Ok(base64.encode(output_buffer.into_inner()))
if let Some(header) = any_header {
wav::write(
header,
&wav::BitDepth::Sixteen(concat_samples),
&mut output_buffer,
)
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;

Ok(base64.encode(output_buffer.into_inner()))
} else {
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?
}
}

/// Check size of report
Expand All @@ -67,12 +67,12 @@ pub(crate) fn check_report_reason(reason: &str, local_site: &LocalSite) -> Resul

check_slurs(reason, slur_regex)?;
if reason.is_empty() {
Err(LemmyErrorType::ReportReasonRequired)?;
}
if reason.chars().count() > 1000 {
Err(LemmyErrorType::ReportTooLong)?;
Err(LemmyErrorType::ReportReasonRequired)?
} else if reason.chars().count() > 1000 {
Err(LemmyErrorType::ReportTooLong)?
} else {
Ok(())
}
Ok(())
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Perform for BlockPerson {

// Don't let a person block themselves
if target_id == person_id {
return Err(LemmyErrorType::CantBlockYourself)?;
Err(LemmyErrorType::CantBlockYourself)?
}

let person_block_form = PersonBlockForm {
Expand All @@ -37,7 +37,7 @@ impl Perform for BlockPerson {

let target_user = LocalUserView::read_person(&mut context.pool(), target_id).await;
if target_user.map(|t| t.local_user.admin) == Ok(true) {
return Err(LemmyErrorType::CantBlockAdmin)?;
Err(LemmyErrorType::CantBlockAdmin)?
}

if data.block {
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/change_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Perform for ChangePassword {

// Make sure passwords match
if data.new_password != data.new_password_verify {
return Err(LemmyErrorType::PasswordsDoNotMatch)?;
Err(LemmyErrorType::PasswordsDoNotMatch)?
}

// Check the old password
Expand All @@ -35,7 +35,7 @@ impl Perform for ChangePassword {
)
.unwrap_or(false);
if !valid {
return Err(LemmyErrorType::IncorrectLogin)?;
Err(LemmyErrorType::IncorrectLogin)?
}

let local_user_id = local_user_view.local_user.id;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/change_password_after_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Perform for PasswordChangeAfterReset {

// Make sure passwords match
if data.password != data.password_verify {
return Err(LemmyErrorType::PasswordsDoNotMatch)?;
Err(LemmyErrorType::PasswordsDoNotMatch)?
}

// Update the user with the new password
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Perform for Login {
)
.unwrap_or(false);
if !valid {
return Err(LemmyErrorType::IncorrectLogin)?;
Err(LemmyErrorType::IncorrectLogin)?
}
check_user_valid(
local_user_view.person.banned,
Expand All @@ -51,7 +51,7 @@ impl Perform for Login {
&& site_view.local_site.require_email_verification
&& !local_user_view.local_user.email_verified
{
return Err(LemmyErrorType::EmailNotVerified)?;
Err(LemmyErrorType::EmailNotVerified)?
}

check_registration_application(&local_user_view, &site_view.local_site, &mut context.pool())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Perform for MarkPersonMentionAsRead {
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?;

if local_user_view.person.id != read_person_mention.recipient_id {
return Err(LemmyErrorType::CouldntUpdateComment)?;
Err(LemmyErrorType::CouldntUpdateComment)?
}

let person_mention_id = read_person_mention.id;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/notifications/mark_reply_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn mark_reply_as_read(
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;

if local_user_view.person.id != read_comment_reply.recipient_id {
return Err(LemmyErrorType::CouldntUpdateComment)?;
Err(LemmyErrorType::CouldntUpdateComment)?
}

let comment_reply_id = read_comment_reply.id;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/reset_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Perform for PasswordReset {
)
.await?;
if recent_resets_count >= 3 {
return Err(LemmyErrorType::PasswordResetLimitReached)?;
Err(LemmyErrorType::PasswordResetLimitReached)?
}

// Email the pure token to the user.
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/save_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Perform for SaveUserSettings {
// When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
if let Some(email) = &email {
if email.is_none() && site_view.local_site.require_email_verification {
return Err(LemmyErrorType::EmailRequired)?;
Err(LemmyErrorType::EmailRequired)?
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/private_message/mark_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Perform for MarkPrivateMessageAsRead {
let orig_private_message =
PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.recipient_id {
return Err(LemmyErrorType::CouldntUpdatePrivateMessage)?;
Err(LemmyErrorType::CouldntUpdatePrivateMessage)?
}

// Doing the update
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/site/leave_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Perform for LeaveAdmin {
// Make sure there isn't just one admin (so if one leaves, there will still be one left)
let admins = PersonView::admins(&mut context.pool()).await?;
if admins.len() == 1 {
return Err(LemmyErrorType::CannotLeaveAdmin)?;
Err(LemmyErrorType::CannotLeaveAdmin)?
}

LocalUser::update(
Expand Down
2 changes: 1 addition & 1 deletion crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn html_to_site_metadata(html_bytes: &[u8], url: &Url) -> Result<SiteMetadata, L
.to_lowercase();

if !first_line.starts_with("<!doctype html>") {
Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?;
Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?
}

let mut page = HTML::from_string(html.to_string(), None)?;
Expand Down
47 changes: 26 additions & 21 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ pub async fn is_mod_or_admin(
) -> Result<(), LemmyError> {
let is_mod_or_admin = CommunityView::is_mod_or_admin(pool, person_id, community_id).await?;
if !is_mod_or_admin {
return Err(LemmyErrorType::NotAModOrAdmin)?;
Err(LemmyErrorType::NotAModOrAdmin)?
} else {
Ok(())
}
Ok(())
}

#[tracing::instrument(skip_all)]
Expand All @@ -78,9 +79,10 @@ pub async fn is_mod_or_admin_opt(

pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
if !local_user_view.local_user.admin {
return Err(LemmyErrorType::NotAnAdmin)?;
Err(LemmyErrorType::NotAnAdmin)?
} else {
Ok(())
}
Ok(())
}

pub fn is_top_mod(
Expand All @@ -93,9 +95,10 @@ pub fn is_top_mod(
.map(|cm| cm.moderator.id)
.unwrap_or(PersonId(0))
{
Err(LemmyErrorType::NotTopMod)?;
Err(LemmyErrorType::NotTopMod)?
} else {
Ok(())
}
Ok(())
}

#[tracing::instrument(skip_all)]
Expand Down Expand Up @@ -190,15 +193,14 @@ pub fn check_user_valid(
) -> Result<(), LemmyError> {
// Check for a site ban
if is_banned(banned, ban_expires) {
Err(LemmyErrorType::SiteBan)?;
Err(LemmyErrorType::SiteBan)?
}

// check for account deletion
if deleted {
Err(LemmyErrorType::Deleted)?;
else if deleted {
Err(LemmyErrorType::Deleted)?
} else {
Ok(())
}

Ok(())
}

#[tracing::instrument(skip_all)]
Expand Down Expand Up @@ -259,9 +261,10 @@ pub async fn check_person_block(
#[tracing::instrument(skip_all)]
pub fn check_downvotes_enabled(score: i16, local_site: &LocalSite) -> Result<(), LemmyError> {
if score == -1 && !local_site.enable_downvotes {
Err(LemmyErrorType::DownvotesAreDisabled)?;
Err(LemmyErrorType::DownvotesAreDisabled)?
} else {
Ok(())
}
Ok(())
}

#[tracing::instrument(skip_all)]
Expand All @@ -270,9 +273,10 @@ pub fn check_private_instance(
local_site: &LocalSite,
) -> Result<(), LemmyError> {
if local_user_view.is_none() && local_site.private_instance {
Err(LemmyErrorType::InstanceIsPrivate)?;
Err(LemmyErrorType::InstanceIsPrivate)?
} else {
Ok(())
}
Ok(())
}

#[tracing::instrument(skip_all)]
Expand Down Expand Up @@ -517,11 +521,11 @@ pub async fn check_registration_application(
if let Some(deny_reason) = registration.deny_reason {
let lang = get_interface_language(local_user_view);
let registration_denied_message = format!("{}: {}", lang.registration_denied(), deny_reason);
return Err(LemmyErrorType::RegistrationDenied(
Err(LemmyErrorType::RegistrationDenied(
registration_denied_message,
))?;
))?
} else {
return Err(LemmyErrorType::RegistrationApplicationIsPending)?;
Err(LemmyErrorType::RegistrationApplicationIsPending)?
}
}
Ok(())
Expand All @@ -531,9 +535,10 @@ pub fn check_private_instance_and_federation_enabled(
local_site: &LocalSite,
) -> Result<(), LemmyError> {
if local_site.private_instance && local_site.federation_enabled {
Err(LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether)?;
Err(LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether)?
} else {
Ok(())
}
Ok(())
}

pub async fn purge_image_posts_for_person(
Expand Down
4 changes: 2 additions & 2 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn create_comment(

// Check if post is locked, no new comments
if post.locked {
return Err(LemmyErrorType::Locked)?;
Err(LemmyErrorType::Locked)?
}

// Fetch the parent, if it exists
Expand All @@ -79,7 +79,7 @@ pub async fn create_comment(
// Strange issue where sometimes the post ID of the parent comment is incorrect
if let Some(parent) = parent_opt.as_ref() {
if parent.post_id != post_id {
return Err(LemmyErrorType::CouldntCreateComment)?;
Err(LemmyErrorType::CouldntCreateComment)?
}
check_comment_depth(parent)?;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/api_crud/src/comment/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn delete_comment(

// Dont delete it if its already been deleted.
if orig_comment.comment.deleted == data.deleted {
return Err(LemmyErrorType::CouldntUpdateComment)?;
Err(LemmyErrorType::CouldntUpdateComment)?
}

check_community_ban(
Expand All @@ -41,7 +41,7 @@ pub async fn delete_comment(

// Verify that only the creator can delete
if local_user_view.person.id != orig_comment.creator.id {
return Err(LemmyErrorType::NoCommentEditAllowed)?;
Err(LemmyErrorType::NoCommentEditAllowed)?
}

// Do the delete
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/comment/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn update_comment(

// Verify that only the creator can edit
if local_user_view.person.id != orig_comment.creator.id {
return Err(LemmyErrorType::NoCommentEditAllowed)?;
Err(LemmyErrorType::NoCommentEditAllowed)?
}

let language_id = data.language_id;
Expand Down
6 changes: 3 additions & 3 deletions crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn create_community(
let local_site = site_view.local_site;

if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?
}

// Check to make sure the icon and banners are urls
Expand Down Expand Up @@ -79,7 +79,7 @@ pub async fn create_community(
let community_dupe =
Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
if community_dupe.is_some() {
return Err(LemmyErrorType::CommunityAlreadyExists)?;
Err(LemmyErrorType::CommunityAlreadyExists)?
}

// When you create a community, make sure the user becomes a moderator and a follower
Expand Down Expand Up @@ -135,7 +135,7 @@ pub async fn create_community(
// https://stackoverflow.com/a/64227550
let is_subset = languages.iter().all(|item| site_languages.contains(item));
if !is_subset {
return Err(LemmyErrorType::LanguageNotAllowed)?;
Err(LemmyErrorType::LanguageNotAllowed)?
}
CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
}
Expand Down
Loading

0 comments on commit 56e26fc

Please sign in to comment.