fix(ui): allow header marker edits in read-only dashboard
mattermost--set-header-marker calls replace-match, which raised
"Buffer is read-only" inside toggle, aborting before the collapsed
state was recorded. All marker/overlay mutation now goes through
mattermost--collapse-section with inhibit-read-only bound.
Also deduplicate section rendering into mattermost--insert-section.
fix(mm-client): parse files/info tolerantly and log raw body
Accept array, {file_infos: [...]}, and {order, file_infos: {id: ...}}
shapes; add serde defaults to FileInfo fields. Log body prefix at
debug level to identify the actual Mattermost 10.9.3 response shape.
This commit is contained in:
@@ -34,8 +34,7 @@ impl MattermostClient {
|
||||
.map_err(ClientError::Http)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let err = api_error(response).await;
|
||||
return Err(err);
|
||||
return Err(api_error(response).await);
|
||||
}
|
||||
|
||||
let token = response
|
||||
@@ -84,6 +83,7 @@ impl MattermostClient {
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
/// Batch-fetch users by their IDs.
|
||||
pub async fn get_users_by_ids(
|
||||
&self,
|
||||
token: &str,
|
||||
@@ -174,6 +174,7 @@ impl MattermostClient {
|
||||
Ok(ChannelId::new(ch.id))
|
||||
}
|
||||
|
||||
/// Get posts for a channel. `before` = oldest known post id for pagination.
|
||||
pub async fn get_posts(
|
||||
&self,
|
||||
token: &str,
|
||||
@@ -220,6 +221,9 @@ impl MattermostClient {
|
||||
}
|
||||
|
||||
/// Fetch file metadata for a post.
|
||||
///
|
||||
/// Mattermost has returned different shapes for this endpoint across
|
||||
/// versions, so the body is parsed tolerantly and logged on debug.
|
||||
pub async fn get_post_files(
|
||||
&self,
|
||||
token: &str,
|
||||
@@ -238,14 +242,22 @@ impl MattermostClient {
|
||||
return Err(api_error(response).await);
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct FilesResponse {
|
||||
#[serde(default)]
|
||||
file_infos: Vec<FileInfo>,
|
||||
}
|
||||
let body = response.text().await.map_err(ClientError::Http)?;
|
||||
let prefix: String = body.chars().take(300).collect();
|
||||
tracing::debug!(post_id, body = %prefix, "files/info raw response");
|
||||
|
||||
let data: FilesResponse = response.json().await.map_err(ClientError::Http)?;
|
||||
Ok(data.file_infos)
|
||||
let value: serde_json::Value =
|
||||
serde_json::from_str(&body).map_err(|e| ClientError::Api {
|
||||
id: "decode_error".to_string(),
|
||||
message: format!("{e}; body prefix: {prefix}"),
|
||||
status_code: 200,
|
||||
})?;
|
||||
|
||||
let files = parse_file_infos(&value);
|
||||
if files.is_empty() {
|
||||
tracing::debug!(post_id, body = %prefix, "files/info parsed to zero files");
|
||||
}
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
pub async fn create_post(
|
||||
@@ -284,6 +296,50 @@ impl MattermostClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept `[fileinfo, ...]`, `{file_infos: [...]}`, or
|
||||
/// `{order: [...], file_infos: {id: fileinfo}}`.
|
||||
fn parse_file_infos(value: &serde_json::Value) -> Vec<FileInfo> {
|
||||
fn many(items: &[serde_json::Value]) -> Vec<FileInfo> {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|v| serde_json::from_value::<FileInfo>(v.clone()).ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
match value {
|
||||
serde_json::Value::Array(items) => many(items),
|
||||
serde_json::Value::Object(map) => {
|
||||
let order: Vec<String> = map
|
||||
.get("order")
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|a| {
|
||||
a.iter()
|
||||
.filter_map(|v| v.as_str().map(String::from))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
match map.get("file_infos") {
|
||||
Some(serde_json::Value::Array(items)) => many(items),
|
||||
Some(serde_json::Value::Object(infos)) => {
|
||||
if order.is_empty() {
|
||||
many(&infos.values().cloned().collect::<Vec<_>>())
|
||||
} else {
|
||||
let items: Vec<serde_json::Value> = order
|
||||
.iter()
|
||||
.filter_map(|id| infos.get(id))
|
||||
.cloned()
|
||||
.collect();
|
||||
many(&items)
|
||||
}
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn api_error(response: reqwest::Response) -> ClientError {
|
||||
let status = response.status().as_u16();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
|
||||
@@ -96,8 +96,11 @@ pub struct Channel {
|
||||
/// File metadata attached to a post.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileInfo {
|
||||
#[serde(default)]
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub size: i64,
|
||||
#[serde(default)]
|
||||
pub mime_type: String,
|
||||
|
||||
Reference in New Issue
Block a user