2 newtypes::{DbUrl, SiteId},
3 schema::site::dsl::{actor_id, id, site},
5 actor_language::SiteLanguage,
6 site::{Site, SiteInsertForm, SiteUpdateForm},
9 utils::{get_conn, DbPool},
11 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
12 use diesel_async::RunQueryDsl;
17 type InsertForm = SiteInsertForm;
18 type UpdateForm = SiteUpdateForm;
21 async fn read(pool: &DbPool, _site_id: SiteId) -> Result<Self, Error> {
22 let conn = &mut get_conn(pool).await?;
23 site.first::<Self>(conn).await
26 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
27 let conn = &mut get_conn(pool).await?;
28 let site_ = insert_into(site)
30 .on_conflict(actor_id)
33 .get_result::<Self>(conn)
36 // initialize with all languages
37 SiteLanguage::update(pool, vec![], &site_).await?;
44 new_site: &Self::UpdateForm,
45 ) -> Result<Self, Error> {
46 let conn = &mut get_conn(pool).await?;
47 diesel::update(site.find(site_id))
49 .get_result::<Self>(conn)
53 async fn delete(pool: &DbPool, site_id: SiteId) -> Result<usize, Error> {
54 let conn = &mut get_conn(pool).await?;
55 diesel::delete(site.find(site_id)).execute(conn).await
60 pub async fn read_from_apub_id(pool: &DbPool, object_id: Url) -> Result<Option<Self>, Error> {
61 let conn = &mut get_conn(pool).await?;
62 let object_id: DbUrl = object_id.into();
65 .filter(actor_id.eq(object_id))
73 // TODO this needs fixed
74 pub async fn read_remote_sites(pool: &DbPool) -> Result<Vec<Self>, Error> {
75 let conn = &mut get_conn(pool).await?;
76 site.order_by(id).offset(1).get_results::<Self>(conn).await