use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
- schema::person::dsl::{
- actor_id,
- avatar,
- banner,
- bio,
- deleted,
- display_name,
- local,
- matrix_user_id,
- name,
- person,
- updated,
- },
+ schema::{instance, local_user, person, person_follower},
source::person::{
Person,
PersonFollower,
traits::{ApubActor, Crud, Followable},
utils::{functions::lower, get_conn, naive_now, DbPool},
};
-use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
+use diesel::{dsl::insert_into, result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
#[async_trait]
type InsertForm = PersonInsertForm;
type UpdateForm = PersonUpdateForm;
type IdType = PersonId;
- async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
+ async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
- person
- .filter(deleted.eq(false))
+ person::table
+ .filter(person::deleted.eq(false))
.find(person_id)
.first::<Self>(conn)
.await
}
- async fn delete(pool: &DbPool, person_id: PersonId) -> Result<usize, Error> {
+ async fn delete(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
- diesel::delete(person.find(person_id)).execute(conn).await
+ diesel::delete(person::table.find(person_id))
+ .execute(conn)
+ .await
}
- async fn create(pool: &DbPool, form: &PersonInsertForm) -> Result<Self, Error> {
+ async fn create(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
- insert_into(person)
+ insert_into(person::table)
.values(form)
- .on_conflict(actor_id)
- .do_update()
- .set(form)
.get_result::<Self>(conn)
.await
}
async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
person_id: PersonId,
form: &PersonUpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
- diesel::update(person.find(person_id))
+ diesel::update(person::table.find(person_id))
.set(form)
.get_result::<Self>(conn)
.await
}
impl Person {
- pub async fn delete_account(pool: &DbPool, person_id: PersonId) -> Result<Person, Error> {
- use crate::schema::local_user;
+ /// Update or insert the person.
+ ///
+ /// This is necessary for federation, because Activitypub doesnt distinguish between these actions.
+ pub async fn upsert(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result<Self, Error> {
+ let conn = &mut get_conn(pool).await?;
+ insert_into(person::table)
+ .values(form)
+ .on_conflict(person::actor_id)
+ .do_update()
+ .set(form)
+ .get_result::<Self>(conn)
+ .await
+ }
+ pub async fn delete_account(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
// Set the local user info to none
.execute(conn)
.await?;
- diesel::update(person.find(person_id))
+ diesel::update(person::table.find(person_id))
.set((
- display_name.eq::<Option<String>>(None),
- avatar.eq::<Option<String>>(None),
- banner.eq::<Option<String>>(None),
- bio.eq::<Option<String>>(None),
- matrix_user_id.eq::<Option<String>>(None),
- deleted.eq(true),
- updated.eq(naive_now()),
+ person::display_name.eq::<Option<String>>(None),
+ person::avatar.eq::<Option<String>>(None),
+ person::banner.eq::<Option<String>>(None),
+ person::bio.eq::<Option<String>>(None),
+ person::matrix_user_id.eq::<Option<String>>(None),
+ person::deleted.eq(true),
+ person::updated.eq(naive_now()),
))
.get_result::<Self>(conn)
.await
#[async_trait]
impl ApubActor for Person {
- async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
+ async fn read_from_apub_id(
+ pool: &mut DbPool<'_>,
+ object_id: &DbUrl,
+ ) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
Ok(
- person
- .filter(deleted.eq(false))
- .filter(actor_id.eq(object_id))
+ person::table
+ .filter(person::deleted.eq(false))
+ .filter(person::actor_id.eq(object_id))
.first::<Person>(conn)
.await
.ok()
}
async fn read_from_name(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
from_name: &str,
include_deleted: bool,
) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
- let mut q = person
+ let mut q = person::table
.into_boxed()
- .filter(local.eq(true))
- .filter(lower(name).eq(lower(from_name)));
+ .filter(person::local.eq(true))
+ .filter(lower(person::name).eq(from_name.to_lowercase()));
if !include_deleted {
- q = q.filter(deleted.eq(false))
+ q = q.filter(person::deleted.eq(false))
}
q.first::<Self>(conn).await
}
async fn read_from_name_and_domain(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
person_name: &str,
- protocol_domain: &str,
+ for_domain: &str,
) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
- person
- .filter(lower(name).eq(lower(person_name)))
- .filter(actor_id.like(format!("{protocol_domain}%")))
+
+ person::table
+ .inner_join(instance::table)
+ .filter(lower(person::name).eq(person_name.to_lowercase()))
+ .filter(instance::domain.eq(for_domain))
+ .select(person::all_columns)
.first::<Self>(conn)
.await
}
#[async_trait]
impl Followable for PersonFollower {
type Form = PersonFollowerForm;
- async fn follow(pool: &DbPool, form: &PersonFollowerForm) -> Result<Self, Error> {
+ async fn follow(pool: &mut DbPool<'_>, form: &PersonFollowerForm) -> Result<Self, Error> {
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
let conn = &mut get_conn(pool).await?;
insert_into(person_follower)
.get_result::<Self>(conn)
.await
}
- async fn follow_accepted(_: &DbPool, _: CommunityId, _: PersonId) -> Result<Self, Error> {
+ async fn follow_accepted(_: &mut DbPool<'_>, _: CommunityId, _: PersonId) -> Result<Self, Error> {
unimplemented!()
}
- async fn unfollow(pool: &DbPool, form: &PersonFollowerForm) -> Result<usize, Error> {
+ async fn unfollow(pool: &mut DbPool<'_>, form: &PersonFollowerForm) -> Result<usize, Error> {
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
let conn = &mut get_conn(pool).await?;
diesel::delete(
}
impl PersonFollower {
- pub async fn list_followers(pool: &DbPool, person_id_: PersonId) -> Result<Vec<Person>, Error> {
- use crate::schema::{person, person_follower, person_follower::person_id};
+ pub async fn list_followers(
+ pool: &mut DbPool<'_>,
+ for_person_id: PersonId,
+ ) -> Result<Vec<Person>, Error> {
let conn = &mut get_conn(pool).await?;
person_follower::table
- .inner_join(person::table)
- .filter(person_id.eq(person_id_))
+ .inner_join(person::table.on(person_follower::follower_id.eq(person::id)))
+ .filter(person_follower::person_id.eq(for_person_id))
.select(person::all_columns)
.load(conn)
.await
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
#[serial]
async fn follow() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
.unwrap();