2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-07-21 01:37:44 +00:00
|
|
|
naive_now,
|
2020-05-16 14:04:08 +00:00
|
|
|
schema::{community, community_follower, community_moderator, community_user_ban},
|
2020-07-10 18:15:41 +00:00
|
|
|
Bannable,
|
|
|
|
Crud,
|
|
|
|
Followable,
|
|
|
|
Joinable,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "community"]
|
2019-03-04 16:39:07 +00:00
|
|
|
pub struct Community {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
pub title: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub category_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub creator_id: i32,
|
2019-04-20 04:06:25 +00:00
|
|
|
pub removed: bool,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-04-29 19:14:54 +00:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub deleted: bool,
|
2019-08-14 02:52:43 +00:00
|
|
|
pub nsfw: bool,
|
2020-04-03 04:12:05 +00:00
|
|
|
pub actor_id: String,
|
|
|
|
pub local: bool,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: Option<String>,
|
|
|
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub icon: Option<String>,
|
|
|
|
pub banner: Option<String>,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Debug)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "community"]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct CommunityForm {
|
|
|
|
pub name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
pub title: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub category_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub creator_id: i32,
|
2019-04-20 07:24:59 +00:00
|
|
|
pub removed: Option<bool>,
|
2020-04-07 16:47:19 +00:00
|
|
|
pub published: Option<chrono::NaiveDateTime>,
|
2019-04-29 19:14:54 +00:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub deleted: Option<bool>,
|
2019-08-14 02:52:43 +00:00
|
|
|
pub nsfw: bool,
|
2020-08-31 13:48:02 +00:00
|
|
|
pub actor_id: Option<String>,
|
2020-04-03 04:12:05 +00:00
|
|
|
pub local: bool,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: Option<String>,
|
|
|
|
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub icon: Option<Option<String>>,
|
|
|
|
pub banner: Option<Option<String>>,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
impl Crud<CommunityForm> for Community {
|
|
|
|
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
community.find(community_id).first::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(community.find(community_id)).execute(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
insert_into(community)
|
|
|
|
.values(new_community)
|
|
|
|
.get_result::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
fn update(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_id: i32,
|
|
|
|
new_community: &CommunityForm,
|
|
|
|
) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
2019-04-16 23:04:23 +00:00
|
|
|
diesel::update(community.find(community_id))
|
|
|
|
.set(new_community)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
impl Community {
|
2020-04-24 14:04:36 +00:00
|
|
|
pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
community
|
2020-10-02 12:16:40 +00:00
|
|
|
.filter(local)
|
2019-09-07 15:35:05 +00:00
|
|
|
.filter(name.eq(community_name))
|
2019-04-25 21:52:18 +00:00
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2020-07-15 13:55:38 +00:00
|
|
|
pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
|
2020-04-07 16:47:19 +00:00
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
community
|
2020-07-15 13:55:38 +00:00
|
|
|
.filter(actor_id.eq(for_actor_id))
|
2020-04-07 16:47:19 +00:00
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:37:39 +00:00
|
|
|
pub fn update_deleted(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_id: i32,
|
|
|
|
new_deleted: bool,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
diesel::update(community.find(community_id))
|
2020-08-05 16:03:46 +00:00
|
|
|
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
|
2020-07-20 17:37:39 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_removed(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_id: i32,
|
|
|
|
new_removed: bool,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
diesel::update(community.find(community_id))
|
2020-08-05 16:03:46 +00:00
|
|
|
.set((removed.eq(new_removed), updated.eq(naive_now())))
|
2020-07-20 17:37:39 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-08-17 18:12:36 +00:00
|
|
|
pub fn update_removed_for_creator(
|
|
|
|
conn: &PgConnection,
|
|
|
|
for_creator_id: i32,
|
|
|
|
new_removed: bool,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
diesel::update(community.filter(creator_id.eq(for_creator_id)))
|
|
|
|
.set((removed.eq(new_removed), updated.eq(naive_now())))
|
|
|
|
.get_results::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:37:39 +00:00
|
|
|
pub fn update_creator(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_id: i32,
|
|
|
|
new_creator_id: i32,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
diesel::update(community.find(community_id))
|
2020-07-21 01:37:44 +00:00
|
|
|
.set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
|
2020-07-20 17:37:39 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-07-21 01:37:44 +00:00
|
|
|
|
2020-07-28 15:42:40 +00:00
|
|
|
fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
|
2020-07-21 01:37:44 +00:00
|
|
|
use crate::{community_view::CommunityModeratorView, user_view::UserView};
|
|
|
|
let mut mods_and_admins: Vec<i32> = Vec::new();
|
|
|
|
mods_and_admins.append(
|
|
|
|
&mut CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
.map(|v| v.into_iter().map(|m| m.user_id).collect())?,
|
|
|
|
);
|
|
|
|
mods_and_admins
|
|
|
|
.append(&mut UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())?);
|
|
|
|
Ok(mods_and_admins)
|
|
|
|
}
|
2020-07-21 14:15:17 +00:00
|
|
|
|
2020-09-24 13:46:57 +00:00
|
|
|
pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
|
|
|
|
use crate::schema::community::dsl::*;
|
|
|
|
community.select(actor_id).distinct().load::<String>(conn)
|
|
|
|
}
|
|
|
|
|
2020-07-21 14:15:17 +00:00
|
|
|
pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool {
|
|
|
|
Self::community_mods_and_admins(conn, community_id)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.contains(&user_id)
|
|
|
|
}
|
2020-08-31 13:48:02 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-04-25 21:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
|
|
|
#[belongs_to(Community)]
|
2019-04-03 06:49:32 +00:00
|
|
|
#[table_name = "community_moderator"]
|
|
|
|
pub struct CommunityModerator {
|
2019-03-04 16:39:07 +00:00
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "community_moderator"]
|
2019-04-03 06:49:32 +00:00
|
|
|
pub struct CommunityModeratorForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
impl Joinable<CommunityModeratorForm> for CommunityModerator {
|
2019-09-07 15:35:05 +00:00
|
|
|
fn join(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_user_form: &CommunityModeratorForm,
|
|
|
|
) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_moderator::dsl::*;
|
2019-04-16 23:04:23 +00:00
|
|
|
insert_into(community_moderator)
|
|
|
|
.values(community_user_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
fn leave(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_user_form: &CommunityModeratorForm,
|
|
|
|
) -> Result<usize, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_moderator::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_moderator
|
|
|
|
.filter(community_id.eq(community_user_form.community_id))
|
|
|
|
.filter(user_id.eq(community_user_form.user_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 02:40:41 +00:00
|
|
|
impl CommunityModerator {
|
|
|
|
pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
|
|
|
|
use crate::schema::community_moderator::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
|
|
|
#[belongs_to(Community)]
|
|
|
|
#[table_name = "community_user_ban"]
|
|
|
|
pub struct CommunityUserBan {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "community_user_ban"]
|
2019-04-15 23:12:06 +00:00
|
|
|
pub struct CommunityUserBanForm {
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
impl Bannable<CommunityUserBanForm> for CommunityUserBan {
|
2019-09-07 15:35:05 +00:00
|
|
|
fn ban(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_user_ban_form: &CommunityUserBanForm,
|
|
|
|
) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_user_ban::dsl::*;
|
2019-04-16 23:04:23 +00:00
|
|
|
insert_into(community_user_ban)
|
|
|
|
.values(community_user_ban_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
fn unban(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_user_ban_form: &CommunityUserBanForm,
|
|
|
|
) -> Result<usize, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_user_ban::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_user_ban
|
|
|
|
.filter(community_id.eq(community_user_ban_form.community_id))
|
|
|
|
.filter(user_id.eq(community_user_ban_form.user_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
|
|
|
#[belongs_to(Community)]
|
|
|
|
#[table_name = "community_follower"]
|
|
|
|
pub struct CommunityFollower {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "community_follower"]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct CommunityFollowerForm {
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
impl Followable<CommunityFollowerForm> for CommunityFollower {
|
2019-09-07 15:35:05 +00:00
|
|
|
fn follow(
|
|
|
|
conn: &PgConnection,
|
|
|
|
community_follower_form: &CommunityFollowerForm,
|
|
|
|
) -> Result<Self, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_follower::dsl::*;
|
2019-03-04 16:39:07 +00:00
|
|
|
insert_into(community_follower)
|
|
|
|
.values(community_follower_form)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2020-05-04 18:26:16 +00:00
|
|
|
fn unfollow(
|
2019-09-07 15:35:05 +00:00
|
|
|
conn: &PgConnection,
|
|
|
|
community_follower_form: &CommunityFollowerForm,
|
|
|
|
) -> Result<usize, Error> {
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::community_follower::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_follower
|
|
|
|
.filter(community_id.eq(&community_follower_form.community_id))
|
|
|
|
.filter(user_id.eq(&community_follower_form.user_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-10 18:15:41 +00:00
|
|
|
use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType};
|
2020-05-16 14:04:08 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[test]
|
2019-03-04 16:39:07 +00:00
|
|
|
fn test_crud() {
|
2020-01-12 15:31:51 +00:00
|
|
|
let conn = establish_unpooled_connection();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let new_user = UserForm {
|
2019-04-15 23:12:06 +00:00
|
|
|
name: "bobbee".into(),
|
2019-04-03 06:49:32 +00:00
|
|
|
preferred_username: None,
|
|
|
|
password_encrypted: "nope".into(),
|
|
|
|
email: None,
|
2020-01-22 21:35:29 +00:00
|
|
|
matrix_user_id: None,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner: None,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin: false,
|
|
|
|
banned: false,
|
2020-09-18 11:04:12 +00:00
|
|
|
published: None,
|
2019-08-14 03:15:52 +00:00
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
2020-10-03 16:44:25 +00:00
|
|
|
theme: "browser".into(),
|
2019-10-21 04:21:54 +00:00
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
lang: "browser".into(),
|
2020-01-02 21:55:54 +00:00
|
|
|
show_avatars: true,
|
|
|
|
send_notifications_to_email: false,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: None,
|
2020-04-03 04:12:05 +00:00
|
|
|
bio: None,
|
|
|
|
local: true,
|
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: None,
|
2019-04-03 06:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let new_community = CommunityForm {
|
|
|
|
name: "TIL".into(),
|
2019-04-03 20:59:37 +00:00
|
|
|
creator_id: inserted_user.id,
|
|
|
|
title: "nada".to_owned(),
|
|
|
|
description: None,
|
|
|
|
category_id: 1,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: None,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: None,
|
2019-04-03 06:49:32 +00:00
|
|
|
updated: None,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: None,
|
2020-04-03 04:12:05 +00:00
|
|
|
local: true,
|
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: None,
|
2020-04-07 16:47:19 +00:00
|
|
|
published: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: None,
|
|
|
|
banner: None,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
let expected_community = Community {
|
|
|
|
id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
creator_id: inserted_user.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
name: "TIL".into(),
|
2019-04-03 20:59:37 +00:00
|
|
|
title: "nada".to_owned(),
|
|
|
|
description: None,
|
|
|
|
category_id: 1,
|
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,
|
2020-07-19 17:12:42 +00:00
|
|
|
actor_id: inserted_community.actor_id.to_owned(),
|
2020-04-03 04:12:05 +00:00
|
|
|
local: true,
|
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: inserted_community.published,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: None,
|
|
|
|
banner: None,
|
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,
|
2019-09-07 15:35:05 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
let inserted_community_follower =
|
|
|
|
CommunityFollower::follow(&conn, &community_follower_form).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,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id,
|
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
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let community_user_form = CommunityModeratorForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
community_id: inserted_community.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let expected_community_user = CommunityModerator {
|
2019-03-04 16:39:07 +00:00
|
|
|
id: inserted_community_user.id,
|
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
published: inserted_community_user.published,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
let community_user_ban_form = CommunityUserBanForm {
|
|
|
|
community_id: inserted_community.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-04-15 23:12:06 +00:00
|
|
|
};
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
let inserted_community_user_ban =
|
|
|
|
CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let expected_community_user_ban = CommunityUserBan {
|
|
|
|
id: inserted_community_user_ban.id,
|
|
|
|
community_id: inserted_community.id,
|
|
|
|
user_id: inserted_user.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
published: inserted_community_user_ban.published,
|
2019-04-15 23:12:06 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let read_community = Community::read(&conn, inserted_community.id).unwrap();
|
2019-09-07 15:35:05 +00:00
|
|
|
let updated_community =
|
|
|
|
Community::update(&conn, inserted_community.id, &new_community).unwrap();
|
2020-05-04 18:26:16 +00:00
|
|
|
let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
|
2019-04-03 06:49:32 +00:00
|
|
|
let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
|
2019-04-15 23:12:06 +00:00
|
|
|
let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
|
2019-03-23 01:42:57 +00:00
|
|
|
let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
|
|
|
|
User_::delete(&conn, inserted_user.id).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);
|
|
|
|
assert_eq!(expected_community_user, inserted_community_user);
|
2019-04-15 23:12:06 +00:00
|
|
|
assert_eq!(expected_community_user_ban, inserted_community_user_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);
|
|
|
|
}
|
|
|
|
}
|