Line data Source code
1 : import 'dart:convert';
2 :
3 : /// Push Notification object from https://spec.matrix.org/v1.2/push-gateway-api/
4 : class PushNotification {
5 : final Map<String, Object?>? content;
6 : final PushNotificationCounts? counts;
7 : final List<PushNotificationDevice>? devices;
8 : final String? eventId;
9 : final String? prio;
10 : final String? roomAlias;
11 : final String? roomId;
12 : final String? roomName;
13 : final String? sender;
14 : final String? senderDisplayName;
15 : final String? type;
16 :
17 1 : const PushNotification({
18 : this.content,
19 : this.counts,
20 : this.devices,
21 : this.eventId,
22 : this.prio,
23 : this.roomAlias,
24 : this.roomId,
25 : this.roomName,
26 : this.sender,
27 : this.senderDisplayName,
28 : this.type,
29 : });
30 :
31 : /// Generate a Push Notification object from JSON. It also supports a
32 : /// `map<String, String>` which usually comes from Firebase Cloud Messaging.
33 0 : factory PushNotification.fromJson(Map<String, Object?> json) =>
34 0 : PushNotification(
35 0 : content: json['content'] is Map
36 0 : ? Map<String, Object?>.from(json['content'] as Map)
37 0 : : json['content'] is String
38 0 : ? jsonDecode(json['content'] as String)
39 : : null,
40 0 : counts: json['counts'] is Map
41 0 : ? PushNotificationCounts.fromJson(
42 0 : json['counts'] as Map<String, Object?>,
43 : )
44 0 : : json['counts'] is String
45 0 : ? PushNotificationCounts.fromJson(
46 0 : jsonDecode(json['counts'] as String),
47 : )
48 : : null,
49 0 : devices: json['devices'] is List
50 0 : ? (json['devices'] as List)
51 0 : .map((d) => PushNotificationDevice.fromJson(d))
52 0 : .toList()
53 0 : : json['devices'] is String
54 0 : ? (jsonDecode(json['devices'] as String) as List)
55 0 : .map((d) => PushNotificationDevice.fromJson(d))
56 0 : .toList()
57 : : null,
58 0 : eventId: json['event_id'] as String?,
59 0 : prio: json['prio'] as String?,
60 0 : roomAlias: json['room_alias'] as String?,
61 0 : roomId: json['room_id'] as String?,
62 0 : roomName: json['room_name'] as String?,
63 0 : sender: json['sender'] as String?,
64 0 : senderDisplayName: json['sender_display_name'] as String?,
65 0 : type: json['type'] as String?,
66 : );
67 :
68 0 : Map<String, Object?> toJson() => {
69 0 : if (content != null) 'content': content,
70 0 : if (counts != null) 'counts': counts?.toJson(),
71 0 : if (devices != null)
72 0 : 'devices': devices?.map((i) => i.toJson()).toList(),
73 0 : if (eventId != null) 'event_id': eventId,
74 0 : if (prio != null) 'prio': prio,
75 0 : if (roomAlias != null) 'room_alias': roomAlias,
76 0 : if (roomId != null) 'room_id': roomId,
77 0 : if (roomName != null) 'room_name': roomName,
78 0 : if (sender != null) 'sender': sender,
79 0 : if (senderDisplayName != null) 'sender_display_name': senderDisplayName,
80 0 : if (type != null) 'type': type,
81 : };
82 : }
83 :
84 : class PushNotificationCounts {
85 : final int? missedCalls;
86 : final int? unread;
87 :
88 0 : const PushNotificationCounts({
89 : this.missedCalls,
90 : this.unread,
91 : });
92 :
93 0 : factory PushNotificationCounts.fromJson(Map<String, Object?> json) =>
94 0 : PushNotificationCounts(
95 0 : missedCalls: json['missed_calls'] as int?,
96 0 : unread: json['unread'] as int?,
97 : );
98 :
99 0 : Map<String, Object?> toJson() => {
100 0 : if (missedCalls != null) 'missed_calls': missedCalls,
101 0 : if (unread != null) 'unread': unread,
102 : };
103 : }
104 :
105 : class PushNotificationDevice {
106 : final String? appId;
107 : final Map<String, Object?>? data;
108 : final String? pushkey;
109 : final int? pushkeyTs;
110 : final Tweaks? tweaks;
111 :
112 0 : const PushNotificationDevice({
113 : this.appId,
114 : this.data,
115 : this.pushkey,
116 : this.pushkeyTs,
117 : this.tweaks,
118 : });
119 :
120 0 : factory PushNotificationDevice.fromJson(Map<String, Object?> json) =>
121 0 : PushNotificationDevice(
122 0 : appId: json['app_id'] as String?,
123 0 : data: json['data'] == null
124 : ? null
125 0 : : Map<String, Object?>.from(json['data'] as Map),
126 0 : pushkey: json['pushkey'] as String?,
127 0 : pushkeyTs: json['pushkey_ts'] as int?,
128 0 : tweaks: json['tweaks'] == null
129 : ? null
130 0 : : Tweaks.fromJson(json['tweaks'] as Map<String, Object?>),
131 : );
132 :
133 0 : Map<String, Object?> toJson() => {
134 0 : 'app_id': appId,
135 0 : if (data != null) 'data': data,
136 0 : 'pushkey': pushkey,
137 0 : if (pushkeyTs != null) 'pushkey_ts': pushkeyTs,
138 0 : if (tweaks != null) 'tweaks': tweaks?.toJson(),
139 : };
140 : }
141 :
142 : class Tweaks {
143 : final String? sound;
144 :
145 0 : const Tweaks({
146 : this.sound,
147 : });
148 :
149 0 : factory Tweaks.fromJson(Map<String, Object?> json) => Tweaks(
150 0 : sound: json['sound'] as String?,
151 : );
152 :
153 0 : Map<String, Object?> toJson() => {
154 0 : if (sound != null) 'sound': sound,
155 : };
156 : }
|