@@ -2,21 +2,27 @@ import 'package:json_annotation/json_annotation.dart';
2
2
3
3
part 'polls.g.dart' ;
4
4
5
+ // const maxLatestOptionIndex = 1000; // TODO validate
6
+
5
7
/// Data structure seen from [Submessage.content] to support Zulip's widget
6
8
/// system.
7
9
sealed class WidgetData {}
8
10
11
+ /// [WidgetData] that defines the starting state of the poll.
12
+ ///
13
+ /// If a message contains a poll, the first [Submessage] from
14
+ /// [Message.submessages] should always be a [PollStartData] .
9
15
@JsonSerializable (fieldRename: FieldRename .snake)
10
- class WidgetStartData extends WidgetData {
11
- WidgetStartData ({required this .widgetType, required this .extraData});
16
+ class PollStartData extends WidgetData {
17
+ PollStartData ({required this .widgetType, required this .extraData});
12
18
13
19
final WidgetType widgetType;
14
20
final PollExtraData extraData;
15
21
16
- factory WidgetStartData .fromJson (Map <String , dynamic > json) =>
17
- _$WidgetStartDataFromJson (json);
22
+ factory PollStartData .fromJson (Map <String , dynamic > json) =>
23
+ _$PollStartDataFromJson (json);
18
24
19
- Map <String , dynamic > toJson () => _$WidgetStartDataToJson (this );
25
+ Map <String , dynamic > toJson () => _$PollStartDataToJson (this );
20
26
}
21
27
22
28
@JsonSerializable (fieldRename: FieldRename .snake)
@@ -55,18 +61,21 @@ sealed class PollEvent extends WidgetData {
55
61
Map <String , dynamic > toJson ();
56
62
}
57
63
64
+ /// Event when an option is added to the poll.
58
65
@JsonSerializable (fieldRename: FieldRename .snake)
59
66
class PollOptionEvent extends PollEvent {
60
67
@override
61
68
@JsonKey (includeToJson: true )
62
69
PollEventType get type => PollEventType .newOption;
63
70
64
71
final String option;
65
- final int idx;
72
+ /// The index of the latest option added by a specific user.
73
+ @JsonKey (name: 'idx' )
74
+ final int latestOptionIndex;
66
75
67
76
PollOptionEvent ({
68
77
required this .option,
69
- required this .idx ,
78
+ required this .latestOptionIndex ,
70
79
});
71
80
72
81
@override
@@ -77,6 +86,7 @@ class PollOptionEvent extends PollEvent {
77
86
Map <String , dynamic > toJson () => _$PollOptionEventToJson (this );
78
87
}
79
88
89
+ /// Event when the question of the poll has been edited.
80
90
@JsonSerializable (fieldRename: FieldRename .snake)
81
91
class PollQuestionEvent extends PollEvent {
82
92
@override
@@ -97,18 +107,22 @@ class PollQuestionEvent extends PollEvent {
97
107
Map <String , dynamic > toJson () => _$PollQuestionEventToJson (this );
98
108
}
99
109
110
+ /// Event when a vote has been casted or removed.
100
111
@JsonSerializable (fieldRename: FieldRename .snake)
101
112
class PollVoteEvent extends PollEvent {
102
113
@override
103
114
@JsonKey (includeToJson: true )
104
115
PollEventType get type => PollEventType .vote;
105
116
117
+ /// Key corresponding to an option in the `'${sender_id},{latestOptionIndex}'`
118
+ /// format.
106
119
final String key;
107
- final int vote;
120
+ @JsonKey (name: 'vote' , unknownEnumValue: VoteOp .unknown)
121
+ final VoteOp op;
108
122
109
123
PollVoteEvent ({
110
124
required this .key,
111
- required this .vote ,
125
+ required this .op ,
112
126
});
113
127
114
128
@override
@@ -119,6 +133,19 @@ class PollVoteEvent extends PollEvent {
119
133
Map <String , dynamic > toJson () => _$PollVoteEventToJson (this );
120
134
}
121
135
136
+ @JsonEnum (valueField: 'apiValue' )
137
+ enum VoteOp {
138
+ add (apiValue: 1 ),
139
+ remove (apiValue: - 1 ),
140
+ unknown (apiValue: null );
141
+
142
+ const VoteOp ({required this .apiValue});
143
+
144
+ final int ? apiValue;
145
+
146
+ int ? toJson () => apiValue;
147
+ }
148
+
122
149
class UnknownPollEvent extends PollEvent {
123
150
@override
124
151
@JsonKey (includeToJson: true )
0 commit comments