Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions crates/openfang-channels/src/nextcloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! headers.

use crate::types::{
split_message, ChannelAdapter, ChannelContent, ChannelMessage, ChannelType, ChannelUser,
ChannelAdapter, ChannelContent, ChannelMessage, ChannelType, ChannelUser, split_message,
};
use async_trait::async_trait;
use chrono::Utc;
Expand All @@ -15,7 +15,7 @@ use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{mpsc, watch, RwLock};
use tokio::sync::{RwLock, mpsc, watch};
use tracing::{info, warn};
use zeroize::Zeroizing;

Expand All @@ -25,6 +25,9 @@ const MAX_MESSAGE_LEN: usize = 32000;
/// Polling interval in seconds for the chat endpoint.
const POLL_INTERVAL_SECS: u64 = 3;

/// Long-poll timeout in seconds for Nextcloud chat polling.
const CHAT_POLL_TIMEOUT_SECS: u64 = 10;

/// Nextcloud Talk channel adapter using OCS REST API with polling.
///
/// Polls the Nextcloud Talk chat endpoint for new messages and sends replies
Expand Down Expand Up @@ -92,6 +95,13 @@ impl NextcloudAdapter {
Ok(user_id)
}

fn build_chat_poll_url_for_server(server_url: &str, room_token: &str, last_id: i64) -> String {
format!(
"{}/ocs/v2.php/apps/spreed/api/v1/chat/{}?lookIntoFuture=1&timeout={}&limit=100&lastKnownMessageId={}",
server_url, room_token, CHAT_POLL_TIMEOUT_SECS, last_id
)
}

/// Fetch the list of joined rooms from the Nextcloud Talk API.
#[allow(dead_code)]
async fn fetch_rooms(&self) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -258,18 +268,19 @@ impl ChannelAdapter for NextcloudAdapter {
ids.get(room_token).copied().unwrap_or(0)
};

// Use lookIntoFuture=1 and lastKnownMessageId for incremental polling
let url = format!(
"{}/ocs/v2.php/apps/spreed/api/v4/room/{}/chat?format=json&lookIntoFuture=1&limit=100&lastKnownMessageId={}",
server_url, room_token, last_id
// Use the documented v1 chat endpoint for incremental long polling.
let url = NextcloudAdapter::build_chat_poll_url_for_server(
&server_url,
room_token,
last_id,
);

let resp = match client
.get(&url)
.header("Authorization", format!("Bearer {}", token.as_str()))
.header("OCS-APIRequest", "true")
.header("Accept", "application/json")
.timeout(Duration::from_secs(30))
.timeout(Duration::from_secs(CHAT_POLL_TIMEOUT_SECS))
.send()
.await
{
Expand Down Expand Up @@ -506,4 +517,18 @@ mod tests {
);
assert_eq!(adapter.token.as_str(), "secret-token-value");
}

#[test]
fn test_nextcloud_chat_poll_url_uses_documented_v1_endpoint() {
let adapter = NextcloudAdapter::new(
"https://cloud.example.com/".to_string(),
"tok".to_string(),
vec![],
);

assert_eq!(
NextcloudAdapter::build_chat_poll_url_for_server(&adapter.server_url, "room-token", 42,),
"https://cloud.example.com/ocs/v2.php/apps/spreed/api/v1/chat/room-token?lookIntoFuture=1&timeout=10&limit=100&lastKnownMessageId=42"
);
}
}