lemmy/crates/db_schema/src/impls/site.rs
phiresky 375d9a2a3c
Persistent, performant, reliable federation queue (#3605)
* persistent activity queue

* fixes

* fixes

* make federation workers function callable from outside

* log federation instances

* dead instance detection not needed here

* taplo fmt

* split federate bin/lib

* minor fix

* better logging

* log

* create struct to hold cancellable task for readability

* use boxfuture for readability

* reset submodule

* fix

* fix lint

* swap

* remove json column, use separate array columns instead

* some review comments

* make worker a struct for readability

* minor readability

* add local filter to community follower view

* remove separate lemmy_federate entry point

* fix remaining duration

* address review comments mostly

* fix lint

* upgrade actitypub-fed to simpler interface

* fix sql format

* increase delays a bit

* fixes after merge

* remove selectable

* fix instance selectable

* add comment

* start federation based on latest id at the time

* rename federate process args

* dead instances in one query

* filter follow+report activities by local

* remove synchronous federation

remove activity sender queue

* lint

* fix federation tests by waiting for results to change

* fix fed test

* fix comment report

* wait some more

* Apply suggestions from code review

Co-authored-by: SorteKanin <sortekanin@gmail.com>

* fix most remaining tests

* wait until private messages

* fix community tests

* fix community tests

* move arg parse

* use instance_id instead of domain in federation_queue_state table

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: SorteKanin <sortekanin@gmail.com>
2023-09-09 12:25:03 -04:00

103 lines
2.7 KiB
Rust

use crate::{
newtypes::{DbUrl, InstanceId, SiteId},
schema::site::dsl::{actor_id, id, instance_id, site},
source::{
actor_language::SiteLanguage,
site::{Site, SiteInsertForm, SiteUpdateForm},
},
traits::Crud,
utils::{get_conn, DbPool},
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, OptionalExtension, 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_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()
}
pub async fn read_from_apub_id(
pool: &mut DbPool<'_>,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
site
.filter(actor_id.eq(object_id))
.first::<Site>(conn)
.await
.optional()
.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
}
}