Compare commits
No commits in common. "15e9118ed230e8b65ed8e3471249e3dd3466e256" and "ffa92b4f618b1f9ee65be29e7b41561f06173816" have entirely different histories.
15e9118ed2
...
ffa92b4f61
8 changed files with 104 additions and 46 deletions
|
@ -83,7 +83,6 @@ pub async fn faker(ctx: upub::Context, count: i64) -> Result<(), sea_orm::DbErr>
|
|||
replies: Set(0),
|
||||
likes: Set(0),
|
||||
announces: Set(0),
|
||||
audience: Set(None),
|
||||
to: Set(Audience(vec![apb::target::PUBLIC.to_string()])),
|
||||
bto: Set(Audience::default()),
|
||||
cc: Set(Audience(vec![])),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use apb::{ActivityMut, ActivityType, BaseMut, ObjectMut};
|
||||
use apb::{field::OptionalString, ActivityMut, ActivityType, BaseMut, ObjectMut};
|
||||
use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns};
|
||||
|
||||
use crate::model::Audience;
|
||||
|
@ -79,6 +79,25 @@ impl Entity {
|
|||
}
|
||||
}
|
||||
|
||||
impl ActiveModel {
|
||||
#[deprecated = "use AP::activity() from processor::normalize"]
|
||||
pub fn new(activity: &impl apb::Activity) -> Result<Self, apb::FieldErr> {
|
||||
Ok(ActiveModel {
|
||||
internal: sea_orm::ActiveValue::NotSet,
|
||||
id: sea_orm::ActiveValue::Set(activity.id()?.to_string()),
|
||||
activity_type: sea_orm::ActiveValue::Set(activity.activity_type()?),
|
||||
actor: sea_orm::ActiveValue::Set(activity.actor().id()?.to_string()),
|
||||
object: sea_orm::ActiveValue::Set(activity.object().id().str()),
|
||||
target: sea_orm::ActiveValue::Set(activity.target().id().str()),
|
||||
published: sea_orm::ActiveValue::Set(activity.published().unwrap_or(chrono::Utc::now())),
|
||||
to: sea_orm::ActiveValue::Set(activity.to().into()),
|
||||
bto: sea_orm::ActiveValue::Set(activity.bto().into()),
|
||||
cc: sea_orm::ActiveValue::Set(activity.cc().into()),
|
||||
bcc: sea_orm::ActiveValue::Set(activity.bcc().into()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn ap(self) -> serde_json::Value {
|
||||
apb::new()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns};
|
||||
|
||||
use apb::{ActorMut, ActorType, BaseMut, DocumentMut, EndpointsMut, ObjectMut, PublicKeyMut};
|
||||
use apb::{field::OptionalString, Actor, ActorMut, ActorType, BaseMut, DocumentMut, Endpoints, EndpointsMut, Object, ObjectMut, PublicKey, PublicKeyMut};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "actors")]
|
||||
|
@ -150,6 +150,37 @@ impl Entity {
|
|||
}
|
||||
}
|
||||
|
||||
impl ActiveModel {
|
||||
#[deprecated = "use AP::actor() from processor::normalize"]
|
||||
pub fn new(object: &impl Actor) -> Result<Self, apb::FieldErr> {
|
||||
let ap_id = object.id()?.to_string();
|
||||
let (domain, fallback_preferred_username) = split_user_id(&ap_id);
|
||||
Ok(ActiveModel {
|
||||
internal: sea_orm::ActiveValue::NotSet,
|
||||
domain: sea_orm::ActiveValue::Set(domain),
|
||||
id: sea_orm::ActiveValue::Set(ap_id),
|
||||
preferred_username: sea_orm::ActiveValue::Set(object.preferred_username().unwrap_or(&fallback_preferred_username).to_string()),
|
||||
actor_type: sea_orm::ActiveValue::Set(object.actor_type()?),
|
||||
name: sea_orm::ActiveValue::Set(object.name().str()),
|
||||
summary: sea_orm::ActiveValue::Set(object.summary().str()),
|
||||
icon: sea_orm::ActiveValue::Set(object.icon().get().and_then(|x| x.url().id().str())),
|
||||
image: sea_orm::ActiveValue::Set(object.image().get().and_then(|x| x.url().id().str())),
|
||||
inbox: sea_orm::ActiveValue::Set(object.inbox().id().str()),
|
||||
outbox: sea_orm::ActiveValue::Set(object.outbox().id().str()),
|
||||
shared_inbox: sea_orm::ActiveValue::Set(object.endpoints().get().and_then(|x| x.shared_inbox().str())),
|
||||
followers: sea_orm::ActiveValue::Set(object.followers().id().str()),
|
||||
following: sea_orm::ActiveValue::Set(object.following().id().str()),
|
||||
published: sea_orm::ActiveValue::Set(object.published().unwrap_or(chrono::Utc::now())),
|
||||
updated: sea_orm::ActiveValue::Set(chrono::Utc::now()),
|
||||
following_count: sea_orm::ActiveValue::Set(object.following_count().unwrap_or(0) as i32),
|
||||
followers_count: sea_orm::ActiveValue::Set(object.followers_count().unwrap_or(0) as i32),
|
||||
statuses_count: sea_orm::ActiveValue::Set(object.statuses_count().unwrap_or(0) as i32),
|
||||
public_key: sea_orm::ActiveValue::Set(object.public_key().get().ok_or(apb::FieldErr("publicKey"))?.public_key_pem().to_string()),
|
||||
private_key: sea_orm::ActiveValue::Set(None), // there's no way to transport privkey over AP json, must come from DB
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn ap(self) -> serde_json::Value {
|
||||
apb::new()
|
||||
|
@ -189,3 +220,13 @@ impl Model {
|
|||
.set_discoverable(Some(true))
|
||||
}
|
||||
}
|
||||
|
||||
fn split_user_id(id: &str) -> (String, String) {
|
||||
let clean = id
|
||||
.replace("http://", "")
|
||||
.replace("https://", "");
|
||||
let mut splits = clean.split('/');
|
||||
let first = splits.next().unwrap_or("");
|
||||
let last = splits.last().unwrap_or(first);
|
||||
(first.to_string(), last.to_string())
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use apb::{BaseMut, CollectionMut, ObjectMut, ObjectType};
|
||||
use apb::{field::OptionalString, BaseMut, Collection, CollectionMut, ObjectMut, ObjectType};
|
||||
use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns};
|
||||
|
||||
use super::Audience;
|
||||
|
@ -28,8 +28,6 @@ pub struct Model {
|
|||
pub bcc: Audience,
|
||||
pub published: ChronoDateTimeUtc,
|
||||
pub updated: ChronoDateTimeUtc,
|
||||
|
||||
pub audience: Option<String>, // added with migration m20240606_000001
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
|
@ -142,6 +140,47 @@ impl Entity {
|
|||
}
|
||||
}
|
||||
|
||||
impl ActiveModel {
|
||||
#[deprecated = "use AP::object() from processor::normalize"]
|
||||
pub fn new(object: &impl apb::Object) -> Result<Self, apb::FieldErr> {
|
||||
let t = object.object_type()?;
|
||||
if matches!(t,
|
||||
apb::ObjectType::Activity(_)
|
||||
| apb::ObjectType::Actor(_)
|
||||
| apb::ObjectType::Collection(_)
|
||||
| apb::ObjectType::Document(_)
|
||||
) {
|
||||
return Err(apb::FieldErr("type"));
|
||||
}
|
||||
Ok(ActiveModel {
|
||||
internal: sea_orm::ActiveValue::NotSet,
|
||||
id: sea_orm::ActiveValue::Set(object.id()?.to_string()),
|
||||
object_type: sea_orm::ActiveValue::Set(t),
|
||||
attributed_to: sea_orm::ActiveValue::Set(object.attributed_to().id().str()),
|
||||
name: sea_orm::ActiveValue::Set(object.name().str()),
|
||||
summary: sea_orm::ActiveValue::Set(object.summary().str()),
|
||||
content: sea_orm::ActiveValue::Set(object.content().str()),
|
||||
context: sea_orm::ActiveValue::Set(object.context().id().str()),
|
||||
in_reply_to: sea_orm::ActiveValue::Set(object.in_reply_to().id().str()),
|
||||
published: sea_orm::ActiveValue::Set(object.published().unwrap_or_else(|_| chrono::Utc::now())),
|
||||
updated: sea_orm::ActiveValue::Set(object.updated().unwrap_or_else(|_| chrono::Utc::now())),
|
||||
url: sea_orm::ActiveValue::Set(object.url().id().str()),
|
||||
replies: sea_orm::ActiveValue::Set(object.replies().get()
|
||||
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32),
|
||||
likes: sea_orm::ActiveValue::Set(object.likes().get()
|
||||
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32),
|
||||
announces: sea_orm::ActiveValue::Set(object.shares().get()
|
||||
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32),
|
||||
to: sea_orm::ActiveValue::Set(object.to().into()),
|
||||
bto: sea_orm::ActiveValue::Set(object.bto().into()),
|
||||
cc: sea_orm::ActiveValue::Set(object.cc().into()),
|
||||
bcc: sea_orm::ActiveValue::Set(object.bcc().into()),
|
||||
|
||||
sensitive: sea_orm::ActiveValue::Set(object.sensitive().unwrap_or(false)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn ap(self) -> serde_json::Value {
|
||||
apb::new()
|
||||
|
@ -156,7 +195,6 @@ impl Model {
|
|||
.set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone()))
|
||||
.set_published(Some(self.published))
|
||||
.set_updated(Some(self.updated))
|
||||
.set_audience(apb::Node::maybe_link(self.audience))
|
||||
.set_to(apb::Node::links(self.to.0.clone()))
|
||||
.set_bto(apb::Node::Empty)
|
||||
.set_cc(apb::Node::links(self.cc.0.clone()))
|
||||
|
|
|
@ -231,7 +231,6 @@ impl AP {
|
|||
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32,
|
||||
announces: object.shares().get()
|
||||
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32,
|
||||
audience: object.audience().id().str(),
|
||||
to: object.to().into(),
|
||||
bto: object.bto().into(),
|
||||
cc: object.cc().into(),
|
||||
|
|
|
@ -7,7 +7,6 @@ mod m20240524_000004_create_addressing_deliveries;
|
|||
mod m20240524_000005_create_attachments_tags_mentions;
|
||||
mod m20240529_000001_add_relation_unique_index;
|
||||
mod m20240605_000001_add_jobs_table;
|
||||
mod m20240606_000001_add_audience_to_objects;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
|
@ -22,7 +21,6 @@ impl MigratorTrait for Migrator {
|
|||
Box::new(m20240524_000005_create_attachments_tags_mentions::Migration),
|
||||
Box::new(m20240529_000001_add_relation_unique_index::Migration),
|
||||
Box::new(m20240605_000001_add_jobs_table::Migration),
|
||||
Box::new(m20240606_000001_add_audience_to_objects::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,6 @@ pub enum Objects {
|
|||
Bto,
|
||||
Published,
|
||||
Updated,
|
||||
Audience, // added with migration m20240606_000001
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
use crate::m20240524_000001_create_actor_activity_object_tables::Objects;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Objects::Table)
|
||||
.add_column(ColumnDef::new(Objects::Audience).string().null())
|
||||
.to_owned()
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Objects::Table)
|
||||
.drop_column(Objects::Audience)
|
||||
.to_owned()
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue