Skip to content

Commit

Permalink
widgets: Add question for poll widget in the message itself.
Browse files Browse the repository at this point in the history
Use the command '/poll question?', to start a question.
  • Loading branch information
rheaparekh committed Jun 30, 2018
1 parent fcd85ee commit c9fb84d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
11 changes: 8 additions & 3 deletions static/js/voting_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ var voting_widget = (function () {

var exports = {};

var poll_data_holder = function (is_my_poll) {
var poll_data_holder = function (is_my_poll, question) {
// This object just holds data for a poll, although it
// works closely with the widget's concept of how data
// should be represented for rendering, plus how the
// server sends us data.
var self = {};

var me = people.my_current_user_id();
var poll_question = '';
var poll_question = question;
var key_to_comment = {};
var my_idx = 1;

Expand Down Expand Up @@ -141,9 +141,14 @@ var poll_data_holder = function (is_my_poll) {
exports.activate = function (opts) {
var elem = opts.elem;
var callback = opts.callback;
if (opts.extra_data) {
var question = opts.extra_data.question;
} else {
var question = '';
}

var is_my_poll = people.is_my_user_id(opts.message.sender_id);
var poll_data = poll_data_holder(is_my_poll);
var poll_data = poll_data_holder(is_my_poll, question);

function render() {
var html = templates.render('poll-widget');
Expand Down
31 changes: 28 additions & 3 deletions zerver/lib/widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import MutableMapping, Any
from typing import MutableMapping, Any, Optional, List, Tuple
from django.conf import settings

import re
Expand All @@ -17,6 +17,32 @@ def do_widget_pre_save_actions(message: MutableMapping[str, Any]) -> None:
message['message'].content = 'We are running **1 server**.'
return

def get_widget_data(content: str) -> Tuple[Optional[str], Optional[str]]:
valid_widget_types = ['tictactoe', 'poll']
tokens = content.split(' ')
if not tokens:
return None, None

if tokens[0].startswith('/'):
widget_type = tokens[0][1:]
if widget_type in valid_widget_types:
extra_data = get_extra_data_from_widget_type(tokens, widget_type)
return widget_type, extra_data

return None, None

def get_extra_data_from_widget_type(tokens: List[str],
widget_type: Optional[str]) -> Any:
if widget_type == 'poll':
# This is used to extract the question from the poll command.
# The command '/poll question' will pre-set the question in the poll
question = ' '.join(tokens[1:])
if not question:
question = ''
extra_data = {'question': question}
return extra_data
return None

def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
'''
This is experimental code that only works with the
Expand All @@ -30,9 +56,8 @@ def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:

widget_type = None
extra_data = None
if content in ['/poll', '/tictactoe']:
widget_type = content[1:]

widget_type, extra_data = get_widget_data(content)
widget_content = message.get('widget_content')
if widget_content is not None:
# Note that we validate this data in check_message,
Expand Down

0 comments on commit c9fb84d

Please sign in to comment.