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
This commit is contained in:
parent
b596e77f03
commit
e322d9509a
@ -86,7 +86,7 @@ where
|
|||||||
/// - the context to be passed to each handler.
|
/// - the context to be passed to each handler.
|
||||||
/// - the activity
|
/// - the activity
|
||||||
/// - the reason it has not been handled yet
|
/// - 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
|
/// A matching handler have been found but failed
|
||||||
///
|
///
|
||||||
@ -139,16 +139,16 @@ where
|
|||||||
///
|
///
|
||||||
/// - `ctx`: the context to pass to each handler
|
/// - `ctx`: the context to pass to each handler
|
||||||
/// - `json`: the JSON representation of the incoming activity
|
/// - `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)
|
Inbox::NotHandled(ctx, json, InboxError::NoMatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers an handler on this Inbox.
|
/// Registers an handler on this Inbox.
|
||||||
pub fn with<A, V, M>(self) -> Inbox<'a, C, E, R>
|
pub fn with<A, V, M>(self) -> Inbox<'a, C, E, R>
|
||||||
where
|
where
|
||||||
A: AsActor<&'a C> + FromId<C, Error = E>,
|
A: AsActor<&'a mut C> + FromId<C, Error = E>,
|
||||||
V: activitypub::Activity,
|
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>,
|
M::Output: Into<R>,
|
||||||
{
|
{
|
||||||
if let Inbox::NotHandled(ctx, mut act, e) = self {
|
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
|
/// - `object`: optional object that will be used if the object was not found in the database
|
||||||
/// If absent, the ID will be dereferenced.
|
/// If absent, the ID will be dereferenced.
|
||||||
fn from_id(
|
fn from_id(
|
||||||
ctx: &C,
|
ctx: &mut C,
|
||||||
id: &str,
|
id: &str,
|
||||||
object: Option<Self::Object>,
|
object: Option<Self::Object>,
|
||||||
) -> Result<Self, (Option<serde_json::Value>, Self::Error)> {
|
) -> Result<Self, (Option<serde_json::Value>, Self::Error)> {
|
||||||
@ -308,10 +308,10 @@ pub trait FromId<C>: Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builds a `Self` from its ActivityPub representation
|
/// 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)
|
/// 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.
|
/// Should be implemented by anything representing an ActivityPub actor.
|
||||||
|
@ -132,7 +132,7 @@ impl Blog {
|
|||||||
.map_err(Error::from)
|
.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
|
let from_db = blogs::table
|
||||||
.filter(blogs::fqn.eq(fqn))
|
.filter(blogs::fqn.eq(fqn))
|
||||||
.first(&*c.conn)
|
.first(&*c.conn)
|
||||||
@ -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)
|
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)
|
||||||
.await?
|
.await?
|
||||||
.links
|
.links
|
||||||
@ -340,11 +340,11 @@ impl FromId<PlumeRocket> for Blog {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = CustomGroup;
|
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)
|
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 url = Url::parse(&acct.object.object_props.id_string()?)?;
|
||||||
let inst = url.host_str()?;
|
let inst = url.host_str()?;
|
||||||
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
|
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
|
||||||
@ -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 {
|
fn get_inbox_url(&self) -> String {
|
||||||
self.inbox_url.clone()
|
self.inbox_url.clone()
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ impl Comment {
|
|||||||
.unwrap_or(false)
|
.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 author = User::get(&c.conn, self.author_id)?;
|
||||||
let (html, mentions, _hashtags) = utils::md_to_html(
|
let (html, mentions, _hashtags) = utils::md_to_html(
|
||||||
self.content.get().as_ref(),
|
self.content.get().as_ref(),
|
||||||
@ -140,7 +140,7 @@ impl Comment {
|
|||||||
Ok(note)
|
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 author = User::get(&c.conn, self.author_id)?;
|
||||||
|
|
||||||
let note = self.to_activity(c).await?;
|
let note = self.to_activity(c).await?;
|
||||||
@ -198,11 +198,11 @@ impl FromId<PlumeRocket> for Comment {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = Note;
|
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)
|
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 conn = &*c.conn;
|
||||||
let comm = {
|
let comm = {
|
||||||
let previous_url = note.object_props.in_reply_to.as_ref()?.as_str()?;
|
let previous_url = note.object_props.in_reply_to.as_ref()?.as_str()?;
|
||||||
@ -322,21 +322,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 Error = Error;
|
||||||
type Output = Self;
|
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
|
// The actual creation takes place in the FromId impl
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsObject<User, Delete, &PlumeRocket> for Comment {
|
impl AsObject<User, Delete, &mut PlumeRocket> for Comment {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Output = ();
|
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 {
|
if self.author_id != actor.id {
|
||||||
return Err(Error::Unauthorized);
|
return Err(Error::Unauthorized);
|
||||||
}
|
}
|
||||||
|
@ -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 Error = Error;
|
||||||
type Output = Follow;
|
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,
|
// Mastodon (at least) requires the full Follow object when accepting it,
|
||||||
// so we rebuilt it here
|
// so we rebuilt it here
|
||||||
let mut follow = FollowAct::default();
|
let mut follow = FollowAct::default();
|
||||||
@ -156,11 +156,11 @@ impl FromId<PlumeRocket> for Follow {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = FollowAct;
|
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)
|
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 =
|
let actor =
|
||||||
User::from_id(c, &follow.follow_props.actor_link::<Id>()?, None).map_err(|(_, e)| e)?;
|
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 Error = Error;
|
||||||
type Output = ();
|
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 conn = &*c.conn;
|
||||||
if self.follower_id == actor.id {
|
if self.follower_id == actor.id {
|
||||||
diesel::delete(&self).execute(conn)?;
|
diesel::delete(&self).execute(conn)?;
|
||||||
|
@ -45,7 +45,7 @@ impl_into_inbox_result! {
|
|||||||
Reshare => Reshared
|
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)
|
Inbox::handle(ctx, act)
|
||||||
.with::<User, Announce, Post>()
|
.with::<User, Announce, Post>()
|
||||||
.with::<User, Create, Comment>()
|
.with::<User, Create, Comment>()
|
||||||
@ -72,7 +72,7 @@ pub(crate) mod tests {
|
|||||||
use diesel::Connection;
|
use diesel::Connection;
|
||||||
|
|
||||||
pub fn fill_database(
|
pub fn fill_database(
|
||||||
rockets: &PlumeRocket,
|
rockets: &mut PlumeRocket,
|
||||||
) -> (
|
) -> (
|
||||||
Vec<crate::posts::Post>,
|
Vec<crate::posts::Post>,
|
||||||
Vec<crate::users::User>,
|
Vec<crate::users::User>,
|
||||||
|
@ -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 Error = Error;
|
||||||
type Output = Like;
|
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(
|
let res = Like::insert(
|
||||||
&c.conn,
|
&c.conn,
|
||||||
NewLike {
|
NewLike {
|
||||||
@ -107,11 +107,11 @@ impl FromId<PlumeRocket> for Like {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = activity::Like;
|
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)
|
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(
|
let res = Like::insert(
|
||||||
&c.conn,
|
&c.conn,
|
||||||
NewLike {
|
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 Error = Error;
|
||||||
type Output = ();
|
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 conn = &*c.conn;
|
||||||
if actor.id == self.user_id {
|
if actor.id == self.user_id {
|
||||||
diesel::delete(&self).execute(conn)?;
|
diesel::delete(&self).execute(conn)?;
|
||||||
|
@ -197,8 +197,7 @@ impl Media {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: merge with save_remote?
|
// TODO: merge with save_remote?
|
||||||
pub async fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
|
pub async fn from_activity(c: &mut PlumeRocket, image: &Image) -> Result<Media> {
|
||||||
let conn = &*c.conn;
|
|
||||||
let remote_url = image.object_props.url_string().ok()?;
|
let remote_url = image.object_props.url_string().ok()?;
|
||||||
let ext = remote_url
|
let ext = remote_url
|
||||||
.rsplit('.')
|
.rsplit('.')
|
||||||
@ -215,8 +214,22 @@ impl Media {
|
|||||||
let contents = reqwest::get(remote_url.as_str()).await?.bytes().await?;
|
let contents = reqwest::get(remote_url.as_str()).await?.bytes().await?;
|
||||||
dest.write_all(&contents).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(
|
Media::insert(
|
||||||
conn,
|
&mut c.conn,
|
||||||
NewMedia {
|
NewMedia {
|
||||||
file_path: path.to_str()?.to_string(),
|
file_path: path.to_str()?.to_string(),
|
||||||
alt_text: image.object_props.content_string().ok()?,
|
alt_text: image.object_props.content_string().ok()?,
|
||||||
@ -224,19 +237,7 @@ impl Media {
|
|||||||
remote_url: None,
|
remote_url: None,
|
||||||
sensitive: image.object_props.summary_string().is_ok(),
|
sensitive: image.object_props.summary_string().is_ok(),
|
||||||
content_warning: image.object_props.summary_string().ok(),
|
content_warning: image.object_props.summary_string().ok(),
|
||||||
owner_id: User::from_id(
|
owner_id
|
||||||
c,
|
|
||||||
image
|
|
||||||
.object_props
|
|
||||||
.attributed_to_link_vec::<Id>()
|
|
||||||
.ok()?
|
|
||||||
.into_iter()
|
|
||||||
.next()?
|
|
||||||
.as_ref(),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.map_err(|(_, e)| e)?
|
|
||||||
.id,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -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 user = User::find_by_fqn(c, ment).await?;
|
||||||
let mut mention = link::Mention::default();
|
let mut mention = link::Mention::default();
|
||||||
mention.link_props.set_href_string(user.ap_url)?;
|
mention.link_props.set_href_string(user.ap_url)?;
|
||||||
|
@ -554,12 +554,11 @@ impl FromId<PlumeRocket> for Post {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = LicensedArticle;
|
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)
|
Self::find_by_ap_url(&c.conn, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_activity(c: &PlumeRocket, article: LicensedArticle) -> Result<Self> {
|
fn from_activity(c: &mut PlumeRocket, article: LicensedArticle) -> Result<Self> {
|
||||||
let conn = &*c.conn;
|
|
||||||
let searcher = &c.searcher;
|
let searcher = &c.searcher;
|
||||||
let license = article.custom_props.license_string().unwrap_or_default();
|
let license = article.custom_props.license_string().unwrap_or_default();
|
||||||
let article = article.object;
|
let article = article.object;
|
||||||
@ -570,12 +569,12 @@ impl FromId<PlumeRocket> for Post {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.fold((None, vec![]), |(blog, mut authors), link| {
|
.fold((None, vec![]), |(blog, mut authors), link| {
|
||||||
let url = link;
|
let url = link;
|
||||||
match User::from_id(&c, &url, None) {
|
match User::from_id(&mut c, &url, None) {
|
||||||
Ok(u) => {
|
Ok(u) => {
|
||||||
authors.push(u);
|
authors.push(u);
|
||||||
(blog, authors)
|
(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),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -583,11 +582,11 @@ impl FromId<PlumeRocket> for Post {
|
|||||||
|
|
||||||
let mut r = Runtime::new().unwrap();
|
let mut r = Runtime::new().unwrap();
|
||||||
let cover =
|
let cover =
|
||||||
Some(r.block_on(async { Media::from_activity(&c, &image).await.ok().unwrap().id }));
|
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
|
||||||
|
|
||||||
let title = article.object_props.name_string()?;
|
let title = article.object_props.name_string()?;
|
||||||
let post = Post::insert(
|
let post = Post::insert(
|
||||||
conn,
|
&mut c.conn,
|
||||||
NewPost {
|
NewPost {
|
||||||
blog_id: blog?.id,
|
blog_id: blog?.id,
|
||||||
slug: title.to_kebab_case(),
|
slug: title.to_kebab_case(),
|
||||||
@ -610,7 +609,7 @@ impl FromId<PlumeRocket> for Post {
|
|||||||
|
|
||||||
for author in authors {
|
for author in authors {
|
||||||
PostAuthor::insert(
|
PostAuthor::insert(
|
||||||
conn,
|
&mut c.conn,
|
||||||
NewPostAuthor {
|
NewPostAuthor {
|
||||||
post_id: post.id,
|
post_id: post.id,
|
||||||
author_id: author.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 {
|
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag {
|
||||||
for tag in tags {
|
for tag in tags {
|
||||||
serde_json::from_value::<link::Mention>(tag.clone())
|
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();
|
.ok();
|
||||||
|
|
||||||
serde_json::from_value::<Hashtag>(tag.clone())
|
serde_json::from_value::<Hashtag>(tag.clone())
|
||||||
@ -635,7 +634,7 @@ impl FromId<PlumeRocket> for Post {
|
|||||||
.and_then(|t| {
|
.and_then(|t| {
|
||||||
let tag_name = t.name_string()?;
|
let tag_name = t.name_string()?;
|
||||||
Ok(Tag::from_activity(
|
Ok(Tag::from_activity(
|
||||||
conn,
|
&mut c.conn,
|
||||||
&t,
|
&t,
|
||||||
post.id,
|
post.id,
|
||||||
hashtags.remove(&tag_name),
|
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 Error = Error;
|
||||||
type Output = Post;
|
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?
|
// TODO: check that _actor is actually one of the author?
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsObject<User, Delete, &PlumeRocket> for Post {
|
impl AsObject<User, Delete, &mut PlumeRocket> for Post {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Output = ();
|
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
|
let can_delete = self
|
||||||
.get_authors(&c.conn)?
|
.get_authors(&c.conn)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -693,12 +692,12 @@ impl FromId<PlumeRocket> for PostUpdate {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = LicensedArticle;
|
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
|
// Always fail because we always want to deserialize the AP object
|
||||||
Err(Error::NotFound)
|
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
|
let image = updated
|
||||||
.object
|
.object
|
||||||
.object_props
|
.object_props
|
||||||
@ -707,7 +706,7 @@ impl FromId<PlumeRocket> for PostUpdate {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let mut r = Runtime::new().unwrap();
|
let mut r = Runtime::new().unwrap();
|
||||||
let cover =
|
let cover =
|
||||||
Some(r.block_on(async { Media::from_activity(&c, &image).await.ok().unwrap().id }));
|
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
|
||||||
|
|
||||||
Ok(PostUpdate {
|
Ok(PostUpdate {
|
||||||
ap_url: updated.object.object_props.id_string()?,
|
ap_url: updated.object.object_props.id_string()?,
|
||||||
@ -727,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 Error = Error;
|
||||||
type Output = ();
|
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 conn = &*c.conn;
|
||||||
let searcher = &c.searcher;
|
let searcher = &c.searcher;
|
||||||
let mut post = Post::from_id(c, &self.ap_url, None).map_err(|(_, e)| e)?;
|
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 Error = Error;
|
||||||
type Output = Reshare;
|
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 conn = &*c.conn;
|
||||||
let reshare = Reshare::insert(
|
let reshare = Reshare::insert(
|
||||||
conn,
|
conn,
|
||||||
@ -132,11 +132,11 @@ impl FromId<PlumeRocket> for Reshare {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = Announce;
|
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)
|
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(
|
let res = Reshare::insert(
|
||||||
&c.conn,
|
&c.conn,
|
||||||
NewReshare {
|
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 Error = Error;
|
||||||
type Output = ();
|
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 conn = &*c.conn;
|
||||||
if actor.id == self.user_id {
|
if actor.id == self.user_id {
|
||||||
diesel::delete(&self).execute(conn)?;
|
diesel::delete(&self).execute(conn)?;
|
||||||
|
@ -208,7 +208,7 @@ impl Timeline {
|
|||||||
.map_err(Error::from)
|
.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
|
let timelines = timeline_definition::table
|
||||||
.load::<Self>(rocket.conn.deref())
|
.load::<Self>(rocket.conn.deref())
|
||||||
.map_err(Error::from)?;
|
.map_err(Error::from)?;
|
||||||
@ -231,7 +231,7 @@ impl Timeline {
|
|||||||
Ok(())
|
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)?;
|
let query = TimelineQuery::parse(&self.query)?;
|
||||||
query.matches(rocket, self, post, kind)
|
query.matches(rocket, self, post, kind)
|
||||||
}
|
}
|
||||||
|
@ -162,7 +162,7 @@ enum TQ<'a> {
|
|||||||
impl<'a> TQ<'a> {
|
impl<'a> TQ<'a> {
|
||||||
fn matches(
|
fn matches(
|
||||||
&self,
|
&self,
|
||||||
rocket: &PlumeRocket,
|
rocket: &mut PlumeRocket,
|
||||||
timeline: &Timeline,
|
timeline: &Timeline,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
kind: Kind<'_>,
|
kind: Kind<'_>,
|
||||||
@ -207,7 +207,7 @@ enum Arg<'a> {
|
|||||||
impl<'a> Arg<'a> {
|
impl<'a> Arg<'a> {
|
||||||
pub fn matches(
|
pub fn matches(
|
||||||
&self,
|
&self,
|
||||||
rocket: &PlumeRocket,
|
rocket: &mut PlumeRocket,
|
||||||
timeline: &Timeline,
|
timeline: &Timeline,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
kind: Kind<'_>,
|
kind: Kind<'_>,
|
||||||
@ -232,7 +232,7 @@ enum WithList {
|
|||||||
impl WithList {
|
impl WithList {
|
||||||
pub fn matches(
|
pub fn matches(
|
||||||
&self,
|
&self,
|
||||||
rocket: &PlumeRocket,
|
rocket: &mut PlumeRocket,
|
||||||
timeline: &Timeline,
|
timeline: &Timeline,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
list: &List<'_>,
|
list: &List<'_>,
|
||||||
@ -391,7 +391,7 @@ enum Bool {
|
|||||||
impl Bool {
|
impl Bool {
|
||||||
pub fn matches(
|
pub fn matches(
|
||||||
&self,
|
&self,
|
||||||
rocket: &PlumeRocket,
|
rocket: &mut PlumeRocket,
|
||||||
timeline: &Timeline,
|
timeline: &Timeline,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
kind: Kind<'_>,
|
kind: Kind<'_>,
|
||||||
@ -662,7 +662,7 @@ impl<'a> TimelineQuery<'a> {
|
|||||||
|
|
||||||
pub fn matches(
|
pub fn matches(
|
||||||
&self,
|
&self,
|
||||||
rocket: &PlumeRocket,
|
rocket: &mut PlumeRocket,
|
||||||
timeline: &Timeline,
|
timeline: &Timeline,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
kind: Kind<'_>,
|
kind: Kind<'_>,
|
||||||
|
@ -189,7 +189,7 @@ impl User {
|
|||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async 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
|
let from_db = users::table
|
||||||
.filter(users::fqn.eq(fqn))
|
.filter(users::fqn.eq(fqn))
|
||||||
.first(&*c.conn)
|
.first(&*c.conn)
|
||||||
@ -201,7 +201,7 @@ impl User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<User> {
|
async fn fetch_from_webfinger(c: &mut PlumeRocket, acct: &str) -> Result<User> {
|
||||||
let link = resolve(acct.to_owned(), true)
|
let link = resolve(acct.to_owned(), true)
|
||||||
.await?
|
.await?
|
||||||
.links
|
.links
|
||||||
@ -245,7 +245,7 @@ impl User {
|
|||||||
Ok(json)
|
Ok(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
|
pub async fn fetch_from_url(c: &mut PlumeRocket, url: &str) -> Result<User> {
|
||||||
let json = User::fetch(url).await?;
|
let json = User::fetch(url).await?;
|
||||||
User::from_activity(c, json)
|
User::from_activity(c, json)
|
||||||
}
|
}
|
||||||
@ -821,11 +821,11 @@ impl FromId<PlumeRocket> for User {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Object = CustomPerson;
|
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)
|
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 url = Url::parse(&acct.object.object_props.id_string()?)?;
|
||||||
let inst = url.host_str()?;
|
let inst = url.host_str()?;
|
||||||
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
|
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
|
||||||
@ -917,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 {
|
fn get_inbox_url(&self) -> String {
|
||||||
self.inbox_url.clone()
|
self.inbox_url.clone()
|
||||||
}
|
}
|
||||||
@ -933,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 Error = Error;
|
||||||
type Output = ();
|
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 {
|
if self.id == actor.id {
|
||||||
self.delete(&c.conn, &c.searcher).map(|_| ())
|
self.delete(&c.conn, &c.searcher).map(|_| ())
|
||||||
} else {
|
} else {
|
||||||
|
@ -68,13 +68,12 @@ pub async fn oauth(
|
|||||||
query: Form<OAuthRequest>,
|
query: Form<OAuthRequest>,
|
||||||
rockets: PlumeRocket,
|
rockets: PlumeRocket,
|
||||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||||
let conn = &*rockets.conn;
|
let app = App::find_by_client_id(&rockets.conn, &query.client_id)?;
|
||||||
let app = App::find_by_client_id(conn, &query.client_id)?;
|
|
||||||
if app.client_secret == query.client_secret {
|
if app.client_secret == query.client_secret {
|
||||||
if let Ok(user) = User::find_by_fqn(&rockets, &query.username).await {
|
if let Ok(user) = User::find_by_fqn(&mut rockets, &query.username).await {
|
||||||
if user.auth(&query.password) {
|
if user.auth(&query.password) {
|
||||||
let token = ApiToken::insert(
|
let token = ApiToken::insert(
|
||||||
conn,
|
&rockets.conn,
|
||||||
NewApiToken {
|
NewApiToken {
|
||||||
app_id: app.id,
|
app_id: app.id,
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
@ -94,7 +93,7 @@ pub async fn oauth(
|
|||||||
// Making fake password verification to avoid different
|
// Making fake password verification to avoid different
|
||||||
// response times that would make it possible to know
|
// response times that would make it possible to know
|
||||||
// if a username is registered or not.
|
// 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!({
|
Ok(Json(json!({
|
||||||
"error": "Invalid credentials"
|
"error": "Invalid credentials"
|
||||||
})))
|
})))
|
||||||
|
Loading…
Reference in New Issue
Block a user