- // 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
- fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
- use crate::schema::community_follower::dsl::*;
- diesel::select(exists(
- community_follower.filter(community_id.eq(community_id_)),
- ))
- .get_result(conn)
+}
+
+#[async_trait]
+impl ApubActor for Community {
+ async fn read_from_apub_id(
+ pool: &mut DbPool<'_>,
+ object_id: &DbUrl,
+ ) -> Result<Option<Self>, Error> {
+ let conn = &mut get_conn(pool).await?;
+ Ok(
+ community::table
+ .filter(community::actor_id.eq(object_id))
+ .first::<Community>(conn)
+ .await
+ .ok()
+ .map(Into::into),
+ )
+ }
+
+ async fn read_from_name(
+ pool: &mut DbPool<'_>,
+ community_name: &str,
+ include_deleted: bool,
+ ) -> Result<Community, Error> {
+ let conn = &mut get_conn(pool).await?;
+ let mut q = community::table
+ .into_boxed()
+ .filter(community::local.eq(true))
+ .filter(lower(community::name).eq(community_name.to_lowercase()));
+ if !include_deleted {
+ q = q
+ .filter(community::deleted.eq(false))
+ .filter(community::removed.eq(false));
+ }
+ q.first::<Self>(conn).await
+ }
+
+ async fn read_from_name_and_domain(
+ pool: &mut DbPool<'_>,
+ community_name: &str,
+ for_domain: &str,
+ ) -> Result<Community, Error> {
+ let conn = &mut get_conn(pool).await?;
+ community::table
+ .inner_join(instance::table)
+ .filter(lower(community::name).eq(community_name.to_lowercase()))
+ .filter(instance::domain.eq(for_domain))
+ .select(community::all_columns)
+ .first::<Self>(conn)
+ .await