Line data Source code
1 : import 'package:matrix/matrix.dart';
2 : import 'package:matrix/msc_extensions/msc_3381_polls/models/poll_event_content.dart';
3 :
4 : extension PollRoomExtension on Room {
5 : static const String mTextJsonKey = 'org.matrix.msc1767.text';
6 : static const String startType = 'org.matrix.msc3381.poll.start';
7 :
8 2 : Future<String?> startPoll({
9 : required String question,
10 : required List<PollAnswer> answers,
11 : String? body,
12 : PollKind kind = PollKind.undisclosed,
13 : int maxSelections = 1,
14 : String? txid,
15 : }) async {
16 4 : if (answers.length > 20) {
17 0 : throw Exception('Client must not set more than 20 answers in a poll');
18 : }
19 :
20 : if (body == null) {
21 : body = question;
22 6 : for (var i = 0; i < answers.length; i++) {
23 6 : body = '$body\n$i. ${answers[i].mText}';
24 : }
25 : }
26 :
27 2 : final newPollEvent = PollEventContent(
28 : mText: body!,
29 2 : pollStartContent: PollStartContent(
30 : kind: kind,
31 : maxSelections: maxSelections,
32 2 : question: PollQuestion(mText: question),
33 : answers: answers,
34 : ),
35 : );
36 :
37 2 : return sendEvent(
38 2 : newPollEvent.toJson(),
39 : type: startType,
40 : txid: txid,
41 : );
42 : }
43 : }
|