Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [PoC] '&' -> '&mut' #777

Open
wants to merge 2 commits into
base: go/async
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions plume-common/src/activity_pub/inbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)> {
Expand Down Expand Up @@ -308,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.
Expand Down
10 changes: 5 additions & 5 deletions plume-models/src/blogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Blog {
.map_err(Error::from)
}

pub async 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)
Expand All @@ -144,7 +144,7 @@ impl Blog {
}
}

async fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> {
async fn fetch_from_webfinger(c: &mut PlumeRocket, acct: &str) -> Result<Blog> {
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)
.await?
.links
Expand Down Expand Up @@ -340,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(|_| {
Expand Down Expand Up @@ -436,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()
}
Expand Down
33 changes: 18 additions & 15 deletions plume-models/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use activitypub::{
};
use chrono::{self, NaiveDateTime};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
use futures::stream::{self, StreamExt};
use plume_common::{
activity_pub::{
inbox::{AsActor, AsObject, FromId},
Expand Down Expand Up @@ -105,7 +104,7 @@ impl Comment {
.unwrap_or(false)
}

pub async 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(),
Expand All @@ -131,16 +130,20 @@ 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(
stream::iter(mentions)
.filter_map(|m| async move { Mention::build_activity(c, &m).await.ok() })
.collect::<Vec<link::Mention>>()
.await,
)?;

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 async 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).await?;
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions plume-models/src/follows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)?;

Expand All @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions plume-models/src/inbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,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>()
Expand All @@ -72,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>,
Expand Down
12 changes: 6 additions & 6 deletions plume-models/src/likes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)?;
Expand Down
33 changes: 17 additions & 16 deletions plume-models/src/medias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ impl Media {
}

// TODO: merge with save_remote?
pub async 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('.')
Expand All @@ -215,28 +214,30 @@ impl Media {
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()?,
is_remote: false,
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
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion plume-models/src/mentions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Mention {
}
}

pub async fn build_activity(c: &PlumeRocket, ment: &str) -> Result<link::Mention> {
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)?;
Expand Down
Loading