mirror of
https://github.com/versia-pub/activitypub.git
synced 2025-12-06 06:38:20 +01:00
fix: format files
This commit is contained in:
parent
489a216aca
commit
cd6ff024e4
|
|
@ -1,8 +1,8 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
use dotenv::dotenv;
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
dotenv().ok();
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
|
|
|
|||
12
src/http.rs
12
src/http.rs
|
|
@ -1,5 +1,9 @@
|
|||
use crate::{
|
||||
database::StateHandle, entities::user, error::Error, lysand::{self, conversion::receive_lysand_note}, objects::person::{DbUser, PersonAcceptedActivities}
|
||||
database::StateHandle,
|
||||
entities::user,
|
||||
error::Error,
|
||||
lysand::{self, conversion::receive_lysand_note},
|
||||
objects::person::{DbUser, PersonAcceptedActivities},
|
||||
};
|
||||
use activitypub_federation::{
|
||||
actix_web::{inbox::receive_activity, signing_actor},
|
||||
|
|
@ -33,7 +37,11 @@ pub fn listen(config: &FederationConfig<StateHandle>) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn lysand_inbox(note: web::Json<lysand::objects::Note>, id: web::Path<String>, data: Data<StateHandle>) -> Result<HttpResponse, Error> {
|
||||
pub fn lysand_inbox(
|
||||
note: web::Json<lysand::objects::Note>,
|
||||
id: web::Path<String>,
|
||||
data: Data<StateHandle>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
tokio::spawn(receive_lysand_note(note.into_inner(), id.into_inner()));
|
||||
Ok(HttpResponse::Created().finish())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,23 @@
|
|||
use activitypub_federation::{fetch::object_id::ObjectId, http_signatures::generate_actor_keypair};
|
||||
use activitystreams_kinds::public;
|
||||
use anyhow::{anyhow, Ok};
|
||||
use async_recursion::async_recursion;
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
|
||||
use anyhow::{anyhow, Ok};
|
||||
use url::Url;
|
||||
use async_recursion::async_recursion;
|
||||
|
||||
use crate::{database::State, entities::{self, post, prelude, user}, objects::post::Mention, utils::{generate_object_id, generate_user_id}, API_DOMAIN, DB, FEDERATION_CONFIG, LYSAND_DOMAIN};
|
||||
use crate::{
|
||||
database::State,
|
||||
entities::{self, post, prelude, user},
|
||||
objects::post::Mention,
|
||||
utils::{generate_object_id, generate_user_id},
|
||||
API_DOMAIN, DB, FEDERATION_CONFIG, LYSAND_DOMAIN,
|
||||
};
|
||||
|
||||
use super::{objects::{ContentFormat, Note}, superx::request_client};
|
||||
use super::{
|
||||
objects::{ContentFormat, Note},
|
||||
superx::request_client,
|
||||
};
|
||||
|
||||
pub async fn fetch_user_from_url(url: Url) -> anyhow::Result<super::objects::User> {
|
||||
let req_client = request_client();
|
||||
|
|
@ -29,7 +38,10 @@ pub async fn db_post_from_url(url: Url) -> anyhow::Result<entities::post::Model>
|
|||
return Err(anyhow!("not lysands domain"));
|
||||
}
|
||||
let str_url = url.to_string();
|
||||
let post_res: Option<post::Model> = prelude::Post::find().filter(entities::post::Column::Url.eq(str_url.clone())).one(DB.get().unwrap()).await?;
|
||||
let post_res: Option<post::Model> = prelude::Post::find()
|
||||
.filter(entities::post::Column::Url.eq(str_url.clone()))
|
||||
.one(DB.get().unwrap())
|
||||
.await?;
|
||||
|
||||
if let Some(post) = post_res {
|
||||
Ok(post)
|
||||
|
|
@ -44,7 +56,10 @@ pub async fn db_user_from_url(url: Url) -> anyhow::Result<entities::user::Model>
|
|||
if !url.domain().eq(&Some(LYSAND_DOMAIN.as_str())) {
|
||||
return Err(anyhow!("not lysands domain"));
|
||||
}
|
||||
let user_res: Option<user::Model> = prelude::User::find().filter(entities::user::Column::Url.eq(url.to_string())).one(DB.get().unwrap()).await?;
|
||||
let user_res: Option<user::Model> = prelude::User::find()
|
||||
.filter(entities::user::Column::Url.eq(url.to_string()))
|
||||
.one(DB.get().unwrap())
|
||||
.await?;
|
||||
|
||||
if let Some(user) = user_res {
|
||||
Ok(user)
|
||||
|
|
@ -63,7 +78,9 @@ pub async fn db_user_from_url(url: Url) -> anyhow::Result<entities::user::Model>
|
|||
following_count: Set(0),
|
||||
url: Set(ls_user.uri.to_string()),
|
||||
local: Set(true),
|
||||
created_at: Set(DateTime::from_timestamp(ls_user.created_at.unix_timestamp(), 0).unwrap()),
|
||||
created_at: Set(
|
||||
DateTime::from_timestamp(ls_user.created_at.unix_timestamp(), 0).unwrap(),
|
||||
),
|
||||
summary: Set(option_content_format_text(ls_user.bio).await),
|
||||
updated_at: Set(Some(Utc::now())),
|
||||
followers: Set(Some(ls_user.followers.to_string())),
|
||||
|
|
@ -81,46 +98,70 @@ pub async fn fetch_note_from_url(url: Url) -> anyhow::Result<super::objects::Not
|
|||
Ok(request.json::<super::objects::Note>().await?)
|
||||
}
|
||||
#[async_recursion]
|
||||
pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result<entities::post::Model> {
|
||||
pub async fn receive_lysand_note(
|
||||
note: Note,
|
||||
db_id: String,
|
||||
) -> anyhow::Result<entities::post::Model> {
|
||||
let lysand_author: entities::user::Model = db_user_from_url(note.author.clone()).await?;
|
||||
let user_res = prelude::User::find_by_id(db_id).one(DB.get().unwrap()).await;
|
||||
let user_res = prelude::User::find_by_id(db_id)
|
||||
.one(DB.get().unwrap())
|
||||
.await;
|
||||
if user_res.is_err() {
|
||||
println!("{}", user_res.as_ref().unwrap_err());
|
||||
return Err(user_res.err().unwrap().into());
|
||||
}
|
||||
if let Some(target) = user_res? {
|
||||
let data = FEDERATION_CONFIG.get().unwrap();
|
||||
let id: ObjectId<post::Model> = generate_object_id(data.domain(), ¬e.id.to_string())?.into();
|
||||
let id: ObjectId<post::Model> =
|
||||
generate_object_id(data.domain(), ¬e.id.to_string())?.into();
|
||||
let user_id = generate_user_id(data.domain(), &target.id.to_string())?;
|
||||
let user = fetch_user_from_url(note.author.clone()).await?;
|
||||
let data = FEDERATION_CONFIG.get().unwrap();
|
||||
let mut tag: Vec<Mention> = Vec::new();
|
||||
for l_tag in note.mentions.clone().unwrap_or_default() {
|
||||
tag.push(Mention { href: l_tag, //TODO convert to ap url
|
||||
kind: Default::default(), })
|
||||
tag.push(Mention {
|
||||
href: l_tag, //TODO convert to ap url
|
||||
kind: Default::default(),
|
||||
})
|
||||
}
|
||||
let to = match note.visibility.clone().unwrap_or(super::objects::VisibilityType::Public) {
|
||||
super::objects::VisibilityType::Public => vec![public(), Url::parse(&user.followers.to_string().as_str())?],
|
||||
super::objects::VisibilityType::Followers => vec![Url::parse(&user.followers.to_string().as_str())?],
|
||||
let to = match note
|
||||
.visibility
|
||||
.clone()
|
||||
.unwrap_or(super::objects::VisibilityType::Public)
|
||||
{
|
||||
super::objects::VisibilityType::Public => {
|
||||
vec![public(), Url::parse(&user.followers.to_string().as_str())?]
|
||||
}
|
||||
super::objects::VisibilityType::Followers => {
|
||||
vec![Url::parse(&user.followers.to_string().as_str())?]
|
||||
}
|
||||
super::objects::VisibilityType::Direct => note.mentions.unwrap_or_default(),
|
||||
super::objects::VisibilityType::Unlisted => vec![Url::parse(&user.followers.to_string().as_str())?],
|
||||
super::objects::VisibilityType::Unlisted => {
|
||||
vec![Url::parse(&user.followers.to_string().as_str())?]
|
||||
}
|
||||
};
|
||||
let cc = match note.visibility.clone().unwrap_or(super::objects::VisibilityType::Public) {
|
||||
let cc = match note
|
||||
.visibility
|
||||
.clone()
|
||||
.unwrap_or(super::objects::VisibilityType::Public)
|
||||
{
|
||||
super::objects::VisibilityType::Unlisted => Some(vec![public()]),
|
||||
_ => None
|
||||
_ => None,
|
||||
};
|
||||
let reply: Option<ObjectId<entities::post::Model>> = if let Some(rep) = note.replies_to.clone() {
|
||||
let note = fetch_note_from_url(rep).await?;
|
||||
let fake_rep_url = Url::parse(&format!(
|
||||
"https://{}/apbridge/object/{}",
|
||||
API_DOMAIN.to_string(),
|
||||
¬e.id.to_string()
|
||||
))?;
|
||||
Some(fake_rep_url.into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let quote: Option<ObjectId<entities::post::Model>> = if let Some(rep) = note.quotes.clone() {
|
||||
let reply: Option<ObjectId<entities::post::Model>> =
|
||||
if let Some(rep) = note.replies_to.clone() {
|
||||
let note = fetch_note_from_url(rep).await?;
|
||||
let fake_rep_url = Url::parse(&format!(
|
||||
"https://{}/apbridge/object/{}",
|
||||
API_DOMAIN.to_string(),
|
||||
¬e.id.to_string()
|
||||
))?;
|
||||
Some(fake_rep_url.into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let quote: Option<ObjectId<entities::post::Model>> = if let Some(rep) = note.quotes.clone()
|
||||
{
|
||||
let note = fetch_note_from_url(rep).await?;
|
||||
let fake_rep_url = Url::parse(&format!(
|
||||
"https://{}/apbridge/object/{}",
|
||||
|
|
@ -149,11 +190,17 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result<en
|
|||
to,
|
||||
tag,
|
||||
attributed_to: Url::parse(user.uri.clone().as_str()).unwrap().into(),
|
||||
content: option_content_format_text(note.content).await.unwrap_or_default(),
|
||||
in_reply_to: reply.clone()
|
||||
content: option_content_format_text(note.content)
|
||||
.await
|
||||
.unwrap_or_default(),
|
||||
in_reply_to: reply.clone(),
|
||||
};
|
||||
|
||||
let visibility = match note.visibility.clone().unwrap_or(super::objects::VisibilityType::Public) {
|
||||
let visibility = match note
|
||||
.visibility
|
||||
.clone()
|
||||
.unwrap_or(super::objects::VisibilityType::Public)
|
||||
{
|
||||
super::objects::VisibilityType::Public => "public",
|
||||
super::objects::VisibilityType::Followers => "followers",
|
||||
super::objects::VisibilityType::Direct => "direct",
|
||||
|
|
@ -170,7 +217,9 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result<en
|
|||
creator: Set(lysand_author.id.clone()),
|
||||
content: Set(ap_note.content.clone()),
|
||||
sensitive: Set(ap_note.sensitive),
|
||||
created_at: Set(Utc.timestamp_micros(note.created_at.unix_timestamp()).unwrap()),
|
||||
created_at: Set(Utc
|
||||
.timestamp_micros(note.created_at.unix_timestamp())
|
||||
.unwrap()),
|
||||
local: Set(true),
|
||||
updated_at: Set(Some(Utc::now())),
|
||||
content_type: Set("Note".to_string()),
|
||||
|
|
@ -187,4 +236,4 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result<en
|
|||
} else {
|
||||
Err(anyhow!("User not found"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -2,7 +2,14 @@ use activitypub_federation::{traits::Object, FEDERATION_CONTENT_TYPE};
|
|||
use actix_web::{get, web, HttpResponse};
|
||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||
|
||||
use crate::{database::State, entities::{post::{self, Entity}, prelude}, error, Response, DB, FEDERATION_CONFIG};
|
||||
use crate::{
|
||||
database::State,
|
||||
entities::{
|
||||
post::{self, Entity},
|
||||
prelude,
|
||||
},
|
||||
error, Response, DB, FEDERATION_CONFIG,
|
||||
};
|
||||
|
||||
#[get("/apbridge/object/{post}")]
|
||||
async fn fetch_post(
|
||||
|
|
@ -21,5 +28,10 @@ async fn fetch_post(
|
|||
None => return Ok(HttpResponse::NotFound().finish()),
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok().content_type(FEDERATION_CONTENT_TYPE).json(post.into_json(&FEDERATION_CONFIG.get().unwrap().to_request_data()).await?))
|
||||
}
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(FEDERATION_CONTENT_TYPE)
|
||||
.json(
|
||||
post.into_json(&FEDERATION_CONFIG.get().unwrap().to_request_data())
|
||||
.await?,
|
||||
))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
pub mod conversion;
|
||||
pub mod funcs;
|
||||
pub mod http;
|
||||
pub mod objects;
|
||||
pub mod superx;
|
||||
pub mod test;
|
||||
pub mod conversion;
|
||||
pub mod funcs;
|
||||
pub mod http;
|
||||
|
|
@ -111,21 +111,21 @@ pub struct ContentHash {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct ContentFormat {
|
||||
x: HashMap<String, ContentEntry>,
|
||||
}
|
||||
}
|
||||
|
||||
impl ContentFormat {
|
||||
pub async fn select_rich_text(&self) -> anyhow::Result<String> {
|
||||
if let Some(entry) = self.x.get("text/x.misskeymarkdown") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("text/html") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("text/markdown") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("text/plain") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
|
||||
Ok(self.x.clone().values().next().unwrap().content.clone())
|
||||
|
|
@ -133,25 +133,25 @@ impl ContentFormat {
|
|||
|
||||
pub async fn select_rich_img(&self) -> anyhow::Result<String> {
|
||||
if let Some(entry) = self.x.get("image/webp") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/png") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/avif") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/jxl") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/jpeg") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/gif") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
if let Some(entry) = self.x.get("image/bmp") {
|
||||
return Ok(entry.content.clone())
|
||||
return Ok(entry.content.clone());
|
||||
}
|
||||
|
||||
Ok(self.x.clone().values().next().unwrap().content.clone())
|
||||
|
|
|
|||
|
|
@ -8,10 +8,15 @@ async fn test_user_serial() {
|
|||
let response = client
|
||||
.get("https://social.lysand.org/users/018ec082-0ae1-761c-b2c5-22275a611771")
|
||||
.send()
|
||||
.await.unwrap();
|
||||
let user = super::superx::deserialize_user(response.text().await.unwrap()).await.unwrap();
|
||||
.await
|
||||
.unwrap();
|
||||
let user = super::superx::deserialize_user(response.text().await.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let response_outbox = client.get(user.outbox.as_str()).send().await.unwrap();
|
||||
let outbox = super::superx::deserialize_outbox(response_outbox.text().await.unwrap()).await.unwrap();
|
||||
let outbox = super::superx::deserialize_outbox(response_outbox.text().await.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(outbox.items.len() > 0);
|
||||
}
|
||||
|
||||
|
|
@ -45,7 +50,11 @@ pub async fn main() -> anyhow::Result<()> {
|
|||
|
||||
println!("\n\n\nas AP:");
|
||||
for item in outbox.items {
|
||||
let ap_item = super::conversion::receive_lysand_note(item, "https://ap.lysand.org/example".to_string()).await?;
|
||||
let ap_item = super::conversion::receive_lysand_note(
|
||||
item,
|
||||
"https://ap.lysand.org/example".to_string(),
|
||||
)
|
||||
.await?;
|
||||
println!("{:#?}", ap_item);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ use crate::{
|
|||
objects::post::{Mention, Note},
|
||||
};
|
||||
use crate::{activities::follow::Follow, entities::user};
|
||||
use lazy_static::lazy_static;
|
||||
use dotenv::dotenv;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
mod activities;
|
||||
mod database;
|
||||
|
|
@ -96,7 +96,7 @@ async fn post_manually(
|
|||
content: format!("{} {}", path.1, target.name),
|
||||
tag: vec![mention],
|
||||
in_reply_to: None,
|
||||
cc: vec![].into()
|
||||
cc: vec![].into(),
|
||||
};
|
||||
|
||||
CreatePost::send(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
use crate::{
|
||||
activities::create_post::CreatePost, database::StateHandle, entities::{post, user}, error::Error, lysand::conversion::db_user_from_url, objects::person::DbUser, utils::generate_object_id
|
||||
activities::create_post::CreatePost,
|
||||
database::StateHandle,
|
||||
entities::{post, user},
|
||||
error::Error,
|
||||
lysand::conversion::db_user_from_url,
|
||||
objects::person::DbUser,
|
||||
utils::generate_object_id,
|
||||
};
|
||||
use activitypub_federation::{
|
||||
config::Data,
|
||||
|
|
@ -56,18 +62,25 @@ impl Object for post::Model {
|
|||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
let post = crate::entities::prelude::Post::find()
|
||||
.filter(post::Column::Id.eq(object_id.to_string()))
|
||||
.one(data.app_data().database_connection.clone().as_ref()).await;
|
||||
.filter(post::Column::Id.eq(object_id.to_string()))
|
||||
.one(data.app_data().database_connection.clone().as_ref())
|
||||
.await;
|
||||
Ok(post.unwrap())
|
||||
}
|
||||
|
||||
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
|
||||
let creator = db_user_from_url(Url::parse(self.creator.as_str()).unwrap()).await?;
|
||||
let to = match self.visibility.as_str() {
|
||||
"public" => vec![public(), Url::parse(creator.followers.unwrap().as_str()).unwrap()],
|
||||
"public" => vec![
|
||||
public(),
|
||||
Url::parse(creator.followers.unwrap().as_str()).unwrap(),
|
||||
],
|
||||
"followers" => vec![Url::parse(creator.followers.unwrap().as_str()).unwrap()],
|
||||
"direct" => vec![], //TODO: implement this
|
||||
"unlisted" => vec![Url::parse(creator.followers.unwrap().as_str()).unwrap(), public()],
|
||||
"unlisted" => vec![
|
||||
Url::parse(creator.followers.unwrap().as_str()).unwrap(),
|
||||
public(),
|
||||
],
|
||||
_ => vec![public()],
|
||||
};
|
||||
Ok(Note {
|
||||
|
|
|
|||
|
|
@ -17,5 +17,8 @@ pub fn generate_random_object_id(domain: &str) -> Result<Url, ParseError> {
|
|||
|
||||
/// Generate a follow accept id
|
||||
pub fn generate_follow_accept_id(domain: &str, db_id: i32) -> Result<Url, ParseError> {
|
||||
Url::parse(&format!("https://{}/apbridge/activity/follow/{}", domain, db_id))
|
||||
Url::parse(&format!(
|
||||
"https://{}/apbridge/activity/follow/{}",
|
||||
domain, db_id
|
||||
))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue