Files
Client/lib/api/marianumcloud/talk/room/get_room_response.dart
T
MineTecandClaude Fable 5.1 279b973e89 hardened API decoding against server fields the models did not expect
The Talk room list crashed for a new account because the server omits
lastMessage for rooms without a visible message and the model required it.
Beyond that single field, a review of the Talk models against the OpenAPI
spec of the installed version (23.0.8) found the same crash class in several
places: closed enums over open server values (inCall bitmask, rich-object
types, actor and message types), a deleted parent sent as {id, deleted},
and the autocomplete status arriving as an object.

- json_serializable `checked: true`: decode errors now name class and field
- lists (rooms, messages, participants, autocomplete) decode per entry;
  a malformed entry is skipped and reported via ClientErrorReporter
- unknownEnumValue fallbacks for all response enums of Talk and breakers
- lastMessage nullable; inCall as bitmask; deleted parent placeholder;
  autocomplete status object/string; fixed shareWithDisplayNameUnique key
- GetRoomParams no longer sends null query parameters

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HrepDTL4qDe35TuS29sLW3
2026-09-29 16:35:41 +02:00

195 lines
4.6 KiB
Dart

import 'package:json_annotation/json_annotation.dart';
import '../../../api_response.dart';
import '../../../json_lenient.dart';
import '../chat/get_chat_response.dart';
part 'get_room_response.g.dart';
@JsonSerializable(explicitToJson: true)
class GetRoomResponse extends ApiResponse {
@JsonKey(fromJson: _roomsFromJson)
Set<GetRoomResponseObject> data;
GetRoomResponse(this.data);
factory GetRoomResponse.fromJson(Map<String, dynamic> json) =>
_$GetRoomResponseFromJson(json);
Map<String, dynamic> toJson() => _$GetRoomResponseToJson(this);
List<GetRoomResponseObject> sortBy({
required bool favoritesToTop,
required bool unreadToTop,
}) {
return data.toList()..sort((a, b) {
if (favoritesToTop && a.isFavorite != b.isFavorite) {
return a.isFavorite ? -1 : 1;
}
if (unreadToTop) {
final aUnread = a.unreadMessages > 0;
final bUnread = b.unreadMessages > 0;
if (aUnread != bUnread) return aUnread ? -1 : 1;
}
return b.lastActivity.compareTo(a.lastActivity);
});
}
}
Set<GetRoomResponseObject> _roomsFromJson(Object? raw) =>
decodeSetLenient(raw, GetRoomResponseObject.fromJson, what: 'room');
@JsonSerializable(explicitToJson: true)
class GetRoomResponseObject {
int id;
String token;
@JsonKey(unknownEnumValue: GetRoomResponseObjectConversationType.unknown)
GetRoomResponseObjectConversationType type;
String name;
String displayName;
String description;
int participantType;
int participantFlags;
int readOnly;
int listable;
int lastPing;
String sessionId;
bool hasPassword;
bool hasCall;
int callFlag;
bool canStartCall;
bool canDeleteConversation;
bool canLeaveConversation;
int lastActivity;
bool isFavorite;
@JsonKey(
unknownEnumValue:
GetRoomResponseObjectParticipantNotificationLevel.defaultLevel,
)
GetRoomResponseObjectParticipantNotificationLevel notificationLevel;
int unreadMessages;
bool unreadMention;
bool unreadMentionDirect;
int lastReadMessage;
int lastCommonReadMessage;
/// Omitted by the server for rooms without a visible message (empty room,
/// last message expired or hidden from this user, federated conversation).
GetChatResponseObject? lastMessage;
String? status;
String? statusIcon;
String? statusMessage;
GetRoomResponseObject(
this.id,
this.token,
this.type,
this.name,
this.displayName,
this.description,
this.participantType,
this.participantFlags,
this.readOnly,
this.listable,
this.lastPing,
this.sessionId,
this.hasPassword,
this.hasCall,
this.callFlag,
this.canStartCall,
this.canDeleteConversation,
this.canLeaveConversation,
this.lastActivity,
this.isFavorite,
this.notificationLevel,
this.unreadMessages,
this.unreadMention,
this.unreadMentionDirect,
this.lastReadMessage,
this.lastCommonReadMessage,
this.lastMessage,
this.status,
this.statusIcon,
this.statusMessage,
);
factory GetRoomResponseObject.fromJson(Map<String, dynamic> json) =>
_$GetRoomResponseObjectFromJson(json);
Map<String, dynamic> toJson() => _$GetRoomResponseObjectToJson(this);
}
enum GetRoomResponseObjectConversationType {
@JsonValue(1)
oneToOne,
@JsonValue(2)
group,
@JsonValue(3)
public,
@JsonValue(4)
changelog,
@JsonValue(5)
deleted,
@JsonValue(6)
noteToSelf,
/// Fallback for types this app does not know yet; treated like a group.
unknown,
}
enum GetRoomResponseObjectParticipantNotificationLevel {
@JsonValue(0)
defaultLevel,
@JsonValue(1)
alwaysNotify,
@JsonValue(2)
notifyOnMention,
@JsonValue(3)
neverNotify,
}
extension NotificationLevelApiValue
on GetRoomResponseObjectParticipantNotificationLevel {
int get apiValue =>
_$GetRoomResponseObjectParticipantNotificationLevelEnumMap[this]!;
}
enum GetRoomResponseObjectMessageActorType {
@JsonValue('deleted_users')
deletedUsers,
@JsonValue('users')
user,
@JsonValue('guests')
guest,
@JsonValue('bots')
bot,
@JsonValue('bridged')
bridge,
@JsonValue('groups')
group,
@JsonValue('emails')
email,
@JsonValue('circles')
circle,
@JsonValue('federated_users')
federatedUser,
@JsonValue('phones')
phone,
unknown,
}
enum GetRoomResponseObjectMessageType {
@JsonValue('comment')
comment,
@JsonValue('voice-message')
voiceMessage,
@JsonValue('comment_deleted')
deletedComment,
@JsonValue('system')
system,
@JsonValue('command')
command,
/// Fallback for types this app does not know yet (e.g. call recordings);
/// rendered like a plain comment.
unknown,
}