Skip to content

Commit

Permalink
fix pollbot anon polls (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaxim committed Feb 5, 2020
1 parent 79dd169 commit d5461f2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 45 deletions.
10 changes: 5 additions & 5 deletions pollbot/db.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

CREATE TABLE `polls` (
`id` varchar(16) NOT NULL,
`conv_id` varchar(100) NOT NULL,
`msg_id` int(11) NOT NULL,
`result_msg_id` int(11) NOT NULL,
`choices` int(11) NOT NULL,
PRIMARY KEY (`conv_id`,`msg_id`)
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `votes` (
`conv_id` varchar(100) NOT NULL,
`msg_id` int(11) NOT NULL,
`id` varchar(16) NOT NULL,
`username` varchar(50) NOT NULL,
`choice` int(11) NOT NULL,
PRIMARY KEY (`conv_id`,`msg_id`,`username`)
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
34 changes: 17 additions & 17 deletions pollbot/pollbot/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ func NewDB(db *sql.DB) *DB {
}
}

func (d *DB) CreatePoll(convID chat1.ConvIDStr, msgID chat1.MessageID, resultMsgID chat1.MessageID, numChoices int) error {
func (d *DB) CreatePoll(id string, convID chat1.ConvIDStr, msgID chat1.MessageID, resultMsgID chat1.MessageID, numChoices int) error {
return d.RunTxn(func(tx *sql.Tx) error {
_, err := tx.Exec(`
INSERT INTO polls
(conv_id, msg_id, result_msg_id, choices)
(id, conv_id, msg_id, result_msg_id, choices)
VALUES
(?, ?, ?, ?)
`, base.ShortConvID(convID), msgID, resultMsgID, numChoices)
(?, ?, ?, ?, ?)
`, id, convID, msgID, resultMsgID, numChoices)
return err
})
}

func (d *DB) GetPollInfo(convID chat1.ConvIDStr, msgID chat1.MessageID) (resultMsgID chat1.MessageID, numChoices int, err error) {
func (d *DB) GetPollInfo(id string) (convID chat1.ConvIDStr, resultMsgID chat1.MessageID, numChoices int, err error) {
row := d.DB.QueryRow(`
SELECT result_msg_id, choices
SELECT conv_id, result_msg_id, choices
FROM polls
WHERE conv_id = ? AND msg_id = ?
`, convID, msgID)
if err := row.Scan(&resultMsgID, &numChoices); err != nil {
return resultMsgID, numChoices, err
WHERE id = ?
`, id)
if err := row.Scan(&convID, &resultMsgID, &numChoices); err != nil {
return convID, resultMsgID, numChoices, err
}
return resultMsgID, numChoices, nil
return convID, resultMsgID, numChoices, nil
}

func (d *DB) GetTally(convID chat1.ConvIDStr, msgID chat1.MessageID) (res Tally, err error) {
func (d *DB) GetTally(id string) (res Tally, err error) {
rows, err := d.DB.Query(`
SELECT choice, count(*)
FROM votes
WHERE conv_id = ? AND msg_id = ?
WHERE id = ?
GROUP BY 1 ORDER BY 2 DESC
`, convID, msgID)
`, id)
if err != nil {
return res, err
}
Expand All @@ -72,10 +72,10 @@ func (d *DB) CastVote(username string, vote Vote) error {
return d.RunTxn(func(tx *sql.Tx) error {
_, err := tx.Exec(`
REPLACE INTO votes
(conv_id, msg_id, username, choice)
(id, username, choice)
VALUES
(?, ?, ?, ?)
`, base.ShortConvID(vote.ConvID), vote.MsgID, username, vote.Choice)
(?, ?, ?)
`, vote.ID, username, vote.Choice)
return err
})
}
9 changes: 5 additions & 4 deletions pollbot/pollbot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ func NewHandler(stats *base.StatsRegistry, kbc *kbchat.API, debugConfig *base.Ch
}
}

func (h *Handler) generateVoteLink(convID chat1.ConvIDStr, msgID chat1.MessageID, choice int) string {
vote := NewVote(convID, msgID, choice)
func (h *Handler) generateVoteLink(id string, choice int) string {
vote := NewVote(id, choice)
link := h.httpPrefix + "/pollbot/vote?=" + url.QueryEscape(vote.Encode())
return strings.ReplaceAll(link, "%", "%%")
}

func (h *Handler) generateAnonymousPoll(convID chat1.ConvIDStr, msgID chat1.MessageID, prompt string,
options []string) error {
id := base.RandHexString(8)
promptBody := fmt.Sprintf("Anonymous Poll: *%s*\n\n", prompt)
sendRes, err := h.kbc.SendMessageByConvID(convID, promptBody)
if err != nil {
Expand All @@ -56,7 +57,7 @@ func (h *Handler) generateAnonymousPoll(convID chat1.ConvIDStr, msgID chat1.Mess
var body string
for index, option := range options {
body += fmt.Sprintf("\n%s *%s*\n%s\n", base.NumberToEmoji(index+1), option,
h.generateVoteLink(convID, promptMsgID, index+1))
h.generateVoteLink(id, index+1))
}
h.ChatEcho(convID, body)
if sendRes, err = h.kbc.SendMessageByConvID(convID, "*Results*\n_No votes yet_"); err != nil {
Expand All @@ -66,7 +67,7 @@ func (h *Handler) generateAnonymousPoll(convID chat1.ConvIDStr, msgID chat1.Mess
return fmt.Errorf("failed to get ID of result message")
}
resultMsgID := *sendRes.Result.MessageID
if err := h.db.CreatePoll(convID, promptMsgID, resultMsgID, len(options)); err != nil {
if err := h.db.CreatePoll(id, convID, promptMsgID, resultMsgID, len(options)); err != nil {
return fmt.Errorf("failed to create poll: %s", err)
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pollbot/pollbot/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ func (h *HTTPSrv) handleVote(w http.ResponseWriter, r *http.Request) {
h.showError(w)
return
}
resultMsgID, numChoices, err := h.db.GetPollInfo(vote.ConvID, vote.MsgID)
convID, resultMsgID, numChoices, err := h.db.GetPollInfo(vote.ID)
if err != nil {
h.Errorf("failed to find poll result msg: %s", err)
h.showError(w)
return
}
tally, err := h.db.GetTally(vote.ConvID, vote.MsgID)
tally, err := h.db.GetTally(vote.ID)
if err != nil {
h.Errorf("failed to get tally: %s", err)
h.showError(w)
return
}
if _, err := h.kbc.EditByConvID(vote.ConvID, resultMsgID, formatTally(tally, numChoices)); err != nil {
if _, err := h.kbc.EditByConvID(convID, resultMsgID, formatTally(tally, numChoices)); err != nil {
h.Errorf("failed to post result: %s", err)
h.showError(w)
return
Expand Down
23 changes: 7 additions & 16 deletions pollbot/pollbot/vote.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package pollbot

import (
"encoding/hex"

"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/keybase/managed-bots/base"
)

type Vote struct {
ConvID chat1.ConvIDStr
MsgID chat1.MessageID
ID string
Choice int
}

type voteToEncode struct {
ConvID []byte `codec:"c"`
MsgID chat1.MessageID `codec:"m"`
Choice int `codec:"i"`
ID string `codec:"d"`
Choice int `codec:"i"`
}

func NewVote(convID chat1.ConvIDStr, msgID chat1.MessageID, choice int) Vote {
func NewVote(id string, choice int) Vote {
return Vote{
ConvID: convID,
MsgID: msgID,
ID: id,
Choice: choice,
}
}
Expand All @@ -32,17 +26,14 @@ func NewVoteFromEncoded(sdat string) Vote {
dat, _ := base.URLEncoder().DecodeString(sdat)
_ = base.MsgpackDecode(&ve, dat)
return Vote{
ConvID: chat1.ConvIDStr(hex.EncodeToString(ve.ConvID)),
MsgID: ve.MsgID,
ID: ve.ID,
Choice: ve.Choice,
}
}

func (v Vote) Encode() string {
cdat, _ := hex.DecodeString(string(base.ShortConvID(v.ConvID)))
mdat, _ := base.MsgpackEncode(voteToEncode{
ConvID: cdat,
MsgID: v.MsgID,
ID: v.ID,
Choice: v.Choice,
})
return base.URLEncoder().EncodeToString(mdat)
Expand Down

0 comments on commit d5461f2

Please sign in to comment.