SimpleCloudNotifier/flutter/lib/models/scn_message.dart

101 lines
2.9 KiB
Dart
Raw Permalink Normal View History

import 'package:hive_flutter/hive_flutter.dart';
import 'package:simplecloudnotifier/state/interfaces.dart';
2024-06-15 21:29:51 +02:00
part 'scn_message.g.dart';
@HiveType(typeId: 105)
2024-06-15 21:29:51 +02:00
class SCNMessage extends HiveObject implements FieldDebuggable {
@HiveField(0)
2024-02-18 17:36:58 +01:00
final String messageID;
@HiveField(10)
2024-02-18 17:36:58 +01:00
final String senderUserID;
@HiveField(11)
2024-02-18 17:36:58 +01:00
final String channelInternalName;
@HiveField(12)
2024-02-18 17:36:58 +01:00
final String channelID;
@HiveField(13)
2024-02-18 17:36:58 +01:00
final String? senderName;
@HiveField(14)
2024-02-18 17:36:58 +01:00
final String senderIP;
@HiveField(15)
2024-02-18 17:36:58 +01:00
final String timestamp;
@HiveField(16)
2024-02-18 17:36:58 +01:00
final String title;
@HiveField(17)
2024-02-18 17:36:58 +01:00
final String? content;
@HiveField(18)
2024-02-18 17:36:58 +01:00
final int priority;
@HiveField(19)
2024-02-18 17:36:58 +01:00
final String? userMessageID;
@HiveField(20)
2024-02-18 17:36:58 +01:00
final String usedKeyID;
@HiveField(21)
2024-02-18 17:36:58 +01:00
final bool trimmed;
2024-06-15 21:29:51 +02:00
SCNMessage({
2024-02-18 17:36:58 +01:00
required this.messageID,
required this.senderUserID,
required this.channelInternalName,
required this.channelID,
required this.senderName,
required this.senderIP,
required this.timestamp,
required this.title,
required this.content,
required this.priority,
required this.userMessageID,
required this.usedKeyID,
required this.trimmed,
});
2024-06-15 21:29:51 +02:00
factory SCNMessage.fromJson(Map<String, dynamic> json) {
return SCNMessage(
2024-05-25 21:36:05 +02:00
messageID: json['message_id'] as String,
senderUserID: json['sender_user_id'] as String,
channelInternalName: json['channel_internal_name'] as String,
channelID: json['channel_id'] as String,
2024-05-26 00:20:25 +02:00
senderName: json['sender_name'] as String?,
2024-05-25 21:36:05 +02:00
senderIP: json['sender_ip'] as String,
timestamp: json['timestamp'] as String,
title: json['title'] as String,
2024-05-26 00:20:25 +02:00
content: json['content'] as String?,
2024-05-25 21:36:05 +02:00
priority: json['priority'] as int,
2024-05-26 00:20:25 +02:00
userMessageID: json['usr_message_id'] as String?,
2024-05-25 21:36:05 +02:00
usedKeyID: json['used_key_id'] as String,
trimmed: json['trimmed'] as bool,
2024-05-25 18:09:39 +02:00
);
}
2024-06-15 21:29:51 +02:00
static (String, List<SCNMessage>) fromPaginatedJsonArray(Map<String, dynamic> data, String keyMessages, String keyToken) {
2024-05-25 18:09:39 +02:00
final npt = data[keyToken] as String;
2024-06-15 21:29:51 +02:00
final messages = (data[keyMessages] as List<dynamic>).map<SCNMessage>((e) => SCNMessage.fromJson(e as Map<String, dynamic>)).toList();
2024-05-25 18:09:39 +02:00
return (npt, messages);
2024-02-18 17:36:58 +01:00
}
@override
String toString() {
return 'Message[${this.messageID}]';
}
List<(String, String)> debugFieldList() {
return [
('messageID', this.messageID),
('senderUserID', this.senderUserID),
('channelInternalName', this.channelInternalName),
('channelID', this.channelID),
('senderName', this.senderName ?? ''),
('senderIP', this.senderIP),
('timestamp', this.timestamp),
('title', this.title),
('content', this.content ?? ''),
('priority', '${this.priority}'),
('userMessageID', this.userMessageID ?? ''),
('usedKeyID', this.usedKeyID),
('trimmed', '${this.trimmed}'),
];
}
2024-02-18 17:36:58 +01:00
}