2022-10-06 18:27:58 +00:00
|
|
|
use crate::{
|
2023-09-09 16:25:03 +00:00
|
|
|
newtypes::{DbUrl, InstanceId, SiteId},
|
|
|
|
schema::site::dsl::{actor_id, id, instance_id, site},
|
2022-11-19 04:33:54 +00:00
|
|
|
source::{
|
|
|
|
actor_language::SiteLanguage,
|
|
|
|
site::{Site, SiteInsertForm, SiteUpdateForm},
|
|
|
|
},
|
2022-10-06 18:27:58 +00:00
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2022-10-06 18:27:58 +00:00
|
|
|
};
|
2023-09-09 16:25:03 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, OptionalExtension, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2022-02-07 19:23:12 +00:00
|
|
|
use url::Url;
|
2019-12-11 20:21:47 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Site {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = SiteInsertForm;
|
|
|
|
type UpdateForm = SiteUpdateForm;
|
2022-10-06 18:27:58 +00:00
|
|
|
type IdType = SiteId;
|
2019-12-11 20:21:47 +00:00
|
|
|
|
2023-02-22 01:22:54 +00:00
|
|
|
/// Use SiteView::read_local, or Site::read_from_apub_id instead
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result<Self, Error> {
|
2023-02-22 01:22:54 +00:00
|
|
|
unimplemented!()
|
2022-11-09 10:05:00 +00:00
|
|
|
}
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
|
2023-03-30 15:03:13 +00:00
|
|
|
let is_new_site = match &form.actor_id {
|
|
|
|
Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(),
|
|
|
|
None => true,
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-03-30 15:03:13 +00:00
|
|
|
|
|
|
|
// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
|
2022-10-06 18:27:58 +00:00
|
|
|
let site_ = insert_into(site)
|
2022-10-27 09:24:07 +00:00
|
|
|
.values(form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
.await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2023-03-30 15:03:13 +00:00
|
|
|
// initialize languages if site is newly created
|
|
|
|
if is_new_site {
|
|
|
|
// initialize with all languages
|
|
|
|
SiteLanguage::update(pool, vec![], &site_).await?;
|
|
|
|
}
|
2022-10-06 18:27:58 +00:00
|
|
|
Ok(site_)
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-10-27 09:24:07 +00:00
|
|
|
site_id: SiteId,
|
|
|
|
new_site: &Self::UpdateForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-12-11 20:21:47 +00:00
|
|
|
diesel::update(site.find(site_id))
|
|
|
|
.set(new_site)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Site {
|
2023-09-09 16:25:03 +00:00
|
|
|
pub async fn read_from_instance_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
_instance_id: InstanceId,
|
|
|
|
) -> Result<Option<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
site
|
|
|
|
.filter(instance_id.eq(_instance_id))
|
|
|
|
.get_result(conn)
|
|
|
|
.await
|
|
|
|
.optional()
|
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_from_apub_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
object_id: &DbUrl,
|
|
|
|
) -> Result<Option<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-09-09 16:25:03 +00:00
|
|
|
|
|
|
|
site
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Site>(conn)
|
|
|
|
.await
|
|
|
|
.optional()
|
|
|
|
.map(Into::into)
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_remote_sites(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
site.order_by(id).offset(1).get_results::<Self>(conn).await
|
2020-12-20 01:10:47 +00:00
|
|
|
}
|
2022-11-28 14:29:33 +00:00
|
|
|
|
|
|
|
/// Instance actor is at the root path, so we simply need to clear the path and other unnecessary
|
|
|
|
/// parts of the url.
|
|
|
|
pub fn instance_actor_id_from_url(mut url: Url) -> Url {
|
|
|
|
url.set_fragment(None);
|
|
|
|
url.set_path("");
|
|
|
|
url.set_query(None);
|
|
|
|
url
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
}
|