From f41b20508488b8d668a963f73613560ebf21fa18 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sat, 15 Apr 2023 23:43:19 +0000 Subject: [PATCH] Add support for content warnings --- CHANGELOG.md | 4 ++++ docs/openapi.yaml | 8 ++++++++ .../migrations/V0054__post__add_is_sensitive.sql | 2 ++ mitra-models/migrations/schema.sql | 3 ++- mitra-models/src/posts/queries.rs | 10 +++++++--- mitra-models/src/posts/types.rs | 7 +++++++ src/activitypub/builders/create_note.rs | 4 +++- src/activitypub/handlers/create.rs | 2 ++ src/activitypub/handlers/update.rs | 2 ++ src/activitypub/types.rs | 1 + src/mastodon_api/statuses/types.rs | 5 +++++ src/mastodon_api/statuses/views.rs | 1 + 12 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 mitra-models/migrations/V0054__post__add_is_sensitive.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 41674a2..a58af9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added + +- Added support for content warnings. + ### Changed - Ignore errors when importing activities from outbox. diff --git a/docs/openapi.yaml b/docs/openapi.yaml index f092044..9368a67 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -959,6 +959,10 @@ paths: visibility: description: Visibility of the post. $ref: '#/components/schemas/Visibility' + sensitiive: + description: Mark post and attached media as sensitive? + type: boolean + default: false mentions: description: Array of profile IDs to be mentioned type: array @@ -1907,6 +1911,10 @@ components: visibility: description: Visibility of this post. $ref: '#/components/schemas/Visibility' + sensitiive: + description: Is this post marked as sensitive content? + type: boolean + example: false spoiler_text: description: Subject or summary line, below which post content is collapsed until expanded. type: string diff --git a/mitra-models/migrations/V0054__post__add_is_sensitive.sql b/mitra-models/migrations/V0054__post__add_is_sensitive.sql new file mode 100644 index 0000000..2e00f55 --- /dev/null +++ b/mitra-models/migrations/V0054__post__add_is_sensitive.sql @@ -0,0 +1,2 @@ +ALTER TABLE post ADD COLUMN is_sensitive BOOLEAN NOT NULL DEFAULT FALSE; +ALTER TABLE post ALTER COLUMN is_sensitive DROP DEFAULT; diff --git a/mitra-models/migrations/schema.sql b/mitra-models/migrations/schema.sql index 9ce38e3..56bdc2e 100644 --- a/mitra-models/migrations/schema.sql +++ b/mitra-models/migrations/schema.sql @@ -116,7 +116,8 @@ CREATE TABLE post ( content TEXT NOT NULL, in_reply_to_id UUID REFERENCES post (id) ON DELETE CASCADE, repost_of_id UUID REFERENCES post (id) ON DELETE CASCADE, - visilibity SMALLINT NOT NULL, + visibility SMALLINT NOT NULL, + is_sensitive BOOLEAN NOT NULL, reply_count INTEGER NOT NULL CHECK (reply_count >= 0) DEFAULT 0, reaction_count INTEGER NOT NULL CHECK (reaction_count >= 0) DEFAULT 0, repost_count INTEGER NOT NULL CHECK (repost_count >= 0) DEFAULT 0, diff --git a/mitra-models/src/posts/queries.rs b/mitra-models/src/posts/queries.rs index 82ebb2a..ea0896b 100644 --- a/mitra-models/src/posts/queries.rs +++ b/mitra-models/src/posts/queries.rs @@ -183,10 +183,11 @@ pub async fn create_post( in_reply_to_id, repost_of_id, visibility, + is_sensitive, object_id, created_at ) - SELECT $1, $2, $3, $4, $5, $6, $7, $8 + SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9 WHERE NOT EXISTS ( SELECT 1 FROM post @@ -212,6 +213,7 @@ pub async fn create_post( &post_data.in_reply_to_id, &post_data.repost_of_id, &post_data.visibility, + &post_data.is_sensitive, &post_data.object_id, &post_data.created_at, ], @@ -324,14 +326,16 @@ pub async fn update_post( UPDATE post SET content = $1, - updated_at = $2 - WHERE id = $3 + is_sensitive = $2, + updated_at = $3 + WHERE id = $4 AND repost_of_id IS NULL AND ipfs_cid IS NULL RETURNING post ", &[ &post_data.content, + &post_data.is_sensitive, &post_data.updated_at, &post_id, ], diff --git a/mitra-models/src/posts/types.rs b/mitra-models/src/posts/types.rs index 09b7902..b14bb69 100644 --- a/mitra-models/src/posts/types.rs +++ b/mitra-models/src/posts/types.rs @@ -62,6 +62,7 @@ pub struct DbPost { pub in_reply_to_id: Option, pub repost_of_id: Option, pub visibility: Visibility, + pub is_sensitive: bool, pub reply_count: i32, pub reaction_count: i32, pub repost_count: i32, @@ -88,6 +89,7 @@ pub struct Post { pub in_reply_to_id: Option, pub repost_of_id: Option, pub visibility: Visibility, + pub is_sensitive: bool, pub reply_count: i32, pub reaction_count: i32, pub repost_count: i32, @@ -130,6 +132,7 @@ impl Post { }; if db_post.repost_of_id.is_some() && ( db_post.content.len() != 0 || + db_post.is_sensitive || db_post.in_reply_to_id.is_some() || db_post.ipfs_cid.is_some() || db_post.token_id.is_some() || @@ -149,6 +152,7 @@ impl Post { in_reply_to_id: db_post.in_reply_to_id, repost_of_id: db_post.repost_of_id, visibility: db_post.visibility, + is_sensitive: db_post.is_sensitive, reply_count: db_post.reply_count, reaction_count: db_post.reaction_count, repost_count: db_post.repost_count, @@ -190,6 +194,7 @@ impl Default for Post { in_reply_to_id: None, repost_of_id: None, visibility: Visibility::Public, + is_sensitive: false, reply_count: 0, reaction_count: 0, repost_count: 0, @@ -243,6 +248,7 @@ pub struct PostCreateData { pub in_reply_to_id: Option, pub repost_of_id: Option, pub visibility: Visibility, + pub is_sensitive: bool, pub attachments: Vec, pub mentions: Vec, pub tags: Vec, @@ -269,6 +275,7 @@ impl PostCreateData { #[cfg_attr(test, derive(Default))] pub struct PostUpdateData { pub content: String, + pub is_sensitive: bool, pub attachments: Vec, pub mentions: Vec, pub tags: Vec, diff --git a/src/activitypub/builders/create_note.rs b/src/activitypub/builders/create_note.rs index 16f2a73..63aaa21 100644 --- a/src/activitypub/builders/create_note.rs +++ b/src/activitypub/builders/create_note.rs @@ -70,6 +70,7 @@ pub struct Note { in_reply_to: Option, published: DateTime, + sensitive: bool, #[serde(skip_serializing_if = "Vec::is_empty")] tag: Vec, @@ -205,10 +206,11 @@ pub fn build_note( id: object_id, object_type: NOTE.to_string(), attachment: attachments, - published: post.created_at, attributed_to: actor_id, in_reply_to: in_reply_to_object_id, content: post.content.clone(), + published: post.created_at, + sensitive: post.is_sensitive, tag: tags, to: primary_audience, cc: secondary_audience, diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index 40ed313..a94d0a9 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -639,12 +639,14 @@ pub async fn handle_note( author.username, ); }; + let is_sensitive = object.sensitive.unwrap_or(false); let created_at = object.published.unwrap_or(Utc::now()); let post_data = PostCreateData { content: content, in_reply_to_id, repost_of_id: None, visibility, + is_sensitive, attachments: attachments, mentions: mentions, tags: hashtags, diff --git a/src/activitypub/handlers/update.rs b/src/activitypub/handlers/update.rs index 97fd42b..fc81289 100644 --- a/src/activitypub/handlers/update.rs +++ b/src/activitypub/handlers/update.rs @@ -69,6 +69,7 @@ async fn handle_update_note( let object_url = get_object_url(&object)?; content += &create_content_link(object_url); }; + let is_sensitive = object.sensitive.unwrap_or(false); let storage = MediaStorage::from(config); let (attachments, unprocessed) = get_object_attachments( db_client, @@ -93,6 +94,7 @@ async fn handle_update_note( let updated_at = object.updated.unwrap_or(Utc::now()); let post_data = PostUpdateData { content, + is_sensitive, attachments, mentions, tags: hashtags, diff --git a/src/activitypub/types.rs b/src/activitypub/types.rs index 6beab5b..f710ac8 100644 --- a/src/activitypub/types.rs +++ b/src/activitypub/types.rs @@ -122,6 +122,7 @@ pub struct Object { pub in_reply_to: Option, pub content: Option, pub quote_url: Option, + pub sensitive: Option, #[serde( default, diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index 946b94b..e0dd6f3 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -69,6 +69,7 @@ pub struct Status { pub in_reply_to_id: Option, pub reblog: Option>, pub visibility: String, + pub sensitive: bool, pub spoiler_text: Option, pub replies_count: i32, pub favourites_count: i32, @@ -138,6 +139,7 @@ impl Status { in_reply_to_id: post.in_reply_to_id, reblog: reblog, visibility: visibility.to_string(), + sensitive: post.is_sensitive, spoiler_text: None, replies_count: post.reply_count, favourites_count: post.reaction_count, @@ -169,6 +171,9 @@ pub struct StatusData { pub in_reply_to_id: Option, pub visibility: Option, + #[serde(default)] + pub sensitive: bool, + // Not supported by Mastodon pub mentions: Option>, diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 240cffc..c354712 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -189,6 +189,7 @@ async fn create_status( in_reply_to_id: status_data.in_reply_to_id, repost_of_id: None, visibility: visibility, + is_sensitive: status_data.sensitive, attachments: attachments, mentions: mentions, tags: hashtags,