Allow retrieval of local Note objects using /objects/ URL

This commit is contained in:
silverpill 2021-10-08 18:15:04 +00:00
parent 7a65e34e62
commit 8c18a0ab86
4 changed files with 35 additions and 10 deletions

View file

@ -126,9 +126,9 @@ pub fn create_note(
Some(in_reply_to_id) => {
let post = in_reply_to.unwrap();
assert_eq!(post.id, in_reply_to_id);
match post.author.actor_json {
Some(_) => None, // TODO: store object ID for remote posts
None => Some(get_object_url(&config.instance_url(), &post.id)),
match post.author.is_local() {
false => None, // TODO: store object ID for remote posts
true => Some(get_object_url(&config.instance_url(), &post.id)),
}
},
None => None,

View file

@ -9,8 +9,9 @@ use crate::config::Config;
use crate::database::{Pool, get_database_client};
use crate::errors::HttpError;
use crate::http_signatures::verify::verify_http_signature;
use crate::models::posts::queries::get_thread;
use crate::models::users::queries::get_user_by_name;
use super::activity::OrderedCollection;
use super::activity::{create_note, OrderedCollection};
use super::actor::get_actor_object;
use super::constants::ACTIVITY_CONTENT_TYPE;
use super::receiver::receive_activity;
@ -122,9 +123,25 @@ pub fn activitypub_scope() -> Scope {
#[get("/objects/{object_id}")]
pub async fn get_object(
web::Path(_object_id): web::Path<String>,
config: web::Data<Config>,
db_pool: web::Data<Pool>,
web::Path(object_id): web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
// WARNING: activities/objects are not stored
let response = HttpResponse::Gone().body("");
let db_client = &**get_database_client(&db_pool).await?;
// Try to find local post by ID, return 404 if not found
let thread = get_thread(db_client, &object_id).await?;
let post = thread.iter()
.find(|post| post.id == object_id && post.author.is_local())
.ok_or(HttpError::NotFoundError("post"))?;
let in_reply_to = match post.in_reply_to_id {
Some(in_reply_to_id) => {
thread.iter().find(|post| post.id == in_reply_to_id)
},
None => None,
};
let object = create_note(&config, post, in_reply_to);
let response = HttpResponse::Ok()
.content_type(ACTIVITY_CONTENT_TYPE)
.json(object);
Ok(response)
}

View file

@ -48,9 +48,11 @@ pub struct Account {
impl Account {
pub fn from_profile(profile: DbActorProfile, instance_url: &str) -> Self {
let avatar_url = profile.avatar_file_name.map(|name| get_file_url(instance_url, &name));
let header_url = profile.banner_file_name.map(|name| get_file_url(instance_url, &name));
let source = if profile.actor_json.is_some() {
let avatar_url = profile.avatar_file_name.as_ref()
.map(|name| get_file_url(instance_url, &name));
let header_url = profile.banner_file_name.as_ref()
.map(|name| get_file_url(instance_url, &name));
let source = if !profile.is_local() {
// Remote actor
None
} else {

View file

@ -68,6 +68,12 @@ pub struct DbActorProfile {
pub actor_json: Option<Value>,
}
impl DbActorProfile {
pub fn is_local(&self) -> bool {
self.actor_json.is_none()
}
}
pub struct ProfileCreateData {
pub username: String,
pub display_name: Option<String>,