Add ApubObject trait in DB, with method read_from_apub_id()

This commit is contained in:
Felix Ableitner 2020-12-02 14:25:59 +01:00
parent c5f08768cd
commit af1204c0d0
18 changed files with 95 additions and 71 deletions

View file

@ -20,6 +20,7 @@ use lemmy_db::{
post::Post, post::Post,
site::*, site::*,
user_view::*, user_view::*,
ApubObject,
Bannable, Bannable,
Crud, Crud,
Followable, Followable,
@ -129,7 +130,7 @@ impl Perform for CreateCommunity {
let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string(); let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string();
let actor_id_cloned = actor_id.to_owned(); let actor_id_cloned = actor_id.to_owned();
let community_dupe = blocking(context.pool(), move |conn| { let community_dupe = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &actor_id_cloned) Community::read_from_apub_id(conn, &actor_id_cloned)
}) })
.await?; .await?;
if community_dupe.is_ok() { if community_dupe.is_ok() {

View file

@ -4,7 +4,7 @@ use activitystreams::{
base::{AnyBase, ExtendsExt}, base::{AnyBase, ExtendsExt},
}; };
use anyhow::Context; use anyhow::Context;
use lemmy_db::{community::Community, community_view::CommunityView}; use lemmy_db::{community::Community, community_view::CommunityView, ApubObject};
use lemmy_structs::{blocking, community::CommunityResponse}; use lemmy_structs::{blocking, community::CommunityResponse};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
@ -53,7 +53,7 @@ pub(crate) async fn receive_remove_community(
.single_xsd_any_uri() .single_xsd_any_uri()
.context(location_info!())?; .context(location_info!())?;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, community_uri.as_str()) Community::read_from_apub_id(conn, community_uri.as_str())
}) })
.await??; .await??;
@ -135,7 +135,7 @@ pub(crate) async fn receive_undo_remove_community(
.single_xsd_any_uri() .single_xsd_any_uri()
.context(location_info!())?; .context(location_info!())?;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, community_uri.as_str()) Community::read_from_apub_id(conn, community_uri.as_str())
}) })
.await??; .await??;

View file

@ -16,6 +16,7 @@ use activitystreams::{
use lemmy_db::{ use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm}, community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_, user::User_,
ApubObject,
DbPool, DbPool,
Followable, Followable,
}; };
@ -46,7 +47,7 @@ impl ActorType for User_ {
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let follow_actor_id = follow_actor_id.to_string(); let follow_actor_id = follow_actor_id.to_string();
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &follow_actor_id) Community::read_from_apub_id(conn, &follow_actor_id)
}) })
.await??; .await??;
@ -77,7 +78,7 @@ impl ActorType for User_ {
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let follow_actor_id = follow_actor_id.to_string(); let follow_actor_id = follow_actor_id.to_string();
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &follow_actor_id) Community::read_from_apub_id(conn, &follow_actor_id)
}) })
.await??; .await??;

View file

@ -22,6 +22,7 @@ use lemmy_db::{
post_view::PostView, post_view::PostView,
user::User_, user::User_,
user_view::UserView, user_view::UserView,
ApubObject,
Joinable, Joinable,
SearchType, SearchType,
}; };
@ -236,7 +237,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
) -> Result<User_, LemmyError> { ) -> Result<User_, LemmyError> {
let apub_id_owned = apub_id.to_owned(); let apub_id_owned = apub_id.to_owned();
let user = blocking(context.pool(), move |conn| { let user = blocking(context.pool(), move |conn| {
User_::read_from_actor_id(conn, apub_id_owned.as_ref()) User_::read_from_apub_id(conn, apub_id_owned.as_ref())
}) })
.await?; .await?;
@ -314,7 +315,7 @@ pub(crate) async fn get_or_fetch_and_upsert_community(
) -> Result<Community, LemmyError> { ) -> Result<Community, LemmyError> {
let apub_id_owned = apub_id.to_owned(); let apub_id_owned = apub_id.to_owned();
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, apub_id_owned.as_str()) Community::read_from_apub_id(conn, apub_id_owned.as_str())
}) })
.await?; .await?;

View file

@ -29,6 +29,7 @@ use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm}, community::{Community, CommunityFollower, CommunityFollowerForm},
community_view::CommunityUserBanView, community_view::CommunityUserBanView,
user::User_, user::User_,
ApubObject,
DbPool, DbPool,
Followable, Followable,
}; };
@ -116,7 +117,7 @@ pub(crate) async fn community_receive_message(
// unconditionally. // unconditionally.
let actor_id = actor.actor_id_str(); let actor_id = actor.actor_id_str();
let user = blocking(&context.pool(), move |conn| { let user = blocking(&context.pool(), move |conn| {
User_::read_from_actor_id(&conn, &actor_id) User_::read_from_apub_id(&conn, &actor_id)
}) })
.await??; .await??;
check_community_or_site_ban(&user, &to_community, context.pool()).await?; check_community_or_site_ban(&user, &to_community, context.pool()).await?;
@ -240,7 +241,7 @@ async fn handle_undo_follow(
verify_activity_domains_valid(&follow, &user_url, false)?; verify_activity_domains_valid(&follow, &user_url, false)?;
let user = blocking(&context.pool(), move |conn| { let user = blocking(&context.pool(), move |conn| {
User_::read_from_actor_id(&conn, user_url.as_str()) User_::read_from_apub_id(&conn, user_url.as_str())
}) })
.await??; .await??;
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {

View file

@ -12,7 +12,7 @@ use activitystreams::{
}; };
use actix_web::HttpRequest; use actix_web::HttpRequest;
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{activity::Activity, community::Community, user::User_, DbPool}; use lemmy_db::{activity::Activity, community::Community, user::User_, ApubObject, DbPool};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
@ -119,7 +119,7 @@ pub(crate) async fn is_addressed_to_local_user(
) -> Result<bool, LemmyError> { ) -> Result<bool, LemmyError> {
for url in to_and_cc { for url in to_and_cc {
let url = url.to_string(); let url = url.to_string();
let user = blocking(&pool, move |conn| User_::read_from_actor_id(&conn, &url)).await?; let user = blocking(&pool, move |conn| User_::read_from_apub_id(&conn, &url)).await?;
if let Ok(u) = user { if let Ok(u) = user {
if u.local { if u.local {
return Ok(true); return Ok(true);
@ -141,7 +141,7 @@ pub(crate) async fn is_addressed_to_community_followers(
if url.ends_with("/followers") { if url.ends_with("/followers") {
let community_url = url.replace("/followers", ""); let community_url = url.replace("/followers", "");
let community = blocking(&pool, move |conn| { let community = blocking(&pool, move |conn| {
Community::read_from_actor_id(&conn, &community_url) Community::read_from_apub_id(&conn, &community_url)
}) })
.await??; .await??;
if !community.local { if !community.local {

View file

@ -40,7 +40,7 @@ use activitystreams::{
}; };
use anyhow::Context; use anyhow::Context;
use diesel::result::Error::NotFound; use diesel::result::Error::NotFound;
use lemmy_db::{comment::Comment, post::Post, site::Site, Crud}; use lemmy_db::{comment::Comment, post::Post, site::Site, ApubObject, Crud};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -14,7 +14,7 @@ use crate::{
use activitystreams::{activity::ActorAndObject, prelude::*}; use activitystreams::{activity::ActorAndObject, prelude::*};
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context; use anyhow::Context;
use lemmy_db::{community::Community, DbPool}; use lemmy_db::{community::Community, ApubObject, DbPool};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
@ -135,10 +135,7 @@ async fn extract_local_community_from_destinations(
) -> Result<Option<Community>, LemmyError> { ) -> Result<Option<Community>, LemmyError> {
for url in to_and_cc { for url in to_and_cc {
let url = url.to_string(); let url = url.to_string();
let community = blocking(&pool, move |conn| { let community = blocking(&pool, move |conn| Community::read_from_apub_id(&conn, &url)).await?;
Community::read_from_actor_id(&conn, &url)
})
.await?;
if let Ok(c) = community { if let Ok(c) = community {
if c.local { if c.local {
return Ok(Some(c)); return Ok(Some(c));

View file

@ -51,6 +51,7 @@ use lemmy_db::{
community::{Community, CommunityFollower}, community::{Community, CommunityFollower},
private_message::PrivateMessage, private_message::PrivateMessage,
user::User_, user::User_,
ApubObject,
Followable, Followable,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
@ -375,7 +376,7 @@ async fn find_community_or_private_message_by_id(
) -> Result<CommunityOrPrivateMessage, LemmyError> { ) -> Result<CommunityOrPrivateMessage, LemmyError> {
let ap_id = apub_id.to_string(); let ap_id = apub_id.to_string();
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &ap_id) Community::read_from_apub_id(conn, &ap_id)
}) })
.await?; .await?;
if let Ok(c) = community { if let Ok(c) = community {

View file

@ -26,6 +26,7 @@ use lemmy_db::{
community::Community, community::Community,
post::Post, post::Post,
user::User_, user::User_,
ApubObject,
Crud, Crud,
DbPool, DbPool,
}; };

View file

@ -25,6 +25,7 @@ use lemmy_db::{
community::{Community, CommunityForm}, community::{Community, CommunityForm},
community_view::CommunityModeratorView, community_view::CommunityModeratorView,
naive_now, naive_now,
ApubObject,
DbPool, DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
@ -124,7 +125,7 @@ impl FromApub for Community {
let domain = community_id.domain().context(location_info!())?; let domain = community_id.domain().context(location_info!())?;
if domain == Settings::get().hostname { if domain == Settings::get().hostname {
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, community_id.as_str()) Community::read_from_apub_id(conn, community_id.as_str())
}) })
.await??; .await??;
Ok(community) Ok(community)

View file

@ -23,6 +23,7 @@ use lemmy_db::{
community::Community, community::Community,
post::{Post, PostForm}, post::{Post, PostForm},
user::User_, user::User_,
ApubObject,
Crud, Crud,
DbPool, DbPool,
}; };

View file

@ -21,6 +21,7 @@ use anyhow::Context;
use lemmy_db::{ use lemmy_db::{
naive_now, naive_now,
user::{UserForm, User_}, user::{UserForm, User_},
ApubObject,
DbPool, DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
@ -101,7 +102,7 @@ impl FromApub for User_ {
let domain = user_id.domain().context(location_info!())?; let domain = user_id.domain().context(location_info!())?;
if domain == Settings::get().hostname { if domain == Settings::get().hostname {
let user = blocking(context.pool(), move |conn| { let user = blocking(context.pool(), move |conn| {
User_::read_from_actor_id(conn, user_id.as_str()) User_::read_from_apub_id(conn, user_id.as_str())
}) })
.await??; .await??;
Ok(user) Ok(user)

View file

@ -2,6 +2,7 @@ use super::post::Post;
use crate::{ use crate::{
naive_now, naive_now,
schema::{comment, comment_like, comment_saved}, schema::{comment, comment_like, comment_saved},
ApubObject,
Crud, Crud,
Likeable, Likeable,
Saveable, Saveable,
@ -86,6 +87,13 @@ impl Crud<CommentForm> for Comment {
} }
} }
impl ApubObject 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)
}
}
impl Comment { impl Comment {
pub fn update_ap_id( pub fn update_ap_id(
conn: &PgConnection, conn: &PgConnection,
@ -99,11 +107,6 @@ impl Comment {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub 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)
}
pub fn permadelete_for_creator( pub fn permadelete_for_creator(
conn: &PgConnection, conn: &PgConnection,
for_creator_id: i32, for_creator_id: i32,

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
naive_now, naive_now,
schema::{community, community_follower, community_moderator, community_user_ban}, schema::{community, community_follower, community_moderator, community_user_ban},
ApubObject,
Bannable, Bannable,
Crud, Crud,
Followable, Followable,
@ -83,6 +84,15 @@ impl Crud<CommunityForm> for Community {
} }
} }
impl ApubObject 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)
}
}
impl Community { impl Community {
pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> { pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use crate::schema::community::dsl::*;
@ -92,13 +102,6 @@ impl Community {
.first::<Self>(conn) .first::<Self>(conn)
} }
pub fn read_from_actor_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)
}
pub fn update_deleted( pub fn update_deleted(
conn: &PgConnection, conn: &PgConnection,
community_id: i32, community_id: i32,

View file

@ -124,6 +124,12 @@ pub trait Reportable<T> {
Self: Sized; Self: Sized;
} }
pub trait ApubObject {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
where
Self: Sized;
}
pub trait MaybeOptional<T> { pub trait MaybeOptional<T> {
fn get_optional(self) -> Option<T>; fn get_optional(self) -> Option<T>;
} }

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
naive_now, naive_now,
schema::{post, post_like, post_read, post_saved}, schema::{post, post_like, post_read, post_saved},
ApubObject,
Crud, Crud,
Likeable, Likeable,
Readable, Readable,
@ -62,6 +63,37 @@ impl PostForm {
} }
} }
impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
use crate::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn)
}
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(conn)
}
}
impl ApubObject 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)
}
}
impl Post { impl Post {
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> { pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use crate::schema::post::dsl::*;
@ -81,11 +113,6 @@ impl Post {
.load::<Self>(conn) .load::<Self>(conn)
} }
pub 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)
}
pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> { pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use crate::schema::post::dsl::*;
@ -189,30 +216,6 @@ impl Post {
} }
} }
impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
use crate::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn)
}
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(conn)
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Post)] #[belongs_to(Post)]
#[table_name = "post_like"] #[table_name = "post_like"]

View file

@ -2,6 +2,7 @@ use crate::{
is_email_regex, is_email_regex,
naive_now, naive_now,
schema::{user_, user_::dsl::*}, schema::{user_, user_::dsl::*},
ApubObject,
Crud, Crud,
}; };
use bcrypt::{hash, DEFAULT_COST}; use bcrypt::{hash, DEFAULT_COST};
@ -89,6 +90,16 @@ impl Crud<UserForm> for User_ {
} }
} }
impl ApubObject for User_ {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::user_::dsl::*;
user_
.filter(deleted.eq(false))
.filter(actor_id.eq(object_id))
.first::<Self>(conn)
}
}
impl User_ { impl User_ {
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> { pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
let mut edited_user = form.clone(); let mut edited_user = form.clone();
@ -135,14 +146,6 @@ impl User_ {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::user_::dsl::*;
user_
.filter(deleted.eq(false))
.filter(actor_id.eq(object_id))
.first::<Self>(conn)
}
pub fn find_by_email_or_username( pub fn find_by_email_or_username(
conn: &PgConnection, conn: &PgConnection,
username_or_email: &str, username_or_email: &str,