use super::MessageV3Event;
use helix_core::effect::{Row, SqlValue};
use serde_json::Value;
pub(super) fn from_row(row: &Row) -> Result<Option<MessageV3Event>, crate::ImError> {
let id = text_column(row, "id");
let channel_id = text_column(row, "channel_id");
let event_seq = integer_column(row, "event_seq");
let update_at = integer_column(row, "update_at");
let quick_reply = text_column(row, "quick_reply")
.and_then(|value| serde_json::from_str::<Value>(value).ok())
.filter(Value::is_array);
let (Some(id), Some(channel_id), Some(event_seq), Some(update_at), Some(quick_reply)) =
(id, channel_id, event_seq, update_at, quick_reply)
else {
return Ok(None);
};
super::super::encode(
"im:post:updated",
serde_json::json!({
"id": id,
"channelId": channel_id,
"eventSeq": event_seq,
"updateAt": update_at,
"quickReply": quick_reply,
}),
)
.map(Some)
}
fn text_column<'a>(row: &'a Row, column: &str) -> Option<&'a str> {
row.iter().find_map(|(name, value)| {
(name == column)
.then_some(value)
.and_then(|value| match value {
SqlValue::Text(value) => Some(value.as_str()),
_ => None,
})
})
}
fn integer_column(row: &Row, column: &str) -> Option<i64> {
row.iter().find_map(|(name, value)| {
(name == column)
.then_some(value)
.and_then(|value| match value {
SqlValue::Integer(value) => Some(*value),
_ => None,
})
})
}