use crate::{
+ functions::lower,
naive_now,
newtypes::{CommunityId, DbUrl, PersonId},
source::community::{
CommunityPersonBanForm,
CommunitySafe,
},
- traits::{Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
+ traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
+};
+use diesel::{
+ dsl::*,
+ result::Error,
+ ExpressionMethods,
+ PgConnection,
+ QueryDsl,
+ RunQueryDsl,
+ TextExpressionMethods,
};
-use chrono::NaiveDateTime;
-use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
-use lemmy_apub_lib::traits::{ActorType, ApubObject};
-use lemmy_utils::LemmyError;
-use url::Url;
mod safe_type {
use crate::{schema::community::*, source::community::Community, traits::ToSafe};
local,
icon,
banner,
+ hidden,
+ posting_restricted_to_mods,
);
impl ToSafe for Community {
local,
icon,
banner,
+ hidden,
+ posting_restricted_to_mods,
)
}
}
}
impl Community {
- pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
- use crate::schema::community::dsl::*;
- community
- .filter(local.eq(true))
- .filter(name.eq(community_name))
- .first::<Self>(conn)
- }
-
pub fn update_deleted(
conn: &PgConnection,
community_id: CommunityId,
.get_result::<Self>(conn)
}
- pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
- use crate::schema::community::dsl::*;
- community.select(actor_id).distinct().load::<String>(conn)
- }
-
- pub fn read_from_followers_url(
- conn: &PgConnection,
- followers_url_: &DbUrl,
- ) -> Result<Community, Error> {
+ pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<DbUrl>, Error> {
use crate::schema::community::dsl::*;
- community
- .filter(followers_url.eq(followers_url_))
- .first::<Self>(conn)
+ community.select(actor_id).distinct().load::<DbUrl>(conn)
}
pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
use crate::schema::community_person_ban::dsl::*;
insert_into(community_person_ban)
.values(community_person_ban_form)
+ .on_conflict((community_id, person_id))
+ .do_update()
+ .set(community_person_ban_form)
.get_result::<Self>(conn)
}
}
}
-impl ApubObject for Community {
- type DataType = PgConnection;
-
- fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
- Some(self.last_refreshed_at)
- }
-
- fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
+impl ApubActor for Community {
+ fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
use crate::schema::community::dsl::*;
- let object_id: DbUrl = object_id.into();
Ok(
community
.filter(actor_id.eq(object_id))
- .first::<Self>(conn)
- .ok(),
+ .first::<Community>(conn)
+ .ok()
+ .map(Into::into),
)
}
- fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
+ fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
use crate::schema::community::dsl::*;
- diesel::update(community.find(self.id))
- .set((deleted.eq(true), updated.eq(naive_now())))
- .get_result::<Self>(conn)?;
- Ok(())
- }
-}
-
-impl ActorType for Community {
- fn is_local(&self) -> bool {
- self.local
- }
- fn actor_id(&self) -> Url {
- self.actor_id.to_owned().into()
- }
- fn name(&self) -> String {
- self.name.clone()
- }
- fn public_key(&self) -> Option<String> {
- self.public_key.to_owned()
- }
- fn private_key(&self) -> Option<String> {
- self.private_key.to_owned()
- }
-
- fn inbox_url(&self) -> Url {
- self.inbox_url.clone().into()
+ community
+ .filter(local.eq(true))
+ .filter(lower(name).eq(lower(community_name)))
+ .first::<Self>(conn)
}
- fn shared_inbox_url(&self) -> Option<Url> {
- self.shared_inbox_url.clone().map(|s| s.into_inner())
+ fn read_from_name_and_domain(
+ conn: &PgConnection,
+ community_name: &str,
+ protocol_domain: &str,
+ ) -> Result<Community, Error> {
+ use crate::schema::community::dsl::*;
+ community
+ .filter(lower(name).eq(lower(community_name)))
+ .filter(actor_id.like(format!("{}%", protocol_domain)))
+ .first::<Self>(conn)
}
}
let new_community = CommunityForm {
name: "TIL".into(),
title: "nada".to_owned(),
+ public_key: "nada".to_owned(),
..CommunityForm::default()
};
actor_id: inserted_community.actor_id.to_owned(),
local: true,
private_key: None,
- public_key: None,
+ public_key: "nada".to_owned(),
last_refreshed_at: inserted_community.published,
icon: None,
banner: None,
followers_url: inserted_community.followers_url.to_owned(),
inbox_url: inserted_community.inbox_url.to_owned(),
shared_inbox_url: None,
+ hidden: false,
+ posting_restricted_to_mods: false,
};
let community_follower_form = CommunityFollowerForm {
let community_person_ban_form = CommunityPersonBanForm {
community_id: inserted_community.id,
person_id: inserted_person.id,
+ expires: None,
};
let inserted_community_person_ban =
community_id: inserted_community.id,
person_id: inserted_person.id,
published: inserted_community_person_ban.published,
+ expires: None,
};
let read_community = Community::read(&conn, inserted_community.id).unwrap();