Update activitystreams

This commit is contained in:
asonix
2020-06-19 23:11:02 -05:00
parent e65c21582c
commit 2bbcc82d9c
27 changed files with 334 additions and 366 deletions
+25 -52
View File
@@ -1,5 +1,5 @@
use crate::{apub::AcceptedActors, db::Db, error::MyError, requests::Requests};
use activitystreams_new::{prelude::*, primitives::XsdAnyUri, uri};
use activitystreams_new::{prelude::*, primitives::XsdAnyUri, uri, url::Url};
use log::error;
use std::{collections::HashSet, sync::Arc, time::Duration};
use tokio::sync::RwLock;
@@ -32,8 +32,8 @@ impl<T> MaybeCached<T> {
#[derive(Clone)]
pub struct ActorCache {
db: Db,
cache: Arc<RwLock<TtlCache<XsdAnyUri, Actor>>>,
following: Arc<RwLock<HashSet<XsdAnyUri>>>,
cache: Arc<RwLock<TtlCache<Url, Actor>>>,
following: Arc<RwLock<HashSet<Url>>>,
}
impl ActorCache {
@@ -49,43 +49,25 @@ impl ActorCache {
cache
}
pub async fn is_following(&self, id: &XsdAnyUri) -> bool {
pub async fn is_following(&self, id: &Url) -> bool {
self.following.read().await.contains(id)
}
pub async fn get_no_cache(
&self,
id: &XsdAnyUri,
requests: &Requests,
) -> Result<Actor, MyError> {
pub async fn get_no_cache(&self, id: &Url, requests: &Requests) -> Result<Actor, MyError> {
let accepted_actor = requests.fetch::<AcceptedActors>(id.as_str()).await?;
let input_host = id.as_url().host();
let accepted_actor_id = accepted_actor.id().ok_or(MyError::MissingId)?;
let actor_host = accepted_actor_id.as_url().host();
let inbox_host = get_inbox(&accepted_actor).as_url().host();
let input_domain = id.domain().ok_or(MyError::MissingDomain)?;
let accepted_actor_id = accepted_actor
.id(&input_domain)?
.ok_or(MyError::MissingId)?;
if input_host != actor_host {
let input_host = input_host.map(|h| h.to_string()).unwrap_or_default();
let actor_host = actor_host.map(|h| h.to_string()).unwrap_or_default();
return Err(MyError::HostMismatch(input_host, actor_host));
}
if actor_host != inbox_host {
let actor_host = actor_host.map(|h| h.to_string()).unwrap_or_default();
let inbox_host = inbox_host.map(|h| h.to_string()).unwrap_or_default();
return Err(MyError::HostMismatch(actor_host, inbox_host));
}
let inbox = get_inbox(&accepted_actor).clone();
let inbox = get_inbox(&accepted_actor)?.clone();
let actor = Actor {
id: accepted_actor_id.clone(),
id: accepted_actor_id.clone().into(),
public_key: accepted_actor.ext_one.public_key.public_key_pem,
public_key_id: accepted_actor.ext_one.public_key.id,
inbox,
inbox: inbox.into(),
};
self.cache
@@ -99,11 +81,7 @@ impl ActorCache {
Ok(actor)
}
pub async fn get(
&self,
id: &XsdAnyUri,
requests: &Requests,
) -> Result<MaybeCached<Actor>, MyError> {
pub async fn get(&self, id: &Url, requests: &Requests) -> Result<MaybeCached<Actor>, MyError> {
if let Some(actor) = self.cache.read().await.get(id) {
return Ok(MaybeCached::Cached(actor.clone()));
}
@@ -125,11 +103,11 @@ impl ActorCache {
self.save(actor.clone()).await
}
pub async fn cache_follower(&self, id: XsdAnyUri) {
pub async fn cache_follower(&self, id: Url) {
self.following.write().await.insert(id);
}
pub async fn bust_follower(&self, id: &XsdAnyUri) {
pub async fn bust_follower(&self, id: &Url) {
self.following.write().await.remove(id);
}
@@ -174,7 +152,7 @@ impl ActorCache {
Ok(None)
}
async fn lookup(&self, id: &XsdAnyUri) -> Result<Option<Actor>, MyError> {
async fn lookup(&self, id: &Url) -> Result<Option<Actor>, MyError> {
let row_opt = self
.db
.pool()
@@ -203,10 +181,10 @@ impl ActorCache {
let public_key_id: String = row.try_get(2)?;
Ok(Some(Actor {
id: id.clone(),
inbox: uri!(inbox),
id: id.clone().into(),
inbox: uri!(inbox).into(),
public_key: row.try_get(1)?,
public_key_id: uri!(public_key_id),
public_key_id: uri!(public_key_id).into(),
}))
}
@@ -262,12 +240,7 @@ impl ActorCache {
Ok(())
}
async fn update(
&self,
id: &XsdAnyUri,
public_key: &str,
public_key_id: &XsdAnyUri,
) -> Result<(), MyError> {
async fn update(&self, id: &Url, public_key: &str, public_key_id: &Url) -> Result<(), MyError> {
self.db
.pool()
.get()
@@ -335,11 +308,11 @@ impl ActorCache {
}
}
fn get_inbox(actor: &AcceptedActors) -> &XsdAnyUri {
actor
.endpoints()
.and_then(|e| e.shared_inbox.as_ref())
.unwrap_or(actor.inbox())
fn get_inbox(actor: &AcceptedActors) -> Result<&Url, MyError> {
Ok(actor
.endpoints()?
.and_then(|e| e.shared_inbox)
.unwrap_or(actor.inbox()?))
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
+7 -7
View File
@@ -1,5 +1,5 @@
use crate::{db::Db, error::MyError};
use activitystreams_new::primitives::XsdAnyUri;
use activitystreams_new::url::Url;
use async_mutex::Mutex;
use bytes::Bytes;
use futures::join;
@@ -14,8 +14,8 @@ static MEDIA_DURATION: Duration = Duration::from_secs(60 * 60 * 24 * 2);
#[derive(Clone)]
pub struct Media {
db: Db,
inverse: Arc<Mutex<HashMap<XsdAnyUri, Uuid>>>,
url_cache: Arc<Mutex<LruCache<Uuid, XsdAnyUri>>>,
inverse: Arc<Mutex<HashMap<Url, Uuid>>>,
url_cache: Arc<Mutex<LruCache<Uuid, Url>>>,
byte_cache: Arc<RwLock<TtlCache<Uuid, (String, Bytes)>>>,
}
@@ -29,7 +29,7 @@ impl Media {
}
}
pub async fn get_uuid(&self, url: &XsdAnyUri) -> Result<Option<Uuid>, MyError> {
pub async fn get_uuid(&self, url: &Url) -> Result<Option<Uuid>, MyError> {
let res = self.inverse.lock().await.get(url).cloned();
let uuid = match res {
Some(uuid) => uuid,
@@ -90,7 +90,7 @@ impl Media {
Ok(None)
}
pub async fn get_url(&self, uuid: Uuid) -> Result<Option<XsdAnyUri>, MyError> {
pub async fn get_url(&self, uuid: Uuid) -> Result<Option<Url>, MyError> {
if let Some(url) = self.url_cache.lock().await.get(&uuid).cloned() {
return Ok(Some(url));
}
@@ -111,7 +111,7 @@ impl Media {
if let Some(row) = row_opt {
let url: String = row.try_get(0)?;
let url: XsdAnyUri = url.parse()?;
let url: Url = url.parse()?;
return Ok(Some(url));
}
@@ -122,7 +122,7 @@ impl Media {
self.byte_cache.read().await.get(&uuid).cloned()
}
pub async fn store_url(&self, url: &XsdAnyUri) -> Result<Uuid, MyError> {
pub async fn store_url(&self, url: &Url) -> Result<Uuid, MyError> {
let uuid = Uuid::new_v4();
let (_, _, res) = join!(
+19 -20
View File
@@ -1,5 +1,5 @@
use crate::{db::Db, error::MyError};
use activitystreams_new::{primitives::XsdAnyUri, uri};
use activitystreams_new::{primitives::XsdAnyUri, uri, url::Url};
use log::{debug, error};
use std::{
collections::{HashMap, HashSet},
@@ -10,13 +10,13 @@ use tokio::sync::RwLock;
use tokio_postgres::types::Json;
use uuid::Uuid;
pub type ListenersCache = Arc<RwLock<HashSet<XsdAnyUri>>>;
pub type ListenersCache = Arc<RwLock<HashSet<Url>>>;
#[derive(Clone)]
pub struct NodeCache {
db: Db,
listeners: ListenersCache,
nodes: Arc<RwLock<HashMap<XsdAnyUri, Node>>>,
nodes: Arc<RwLock<HashMap<Url, Node>>>,
}
impl NodeCache {
@@ -45,7 +45,7 @@ impl NodeCache {
.collect()
}
pub async fn is_nodeinfo_outdated(&self, listener: &XsdAnyUri) -> bool {
pub async fn is_nodeinfo_outdated(&self, listener: &Url) -> bool {
let read_guard = self.nodes.read().await;
let node = match read_guard.get(listener) {
@@ -65,7 +65,7 @@ impl NodeCache {
}
}
pub async fn is_contact_outdated(&self, listener: &XsdAnyUri) -> bool {
pub async fn is_contact_outdated(&self, listener: &Url) -> bool {
let read_guard = self.nodes.read().await;
let node = match read_guard.get(listener) {
@@ -85,7 +85,7 @@ impl NodeCache {
}
}
pub async fn is_instance_outdated(&self, listener: &XsdAnyUri) -> bool {
pub async fn is_instance_outdated(&self, listener: &Url) -> bool {
let read_guard = self.nodes.read().await;
let node = match read_guard.get(listener) {
@@ -196,7 +196,7 @@ impl NodeCache {
pub async fn set_info(
&self,
listener: &XsdAnyUri,
listener: &Url,
software: String,
version: String,
reg: bool,
@@ -221,7 +221,7 @@ impl NodeCache {
pub async fn set_instance(
&self,
listener: &XsdAnyUri,
listener: &Url,
title: String,
description: String,
version: String,
@@ -248,11 +248,11 @@ impl NodeCache {
pub async fn set_contact(
&self,
listener: &XsdAnyUri,
listener: &Url,
username: String,
display_name: String,
url: XsdAnyUri,
avatar: XsdAnyUri,
url: Url,
avatar: Url,
) -> Result<(), MyError> {
if !self.listeners.read().await.contains(listener) {
let mut nodes = self.nodes.write().await;
@@ -272,7 +272,7 @@ impl NodeCache {
Ok(())
}
pub async fn save(&self, listener: &XsdAnyUri, node: &Node) -> Result<(), MyError> {
pub async fn save(&self, listener: &Url, node: &Node) -> Result<(), MyError> {
let row_opt = self
.db
.pool()
@@ -328,21 +328,20 @@ impl NodeCache {
#[derive(Clone, Debug)]
pub struct Node {
pub base: XsdAnyUri,
pub base: Url,
pub info: Option<Info>,
pub instance: Option<Instance>,
pub contact: Option<Contact>,
}
impl Node {
pub fn new(mut uri: XsdAnyUri) -> Self {
let url = uri.as_mut();
pub fn new(mut url: Url) -> Self {
url.set_fragment(None);
url.set_query(None);
url.set_path("");
Node {
base: uri,
base: url,
info: None,
instance: None,
contact: None,
@@ -382,14 +381,14 @@ impl Node {
&mut self,
username: String,
display_name: String,
url: XsdAnyUri,
avatar: XsdAnyUri,
url: Url,
avatar: Url,
) -> &mut Self {
self.contact = Some(Contact {
username,
display_name,
url,
avatar,
url: url.into(),
avatar: avatar.into(),
updated: SystemTime::now(),
});
self
+15 -15
View File
@@ -5,7 +5,7 @@ use crate::{
error::MyError,
requests::Requests,
};
use activitystreams_new::primitives::XsdAnyUri;
use activitystreams_new::url::Url;
use actix_rt::{
spawn,
time::{interval_at, Instant},
@@ -24,10 +24,10 @@ pub struct State {
pub public_key: RSAPublicKey,
private_key: RSAPrivateKey,
config: Config,
actor_id_cache: Arc<RwLock<LruCache<XsdAnyUri, XsdAnyUri>>>,
actor_id_cache: Arc<RwLock<LruCache<Url, Url>>>,
blocks: Arc<RwLock<HashSet<String>>>,
whitelists: Arc<RwLock<HashSet<String>>>,
listeners: Arc<RwLock<HashSet<XsdAnyUri>>>,
listeners: Arc<RwLock<HashSet<Url>>>,
node_cache: NodeCache,
}
@@ -57,11 +57,11 @@ impl State {
self.blocks.write().await.remove(block);
}
pub async fn bust_listener(&self, inbox: &XsdAnyUri) {
pub async fn bust_listener(&self, inbox: &Url) {
self.listeners.write().await.remove(inbox);
}
pub async fn listeners(&self) -> Vec<XsdAnyUri> {
pub async fn listeners(&self) -> Vec<Url> {
self.listeners.read().await.iter().cloned().collect()
}
@@ -69,13 +69,13 @@ impl State {
self.blocks.read().await.iter().cloned().collect()
}
pub async fn listeners_without(&self, inbox: &XsdAnyUri, domain: &str) -> Vec<XsdAnyUri> {
pub async fn listeners_without(&self, inbox: &Url, domain: &str) -> Vec<Url> {
self.listeners
.read()
.await
.iter()
.filter_map(|listener| {
if let Some(dom) = listener.as_url().domain() {
if let Some(dom) = listener.domain() {
if listener != inbox && dom != domain {
return Some(listener.clone());
}
@@ -86,35 +86,35 @@ impl State {
.collect()
}
pub async fn is_whitelisted(&self, actor_id: &XsdAnyUri) -> bool {
pub async fn is_whitelisted(&self, actor_id: &Url) -> bool {
if !self.config.whitelist_mode() {
return true;
}
if let Some(domain) = actor_id.as_url().domain() {
if let Some(domain) = actor_id.domain() {
return self.whitelists.read().await.contains(domain);
}
false
}
pub async fn is_blocked(&self, actor_id: &XsdAnyUri) -> bool {
if let Some(domain) = actor_id.as_url().domain() {
pub async fn is_blocked(&self, actor_id: &Url) -> bool {
if let Some(domain) = actor_id.domain() {
return self.blocks.read().await.contains(domain);
}
true
}
pub async fn is_listener(&self, actor_id: &XsdAnyUri) -> bool {
pub async fn is_listener(&self, actor_id: &Url) -> bool {
self.listeners.read().await.contains(actor_id)
}
pub async fn is_cached(&self, object_id: &XsdAnyUri) -> bool {
pub async fn is_cached(&self, object_id: &Url) -> bool {
self.actor_id_cache.read().await.contains(object_id)
}
pub async fn cache(&self, object_id: XsdAnyUri, actor_id: XsdAnyUri) {
pub async fn cache(&self, object_id: Url, actor_id: Url) {
self.actor_id_cache.write().await.put(object_id, actor_id);
}
@@ -126,7 +126,7 @@ impl State {
self.whitelists.write().await.insert(host);
}
pub async fn cache_listener(&self, listener: XsdAnyUri) {
pub async fn cache_listener(&self, listener: Url) {
self.listeners.write().await.insert(listener);
}