mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-14 00:14:03 +00:00
Fix tests (changes in get_object_from_apub() prevented Update
from working)
This commit is contained in:
parent
32c4d3224c
commit
32f69775c3
7 changed files with 68 additions and 68 deletions
|
@ -13,7 +13,7 @@ use anyhow::{anyhow, Context};
|
|||
use chrono::NaiveDateTime;
|
||||
use lemmy_db::{ApubObject, Crud, DbPool};
|
||||
use lemmy_structs::blocking;
|
||||
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
|
||||
use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError};
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use url::Url;
|
||||
|
||||
|
@ -185,18 +185,19 @@ pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm>(
|
|||
) -> Result<To, LemmyError>
|
||||
where
|
||||
From: BaseExt<Kind>,
|
||||
To: ApubObject + Crud<ToForm> + Send + 'static,
|
||||
To: ApubObject<ToForm> + Crud<ToForm> + Send + 'static,
|
||||
ToForm: FromApubToForm<From> + Send + 'static,
|
||||
{
|
||||
let object_id = from.id_unchecked().context(location_info!())?.to_owned();
|
||||
let object_in_database = blocking(context.pool(), move |conn| {
|
||||
To::read_from_apub_id(conn, object_id.as_str())
|
||||
})
|
||||
.await?;
|
||||
let domain = object_id.domain().context(location_info!())?;
|
||||
|
||||
// if we already have the object in our database, return that directly
|
||||
if let Ok(to) = object_in_database {
|
||||
Ok(to)
|
||||
if Settings::get().hostname == domain {
|
||||
let object = blocking(context.pool(), move |conn| {
|
||||
To::read_from_apub_id(conn, object_id.as_str())
|
||||
})
|
||||
.await??;
|
||||
Ok(object)
|
||||
}
|
||||
// if we dont have it, parse from apub and insert into database
|
||||
// TODO: this is insecure, a `Like/Post` could result in a non-existent post from a different
|
||||
|
@ -205,7 +206,7 @@ where
|
|||
else {
|
||||
let to_form = ToForm::from_apub(&from, context, expected_domain, request_counter).await?;
|
||||
|
||||
let to = blocking(context.pool(), move |conn| To::create(conn, &to_form)).await??;
|
||||
let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
|
||||
Ok(to)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,11 +87,21 @@ impl Crud<CommentForm> for Comment {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for Comment {
|
||||
impl ApubObject<CommentForm> for Comment {
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
|
||||
use crate::schema::comment::dsl::*;
|
||||
comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
|
||||
use crate::schema::comment::dsl::*;
|
||||
insert_into(comment)
|
||||
.values(comment_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(comment_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Comment {
|
||||
|
@ -171,16 +181,6 @@ impl Comment {
|
|||
.set((content.eq(new_content), updated.eq(naive_now())))
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
|
||||
pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
|
||||
use crate::schema::comment::dsl::*;
|
||||
insert_into(comment)
|
||||
.values(comment_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(comment_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
|
||||
|
|
|
@ -84,13 +84,23 @@ impl Crud<CommunityForm> for Community {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for Community {
|
||||
impl ApubObject<CommunityForm> for Community {
|
||||
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
|
||||
use crate::schema::community::dsl::*;
|
||||
community
|
||||
.filter(actor_id.eq(for_actor_id))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
|
||||
use crate::schema::community::dsl::*;
|
||||
insert_into(community)
|
||||
.values(community_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(community_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Community {
|
||||
|
@ -168,16 +178,6 @@ impl Community {
|
|||
.unwrap_or_default()
|
||||
.contains(&user_id)
|
||||
}
|
||||
|
||||
pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
|
||||
use crate::schema::community::dsl::*;
|
||||
insert_into(community)
|
||||
.values(community_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(community_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
||||
|
|
|
@ -124,10 +124,13 @@ pub trait Reportable<T> {
|
|||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait ApubObject {
|
||||
pub trait ApubObject<T> {
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized;
|
||||
fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait MaybeOptional<T> {
|
||||
|
|
|
@ -87,11 +87,21 @@ impl Crud<PostForm> for Post {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for Post {
|
||||
impl ApubObject<PostForm> for Post {
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
|
||||
use crate::schema::post::dsl::*;
|
||||
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
|
||||
use crate::schema::post::dsl::*;
|
||||
insert_into(post)
|
||||
.values(post_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(post_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Post {
|
||||
|
@ -204,16 +214,6 @@ impl Post {
|
|||
pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
|
||||
user_id == post_creator_id
|
||||
}
|
||||
|
||||
pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
|
||||
use crate::schema::post::dsl::*;
|
||||
insert_into(post)
|
||||
.values(post_form)
|
||||
.on_conflict(ap_id)
|
||||
.do_update()
|
||||
.set(post_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
||||
|
|
|
@ -55,7 +55,7 @@ impl Crud<PrivateMessageForm> for PrivateMessage {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for PrivateMessage {
|
||||
impl ApubObject<PrivateMessageForm> for PrivateMessage {
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized,
|
||||
|
@ -65,6 +65,16 @@ 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 crate::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 PrivateMessage {
|
||||
|
@ -123,20 +133,6 @@ impl PrivateMessage {
|
|||
.set(read.eq(true))
|
||||
.get_results::<Self>(conn)
|
||||
}
|
||||
|
||||
// TODO use this
|
||||
pub fn upsert(
|
||||
conn: &PgConnection,
|
||||
private_message_form: &PrivateMessageForm,
|
||||
) -> Result<Self, Error> {
|
||||
use crate::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)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -90,7 +90,7 @@ impl Crud<UserForm> for User_ {
|
|||
}
|
||||
}
|
||||
|
||||
impl ApubObject for User_ {
|
||||
impl ApubObject<UserForm> for User_ {
|
||||
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
|
||||
use crate::schema::user_::dsl::*;
|
||||
user_
|
||||
|
@ -98,6 +98,15 @@ impl ApubObject for User_ {
|
|||
.filter(actor_id.eq(object_id))
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
|
||||
fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
|
||||
insert_into(user_)
|
||||
.values(user_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(user_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl User_ {
|
||||
|
@ -182,15 +191,6 @@ impl User_ {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
|
||||
insert_into(user_)
|
||||
.values(user_form)
|
||||
.on_conflict(actor_id)
|
||||
.do_update()
|
||||
.set(user_form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
|
||||
pub fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
|
||||
diesel::update(user_.find(user_id))
|
||||
.set((last_refreshed_at.eq(naive_now()),))
|
||||
|
|
Loading…
Reference in a new issue