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 /// Use SiteView::read_local, or Site::read_from_apub_id instead
22 async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result<Self, Error> {
26 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
27 let is_new_site = match &form.actor_id {
28 Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(),
31 let conn = &mut get_conn(pool).await?;
33 // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
34 let site_ = insert_into(site)
36 .on_conflict(actor_id)
39 .get_result::<Self>(conn)
42 // initialize languages if site is newly created
44 // initialize with all languages
45 SiteLanguage::update(pool, vec![], &site_).await?;
51 pool: &mut DbPool<'_>,
53 new_site: &Self::UpdateForm,
54 ) -> Result<Self, Error> {
55 let conn = &mut get_conn(pool).await?;
56 diesel::update(site.find(site_id))
58 .get_result::<Self>(conn)
62 async fn delete(pool: &mut DbPool<'_>, site_id: SiteId) -> Result<usize, Error> {
63 let conn = &mut get_conn(pool).await?;
64 diesel::delete(site.find(site_id)).execute(conn).await
69 pub async fn read_from_apub_id(
70 pool: &mut DbPool<'_>,
72 ) -> Result<Option<Self>, Error> {
73 let conn = &mut get_conn(pool).await?;
76 .filter(actor_id.eq(object_id))
84 // TODO this needs fixed
85 pub async fn read_remote_sites(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
86 let conn = &mut get_conn(pool).await?;
87 site.order_by(id).offset(1).get_results::<Self>(conn).await
90 /// Instance actor is at the root path, so we simply need to clear the path and other unnecessary
92 pub fn instance_actor_id_from_url(mut url: Url) -> Url {
93 url.set_fragment(None);