From 8610719bd01d82990826a58a9b09cff5a83f4e9a Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 8 Oct 2021 22:39:40 +0000 Subject: [PATCH] Store IDs of incoming Note objects --- migrations/V0007__post__add_object_id.sql | 1 + migrations/schema.sql | 1 + src/activitypub/activity.rs | 2 +- src/activitypub/receiver.rs | 1 + src/mastodon_api/statuses/types.rs | 1 + src/models/posts/queries.rs | 1 + src/models/posts/types.rs | 6 ++++++ 7 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 migrations/V0007__post__add_object_id.sql diff --git a/migrations/V0007__post__add_object_id.sql b/migrations/V0007__post__add_object_id.sql new file mode 100644 index 0000000..15638c6 --- /dev/null +++ b/migrations/V0007__post__add_object_id.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD COLUMN object_id VARCHAR(200) UNIQUE; diff --git a/migrations/schema.sql b/migrations/schema.sql index 456b7f9..d775015 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -35,6 +35,7 @@ CREATE TABLE post ( content TEXT NOT NULL, in_reply_to_id UUID REFERENCES post (id) ON DELETE CASCADE, reply_count INTEGER NOT NULL CHECK (reply_count >= 0) DEFAULT 0, + object_id VARCHAR(200) UNIQUE, ipfs_cid VARCHAR(200), token_id INTEGER, token_tx_id VARCHAR(200), diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index ecd58b7..8e2503f 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -127,7 +127,7 @@ pub fn create_note( let post = in_reply_to.unwrap(); assert_eq!(post.id, in_reply_to_id); match post.author.is_local() { - false => None, // TODO: store object ID for remote posts + false => post.object_id.clone(), true => Some(get_object_url(&config.instance_url(), &post.id)), } }, diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index b8f8992..670ac22 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -125,6 +125,7 @@ pub async fn receive_activity( // TODO: parse inReplyTo field in_reply_to_id: None, attachments: attachments, + object_id: Some(object.id), created_at: object.published, }; create_post(db_client, &author.id, post_data).await?; diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index 7968f23..5e470f9 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -62,6 +62,7 @@ impl From for PostCreateData { content: value.status, in_reply_to_id: value.in_reply_to_id, attachments: value.media_ids.unwrap_or(vec![]), + object_id: None, created_at: None, } } diff --git a/src/models/posts/queries.rs b/src/models/posts/queries.rs index 7f33c36..b8bccaa 100644 --- a/src/models/posts/queries.rs +++ b/src/models/posts/queries.rs @@ -133,6 +133,7 @@ pub async fn create_post( in_reply_to_id: db_post.in_reply_to_id, reply_count: db_post.reply_count, attachments: db_attachments, + object_id: db_post.object_id, ipfs_cid: db_post.ipfs_cid, token_id: db_post.token_id, token_tx_id: db_post.token_tx_id, diff --git a/src/models/posts/types.rs b/src/models/posts/types.rs index a5e4230..f0f7159 100644 --- a/src/models/posts/types.rs +++ b/src/models/posts/types.rs @@ -18,6 +18,7 @@ pub struct DbPost { pub content: String, pub in_reply_to_id: Option, pub reply_count: i32, + pub object_id: Option, pub ipfs_cid: Option, pub token_id: Option, pub token_tx_id: Option, @@ -32,6 +33,7 @@ pub struct Post { pub in_reply_to_id: Option, pub reply_count: i32, pub attachments: Vec, + pub object_id: Option, pub ipfs_cid: Option, pub token_id: Option, pub token_tx_id: Option, @@ -53,6 +55,7 @@ impl TryFrom<&Row> for Post { in_reply_to_id: db_post.in_reply_to_id, reply_count: db_post.reply_count, attachments: db_attachments, + object_id: db_post.object_id, ipfs_cid: db_post.ipfs_cid, token_id: db_post.token_id, token_tx_id: db_post.token_tx_id, @@ -66,6 +69,7 @@ pub struct PostCreateData { pub content: String, pub in_reply_to_id: Option, pub attachments: Vec, + pub object_id: Option, pub created_at: Option>, } @@ -92,6 +96,7 @@ mod tests { content: " ".to_string(), in_reply_to_id: None, attachments: vec![], + object_id: None, created_at: None, }; assert_eq!(post_data_1.validate().is_ok(), false); @@ -103,6 +108,7 @@ mod tests { content: "test ".to_string(), in_reply_to_id: None, attachments: vec![], + object_id: None, created_at: None, }; assert_eq!(post_data_2.validate().is_ok(), true);