Skip to content

Commit

Permalink
SlackMessageEventEdited include original message's id (#226)
Browse files Browse the repository at this point in the history
- Add parse test
- Haven't included the `previous_message` field in the updated
  type defintion because I couldn't find public documentation
  about it, but it seems to be present in the payload for change
  events

Closes #224
  • Loading branch information
shantanuraj authored Dec 8, 2023
1 parent 138b9dd commit 375f116
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
90 changes: 90 additions & 0 deletions src/models/events/fixtures/message_changed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"token": "XXXXXXXXXXXXXXXXXXXXXXXX",
"team_id": "TXXXXXXXXXX",
"context_team_id": "TXXXXXXXXXX",
"context_enterprise_id": null,
"api_app_id": "AXXXXXXXXXX",
"event": {
"type": "message",
"subtype": "message_changed",
"message": {
"client_msg_id": "000000000000000000000000000000000000",
"type": "message",
"text": "edited message",
"user": "UXXXXXXXXXX",
"blocks": [
{
"type": "rich_text",
"block_id": "wcwS3",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "hi!"
}
]
}
]
}
],
"team": "TXXXXXXXXXX",
"edited": {
"user": "UXXXXXXXXXX",
"ts": "1701743154.000000"
},
"ts": "1701735043.989889",
"source_team": "TXXXXXXXXXX",
"user_team": "TXXXXXXXXXX"
},
"previous_message": {
"client_msg_id": "000000000000000000000000000000000000",
"type": "message",
"text": "hey!",
"user": "UXXXXXXXXXX",
"ts": "1701735043.989889",
"blocks": [
{
"type": "rich_text",
"block_id": "zUXnE",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "hey!"
}
]
}
]
}
],
"team": "TXXXXXXXXXX",
"edited": {
"user": "UXXXXXXXXXX",
"ts": "1701742890.000000"
}
},
"channel": "CXXXXXXXXXX",
"hidden": true,
"ts": "1701743154.000500",
"event_ts": "1701743154.000500",
"channel_type": "group"
},
"type": "event_callback",
"event_id": "EXXXXXXXXXXX",
"event_time": 1701743154,
"authorizations": [
{
"enterprise_id": null,
"team_id": "TXXXXXXXXXX",
"user_id": "UXXXXXXXXXX",
"is_bot": true,
"is_enterprise_install": false
}
],
"is_ext_shared_channel": false,
"event_context": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
41 changes: 39 additions & 2 deletions src/models/events/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct SlackMessageEvent {
pub sender: SlackMessageSender,
pub subtype: Option<SlackMessageEventType>,
pub hidden: Option<bool>,
pub edited: Option<SlackMessageEventEdited>,
pub message: Option<SlackMessageEventEdited>,
pub deleted_ts: Option<SlackTs>,
}

Expand Down Expand Up @@ -180,7 +180,15 @@ pub struct SlackAppMentionEvent {
pub edited: Option<SlackMessageEdited>,
}

type SlackMessageEventEdited = SlackMessageEdited;
#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackMessageEventEdited {
#[serde(flatten)]
pub content: Option<SlackMessageContent>,
pub user: SlackUserId,
pub ts: SlackTs,
pub edited: SlackMessageEdited,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
Expand Down Expand Up @@ -362,3 +370,32 @@ pub struct SlackUserStatusChangedEvent {
pub event_ts: SlackTs,
pub cache_ts: SlackDateTime,
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_slack_event_message_change_event() {
let payload = include_str!("./fixtures/message_changed.json");
let event: SlackPushEventCallback = serde_json::from_str(payload).unwrap();
match event.event {
SlackEventCallbackBody::Message(SlackMessageEvent {
subtype, message, ..
}) => {
assert_eq!(subtype, Some(SlackMessageEventType::MessageChanged));
if let Some(message) = message {
assert_eq!(message.ts, "1701735043.989889".into());
assert_eq!(message.edited.ts, "1701743154.000000".into());
assert_eq!(
message.content.unwrap().text,
Some("edited message".to_string())
);
} else {
panic!("Message is None");
}
}
_ => panic!("Unexpected event type"),
}
}
}

0 comments on commit 375f116

Please sign in to comment.