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