Skip to content

Commit 66b648a

Browse files
committed
api: Add presence event
1 parent 5945711 commit 66b648a

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

lib/api/model/events.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ sealed class Event {
7474
}
7575
case 'submessage': return SubmessageEvent.fromJson(json);
7676
case 'typing': return TypingEvent.fromJson(json);
77+
case 'presence': return PresenceEvent.fromJson(json);
7778
case 'reaction': return ReactionEvent.fromJson(json);
7879
case 'heartbeat': return HeartbeatEvent.fromJson(json);
7980
// TODO add many more event types
@@ -1195,6 +1196,69 @@ enum TypingOp {
11951196
String toJson() => _$TypingOpEnumMap[this]!;
11961197
}
11971198

1199+
/// A Zulip event of type `presence`.
1200+
///
1201+
/// See:
1202+
/// https://zulip.com/api/get-events#presence
1203+
@JsonSerializable(fieldRename: FieldRename.snake)
1204+
class PresenceEvent extends Event {
1205+
@override
1206+
@JsonKey(includeToJson: true)
1207+
String get type => 'presence';
1208+
1209+
final int userId;
1210+
// final String email; // deprecated; ignore
1211+
final int serverTimestamp;
1212+
final Map<String, PerClientPresence> presence;
1213+
1214+
PresenceEvent({
1215+
required super.id,
1216+
required this.userId,
1217+
required this.serverTimestamp,
1218+
required this.presence,
1219+
});
1220+
1221+
factory PresenceEvent.fromJson(Map<String, dynamic> json) =>
1222+
_$PresenceEventFromJson(json);
1223+
1224+
@override
1225+
Map<String, dynamic> toJson() => _$PresenceEventToJson(this);
1226+
}
1227+
1228+
/// A value in [PresenceEvent.presence].
1229+
///
1230+
/// The "per client" name follows the event's structure,
1231+
/// but that structure is already an API wart; see the doc's "Changes" note
1232+
/// on [client] and on the `client_name` key of the map that holds these values:
1233+
///
1234+
/// https://zulip.com/api/get-events#presence
1235+
/// > Starting with Zulip 7.0 (feature level 178), this will always be "website"
1236+
/// > as the server no longer stores which client submitted presence updates.
1237+
///
1238+
/// This will probably be deprecated in favor of a form like [PerUserPresence].
1239+
/// See #1611 and discussion:
1240+
/// https://chat.zulip.org/#narrow/channel/378-api-design/topic/presence.20rewrite/near/2200812
1241+
// TODO(#1611) update comment about #1611
1242+
@JsonSerializable(fieldRename: FieldRename.snake)
1243+
class PerClientPresence {
1244+
final String client; // always "website" (on 7.0+, so on all supported servers)
1245+
final PresenceStatus status;
1246+
final int timestamp;
1247+
final bool pushable; // always false (on 7.0+, so on all supported servers)
1248+
1249+
PerClientPresence({
1250+
required this.client,
1251+
required this.status,
1252+
required this.timestamp,
1253+
required this.pushable,
1254+
});
1255+
1256+
factory PerClientPresence.fromJson(Map<String, dynamic> json) =>
1257+
_$PerClientPresenceFromJson(json);
1258+
1259+
Map<String, dynamic> toJson() => _$PerClientPresenceToJson(this);
1260+
}
1261+
11981262
/// A Zulip event of type `reaction`, with op `add` or `remove`.
11991263
///
12001264
/// See:

lib/api/model/events.g.dart

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api/model/model.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ class PerUserPresence {
350350
Map<String, dynamic> toJson() => _$PerUserPresenceToJson(this);
351351
}
352352

353+
/// As in [PerClientPresence.status].
354+
@JsonEnum(fieldRename: FieldRename.snake, alwaysCreate: true)
355+
enum PresenceStatus {
356+
active,
357+
idle;
358+
359+
String toJson() => _$PresenceStatusEnumMap[this]!;
360+
}
361+
353362
/// An item in `saved_snippets` from the initial snapshot.
354363
///
355364
/// For docs, search for "saved_snippets:"

lib/api/model/model.g.dart

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/model/store.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
952952
assert(debugLog("server event: typing/${event.op} ${event.messageType}"));
953953
typingStatus.handleTypingEvent(event);
954954

955+
case PresenceEvent():
956+
// TODO handle
957+
break;
958+
955959
case ReactionEvent():
956960
assert(debugLog("server event: reaction/${event.op}"));
957961
_messages.handleReactionEvent(event);

0 commit comments

Comments
 (0)