2021-10-17 16:55:33 +00:00
|
|
|
/// https://docs.joinmastodon.org/methods/statuses/
|
2021-04-09 00:22:17 +00:00
|
|
|
use actix_web::{get, post, web, HttpResponse, Scope};
|
2021-10-05 18:10:14 +00:00
|
|
|
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
2021-04-09 00:22:17 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-10-29 19:21:26 +00:00
|
|
|
use crate::activitypub::activity::{
|
|
|
|
create_activity_note,
|
|
|
|
create_activity_like,
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::activitypub::actor::Actor;
|
|
|
|
use crate::activitypub::deliverer::deliver_activity;
|
2021-11-04 23:49:53 +00:00
|
|
|
use crate::activitypub::views::get_object_url;
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::config::Config;
|
|
|
|
use crate::database::{Pool, get_database_client};
|
2021-10-17 16:55:33 +00:00
|
|
|
use crate::errors::{DatabaseError, HttpError};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::ethereum::nft::create_mint_signature;
|
|
|
|
use crate::ipfs::store as ipfs_store;
|
|
|
|
use crate::ipfs::utils::{IPFS_LOGO, get_ipfs_url};
|
2021-10-05 18:10:14 +00:00
|
|
|
use crate::mastodon_api::oauth::auth::get_current_user;
|
2021-09-28 21:44:31 +00:00
|
|
|
use crate::models::attachments::queries::set_attachment_ipfs_cid;
|
2021-11-20 01:10:56 +00:00
|
|
|
use crate::models::posts::helpers::can_view_post;
|
2021-11-10 17:00:39 +00:00
|
|
|
use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::models::profiles::queries::get_followers;
|
2021-10-18 22:35:52 +00:00
|
|
|
use crate::models::posts::helpers::{
|
|
|
|
get_actions_for_posts,
|
|
|
|
};
|
2021-09-22 16:32:44 +00:00
|
|
|
use crate::models::posts::queries::{
|
|
|
|
create_post,
|
|
|
|
get_post_by_id,
|
|
|
|
get_thread,
|
|
|
|
update_post,
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::models::posts::types::PostCreateData;
|
2021-10-18 23:42:56 +00:00
|
|
|
use crate::models::reactions::queries::{
|
|
|
|
create_reaction,
|
|
|
|
delete_reaction,
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
use super::types::{Status, StatusData};
|
|
|
|
|
|
|
|
#[post("")]
|
|
|
|
async fn create_status(
|
2021-10-05 18:10:14 +00:00
|
|
|
auth: BearerAuth,
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
data: web::Json<StatusData>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &mut **get_database_client(&db_pool).await?;
|
2021-10-05 18:10:14 +00:00
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-11-10 17:00:39 +00:00
|
|
|
let instance = config.instance();
|
2021-04-09 00:22:17 +00:00
|
|
|
let mut post_data = PostCreateData::from(data.into_inner());
|
|
|
|
post_data.validate()?;
|
2021-11-10 17:00:39 +00:00
|
|
|
// Mentions
|
|
|
|
let mention_map = find_mentioned_profiles(
|
|
|
|
db_client,
|
|
|
|
&instance.host(),
|
|
|
|
&post_data.content,
|
|
|
|
).await?;
|
|
|
|
post_data.content = replace_mentions(
|
|
|
|
&mention_map,
|
|
|
|
&instance.host(),
|
|
|
|
&instance.url(),
|
|
|
|
&post_data.content,
|
|
|
|
);
|
2021-11-11 19:10:28 +00:00
|
|
|
post_data.mentions = mention_map.values()
|
|
|
|
.map(|profile| profile.id).collect();
|
2021-04-09 00:22:17 +00:00
|
|
|
let post = create_post(db_client, ¤t_user.id, post_data).await?;
|
|
|
|
// Federate
|
2021-11-01 00:23:02 +00:00
|
|
|
let maybe_in_reply_to = match post.in_reply_to_id {
|
2021-10-08 13:56:45 +00:00
|
|
|
Some(in_reply_to_id) => {
|
|
|
|
let in_reply_to = get_post_by_id(db_client, &in_reply_to_id).await?;
|
|
|
|
Some(in_reply_to)
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
2021-10-09 12:53:53 +00:00
|
|
|
let activity = create_activity_note(
|
2021-11-11 21:51:47 +00:00
|
|
|
&instance.host(),
|
2021-11-10 17:00:39 +00:00
|
|
|
&instance.url(),
|
2021-10-09 12:53:53 +00:00
|
|
|
&post,
|
2021-11-01 00:23:02 +00:00
|
|
|
maybe_in_reply_to.as_ref(),
|
2021-10-09 12:53:53 +00:00
|
|
|
);
|
2021-04-09 00:22:17 +00:00
|
|
|
let followers = get_followers(db_client, ¤t_user.id).await?;
|
|
|
|
let mut recipients: Vec<Actor> = Vec::new();
|
|
|
|
for follower in followers {
|
2021-11-12 23:41:03 +00:00
|
|
|
let maybe_remote_actor = follower.remote_actor()
|
2021-10-31 20:46:29 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
|
|
|
if let Some(remote_actor) = maybe_remote_actor {
|
|
|
|
recipients.push(remote_actor);
|
2021-04-09 00:22:17 +00:00
|
|
|
};
|
|
|
|
};
|
2021-11-01 00:23:02 +00:00
|
|
|
if let Some(in_reply_to) = maybe_in_reply_to {
|
2021-11-12 23:41:03 +00:00
|
|
|
let maybe_remote_actor = in_reply_to.author.remote_actor()
|
2021-11-01 00:23:02 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
|
|
|
if let Some(remote_actor) = maybe_remote_actor {
|
|
|
|
recipients.push(remote_actor);
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 21:51:47 +00:00
|
|
|
for profile in post.mentions.iter() {
|
2021-11-12 23:41:03 +00:00
|
|
|
let maybe_remote_actor = profile.remote_actor()
|
2021-11-11 21:51:47 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
|
|
|
if let Some(remote_actor) = maybe_remote_actor {
|
|
|
|
recipients.push(remote_actor);
|
|
|
|
};
|
|
|
|
};
|
2021-10-30 22:35:18 +00:00
|
|
|
deliver_activity(&config, ¤t_user, activity, recipients);
|
2021-11-10 17:00:39 +00:00
|
|
|
let status = Status::from_post(post, &instance.url());
|
2021-04-09 00:22:17 +00:00
|
|
|
Ok(HttpResponse::Created().json(status))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/{status_id}")]
|
|
|
|
async fn get_status(
|
2021-10-18 22:35:52 +00:00
|
|
|
auth: Option<BearerAuth>,
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
let maybe_current_user = match auth {
|
|
|
|
Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
let mut post = get_post_by_id(db_client, &status_id).await?;
|
2021-11-20 01:10:56 +00:00
|
|
|
if !can_view_post(maybe_current_user.as_ref(), &post) {
|
|
|
|
return Err(HttpError::NotFoundError("post"));
|
|
|
|
};
|
2021-10-18 22:35:52 +00:00
|
|
|
if let Some(user) = maybe_current_user {
|
2021-11-24 00:33:22 +00:00
|
|
|
get_actions_for_posts(db_client, &user.id, vec![&mut post]).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
}
|
2021-04-09 00:22:17 +00:00
|
|
|
let status = Status::from_post(post, &config.instance_url());
|
|
|
|
Ok(HttpResponse::Ok().json(status))
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:32:44 +00:00
|
|
|
#[get("/{status_id}/context")]
|
|
|
|
async fn get_context(
|
2021-10-18 22:35:52 +00:00
|
|
|
auth: Option<BearerAuth>,
|
2021-09-22 16:32:44 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
let maybe_current_user = match auth {
|
|
|
|
Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
|
|
|
|
None => None,
|
|
|
|
};
|
2021-11-19 23:17:04 +00:00
|
|
|
let mut posts = get_thread(
|
|
|
|
db_client,
|
|
|
|
&status_id,
|
|
|
|
maybe_current_user.as_ref().map(|user| &user.id),
|
|
|
|
).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
if let Some(user) = maybe_current_user {
|
|
|
|
get_actions_for_posts(
|
|
|
|
db_client,
|
|
|
|
&user.id,
|
|
|
|
posts.iter_mut().collect(),
|
|
|
|
).await?;
|
|
|
|
}
|
|
|
|
let statuses: Vec<Status> = posts
|
2021-09-22 16:32:44 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|post| Status::from_post(post, &config.instance_url()))
|
|
|
|
.collect();
|
|
|
|
Ok(HttpResponse::Ok().json(statuses))
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:55:33 +00:00
|
|
|
#[post("/{status_id}/favourite")]
|
|
|
|
async fn favourite(
|
|
|
|
auth: BearerAuth,
|
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &mut **get_database_client(&db_pool).await?;
|
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-11-20 01:10:56 +00:00
|
|
|
let post = get_post_by_id(db_client, &status_id).await?;
|
|
|
|
if !can_view_post(Some(¤t_user), &post) {
|
|
|
|
return Err(HttpError::NotFoundError("post"));
|
|
|
|
};
|
2021-10-29 19:21:26 +00:00
|
|
|
let reaction_created = match create_reaction(
|
|
|
|
db_client, ¤t_user.id, &status_id,
|
|
|
|
).await {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(DatabaseError::AlreadyExists(_)) => false, // post already favourited
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
2021-10-18 22:35:52 +00:00
|
|
|
let mut post = get_post_by_id(db_client, &status_id).await?;
|
2021-11-24 00:33:22 +00:00
|
|
|
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
2021-10-29 19:21:26 +00:00
|
|
|
|
|
|
|
if reaction_created {
|
2021-11-12 23:41:03 +00:00
|
|
|
let maybe_remote_actor = post.author.remote_actor()
|
2021-10-31 20:46:29 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
|
|
|
if let Some(remote_actor) = maybe_remote_actor {
|
2021-10-29 19:21:26 +00:00
|
|
|
// Federate
|
|
|
|
let object_id = post.object_id.as_ref().ok_or(HttpError::InternalError)?;
|
|
|
|
let activity = create_activity_like(
|
|
|
|
&config.instance_url(),
|
|
|
|
¤t_user.profile,
|
2021-11-13 17:37:31 +00:00
|
|
|
object_id,
|
2021-10-29 19:21:26 +00:00
|
|
|
);
|
2021-10-31 20:46:29 +00:00
|
|
|
deliver_activity(&config, ¤t_user, activity, vec![remote_actor]);
|
2021-10-29 19:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:55:33 +00:00
|
|
|
let status = Status::from_post(post, &config.instance_url());
|
|
|
|
Ok(HttpResponse::Ok().json(status))
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:42:56 +00:00
|
|
|
#[post("/{status_id}/unfavourite")]
|
|
|
|
async fn unfavourite(
|
|
|
|
auth: BearerAuth,
|
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &mut **get_database_client(&db_pool).await?;
|
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-11-20 01:10:56 +00:00
|
|
|
let post = get_post_by_id(db_client, &status_id).await?;
|
|
|
|
if !can_view_post(Some(¤t_user), &post) {
|
|
|
|
return Err(HttpError::NotFoundError("post"));
|
|
|
|
};
|
2021-10-18 23:42:56 +00:00
|
|
|
match delete_reaction(db_client, ¤t_user.id, &status_id).await {
|
|
|
|
Err(DatabaseError::NotFound(_)) => (), // post not favourited
|
|
|
|
other_result => other_result?,
|
|
|
|
}
|
|
|
|
let mut post = get_post_by_id(db_client, &status_id).await?;
|
2021-11-24 00:33:22 +00:00
|
|
|
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
2021-10-18 23:42:56 +00:00
|
|
|
let status = Status::from_post(post, &config.instance_url());
|
|
|
|
Ok(HttpResponse::Ok().json(status))
|
|
|
|
}
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
// https://docs.opensea.io/docs/metadata-standards
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct PostMetadata {
|
|
|
|
name: String,
|
|
|
|
description: String,
|
|
|
|
image: String,
|
|
|
|
external_url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/{status_id}/make_permanent")]
|
|
|
|
async fn make_permanent(
|
2021-10-05 18:10:14 +00:00
|
|
|
auth: BearerAuth,
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
let mut post = get_post_by_id(db_client, &status_id).await?;
|
2021-11-20 01:10:56 +00:00
|
|
|
if post.author.id != current_user.id || !post.is_public() {
|
|
|
|
// Users can only archive their own public posts
|
|
|
|
return Err(HttpError::NotFoundError("post"));
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
let ipfs_api_url = config.ipfs_api_url.as_ref()
|
|
|
|
.ok_or(HttpError::NotSupported)?;
|
|
|
|
|
|
|
|
let post_image_cid = if let Some(attachment) = post.attachments.first() {
|
|
|
|
// Add attachment to IPFS
|
|
|
|
let image_path = config.media_dir().join(&attachment.file_name);
|
|
|
|
let image_data = std::fs::read(image_path)
|
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
2021-11-13 17:37:31 +00:00
|
|
|
let image_cid = ipfs_store::add(ipfs_api_url, image_data).await
|
2021-04-09 00:22:17 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
2021-09-28 21:44:31 +00:00
|
|
|
set_attachment_ipfs_cid(db_client, &attachment.id, &image_cid).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
image_cid
|
|
|
|
} else {
|
|
|
|
// Use IPFS logo if there's no image
|
|
|
|
IPFS_LOGO.to_string()
|
|
|
|
};
|
2021-11-04 23:49:53 +00:00
|
|
|
let post_url = get_object_url(
|
|
|
|
&config.instance_url(),
|
|
|
|
&post.id,
|
|
|
|
);
|
2021-04-09 00:22:17 +00:00
|
|
|
let post_metadata = PostMetadata {
|
|
|
|
name: format!("Post {}", post.id),
|
|
|
|
description: post.content.clone(),
|
|
|
|
image: get_ipfs_url(&post_image_cid),
|
2021-11-04 23:49:53 +00:00
|
|
|
external_url: post_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
};
|
|
|
|
let post_metadata_json = serde_json::to_string(&post_metadata)
|
|
|
|
.map_err(|_| HttpError::InternalError)?
|
|
|
|
.as_bytes().to_vec();
|
2021-11-13 17:37:31 +00:00
|
|
|
let post_metadata_cid = ipfs_store::add(ipfs_api_url, post_metadata_json).await
|
2021-04-09 00:22:17 +00:00
|
|
|
.map_err(|_| HttpError::InternalError)?;
|
|
|
|
|
|
|
|
// Update post
|
|
|
|
post.ipfs_cid = Some(post_metadata_cid);
|
|
|
|
update_post(db_client, &post).await?;
|
2021-11-24 00:33:22 +00:00
|
|
|
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
let status = Status::from_post(post, &config.instance_url());
|
|
|
|
Ok(HttpResponse::Ok().json(status))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/{status_id}/signature")]
|
|
|
|
async fn get_signature(
|
2021-10-05 18:10:14 +00:00
|
|
|
auth: BearerAuth,
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
web::Path(status_id): web::Path<Uuid>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2021-10-05 18:10:14 +00:00
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
let contract_config = config.ethereum_contract.as_ref()
|
|
|
|
.ok_or(HttpError::NotSupported)?;
|
|
|
|
let post = get_post_by_id(db_client, &status_id).await?;
|
2021-11-20 01:10:56 +00:00
|
|
|
if post.author.id != current_user.id || !post.is_public() {
|
|
|
|
// Users can only tokenize their own public posts
|
2021-11-13 17:37:31 +00:00
|
|
|
return Err(HttpError::NotFoundError("post"));
|
2021-11-20 01:10:56 +00:00
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
let ipfs_cid = post.ipfs_cid
|
|
|
|
// Post metadata is not immutable
|
|
|
|
.ok_or(HttpError::ValidationError("post is not immutable".into()))?;
|
|
|
|
let token_uri = get_ipfs_url(&ipfs_cid);
|
|
|
|
let signature = create_mint_signature(
|
2021-11-13 17:37:31 +00:00
|
|
|
contract_config,
|
2021-04-09 00:22:17 +00:00
|
|
|
¤t_user.wallet_address,
|
|
|
|
&token_uri,
|
|
|
|
).map_err(|_| HttpError::InternalError)?;
|
|
|
|
Ok(HttpResponse::Ok().json(signature))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn status_api_scope() -> Scope {
|
|
|
|
web::scope("/api/v1/statuses")
|
|
|
|
// Routes without status ID
|
|
|
|
.service(create_status)
|
|
|
|
// Routes with status ID
|
|
|
|
.service(get_status)
|
2021-09-22 16:32:44 +00:00
|
|
|
.service(get_context)
|
2021-10-17 16:55:33 +00:00
|
|
|
.service(favourite)
|
2021-10-18 23:42:56 +00:00
|
|
|
.service(unfavourite)
|
2021-04-09 00:22:17 +00:00
|
|
|
.service(make_permanent)
|
|
|
|
.service(get_signature)
|
|
|
|
}
|