|
| 1 | +import 'dart:convert'; |
| 2 | + |
1 | 3 | import 'package:json_annotation/json_annotation.dart';
|
2 | 4 |
|
| 5 | +import '../../log.dart'; |
| 6 | + |
3 | 7 | part 'submessage.g.dart';
|
4 | 8 |
|
5 | 9 | /// Data used for Zulip "widgets" within messages, like polls and todo lists.
|
@@ -38,7 +42,7 @@ class Submessage {
|
38 | 42 | // We cannot parse the String into one of the [SubmessageData] classes because
|
39 | 43 | // information from other submessages are required. Specifically, we need:
|
40 | 44 | // * the index of this submessage in [Message.submessages];
|
41 |
| - // * the [WidgetType] of the first [Message.submessages]. |
| 45 | + // * the parsed [WidgetData] from the first [Message.submessages]. |
42 | 46 | final String content;
|
43 | 47 |
|
44 | 48 | factory Submessage.fromJson(Map<String, Object?> json) =>
|
@@ -313,3 +317,126 @@ class UnknownPollEventSubmessage extends PollEventSubmessage {
|
313 | 317 | @override
|
314 | 318 | Map<String, Object?> toJson() => json;
|
315 | 319 | }
|
| 320 | + |
| 321 | +/// States of a poll Zulip widget. |
| 322 | +/// |
| 323 | +/// See also: |
| 324 | +/// - https://zulip.com/help/create-a-poll |
| 325 | +/// - https://github.com/zulip/zulip/blob/304d948416465c1a085122af5d752f03d6797003/web/shared/src/poll_data.ts |
| 326 | +class Poll { |
| 327 | + Poll({ |
| 328 | + required this.pollSenderId, |
| 329 | + required this.question, |
| 330 | + required final List<String> options, |
| 331 | + }) { |
| 332 | + for (int index = 0; index < options.length; index += 1) { |
| 333 | + // Initial poll options use a placeholder senderId. |
| 334 | + // See [PollEventSubmessage.optionKey] for details. |
| 335 | + _addOption(null, options[index], idx: index); |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + factory Poll.fromSubmessages( |
| 340 | + List<Submessage> submessages, |
| 341 | + {required int senderId, required PollWidgetData widgetData} |
| 342 | + ) { |
| 343 | + assert(submessages.isNotEmpty); |
| 344 | + final widgetEventSubmessages = submessages.skip(1); |
| 345 | + |
| 346 | + final poll = Poll( |
| 347 | + pollSenderId: senderId, |
| 348 | + question: widgetData.extraData.question, |
| 349 | + options: widgetData.extraData.options, |
| 350 | + ); |
| 351 | + |
| 352 | + for (final submessage in widgetEventSubmessages) { |
| 353 | + final event = PollEventSubmessage.fromJson(jsonDecode(submessage.content) as Map<String, Object?>); |
| 354 | + poll.applyEvent(submessage.senderId, event); |
| 355 | + } |
| 356 | + return poll; |
| 357 | + } |
| 358 | + |
| 359 | + final int pollSenderId; |
| 360 | + String question; |
| 361 | + |
| 362 | + /// The limit of options any single user can add to a poll. |
| 363 | + /// |
| 364 | + /// See https://github.com/zulip/zulip/blob/304d948416465c1a085122af5d752f03d6797003/web/shared/src/poll_data.ts#L69-L71 |
| 365 | + static const maxIdx = 1000; // TODO validate |
| 366 | + |
| 367 | + Iterable<PollOption> get options => _options.values; |
| 368 | + final Set<String> _optionNames = {}; |
| 369 | + final Map<String, PollOption> _options = {}; |
| 370 | + |
| 371 | + void applyEvent(int senderId, PollEventSubmessage event) { |
| 372 | + switch (event) { |
| 373 | + case PollNewOptionEventSubmessage(): |
| 374 | + _addOption( |
| 375 | + senderId, |
| 376 | + event.option, |
| 377 | + idx: event.idx, |
| 378 | + ); |
| 379 | + |
| 380 | + case PollQuestionEventSubmessage(): |
| 381 | + if (senderId != pollSenderId) { |
| 382 | + // Only the message owner can edit the question. |
| 383 | + assert(debugLog('unexpected poll data: user $senderId is not allowed to edit the question')); // TODO(log) |
| 384 | + return; |
| 385 | + } |
| 386 | + |
| 387 | + question = event.question; |
| 388 | + |
| 389 | + case PollVoteEventSubmessage(): |
| 390 | + final option = _options[event.key]; |
| 391 | + if (option == null) { |
| 392 | + assert(debugLog('vote for unknown key ${event.key}')); // TODO(log) |
| 393 | + return; |
| 394 | + } |
| 395 | + |
| 396 | + switch (event.op) { |
| 397 | + case PollVoteOp.add: |
| 398 | + option.voters.add(senderId); |
| 399 | + case PollVoteOp.remove: |
| 400 | + option.voters.remove(senderId); |
| 401 | + case PollVoteOp.unknown: |
| 402 | + assert(debugLog('unknown vote op ${event.op}')); // TODO(log) |
| 403 | + } |
| 404 | + |
| 405 | + case UnknownPollEventSubmessage(): |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + void _addOption(int? senderId, String option, {required int idx}) { |
| 410 | + if (idx > maxIdx) return; |
| 411 | + final key = PollEventSubmessage.optionKey(senderId: senderId, idx: idx); |
| 412 | + // The web client suppresses duplicate options, which can be created through |
| 413 | + // the /poll command as there is no server-side validation. |
| 414 | + if (_optionNames.contains(option)) return; |
| 415 | + assert(!_options.containsKey(key)); |
| 416 | + _options[key] = PollOption(text: option); |
| 417 | + _optionNames.add(option); |
| 418 | + } |
| 419 | +} |
| 420 | + |
| 421 | +class PollOption { |
| 422 | + PollOption({required this.text}); |
| 423 | + |
| 424 | + factory PollOption.withVoters(String text, Iterable<int> voters) => |
| 425 | + PollOption(text: text)..voters.addAll(voters); |
| 426 | + |
| 427 | + final String text; |
| 428 | + final Set<int> voters = {}; |
| 429 | + |
| 430 | + @override |
| 431 | + bool operator ==(Object other) { |
| 432 | + if (other is! PollOption) return false; |
| 433 | + |
| 434 | + return other.hashCode == hashCode; |
| 435 | + } |
| 436 | + |
| 437 | + @override |
| 438 | + int get hashCode => Object.hash('Option', text, voters.join(',')); |
| 439 | + |
| 440 | + @override |
| 441 | + String toString() => 'Option(option: $text, voters: {${voters.join(', ')}})'; |
| 442 | +} |
0 commit comments