lemmy/crates/db_schema/src/source/activity.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

97 lines
2.5 KiB
Rust

use crate::{
newtypes::{CommunityId, DbUrl},
schema::sent_activity,
};
use chrono::{DateTime, Utc};
use diesel::{sql_types::Nullable, Queryable};
use serde_json::Value;
use std::{collections::HashSet, fmt::Debug};
use url::Url;
#[derive(FromSqlRow, PartialEq, Eq, Debug, Default, Clone)]
/// describes where an activity should be sent
pub struct ActivitySendTargets {
/// send to these inboxes explicitly
pub inboxes: HashSet<Url>,
/// send to all followers of these local communities
pub community_followers_of: Option<CommunityId>,
/// send to all remote instances
pub all_instances: bool,
}
// todo: in different file?
impl ActivitySendTargets {
pub fn empty() -> ActivitySendTargets {
ActivitySendTargets::default()
}
pub fn to_inbox(url: Url) -> ActivitySendTargets {
let mut a = ActivitySendTargets::empty();
a.inboxes.insert(url);
a
}
pub fn to_local_community_followers(id: CommunityId) -> ActivitySendTargets {
let mut a = ActivitySendTargets::empty();
a.community_followers_of = Some(id);
a
}
pub fn to_all_instances() -> ActivitySendTargets {
let mut a = ActivitySendTargets::empty();
a.all_instances = true;
a
}
pub fn set_all_instances(&mut self) {
self.all_instances = true;
}
pub fn add_inbox(&mut self, inbox: Url) {
self.inboxes.insert(inbox);
}
pub fn add_inboxes(&mut self, inboxes: impl Iterator<Item = Url>) {
self.inboxes.extend(inboxes);
}
}
#[derive(PartialEq, Eq, Debug, Queryable)]
#[diesel(table_name = sent_activity)]
pub struct SentActivity {
pub id: i64,
pub ap_id: DbUrl,
pub data: Value,
pub sensitive: bool,
pub published: DateTime<Utc>,
pub send_inboxes: Vec<Option<DbUrl>>,
pub send_community_followers_of: Option<CommunityId>,
pub send_all_instances: bool,
pub actor_type: ActorType,
pub actor_apub_id: Option<DbUrl>,
}
#[derive(Insertable)]
#[diesel(table_name = sent_activity)]
pub struct SentActivityForm {
pub ap_id: DbUrl,
pub data: Value,
pub sensitive: bool,
pub send_inboxes: Vec<Option<DbUrl>>,
pub send_community_followers_of: Option<i32>,
pub send_all_instances: bool,
pub actor_type: ActorType,
pub actor_apub_id: DbUrl,
}
#[derive(Clone, Copy, Debug, diesel_derive_enum::DbEnum, PartialEq, Eq)]
#[ExistingTypePath = "crate::schema::sql_types::ActorTypeEnum"]
pub enum ActorType {
Site,
Community,
Person,
}
#[derive(PartialEq, Eq, Debug, Queryable)]
#[diesel(table_name = received_activity)]
pub struct ReceivedActivity {
pub id: i64,
pub ap_id: DbUrl,
pub published: DateTime<Utc>,
}