use super::MessageV3Event;
use serde_json::Value;
#[allow(clippy::too_many_arguments)]
pub fn window(
channel_id: &str,
window_token: &str,
state: &str,
rows: Vec<Value>,
has_older: bool,
has_newer: bool,
target_message_id: Option<&str>,
) -> Result<MessageV3Event, crate::ImError> {
ready(serde_json::json!({
"channelId": channel_id,
"windowToken": window_token,
"state": state,
"messages": rows,
"hasOlder": has_older,
"hasNewer": has_newer,
"targetMessageId": target_message_id,
}))
}
#[allow(clippy::too_many_arguments)]
pub fn page(
channel_id: &str,
window_token: &str,
direction: &str,
state: &str,
rows: Vec<Value>,
has_older: bool,
has_newer: bool,
anchor_post_id: Option<&str>,
) -> Result<MessageV3Event, crate::ImError> {
update(serde_json::json!({
"channelId": channel_id,
"windowToken": window_token,
"direction": direction,
"state": state,
"messages": rows,
"hasOlder": has_older,
"hasNewer": has_newer,
"anchorPostId": anchor_post_id,
}))
}
#[allow(clippy::too_many_arguments)]
pub fn anchored_update(
channel_id: &str,
window_token: &str,
state: &str,
rows: Vec<Value>,
has_older: bool,
has_newer: bool,
anchor_post_id: Option<&str>,
newer_count: usize,
) -> Result<MessageV3Event, crate::ImError> {
update(serde_json::json!({
"channelId": channel_id,
"windowToken": window_token,
"direction": "newer",
"state": state,
"messages": rows,
"hasOlder": has_older,
"hasNewer": has_newer,
"anchorPostId": anchor_post_id,
"newerCount": newer_count,
}))
}
pub fn ready(data: Value) -> Result<MessageV3Event, crate::ImError> {
super::encode("im:timeline:ready", data)
}
pub fn update(data: Value) -> Result<MessageV3Event, crate::ImError> {
super::encode("im:timeline:update", data)
}
pub fn located(data: Value) -> Result<MessageV3Event, crate::ImError> {
super::encode("im:timeline:located", data)
}
pub fn thread(data: Value) -> Result<MessageV3Event, crate::ImError> {
super::encode("im:timeline:thread", data)
}
pub fn thread_messages(rows: &[Value]) -> Vec<Value> {
const KEYS: [&str; 13] = [
"id",
"temporaryId",
"channelId",
"userId",
"type",
"text",
"props",
"replyId",
"replyRootId",
"replyFirstLevelId",
"replyCount",
"createAt",
"eventSeq",
];
rows.iter()
.filter_map(|row| {
let object = row.as_object()?;
let clean = KEYS
.iter()
.filter_map(|key| {
object
.get(*key)
.map(|value| ((*key).to_string(), value.clone()))
})
.collect();
Some(Value::Object(clean))
})
.collect()
}