|
| 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.
|
@@ -41,6 +45,30 @@ class Submessage {
|
41 | 45 | // * the parsed [WidgetType] from the first [Message.submessages].
|
42 | 46 | final String content;
|
43 | 47 |
|
| 48 | + /// Parse a JSON list into a [Poll]. |
| 49 | + // TODO: Use a generalized return type when supporting other Zulip widgets. |
| 50 | + static Poll? parseSubmessagesJson(List<Object?> json, { |
| 51 | + required int messageSenderId, |
| 52 | + }) { |
| 53 | + final submessages = json.map((e) => Submessage.fromJson(e as Map<String, Object?>)).toList(); |
| 54 | + if (submessages.isEmpty) return null; |
| 55 | + |
| 56 | + assert(submessages.first.senderId == messageSenderId); |
| 57 | + |
| 58 | + final widgetData = WidgetData.fromJson(jsonDecode(submessages.first.content)); |
| 59 | + switch (widgetData) { |
| 60 | + case PollWidgetData(): |
| 61 | + return Poll.fromSubmessages( |
| 62 | + widgetData: widgetData, |
| 63 | + pollEventSubmessages: submessages.skip(1), |
| 64 | + messageSenderId: messageSenderId, |
| 65 | + ); |
| 66 | + case UnsupportedWidgetData(): |
| 67 | + assert(debugLog('Unsupported widgetData: ${widgetData.json}')); |
| 68 | + return null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
44 | 72 | factory Submessage.fromJson(Map<String, Object?> json) =>
|
45 | 73 | _$SubmessageFromJson(json);
|
46 | 74 |
|
@@ -319,3 +347,126 @@ class UnknownPollEventSubmessage extends PollEventSubmessage {
|
319 | 347 | @override
|
320 | 348 | Map<String, Object?> toJson() => json;
|
321 | 349 | }
|
| 350 | + |
| 351 | +/// States of a poll Zulip widget. |
| 352 | +/// |
| 353 | +/// See also: |
| 354 | +/// - https://zulip.com/help/create-a-poll |
| 355 | +/// - https://github.com/zulip/zulip/blob/304d948416465c1a085122af5d752f03d6797003/web/shared/src/poll_data.ts |
| 356 | +class Poll { |
| 357 | + /// Construct a poll from submessages. |
| 358 | + /// |
| 359 | + /// For a poll Zulip widget, the first submessage's content contains a |
| 360 | + /// [PollWidgetData], and all the following submessages' content each contains |
| 361 | + /// a [PollEventSubmessage]. |
| 362 | + factory Poll.fromSubmessages({ |
| 363 | + required PollWidgetData widgetData, |
| 364 | + required Iterable<Submessage> pollEventSubmessages, |
| 365 | + required int messageSenderId, |
| 366 | + }) { |
| 367 | + final poll = Poll._( |
| 368 | + messageSenderId: messageSenderId, |
| 369 | + question: widgetData.extraData.question, |
| 370 | + options: widgetData.extraData.options, |
| 371 | + ); |
| 372 | + |
| 373 | + for (final submessage in pollEventSubmessages) { |
| 374 | + final event = PollEventSubmessage.fromJson(jsonDecode(submessage.content) as Map<String, Object?>); |
| 375 | + poll._applyEvent(submessage.senderId, event); |
| 376 | + } |
| 377 | + return poll; |
| 378 | + } |
| 379 | + |
| 380 | + Poll._({ |
| 381 | + required this.messageSenderId, |
| 382 | + required this.question, |
| 383 | + required List<String> options, |
| 384 | + }) { |
| 385 | + for (int index = 0; index < options.length; index += 1) { |
| 386 | + // Initial poll options use a placeholder senderId. |
| 387 | + // See [PollEventSubmessage.optionKey] for details. |
| 388 | + _addOption(senderId: null, idx: index, option: options[index]); |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + final int messageSenderId; |
| 393 | + String question; |
| 394 | + |
| 395 | + /// The limit of options any single user can add to a poll. |
| 396 | + /// |
| 397 | + /// See https://github.com/zulip/zulip/blob/304d948416465c1a085122af5d752f03d6797003/web/shared/src/poll_data.ts#L69-L71 |
| 398 | + static const _maxIdx = 1000; |
| 399 | + |
| 400 | + Iterable<PollOption> get options => _options.values; |
| 401 | + /// Contains the text of all options from [_options]. |
| 402 | + final Set<String> _existingOptionTexts = {}; |
| 403 | + final Map<PollOptionKey, PollOption> _options = {}; |
| 404 | + |
| 405 | + void _applyEvent(int senderId, PollEventSubmessage event) { |
| 406 | + switch (event) { |
| 407 | + case PollNewOptionEventSubmessage(): |
| 408 | + _addOption(senderId: senderId, idx: event.idx, option: event.option); |
| 409 | + |
| 410 | + case PollQuestionEventSubmessage(): |
| 411 | + if (senderId != messageSenderId) { |
| 412 | + // Only the message owner can edit the question. |
| 413 | + assert(debugLog('unexpected poll data: user $senderId is not allowed to edit the question')); // TODO(log) |
| 414 | + return; |
| 415 | + } |
| 416 | + |
| 417 | + question = event.question; |
| 418 | + |
| 419 | + case PollVoteEventSubmessage(): |
| 420 | + final option = _options[event.key]; |
| 421 | + if (option == null) { |
| 422 | + assert(debugLog('vote for unknown key ${event.key}')); // TODO(log) |
| 423 | + return; |
| 424 | + } |
| 425 | + |
| 426 | + switch (event.op) { |
| 427 | + case PollVoteOp.add: |
| 428 | + option.voters.add(senderId); |
| 429 | + case PollVoteOp.remove: |
| 430 | + option.voters.remove(senderId); |
| 431 | + case PollVoteOp.unknown: |
| 432 | + assert(debugLog('unknown vote op ${event.op}')); // TODO(log) |
| 433 | + } |
| 434 | + |
| 435 | + case UnknownPollEventSubmessage(): |
| 436 | + } |
| 437 | + } |
| 438 | + |
| 439 | + void _addOption({required int? senderId, required int idx, required String option}) { |
| 440 | + if (idx > _maxIdx || idx < 0) return; |
| 441 | + |
| 442 | + // The web client suppresses duplicate options, which can be created through |
| 443 | + // the /poll command as there is no server-side validation. |
| 444 | + if (_existingOptionTexts.contains(option)) return; |
| 445 | + |
| 446 | + final key = PollEventSubmessage.optionKey(senderId: senderId, idx: idx); |
| 447 | + assert(!_options.containsKey(key)); |
| 448 | + _options[key] = PollOption(text: option); |
| 449 | + _existingOptionTexts.add(option); |
| 450 | + } |
| 451 | + |
| 452 | + static Poll? fromJson(Object? json) { |
| 453 | + // [Submessage.parseSubmessagesJson] does all the heavy lifting for parsing. |
| 454 | + return json as Poll?; |
| 455 | + } |
| 456 | + |
| 457 | + static List<Submessage> toJson(Poll? poll) { |
| 458 | + // Rather than maintaining a up-to-date submessages list, return as if it is |
| 459 | + // empty, because we are not sending the submessages to the server anyway. |
| 460 | + return []; |
| 461 | + } |
| 462 | +} |
| 463 | + |
| 464 | +class PollOption { |
| 465 | + PollOption({required this.text}); |
| 466 | + |
| 467 | + final String text; |
| 468 | + final Set<int> voters = {}; |
| 469 | + |
| 470 | + @override |
| 471 | + String toString() => 'PollOption(text: $text, voters: {${voters.join(', ')}})'; |
| 472 | +} |
0 commit comments