From b451285af70df0651989b43149eb748648ac8c1b Mon Sep 17 00:00:00 2001 From: Wim Date: Fri, 21 Aug 2020 00:14:33 +0200 Subject: Sync with upstream gozulipbot fixes (#1202) --- vendor/github.com/matterbridge/gozulipbot/bot.go | 4 -- vendor/github.com/matterbridge/gozulipbot/flag.go | 53 +++++++++++++++++----- .../github.com/matterbridge/gozulipbot/message.go | 12 +++-- vendor/github.com/matterbridge/gozulipbot/queue.go | 18 +++++++- vendor/modules.txt | 2 +- 5 files changed, 67 insertions(+), 22 deletions(-) (limited to 'vendor') diff --git a/vendor/github.com/matterbridge/gozulipbot/bot.go b/vendor/github.com/matterbridge/gozulipbot/bot.go index 24bc8768..f5644a29 100644 --- a/vendor/github.com/matterbridge/gozulipbot/bot.go +++ b/vendor/github.com/matterbridge/gozulipbot/bot.go @@ -186,10 +186,6 @@ func (b *Bot) RegisterEvents(ets []EventType, n Narrow) (*Queue, error) { return nil, err } - if q.LastEventID < q.MaxMessageID { - q.LastEventID = q.MaxMessageID - } - b.Queues = append(b.Queues, q) return q, nil diff --git a/vendor/github.com/matterbridge/gozulipbot/flag.go b/vendor/github.com/matterbridge/gozulipbot/flag.go index 6305aac1..0a263388 100644 --- a/vendor/github.com/matterbridge/gozulipbot/flag.go +++ b/vendor/github.com/matterbridge/gozulipbot/flag.go @@ -3,30 +3,61 @@ package gozulipbot import ( "flag" "fmt" + "os" "time" ) func (b *Bot) GetConfigFromFlags() error { var ( - apiKey = flag.String("apikey", "", "bot api key") - apiURL = flag.String("apiurl", "", "url of zulip server") - email = flag.String("email", "", "bot email address") - backoff = flag.Duration("backoff", 1*time.Second, "backoff base duration") + apiKey = flag.String("apikey", "ZULIP_APIKEY", "bot api key or env var") + apiURL = flag.String("apiurl", "ZULIP_APIURL", "url of zulip server or env var") + backoff = flag.Duration("backoff", 1*time.Second, "backoff base duration or env var") + email = flag.String("email", "ZULIP_EMAIL", "bot email address or env var") + env = flag.Bool("env", false, "get string values from environment variables") ) flag.Parse() - if *apiKey == "" { + b.APIKey = *apiKey + b.APIURL = *apiURL + b.Email = *email + b.Backoff = *backoff + if *env { + b.GetConfigFromEnvironment() + } + return b.checkConfig() +} + +func (b *Bot) GetConfigFromEnvironment() error { + if apiKey, exists := os.LookupEnv(b.APIKey); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.APIKey) + } else { + b.APIKey = apiKey + } + if apiURL, exists := os.LookupEnv(b.APIURL); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.APIURL) + } else { + b.APIURL = apiURL + } + if email, exists := os.LookupEnv(b.Email); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.Email) + } else { + b.Email = email + } + return nil +} + +func (b *Bot) checkConfig() error { + if b.APIKey == "" { return fmt.Errorf("--apikey is required") } - if *apiURL == "" { + if b.APIURL == "" { return fmt.Errorf("--apiurl is required") } - if *email == "" { + if b.Email == "" { return fmt.Errorf("--email is required") } - b.APIKey = *apiKey - b.APIURL = *apiURL - b.Email = *email - b.Backoff = *backoff + if b.Backoff <= 0 { + return fmt.Errorf("--backoff must be greater than zero") + } return nil } diff --git a/vendor/github.com/matterbridge/gozulipbot/message.go b/vendor/github.com/matterbridge/gozulipbot/message.go index df963f43..16d88312 100644 --- a/vendor/github.com/matterbridge/gozulipbot/message.go +++ b/vendor/github.com/matterbridge/gozulipbot/message.go @@ -194,8 +194,10 @@ func (b *Bot) UpdateMessage(id string, content string) (*http.Response, error) { // React adds an emoji reaction to an EventMessage. func (b *Bot) React(e EventMessage, emoji string) (*http.Response, error) { - url := fmt.Sprintf("messages/%d/emoji_reactions/%s", e.ID, emoji) - req, err := b.constructRequest("PUT", url, "") + requestURL := fmt.Sprintf("messages/%d/reactions", e.ID) + values := url.Values{} + values.Set("emoji_name", emoji) + req, err := b.constructRequest("POST", requestURL, values.Encode()) if err != nil { return nil, err } @@ -204,8 +206,10 @@ func (b *Bot) React(e EventMessage, emoji string) (*http.Response, error) { // Unreact removes an emoji reaction from an EventMessage. func (b *Bot) Unreact(e EventMessage, emoji string) (*http.Response, error) { - url := fmt.Sprintf("messages/%d/emoji_reactions/%s", e.ID, emoji) - req, err := b.constructRequest("DELETE", url, "") + requestURL := fmt.Sprintf("messages/%d/reactions", e.ID) + values := url.Values{} + values.Set("emoji_name", emoji) + req, err := b.constructRequest("DELETE", requestURL, values.Encode()) if err != nil { return nil, err } diff --git a/vendor/github.com/matterbridge/gozulipbot/queue.go b/vendor/github.com/matterbridge/gozulipbot/queue.go index d6e910ee..77aef7fc 100644 --- a/vendor/github.com/matterbridge/gozulipbot/queue.go +++ b/vendor/github.com/matterbridge/gozulipbot/queue.go @@ -59,6 +59,7 @@ func (q *Queue) EventsChan() (chan EventMessage, func()) { case err == BackoffError: time.Sleep(time.Until(backoffTime)) atomic.AddInt64(&q.Bot.Retries, 1) + continue case err == UnauthorizedError: // TODO? have error channel when ending the continuously running process? return @@ -81,7 +82,7 @@ func (q *Queue) EventsChan() (chan EventMessage, func()) { return out, endFunc } -// EventsCallback will repeatedly call provided callback function with +// EventsCallback will repeatedly call the provided callback function with // the output of continual queue.GetEvents calls. // It returns a function which can be called to end the calls. // @@ -107,6 +108,7 @@ func (q *Queue) EventsCallback(fn func(EventMessage, error)) func() { case err == BackoffError: time.Sleep(time.Until(backoffTime)) atomic.AddInt64(&q.Bot.Retries, 1) + continue case err == UnauthorizedError: // TODO? have error channel when ending the continuously running process? return @@ -221,9 +223,19 @@ func (q *Queue) ParseEventMessages(rawEventResponse []byte) ([]EventMessage, err } messages := []EventMessage{} + newLastEventID := 0 for _, event := range events { - // if the event is a heartbeat, return a special error + // Update the lastEventID + var id int + json.Unmarshal(event["id"], &id) + if id > newLastEventID { + newLastEventID = id + } + + // If the event is a heartbeat, there won't be any more events. + // So update the last event id and return a special error. if string(event["type"]) == `"heartbeat"` { + q.LastEventID = newLastEventID return nil, HeartbeatError } var msg EventMessage @@ -236,5 +248,7 @@ func (q *Queue) ParseEventMessages(rawEventResponse []byte) ([]EventMessage, err messages = append(messages, msg) } + q.LastEventID = newLastEventID + return messages, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 2308e7af..da0774ff 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -113,7 +113,7 @@ github.com/matterbridge/emoji github.com/matterbridge/go-xmpp # github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6 github.com/matterbridge/gomatrix -# github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 +# github.com/matterbridge/gozulipbot v0.0.0-20200820220548-be5824faa913 github.com/matterbridge/gozulipbot # github.com/matterbridge/logrus-prefixed-formatter v0.5.3-0.20200523233437-d971309a77ba github.com/matterbridge/logrus-prefixed-formatter -- cgit v1.2.3