Files
pipelines/app/lib/features/attention/domain/attention_acks.dart
T

111 lines
3.7 KiB
Dart

import 'dart:convert';
import '../../../core/api_gateway.dart';
/// The server-shared "I have seen this" state (`attention_acks` on the wire):
/// dismissed Needs-you identities (`{runId}:{kind}`) and per-chat-session
/// read watermarks in epoch milliseconds.
///
/// The set only grows — dismissals are added and watermarks only advance — so
/// merging is a union/maximum and applying a frame twice or out of order is
/// harmless. The same shape is used for the whole set, a pushed delta and the
/// offline queue of acknowledgements not yet accepted by the server.
class AttentionAcks {
const AttentionAcks({this.dismissed = const {}, this.chatSeen = const {}});
final Set<String> dismissed;
final Map<String, int> chatSeen;
bool get isEmpty => dismissed.isEmpty && chatSeen.isEmpty;
int get length => dismissed.length + chatSeen.length;
/// The union of both sets, keeping the larger watermark per session.
/// Returns this instance when [other] adds nothing.
AttentionAcks merge(AttentionAcks other) {
final addsDismissal = !dismissed.containsAll(other.dismissed);
final advances = other.chatSeen.entries.any(
(entry) => entry.value > (chatSeen[entry.key] ?? 0),
);
if (!addsDismissal && !advances) return this;
final seen = {...chatSeen};
other.chatSeen.forEach((id, at) {
if (at > (seen[id] ?? 0)) seen[id] = at;
});
return AttentionAcks(
dismissed: {...dismissed, ...other.dismissed},
chatSeen: seen,
);
}
/// What remains of this queue after [sent] was accepted: a dismissal is
/// gone once sent, a watermark only when no newer value was queued since.
AttentionAcks without(AttentionAcks sent) => AttentionAcks(
dismissed: dismissed.difference(sent.dismissed),
chatSeen: {
for (final entry in chatSeen.entries)
if (entry.value > (sent.chatSeen[entry.key] ?? 0))
entry.key: entry.value,
},
);
/// The wire body of `ack_attention` / the stored queue.
JsonMap toJson() {
final keys = dismissed.toList()..sort();
return {'dismissed': keys, 'chat_seen': chatSeen};
}
String encode() => jsonEncode(toJson());
/// Total decode of a wire or stored value: unreadable members are skipped
/// and a malformed value is the empty set, never an exception.
static AttentionAcks fromJson(Object? value) {
if (value is! Map) return const AttentionAcks();
final dismissed = value['dismissed'];
final chatSeen = value['chat_seen'];
return AttentionAcks(
dismissed: {
if (dismissed is List)
for (final key in dismissed)
if (key is String && key.isNotEmpty) key,
},
chatSeen: {
if (chatSeen is Map)
for (final entry in chatSeen.entries)
if (entry.key is String &&
entry.value is num &&
(entry.value as num) > 0)
entry.key as String: (entry.value as num).toInt(),
},
);
}
static AttentionAcks decode(String? stored) {
final text = (stored ?? '').trim();
if (text.isEmpty) return const AttentionAcks();
try {
return fromJson(jsonDecode(text));
} catch (_) {
return const AttentionAcks();
}
}
}
/// The server's bound on one `ack_attention`; a larger queue is sent in parts.
const maxAttentionAckItems = 500;
/// The first [limit] items of [acks], dismissals first, for one request.
AttentionAcks attentionAckBatch(
AttentionAcks acks, {
int limit = maxAttentionAckItems,
}) {
if (acks.length <= limit) return acks;
final keys = acks.dismissed.toList()..sort();
final dismissed = keys.take(limit).toSet();
final room = limit - dismissed.length;
return AttentionAcks(
dismissed: dismissed,
chatSeen: Map.fromEntries(acks.chatSeen.entries.take(room)),
);
}