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,
site::*,
user_view::*,
ApubObject,
Bannable,
Crud,
Followable,
@ -129,7 +130,7 @@ impl Perform for CreateCommunity {
let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string();
let actor_id_cloned = actor_id.to_owned();
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?;
if community_dupe.is_ok() {

View File

@ -4,7 +4,7 @@ use activitystreams::{
base::{AnyBase, ExtendsExt},
};
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_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
@ -53,7 +53,7 @@ pub(crate) async fn receive_remove_community(
.single_xsd_any_uri()
.context(location_info!())?;
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??;
@ -135,7 +135,7 @@ pub(crate) async fn receive_undo_remove_community(
.single_xsd_any_uri()
.context(location_info!())?;
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??;

View File

@ -16,6 +16,7 @@ use activitystreams::{
use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_,
ApubObject,
DbPool,
Followable,
};
@ -46,7 +47,7 @@ impl ActorType for User_ {
) -> Result<(), LemmyError> {
let follow_actor_id = follow_actor_id.to_string();
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??;
@ -77,7 +78,7 @@ impl ActorType for User_ {
) -> Result<(), LemmyError> {
let follow_actor_id = follow_actor_id.to_string();
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??;

View File

@ -22,6 +22,7 @@ use lemmy_db::{
post_view::PostView,
user::User_,
user_view::UserView,
ApubObject,
Joinable,
SearchType,
};
@ -236,7 +237,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
) -> Result<User_, LemmyError> {
let apub_id_owned = apub_id.to_owned();
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?;
@ -314,7 +315,7 @@ pub(crate) async fn get_or_fetch_and_upsert_community(
) -> Result<Community, LemmyError> {
let apub_id_owned = apub_id.to_owned();
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?;

View File

@ -29,6 +29,7 @@ use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm},
community_view::CommunityUserBanView,
user::User_,
ApubObject,
DbPool,
Followable,
};
@ -116,7 +117,7 @@ pub(crate) async fn community_receive_message(
// unconditionally.
let actor_id = actor.actor_id_str();
let user = blocking(&context.pool(), move |conn| {
User_::read_from_actor_id(&conn, &actor_id)
User_::read_from_apub_id(&conn, &actor_id)
})
.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)?;
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??;
let community_follower_form = CommunityFollowerForm {

View File

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

View File

@ -40,7 +40,7 @@ use activitystreams::{
};
use anyhow::Context;
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_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;

View File

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

View File

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

View File

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

View File

@ -25,6 +25,7 @@ use lemmy_db::{
community::{Community, CommunityForm},
community_view::CommunityModeratorView,
naive_now,
ApubObject,
DbPool,
};
use lemmy_structs::blocking;
@ -124,7 +125,7 @@ impl FromApub for Community {
let domain = community_id.domain().context(location_info!())?;
if domain == Settings::get().hostname {
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??;
Ok(community)

View File

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

View File

@ -21,6 +21,7 @@ use anyhow::Context;
use lemmy_db::{
naive_now,
user::{UserForm, User_},
ApubObject,
DbPool,
};
use lemmy_structs::blocking;
@ -101,7 +102,7 @@ impl FromApub for User_ {
let domain = user_id.domain().context(location_info!())?;
if domain == Settings::get().hostname {
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??;
Ok(user)

View File

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

View File

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

View File

@ -124,6 +124,12 @@ pub trait Reportable<T> {
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> {
fn get_optional(self) -> Option<T>;
}

View File

@ -1,6 +1,7 @@
use crate::{
naive_now,
schema::{post, post_like, post_read, post_saved},
ApubObject,
Crud,
Likeable,
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 {
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
@ -81,11 +113,6 @@ impl Post {
.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> {
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)]
#[belongs_to(Post)]
#[table_name = "post_like"]

View File

@ -2,6 +2,7 @@ use crate::{
is_email_regex,
naive_now,
schema::{user_, user_::dsl::*},
ApubObject,
Crud,
};
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_ {
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
let mut edited_user = form.clone();
@ -135,14 +146,6 @@ impl User_ {
.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(
conn: &PgConnection,
username_or_email: &str,