2 newtypes::{CommunityId, DbUrl, PersonId},
3 utils::{get_conn, DbPool},
6 associations::HasTable,
8 query_builder::{DeleteStatement, IntoUpdateTarget},
9 query_dsl::methods::{FindDsl, LimitDsl},
14 methods::{ExecuteDsl, LoadQuery},
19 /// Returned by `diesel::delete`
20 pub type Delete<T> = DeleteStatement<<T as HasTable>::Table, <T as IntoUpdateTarget>::WhereClause>;
22 /// Returned by `Self::table().find(id)`
23 pub type Find<T> = dsl::Find<<T as HasTable>::Table, <T as Crud>::IdType>;
25 pub type PrimaryKey<T> = <<T as HasTable>::Table as Table>::PrimaryKey;
27 // Trying to create default implementations for `create` and `update` results in a lifetime mess and weird compile errors.
28 // https://github.com/rust-lang/rust/issues/102211
30 pub trait Crud: HasTable + Sized
32 Self::Table: FindDsl<Self::IdType>,
33 Find<Self>: LimitDsl + IntoUpdateTarget + Send,
34 Delete<Find<Self>>: ExecuteDsl<AsyncPgConnection> + Send + 'static,
36 // Used by `RunQueryDsl::first`
37 dsl::Limit<Find<Self>>: LoadQuery<'static, AsyncPgConnection, Self> + Send + 'static,
43 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>;
45 async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> {
46 let query: Find<Self> = Self::table().find(id);
47 let conn = &mut *get_conn(pool).await?;
48 query.first::<Self>(conn).await
51 /// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
53 pool: &mut DbPool<'_>,
55 form: &Self::UpdateForm,
56 ) -> Result<Self, Error>;
58 async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> {
59 let query: Delete<Find<Self>> = diesel::delete(Self::table().find(id));
60 let conn = &mut *get_conn(pool).await?;
61 query.execute(conn).await
66 pub trait Followable {
68 async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
71 async fn follow_accepted(
72 pool: &mut DbPool<'_>,
73 community_id: CommunityId,
75 ) -> Result<Self, Error>
78 async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
86 async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
89 async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
98 async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
102 pool: &mut DbPool<'_>,
104 item_id: Self::IdType,
105 ) -> Result<usize, Error>
113 async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
116 async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
124 async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
127 async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
133 pub trait Blockable {
135 async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
138 async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
146 async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
149 async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
155 pub trait Reportable {
159 async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
163 pool: &mut DbPool<'_>,
164 report_id: Self::IdType,
165 resolver_id: PersonId,
166 ) -> Result<usize, Error>
169 async fn resolve_all_for_object(
170 pool: &mut DbPool<'_>,
171 comment_id_: Self::ObjectIdType,
172 by_resolver_id: PersonId,
173 ) -> Result<usize, Error>
177 pool: &mut DbPool<'_>,
178 report_id: Self::IdType,
179 resolver_id: PersonId,
180 ) -> Result<usize, Error>
187 fn from_tuple(tuple: Self::JoinTuple) -> Self
193 pub trait ApubActor {
194 async fn read_from_apub_id(
195 pool: &mut DbPool<'_>,
197 ) -> Result<Option<Self>, Error>
200 /// - actor_name is the name of the community or user to read.
201 /// - include_deleted, if true, will return communities or users that were deleted/removed
202 async fn read_from_name(
203 pool: &mut DbPool<'_>,
205 include_deleted: bool,
206 ) -> Result<Self, Error>
209 async fn read_from_name_and_domain(
210 pool: &mut DbPool<'_>,
212 protocol_domain: &str,
213 ) -> Result<Self, Error>