Fix follow IDs (#455)

* Generate valid IDs for Follow

Fixes #449

* Use the new post-insert hook for all the models

* Fix plume-cli build
This commit is contained in:
Baptiste Gelez
2019-03-04 21:35:03 +01:00
committed by GitHub
parent 9b48b8a846
commit 2a188abfa1
12 changed files with 162 additions and 181 deletions
+12 -24
View File
@@ -6,7 +6,7 @@ use activitypub::{
};
use canapi::{Error as ApiError, Provider};
use chrono::{NaiveDateTime, TimeZone, Utc};
use diesel::{self, BelongingToDsl, ExpressionMethods, QueryDsl, RunQueryDsl};
use diesel::{self, BelongingToDsl, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
use heck::{CamelCase, KebabCase};
use scheduled_thread_pool::ScheduledThreadPool as Worker;
use serde_json;
@@ -198,7 +198,6 @@ impl<'a> Provider<(&'a Connection, &'a Worker, &'a Searcher, Option<i32>)> for P
source: query.source.expect("Post API::create: no source error"),
cover_id: query.cover_id,
}, search).map_err(|_| ApiError::NotFound("Creation error".into()))?;
post.update_ap_url(conn).map_err(|_| ApiError::NotFound("Error setting ActivityPub URLs".into()))?;;
PostAuthor::insert(conn, NewPostAuthor {
author_id: author.id,
@@ -265,7 +264,17 @@ impl Post {
diesel::insert_into(posts::table)
.values(new)
.execute(conn)?;
let post = Self::last(conn)?;
let mut post = Self::last(conn)?;
if post.ap_url.is_empty() {
post.ap_url = ap_url(&format!(
"{}/~/{}/{}/",
*BASE_URL,
post.get_blog(conn)?.get_fqn(conn),
post.slug
));
let _: Post = post.save_changes(conn)?;
}
searcher.add_document(conn, &post)?;
Ok(post)
}
@@ -278,7 +287,6 @@ impl Post {
Ok(post)
}
pub fn list_by_tag(conn: &Connection, tag: String, (min, max): (i32, i32)) -> Result<Vec<Post>> {
use schema::tags;
@@ -505,17 +513,6 @@ impl Post {
.map_err(Error::from)
}
pub fn update_ap_url(&self, conn: &Connection) -> Result<Post> {
if self.ap_url.is_empty() {
diesel::update(self)
.set(posts::ap_url.eq(self.compute_id(conn)?))
.execute(conn)?;
Post::get(conn, self.id)
} else {
Ok(self.clone())
}
}
pub fn get_receivers_urls(&self, conn: &Connection) -> Result<Vec<String>> {
let followers = self
.get_authors(conn)?
@@ -856,15 +853,6 @@ impl Post {
Ok(format!("/~/{}/{}", blog.get_fqn(conn), self.slug))
}
pub fn compute_id(&self, conn: &Connection) -> Result<String> {
Ok(ap_url(&format!(
"{}/~/{}/{}/",
BASE_URL.as_str(),
self.get_blog(conn)?.get_fqn(conn),
self.slug
)))
}
pub fn cover_url(&self, conn: &Connection) -> Option<String> {
self.cover_id.and_then(|i| Media::get(conn, i).ok()).and_then(|c| c.url(conn).ok())
}