summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorD. Scott Boggs <scott@tams.tech>2023-01-30 10:07:35 -0500
committerD. Scott Boggs <scott@tams.tech>2023-01-30 10:20:03 -0500
commitf944ecc2a9eab5c3abd04e0ec88bc55399a6d88c (patch)
tree54623e6582fc8d39a7526aba020c97b91e0df687
parent234868d82dee0f084762291ca69cee5850daed51 (diff)
Add entity for dealing with announcementscomb-entities/announcement
-rw-r--r--entities/src/announcement.rs127
-rw-r--r--entities/src/ids.rs1
-rw-r--r--entities/src/lib.rs2
3 files changed, 130 insertions, 0 deletions
diff --git a/entities/src/announcement.rs b/entities/src/announcement.rs
new file mode 100644
index 0000000..cd8fbe4
--- /dev/null
+++ b/entities/src/announcement.rs
@@ -0,0 +1,127 @@
+use serde::{Deserialize, Serialize};
+use time::{serde::iso8601, OffsetDateTime};
+use url::Url;
+
+use crate::{custom_emoji::CustomEmoji, status, AccountId, AnnouncementId, StatusId};
+
+/// Represents an announcement set by an administrator.
+///
+/// See also [the API documentation](https://docs.joinmastodon.org/entities/Announcement/)
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+struct Announcement {
+ /// The ID of the announcement in the database.
+ pub id: AnnouncementId,
+ /// The text of the announcement, as HTML.
+ pub content: String,
+ /// When the announcement will start.
+ #[serde(with = "iso8601::option")]
+ pub starts_at: Option<OffsetDateTime>,
+ /// When the announcement will end.
+ #[serde(with = "iso8601::option")]
+ pub ends_at: Option<OffsetDateTime>,
+ /// Whether the announcement should start and end on dates only instead of
+ /// datetimes. Will be false if there is no starts_at or ends_at time.
+ pub all_day: bool,
+ /// When the announcement was published.
+ #[serde(with = "iso8601")]
+ pub published_at: OffsetDateTime,
+ /// When the announcement was last updated.
+ #[serde(with = "iso8601")]
+ pub updated_at: OffsetDateTime,
+ /// Whether the announcement has been read by the current user.
+ #[serde(default)]
+ pub read: bool,
+ /// Accounts mentioned in the announcement text.
+ pub mentions: Vec<Account>,
+ /// Statuses linked in the announcement text.
+ pub statuses: Vec<Status>,
+ /// Tags linked in the announcement text.
+ pub tags: Vec<status::Tag>,
+ /// Custom emoji used in the announcement text.
+ pub emojis: Vec<CustomEmoji>,
+ /// Emoji reactions attached to the announcement.
+ pub reactions: Vec<Reaction>,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Reaction {
+ pub name: String,
+ pub count: i64,
+ pub me: bool,
+ pub url: Option<String>,
+ pub static_url: Option<String>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Account {
+ /// The account ID of the mentioned user.
+ pub id: AccountId,
+ /// The username of the mentioned user.
+ pub username: String,
+ /// The location of the mentioned user’s profile.
+ pub url: Url,
+ /// The webfinger acct: URI of the mentioned user. Equivalent to username for local users, or username@domain for remote users.
+ pub acct: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Status {
+ /// The ID of an attached Status in the database
+ pub id: StatusId,
+ /// The URL of an attached Status.
+ pub url: Url,
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_deserialize() {
+ let sample = r##"
+ {
+ "id": "8",
+ "content": "<p>Looks like there was an issue processing audio attachments without embedded art since yesterday due to an experimental new feature. That issue has now been fixed, so you may see older posts with audio from other servers pop up in your feeds now as they are being finally properly processed. Sorry!</p>",
+ "starts_at": null,
+ "ends_at": null,
+ "all_day": false,
+ "published_at": "2020-07-03T01:27:38.726Z",
+ "updated_at": "2020-07-03T01:27:38.752Z",
+ "read": true,
+ "mentions": [],
+ "statuses": [],
+ "tags": [],
+ "emojis": [],
+ "reactions": [
+ {
+ "name": "bongoCat",
+ "count": 9,
+ "me": false,
+ "url": "https://files.mastodon.social/custom_emojis/images/000/067/715/original/fdba57dff7576d53.png",
+ "static_url": "https://files.mastodon.social/custom_emojis/images/000/067/715/static/fdba57dff7576d53.png"
+ },
+ {
+ "name": "thonking",
+ "count": 1,
+ "me": false,
+ "url": "https://files.mastodon.social/custom_emojis/images/000/098/690/original/a8d36edc4a7032e8.png",
+ "static_url": "https://files.mastodon.social/custom_emojis/images/000/098/690/static/a8d36edc4a7032e8.png"
+ },
+ {
+ "name": "AAAAAA",
+ "count": 1,
+ "me": false,
+ "url": "https://files.mastodon.social/custom_emojis/images/000/071/387/original/AAAAAA.png",
+ "static_url": "https://files.mastodon.social/custom_emojis/images/000/071/387/static/AAAAAA.png"
+ },
+ {
+ "name": "🤔",
+ "count": 1,
+ "me": true
+ }
+ ]
+}"##;
+ let ann: Announcement = serde_json::from_str(sample).expect("deserialize");
+ assert_eq!(ann.id, AnnouncementId::new("8"));
+ }
+}
diff --git a/entities/src/ids.rs b/entities/src/ids.rs
index a24f541..dd1b985 100644
--- a/entities/src/ids.rs
+++ b/entities/src/ids.rs
@@ -56,4 +56,5 @@ define_ids!(
"an ID of a domain block" as DomainBlockId,
"an ID of an email domain block" as EmailDomainBlockId,
"a measurement key" as MeasureKey,
+ "an announcement ID" as AnnouncementId,
);
diff --git a/entities/src/lib.rs b/entities/src/lib.rs
index 52fd46b..fcbe581 100644
--- a/entities/src/lib.rs
+++ b/entities/src/lib.rs
@@ -9,6 +9,8 @@ pub mod error;
pub mod account;
/// Data structures for ser/de of admin-related resources
pub mod admin;
+/// Data structures for ser/de of announcement-related resources
+pub mod announcement;
/// Data structures for ser/de of attachment-related resources
pub mod attachment;
/// Data structures for ser/de of card-related resources