Skip to content
Closed
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions docs/kiingo-fork-inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,18 @@
"mobile/lib/shared/community/community.dart",
"mobile/test/shared/community/push_default_on_test.dart"
]
},
{
"classification": "retain-generic",
"owner": "Buzz messaging maintainers",
"paths": [
"mobile/lib/features/channels/channel_typing_indicator.dart",
"mobile/lib/features/channels/channel_typing_provider.dart",
"mobile/test/features/channels/channel_typing_provider_test.dart"
],
"purpose": "Typing status phases on mobile: reads the optional {\"v\":1,\"phase\":...} content of kind:20002 typing events and shows the sender's status line (starting up, warming up a session, reconnecting, working, typing); unknown or malformed payloads stay plain typing and \"idle\" ends that sender's typing at once. Typing and the replies that clear it are keyed by the reply e-tag, matching desktop. Kiingo M-4017.",
"removalCondition": "Drop when upstream reads typing status phases and scopes mobile typing by the reply tag, or Kiingo agents stop sending phases.",
"upstreamStatus": "Generic typing-status extension (backward-compatible content field); not yet submitted to block/buzz."
}
],
"budgetAmendments": [
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/features/channels/channel_typing_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ChannelTypingIndicator extends ConsumerWidget {
for (final entry in entries) labels[entry.pubkey.toLowerCase()]!,
];
final text = switch (names.length) {
1 => '${names[0]} is typing…',
1 => '${names[0]} ${typingPhaseSentence(entries.first.phase)}',
2 => '${names[0]} and ${names[1]} are typing…',
_ => '${names[0]} and ${names.length - 1} others are typing…',
};
Expand Down
50 changes: 48 additions & 2 deletions mobile/lib/features/channels/channel_typing_provider.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../shared/relay/relay.dart';

/// Status an agent can attach to its typing pulse (`{"v":1,"phase":...}`
/// in the ephemeral kind:20002 content). Absent or unknown means plain
/// "is typing…"; `idle` ends the sender's typing at once.
enum TypingPhase { starting, warming, reconnecting, working, typing, idle }

/// Parses a typing pulse's optional status payload.
TypingPhase? parseTypingPhase(String content) {
if (content.isEmpty || content.length > 512) return null;
try {
final value = jsonDecode(content);
if (value is! Map || value['v'] != 1) return null;
final phase = value['phase'];
if (phase is! String) return null;
for (final candidate in TypingPhase.values) {
if (candidate.name == phase) return candidate;
}
} on FormatException {
return null;
}
return null;
}

/// The status-line words after the sender's name, e.g. "is starting up…".
String typingPhaseSentence(TypingPhase? phase) => switch (phase) {
TypingPhase.starting => 'is starting up…',
TypingPhase.warming => 'is warming up a session…',
TypingPhase.reconnecting => 'is reconnecting…',
TypingPhase.working => 'is working…',
_ => 'is typing…',
};

/// A single typing indicator entry.
@immutable
class TypingEntry {
final String pubkey;
final String? threadHeadId;
final int expiresAtMs;

/// The latest pulse's status; null is plain "is typing…".
final TypingPhase? phase;

const TypingEntry({
required this.pubkey,
this.threadHeadId,
required this.expiresAtMs,
this.phase,
});
}

Expand Down Expand Up @@ -131,14 +167,22 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
final now = DateTime.now().millisecondsSinceEpoch;
final at = event.createdAt * 1000;
if (event.channelId != channelId || at > now || at + _ttlMs <= now) return;
final scope = (event.pubkey.toLowerCase(), event.threadReference.rootId);
// Typing and replies are scoped to the message they answer (the `reply`
// e-tag), exactly as desktop and the thread view key them.
final scope = (event.pubkey.toLowerCase(), event.threadReference.parentId);
_activity.removeWhere((_, value) => value.retainUntil <= now);
if (!_activity.containsKey(scope) && _activity.length >= 1024) return;
final record = _activity.putIfAbsent(scope, _TypingActivity.new);
if (event.kind == EventKind.typingIndicator) {
if (at <= record.lastActivity || at <= record.lastMessage) return;
record.lastActivity = at;
if (now >= record.quietUntil) record.visibleUntil = at + _ttlMs;
final phase = parseTypingPhase(event.content);
if (phase == TypingPhase.idle) {
record.visibleUntil = 0;
} else {
record.phase = phase;
if (now >= record.quietUntil) record.visibleUntil = at + _ttlMs;
}
} else {
if (at <= record.lastMessage) return;
record.lastMessage = at;
Expand Down Expand Up @@ -168,6 +212,7 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
pubkey: entry.key.$1,
threadHeadId: entry.key.$2,
expiresAtMs: entry.value.visibleUntil,
phase: entry.value.phase,
),
];
if (_activity.isEmpty) {
Expand All @@ -187,6 +232,7 @@ class _TypingActivity {
int lastMessage = -1;
int visibleUntil = 0;
int quietUntil = 0;
TypingPhase? phase;
int get retainUntil {
final expiry =
(lastActivity > lastMessage ? lastActivity : lastMessage) + 8000;
Expand Down
58 changes: 54 additions & 4 deletions mobile/test/features/channels/channel_typing_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,20 @@ void main() {
container.read(provider);
await Future<void>.value();
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
void emit(int kind, String agent, String root, int time) => relay.receive(
// Scope is the message the pulse or reply answers (its `reply` e-tag),
// as on desktop and in the thread view; the thread root does not split it.
void emit(int kind, String agent, String head, int time) => relay.receive(
NostrEvent(
id: '$kind-$agent-$root-$time',
id: '$kind-$agent-$head-$time',
pubkey: agent,
kind: kind,
createdAt: time,
content: '',
sig: '',
tags: [
['h', 'chat'],
['e', root, '', 'root'],
['e', threadIdParent, '', 'reply'],
['e', threadIdParent, '', 'root'],
['e', head, '', 'reply'],
],
),
);
Expand Down Expand Up @@ -206,4 +208,52 @@ void main() {
expect(container.read(provider), hasLength(2));
},
);
test('typing status phases label the pulse; idle ends it at once', () async {
final relay = _Relay();
final container = ProviderContainer(
overrides: [relaySessionProvider.overrideWith(() => relay)],
);
addTearDown(container.dispose);
final provider = channelTypingProvider('chat');
container.read(provider);
await Future<void>.value();
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
void pulse(String content, int time) => relay.receive(
NostrEvent(
id: 'pulse-$time-${content.hashCode}',
pubkey: 'agent',
kind: 20002,
createdAt: time,
content: content,
sig: '',
tags: [
['h', 'chat'],
],
),
);
pulse('{"v":1,"phase":"starting"}', now - 3);
expect(container.read(provider).single.phase, TypingPhase.starting);
pulse('{"v":1,"phase":"working"}', now - 2);
expect(container.read(provider).single.phase, TypingPhase.working);
pulse('{"v":1,"phase":"idle"}', now - 1);
expect(container.read(provider), isEmpty);
pulse('', now);
expect(container.read(provider).single.phase, isNull);
});

test('unknown or malformed status is plain typing', () {
expect(parseTypingPhase(''), isNull);
expect(parseTypingPhase('not json'), isNull);
expect(parseTypingPhase('{"v":2,"phase":"working"}'), isNull);
expect(parseTypingPhase('{"v":1,"phase":"dancing"}'), isNull);
expect(typingPhaseSentence(null), 'is typing…');
expect(typingPhaseSentence(TypingPhase.starting), 'is starting up…');
expect(
typingPhaseSentence(TypingPhase.warming),
'is warming up a session…',
);
expect(typingPhaseSentence(TypingPhase.reconnecting), 'is reconnecting…');
expect(typingPhaseSentence(TypingPhase.working), 'is working…');
expect(typingPhaseSentence(TypingPhase.typing), 'is typing…');
});
}