2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{CommunityId, DbUrl, PersonId},
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::community::dsl::{actor_id, community, deleted, local, name, removed},
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
|
|
|
actor_language::{CommunityLanguage, SiteLanguage},
|
|
|
|
community::{
|
|
|
|
Community,
|
|
|
|
CommunityFollower,
|
|
|
|
CommunityFollowerForm,
|
2022-10-27 09:24:07 +00:00
|
|
|
CommunityInsertForm,
|
2022-10-06 18:27:58 +00:00
|
|
|
CommunityModerator,
|
|
|
|
CommunityModeratorForm,
|
|
|
|
CommunityPersonBan,
|
|
|
|
CommunityPersonBanForm,
|
|
|
|
CommunitySafe,
|
2022-10-27 09:24:07 +00:00
|
|
|
CommunityUpdateForm,
|
2022-10-06 18:27:58 +00:00
|
|
|
},
|
2020-12-21 12:28:12 +00:00
|
|
|
},
|
2022-01-27 16:39:22 +00:00
|
|
|
traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{functions::lower, get_conn, DbPool},
|
2022-06-22 12:05:41 +00:00
|
|
|
SubscribedType,
|
2022-01-27 16:39:22 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{
|
|
|
|
dsl::{exists, insert_into},
|
|
|
|
result::Error,
|
|
|
|
ExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
TextExpressionMethods,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2020-12-06 03:49:15 +00:00
|
|
|
|
|
|
|
mod safe_type {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::{
|
|
|
|
schema::community::{
|
|
|
|
actor_id,
|
|
|
|
banner,
|
|
|
|
deleted,
|
|
|
|
description,
|
|
|
|
hidden,
|
|
|
|
icon,
|
|
|
|
id,
|
|
|
|
instance_id,
|
|
|
|
local,
|
|
|
|
name,
|
|
|
|
nsfw,
|
|
|
|
posting_restricted_to_mods,
|
|
|
|
published,
|
|
|
|
removed,
|
|
|
|
title,
|
|
|
|
updated,
|
|
|
|
},
|
|
|
|
source::community::Community,
|
|
|
|
traits::ToSafe,
|
|
|
|
};
|
2020-12-21 12:28:12 +00:00
|
|
|
|
2020-12-06 03:49:15 +00:00
|
|
|
type Columns = (
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
removed,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
deleted,
|
|
|
|
nsfw,
|
|
|
|
actor_id,
|
|
|
|
local,
|
|
|
|
icon,
|
|
|
|
banner,
|
2022-02-18 02:30:47 +00:00
|
|
|
hidden,
|
2022-04-28 20:32:32 +00:00
|
|
|
posting_restricted_to_mods,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id,
|
2020-12-06 03:49:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl ToSafe for Community {
|
|
|
|
type SafeColumns = Columns;
|
|
|
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
|
|
|
(
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
removed,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
deleted,
|
|
|
|
nsfw,
|
|
|
|
actor_id,
|
|
|
|
local,
|
|
|
|
icon,
|
|
|
|
banner,
|
2022-02-18 02:30:47 +00:00
|
|
|
hidden,
|
2022-04-28 20:32:32 +00:00
|
|
|
posting_restricted_to_mods,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id,
|
2020-12-06 03:49:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Community {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = CommunityInsertForm;
|
|
|
|
type UpdateForm = CommunityUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = CommunityId;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read(pool: &DbPool, community_id: CommunityId) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
community.find(community_id).first::<Self>(conn).await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn delete(pool: &DbPool, community_id: CommunityId) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(community.find(community_id))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
let community_ = insert_into(community)
|
2022-10-27 09:24:07 +00:00
|
|
|
.values(form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
.await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let site_languages = SiteLanguage::read_local(pool).await;
|
2022-10-06 18:27:58 +00:00
|
|
|
if let Ok(langs) = site_languages {
|
|
|
|
// if site exists, init user with site languages
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityLanguage::update(pool, langs, community_.id).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
} else {
|
|
|
|
// otherwise, init with all languages (this only happens during tests)
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityLanguage::update(pool, vec![], community_.id).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(community_)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
community_id: CommunityId,
|
2022-10-27 09:24:07 +00:00
|
|
|
form: &Self::UpdateForm,
|
2019-09-07 15:35:05 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-04-16 23:04:23 +00:00
|
|
|
diesel::update(community.find(community_id))
|
2022-10-27 09:24:07 +00:00
|
|
|
.set(form)
|
2022-06-13 19:15:04 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
2019-04-25 21:52:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Joinable for CommunityModerator {
|
|
|
|
type Form = CommunityModeratorForm;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn join(
|
|
|
|
pool: &DbPool,
|
2021-02-26 13:49:58 +00:00
|
|
|
community_moderator_form: &CommunityModeratorForm,
|
2019-09-07 15:35:05 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_moderator::dsl::community_moderator;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-04-16 23:04:23 +00:00
|
|
|
insert_into(community_moderator)
|
2021-02-26 13:49:58 +00:00
|
|
|
.values(community_moderator_form)
|
2019-04-16 23:04:23 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn leave(
|
|
|
|
pool: &DbPool,
|
2021-02-26 13:49:58 +00:00
|
|
|
community_moderator_form: &CommunityModeratorForm,
|
2019-09-07 15:35:05 +00:00
|
|
|
) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_moderator
|
2021-02-26 13:49:58 +00:00
|
|
|
.filter(community_id.eq(community_moderator_form.community_id))
|
|
|
|
.filter(person_id.eq(community_moderator_form.person_id)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
impl DeleteableOrRemoveable for CommunitySafe {
|
|
|
|
fn blank_out_deleted_or_removed_info(mut self) -> Self {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.title = String::new();
|
2021-07-30 18:44:15 +00:00
|
|
|
self.description = None;
|
|
|
|
self.icon = None;
|
|
|
|
self.banner = None;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeleteableOrRemoveable for Community {
|
|
|
|
fn blank_out_deleted_or_removed_info(mut self) -> Self {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.title = String::new();
|
2021-07-30 18:44:15 +00:00
|
|
|
self.description = None;
|
|
|
|
self.icon = None;
|
|
|
|
self.banner = None;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl CommunityModerator {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn delete_for_community(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_community_id: CommunityId,
|
|
|
|
) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_moderator::dsl::{community_id, community_moderator};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
|
|
|
|
diesel::delete(community_moderator.filter(community_id.eq(for_community_id)))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn get_person_moderated_communities(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_person_id: PersonId,
|
|
|
|
) -> Result<Vec<CommunityId>, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
community_moderator
|
2021-02-26 13:49:58 +00:00
|
|
|
.filter(person_id.eq(for_person_id))
|
2020-11-04 02:15:11 +00:00
|
|
|
.select(community_id)
|
2021-03-18 20:25:21 +00:00
|
|
|
.load::<CommunityId>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-10-25 02:59:13 +00:00
|
|
|
}
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Bannable for CommunityPersonBan {
|
|
|
|
type Form = CommunityPersonBanForm;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn ban(
|
|
|
|
pool: &DbPool,
|
2021-02-26 13:49:58 +00:00
|
|
|
community_person_ban_form: &CommunityPersonBanForm,
|
2019-09-07 15:35:05 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-02-26 13:49:58 +00:00
|
|
|
insert_into(community_person_ban)
|
|
|
|
.values(community_person_ban_form)
|
2022-01-08 12:37:07 +00:00
|
|
|
.on_conflict((community_id, person_id))
|
|
|
|
.do_update()
|
|
|
|
.set(community_person_ban_form)
|
2019-04-16 23:04:23 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn unban(
|
|
|
|
pool: &DbPool,
|
2021-02-26 13:49:58 +00:00
|
|
|
community_person_ban_form: &CommunityPersonBanForm,
|
2019-09-07 15:35:05 +00:00
|
|
|
) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
2021-02-26 13:49:58 +00:00
|
|
|
community_person_ban
|
|
|
|
.filter(community_id.eq(community_person_ban_form.community_id))
|
|
|
|
.filter(person_id.eq(community_person_ban_form.person_id)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 12:05:41 +00:00
|
|
|
impl CommunityFollower {
|
|
|
|
pub fn to_subscribed_type(follower: &Option<Self>) -> SubscribedType {
|
|
|
|
match follower {
|
|
|
|
Some(f) => {
|
|
|
|
if f.pending.unwrap_or(false) {
|
|
|
|
SubscribedType::Pending
|
|
|
|
} else {
|
|
|
|
SubscribedType::Subscribed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the row doesn't exist, the person isn't a follower.
|
|
|
|
None => SubscribedType::NotSubscribed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Followable for CommunityFollower {
|
|
|
|
type Form = CommunityFollowerForm;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn follow(
|
|
|
|
pool: &DbPool,
|
2019-09-07 15:35:05 +00:00
|
|
|
community_follower_form: &CommunityFollowerForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-03-04 16:39:07 +00:00
|
|
|
insert_into(community_follower)
|
|
|
|
.values(community_follower_form)
|
2021-03-10 22:33:55 +00:00
|
|
|
.on_conflict((community_id, person_id))
|
2020-12-16 14:42:57 +00:00
|
|
|
.do_update()
|
|
|
|
.set(community_follower_form)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn follow_accepted(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
community_id_: CommunityId,
|
|
|
|
person_id_: PersonId,
|
2022-11-09 10:05:00 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_follower::dsl::{
|
|
|
|
community_follower,
|
|
|
|
community_id,
|
|
|
|
pending,
|
|
|
|
person_id,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-11-10 15:45:10 +00:00
|
|
|
diesel::update(
|
|
|
|
community_follower
|
|
|
|
.filter(community_id.eq(community_id_))
|
2021-03-10 22:33:55 +00:00
|
|
|
.filter(person_id.eq(person_id_)),
|
2020-11-10 15:45:10 +00:00
|
|
|
)
|
2022-07-20 14:10:29 +00:00
|
|
|
.set(pending.eq(false))
|
2020-11-10 15:45:10 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-11-10 15:45:10 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn unfollow(
|
|
|
|
pool: &DbPool,
|
2019-09-07 15:35:05 +00:00
|
|
|
community_follower_form: &CommunityFollowerForm,
|
|
|
|
) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_follower
|
|
|
|
.filter(community_id.eq(&community_follower_form.community_id))
|
2021-03-10 22:33:55 +00:00
|
|
|
.filter(person_id.eq(&community_follower_form.person_id)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2020-11-11 16:40:45 +00:00
|
|
|
// TODO: this function name only makes sense if you call it with a remote community. for a local
|
|
|
|
// community, it will also return true if only remote followers exist
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn has_local_followers(pool: &DbPool, community_id_: CommunityId) -> Result<bool, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::community_follower::dsl::{community_follower, community_id};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-11-11 16:40:45 +00:00
|
|
|
diesel::select(exists(
|
|
|
|
community_follower.filter(community_id.eq(community_id_)),
|
|
|
|
))
|
|
|
|
.get_result(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-11-11 16:40:45 +00:00
|
|
|
}
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2022-01-27 16:39:22 +00:00
|
|
|
impl ApubActor for Community {
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-01-27 16:39:22 +00:00
|
|
|
Ok(
|
|
|
|
community
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Community>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-01-27 16:39:22 +00:00
|
|
|
.ok()
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name(
|
|
|
|
pool: &DbPool,
|
2022-07-05 21:40:44 +00:00
|
|
|
community_name: &str,
|
|
|
|
include_deleted: bool,
|
|
|
|
) -> Result<Community, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-07-05 21:40:44 +00:00
|
|
|
let mut q = community
|
|
|
|
.into_boxed()
|
2022-01-27 16:39:22 +00:00
|
|
|
.filter(local.eq(true))
|
2022-07-05 21:40:44 +00:00
|
|
|
.filter(lower(name).eq(lower(community_name)));
|
|
|
|
if !include_deleted {
|
|
|
|
q = q.filter(deleted.eq(false)).filter(removed.eq(false));
|
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
q.first::<Self>(conn).await
|
2022-01-27 16:39:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name_and_domain(
|
|
|
|
pool: &DbPool,
|
2022-01-27 16:39:22 +00:00
|
|
|
community_name: &str,
|
|
|
|
protocol_domain: &str,
|
|
|
|
) -> Result<Community, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-01-27 16:39:22 +00:00
|
|
|
community
|
|
|
|
.filter(lower(name).eq(lower(community_name)))
|
|
|
|
.filter(actor_id.like(format!("{}%", protocol_domain)))
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-01-27 16:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2022-11-19 04:33:54 +00:00
|
|
|
source::{
|
|
|
|
community::{
|
|
|
|
Community,
|
|
|
|
CommunityFollower,
|
|
|
|
CommunityFollowerForm,
|
|
|
|
CommunityInsertForm,
|
|
|
|
CommunityModerator,
|
|
|
|
CommunityModeratorForm,
|
|
|
|
CommunityPersonBan,
|
|
|
|
CommunityPersonBanForm,
|
|
|
|
CommunityUpdateForm,
|
|
|
|
},
|
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{Bannable, Crud, Followable, Joinable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2020-05-16 14:04:08 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_crud() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let new_person = PersonInsertForm::builder()
|
|
|
|
.name("bobbee".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_person = Person::create(pool, &new_person).await.unwrap();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_community = CommunityInsertForm::builder()
|
|
|
|
.name("TIL".into())
|
|
|
|
.title("nada".to_owned())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_community = Community::create(pool, &new_community).await.unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
let expected_community = Community {
|
|
|
|
id: inserted_community.id,
|
|
|
|
name: "TIL".into(),
|
2019-04-03 20:59:37 +00:00
|
|
|
title: "nada".to_owned(),
|
|
|
|
description: None,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
2019-04-20 04:06:25 +00:00
|
|
|
removed: false,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: false,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_community.published,
|
2019-09-07 15:35:05 +00:00
|
|
|
updated: None,
|
2022-11-19 04:33:54 +00:00
|
|
|
actor_id: inserted_community.actor_id.clone(),
|
2020-04-03 04:12:05 +00:00
|
|
|
local: true,
|
|
|
|
private_key: None,
|
2022-07-11 18:25:33 +00:00
|
|
|
public_key: "pubkey".to_owned(),
|
2020-04-03 04:12:05 +00:00
|
|
|
last_refreshed_at: inserted_community.published,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: None,
|
|
|
|
banner: None,
|
2022-11-19 04:33:54 +00:00
|
|
|
followers_url: inserted_community.followers_url.clone(),
|
|
|
|
inbox_url: inserted_community.inbox_url.clone(),
|
2021-02-04 16:34:58 +00:00
|
|
|
shared_inbox_url: None,
|
2022-02-18 02:30:47 +00:00
|
|
|
hidden: false,
|
2022-04-28 20:32:32 +00:00
|
|
|
posting_restricted_to_mods: false,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id: inserted_instance.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
2022-05-17 18:02:48 +00:00
|
|
|
pending: false,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_community_follower = CommunityFollower::follow(pool, &community_follower_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let expected_community_follower = CommunityFollower {
|
|
|
|
id: inserted_community_follower.id,
|
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
2022-05-26 15:17:04 +00:00
|
|
|
pending: Some(false),
|
2019-09-07 15:35:05 +00:00
|
|
|
published: inserted_community_follower.published,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_community_moderator = CommunityModerator::join(pool, &community_moderator_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let expected_community_moderator = CommunityModerator {
|
|
|
|
id: inserted_community_moderator.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
|
|
|
published: inserted_community_moderator.published,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let community_person_ban_form = CommunityPersonBanForm {
|
2019-04-15 23:12:06 +00:00
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
2022-01-08 12:37:07 +00:00
|
|
|
expires: None,
|
2019-04-15 23:12:06 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_community_person_ban = CommunityPersonBan::ban(pool, &community_person_ban_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let expected_community_person_ban = CommunityPersonBan {
|
|
|
|
id: inserted_community_person_ban.id,
|
2019-04-15 23:12:06 +00:00
|
|
|
community_id: inserted_community.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
|
|
|
published: inserted_community_person_ban.published,
|
2022-01-08 12:37:07 +00:00
|
|
|
expires: None,
|
2019-04-15 23:12:06 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let read_community = Community::read(pool, inserted_community.id).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let update_community_form = CommunityUpdateForm::builder()
|
|
|
|
.title(Some("nada".to_owned()))
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let updated_community = Community::update(pool, inserted_community.id, &update_community_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let ignored_community = CommunityFollower::unfollow(pool, &community_follower_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let left_community = CommunityModerator::leave(pool, &community_moderator_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let unban = CommunityPersonBan::unban(pool, &community_person_ban_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let num_deleted = Community::delete(pool, inserted_community.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Person::delete(pool, inserted_person.id).await.unwrap();
|
|
|
|
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
assert_eq!(expected_community, read_community);
|
|
|
|
assert_eq!(expected_community, inserted_community);
|
|
|
|
assert_eq!(expected_community, updated_community);
|
|
|
|
assert_eq!(expected_community_follower, inserted_community_follower);
|
2021-02-26 13:49:58 +00:00
|
|
|
assert_eq!(expected_community_moderator, inserted_community_moderator);
|
|
|
|
assert_eq!(expected_community_person_ban, inserted_community_person_ban);
|
2019-03-04 16:39:07 +00:00
|
|
|
assert_eq!(1, ignored_community);
|
|
|
|
assert_eq!(1, left_community);
|
2019-04-15 23:12:06 +00:00
|
|
|
assert_eq!(1, unban);
|
2019-04-03 06:49:32 +00:00
|
|
|
// assert_eq!(2, loaded_count);
|
2019-03-04 16:39:07 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|