Files

119 lines
3.4 KiB
Dart

import 'package:social_app/data/network/i_api_client.dart';
class InboxApi {
final IApiClient _client;
static const _prefix = '/api/v1/inbox/messages';
InboxApi(this._client);
Future<List<InboxMessageResponse>> getMessages({bool? isRead}) async {
final queryParams = isRead != null ? '?is_read=$isRead' : '';
final response = await _client.get('$_prefix$queryParams');
final List<dynamic> data = response.data;
return data.map((json) => InboxMessageResponse.fromJson(json)).toList();
}
Future<InboxMessageResponse> markAsRead(String messageId) async {
final response = await _client.patch('$_prefix/$messageId/read');
return InboxMessageResponse.fromJson(response.data);
}
Future<Stream<String>> streamEvents({String? lastEventId}) {
final headers = <String, String>{'Accept': 'text/event-stream'};
if (lastEventId != null && lastEventId.isNotEmpty) {
headers['Last-Event-ID'] = lastEventId;
}
return _client.getSseLines('$_prefix/stream', headers: headers);
}
}
class InboxMessageResponse {
final String id;
final String recipientId;
final String? senderId;
final InboxMessageType messageType;
final String? scheduleItemId;
final String? friendshipId;
final Map<String, dynamic>? content;
final bool isRead;
final InboxMessageStatus status;
final DateTime createdAt;
InboxMessageResponse({
required this.id,
required this.recipientId,
this.senderId,
required this.messageType,
this.scheduleItemId,
this.friendshipId,
this.content,
required this.isRead,
required this.status,
required this.createdAt,
});
factory InboxMessageResponse.fromJson(Map<String, dynamic> json) {
return InboxMessageResponse(
id: json['id'] as String,
recipientId: json['recipient_id'] as String,
senderId: json['sender_id'] as String?,
messageType: InboxMessageType.fromJson(json['message_type'] as String),
scheduleItemId: json['schedule_item_id'] as String?,
friendshipId: json['friendship_id'] as String?,
content: json['content'] as Map<String, dynamic>?,
isRead: json['is_read'] as bool,
status: InboxMessageStatus.fromJson(json['status'] as String),
createdAt: DateTime.parse(json['created_at'] as String),
);
}
InboxMessageResponse copyWith({bool? isRead}) {
return InboxMessageResponse(
id: id,
recipientId: recipientId,
senderId: senderId,
messageType: messageType,
scheduleItemId: scheduleItemId,
friendshipId: friendshipId,
content: content,
isRead: isRead ?? this.isRead,
status: status,
createdAt: createdAt,
);
}
}
enum InboxMessageType {
friendRequest('friend_request'),
calendar('calendar'),
system('system'),
group('group');
final String value;
const InboxMessageType(this.value);
static InboxMessageType fromJson(String json) {
return InboxMessageType.values.firstWhere(
(e) => e.value == json,
orElse: () => InboxMessageType.system,
);
}
}
enum InboxMessageStatus {
pending('pending'),
accepted('accepted'),
rejected('rejected'),
dismissed('dismissed');
final String value;
const InboxMessageStatus(this.value);
static InboxMessageStatus fromJson(String json) {
return InboxMessageStatus.values.firstWhere(
(e) => e.value == json,
orElse: () => InboxMessageStatus.pending,
);
}
}