91834d0d21
* h * Start doing stuff * Default impl for Crud::read * Simplify Crud::read lifetimes * fmt * Stuff * Stuff * Successfully make default read implementation work * Restore Person::read * Clean up default Crud::read and rename 'query2 * Replace filter with find * Attempt default Crud::create * Change Crud to Crud<'a> (won't compile) * Revert "Change Crud to Crud<'a> (won't compile)" This reverts commit 7ed20f5f713600bd48c85aad0848d8dbaae56503. * Default Crud::delete * Remove Crud::delete definitions that match default * Remove commented Site::read * Insert trait * Revert "Insert trait" This reverts commit 9d780c24035d3a9fb12968d3009a28724046dc3a. * Use non-borrowed forms * Revert "Use non-borrowed forms" This reverts commit d2dd4425634b54ef105aab44f1c37cc10a32491e. * Revert "Revert "Change Crud to Crud<'a> (won't compile)"" This reverts commit 25a27165a8ef56495e9f605ac15c9924b101d1bf. * Fix lifetime for everything except Crud::delete * Fix Crud::delete * Add comment about futures * Attempt Crud::create * Attempt separate CrudBounds * Revert "Attempt separate CrudBounds" This reverts commit 1b4ca321c3d2a1d045e2f4c542c593582e9c6d80. * Try to fix Crud::create * Move lifetime parameters to associated types * Revert "Move lifetime parameters to associated types" This reverts commit af1bc858ce5e1dacddc4bbded2da7e4b7237e237. * Revert "Try to fix Crud::create" This reverts commit eec238496c38127cbf3d542b7cfd57ec55622d1f. * Revert "Revert "Attempt separate CrudBounds"" This reverts commit 1ec33ce5022c58a5ad079ed7f5c220fafe5f0a5f. * Revert "Attempt separate CrudBounds" This reverts commit 1b4ca321c3d2a1d045e2f4c542c593582e9c6d80. * Revert "Attempt Crud::create" This reverts commit 47e8071b6826f27e2a562680b4948c37dffa68cb. * Revert "Add comment about futures" This reverts commit b266b1465393995b3be51d5ba207d5249560884f. * Revert "Fix Crud::delete" This reverts commit 3abcce2eec55208993dd9c7c3e51cff83412d15a. * Revert "Fix lifetime for everything except Crud::delete" This reverts commit c1ad7a161bbc8495dbfb8b52073f35ae88519da6. * Revert "Revert "Revert "Change Crud to Crud<'a> (won't compile)""" This reverts commit 3129cd0fc302f34bc0aad59987b9d0eb1139076c. * Clean up * Update site.rs
92 lines
2.4 KiB
Rust
92 lines
2.4 KiB
Rust
use crate::{
|
|
newtypes::{DbUrl, SiteId},
|
|
schema::site::dsl::{actor_id, id, site},
|
|
source::{
|
|
actor_language::SiteLanguage,
|
|
site::{Site, SiteInsertForm, SiteUpdateForm},
|
|
},
|
|
traits::Crud,
|
|
utils::{get_conn, DbPool},
|
|
};
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
|
use diesel_async::RunQueryDsl;
|
|
use url::Url;
|
|
|
|
#[async_trait]
|
|
impl Crud for Site {
|
|
type InsertForm = SiteInsertForm;
|
|
type UpdateForm = SiteUpdateForm;
|
|
type IdType = SiteId;
|
|
|
|
/// Use SiteView::read_local, or Site::read_from_apub_id instead
|
|
async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result<Self, Error> {
|
|
unimplemented!()
|
|
}
|
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
|
|
let is_new_site = match &form.actor_id {
|
|
Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(),
|
|
None => true,
|
|
};
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
|
|
let site_ = insert_into(site)
|
|
.values(form)
|
|
.on_conflict(actor_id)
|
|
.do_update()
|
|
.set(form)
|
|
.get_result::<Self>(conn)
|
|
.await?;
|
|
|
|
// initialize languages if site is newly created
|
|
if is_new_site {
|
|
// initialize with all languages
|
|
SiteLanguage::update(pool, vec![], &site_).await?;
|
|
}
|
|
Ok(site_)
|
|
}
|
|
|
|
async fn update(
|
|
pool: &mut DbPool<'_>,
|
|
site_id: SiteId,
|
|
new_site: &Self::UpdateForm,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
diesel::update(site.find(site_id))
|
|
.set(new_site)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
}
|
|
|
|
impl Site {
|
|
pub async fn read_from_apub_id(
|
|
pool: &mut DbPool<'_>,
|
|
object_id: &DbUrl,
|
|
) -> Result<Option<Self>, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
Ok(
|
|
site
|
|
.filter(actor_id.eq(object_id))
|
|
.first::<Site>(conn)
|
|
.await
|
|
.ok()
|
|
.map(Into::into),
|
|
)
|
|
}
|
|
|
|
pub async fn read_remote_sites(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
site.order_by(id).offset(1).get_results::<Self>(conn).await
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
}
|