mirror of https://github.com/LemmyNet/lemmy.git
move upsert() method out of ApubObject trait
parent
d4330dfea5
commit
24fc616774
|
@ -10,7 +10,6 @@ use lemmy_db_queries::{ApubObject, DbPool};
|
|||
use lemmy_db_schema::DbUrl;
|
||||
use lemmy_utils::{request::retry, settings::structs::Settings, LemmyError};
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use log::debug;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
|
@ -54,7 +53,6 @@ where
|
|||
context: &LemmyContext,
|
||||
request_counter: &mut i32,
|
||||
) -> Result<Kind, LemmyError> {
|
||||
debug!("dereference {}", self.to_string());
|
||||
let db_object = self.dereference_locally(context.pool()).await?;
|
||||
|
||||
// if its a local object, only fetch it from the database and not over http
|
||||
|
@ -69,7 +67,6 @@ where
|
|||
if let Some(last_refreshed_at) = object.last_refreshed_at() {
|
||||
// TODO: rename to should_refetch_object()
|
||||
if should_refetch_actor(last_refreshed_at) {
|
||||
debug!("Refetching remote object {}", self.0.as_str());
|
||||
return self
|
||||
.dereference_remotely(context, request_counter, Some(object))
|
||||
.await;
|
||||
|
@ -77,7 +74,6 @@ where
|
|||
}
|
||||
Ok(object)
|
||||
} else {
|
||||
debug!("Fetching remote object {}", self.0.as_str());
|
||||
self
|
||||
.dereference_remotely(context, request_counter, None)
|
||||
.await
|
||||
|
@ -86,7 +82,6 @@ where
|
|||
|
||||
/// returning none means the object was not found in local db
|
||||
async fn dereference_locally(&self, pool: &DbPool) -> Result<Option<Kind>, LemmyError> {
|
||||
debug!("dereference_locally {}", self.to_string());
|
||||
let id: DbUrl = self.0.clone().into();
|
||||
let object = blocking(pool, move |conn| ApubObject::read_from_apub_id(conn, &id)).await?;
|
||||
match object {
|
||||
|
@ -102,7 +97,6 @@ where
|
|||
request_counter: &mut i32,
|
||||
db_object: Option<Kind>,
|
||||
) -> Result<Kind, LemmyError> {
|
||||
debug!("dereference_remotely {}", self.to_string());
|
||||
// dont fetch local objects this way
|
||||
debug_assert!(self.0.domain() != Some(&Settings::get().hostname));
|
||||
|
||||
|
@ -122,7 +116,6 @@ where
|
|||
.await?;
|
||||
|
||||
if res.status() == StatusCode::GONE {
|
||||
debug!("is deleted {}", self.to_string());
|
||||
if let Some(db_object) = db_object {
|
||||
db_object.delete(context).await?;
|
||||
}
|
||||
|
|
|
@ -33,8 +33,6 @@ pub enum PageOrNote {
|
|||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl ApubObject for PostOrComment {
|
||||
type Form = PostOrCommentForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
None
|
||||
}
|
||||
|
@ -50,18 +48,6 @@ impl ApubObject for PostOrComment {
|
|||
Err(_) => PostOrComment::Comment(Box::new(Comment::read_from_apub_id(conn, object_id)?)),
|
||||
})
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, user_form: &PostOrCommentForm) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Ok(match user_form {
|
||||
PostOrCommentForm::PostForm(f) => PostOrComment::Post(Box::new(Post::upsert(conn, f)?)),
|
||||
PostOrCommentForm::CommentForm(f) => {
|
||||
PostOrComment::Comment(Box::new(Comment::upsert(conn, f)?))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
|
|
|
@ -95,8 +95,6 @@ pub enum SearchableApubTypes {
|
|||
}
|
||||
|
||||
impl ApubObject for SearchableObjects {
|
||||
type Form = ();
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
match self {
|
||||
SearchableObjects::Person(p) => p.last_refreshed_at(),
|
||||
|
@ -127,11 +125,6 @@ impl ApubObject for SearchableObjects {
|
|||
let c = Comment::read_from_apub_id(conn, object_id);
|
||||
Ok(SearchableObjects::Comment(c?))
|
||||
}
|
||||
|
||||
// TODO: move this (and the Form type) elsewhere, as it isnt really needed on this trait
|
||||
fn upsert(_conn: &PgConnection, _user_form: &Self::Form) -> Result<Self, Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
|
|
|
@ -20,7 +20,7 @@ use lemmy_apub_lib::{
|
|||
values::{MediaTypeHtml, MediaTypeMarkdown, PublicUrl},
|
||||
verify_domains_match,
|
||||
};
|
||||
use lemmy_db_queries::{ApubObject, Crud, DbPool};
|
||||
use lemmy_db_queries::{source::comment::Comment_, Crud, DbPool};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
comment::{Comment, CommentForm},
|
||||
|
|
|
@ -18,7 +18,7 @@ use lemmy_apub_lib::{
|
|||
values::{MediaTypeHtml, MediaTypeMarkdown},
|
||||
verify_domains_match,
|
||||
};
|
||||
use lemmy_db_queries::{ApubObject, DbPool};
|
||||
use lemmy_db_queries::{source::community::Community_, DbPool};
|
||||
use lemmy_db_schema::{
|
||||
naive_now,
|
||||
source::community::{Community, CommunityForm},
|
||||
|
|
|
@ -17,7 +17,7 @@ use lemmy_apub_lib::{
|
|||
values::{MediaTypeHtml, MediaTypeMarkdown},
|
||||
verify_domains_match,
|
||||
};
|
||||
use lemmy_db_queries::{ApubObject, DbPool};
|
||||
use lemmy_db_queries::{source::person::Person_, DbPool};
|
||||
use lemmy_db_schema::{
|
||||
naive_now,
|
||||
source::person::{Person as DbPerson, PersonForm},
|
||||
|
|
|
@ -21,7 +21,7 @@ use lemmy_apub_lib::{
|
|||
values::{MediaTypeHtml, MediaTypeMarkdown},
|
||||
verify_domains_match,
|
||||
};
|
||||
use lemmy_db_queries::{ApubObject, Crud, DbPool};
|
||||
use lemmy_db_queries::{source::post::Post_, ApubObject, Crud, DbPool};
|
||||
use lemmy_db_schema::{
|
||||
self,
|
||||
source::{
|
||||
|
|
|
@ -16,7 +16,7 @@ use lemmy_apub_lib::{
|
|||
values::{MediaTypeHtml, MediaTypeMarkdown},
|
||||
verify_domains_match,
|
||||
};
|
||||
use lemmy_db_queries::{ApubObject, Crud, DbPool};
|
||||
use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud, DbPool};
|
||||
use lemmy_db_schema::source::{
|
||||
person::Person,
|
||||
private_message::{PrivateMessage, PrivateMessageForm},
|
||||
|
|
|
@ -146,17 +146,14 @@ pub trait DeleteableOrRemoveable {
|
|||
fn blank_out_deleted_or_removed_info(self) -> Self;
|
||||
}
|
||||
|
||||
// TODO: move this to apub lib
|
||||
pub trait ApubObject {
|
||||
type Form;
|
||||
/// If this object should be refetched after a certain interval, it should return the last refresh
|
||||
/// time here. This is mainly used to update remote actors.
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime>;
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized;
|
||||
fn upsert(conn: &PgConnection, user_form: &Self::Form) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait MaybeOptional<T> {
|
||||
|
|
|
@ -51,6 +51,7 @@ pub trait Comment_ {
|
|||
comment_id: CommentId,
|
||||
new_content: &str,
|
||||
) -> Result<Comment, Error>;
|
||||
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error>;
|
||||
}
|
||||
|
||||
impl Comment_ for Comment {
|
||||
|
@ -134,6 +135,16 @@ impl Comment_ for Comment {
|
|||
.set((content.eq(new_content), updated.eq(naive_now())))
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
|
||||
use lemmy_db_schema::schema::comment::dsl::*;
|
||||
insert_into(comment)
|
||||
.values(comment_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(comment_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Crud for Comment {
|
||||
|
@ -169,8 +180,6 @@ impl Crud for Comment {
|
|||
}
|
||||
|
||||
impl ApubObject for Comment {
|
||||
type Form = CommentForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
None
|
||||
}
|
||||
|
@ -179,16 +188,6 @@ impl ApubObject for Comment {
|
|||
use lemmy_db_schema::schema::comment::dsl::*;
|
||||
comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
|
||||
use lemmy_db_schema::schema::comment::dsl::*;
|
||||
insert_into(comment)
|
||||
.values(comment_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(comment_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Likeable for CommentLike {
|
||||
|
|
|
@ -94,8 +94,6 @@ impl Crud for Community {
|
|||
}
|
||||
|
||||
impl ApubObject for Community {
|
||||
type Form = CommunityForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
Some(self.last_refreshed_at)
|
||||
}
|
||||
|
@ -106,16 +104,6 @@ impl ApubObject for Community {
|
|||
.filter(actor_id.eq(for_actor_id))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
|
||||
use lemmy_db_schema::schema::community::dsl::*;
|
||||
insert_into(community)
|
||||
.values(community_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(community_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Community_ {
|
||||
|
@ -135,6 +123,7 @@ pub trait Community_ {
|
|||
conn: &PgConnection,
|
||||
followers_url: &DbUrl,
|
||||
) -> Result<Community, Error>;
|
||||
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error>;
|
||||
}
|
||||
|
||||
impl Community_ for Community {
|
||||
|
@ -182,6 +171,16 @@ impl Community_ for Community {
|
|||
.filter(followers_url.eq(followers_url_))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
|
||||
use lemmy_db_schema::schema::community::dsl::*;
|
||||
insert_into(community)
|
||||
.values(community_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(community_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Joinable for CommunityModerator {
|
||||
|
|
|
@ -182,8 +182,6 @@ impl Crud for Person {
|
|||
}
|
||||
|
||||
impl ApubObject for Person {
|
||||
type Form = PersonForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
Some(self.last_refreshed_at)
|
||||
}
|
||||
|
@ -195,15 +193,6 @@ impl ApubObject for Person {
|
|||
.filter(actor_id.eq(object_id))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
|
||||
insert_into(person)
|
||||
.values(person_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(person_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Person_ {
|
||||
|
@ -212,6 +201,7 @@ pub trait Person_ {
|
|||
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
|
||||
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
|
||||
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
|
||||
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error>;
|
||||
}
|
||||
|
||||
impl Person_ for Person {
|
||||
|
@ -262,6 +252,15 @@ impl Person_ for Person {
|
|||
))
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
|
||||
insert_into(person)
|
||||
.values(person_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(person_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -73,6 +73,7 @@ pub trait Post_ {
|
|||
new_stickied: bool,
|
||||
) -> Result<Post, Error>;
|
||||
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool;
|
||||
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error>;
|
||||
}
|
||||
|
||||
impl Post_ for Post {
|
||||
|
@ -180,19 +181,6 @@ impl Post_ for Post {
|
|||
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
|
||||
person_id == post_creator_id
|
||||
}
|
||||
}
|
||||
|
||||
impl ApubObject for Post {
|
||||
type Form = PostForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
None
|
||||
}
|
||||
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
|
||||
use lemmy_db_schema::schema::post::dsl::*;
|
||||
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
|
||||
use lemmy_db_schema::schema::post::dsl::*;
|
||||
|
@ -205,6 +193,17 @@ impl ApubObject for Post {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for Post {
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
None
|
||||
}
|
||||
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
|
||||
use lemmy_db_schema::schema::post::dsl::*;
|
||||
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Likeable for PostLike {
|
||||
type Form = PostLikeForm;
|
||||
type IdType = PostId;
|
||||
|
|
|
@ -31,8 +31,6 @@ impl Crud for PrivateMessage {
|
|||
}
|
||||
|
||||
impl ApubObject for PrivateMessage {
|
||||
type Form = PrivateMessageForm;
|
||||
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
None
|
||||
}
|
||||
|
@ -46,16 +44,6 @@ impl ApubObject for PrivateMessage {
|
|||
.filter(ap_id.eq(object_id))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
|
||||
use lemmy_db_schema::schema::private_message::dsl::*;
|
||||
insert_into(private_message)
|
||||
.values(private_message_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(private_message_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PrivateMessage_ {
|
||||
|
@ -83,6 +71,10 @@ pub trait PrivateMessage_ {
|
|||
conn: &PgConnection,
|
||||
for_recipient_id: PersonId,
|
||||
) -> Result<Vec<PrivateMessage>, Error>;
|
||||
fn upsert(
|
||||
conn: &PgConnection,
|
||||
private_message_form: &PrivateMessageForm,
|
||||
) -> Result<PrivateMessage, Error>;
|
||||
}
|
||||
|
||||
impl PrivateMessage_ for PrivateMessage {
|
||||
|
@ -144,6 +136,19 @@ impl PrivateMessage_ for PrivateMessage {
|
|||
.set(read.eq(true))
|
||||
.get_results::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(
|
||||
conn: &PgConnection,
|
||||
private_message_form: &PrivateMessageForm,
|
||||
) -> Result<PrivateMessage, Error> {
|
||||
use lemmy_db_schema::schema::private_message::dsl::*;
|
||||
insert_into(private_message)
|
||||
.values(private_message_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(private_message_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl DeleteableOrRemoveable for PrivateMessage {
|
||||
|
|
Loading…
Reference in New Issue