Compare commits

...

48 Commits

Author SHA1 Message Date
Jeb Rosen 4f904b7ac7 Work around a difficult lifetime interaction between closures and async blocks. 4 years ago
Jeb Rosen e322d9509a Change '&' to '&mut'.
Key changes:
* `find plume-models -name '*.rs' -exec sed -i -e 's/&PlumeRocket/\&mut PlumeRocket/' '{}' \;`
* Remove `let conn = &*rockets.conn;` lines
* Change `conn` to `&mut *rockets.conn` where `conn` was used
4 years ago
Mina Galić b596e77f03
remove redundant use statements 4 years ago
Mina Galić 41f97b01f0
unroll filter_map() to easier .await 4 years ago
Mina Galić a508a4150c
remove redundant use statements 4 years ago
Mina Galić 25c40adf20
yet another dubious clippy warnings fix re returns 4 years ago
Mina Galić 7490567a21
fix warnings about unused doc comments
we do this by making the macro parse and generate doc comments
4 years ago
Mina Galić 492bbb1ba6
make clippy happy with a weird quirk wrt return
not happy about this lint or rust behaviour where the return statement
must not have a `;`
4 years ago
Mina Galić cf3708e1c6
make clippy happy by removing unused imports 4 years ago
Mina Galić df442002c2
replace .map().map_err() with a match
so we can .await without worries.
plus, it changes nothing about the code, other than making the intent
clearer at first sight.
4 years ago
Mina Galić 07036b5fad
upgrade validator: it now uses types! in macros!! 4 years ago
Mina Galić 0726375859
add another async (and correctly convert followers_count) 4 years ago
Mina Galić cb1c260692
remove an experiment of disabling Send… it makes no sense 4 years ago
Mina Galić de6bfca084
removed a few unused imports 4 years ago
Mina Galić 7aabb9661e
upgrade webfinger everywhere, and implement async 4 years ago
Mina Galić 18bb413011
add async/.await until all our errors are the same:
that our Connection is not Send-safe.
4 years ago
Mina Galić d2881ee3f7
add async/.await until all our errors are the same: that our Connection is not Send-safe. 4 years ago
Mina Galić 850b3c1337
add async/.await until all our errors are the same:
that our Connection is not Send-safe.
once we get there, we can start thinking about restructing the way we
pass along our connection, or consider using #[database].
4 years ago
Mina Galić 44ebce516c
fix some, break some compiling by adding async/await in front of it
i forgot that we can't `Send` diesel connections over threads, so this
is going to require some rethinking.
4 years ago
Mina Galić 3c830ab0ce
move towards using #[rocket::async_trait]
this also upgrades some dependencies
some of that fixes stuff, others breaks stuff.
4 years ago
Mina Galić 097d0ea9ce
make plume-models async (again) 4 years ago
Mina Galić 6fe16c9f84
upgrade and use futures… then block_on .await in a trait? 4 years ago
Mina Galić 43cb9f700c
update webfinger
as requested in https://github.com/Plume-org/webfinger/issues/8
and provided in https://github.com/Plume-org/webfinger/pull/9

```
meena@76ix ~/s/a/plume (go/async) [101]> cargo update --package webfinger
    Updating crates.io index
    Updating git repository `https://github.com/Plume-org/webfinger`
    Removing bytes v0.4.12
    Removing cookie v0.12.0
    Removing cookie_store v0.7.0
    Removing crossbeam-deque v0.7.2
    Removing crossbeam-epoch v0.8.0
    Removing crossbeam-queue v0.1.2
    Removing crossbeam-utils v0.7.0
    Removing h2 v0.1.26
    Removing http v0.1.21
    Removing http-body v0.1.0
    Removing hyper v0.12.35
    Removing hyper-tls v0.3.2
    Removing publicsuffix v1.5.4
    Removing reqwest v0.9.24
    Removing serde_urlencoded v0.5.5
    Removing string v0.2.1
    Removing tokio v0.1.22
    Removing tokio-buf v0.1.1
    Removing tokio-current-thread v0.1.6
    Removing tokio-executor v0.1.9
    Removing tokio-io v0.1.12
    Removing tokio-reactor v0.1.11
    Removing tokio-sync v0.1.7
    Removing tokio-tcp v0.1.3
    Removing tokio-threadpool v0.1.17
    Removing tokio-timer v0.2.12
    Removing try_from v0.3.2
    Removing want v0.2.0
      Adding webfinger v0.5.0 (https://github.com/Plume-org/webfinger?rev=update-deps#cdaab95e)
    Removing webfinger v0.5.0
meena@76ix ~/s/a/plume (go/async)>
```
4 years ago
Mina Galić 2c285b9aca
start async-ifying routes
template utils and error routes
4 years ago
Mina Galić e4bb73d22e
cargo clippy 4 years ago
Mina Galić e9c7259ffb
cargo fmt 4 years ago
Mina Galić be8c67ee9a
move reqwest client out of thread spawning
this way, we only spawn one, and reuse that.
4 years ago
Mina Galić 65b2c38c29
.await? result from read_to_string() 4 years ago
Jeb Rosen 8aa99cea35
move signature outside the spawning
this allows us to actually move stuff into the async block
and we can drop the 'static life-time.
4 years ago
Mina Galić a010025074
asyncify reqwest calls (again?) 4 years ago
Mina Galić 82088596a8
asyncify from_activity calls (i.e.: block_on()) 4 years ago
Mina Galić 87ce3a7b51
asyncify plume-models: media upload is now async
including the use of tokio!
4 years ago
Mina Galić 3472a58299
move ClientBuilder into thread, since we cannot Copy it 4 years ago
Mina Galić a3f165f9f4
Use blocking reqwest API in defer
defer, or, trait functions such as it in general(?) cannot be async (yet)
4 years ago
Mina Galić 25c5da1a7c
add tokio (0.2) as dependency to further async-ify our FromData code
i'm using this opportunity to also update reqwest (0.10), but it's
turning out to be a little trickier, as it requires more modern async
setup, and that appears to need a lot of thinking…
4 years ago
Mina Galić 022e037eea
when using macros!() we need to import the things that they use 4 years ago
Mina Galić 45c335e17b
"manually" create ETag and Cache-Control headers 4 years ago
Mina Galić b51551973a
start fixing tests in plume-models 4 years ago
Mina Galić 59e5c49aa8
convert plume-models to all async
where sensible! note that State has no asynchronous work to do, so
wrapping it up in async makes no sense.
4 years ago
Mina Galić ce119ffe50
start making PlumeRocket async 4 years ago
Mina Galić 944f8c42fa
plume-models: convert api-tokens. use DbConn::from_request() directly
there doesn't seem to be a request.guard_async (yet?)
4 years ago
Mina Galić 909f677bdd
plume-models: convert admin & api-tokens to async
n.b.: I do *not* like the error handling in api_tokens 😒
4 years ago
Mina Galić fd9764ff17
plume-common: also make requests async 4 years ago
Mina Galić 75722abc9e
rocket does not need decl_macro anymore 4 years ago
Mina Galić ec9b699c6e
convert plume-common to rocket async
it only took 3 hours of @jebrosen's patient help.
4 years ago
Mina Galić bb5c2b69a7
update rocket* everywhere!
and run cargo update
4 years ago
Mina Galić e52944e477
update rocket*; which gets us stuck in dependency conflicts 4 years ago
Mina Galić 928470610e
remove csrf for now, so we can update the rest 4 years ago

1
.gitignore vendored

@ -18,3 +18,4 @@ tags.*
search_index
.buildconfig
__pycache__
.vscode/

3689
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
activitypub = "0.1.3"
askama_escape = "0.1"
async-trait = "*"
atom_syndication = "0.6"
clap = "2.33"
colored = "1.8"
@ -20,20 +21,19 @@ heck = "0.3.0"
lettre = "0.9.2"
lettre_email = "0.9.2"
num_cpus = "1.10"
rocket = "0.4.2"
rocket_contrib = { version = "0.4.2", features = ["json"] }
rocket_i18n = { git = "https://github.com/Plume-org/rocket_i18n", rev = "e922afa7c366038b3433278c03b1456b346074f2" }
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" , features = ["json"] }
rpassword = "4.0"
runtime-fmt = "0.4.0"
scheduled-thread-pool = "0.2.2"
serde = "1.0"
serde_json = "1.0"
serde_qs = "0.5"
shrinkwraprs = "0.2.1"
syntect = "3.3"
validator = "0.8"
validator_derive = "0.8"
webfinger = "0.4.1"
tokio = "0.2"
validator = "0.10"
validator_derive = "0.10"
webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "4e8f12810c4a7ba7a07bbcb722cd265fdff512b6", features = ["async"] }
[[bin]]
name = "plume"
@ -65,9 +65,11 @@ path = "plume-common"
[dependencies.plume-models]
path = "plume-models"
[dependencies.rocket_csrf]
git = "https://github.com/fdb-hiroshima/rocket_csrf"
rev = "29910f2829e7e590a540da3804336577b48c7b31"
[dependencies.rocket_i18n]
git = "https://github.com/Plume-org/rocket_i18n"
branch = "go-async"
default-features = false
features = ["rocket"]
[build-dependencies]
ructe = "0.9.0"

@ -1,5 +1,3 @@
use rsass;
use ructe::Ructe;
use std::process::{Command, Stdio};
use std::{ffi::OsStr, fs::*, io::Write, path::*};

@ -6,22 +6,22 @@ edition = "2018"
[dependencies]
activitypub = "0.1.1"
activitystreams-derive = "0.1.1"
activitystreams-derive = "0.2"
activitystreams-traits = "0.1.0"
array_tool = "1.0"
base64 = "0.10"
futures-util = "*"
heck = "0.3.0"
hex = "0.3"
hyper = "0.12.33"
hyper = "0.13"
openssl = "0.10.22"
rocket = "0.4.0"
reqwest = "0.9"
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
shrinkwraprs = "0.2.1"
syntect = "3.3"
tokio = "0.1.22"
tokio = "0.2"
regex-syntax = { version = "0.6.17", default-features = false, features = ["unicode-perl"] }
[dependencies.chrono]
@ -31,3 +31,7 @@ version = "0.4"
[dependencies.pulldown-cmark]
default-features = false
version = "0.2.0"
[dependencies.reqwest]
features = ["json", "blocking"]
version = "0.10"

@ -86,7 +86,7 @@ where
/// - the context to be passed to each handler.
/// - the activity
/// - the reason it has not been handled yet
NotHandled(&'a C, serde_json::Value, InboxError<E>),
NotHandled(&'a mut C, serde_json::Value, InboxError<E>),
/// A matching handler have been found but failed
///
@ -139,16 +139,16 @@ where
///
/// - `ctx`: the context to pass to each handler
/// - `json`: the JSON representation of the incoming activity
pub fn handle(ctx: &'a C, json: serde_json::Value) -> Inbox<'a, C, E, R> {
pub fn handle(ctx: &'a mut C, json: serde_json::Value) -> Inbox<'a, C, E, R> {
Inbox::NotHandled(ctx, json, InboxError::NoMatch)
}
/// Registers an handler on this Inbox.
pub fn with<A, V, M>(self) -> Inbox<'a, C, E, R>
where
A: AsActor<&'a C> + FromId<C, Error = E>,
A: AsActor<&'a mut C> + FromId<C, Error = E>,
V: activitypub::Activity,
M: AsObject<A, V, &'a C, Error = E> + FromId<C, Error = E>,
M: AsObject<A, V, &'a mut C, Error = E> + FromId<C, Error = E>,
M::Output: Into<R>,
{
if let Inbox::NotHandled(ctx, mut act, e) = self {
@ -264,7 +264,7 @@ pub trait FromId<C>: Sized {
/// - `object`: optional object that will be used if the object was not found in the database
/// If absent, the ID will be dereferenced.
fn from_id(
ctx: &C,
ctx: &mut C,
id: &str,
object: Option<Self::Object>,
) -> Result<Self, (Option<serde_json::Value>, Self::Error)> {
@ -279,8 +279,9 @@ pub trait FromId<C>: Sized {
/// Dereferences an ID
fn deref(id: &str) -> Result<Self::Object, (Option<serde_json::Value>, Self::Error)> {
reqwest::ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
// Use blocking reqwest API here, since defer cannot be async (yet)
reqwest::blocking::Client::builder()
.connect_timeout(std::time::Duration::from_secs(5))
.build()
.map_err(|_| (None, InboxError::DerefError.into()))?
.get(id)
@ -296,7 +297,7 @@ pub trait FromId<C>: Sized {
)
.send()
.map_err(|_| (None, InboxError::DerefError))
.and_then(|mut r| {
.and_then(|r| {
let json: serde_json::Value = r
.json()
.map_err(|_| (None, InboxError::InvalidObject(None)))?;
@ -307,10 +308,10 @@ pub trait FromId<C>: Sized {
}
/// Builds a `Self` from its ActivityPub representation
fn from_activity(ctx: &C, activity: Self::Object) -> Result<Self, Self::Error>;
fn from_activity(ctx: &mut C, activity: Self::Object) -> Result<Self, Self::Error>;
/// Tries to find a `Self` with a given ID (`id`), using `ctx` (a database)
fn from_db(ctx: &C, id: &str) -> Result<Self, Self::Error>;
fn from_db(ctx: &mut C, id: &str) -> Result<Self, Self::Error>;
}
/// Should be implemented by anything representing an ActivityPub actor.

@ -1,14 +1,12 @@
use activitypub::{Activity, Link, Object};
use array_tool::vec::Uniq;
use reqwest::r#async::ClientBuilder;
use reqwest::ClientBuilder;
use rocket::{
http::Status,
request::{FromRequest, Request},
response::{Responder, Response},
response::{Responder, Response, Result},
Outcome,
};
use serde_json;
use tokio::prelude::*;
use self::sign::Signable;
@ -62,39 +60,45 @@ impl<T> ActivityStream<T> {
ActivityStream(t)
}
}
impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
fn respond_to(self, request: &Request<'_>) -> Result<Response<'r>, Status> {
#[rocket::async_trait]
impl<'r, O: Object + Send + 'r> Responder<'r> for ActivityStream<O> {
async fn respond_to(self, request: &'r Request<'_>) -> Result<'r> {
let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?;
json["@context"] = context();
serde_json::to_string(&json).respond_to(request).map(|r| {
Response::build_from(r)
let result = serde_json::to_string(&json).map_err(rocket::response::Debug);
match result.respond_to(request).await {
Ok(r) => Response::build_from(r)
.raw_header("Content-Type", "application/activity+json")
.finalize()
})
.ok(),
Err(e) => Err(e),
}
}
}
#[derive(Clone)]
pub struct ApRequest;
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for ApRequest {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
async fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
request
.headers()
.get_one("Accept")
.map(|header| {
header
.split(',')
.map(|ct| match ct.trim() {
// bool for Forward: true if found a valid Content-Type for Plume first (HTML), false otherwise
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\""
| "application/ld+json;profile=\"https://w3.org/ns/activitystreams\""
| "application/activity+json"
| "application/ld+json" => Outcome::Success(ApRequest),
"text/html" => Outcome::Forward(true),
_ => Outcome::Forward(false),
.map(|ct| {
match ct.trim() {
// bool for Forward: true if found a valid Content-Type for Plume first (HTML),
// false otherwise
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\""
| "application/ld+json;profile=\"https://w3.org/ns/activitystreams\""
| "application/activity+json"
| "application/ld+json" => Outcome::Success(ApRequest),
"text/html" => Outcome::Forward(true),
_ => Outcome::Forward(false),
}
})
.fold(Outcome::Forward(false), |out, ct| {
if out.clone().forwarded().unwrap_or_else(|| out.is_success()) {
@ -130,36 +134,38 @@ where
.sign(sender)
.expect("activity_pub::broadcast: signature error");
let mut rt = tokio::runtime::current_thread::Runtime::new()
.expect("Error while initializing tokio runtime for federation");
let client = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
let rt = tokio::runtime::Builder::new()
.threaded_scheduler()
.build()
.expect("Can't build client");
.expect("Error while initializing tokio runtime for federation");
for inbox in boxes {
let body = signed.to_string();
let mut headers = request::headers();
headers.insert("Digest", request::Digest::digest(&body));
rt.spawn(
let sig = request::signature(sender, &headers)
.expect("activity_pub::broadcast: request signature error");
let client = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()
.expect("Can't build client");
rt.spawn(async move {
client
.post(&inbox)
.headers(headers.clone())
.header(
"Signature",
request::signature(sender, &headers)
.expect("activity_pub::broadcast: request signature error"),
)
.header("Signature", sig)
.body(body)
.send()
.and_then(|r| r.into_body().concat2())
.await
.unwrap()
.text()
.await
.map(move |response| {
println!("Successfully sent activity to inbox ({})", inbox);
println!("Response: \"{:?}\"\n", response)
})
.map_err(|e| println!("Error while sending to inbox ({:?})", e)),
);
.map_err(|e| println!("Error while sending to inbox ({:?})", e))
});
}
rt.run().unwrap();
}
#[derive(Shrinkwrap, Clone, Serialize, Deserialize)]
@ -203,8 +209,7 @@ pub struct PublicKey {
pub public_key_pem: Option<serde_json::Value>,
}
#[derive(Clone, Debug, Default, UnitString)]
#[activitystreams(Hashtag)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct HashtagType;
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]

@ -1,4 +1,3 @@
use base64;
use chrono::{offset::Utc, DateTime};
use openssl::hash::{Hasher, MessageDigest};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT};

@ -1,10 +1,7 @@
use super::request;
use base64;
use chrono::{naive::NaiveDateTime, DateTime, Duration, Utc};
use hex;
use openssl::{pkey::PKey, rsa::Rsa, sha::sha256};
use rocket::http::HeaderMap;
use serde_json;
/// Returns (public key, private key)
pub fn gen_keypair() -> (Vec<u8>, Vec<u8>) {

@ -2,9 +2,7 @@
#[macro_use]
extern crate activitystreams_derive;
use activitystreams_traits;
use serde;
#[macro_use]
extern crate shrinkwraprs;
#[macro_use]

@ -10,24 +10,24 @@ ammonia = "2.1.1"
askama_escape = "0.1"
bcrypt = "0.5"
guid-create = "0.1"
futures = "0.3"
heck = "0.3.0"
itertools = "0.8.0"
lazy_static = "1.0"
migrations_internals= "1.4.0"
openssl = "0.10.22"
rocket = "0.4.0"
rocket_i18n = { git = "https://github.com/Plume-org/rocket_i18n", rev = "e922afa7c366038b3433278c03b1456b346074f2" }
reqwest = "0.9"
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
scheduled-thread-pool = "0.2.2"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tantivy = "0.10.1"
tokio = "0.2"
url = "2.1"
walkdir = "2.2"
webfinger = "0.4.1"
webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "4e8f12810c4a7ba7a07bbcb722cd265fdff512b6", features = ["async"] }
whatlang = "0.7.1"
shrinkwraprs = "0.2.1"
shrinkwraprs = "0.3"
diesel-derive-newtype = "0.1.2"
glob = "0.3.0"
@ -48,8 +48,19 @@ path = "../plume-common"
[dependencies.plume-macro]
path = "../plume-macro"
[dependencies.reqwest]
features = ["json", "blocking"]
version = "0.10"
[dependencies.rocket_i18n]
git = "https://github.com/Plume-org/rocket_i18n"
branch = "go-async"
default-features = false
features = ["rocket"]
[dev-dependencies]
diesel_migrations = "1.3.0"
diesel_migrations = "1.4.0"
[features]
postgres = ["diesel/postgres", "plume-macro/postgres" ]

@ -8,11 +8,12 @@ use rocket::{
/// Wrapper around User to use as a request guard on pages reserved to admins.
pub struct Admin(pub User);
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for Admin {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Admin, ()> {
let user = request.guard::<User>()?;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let user = try_outcome!(User::from_request(request).await);
if user.is_admin() {
Outcome::Success(Admin(user))
} else {
@ -24,11 +25,12 @@ impl<'a, 'r> FromRequest<'a, 'r> for Admin {
/// Same as `Admin` but for moderators.
pub struct Moderator(pub User);
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for Moderator {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Moderator, ()> {
let user = request.guard::<User>()?;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let user = try_outcome!(User::from_request(request).await);
if user.is_moderator() {
Outcome::Success(Moderator(user))
} else {

@ -76,32 +76,36 @@ pub enum TokenError {
DbError,
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for ApiToken {
type Error = TokenError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<ApiToken, TokenError> {
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let headers: Vec<_> = request.headers().get("Authorization").collect();
if headers.len() != 1 {
return Outcome::Failure((Status::BadRequest, TokenError::NoHeader));
}
let mut parsed_header = headers[0].split(' ');
let auth_type = parsed_header.next().map_or_else(
|| Outcome::Failure((Status::BadRequest, TokenError::NoType)),
Outcome::Success,
)?;
let val = parsed_header.next().map_or_else(
|| Outcome::Failure((Status::BadRequest, TokenError::NoValue)),
Outcome::Success,
)?;
if auth_type == "Bearer" {
let conn = request
.guard::<DbConn>()
.map_failure(|_| (Status::InternalServerError, TokenError::DbError))?;
if let Ok(token) = ApiToken::find_by_value(&*conn, val) {
return Outcome::Success(token);
if let Some(auth_type) = parsed_header.next() {
if let Some(val) = parsed_header.next() {
if auth_type == "Bearer" {
if let Outcome::Success(conn) = DbConn::from_request(request).await {
if let Ok(token) = ApiToken::find_by_value(&*conn, val) {
return Outcome::Success(token);
}
} else {
return Outcome::Failure((
Status::InternalServerError,
TokenError::DbError,
));
}
}
} else {
return Outcome::Failure((Status::BadRequest, TokenError::NoValue));
}
} else {
return Outcome::Failure((Status::BadRequest, TokenError::NoType));
}
Outcome::Forward(())

@ -20,7 +20,6 @@ use plume_common::activity_pub::{
inbox::{AsActor, FromId},
sign, ActivityStream, ApSignature, Id, IntoId, PublicKey, Source,
};
use serde_json;
use url::Url;
use webfinger::*;
@ -71,7 +70,8 @@ impl Blog {
insert!(blogs, NewBlog, |inserted, conn| {
let instance = inserted.get_instance(conn)?;
if inserted.outbox_url.is_empty() {
inserted.outbox_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "outbox");
inserted.outbox_url =
instance.compute_box(BLOG_PREFIX, &inserted.actor_id, r#"outbox"#);
}
if inserted.inbox_url.is_empty() {
@ -132,7 +132,7 @@ impl Blog {
.map_err(Error::from)
}
pub fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result<Blog> {
pub async fn find_by_fqn(c: &mut PlumeRocket, fqn: &str) -> Result<Blog> {
let from_db = blogs::table
.filter(blogs::fqn.eq(fqn))
.first(&*c.conn)
@ -140,12 +140,13 @@ impl Blog {
if let Some(from_db) = from_db {
Ok(from_db)
} else {
Blog::fetch_from_webfinger(c, fqn)
Blog::fetch_from_webfinger(c, fqn).await
}
}
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> {
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)?
async fn fetch_from_webfinger(c: &mut PlumeRocket, acct: &str) -> Result<Blog> {
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)
.await?
.links
.into_iter()
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
@ -339,11 +340,11 @@ impl FromId<PlumeRocket> for Blog {
type Error = Error;
type Object = CustomGroup;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Self::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, acct: CustomGroup) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, acct: CustomGroup) -> Result<Self> {
let url = Url::parse(&acct.object.object_props.id_string()?)?;
let inst = url.host_str()?;
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
@ -435,7 +436,7 @@ impl FromId<PlumeRocket> for Blog {
}
}
impl AsActor<&PlumeRocket> for Blog {
impl AsActor<&mut PlumeRocket> for Blog {
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}

@ -24,7 +24,6 @@ use plume_common::{
},
utils,
};
use serde_json;
use std::collections::HashSet;
#[derive(Queryable, Identifiable, Clone, AsChangeset)]
@ -105,7 +104,7 @@ impl Comment {
.unwrap_or(false)
}
pub fn to_activity(&self, c: &PlumeRocket) -> Result<Note> {
pub async fn to_activity(&self, c: &mut PlumeRocket) -> Result<Note> {
let author = User::get(&c.conn, self.author_id)?;
let (html, mentions, _hashtags) = utils::md_to_html(
self.content.get().as_ref(),
@ -131,19 +130,23 @@ impl Comment {
.set_published_string(chrono::Utc::now().to_rfc3339())?;
note.object_props.set_attributed_to_link(author.into_id())?;
note.object_props.set_to_link_vec(to)?;
note.object_props.set_tag_link_vec(
mentions
.into_iter()
.filter_map(|m| Mention::build_activity(c, &m).ok())
.collect::<Vec<link::Mention>>(),
)?;
let mut tag_link_vec = vec![];
let mut iter = mentions.into_iter();
while let Some(m) = iter.next() {
if let Ok(a) = Mention::build_activity(c, &m).await {
tag_link_vec.push(a);
}
}
note.object_props.set_tag_link_vec(tag_link_vec)?;
Ok(note)
}
pub fn create_activity(&self, c: &PlumeRocket) -> Result<Create> {
pub async fn create_activity(&self, c: &mut PlumeRocket) -> Result<Create> {
let author = User::get(&c.conn, self.author_id)?;
let note = self.to_activity(c)?;
let note = self.to_activity(c).await?;
let mut act = Create::default();
act.create_props.set_actor_link(author.into_id())?;
act.create_props.set_object_object(note.clone())?;
@ -198,11 +201,11 @@ impl FromId<PlumeRocket> for Comment {
type Error = Error;
type Object = Note;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Self::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, note: Note) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, note: Note) -> Result<Self> {
let conn = &*c.conn;
let comm = {
let previous_url = note.object_props.in_reply_to.as_ref()?.as_str()?;
@ -322,21 +325,21 @@ impl FromId<PlumeRocket> for Comment {
}
}
impl AsObject<User, Create, &PlumeRocket> for Comment {
impl AsObject<User, Create, &mut PlumeRocket> for Comment {
type Error = Error;
type Output = Self;
fn activity(self, _c: &PlumeRocket, _actor: User, _id: &str) -> Result<Self> {
fn activity(self, _c: &mut PlumeRocket, _actor: User, _id: &str) -> Result<Self> {
// The actual creation takes place in the FromId impl
Ok(self)
}
}
impl AsObject<User, Delete, &PlumeRocket> for Comment {
impl AsObject<User, Delete, &mut PlumeRocket> for Comment {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
if self.author_id != actor.id {
return Err(Error::Unauthorized);
}

@ -7,7 +7,7 @@ use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl};
use rocket::{
http::Status,
request::{self, FromRequest},
Outcome, Request, State,
Outcome, Request,
};
use std::ops::Deref;
@ -21,14 +21,14 @@ pub struct DbConn(pub PooledConnection<ConnectionManager<Connection>>);
/// Attempts to retrieve a single connection from the managed database pool. If
/// no pool is currently managed, fails with an `InternalServerError` status. If
/// no connections are available, fails with a `ServiceUnavailable` status.
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let pool = request.guard::<State<'_, DbPool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
match DbConn::from_request(request).await {
Outcome::Success(a) => Outcome::Success(a),
_ => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}

@ -136,11 +136,11 @@ impl Follow {
}
}
impl AsObject<User, FollowAct, &PlumeRocket> for User {
impl AsObject<User, FollowAct, &mut PlumeRocket> for User {
type Error = Error;
type Output = Follow;
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Follow> {
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Follow> {
// Mastodon (at least) requires the full Follow object when accepting it,
// so we rebuilt it here
let mut follow = FollowAct::default();
@ -156,11 +156,11 @@ impl FromId<PlumeRocket> for Follow {
type Error = Error;
type Object = FollowAct;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Follow::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, follow: FollowAct) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, follow: FollowAct) -> Result<Self> {
let actor =
User::from_id(c, &follow.follow_props.actor_link::<Id>()?, None).map_err(|(_, e)| e)?;
@ -170,11 +170,11 @@ impl FromId<PlumeRocket> for Follow {
}
}
impl AsObject<User, Undo, &PlumeRocket> for Follow {
impl AsObject<User, Undo, &mut PlumeRocket> for Follow {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
let conn = &*c.conn;
if self.follower_id == actor.id {
diesel::delete(&self).execute(conn)?;

@ -6,10 +6,11 @@ use rocket::{
pub struct Headers<'r>(pub HeaderMap<'r>);
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for Headers<'r> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
let mut headers = HeaderMap::new();
for header in request.headers().clone().into_iter() {
headers.add(header);

@ -1,5 +1,4 @@
use activitypub::activity::*;
use serde_json;
use crate::{
comments::Comment,
@ -46,7 +45,7 @@ impl_into_inbox_result! {
Reshare => Reshared
}
pub fn inbox(ctx: &PlumeRocket, act: serde_json::Value) -> Result<InboxResult, Error> {
pub fn inbox(ctx: &mut PlumeRocket, act: serde_json::Value) -> Result<InboxResult, Error> {
Inbox::handle(ctx, act)
.with::<User, Announce, Post>()
.with::<User, Create, Comment>()
@ -73,7 +72,7 @@ pub(crate) mod tests {
use diesel::Connection;
pub fn fill_database(
rockets: &PlumeRocket,
rockets: &mut PlumeRocket,
) -> (
Vec<crate::posts::Post>,
Vec<crate::users::User>,

@ -4,6 +4,7 @@
#[macro_use]
extern crate diesel;
extern crate futures;
#[macro_use]
extern crate lazy_static;
#[macro_use]
@ -75,6 +76,12 @@ impl From<std::option::NoneError> for Error {
}
}
impl From<Error> for std::option::NoneError {
fn from(_: Error) -> Self {
std::option::NoneError
}
}
impl From<url::ParseError> for Error {
fn from(_: url::ParseError) -> Self {
Error::Url

@ -83,11 +83,11 @@ impl Like {
}
}
impl AsObject<User, activity::Like, &PlumeRocket> for Post {
impl AsObject<User, activity::Like, &mut PlumeRocket> for Post {
type Error = Error;
type Output = Like;
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Like> {
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Like> {
let res = Like::insert(
&c.conn,
NewLike {
@ -107,11 +107,11 @@ impl FromId<PlumeRocket> for Like {
type Error = Error;
type Object = activity::Like;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Like::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, act: activity::Like) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, act: activity::Like) -> Result<Self> {
let res = Like::insert(
&c.conn,
NewLike {
@ -129,11 +129,11 @@ impl FromId<PlumeRocket> for Like {
}
}
impl AsObject<User, activity::Undo, &PlumeRocket> for Like {
impl AsObject<User, activity::Undo, &mut PlumeRocket> for Like {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
let conn = &*c.conn;
if actor.id == self.user_id {
diesel::delete(&self).execute(conn)?;

@ -7,8 +7,8 @@ use crate::{
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use std::convert::{TryFrom, TryInto};
/// Represent what a list is supposed to store. Represented in database as an integer
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Represent what a list is supposed to store. Represented in database as an integer
pub enum ListType {
User,
Blog,
@ -58,7 +58,11 @@ struct NewList<'a> {
}
macro_rules! func {
(@elem User $id:expr, $value:expr) => {
(
$(#[$outer:meta])*
@elem User $id:expr, $value:expr
) => {
$(#[$outer])*
NewListElem {
list_id: $id,
user_id: Some(*$value),
@ -66,7 +70,11 @@ macro_rules! func {
word: None,
}
};
(@elem Blog $id:expr, $value:expr) => {
(
$(#[$outer:meta])*
@elem Blog $id:expr, $value:expr
) => {
$(#[$outer])*
NewListElem {
list_id: $id,
user_id: None,
@ -74,7 +82,11 @@ macro_rules! func {
word: None,
}
};
(@elem Word $id:expr, $value:expr) => {
(
$(#[$outer:meta])*
@elem Word $id:expr, $value:expr
) => {
$(#[$outer])*
NewListElem {
list_id: $id,
user_id: None,
@ -82,7 +94,11 @@ macro_rules! func {
word: Some($value),
}
};
(@elem Prefix $id:expr, $value:expr) => {
(
$(#[$outer:meta])*
@elem Prefix $id:expr, $value:expr
) => {
$(#[$outer])*
NewListElem {
list_id: $id,
user_id: None,
@ -99,7 +115,11 @@ macro_rules! func {
(@out_type Word) => { String };
(@out_type Prefix) => { String };
(add: $fn:ident, $kind:ident) => {
(
$(#[$outer:meta])*
add: $fn:ident, $kind:ident
) => {
$(#[$outer])*
pub fn $fn(&self, conn: &Connection, vals: &[func!(@in_type $kind)]) -> Result<()> {
if self.kind() != ListType::$kind {
return Err(Error::InvalidValue);
@ -116,7 +136,11 @@ macro_rules! func {
}
};
(list: $fn:ident, $kind:ident, $table:ident) => {
(
$(#[$outer:meta])*
list: $fn:ident, $kind:ident, $table:ident
) => {
$(#[$outer])*
pub fn $fn(&self, conn: &Connection) -> Result<Vec<func!(@out_type $kind)>> {
if self.kind() != ListType::$kind {
return Err(Error::InvalidValue);
@ -132,7 +156,11 @@ macro_rules! func {
(set: $fn:ident, $kind:ident, $add:ident) => {
(
$(#[$outer:meta])*
set: $fn:ident, $kind:ident, $add:ident
) => {
$(#[$outer])*
pub fn $fn(&self, conn: &Connection, val: &[func!(@in_type $kind)]) -> Result<()> {
if self.kind() != ListType::$kind {
return Err(Error::InvalidValue);
@ -246,23 +274,35 @@ impl List {
private::ListElem::prefix_in_list(conn, self, word)
}
/// Insert new users in a list
func! {add: add_users, User}
func! {
/// Insert new users in a list
add: add_users, User
}
/// Insert new blogs in a list
func! {add: add_blogs, Blog}
func! {
/// Insert new blogs in a list
add: add_blogs, Blog
}
/// Insert new words in a list
func! {add: add_words, Word}
func! {
/// Insert new words in a list
add: add_words, Word
}
/// Insert new prefixes in a list
func! {add: add_prefixes, Prefix}
func! {
/// Insert new prefixes in a list
add: add_prefixes, Prefix
}
/// Get all users in the list
func! {list: list_users, User, users}
func! {
/// Get all users in the list
list: list_users, User, users
}
/// Get all blogs in the list
func! {list: list_blogs, Blog, blogs}
func! {
/// Get all blogs in the list
list: list_blogs, Blog, blogs
}
/// Get all words in the list
pub fn list_words(&self, conn: &Connection) -> Result<Vec<String>> {

@ -10,8 +10,8 @@ use plume_common::{
activity_pub::{inbox::FromId, Id},
utils::MediaProcessor,
};
use reqwest;
use std::{fs, path::Path};
use tokio::prelude::*;
#[derive(Clone, Identifiable, Queryable)]
pub struct Media {
@ -197,8 +197,7 @@ impl Media {
}
// TODO: merge with save_remote?
pub fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
let conn = &*c.conn;
pub async fn from_activity(c: &mut PlumeRocket, image: &Image) -> Result<Media> {
let remote_url = image.object_props.url_string().ok()?;
let ext = remote_url
.rsplit('.')
@ -211,14 +210,26 @@ impl Media {
ext
));
let mut dest = fs::File::create(path.clone()).ok()?;
reqwest::get(remote_url.as_str())
.ok()?
.copy_to(&mut dest)
.ok()?;
let mut dest = tokio::fs::File::create(path.clone()).await?;
let contents = reqwest::get(remote_url.as_str()).await?.bytes().await?;
dest.write_all(&contents).await?;
let owner_id = User::from_id(
c,
image
.object_props
.attributed_to_link_vec::<Id>()
.ok()?
.into_iter()
.next()?
.as_ref(),
None,
)
.map_err(|(_, e)| e)?
.id;
Media::insert(
conn,
&mut c.conn,
NewMedia {
file_path: path.to_str()?.to_string(),
alt_text: image.object_props.content_string().ok()?,
@ -226,19 +237,7 @@ impl Media {
remote_url: None,
sensitive: image.object_props.summary_string().is_ok(),
content_warning: image.object_props.summary_string().ok(),
owner_id: User::from_id(
c,
image
.object_props
.attributed_to_link_vec::<Id>()
.ok()?
.into_iter()
.next()?
.as_ref(),
None,
)
.map_err(|(_, e)| e)?
.id,
owner_id
},
)
}

@ -52,8 +52,8 @@ impl Mention {
}
}
pub fn build_activity(c: &PlumeRocket, ment: &str) -> Result<link::Mention> {
let user = User::find_by_fqn(c, ment)?;
pub async fn build_activity(c: &mut PlumeRocket, ment: &str) -> Result<link::Mention> {
let user = User::find_by_fqn(c, ment).await?;
let mut mention = link::Mention::default();
mention.link_props.set_href_string(user.ap_url)?;
mention.link_props.set_name_string(format!("@{}", ment))?;

@ -20,20 +20,35 @@ mod module {
pub flash_msg: Option<(String, String)>,
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
let conn = request.guard::<DbConn>()?;
let intl = request.guard::<rocket_i18n::I18n>()?;
let user = request.guard::<users::User>().succeeded();
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded();
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let conn = DbConn::from_request(request).await.succeeded().unwrap();
let intl = rocket_i18n::I18n::from_request(request)
.await
.succeeded()
.unwrap();
let user = users::User::from_request(request)
.await
.succeeded()
.unwrap();
let worker = request
.guard::<State<'_, Arc<ScheduledThreadPool>>>()
.await
.succeeded()
.unwrap();
let searcher = request
.guard::<State<'_, Arc<search::Searcher>>>()
.await
.succeeded()
.unwrap();
let flash_msg = request.guard::<FlashMessage<'_, '_>>().await.succeeded();
Outcome::Success(PlumeRocket {
conn,
intl,
user,
user: Some(user),
flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())),
worker: worker.clone(),
searcher: searcher.clone(),
@ -60,17 +75,18 @@ mod module {
pub worker: Arc<ScheduledThreadPool>,
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
let conn = request.guard::<DbConn>()?;
let user = request.guard::<users::User>().succeeded();
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let conn = try_outcome!(DbConn::from_request(request).await);
let user = try_outcome!(users::User::from_request(request).await);
let worker = try_outcome!(request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>());
let searcher = try_outcome!(request.guard::<'_, State<'_, Arc<search::Searcher>>>());
Outcome::Success(PlumeRocket {
conn,
user,
user: Some(user),
worker: worker.clone(),
searcher: searcher.clone(),
})

@ -19,8 +19,8 @@ use plume_common::{
},
utils::md_to_html,
};
use serde_json;
use std::collections::HashSet;
use tokio::runtime::Runtime;
pub type LicensedArticle = CustomObject<Licensed, Article>;
@ -554,12 +554,11 @@ impl FromId<PlumeRocket> for Post {
type Error = Error;
type Object = LicensedArticle;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Self::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, article: LicensedArticle) -> Result<Self> {
let conn = &*c.conn;
fn from_activity(c: &mut PlumeRocket, article: LicensedArticle) -> Result<Self> {
let searcher = &c.searcher;
let license = article.custom_props.license_string().unwrap_or_default();
let article = article.object;
@ -570,24 +569,24 @@ impl FromId<PlumeRocket> for Post {
.into_iter()
.fold((None, vec![]), |(blog, mut authors), link| {
let url = link;
match User::from_id(&c, &url, None) {
match User::from_id(&mut c, &url, None) {
Ok(u) => {
authors.push(u);
(blog, authors)
}
Err(_) => (blog.or_else(|| Blog::from_id(&c, &url, None).ok()), authors),
Err(_) => (blog.or_else(|| Blog::from_id(&mut c, &url, None).ok()), authors),
}
});
let cover = article
.object_props
.icon_object::<Image>()
.ok()
.and_then(|img| Media::from_activity(&c, &img).ok().map(|m| m.id));
let image = article.object_props.icon_object::<Image>().ok().unwrap();
let mut r = Runtime::new().unwrap();
let cover =
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
let title = article.object_props.name_string()?;
let post = Post::insert(
conn,
&mut c.conn,
NewPost {
blog_id: blog?.id,
slug: title.to_kebab_case(),
@ -610,7 +609,7 @@ impl FromId<PlumeRocket> for Post {
for author in authors {
PostAuthor::insert(
conn,
&mut c.conn,
NewPostAuthor {
post_id: post.id,
author_id: author.id,
@ -627,7 +626,7 @@ impl FromId<PlumeRocket> for Post {
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag {
for tag in tags {
serde_json::from_value::<link::Mention>(tag.clone())
.map(|m| Mention::from_activity(conn, &m, post.id, true, true))
.map(|m| Mention::from_activity(&mut c.conn, &m, post.id, true, true))
.ok();
serde_json::from_value::<Hashtag>(tag.clone())
@ -635,7 +634,7 @@ impl FromId<PlumeRocket> for Post {
.and_then(|t| {
let tag_name = t.name_string()?;
Ok(Tag::from_activity(
conn,
&mut c.conn,
&t,
post.id,
hashtags.remove(&tag_name),
@ -651,21 +650,21 @@ impl FromId<PlumeRocket> for Post {
}
}
impl AsObject<User, Create, &PlumeRocket> for Post {
impl AsObject<User, Create, &mut PlumeRocket> for Post {
type Error = Error;
type Output = Post;
fn activity(self, _c: &PlumeRocket, _actor: User, _id: &str) -> Result<Post> {
fn activity(self, _c: &mut PlumeRocket, _actor: User, _id: &str) -> Result<Post> {
// TODO: check that _actor is actually one of the author?
Ok(self)
}
}
impl AsObject<User, Delete, &PlumeRocket> for Post {
impl AsObject<User, Delete, &mut PlumeRocket> for Post {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
let can_delete = self
.get_authors(&c.conn)?
.into_iter()
@ -693,23 +692,28 @@ impl FromId<PlumeRocket> for PostUpdate {
type Error = Error;
type Object = LicensedArticle;
fn from_db(_: &PlumeRocket, _: &str) -> Result<Self> {
fn from_db(_: &mut PlumeRocket, _: &str) -> Result<Self> {
// Always fail because we always want to deserialize the AP object
Err(Error::NotFound)
}
fn from_activity(c: &PlumeRocket, updated: LicensedArticle) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, updated: LicensedArticle) -> Result<Self> {
let image = updated
.object
.object_props
.icon_object::<Image>()
.ok()
.unwrap();
let mut r = Runtime::new().unwrap();
let cover =
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
Ok(PostUpdate {
ap_url: updated.object.object_props.id_string()?,
title: updated.object.object_props.name_string().ok(),
subtitle: updated.object.object_props.summary_string().ok(),
content: updated.object.object_props.content_string().ok(),
cover: updated
.object
.object_props
.icon_object::<Image>()
.ok()
.and_then(|img| Media::from_activity(&c, &img).ok().map(|m| m.id)),
cover,
source: updated
.object
.ap_object_props
@ -722,11 +726,11 @@ impl FromId<PlumeRocket> for PostUpdate {
}
}
impl AsObject<User, Update, &PlumeRocket> for PostUpdate {
impl AsObject<User, Update, &mut PlumeRocket> for PostUpdate {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
let conn = &*c.conn;
let searcher = &c.searcher;
let mut post = Post::from_id(c, &self.ap_url, None).map_err(|(_, e)| e)?;

@ -107,11 +107,11 @@ impl Reshare {
}
}
impl AsObject<User, Announce, &PlumeRocket> for Post {
impl AsObject<User, Announce, &mut PlumeRocket> for Post {
type Error = Error;
type Output = Reshare;
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Reshare> {
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Reshare> {
let conn = &*c.conn;
let reshare = Reshare::insert(
conn,
@ -132,11 +132,11 @@ impl FromId<PlumeRocket> for Reshare {
type Error = Error;
type Object = Announce;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Reshare::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, act: Announce) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, act: Announce) -> Result<Self> {
let res = Reshare::insert(
&c.conn,
NewReshare {
@ -154,11 +154,11 @@ impl FromId<PlumeRocket> for Reshare {
}
}
impl AsObject<User, Undo, &PlumeRocket> for Reshare {
impl AsObject<User, Undo, &mut PlumeRocket> for Reshare {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
let conn = &*c.conn;
if actor.id == self.user_id {
diesel::delete(&self).execute(conn)?;

@ -208,7 +208,7 @@ impl Timeline {
.map_err(Error::from)
}
pub fn add_to_all_timelines(rocket: &PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<()> {
pub fn add_to_all_timelines(rocket: &mut PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<()> {
let timelines = timeline_definition::table
.load::<Self>(rocket.conn.deref())
.map_err(Error::from)?;
@ -231,7 +231,7 @@ impl Timeline {
Ok(())
}
pub fn matches(&self, rocket: &PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<bool> {
pub fn matches(&self, rocket: &mut PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<bool> {
let query = TimelineQuery::parse(&self.query)?;
query.matches(rocket, self, post, kind)
}

@ -7,7 +7,9 @@ use crate::{
users::User,
PlumeRocket, Result,
};
use futures::stream::{self, StreamExt};
use plume_common::activity_pub::inbox::AsActor;
use tokio::runtime::Runtime;
use whatlang::{self, Lang};
#[derive(Debug, Clone, PartialEq)]
@ -160,7 +162,7 @@ enum TQ<'a> {
impl<'a> TQ<'a> {
fn matches(
&self,
rocket: &PlumeRocket,
rocket: &mut PlumeRocket,
timeline: &Timeline,
post: &Post,
kind: Kind<'_>,
@ -205,7 +207,7 @@ enum Arg<'a> {
impl<'a> Arg<'a> {
pub fn matches(
&self,
rocket: &PlumeRocket,
rocket: &mut PlumeRocket,
timeline: &Timeline,
post: &Post,
kind: Kind<'_>,
@ -230,7 +232,7 @@ enum WithList {
impl WithList {
pub fn matches(
&self,
rocket: &PlumeRocket,
rocket: &mut PlumeRocket,
timeline: &Timeline,
post: &Post,
list: &List<'_>,
@ -295,15 +297,33 @@ impl WithList {
}
}
List::Array(list) => match self {
WithList::Blog => Ok(list
.iter()
.filter_map(|b| Blog::find_by_fqn(rocket, b).ok())
.any(|b| b.id == post.blog_id)),
WithList::Blog => {
let mut rt = Runtime::new().unwrap();
rt.block_on(async move {
Ok(stream::iter(list)
.filter_map(|b| async move {
Some(Blog::find_by_fqn(rocket, b).await.ok().unwrap())
})
.collect::<Vec<_>>()
.await
.into_iter()
.any(|b| b.id == post.blog_id))
})
}
WithList::Author { boosts, likes } => match kind {
Kind::Original => Ok(list
.iter()
.filter_map(|a| User::find_by_fqn(rocket, a).ok())
.any(|a| post.is_author(&rocket.conn, a.id).unwrap_or(false))),
Kind::Original => {
let mut rt = Runtime::new().unwrap();
rt.block_on(async move {
Ok(stream::iter(list)
.filter_map(|a| async move {
Some(User::find_by_fqn(rocket, a).await.ok().unwrap())
})
.collect::<Vec<_>>()
.await
.into_iter()
.any(|a| post.is_author(&rocket.conn, a.id).unwrap_or(false)))
})
}
Kind::Reshare(u) => {
if *boosts {
Ok(list.iter().any(|user| &u.fqn == user))
@ -371,7 +391,7 @@ enum Bool {
impl Bool {
pub fn matches(
&self,
rocket: &PlumeRocket,
rocket: &mut PlumeRocket,
timeline: &Timeline,
post: &Post,
kind: Kind<'_>,
@ -642,7 +662,7 @@ impl<'a> TimelineQuery<'a> {
pub fn matches(
&self,
rocket: &PlumeRocket,
rocket: &mut PlumeRocket,
timeline: &Timeline,
post: &Post,
kind: Kind<'_>,

@ -11,7 +11,6 @@ use activitypub::{
object::{Image, Tombstone},
Activity, CustomObject, Endpoint,
};
use bcrypt;
use chrono::{NaiveDateTime, Utc};
use diesel::{self, BelongingToDsl, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use openssl::{
@ -37,7 +36,6 @@ use rocket::{
outcome::IntoOutcome,
request::{self, FromRequest, Request},
};
use serde_json;
use std::{
cmp::PartialEq,
hash::{Hash, Hasher},
@ -191,7 +189,7 @@ impl User {
.map_err(Error::from)
}
pub fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result<User> {
pub async fn find_by_fqn(c: &mut PlumeRocket, fqn: &str) -> Result<User> {
let from_db = users::table
.filter(users::fqn.eq(fqn))
.first(&*c.conn)
@ -199,12 +197,13 @@ impl User {
if let Some(from_db) = from_db {
Ok(from_db)
} else {
User::fetch_from_webfinger(c, fqn)
User::fetch_from_webfinger(c, fqn).await
}
}
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<User> {
let link = resolve(acct.to_owned(), true)?
async fn fetch_from_webfinger(c: &mut PlumeRocket, acct: &str) -> Result<User> {
let link = resolve(acct.to_owned(), true)
.await?
.links
.into_iter()
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
@ -212,8 +211,9 @@ impl User {
User::from_id(c, link.href.as_ref()?, None).map_err(|(_, e)| e)
}
pub fn fetch_remote_interact_uri(acct: &str) -> Result<String> {
resolve(acct.to_owned(), true)?
pub async fn fetch_remote_interact_uri(acct: &str) -> Result<String> {
resolve(acct.to_owned(), true)
.await?
.links
.into_iter()
.find(|l| l.rel == "http://ostatus.org/schema/1.0/subscribe")
@ -221,9 +221,9 @@ impl User {
.ok_or(Error::Webfinger)
}
fn fetch(url: &str) -> Result<CustomPerson> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
async fn fetch(url: &str) -> Result<CustomPerson> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
.header(
@ -235,8 +235,9 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send()
.await?;
let text = &res.text().await?;
// without this workaround, publicKey is not correctly deserialized
let ap_sign = serde_json::from_str::<ApSignature>(text)?;
let mut json = serde_json::from_str::<CustomPerson>(text)?;
@ -244,48 +245,48 @@ impl User {
Ok(json)
}
pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
User::fetch(url).and_then(|json| User::from_activity(c, json))
pub async fn fetch_from_url(c: &mut PlumeRocket, url: &str) -> Result<User> {
let json = User::fetch(url).await?;
User::from_activity(c, json)
}
pub fn refetch(&self, conn: &Connection) -> Result<()> {
User::fetch(&self.ap_url.clone()).and_then(|json| {
let avatar = Media::save_remote(
conn,
json.object
.object_props
.icon_image()?
.object_props
.url_string()?,
&self,
)
.ok();
pub async fn refetch(&self, conn: &Connection) -> Result<()> {
let json = User::fetch(&self.ap_url.clone()).await?;
let avatar = Media::save_remote(
conn,
json.object
.object_props
.icon_image()?
.object_props
.url_string()?,
&self,
)
.ok();
diesel::update(self)
.set((
users::username.eq(json.object.ap_actor_props.preferred_username_string()?),
users::display_name.eq(json.object.object_props.name_string()?),
users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?),
users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?),
users::summary.eq(SafeString::new(
&json
.object
.object_props
.summary_string()
.unwrap_or_default(),
)),
users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?),
users::avatar_id.eq(avatar.map(|a| a.id)),
users::last_fetched_date.eq(Utc::now().naive_utc()),
users::public_key.eq(json
.custom_props
.public_key_publickey()?
.public_key_pem_string()?),
))
.execute(conn)
.map(|_| ())
.map_err(Error::from)
})
diesel::update(self)
.set((
users::username.eq(json.object.ap_actor_props.preferred_username_string()?),
users::display_name.eq(json.object.object_props.name_string()?),
users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?),
users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?),
users::summary.eq(SafeString::new(
&json
.object
.object_props
.summary_string()
.unwrap_or_default(),
)),
users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?),
users::avatar_id.eq(avatar.map(|a| a.id)),
users::last_fetched_date.eq(Utc::now().naive_utc()),
users::public_key.eq(json
.custom_props
.public_key_publickey()?
.public_key_pem_string()?),
))
.execute(conn)
.map(|_| ())
.map_err(Error::from)
}
pub fn hash_pass(pass: &str) -> Result<String> {
@ -356,9 +357,10 @@ impl User {
.set_part_of_link(Id::new(&self.outbox_url))?;
Ok(ActivityStream::new(coll))
}
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
async fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
.header(
@ -370,8 +372,9 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send()
.await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
let items = json["items"]
.as_array()
@ -386,9 +389,9 @@ impl User {
};
Ok((items, next))
}
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
pub async fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.outbox_url[..])
.header(
@ -400,13 +403,14 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send()
.await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
if let Some(first) = json.get("first") {
let mut items: Vec<T> = Vec::new();
let mut next = first.as_str().unwrap().to_owned();
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next) {
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next).await {
if page.is_empty() {
break;
}
@ -431,9 +435,9 @@ impl User {
}
}
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
pub async fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.followers_endpoint[..])
.header(
@ -445,8 +449,9 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send()
.await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
Ok(json["items"]
.as_array()
@ -789,11 +794,12 @@ impl User {
}
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<User, ()> {
let conn = request.guard::<DbConn>()?;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let conn = try_outcome!(DbConn::from_request(request).await);
request
.cookies()
.get_private(AUTH_COOKIE)
@ -815,11 +821,11 @@ impl FromId<PlumeRocket> for User {
type Error = Error;
type Object = CustomPerson;
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
Self::find_by_ap_url(&c.conn, id)
}
fn from_activity(c: &PlumeRocket, acct: CustomPerson) -> Result<Self> {
fn from_activity(c: &mut PlumeRocket, acct: CustomPerson) -> Result<Self> {
let url = Url::parse(&acct.object.object_props.id_string()?)?;
let inst = url.host_str()?;
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
@ -911,7 +917,7 @@ impl FromId<PlumeRocket> for User {
}
}
impl AsActor<&PlumeRocket> for User {
impl AsActor<&mut PlumeRocket> for User {
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}
@ -927,11 +933,11 @@ impl AsActor<&PlumeRocket> for User {
}
}
impl AsObject<User, Delete, &PlumeRocket> for User {
impl AsObject<User, Delete, &mut PlumeRocket> for User {
type Error = Error;
type Output = ();
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
if self.id == actor.id {
self.delete(&c.conn, &c.searcher).map(|_| ())
} else {

@ -10,7 +10,8 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5);\n"
"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 "
"&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5);\n"
"X-Crowdin-Project: plume\n"
"X-Crowdin-Language: ar\n"
"X-Crowdin-File: /master/po/plume/plume.pot\n"

@ -1 +1 @@
nightly-2020-01-15
nightly-2020-05-05

@ -35,6 +35,7 @@ impl Scope for plume_models::posts::Post {
pub struct Authorization<A, S>(pub ApiToken, PhantomData<(A, S)>);
#[rocket::async_trait]
impl<'a, 'r, A, S> FromRequest<'a, 'r> for Authorization<A, S>
where
A: Action,
@ -42,9 +43,10 @@ where
{
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Authorization<A, S>, ()> {
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Authorization<A, S>, ()> {
request
.guard::<ApiToken>()
.await
.map_failure(|_| (Status::Unauthorized, ()))
.and_then(|token| {
if token.can(A::to_str(), S::to_str()) {

@ -4,7 +4,6 @@ use rocket::{
response::{self, Responder},
};
use rocket_contrib::json::Json;
use serde_json;
use plume_common::utils::random_hex;
use plume_models::{api_tokens::*, apps::App, users::User, Error, PlumeRocket};
@ -26,21 +25,31 @@ impl From<std::option::NoneError> for ApiError {
}
}
#[rocket::async_trait]
impl<'r> Responder<'r> for ApiError {
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> {
async fn respond_to(self, req: &'r Request<'_>) -> response::Result<'r> {
match self.0 {
Error::NotFound => Json(json!({
"error": "Not found"
}))
.respond_to(req),
Error::Unauthorized => Json(json!({
"error": "You are not authorized to access this resource"
}))
.respond_to(req),
_ => Json(json!({
"error": "Server error"
}))
.respond_to(req),
Error::NotFound => {
Json(json!({
"error": "Not found"
}))
.respond_to(req)
.await
}
Error::Unauthorized => {
Json(json!({
"error": "You are not authorized to access this resource"
}))
.respond_to(req)
.await
}
_ => {
Json(json!({
"error": "Server error"
}))
.respond_to(req)
.await
}
}
}
}
@ -55,17 +64,16 @@ pub struct OAuthRequest {
}
#[get("/oauth2?<query..>")]
pub fn oauth(
pub async fn oauth(
query: Form<OAuthRequest>,
rockets: PlumeRocket,
) -> Result<Json<serde_json::Value>, ApiError> {
let conn = &*rockets.conn;
let app = App::find_by_client_id(conn, &query.client_id)?;
let app = App::find_by_client_id(&rockets.conn, &query.client_id)?;
if app.client_secret == query.client_secret {
if let Ok(user) = User::find_by_fqn(&rockets, &query.username) {
if let Ok(user) = User::find_by_fqn(&mut rockets, &query.username).await {
if user.auth(&query.password) {
let token = ApiToken::insert(
conn,
&rockets.conn,
NewApiToken {
app_id: app.id,
user_id: user.id,
@ -85,7 +93,7 @@ pub fn oauth(
// Making fake password verification to avoid different
// response times that would make it possible to know
// if a username is registered or not.
User::get(conn, 1)?.auth(&query.password);
User::get(&rockets.conn, 1)?.auth(&query.password);
Ok(Json(json!({
"error": "Invalid credentials"
})))

@ -98,7 +98,7 @@ pub fn list(
}
#[post("/posts", data = "<payload>")]
pub fn create(
pub async fn create(
auth: Authorization<Write, Post>,
payload: Json<NewPostData>,
rockets: PlumeRocket,
@ -192,7 +192,7 @@ pub fn create(
for m in mentions.into_iter() {
Mention::from_activity(
&*conn,
&Mention::build_activity(&rockets, &m)?,
&Mention::build_activity(&rockets, &m).await?,
post.id,
true,
true,

@ -9,9 +9,9 @@ use plume_models::{
use rocket::{data::*, http::Status, response::status, Outcome::*, Request};
use rocket_contrib::json::*;
use serde::Deserialize;
use std::io::Read;
use tokio::io::AsyncReadExt;
pub fn handle_incoming(
pub async fn handle_incoming(
rockets: PlumeRocket,
data: SignedJson<serde_json::Value>,
headers: Headers<'_>,
@ -32,6 +32,7 @@ pub fn handle_incoming(
// maybe we just know an old key?
actor
.refetch(conn)
.await
.and_then(|_| User::get(conn, actor.id))
.and_then(|u| {
if verify_http_headers(&u, &headers.0, &sig).is_secure() || act.clone().verify(&u) {
@ -73,32 +74,31 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for SignedJson<T> {
type Owned = String;
type Borrowed = str;
fn transform(
r: &Request<'_>,
d: Data,
) -> Transform<rocket::data::Outcome<Self::Owned, Self::Error>> {
let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT);
let mut s = String::with_capacity(512);
match d.open().take(size_limit).read_to_string(&mut s) {
Ok(_) => Transform::Borrowed(Success(s)),
Err(e) => Transform::Borrowed(Failure((Status::BadRequest, JsonError::Io(e)))),
}
fn transform<'r>(r: &'r Request, d: Data) -> TransformFuture<'r, Self::Owned, Self::Error> {
Box::pin(async move {
let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT);
let mut s = String::with_capacity(512);
let outcome = match d.open().take(size_limit).read_to_string(&mut s).await {
Ok(_) => Success(s),
Err(e) => Failure((Status::BadRequest, JsonError::Io(e))),
};
Transform::Borrowed(outcome)
})
}
fn from_data(
_: &Request<'_>,
o: Transformed<'a, Self>,
) -> rocket::data::Outcome<Self, Self::Error> {
let string = o.borrowed()?;
match serde_json::from_str(&string) {
Ok(v) => Success(SignedJson(Digest::from_body(&string), Json(v))),
Err(e) => {
if e.is_data() {
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
} else {
Failure((Status::BadRequest, JsonError::Parse(string, e)))
) -> FromDataFuture<'a, Self, Self::Error> {
Box::pin(async move {
let string = try_outcome!(o.borrowed());
match serde_json::from_str(&string) {
Ok(v) => Success(SignedJson(Digest::from_body(&string), Json(v))),
Err(e) if e.is_data() => {
return Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
}
Err(e) => Failure((Status::BadRequest, JsonError::Parse(string, e))),
}
}
})
}
}

@ -1,16 +1,15 @@
#![allow(clippy::too_many_arguments)]
#![feature(decl_macro, proc_macro_hygiene, try_trait)]
#![feature(proc_macro_hygiene, try_trait)]
#[macro_use]
extern crate gettext_macros;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate runtime_fmt;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate validator_derive;
extern crate validator;
use clap::App;
use diesel::r2d2::ConnectionManager;
@ -21,7 +20,6 @@ use plume_models::{
search::{Searcher as UnmanagedSearcher, SearcherError},
Connection, Error, CONFIG,
};
use rocket_csrf::CsrfFairingBuilder;
use scheduled_thread_pool::ScheduledThreadPool;
use std::process::exit;
use std::sync::{Arc, Mutex};
@ -275,25 +273,7 @@ Then try to restart Plume
.manage(dbpool)
.manage(Arc::new(workpool))
.manage(searcher)
.manage(include_i18n!())
.attach(
CsrfFairingBuilder::new()
.set_default_target(
"/csrf-violation?target=<uri>".to_owned(),
rocket::http::Method::Post,
)
.add_exceptions(vec![
("/inbox".to_owned(), "/inbox".to_owned(), None),
(
"/@/<name>/inbox".to_owned(),
"/@/<name>/inbox".to_owned(),
None,
),
("/api/<path..>".to_owned(), "/api/<path..>".to_owned(), None),
])
.finalize()
.expect("main: csrf fairing creation error"),
);
.manage(include_i18n!());
#[cfg(feature = "test")]
let rocket = rocket.mount("/test", routes![test_routes::health,]);

@ -19,10 +19,14 @@ use plume_models::{
};
#[get("/~/<name>?<page>", rank = 2)]
pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
pub async fn details(
name: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<Ructe, ErrorPage> {
let page = page.unwrap_or_default();
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &name)?;
let blog = Blog::find_by_fqn(&rockets, &name).await?;
let posts = Post::blog_page(conn, &blog, page.limits())?;
let articles_count = Post::count_for_blog(conn, &blog)?;
let authors = &blog.list_authors(conn)?;
@ -38,12 +42,12 @@ pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result
}
#[get("/~/<name>", rank = 1)]
pub fn activity_details(
pub async fn activity_details(
name: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<CustomGroup>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
let blog = Blog::find_by_fqn(&rockets, &name).await?;
Some(ActivityStream::new(blog.to_activity(&*rockets.conn).ok()?))
}
@ -83,7 +87,7 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> {
}
#[post("/blogs/new", data = "<form>")]
pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOrRedirect {
pub async fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOrRedirect {
let slug = utils::make_actor_id(&form.title);
let conn = &*rockets.conn;
let intl = &rockets.intl.catalog;
@ -93,7 +97,7 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
Ok(_) => ValidationErrors::new(),
Err(e) => e,
};
if Blog::find_by_fqn(&rockets, &slug).is_ok() {
if Blog::find_by_fqn(&rockets, &slug).await.is_ok() {
errors.add(
"title",
ValidationError {
@ -143,9 +147,11 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
}
#[post("/~/<name>/delete")]
pub fn delete(name: String, rockets: PlumeRocket) -> RespondOrRedirect {
pub async fn delete(name: String, rockets: PlumeRocket) -> RespondOrRedirect {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &name).expect("blog::delete: blog not found");
let blog = Blog::find_by_fqn(&rockets, &name)
.await
.expect("blog::delete: blog not found");
if rockets
.user
@ -184,9 +190,9 @@ pub struct EditForm {
}
#[get("/~/<name>/edit")]
pub fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
pub async fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &name)?;
let blog = Blog::find_by_fqn(&rockets, &name).await?;
if rockets
.user
.clone()
@ -233,14 +239,16 @@ fn check_media(conn: &Connection, id: i32, user: &User) -> bool {
}
#[put("/~/<name>/edit", data = "<form>")]
pub fn update(
pub async fn update(
name: String,
form: LenientForm<EditForm>,
rockets: PlumeRocket,
) -> RespondOrRedirect {
let conn = &*rockets.conn;
let intl = &rockets.intl.catalog;
let mut blog = Blog::find_by_fqn(&rockets, &name).expect("blog::update: blog not found");
let mut blog = Blog::find_by_fqn(&rockets, &name)
.await
.expect("blog::update: blog not found");
if !rockets
.user
.clone()
@ -342,23 +350,28 @@ pub fn update(
}
#[get("/~/<name>/outbox")]
pub fn outbox(name: String, rockets: PlumeRocket) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
pub async fn outbox(
name: String,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_by_fqn(&rockets, &name).await?;
Some(blog.outbox(&*rockets.conn).ok()?)
}
#[allow(unused_variables)]
#[get("/~/<name>/outbox?<page>")]
pub fn outbox_page(
pub async fn outbox_page(
name: String,
page: Page,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollectionPage>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
let blog = Blog::find_by_fqn(&rockets, &name).await?;
Some(blog.outbox_page(&*rockets.conn, page.limits()).ok()?)
}
#[get("/~/<name>/atom.xml")]
pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
pub async fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
let blog = Blog::find_by_fqn(&rockets, &name).await?;
let conn = &*rockets.conn;
let entries = Post::get_recents_for_blog(&*conn, &blog, 15).ok()?;
let uri = Instance::get_local()

@ -22,13 +22,13 @@ use plume_models::{
#[derive(Default, FromForm, Debug, Validate)]
pub struct NewCommentForm {
pub responding_to: Option<i32>,
#[validate(length(min = "1", message = "Your comment can't be empty"))]
#[validate(length(min = 1, message = "Your comment can't be empty"))]
pub content: String,
pub warning: String,
}
#[post("/~/<blog_name>/<slug>/comment", data = "<form>")]
pub fn create(
pub async fn create(
blog_name: String,
slug: String,
form: LenientForm<NewCommentForm>,
@ -36,10 +36,12 @@ pub fn create(
rockets: PlumeRocket,
) -> Result<Flash<Redirect>, Ructe> {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &blog_name).expect("comments::create: blog error");
let blog = Blog::find_by_fqn(&rockets, &blog_name)
.await
.expect("comments::create: blog error");
let post = Post::find_by_slug(&*conn, &slug, blog.id).expect("comments::create: post error");
form.validate()
.map(|_| {
match form.validate() {
Ok(_ok) => {
let (html, mentions, _hashtags) = utils::md_to_html(
form.content.as_ref(),
Some(
@ -66,6 +68,7 @@ pub fn create(
.expect("comments::create: insert error");
let new_comment = comm
.create_activity(&rockets)
.await
.expect("comments::create: activity error");
// save mentions
@ -73,6 +76,7 @@ pub fn create(
Mention::from_activity(
&*conn,
&Mention::build_activity(&rockets, &ment)
.await
.expect("comments::create: build mention error"),
comm.id,
false,
@ -90,14 +94,14 @@ pub fn create(
.worker
.execute(move || broadcast(&user_clone, new_comment, dest));
Flash::success(
Ok(Flash::success(
Redirect::to(
uri!(super::posts::details: blog = blog_name, slug = slug, responding_to = _),
),
i18n!(&rockets.intl.catalog, "Your comment has been posted."),
)
})
.map_err(|errors| {
))
}
Err(errors) => {
// TODO: de-duplicate this code
let comments = CommentTree::from_post(&*conn, &post, Some(&user))
.expect("comments::create: comments error");
@ -106,7 +110,7 @@ pub fn create(
.responding_to
.and_then(|r| Comment::get(&*conn, r).ok());
render!(posts::details(
Err(render!(posts::details(
&rockets.to_context(),
post.clone(),
blog,
@ -133,8 +137,9 @@ pub fn create(
post.get_authors(&*conn)
.expect("comments::create: authors error")[0]
.clone()
))
})
)))
}
}
}
#[post("/~/<blog>/<slug>/comment/<id>/delete")]
@ -174,15 +179,16 @@ pub fn delete(
}
#[get("/~/<_blog>/<_slug>/comment/<id>")]
pub fn activity_pub(
pub async fn activity_pub(
_blog: String,
_slug: String,
id: i32,
_ap: ApRequest,
rockets: PlumeRocket,
) -> Option<ActivityStream<Note>> {
Comment::get(&*rockets.conn, id)
.and_then(|c| c.to_activity(&rockets))
.ok()
.map(ActivityStream::new)
let c = match Comment::get(&*rockets.conn, id) {
Ok(c) => c.to_activity(&rockets).await.ok(),
Err(_) => None,
};
c.map(ActivityStream::new)
}

@ -1,6 +1,7 @@
use crate::template_utils::{IntoContext, Ructe};
use plume_models::{Error, PlumeRocket};
use rocket::{
request::FromRequest,
response::{self, Responder},
Request,
};
@ -14,35 +15,46 @@ impl From<Error> for ErrorPage {
}
}
#[rocket::async_trait]
impl<'r> Responder<'r> for ErrorPage {
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> {
let rockets = req.guard::<PlumeRocket>().unwrap();
async fn respond_to(self, req: &'r Request<'_>) -> response::Result<'r> {
let rockets = PlumeRocket::from_request(req).await.unwrap();
match self.0 {
Error::NotFound => render!(errors::not_found(&rockets.to_context())).respond_to(req),
Error::NotFound => {
render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
}
Error::Unauthorized => {
render!(errors::not_found(&rockets.to_context())).respond_to(req)
render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
}
_ => {
render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
}
_ => render!(errors::not_found(&rockets.to_context())).respond_to(req),
}
}
}
#[catch(404)]
pub fn not_found(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().unwrap();
pub async fn not_found(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().await.unwrap();
render!(errors::not_found(&rockets.to_context()))
}
#[catch(422)]
pub fn unprocessable_entity(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().unwrap();
pub async fn unprocessable_entity(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().await.unwrap();
render!(errors::unprocessable_entity(&rockets.to_context()))
}
#[catch(500)]
pub fn server_error(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().unwrap();
pub async fn server_error(req: &Request<'_>) -> Ructe {
let rockets = req.guard::<PlumeRocket>().await.unwrap();
render!(errors::server_error(&rockets.to_context()))
}

@ -5,7 +5,7 @@ use rocket::{
use rocket_contrib::json::Json;
use rocket_i18n::I18n;
use scheduled_thread_pool::ScheduledThreadPool;
use serde_json;
use std::path::PathBuf;
use std::str::FromStr;
use validator::{Validate, ValidationErrors};
@ -76,12 +76,12 @@ pub fn admin_mod(_mod: Moderator, rockets: PlumeRocket) -> Ructe {
#[derive(Clone, FromForm, Validate)]
pub struct InstanceSettingsForm {
#[validate(length(min = "1"))]
#[validate(length(min = 1))]
pub name: String,
pub open_registrations: bool,
pub short_description: SafeString,
pub long_description: SafeString,
#[validate(length(min = "1"))]
#[validate(length(min = 1))]
pub default_license: String,
}
@ -386,17 +386,21 @@ fn ban(
}
#[post("/inbox", data = "<data>")]
pub fn shared_inbox(
pub async fn shared_inbox(
rockets: PlumeRocket,
data: inbox::SignedJson<serde_json::Value>,
headers: Headers<'_>,
) -> Result<String, status::BadRequest<&'static str>> {
inbox::handle_incoming(rockets, data, headers)
inbox::handle_incoming(rockets, data, headers).await
}
#[get("/remote_interact?<target>")]
pub fn interact(rockets: PlumeRocket, user: Option<User>, target: String) -> Option<Redirect> {
if User::find_by_fqn(&rockets, &target).is_ok() {
pub async fn interact(
rockets: PlumeRocket,
user: Option<User>,
target: String,
) -> Option<Redirect> {
if User::find_by_fqn(&rockets, &target).await.is_ok() {
return Some(Redirect::to(uri!(super::user::details: name = target)));
}

@ -7,9 +7,8 @@ use chrono::naive::NaiveDateTime;
use plume_models::{posts::Post, Connection, CONFIG, ITEMS_PER_PAGE};
use rocket::{
http::{
hyper::header::{CacheControl, CacheDirective, ETag, EntityTag},
uri::{FromUriParam, Query},
RawStr, Status,
Header, RawStr, Status,
},
request::{self, FromFormValue, FromRequest, Request},
response::{self, Flash, NamedFile, Redirect, Responder, Response},
@ -95,10 +94,11 @@ impl Page {
#[derive(Shrinkwrap)]
pub struct ContentLen(pub u64);
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for ContentLen {
type Error = ();
fn from_request(r: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
async fn from_request(r: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
match r.limits().get("forms") {
Some(l) => Outcome::Success(ContentLen(l)),
None => Outcome::Failure((Status::InternalServerError, ())),
@ -208,14 +208,15 @@ pub mod well_known;
#[response()]
pub struct CachedFile {
inner: NamedFile,
cache_control: CacheControl,
cache_control: Header<'static>,
}
#[derive(Debug)]
pub struct ThemeFile(NamedFile);
#[rocket::async_trait]
impl<'r> Responder<'r> for ThemeFile {
fn respond_to(self, r: &Request<'_>) -> response::Result<'r> {
async fn respond_to(self, r: &'r Request<'_>) -> response::Result<'r> {
let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?;
let mut hasher = DefaultHasher::new();
@ -228,12 +229,12 @@ impl<'r> Responder<'r> for ThemeFile {
{
Response::build()
.status(Status::NotModified)
.header(ETag(EntityTag::strong(etag)))
.header(Header::new("ETag", etag))
.ok()
} else {
Response::build()
.merge(self.0.respond_to(r)?)
.header(ETag(EntityTag::strong(etag)))
.merge(self.0.respond_to(r).await.ok().unwrap())
.header(Header::new("ETag", etag))
.ok()
}
}
@ -256,7 +257,7 @@ pub fn plume_media_files(file: PathBuf) -> Option<CachedFile> {
.ok()
.map(|f| CachedFile {
inner: f,
cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]),
cache_control: Header::new("Cache-Control", format!("max-age={}", 60 * 60 * 24 * 30)),
})
}
#[get("/static/<file..>", rank = 3)]
@ -265,6 +266,6 @@ pub fn static_files(file: PathBuf) -> Option<CachedFile> {
.ok()
.map(|f| CachedFile {
inner: f,
cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]),
cache_control: Header::new("Cache-Control", format!("max-age={}", 60 * 60 * 24 * 30)),
})
}

@ -1,3 +1,4 @@
use activitypub::link;
use chrono::Utc;
use heck::{CamelCase, KebabCase};
use rocket::request::LenientForm;
@ -11,7 +12,7 @@ use std::{
use validator::{Validate, ValidationError, ValidationErrors};
use crate::routes::{
comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm, RespondOrRedirect,
comments::NewCommentForm, errors::ErrorPage, ContentLen, Page, RemoteForm, RespondOrRedirect,
};
use crate::template_utils::{IntoContext, Ructe};
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest};
@ -33,7 +34,7 @@ use plume_models::{
};
#[get("/~/<blog>/<slug>?<responding_to>", rank = 4)]
pub fn details(
pub async fn details(
blog: String,
slug: String,
responding_to: Option<i32>,
@ -41,7 +42,7 @@ pub fn details(
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let user = rockets.user.clone();
let blog = Blog::find_by_fqn(&rockets, &blog)?;
let blog = Blog::find_by_fqn(&rockets, &blog).await?;
let post = Post::find_by_slug(&*conn, &slug, blog.id)?;
if !(post.published
|| post
@ -99,14 +100,14 @@ pub fn details(
}
#[get("/~/<blog>/<slug>", rank = 3)]
pub fn activity_details(
pub async fn activity_details(
blog: String,
slug: String,
_ap: ApRequest,
rockets: PlumeRocket,
) -> Result<ActivityStream<LicensedArticle>, Option<String>> {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &blog).map_err(|_| None)?;
let blog = Blog::find_by_fqn(&rockets, &blog).await.map_err(|_| None)?;
let post = Post::find_by_slug(&*conn, &slug, blog.id).map_err(|_| None)?;
if post.published {
Ok(ActivityStream::new(
@ -130,9 +131,9 @@ pub fn new_auth(blog: String, i18n: I18n) -> Flash<Redirect> {
}
#[get("/~/<blog>/new", rank = 1)]
pub fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
pub async fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let b = Blog::find_by_fqn(&rockets, &blog)?;
let b = Blog::find_by_fqn(&rockets, &blog).await?;
let user = rockets.user.clone().unwrap();
if !user.is_author_in(&*conn, &b)? {
@ -162,7 +163,7 @@ pub fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result<Ructe,
}
#[get("/~/<blog>/<slug>/edit")]
pub fn edit(
pub async fn edit(
blog: String,
slug: String,
cl: ContentLen,
@ -170,7 +171,7 @@ pub fn edit(
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let intl = &rockets.intl.catalog;
let b = Blog::find_by_fqn(&rockets, &blog)?;
let b = Blog::find_by_fqn(&rockets, &blog).await?;
let post = Post::find_by_slug(&*conn, &slug, b.id)?;
let user = rockets.user.clone().unwrap();
@ -216,7 +217,7 @@ pub fn edit(
}
#[post("/~/<blog>/<slug>/edit", data = "<form>")]
pub fn update(
pub async fn update(
blog: String,
slug: String,
cl: ContentLen,
@ -224,7 +225,9 @@ pub fn update(
rockets: PlumeRocket,
) -> RespondOrRedirect {
let conn = &*rockets.conn;
let b = Blog::find_by_fqn(&rockets, &blog).expect("post::update: blog error");
let b = Blog::find_by_fqn(&rockets, &blog)
.await
.expect("post::update: blog error");
let mut post =
Post::find_by_slug(&*conn, &slug, b.id).expect("post::update: find by slug error");
let user = rockets.user.clone().unwrap();
@ -301,14 +304,16 @@ pub fn update(
.expect("post::update: update error");
if post.published {
post.update_mentions(
&conn,
mentions
.into_iter()
.filter_map(|m| Mention::build_activity(&rockets, &m).ok())
.collect(),
)
.expect("post::update: mentions error");
// NOTE: here we unroll a filter_map(), so we can use .await painlessly
let mut filtered_mentions: Vec<link::Mention> = vec![];
for m in mentions.into_iter() {
match Mention::build_activity(&rockets, &m).await {
Ok(m) => filtered_mentions.push(m),
Err(_) => {}
}
}
post.update_mentions(&conn, filtered_mentions)
.expect("post::update: mentions error");
}
let tags = form
@ -321,7 +326,7 @@ pub fn update(
.filter_map(|t| Tag::build_activity(t).ok())
.collect::<Vec<_>>();
post.update_tags(&conn, tags)
.expect("post::update: tags error");
.expect(r#"post::update: tags error"#);
let hashtags = hashtags
.into_iter()
@ -399,14 +404,16 @@ pub fn valid_slug(title: &str) -> Result<(), ValidationError> {
}
#[post("/~/<blog_name>/new", data = "<form>")]
pub fn create(
pub async fn create(
blog_name: String,
form: LenientForm<NewPostForm>,
cl: ContentLen,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &blog_name).expect("post::create: blog error");
let blog = Blog::find_by_fqn(&rockets, &blog_name)
.await
.expect("post::create: blog error");
let slug = form.title.to_string().to_kebab_case();
let user = rockets.user.clone().unwrap();
@ -520,6 +527,7 @@ pub fn create(
Mention::from_activity(
&*conn,
&Mention::build_activity(&rockets, &m)
.await
.expect("post::create: mention build error"),
post.id,
true,
@ -562,7 +570,7 @@ pub fn create(
}
#[post("/~/<blog_name>/<slug>/delete")]
pub fn delete(
pub async fn delete(
blog_name: String,
slug: String,
rockets: PlumeRocket,
@ -570,6 +578,7 @@ pub fn delete(
) -> Result<Flash<Redirect>, ErrorPage> {
let user = rockets.user.clone().unwrap();
let post = Blog::find_by_fqn(&rockets, &blog_name)
.await
.and_then(|blog| Post::find_by_slug(&*rockets.conn, &slug, blog.id));
if let Ok(post) = post {
@ -615,12 +624,13 @@ pub fn delete(
}
#[get("/~/<blog_name>/<slug>/remote_interact")]
pub fn remote_interact(
pub async fn remote_interact(
rockets: PlumeRocket,
blog_name: String,
slug: String,
) -> Result<Ructe, ErrorPage> {
let target = Blog::find_by_fqn(&rockets, &blog_name)
.await
.and_then(|blog| Post::find_by_slug(&rockets.conn, &slug, blog.id))?;
Ok(render!(posts::remote_interact(
&rockets.to_context(),
@ -633,17 +643,19 @@ pub fn remote_interact(
}
#[post("/~/<blog_name>/<slug>/remote_interact", data = "<remote>")]
pub fn remote_interact_post(
pub async fn remote_interact_post(
rockets: PlumeRocket,
blog_name: String,
slug: String,
remote: LenientForm<RemoteForm>,
) -> Result<RespondOrRedirect, ErrorPage> {
let target = Blog::find_by_fqn(&rockets, &blog_name)
.await
.and_then(|blog| Post::find_by_slug(&rockets.conn, &slug, blog.id))?;
if let Some(uri) = User::fetch_remote_interact_uri(&remote.remote)
.ok()
.and_then(|uri| rt_format!(uri, uri = target.ap_url).ok())
if let uri = User::fetch_remote_interact_uri(&remote.remote)
.await
.map(|uri| uri.replace("{uri}", &format!("{}", target.ap_url)))
.unwrap()
{
Ok(Redirect::to(uri).into())
} else {

@ -10,14 +10,14 @@ use plume_models::{
};
#[post("/~/<blog>/<slug>/reshare")]
pub fn create(
pub async fn create(
blog: String,
slug: String,
user: User,
rockets: PlumeRocket,
) -> Result<Redirect, ErrorPage> {
let conn = &*rockets.conn;
let b = Blog::find_by_fqn(&rockets, &blog)?;
let b = Blog::find_by_fqn(&rockets, &blog).await?;
let post = Post::find_by_slug(&*conn, &slug, b.id)?;
if !user.has_reshared(&*conn, &post)? {

@ -35,21 +35,23 @@ pub fn new(m: Option<String>, rockets: PlumeRocket) -> Ructe {
#[derive(Default, FromForm, Validate)]
pub struct LoginForm {
#[validate(length(min = "1", message = "We need an email, or a username to identify you"))]
#[validate(length(min = 1, message = "We need an email, or a username to identify you"))]
pub email_or_name: String,
#[validate(length(min = "1", message = "Your password can't be empty"))]
#[validate(length(min = 1, message = "Your password can't be empty"))]
pub password: String,
}
#[post("/login", data = "<form>")]
pub fn create(
pub async fn create(
form: LenientForm<LoginForm>,
mut cookies: Cookies<'_>,
rockets: PlumeRocket,
) -> RespondOrRedirect {
let conn = &*rockets.conn;
let user = User::find_by_email(&*conn, &form.email_or_name)
.or_else(|_| User::find_by_fqn(&rockets, &form.email_or_name));
let user = match User::find_by_email(&*conn, &form.email_or_name) {
Ok(user) => Ok(user),
Err(_) => User::find_by_fqn(&rockets, &form.email_or_name).await,
};
let mut errors = match form.validate() {
Ok(_) => ValidationErrors::new(),
Err(e) => e,
@ -197,7 +199,7 @@ pub fn password_reset_form(token: String, rockets: PlumeRocket) -> Result<Ructe,
#[derive(FromForm, Default, Validate)]
#[validate(schema(
function = "passwords_match",
skip_on_field_errors = "false",
skip_on_field_errors = false,
message = "Passwords are not matching"
))]
pub struct NewPasswordForm {

@ -9,7 +9,6 @@ use rocket::{
response::{status, Content, Flash, Redirect},
};
use rocket_i18n::I18n;
use serde_json;
use std::{borrow::Cow, collections::HashMap};
use validator::{Validate, ValidationError, ValidationErrors};
@ -42,7 +41,7 @@ pub fn me(user: Option<User>) -> RespondOrRedirect {
}
#[get("/@/<name>", rank = 2)]
pub fn details(
pub async fn details(
name: String,
rockets: PlumeRocket,
fetch_rockets: PlumeRocket,
@ -50,7 +49,7 @@ pub fn details(
update_conn: DbConn,
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let user = User::find_by_fqn(&rockets, &name)?;
let user = User::find_by_fqn(&rockets, &name).await?;
let recents = Post::get_recents_for_author(&*conn, &user, 6)?;
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6)?;
let worker = &rockets.worker;
@ -61,6 +60,7 @@ pub fn details(
worker.execute(move || {
for create_act in user_clone
.fetch_outbox::<Create>()
.await
.expect("Remote user: outbox couldn't be fetched")
{
match create_act.create_props.object_object::<LicensedArticle>() {
@ -79,6 +79,7 @@ pub fn details(
worker.execute(move || {
for user_id in user_clone
.fetch_followers_ids()
.await
.expect("Remote user: fetching followers error")
{
let follower = User::from_id(&fetch_followers_rockets, &user_id, None)
@ -101,6 +102,7 @@ pub fn details(
worker.execute(move || {
user_clone
.refetch(&*update_conn)
.await
.expect("Couldn't update user info");
});
}
@ -146,13 +148,13 @@ pub fn dashboard_auth(i18n: I18n) -> Flash<Redirect> {
}
#[post("/@/<name>/follow")]
pub fn follow(
pub async fn follow(
name: String,
user: User,
rockets: PlumeRocket,
) -> Result<Flash<Redirect>, ErrorPage> {
let conn = &*rockets.conn;
let target = User::find_by_fqn(&rockets, &name)?;
let target = User::find_by_fqn(&rockets, &name).await?;
let message = if let Ok(follow) = follows::Follow::find(&*conn, user.id, target.id) {
let delete_act = follow.build_undo(&*conn)?;
local_inbox(
@ -190,26 +192,26 @@ pub fn follow(
}
#[post("/@/<name>/follow", data = "<remote_form>", rank = 2)]
pub fn follow_not_connected(
pub async fn follow_not_connected(
rockets: PlumeRocket,
name: String,
remote_form: Option<LenientForm<RemoteForm>>,
i18n: I18n,
) -> Result<RespondOrRedirect, ErrorPage> {
let target = User::find_by_fqn(&rockets, &name)?;
let target = User::find_by_fqn(&rockets, &name).await?;
if let Some(remote_form) = remote_form {
if let Some(uri) = User::fetch_remote_interact_uri(&remote_form)
.await
.ok()
.and_then(|uri| {
rt_format!(
uri,
uri = format!(
Some(uri.replace(
"{uri}",
&format!(
"{}@{}",
target.fqn,
target.get_instance(&rockets.conn).ok()?.public_domain
)
)
.ok()
),
))
})
{
Ok(Redirect::to(uri).into())
@ -266,15 +268,18 @@ pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
}
#[get("/@/<name>/followers?<page>", rank = 2)]
pub fn followers(
pub async fn followers(
name: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let page = page.unwrap_or_default();
let user = User::find_by_fqn(&rockets, &name)?;
let followers_count = user.count_followers(&*conn)?;
let user: User = User::find_by_fqn(&rockets, &name).await?;
let followers_count = match user.count_followers(&conn) {
Ok(num) => num,
Err(_) => 0,
};
Ok(render!(users::followers(
&rockets.to_context(),
@ -293,14 +298,14 @@ pub fn followers(
}
#[get("/@/<name>/followed?<page>", rank = 2)]
pub fn followed(
pub async fn followed(
name: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let page = page.unwrap_or_default();
let user = User::find_by_fqn(&rockets, &name)?;
let user = User::find_by_fqn(&rockets, &name).await?;
let followed_count = user.count_followed(conn)?;
Ok(render!(users::followed(
@ -320,12 +325,12 @@ pub fn followed(
}
#[get("/@/<name>", rank = 1)]
pub fn activity_details(
pub async fn activity_details(
name: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<CustomPerson>> {
let user = User::find_by_fqn(&rockets, &name).ok()?;
let user: User = User::find_by_fqn(&rockets, &name).await?;
Some(ActivityStream::new(user.to_activity(&*rockets.conn).ok()?))
}
@ -412,50 +417,49 @@ pub fn update(
}
#[post("/@/<name>/delete")]
pub fn delete(
pub async fn delete(
name: String,
user: User,
mut cookies: Cookies<'_>,
rockets: PlumeRocket,
) -> Result<Flash<Redirect>, ErrorPage> {
let account = User::find_by_fqn(&rockets, &name)?;
if user.id == account.id {
account.delete(&*rockets.conn, &rockets.searcher)?;
let target = User::one_by_instance(&*rockets.conn)?;
let delete_act = account.delete_activity(&*rockets.conn)?;
rockets
.worker
.execute(move || broadcast(&account, delete_act, target));
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
cookies.remove_private(cookie);
}
Ok(Flash::success(
Redirect::to(uri!(super::instance::index)),
i18n!(rockets.intl.catalog, "Your account has been deleted."),
))
} else {
Ok(Flash::error(
let account = User::find_by_fqn(&rockets, &name).await?;
if user.id != account.id {
return Ok(Flash::error(
Redirect::to(uri!(edit: name = name)),
i18n!(
rockets.intl.catalog,
"You can't delete someone else's account."
),
))
));
}
account.delete(&*rockets.conn, &rockets.searcher)?;
let target = User::one_by_instance(&*rockets.conn)?;
let delete_act = account.delete_activity(&*rockets.conn)?;
rockets
.worker
.execute(move || broadcast(&account, delete_act, target));
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
cookies.remove_private(cookie);
}
Ok(Flash::success(
Redirect::to(uri!(super::instance::index)),
i18n!(rockets.intl.catalog, "Your account has been deleted."),
))
}
#[derive(Default, FromForm, Validate)]
#[validate(schema(
function = "passwords_match",
skip_on_field_errors = "false",
skip_on_field_errors = false,
message = "Passwords are not matching"
))]
pub struct NewUserForm {
#[validate(
length(min = "1", message = "Username can't be empty"),
length(min = 1, message = "Username can't be empty"),
custom(
function = "validate_username",
message = "User name is not allowed to contain any of < > & @ ' or \""
@ -464,9 +468,9 @@ pub struct NewUserForm {
pub username: String,
#[validate(email(message = "Invalid email"))]
pub email: String,
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
#[validate(length(min = 8, message = "Password should be at least 8 characters long"))]
pub password: String,
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
#[validate(length(min = 8, message = "Password should be at least 8 characters long"))]
pub password_confirmation: String,
}
@ -565,37 +569,44 @@ pub fn create(
}
#[get("/@/<name>/outbox")]
pub fn outbox(name: String, rockets: PlumeRocket) -> Option<ActivityStream<OrderedCollection>> {
let user = User::find_by_fqn(&rockets, &name).ok()?;
pub async fn outbox(
name: String,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollection>> {
let user = User::find_by_fqn(&rockets, &name).await.ok()?;
user.outbox(&*rockets.conn).ok()
}
#[get("/@/<name>/outbox?<page>")]
pub fn outbox_page(
pub async fn outbox_page(
name: String,
page: Page,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollectionPage>> {
let user = User::find_by_fqn(&rockets, &name).ok()?;
let user = User::find_by_fqn(&rockets, &name).await.ok()?;
user.outbox_page(&*rockets.conn, page.limits()).ok()
}
#[post("/@/<name>/inbox", data = "<data>")]
pub fn inbox(
pub async fn inbox(
name: String,
data: inbox::SignedJson<serde_json::Value>,
headers: Headers<'_>,
rockets: PlumeRocket,
) -> Result<String, status::BadRequest<&'static str>> {
User::find_by_fqn(&rockets, &name).map_err(|_| status::BadRequest(Some("User not found")))?;
inbox::handle_incoming(rockets, data, headers)
User::find_by_fqn(&rockets, &name)
.await
.map_err(|_| status::BadRequest(Some("User not found")))?;
inbox::handle_incoming(rockets, data, headers).await
}
#[get("/@/<name>/followers", rank = 1)]
pub fn ap_followers(
pub async fn ap_followers(
name: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<OrderedCollection>> {
let user = User::find_by_fqn(&rockets, &name).ok()?;
let user = User::find_by_fqn(&rockets, &name).await?;
let followers = user
.get_followers(&*rockets.conn)
.ok()?
@ -615,9 +626,9 @@ pub fn ap_followers(
}
#[get("/@/<name>/atom.xml")]
pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
pub async fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
let conn = &*rockets.conn;
let author = User::find_by_fqn(&rockets, &name).ok()?;
let author = User::find_by_fqn(&rockets, &name).await?;
let entries = Post::get_recents_for_author(conn, &author, 15).ok()?;
let uri = Instance::get_local()
.ok()?

@ -1,6 +1,5 @@
use rocket::http::ContentType;
use rocket::response::Content;
use serde_json;
use webfinger::*;
use plume_models::{ap_url, blogs::Blog, users::User, PlumeRocket, CONFIG};
@ -43,27 +42,58 @@ pub fn host_meta() -> String {
struct WebfingerResolver;
impl Resolver<PlumeRocket> for WebfingerResolver {
fn instance_domain<'a>() -> &'a str {
#[async_trait::async_trait]
impl AsyncResolver for WebfingerResolver {
type Repo = PlumeRocket;
async fn instance_domain<'a>(&self) -> &'a str {
CONFIG.base_url.as_str()
}
fn find(prefix: Prefix, acct: String, ctx: PlumeRocket) -> Result<Webfinger, ResolverError> {
async fn find(
&self,
prefix: Prefix,
acct: String,
ctx: PlumeRocket,
) -> Result<Webfinger, ResolverError> {
match prefix {
Prefix::Acct => User::find_by_fqn(&ctx, &acct)
.await
.and_then(|usr| usr.webfinger(&*ctx.conn))
.or(Err(ResolverError::NotFound)),
Prefix::Group => Blog::find_by_fqn(&ctx, &acct)
.await
.and_then(|blog| blog.webfinger(&*ctx.conn))
.or(Err(ResolverError::NotFound)),
Prefix::Custom(_) => Err(ResolverError::NotFound),
}
}
async fn endpoint<R: Into<String> + Send>(
&self,
resource: R,
resource_repo: PlumeRocket,
) -> Result<Webfinger, ResolverError> {
let resource = resource.into();
let mut parsed_query = resource.splitn(2, ':');
let res_prefix = Prefix::from(parsed_query.next().ok_or(ResolverError::InvalidResource)?);
let res = parsed_query.next().ok_or(ResolverError::InvalidResource)?;
let mut parsed_res = res.splitn(2, '@');
let user = parsed_res.next().ok_or(ResolverError::InvalidResource)?;
let domain = parsed_res.next().ok_or(ResolverError::InvalidResource)?;
if domain == self.instance_domain().await {
self.find(res_prefix, user.to_string(), resource_repo).await
} else {
Err(ResolverError::WrongDomain)
}
}
}
#[get("/.well-known/webfinger?<resource>")]
pub fn webfinger(resource: String, rockets: PlumeRocket) -> Content<String> {
match WebfingerResolver::endpoint(resource, rockets)
pub async fn webfinger(resource: String, rockets: PlumeRocket) -> Content<String> {
let wf_resolver = WebfingerResolver;
match wf_resolver
.endpoint(resource, rockets)
.await
.and_then(|wf| serde_json::to_string(&wf).map_err(|_| ResolverError::NotFound))
{
Ok(wf) => Content(ContentType::new("application", "jrd+json"), wf),

@ -1,8 +1,7 @@
use plume_models::{notifications::*, users::User, Connection, PlumeRocket};
use crate::templates::Html;
use rocket::http::hyper::header::{ETag, EntityTag};
use rocket::http::{Method, Status};
use rocket::http::{Header, Method, Status};
use rocket::request::Request;
use rocket::response::{self, content::Html as HtmlCt, Responder, Response};
use rocket_i18n::Catalog;
@ -52,11 +51,12 @@ impl IntoContext for PlumeRocket {
#[derive(Debug)]
pub struct Ructe(pub Vec<u8>);
#[rocket::async_trait]
impl<'r> Responder<'r> for Ructe {
fn respond_to(self, r: &Request<'_>) -> response::Result<'r> {
async fn respond_to(self, r: &'r Request<'_>) -> response::Result<'r> {
//if method is not Get or page contain a form, no caching
if r.method() != Method::Get || self.0.windows(6).any(|w| w == b"<form ") {
return HtmlCt(self.0).respond_to(r);
return HtmlCt(self.0).respond_to(r).await;
}
let mut hasher = DefaultHasher::new();
hasher.write(&self.0);
@ -67,12 +67,12 @@ impl<'r> Responder<'r> for Ructe {
{
Response::build()
.status(Status::NotModified)
.header(ETag(EntityTag::strong(etag)))
.header(Header::new("ETag", etag))
.ok()
} else {
Response::build()
.merge(HtmlCt(self.0).respond_to(r)?)
.header(ETag(EntityTag::strong(etag)))
.merge(HtmlCt(self.0).respond_to(r).await.ok().unwrap())
.header(Header::new("ETag", etag))
.ok()
}
}

@ -1,8 +1,12 @@
@use plume_models::CONFIG;
@use plume_models::instance::Instance;
@use rocket::http::RawStr;
@use rocket::request::Form;
@use std::path::Path;
@use std::path::PathBuf;
@use crate::template_utils::*;
@use crate::routes::*;
@use crate::routes::search::SearchQuery;
@(ctx: BaseContext, title: String, head: Content, header: Content, content: Content)

@ -2,7 +2,9 @@
@use plume_models::instance::Instance;
@use plume_models::posts::Post;
@use plume_models::users::User;
@use rocket::http::RawStr;
@use std::path::Path;
@use std::path::PathBuf;
@use crate::templates::{base, partials::post_card};
@use crate::template_utils::*;
@use crate::routes::*;

@ -5,10 +5,12 @@
@use crate::template_utils::*;
@use crate::templates::base;
@use crate::templates::partials::image_select;
@use crate::routes::Page;
@use crate::routes::blogs;
@use crate::routes::blogs::EditForm;
@use crate::routes::medias;
@(ctx: BaseContext, blog: &Blog, medias: Vec<Media>, form: &EditForm, errors: ValidationErrors)
@:base(ctx, i18n!(ctx.1, "Edit \"{}\""; &blog.title), {}, {

@ -3,7 +3,9 @@
@use plume_models::posts::Post;
@use plume_models::tags::Tag;
@use plume_models::users::User;
@use rocket::http::RawStr;
@use std::path::Path;
@use std::path::PathBuf;
@use validator::ValidationErrors;
@use crate::templates::{base, partials::comment};
@use crate::template_utils::*;

Loading…
Cancel
Save