Cleanup post action forms (#5197)

* Removing a few SuccessResponses for PostHide and MarkPostAsRead.

- This also removes the pointless multiple post_ids. These can be done
  as individual calls on the front end anyway.
- Fixes #4755

* Fixing federation tests.

* Upgrading lemmy-js-client deps.

* Add ability to mark several posts as read.

Context:

- https://github.com/LemmyNet/lemmy/pull/5043
- https://github.com/LemmyNet/lemmy/issues/4755
- https://github.com/LemmyNet/lemmy/pull/5160

* Simplifying forms.

* Fixing forms.

* Cleanup post action forms by using derive_new defaults.

- Fixes #5195

* Fix ntfy to notify on success builds also.

* Removing pointless naive_now function.

* Running taplo fmt.
This commit is contained in:
Dessalines 2024-11-15 05:21:08 -05:00 committed by GitHub
parent 7f4e26e29e
commit 231cce9350
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 145 additions and 219 deletions

View file

@ -123,7 +123,10 @@ reqwest-tracing = "0.5.3"
clokwerk = "0.4.0"
doku = { version = "0.21.1", features = ["url-2"] }
bcrypt = "0.15.1"
chrono = { version = "0.4.38", features = ["serde"], default-features = false }
chrono = { version = "0.4.38", features = [
"serde",
"now",
], default-features = false }
serde_json = { version = "1.0.121", features = ["preserve_order"] }
base64 = "0.22.1"
uuid = { version = "1.10.0", features = ["serde", "v4"] }

View file

@ -10,7 +10,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
local_site::LocalSite,
post::{PostLike, PostLikeForm, PostRead},
post::{PostLike, PostLikeForm, PostRead, PostReadForm},
},
traits::Likeable,
};
@ -47,11 +47,7 @@ pub async fn like_post(
)
.await?;
let like_form = PostLikeForm {
post_id: data.post_id,
person_id: local_user_view.person.id,
score: data.score,
};
let like_form = PostLikeForm::new(data.post_id, local_user_view.person.id, data.score);
// Remove any likes first
let person_id = local_user_view.person.id;
@ -67,7 +63,8 @@ pub async fn like_post(
}
// Mark Post Read
PostRead::mark_as_read(&mut context.pool(), post_id, person_id).await?;
let read_form = PostReadForm::new(post_id, person_id);
PostRead::mark_as_read(&mut context.pool(), &read_form).await?;
ActivityChannel::submit_activity(
SendActivityData::LikePostOrComment {

View file

@ -3,7 +3,7 @@ use lemmy_api_common::{
context::LemmyContext,
post::{MarkPostAsRead, PostResponse},
};
use lemmy_db_schema::source::post::PostRead;
use lemmy_db_schema::source::post::{PostRead, PostReadForm};
use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_utils::error::LemmyResult;
@ -17,10 +17,11 @@ pub async fn mark_post_as_read(
let post_id = data.post_id;
// Mark the post as read / unread
let form = PostReadForm::new(post_id, person_id);
if data.read {
PostRead::mark_as_read(&mut context.pool(), post_id, person_id).await?;
PostRead::mark_as_read(&mut context.pool(), &form).await?;
} else {
PostRead::mark_as_unread(&mut context.pool(), post_id, person_id).await?;
PostRead::mark_as_unread(&mut context.pool(), &form).await?;
}
let post_view = PostView::read(
&mut context.pool(),

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
post::{PostResponse, SavePost},
};
use lemmy_db_schema::{
source::post::{PostRead, PostSaved, PostSavedForm},
source::post::{PostRead, PostReadForm, PostSaved, PostSavedForm},
traits::Saveable,
};
use lemmy_db_views::structs::{LocalUserView, PostView};
@ -16,10 +16,7 @@ pub async fn save_post(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<PostResponse>> {
let post_saved_form = PostSavedForm {
post_id: data.post_id,
person_id: local_user_view.person.id,
};
let post_saved_form = PostSavedForm::new(data.post_id, local_user_view.person.id);
if data.save {
PostSaved::save(&mut context.pool(), &post_saved_form)
@ -41,7 +38,8 @@ pub async fn save_post(
)
.await?;
PostRead::mark_as_read(&mut context.pool(), post_id, person_id).await?;
let read_form = PostReadForm::new(post_id, person_id);
PostRead::mark_as_read(&mut context.pool(), &read_form).await?;
Ok(Json(PostResponse { post_view }))
}

View file

@ -1,5 +1,6 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
build_response::{build_comment_response, send_local_notifs},
comment::{CommentResponse, EditComment},
@ -19,7 +20,6 @@ use lemmy_db_schema::{
local_site::LocalSite,
},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views::structs::{CommentView, LocalUserView};
use lemmy_utils::{
@ -74,7 +74,7 @@ pub async fn update_comment(
let form = CommentUpdateForm {
content,
language_id: Some(language_id),
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
..Default::default()
};
let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)

View file

@ -1,6 +1,7 @@
use super::check_community_visibility_allowed;
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
build_response::build_community_response,
community::{CommunityResponse, EditCommunity},
@ -22,7 +23,7 @@ use lemmy_db_schema::{
local_site::LocalSite,
},
traits::Crud,
utils::{diesel_string_update, diesel_url_update, naive_now},
utils::{diesel_string_update, diesel_url_update},
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{
@ -95,7 +96,7 @@ pub async fn update_community(
nsfw: data.nsfw,
posting_restricted_to_mods: data.posting_restricted_to_mods,
visibility: data.visibility,
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
..Default::default()
};

View file

@ -1,10 +1,11 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{context::LemmyContext, oauth_provider::EditOAuthProvider, utils::is_admin};
use lemmy_db_schema::{
source::oauth_provider::{OAuthProvider, OAuthProviderUpdateForm},
traits::Crud,
utils::{diesel_required_string_update, diesel_required_url_update, naive_now},
utils::{diesel_required_string_update, diesel_required_url_update},
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
@ -32,7 +33,7 @@ pub async fn update_oauth_provider(
auto_verify_email: data.auto_verify_email,
account_linking_enabled: data.account_linking_enabled,
enabled: data.enabled,
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
};
let update_result =

View file

@ -20,7 +20,7 @@ use lemmy_db_schema::{
source::{
community::Community,
local_site::LocalSite,
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostRead},
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostRead, PostReadForm},
},
traits::{Crud, Likeable},
utils::diesel_url_create,
@ -142,17 +142,14 @@ pub async fn create_post(
// They like their own post by default
let person_id = local_user_view.person.id;
let post_id = inserted_post.id;
let like_form = PostLikeForm {
post_id,
person_id,
score: 1,
};
let like_form = PostLikeForm::new(post_id, person_id, 1);
PostLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
PostRead::mark_as_read(&mut context.pool(), post_id, person_id).await?;
let read_form = PostReadForm::new(post_id, person_id);
PostRead::mark_as_read(&mut context.pool(), &read_form).await?;
build_post_response(&context, community_id, local_user_view, post_id).await
}

View file

@ -7,7 +7,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{
comment::Comment,
post::{Post, PostRead},
post::{Post, PostRead, PostReadForm},
},
traits::Crud,
};
@ -65,7 +65,8 @@ pub async fn get_post(
let post_id = post_view.post.id;
if let Some(person_id) = person_id {
PostRead::mark_as_read(&mut context.pool(), post_id, person_id).await?;
let read_form = PostReadForm::new(post_id, person_id);
PostRead::mark_as_read(&mut context.pool(), &read_form).await?;
update_read_comments(
person_id,

View file

@ -1,6 +1,7 @@
use super::{convert_published_time, create::send_webmention};
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
build_response::build_post_response,
context::LemmyContext,
@ -22,7 +23,7 @@ use lemmy_db_schema::{
post::{Post, PostUpdateForm},
},
traits::Crud,
utils::{diesel_string_update, diesel_url_update, naive_now},
utils::{diesel_string_update, diesel_url_update},
};
use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_utils::{
@ -131,7 +132,7 @@ pub async fn update_post(
alt_text,
nsfw: data.nsfw,
language_id: Some(language_id),
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
scheduled_publish_time,
..Default::default()
};

View file

@ -1,5 +1,6 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
context::LemmyContext,
private_message::{EditPrivateMessage, PrivateMessageResponse},
@ -12,7 +13,6 @@ use lemmy_db_schema::{
private_message::{PrivateMessage, PrivateMessageUpdateForm},
},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
use lemmy_utils::{
@ -47,7 +47,7 @@ pub async fn update_private_message(
private_message_id,
&PrivateMessageUpdateForm {
content: Some(content),
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
..Default::default()
},
)

View file

@ -2,6 +2,7 @@ use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair};
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
context::LemmyContext,
site::{CreateSite, SiteResponse},
@ -23,7 +24,7 @@ use lemmy_db_schema::{
site::{Site, SiteUpdateForm},
},
traits::Crud,
utils::{diesel_string_update, diesel_url_create, naive_now},
utils::{diesel_string_update, diesel_url_create},
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{
@ -75,7 +76,7 @@ pub async fn create_site(
icon: Some(icon),
banner: Some(banner),
actor_id: Some(actor_id),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
inbox_url,
private_key: Some(Some(keypair.private_key)),
public_key: Some(keypair.public_key),
@ -102,7 +103,7 @@ pub async fn create_site(
legal_information: diesel_string_update(data.legal_information.as_deref()),
application_email_admins: data.application_email_admins,
hide_modlog_mod_names: data.hide_modlog_mod_names,
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
slur_filter_regex: diesel_string_update(data.slur_filter_regex.as_deref()),
actor_name_max_length: data.actor_name_max_length,
federation_enabled: data.federation_enabled,

View file

@ -2,6 +2,7 @@ use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
context::LemmyContext,
request::replace_image,
@ -27,7 +28,7 @@ use lemmy_db_schema::{
site::{Site, SiteUpdateForm},
},
traits::Crud,
utils::{diesel_string_update, diesel_url_update, naive_now},
utils::{diesel_string_update, diesel_url_update},
RegistrationMode,
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
@ -88,7 +89,7 @@ pub async fn update_site(
icon,
banner,
content_warning: diesel_string_update(data.content_warning.as_deref()),
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
..Default::default()
};
@ -111,7 +112,7 @@ pub async fn update_site(
legal_information: diesel_string_update(data.legal_information.as_deref()),
application_email_admins: data.application_email_admins,
hide_modlog_mod_names: data.hide_modlog_mod_names,
updated: Some(Some(naive_now())),
updated: Some(Some(Utc::now())),
slur_filter_regex: diesel_string_update(data.slur_filter_regex.as_deref()),
actor_name_max_length: data.actor_name_max_length,
federation_enabled: data.federation_enabled,

View file

@ -1,5 +1,6 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use chrono::Utc;
use lemmy_api_common::{
context::LemmyContext,
tagline::{TaglineResponse, UpdateTagline},
@ -11,7 +12,6 @@ use lemmy_db_schema::{
tagline::{Tagline, TaglineUpdateForm},
},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
@ -33,7 +33,7 @@ pub async fn update_tagline(
let tagline_form = TaglineUpdateForm {
content,
updated: naive_now(),
updated: Utc::now(),
};
let tagline = Tagline::update(&mut context.pool(), data.id, &tagline_form).await?;

View file

@ -17,6 +17,7 @@ use activitypub_federation::{
kinds::activity::UpdateType,
traits::{ActivityHandler, Actor, Object},
};
use chrono::Utc;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{
source::{
@ -25,7 +26,6 @@ use lemmy_db_schema::{
person::Person,
},
traits::Crud,
utils::naive_now,
};
use lemmy_utils::error::{LemmyError, LemmyResult};
use url::Url;
@ -103,7 +103,7 @@ impl ActivityHandler for UpdateCommunity {
nsfw: Some(self.object.sensitive.unwrap_or(false)),
actor_id: Some(self.object.id.into()),
public_key: Some(self.object.public_key.public_key_pem),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
icon: Some(self.object.icon.map(|i| i.url.into())),
banner: Some(self.object.image.map(|i| i.url.into())),
followers_url: self.object.followers.map(Into::into),

View file

@ -118,11 +118,7 @@ impl ActivityHandler for CreateOrUpdatePage {
let post = ApubPost::from_json(self.object, context).await?;
// author likes their own post by default
let like_form = PostLikeForm {
post_id: post.id,
person_id: post.creator_id,
score: 1,
};
let like_form = PostLikeForm::new(post.id, post.creator_id, 1);
PostLike::like(&mut context.pool(), &like_form).await?;
// Calculate initial hot_rank for post

View file

@ -79,11 +79,7 @@ async fn vote_post(
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let post_id = post.id;
let like_form = PostLikeForm {
post_id: post.id,
person_id: actor.id,
score: vote_type.into(),
};
let like_form = PostLikeForm::new(post.id, actor.id, vote_type.into());
let person_id = actor.id;
PostLike::remove(&mut context.pool(), person_id, post_id).await?;
PostLike::like(&mut context.pool(), &like_form).await?;

View file

@ -200,10 +200,7 @@ pub async fn import_settings(
&context,
|(saved, context)| async move {
let post = saved.dereference(&context).await?;
let form = PostSavedForm {
person_id,
post_id: post.id,
};
let form = PostSavedForm::new(post.id, person_id);
PostSaved::save(&mut context.pool(), &form).await?;
LemmyResult::Ok(())
},

View file

@ -30,7 +30,6 @@ use lemmy_db_schema::{
post::Post,
},
traits::Crud,
utils::naive_now,
};
use lemmy_utils::{
error::{FederationError, LemmyError, LemmyResult},
@ -204,7 +203,7 @@ impl Object for ApubComment {
language_id,
};
let parent_comment_path = parent_comment.map(|t| t.0.path);
let timestamp: DateTime<Utc> = note.updated.or(note.published).unwrap_or_else(naive_now);
let timestamp: DateTime<Utc> = note.updated.or(note.published).unwrap_or_else(Utc::now);
let comment = Comment::insert_apub(
&mut context.pool(),
Some(timestamp),

View file

@ -38,7 +38,6 @@ use lemmy_db_schema::{
local_site::LocalSite,
},
traits::{ApubActor, Crud},
utils::naive_now,
CommunityVisibility,
};
use lemmy_db_views_actor::structs::CommunityFollowerView;
@ -166,7 +165,7 @@ impl Object for ApubCommunity {
nsfw: Some(group.sensitive.unwrap_or(false)),
actor_id: Some(group.id.into()),
local: Some(false),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
icon,
banner,
sidebar,
@ -193,7 +192,7 @@ impl Object for ApubCommunity {
let languages =
LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?;
let timestamp = group.updated.or(group.published).unwrap_or_else(naive_now);
let timestamp = group.updated.or(group.published).unwrap_or_else(Utc::now);
let community: ApubCommunity = Community::insert_apub(&mut context.pool(), timestamp, &form)
.await?
.into();

View file

@ -39,7 +39,6 @@ use lemmy_db_schema::{
site::{Site, SiteInsertForm},
},
traits::Crud,
utils::naive_now,
};
use lemmy_utils::{
error::{FederationError, LemmyError, LemmyResult},
@ -163,7 +162,7 @@ impl Object for ApubSite {
banner,
description: apub.summary,
actor_id: Some(apub.id.clone().into()),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
inbox_url: Some(apub.inbox.clone().into()),
public_key: Some(apub.public_key.public_key_pem.clone()),
private_key: None,

View file

@ -35,7 +35,6 @@ use lemmy_db_schema::{
person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
},
traits::{ApubActor, Crud},
utils::naive_now,
};
use lemmy_utils::{
error::{LemmyError, LemmyResult},
@ -176,7 +175,7 @@ impl Object for ApubPerson {
bot_account: Some(person.kind == UserTypes::Service),
private_key: None,
public_key: person.public_key.public_key_pem,
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
inbox_url: Some(
person
.endpoints

View file

@ -35,7 +35,6 @@ use lemmy_db_schema::{
post::{Post, PostInsertForm, PostUpdateForm},
},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::{
@ -260,7 +259,7 @@ impl Object for ApubPost {
..PostInsertForm::new(name, creator.id, community.id)
};
let timestamp = page.updated.or(page.published).unwrap_or_else(naive_now);
let timestamp = page.updated.or(page.published).unwrap_or_else(Utc::now);
let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?;
let post_ = post.clone();
let context_ = context.reset_request_count();

View file

@ -31,7 +31,6 @@ use lemmy_db_schema::{
private_message::{PrivateMessage, PrivateMessageInsertForm},
},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{
@ -161,7 +160,7 @@ impl Object for ApubPrivateMessage {
ap_id: Some(note.id.into()),
local: Some(false),
};
let timestamp = note.updated.or(note.published).unwrap_or_else(naive_now);
let timestamp = note.updated.or(note.published).unwrap_or_else(Utc::now);
let pm = PrivateMessage::insert_apub(&mut context.pool(), timestamp, &form).await?;
Ok(pm.into())
}

View file

@ -65,11 +65,7 @@ mod tests {
);
let inserted_post = Post::create(pool, &new_post).await?;
let post_like = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
score: 1,
};
let post_like = PostLikeForm::new(inserted_post.id, inserted_person.id, 1);
let _inserted_post_like = PostLike::like(pool, &post_like).await?;
let comment_form = CommentInsertForm::new(

View file

@ -113,11 +113,7 @@ mod tests {
let inserted_child_comment =
Comment::create(pool, &child_comment_form, Some(&inserted_comment.path)).await?;
let post_like = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
score: 1,
};
let post_like = PostLikeForm::new(inserted_post.id, inserted_person.id, 1);
PostLike::like(pool, &post_like).await?;
@ -129,11 +125,7 @@ mod tests {
assert_eq!(0, post_aggs_before_delete.downvotes);
// Add a post dislike from the other person
let post_dislike = PostLikeForm {
post_id: inserted_post.id,
person_id: another_inserted_person.id,
score: -1,
};
let post_dislike = PostLikeForm::new(inserted_post.id, another_inserted_person.id, -1);
PostLike::like(pool, &post_dislike).await?;

View file

@ -12,15 +12,7 @@ use crate::{
CommentUpdateForm,
},
traits::{Crud, Likeable, Saveable},
utils::{
functions::coalesce,
get_conn,
naive_now,
now,
uplete,
DbPool,
DELETED_REPLACEMENT_TEXT,
},
utils::{functions::coalesce, get_conn, now, uplete, DbPool, DELETED_REPLACEMENT_TEXT},
};
use chrono::{DateTime, Utc};
use diesel::{
@ -46,7 +38,7 @@ impl Comment {
.set((
comment::content.eq(DELETED_REPLACEMENT_TEXT),
comment::deleted.eq(true),
comment::updated.eq(naive_now()),
comment::updated.eq(Utc::now()),
))
.get_results::<Self>(conn)
.await
@ -61,7 +53,7 @@ impl Comment {
diesel::update(comment::table.filter(comment::creator_id.eq(for_creator_id)))
.set((
comment::removed.eq(removed),
comment::updated.eq(naive_now()),
comment::updated.eq(Utc::now()),
))
.get_results::<Self>(conn)
.await

View file

@ -6,8 +6,9 @@ use crate::{
},
source::comment_report::{CommentReport, CommentReportForm},
traits::Reportable,
utils::{get_conn, naive_now, DbPool},
utils::{get_conn, DbPool},
};
use chrono::Utc;
use diesel::{
dsl::{insert_into, update},
result::Error,
@ -51,7 +52,7 @@ impl Reportable for CommentReport {
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await
@ -67,7 +68,7 @@ impl Reportable for CommentReport {
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await
@ -88,7 +89,7 @@ impl Reportable for CommentReport {
.set((
resolved.eq(false),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await

View file

@ -16,11 +16,11 @@ use crate::{
utils::{
functions::{coalesce, lower},
get_conn,
naive_now,
now,
DbPool,
},
};
use chrono::Utc;
use diesel::{
dsl::{count_star, insert_into},
result::Error,
@ -52,7 +52,7 @@ impl Instance {
None => {
// Instance not in database yet, insert it
let form = InstanceForm {
updated: Some(naive_now()),
updated: Some(Utc::now()),
..InstanceForm::new(domain_)
};
insert_into(instance::table)

View file

@ -10,8 +10,9 @@ use crate::{
PersonUpdateForm,
},
traits::{ApubActor, Crud, Followable},
utils::{action_query, functions::lower, get_conn, naive_now, now, uplete, DbPool},
utils::{action_query, functions::lower, get_conn, now, uplete, DbPool},
};
use chrono::Utc;
use diesel::{
dsl::{insert_into, not},
expression::SelectableHelper,
@ -93,7 +94,7 @@ impl Person {
person::bio.eq::<Option<String>>(None),
person::matrix_user_id.eq::<Option<String>>(None),
person::deleted.eq(true),
person::updated.eq(naive_now()),
person::updated.eq(Utc::now()),
))
.get_result::<Self>(conn)
.await

View file

@ -1,5 +1,5 @@
use crate::{
diesel::{BoolExpressionMethods, OptionalExtension},
diesel::{BoolExpressionMethods, NullableExpressionMethods, OptionalExtension},
newtypes::{CommunityId, DbUrl, PersonId, PostId},
schema::{community, person, post, post_actions},
source::post::{
@ -19,7 +19,6 @@ use crate::{
utils::{
functions::coalesce,
get_conn,
naive_now,
now,
uplete,
DbPool,
@ -37,7 +36,6 @@ use diesel::{
result::Error,
DecoratableTarget,
ExpressionMethods,
NullableExpressionMethods,
QueryDsl,
TextExpressionMethods,
};
@ -138,7 +136,7 @@ impl Post {
post::url.eq(Option::<&str>::None),
post::body.eq(DELETED_REPLACEMENT_TEXT),
post::deleted.eq(true),
post::updated.eq(naive_now()),
post::updated.eq(Utc::now()),
))
.get_results::<Self>(conn)
.await
@ -160,7 +158,7 @@ impl Post {
}
update
.set((post::removed.eq(removed), post::updated.eq(naive_now())))
.set((post::removed.eq(removed), post::updated.eq(Utc::now())))
.get_results::<Self>(conn)
.await
}
@ -281,7 +279,6 @@ impl Likeable for PostLike {
type IdType = PostId;
async fn like(pool: &mut DbPool<'_>, post_like_form: &PostLikeForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let post_like_form = (post_like_form, post_actions::liked.eq(now().nullable()));
insert_into(post_actions::table)
.values(post_like_form)
.on_conflict((post_actions::post_id, post_actions::person_id))
@ -310,7 +307,6 @@ impl Saveable for PostSaved {
type Form = PostSavedForm;
async fn save(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let post_saved_form = (post_saved_form, post_actions::saved.eq(now().nullable()));
insert_into(post_actions::table)
.values(post_saved_form)
.on_conflict((post_actions::post_id, post_actions::person_id))
@ -335,28 +331,25 @@ impl Saveable for PostSaved {
impl PostRead {
pub async fn mark_as_read(
pool: &mut DbPool<'_>,
post_id: PostId,
person_id: PersonId,
post_read_form: &PostReadForm,
) -> LemmyResult<usize> {
Self::mark_many_as_read(pool, &[post_id], person_id).await
Self::mark_many_as_read(pool, &[post_read_form.post_id], post_read_form.person_id).await
}
pub async fn mark_as_unread(
pool: &mut DbPool<'_>,
post_id_: PostId,
person_id_: PersonId,
) -> LemmyResult<uplete::Count> {
post_read_form: &PostReadForm,
) -> Result<uplete::Count, Error> {
let conn = &mut get_conn(pool).await?;
uplete::new(
post_actions::table
.filter(post_actions::post_id.eq(post_id_))
.filter(post_actions::person_id.eq(person_id_)),
.filter(post_actions::post_id.eq(post_read_form.post_id))
.filter(post_actions::person_id.eq(post_read_form.person_id)),
)
.set_null(post_actions::read)
.get_result(conn)
.await
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)
}
pub async fn mark_many_as_read(
@ -368,16 +361,9 @@ impl PostRead {
let forms = post_ids
.iter()
.map(|post_id| {
(
PostReadForm {
post_id: *post_id,
person_id,
},
post_actions::read.eq(now().nullable()),
)
})
.map(|post_id| (PostReadForm::new(*post_id, person_id)))
.collect::<Vec<_>>();
insert_into(post_actions::table)
.values(forms)
.on_conflict((post_actions::person_id, post_actions::post_id))
@ -397,10 +383,7 @@ impl PostHide {
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
let form = (
&PostHideForm { post_id, person_id },
post_actions::hidden.eq(now().nullable()),
);
let form = &PostHideForm::new(post_id, person_id);
insert_into(post_actions::table)
.values(form)
.on_conflict((post_actions::person_id, post_actions::post_id))
@ -442,6 +425,7 @@ mod tests {
PostLike,
PostLikeForm,
PostRead,
PostReadForm,
PostSaved,
PostSavedForm,
PostUpdateForm,
@ -525,11 +509,7 @@ mod tests {
};
// Post Like
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
score: 1,
};
let post_like_form = PostLikeForm::new(inserted_post.id, inserted_person.id, 1);
let inserted_post_like = PostLike::like(pool, &post_like_form).await?;
@ -541,10 +521,7 @@ mod tests {
};
// Post Save
let post_saved_form = PostSavedForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
};
let post_saved_form = PostSavedForm::new(inserted_post.id, inserted_person.id);
let inserted_post_saved = PostSaved::save(pool, &post_saved_form).await?;
@ -555,8 +532,10 @@ mod tests {
};
// Mark 2 posts as read
PostRead::mark_as_read(pool, inserted_post.id, inserted_person.id).await?;
PostRead::mark_as_read(pool, inserted_post2.id, inserted_person.id).await?;
let post_read_form_1 = PostReadForm::new(inserted_post.id, inserted_person.id);
PostRead::mark_as_read(pool, &post_read_form_1).await?;
let post_read_form_2 = PostReadForm::new(inserted_post2.id, inserted_person.id);
PostRead::mark_as_read(pool, &post_read_form_2).await?;
let read_post = Post::read(pool, inserted_post.id).await?;
@ -575,12 +554,12 @@ mod tests {
let saved_removed = PostSaved::unsave(pool, &post_saved_form).await?;
assert_eq!(uplete::Count::only_updated(1), saved_removed);
let read_removed_1 =
PostRead::mark_as_unread(pool, inserted_post.id, inserted_person.id).await?;
let read_remove_form_1 = PostReadForm::new(inserted_post.id, inserted_person.id);
let read_removed_1 = PostRead::mark_as_unread(pool, &read_remove_form_1).await?;
assert_eq!(uplete::Count::only_deleted(1), read_removed_1);
let read_removed_2 =
PostRead::mark_as_unread(pool, inserted_post2.id, inserted_person.id).await?;
let read_remove_form_2 = PostReadForm::new(inserted_post2.id, inserted_person.id);
let read_removed_2 = PostRead::mark_as_unread(pool, &read_remove_form_2).await?;
assert_eq!(uplete::Count::only_deleted(1), read_removed_2);
let num_deleted = Post::delete(pool, inserted_post.id).await?

View file

@ -6,8 +6,9 @@ use crate::{
},
source::post_report::{PostReport, PostReportForm},
traits::Reportable,
utils::{get_conn, naive_now, DbPool},
utils::{get_conn, DbPool},
};
use chrono::Utc;
use diesel::{
dsl::{insert_into, update},
result::Error,
@ -40,7 +41,7 @@ impl Reportable for PostReport {
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await
@ -56,7 +57,7 @@ impl Reportable for PostReport {
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await
@ -72,7 +73,7 @@ impl Reportable for PostReport {
.set((
resolved.eq(false),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await

View file

@ -3,8 +3,9 @@ use crate::{
schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated},
source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
traits::Reportable,
utils::{get_conn, naive_now, DbPool},
utils::{get_conn, DbPool},
};
use chrono::Utc;
use diesel::{
dsl::{insert_into, update},
result::Error,
@ -40,7 +41,7 @@ impl Reportable for PrivateMessageReport {
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await
@ -65,7 +66,7 @@ impl Reportable for PrivateMessageReport {
.set((
resolved.eq(false),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
updated.eq(Utc::now()),
))
.execute(conn)
.await

View file

@ -84,7 +84,7 @@ pub struct CommentInsertForm {
pub struct CommentUpdateForm {
pub content: Option<String>,
pub removed: Option<bool>,
// Don't use a default naive_now here, because the create function does a lot of comment updates
// Don't use a default Utc::now here, because the create function does a lot of comment updates
pub updated: Option<Option<DateTime<Utc>>>,
pub deleted: Option<bool>,
pub ap_id: Option<DbUrl>,

View file

@ -165,7 +165,7 @@ pub struct PostLike {
pub published: DateTime<Utc>,
}
#[derive(Clone)]
#[derive(Clone, derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
pub struct PostLikeForm {
@ -173,6 +173,8 @@ pub struct PostLikeForm {
pub person_id: PersonId,
#[cfg_attr(feature = "full", diesel(column_name = like_score))]
pub score: i16,
#[new(value = "Utc::now()")]
pub liked: DateTime<Utc>,
}
#[derive(PartialEq, Eq, Debug)]
@ -192,11 +194,14 @@ pub struct PostSaved {
pub published: DateTime<Utc>,
}
#[derive(derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
pub struct PostSavedForm {
pub post_id: PostId,
pub person_id: PersonId,
#[new(value = "Utc::now()")]
pub saved: DateTime<Utc>,
}
#[derive(PartialEq, Eq, Debug)]
@ -216,11 +221,14 @@ pub struct PostRead {
pub published: DateTime<Utc>,
}
#[derive(derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
pub(crate) struct PostReadForm {
pub struct PostReadForm {
pub post_id: PostId,
pub person_id: PersonId,
#[new(value = "Utc::now()")]
pub read: DateTime<Utc>,
}
#[derive(PartialEq, Eq, Debug)]
@ -240,9 +248,12 @@ pub struct PostHide {
pub published: DateTime<Utc>,
}
#[derive(derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
pub(crate) struct PostHideForm {
pub struct PostHideForm {
pub post_id: PostId,
pub person_id: PersonId,
#[new(value = "Utc::now()")]
pub hidden: DateTime<Utc>,
}

View file

@ -1,7 +1,7 @@
pub mod uplete;
use crate::{newtypes::DbUrl, CommentSortType, PostSortType};
use chrono::{DateTime, TimeDelta, Utc};
use chrono::TimeDelta;
use deadpool::Runtime;
use diesel::{
dsl,
@ -499,10 +499,6 @@ pub fn build_db_pool_for_tests() -> ActualDbPool {
build_db_pool().expect("db pool missing")
}
pub fn naive_now() -> DateTime<Utc> {
Utc::now()
}
pub fn post_to_comment_sort_type(sort: PostSortType) -> CommentSortType {
use PostSortType::*;
match sort {

View file

@ -641,6 +641,7 @@ mod tests {
PostLike,
PostLikeForm,
PostRead,
PostReadForm,
PostSaved,
PostSavedForm,
PostUpdateForm,
@ -994,11 +995,8 @@ mod tests {
let pool = &mut pool.into();
let mut data = init_data(pool).await?;
let post_like_form = PostLikeForm {
post_id: data.inserted_post.id,
person_id: data.local_user_view.person.id,
score: 1,
};
let post_like_form =
PostLikeForm::new(data.inserted_post.id, data.local_user_view.person.id, 1);
let inserted_post_like = PostLike::like(pool, &post_like_form).await?;
@ -1054,18 +1052,12 @@ mod tests {
// Like both the bot post, and your own
// The liked_only should not show your own post
let post_like_form = PostLikeForm {
post_id: data.inserted_post.id,
person_id: data.local_user_view.person.id,
score: 1,
};
let post_like_form =
PostLikeForm::new(data.inserted_post.id, data.local_user_view.person.id, 1);
PostLike::like(pool, &post_like_form).await?;
let bot_post_like_form = PostLikeForm {
post_id: data.inserted_bot_post.id,
person_id: data.local_user_view.person.id,
score: 1,
};
let bot_post_like_form =
PostLikeForm::new(data.inserted_bot_post.id, data.local_user_view.person.id, 1);
PostLike::like(pool, &bot_post_like_form).await?;
// Read the liked only
@ -1103,10 +1095,8 @@ mod tests {
// Save only the bot post
// The saved_only should only show the bot post
let post_save_form = PostSavedForm {
post_id: data.inserted_bot_post.id,
person_id: data.local_user_view.person.id,
};
let post_save_form =
PostSavedForm::new(data.inserted_bot_post.id, data.local_user_view.person.id);
PostSaved::save(pool, &post_save_form).await?;
// Read the saved only
@ -1521,12 +1511,8 @@ mod tests {
data.local_user_view.local_user.show_read_posts = false;
// Mark a post as read
PostRead::mark_as_read(
pool,
data.inserted_bot_post.id,
data.local_user_view.person.id,
)
.await?;
let read_form = PostReadForm::new(data.inserted_bot_post.id, data.local_user_view.person.id);
PostRead::mark_as_read(pool, &read_form).await?;
// Make sure you don't see the read post in the results
let post_listings_hide_read = data.default_post_query().list(&data.site, pool).await?;

View file

@ -134,19 +134,11 @@ mod tests {
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
// Timmy upvotes his own post
let timmy_post_vote_form = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_timmy.id,
score: 1,
};
let timmy_post_vote_form = PostLikeForm::new(inserted_post.id, inserted_timmy.id, 1);
PostLike::like(pool, &timmy_post_vote_form).await?;
// Sara downvotes timmy's post
let sara_post_vote_form = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_sara.id,
score: -1,
};
let sara_post_vote_form = PostLikeForm::new(inserted_post.id, inserted_sara.id, -1);
PostLike::like(pool, &sara_post_vote_form).await?;
let expected_post_vote_views = [

View file

@ -22,7 +22,7 @@ use lemmy_db_schema::{
federation_queue_state::FederationQueueState,
instance::{Instance, InstanceForm},
},
utils::{naive_now, ActualDbPool, DbPool},
utils::{ActualDbPool, DbPool},
};
use std::{collections::BinaryHeap, ops::Add, time::Duration};
use tokio::{
@ -291,7 +291,7 @@ impl InstanceWorker {
self.instance.updated = Some(Utc::now());
let form = InstanceForm {
updated: Some(naive_now()),
updated: Some(Utc::now()),
..InstanceForm::new(self.instance.domain.clone())
};
Instance::update(&mut self.pool(), self.instance.id, form).await?;

View file

@ -1,5 +1,6 @@
// This is for db migrations that require code
use activitypub_federation::http_signatures::generate_actor_keypair;
use chrono::Utc;
use diesel::{
sql_types::{Nullable, Text},
ExpressionMethods,
@ -26,7 +27,7 @@ use lemmy_db_schema::{
site::{Site, SiteInsertForm, SiteUpdateForm},
},
traits::Crud,
utils::{get_conn, naive_now, DbPool},
utils::{get_conn, DbPool},
};
use lemmy_utils::{error::LemmyResult, settings::structs::Settings};
use tracing::info;
@ -78,7 +79,7 @@ async fn user_updates_2020_04_02(
)?),
private_key: Some(Some(keypair.private_key)),
public_key: Some(keypair.public_key),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
..Default::default()
};
@ -118,7 +119,7 @@ async fn community_updates_2020_04_02(
actor_id: Some(community_actor_id.clone()),
private_key: Some(Some(keypair.private_key)),
public_key: Some(keypair.public_key),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
..Default::default()
};
@ -334,7 +335,7 @@ async fn instance_actor_2022_01_28(
let actor_id = Url::parse(protocol_and_hostname)?;
let site_form = SiteUpdateForm {
actor_id: Some(actor_id.clone().into()),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
inbox_url: Some(generate_inbox_url()?),
private_key: Some(Some(key_pair.private_key)),
public_key: Some(key_pair.public_key),
@ -465,7 +466,7 @@ async fn initialize_local_site_2022_10_10(
.unwrap_or_else(|| "New Site".to_string());
let site_form = SiteInsertForm {
actor_id: Some(site_actor_id.clone().into()),
last_refreshed_at: Some(naive_now()),
last_refreshed_at: Some(Utc::now()),
inbox_url: Some(generate_inbox_url()?),
private_key: Some(site_key_pair.private_key),
public_key: Some(site_key_pair.public_key),

View file

@ -36,15 +36,7 @@ use lemmy_db_schema::{
post::{Post, PostUpdateForm},
},
traits::Crud,
utils::{
find_action,
functions::coalesce,
get_conn,
naive_now,
now,
DbPool,
DELETED_REPLACEMENT_TEXT,
},
utils::{find_action, functions::coalesce, get_conn, now, DbPool, DELETED_REPLACEMENT_TEXT},
};
use lemmy_routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
use lemmy_utils::error::LemmyResult;
@ -558,7 +550,7 @@ async fn build_update_instance_form(
// Activitypub). That's why we always need to mark instances as updated if they are
// alive.
let mut instance_form = InstanceForm {
updated: Some(naive_now()),
updated: Some(Utc::now()),
..InstanceForm::new(domain.to_string())
};