From e31369e49a258b35f176f1e16f9ece937ebe8123 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 17 Feb 2023 23:19:34 -0600 Subject: [PATCH] Mastodon: Added experimental support for voting in polls --- src/controller/buffers/mastodon/base.py | 28 ++++++++++++- .../buffers/mastodon/notifications.py | 3 ++ src/controller/mainController.py | 5 +++ src/wxUI/dialogs/mastodon/postDialogs.py | 42 ++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/controller/buffers/mastodon/base.py b/src/controller/buffers/mastodon/base.py index 00955359..de4c7a46 100644 --- a/src/controller/buffers/mastodon/base.py +++ b/src/controller/buffers/mastodon/base.py @@ -20,6 +20,7 @@ from extra import ocr from wxUI import buffers, dialogs, commonMessageDialogs from wxUI.dialogs.mastodon import menus from wxUI.dialogs.mastodon import dialogs as mastodon_dialogs +from wxUI.dialogs.mastodon.postDialogs import attachedPoll log = logging.getLogger("controller.buffers.mastodon.base") @@ -538,4 +539,29 @@ class BaseBuffer(base.Buffer): def ocr_image(self): post = self.get_item() media_list = [] - pass \ No newline at end of file + pass + + def vote(self): + post = self.get_item() + if not hasattr(post, "poll") or post.poll == None: + return + poll = post.poll + try: + poll = self.session.api.poll(id=poll.id) + except MastodonNotFoundError: + output.speak(_("this poll no longer exists.")) + return + if poll.expired: + output.speak(_("This poll has already expired.")) + return + if poll.voted: + output.speak(_("You have already voted on this poll.")) + return + options = poll.options + dlg = attachedPoll(poll_options=[option.title for option in options], multiple=poll.multiple) + answer = dlg.ShowModal() + options = dlg.get_selected() + dlg.Destroy() + if answer != wx.ID_OK: + return + poll = self.session.api_call(call_name="poll_vote", id=poll.id, choices=options, preexec_message=_("Sending vote...")) \ No newline at end of file diff --git a/src/controller/buffers/mastodon/notifications.py b/src/controller/buffers/mastodon/notifications.py index 68951382..68f362a3 100644 --- a/src/controller/buffers/mastodon/notifications.py +++ b/src/controller/buffers/mastodon/notifications.py @@ -42,6 +42,9 @@ class NotificationsBuffer(BaseBuffer): def unfav(self): pass + def vote(self): + pass + def can_share(self): return False diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 2b47bf64..ce923737 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -593,6 +593,11 @@ class Controller(object): if hasattr(buffer, "toggle_favorite"): return buffer.toggle_favorite() + def vote(self, *args, **kwargs): + buffer = self.get_current_buffer() + if hasattr(buffer, "vote"): + return buffer.vote() + def view_item(self, *args, **kwargs): buffer = self.get_current_buffer() if hasattr(buffer, "view_item"): diff --git a/src/wxUI/dialogs/mastodon/postDialogs.py b/src/wxUI/dialogs/mastodon/postDialogs.py index 531a893c..a23802fe 100644 --- a/src/wxUI/dialogs/mastodon/postDialogs.py +++ b/src/wxUI/dialogs/mastodon/postDialogs.py @@ -297,7 +297,7 @@ class poll(wx.Dialog): self.option4 = wx.TextCtrl(self, wx.ID_ANY, "") self.option4.SetMaxLength(25) option4_sizer.Add(self.option4, 0, 0, 0) - self.multiple = wx.CheckBox(self, wx.ID_ANY, _("Allow multiple votes per user")) + self.multiple = wx.CheckBox(self, wx.ID_ANY, _("Allow multiple choices per user")) self.multiple.SetValue(False) sizer_1.Add(self.multiple, 0, wx.ALL, 5) self.hide_votes = wx.CheckBox(self, wx.ID_ANY, _("Hide votes count until the poll expires")) @@ -327,4 +327,42 @@ class poll(wx.Dialog): options = self.get_options() if len(options) < 2: return wx.MessageDialog(self, _("Please make sure you have provided at least two options for the poll."), _("Not enough information"), wx.ICON_ERROR).ShowModal() - self.EndModal(wx.ID_OK) \ No newline at end of file + self.EndModal(wx.ID_OK) + +class attachedPoll(wx.Dialog): + def __init__(self, poll_options, multiple=False, *args, **kwds): + super(attachedPoll, self).__init__(parent=None, id=wx.NewId(), title=_("Vote in this poll")) + self.poll_options = poll_options + sizer_1 = wx.BoxSizer(wx.VERTICAL) + sizer_2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, _("Options")), wx.VERTICAL) + sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) + if multiple == False: + for option in range(len(self.poll_options)): + if option == 0: + setattr(self, "option{}".format(option), wx.RadioButton(self, wx.ID_ANY, poll_options[option], style=wx.RB_GROUP)) + else: + setattr(self, "option{}".format(option), wx.RadioButton(self, wx.ID_ANY, poll_options[option])) + else: + for option in range(len(self.poll_options)): + setattr(self, "option{}".format(option), wx.CheckBox(self, wx.ID_ANY, poll_options[option])) + sizer_2.Add(getattr(self, "option{}".format(option)), 1, wx.EXPAND, 0) + btn_sizer = wx.StdDialogButtonSizer() + sizer_1.Add(btn_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 4) + self.button_OK = wx.Button(self, wx.ID_OK) + self.button_OK.SetDefault() + btn_sizer.AddButton(self.button_OK) + self.button_CANCEL = wx.Button(self, wx.ID_CANCEL, "") + btn_sizer.AddButton(self.button_CANCEL) + btn_sizer.Realize() + self.SetSizer(sizer_1) + sizer_1.Fit(self) + self.SetAffirmativeId(self.button_OK.GetId()) + self.SetEscapeId(self.button_CANCEL.GetId()) + self.Layout() + + def get_selected(self): + options = [] + for option in range(len(self.poll_options)): + if getattr(self, "option{}".format(option)).GetValue() == True: + options.append(option) + return options