gatewayd: approvals answered from Mattermost; typing paused; the notice

Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
2026-09-24 10:19:41 -07:00
parent af7137dafe
commit edf9ba3515
12 changed files with 472 additions and 25 deletions
+5 -2
View File
@@ -141,7 +141,7 @@ impl Gateway {
Ending::NoLonger => Ended::NoLonger,
Ending::Answered {
by,
reason: _,
reason: refusal,
outcome,
} => {
let by = match self.client.username(&by) {
@@ -151,7 +151,10 @@ impl Gateway {
match outcome {
GatewayOutcome::Runs {} => Ended::Runs { by },
GatewayOutcome::Denied { reason } => Ended::Denied { by, reason },
GatewayOutcome::Refused {} => Ended::Refused { by, reason: None },
GatewayOutcome::Refused {} => Ended::Refused {
by,
reason: refusal,
},
GatewayOutcome::Gone {} => Ended::NoLonger,
}
}
+37 -7
View File
@@ -4,10 +4,11 @@
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::deliver::{LOOP_DOWN, deliver};
use crate::approvals::Judged;
use crate::deliver::{LOOP_DOWN, Notice, deliver};
use crate::mm::{Post, typing};
use crate::serve::{Gateway, Stop};
use crate::sessions::{BUSY, Batch, Ignored, M4B_COMMAND, Pushed, Route};
use crate::sessions::{BUSY, Batch, Ignored, Pushed, Route};
use crate::state::InFlight;
use crate::ws::WsError;
use crate::ws::conn::Ws;
@@ -81,8 +82,26 @@ impl Gateway {
}
Route::Ignore(_) => {}
Route::Reply { thread, text } => self.post(&thread.channel, &thread.root, &text),
// Until the approvals module is wired in (task 15), M4a's notice.
Route::Approval { thread, .. } => self.post(&thread.channel, &thread.root, M4B_COMMAND),
Route::Approval {
thread,
command,
user,
post: command_post,
at,
} => {
let approvers = self.config.approvers();
let judged = self.registry.judge(
&command,
&user,
&thread.root,
&command_post,
at,
approvers,
);
if let Judged::Reply(text) = judged {
self.post(&thread.channel, &thread.root, &text);
}
}
Route::Queue(message) => {
if message.joins_thread {
self.state.join_thread(&message.thread.root)?;
@@ -117,6 +136,7 @@ impl Gateway {
channel: batch.thread.channel.clone(),
root: batch.thread.root.clone(),
})?;
let (registry, wait) = (Arc::clone(&self.registry), self.notice);
let (client, socket, log, done) = (
Arc::clone(&self.client),
self.loop_socket.clone(),
@@ -125,7 +145,12 @@ impl Gateway {
);
let (session, thread) = (batch.session.clone(), batch.thread.clone());
let spawned = std::thread::Builder::new().spawn(move || {
deliver(client.as_ref(), &socket, &batch, &|line| log(line));
let posted = |id: u64| registry.was_posted(id);
let notice = Notice {
posted: &posted,
wait,
};
deliver(client.as_ref(), &socket, &batch, &|line| log(line), &notice);
let _ = done.send(batch.session);
});
if let Err(e) = spawned {
@@ -156,9 +181,14 @@ impl Gateway {
/// Show this bot as typing in every thread with a turn running.
pub(super) fn typing(&mut self, ws: &mut Ws) -> Result<(), WsError> {
// `for thread in self.queues.threads() { self.seq += 1;
// ws.send_text(&typing(self.seq, &thread.channel, &thread.root))?; }` then Ok(()).
// `let waiting = self.registry.waiting_sessions();` then for each thread, none that waits
// for its approval: A turn waiting for its approval is not working: no typing in its thread.
// Otherwise `self.seq += 1; ws.send_text(...)`; then Ok(()).
let waiting = self.registry.waiting_sessions();
for thread in self.queues.threads() {
if waiting.contains(&format!("mm-{}", thread.root)) {
continue;
}
self.seq += 1;
ws.send_text(&typing(self.seq, &thread.channel, &thread.root))?;
}
+8 -1
View File
@@ -77,6 +77,8 @@ pub struct Tuning {
pub poll: Duration,
/// Connecting and each REST read.
pub rest_timeout: Duration,
/// How long a turn waits for its approval to come through `gateway.sock` before the notice.
pub notice: Duration,
}
impl Default for Tuning {
@@ -86,6 +88,7 @@ impl Default for Tuning {
backoff: secs.to_vec(),
poll: Duration::from_millis(200),
rest_timeout: Duration::from_secs(30),
notice: crate::deliver::NOTICE_WAIT,
}
}
}
@@ -112,6 +115,8 @@ pub(crate) struct Gateway {
gateway_rx: Receiver<ToLoop>,
/// The last post of each approval posted here and not yet ended.
open: HashMap<u64, approvals::Open>,
/// How long a turn waits for its approval to come through `gateway.sock` before the notice.
notice: Duration,
}
/// The wait before attempt `n` (from 0) after a loss.
@@ -167,7 +172,7 @@ pub fn run(config: Config, token: Secret, tuning: Tuning, log: Log, stop: &Atomi
return Stop::Start(format!("cannot start the gateway.sock thread: {e}"));
}
let client = Arc::new(Client::new(connector, token, tuning.rest_timeout));
let mut g = Gateway::new(config, client, state, log, gateway_rx);
let mut g = Gateway::new(config, client, state, log, gateway_rx, tuning.notice);
let mut failures = 0;
loop {
if stop.load(Ordering::SeqCst) {
@@ -235,6 +240,7 @@ impl Gateway {
state: State,
log: Log,
gateway_rx: Receiver<ToLoop>,
notice: Duration,
) -> Gateway {
let (done_tx, done_rx) = mpsc::channel();
let limit = usize::try_from(config.limits.queue).unwrap_or(usize::MAX);
@@ -258,6 +264,7 @@ impl Gateway {
registry: Arc::new(Registry::new()),
gateway_rx,
open: HashMap::new(),
notice,
}
}