Sync with upstream gozulipbot fixes (#1202)
This commit is contained in:
parent
63a1847cdc
commit
b451285af7
2
go.mod
2
go.mod
|
@ -25,7 +25,7 @@ require (
|
|||
github.com/matterbridge/emoji v2.1.1-0.20191117213217-af507f6b02db+incompatible
|
||||
github.com/matterbridge/go-xmpp v0.0.0-20200418225040-c8a3a57b4050
|
||||
github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20200820220548-be5824faa913
|
||||
github.com/matterbridge/logrus-prefixed-formatter v0.5.3-0.20200523233437-d971309a77ba
|
||||
github.com/mattermost/mattermost-server/v5 v5.25.2
|
||||
github.com/mattn/godown v0.0.0-20200217152941-afc959f6a561
|
||||
|
|
4
go.sum
4
go.sum
|
@ -354,8 +354,8 @@ github.com/matterbridge/go-xmpp v0.0.0-20200418225040-c8a3a57b4050 h1:kWkP1lXpkv
|
|||
github.com/matterbridge/go-xmpp v0.0.0-20200418225040-c8a3a57b4050/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q=
|
||||
github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6 h1:Kl65VJv38HjYFnnwH+MP6Z8hcJT5UHuSpHVU5vW1HH0=
|
||||
github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6/go.mod h1:+jWeaaUtXQbBRdKYWfjW6JDDYiI2XXE+3NnTjW5kg8g=
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 h1:fLhwXtWGtfTgZVxHG1lcKjv+re7dRwyyuYFNu69xdho=
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18/go.mod h1:yAjnZ34DuDyPHMPHHjOsTk/FefW4JJjoMMCGt/8uuQA=
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20200820220548-be5824faa913 h1:5UGr9fLsvAvhjP6i5XJmd0ZIwYVR2gZCzU1lJZ7wfLY=
|
||||
github.com/matterbridge/gozulipbot v0.0.0-20200820220548-be5824faa913/go.mod h1:yAjnZ34DuDyPHMPHHjOsTk/FefW4JJjoMMCGt/8uuQA=
|
||||
github.com/matterbridge/logrus-prefixed-formatter v0.5.3-0.20200523233437-d971309a77ba h1:XleOY4IjAEIcxAh+IFwT5JT5Ze3RHiYz6m+4ZfZ0rc0=
|
||||
github.com/matterbridge/logrus-prefixed-formatter v0.5.3-0.20200523233437-d971309a77ba/go.mod h1:iXGEotOvwI1R1SjLxRc+BF5rUORTMtE0iMZBT2lxqAU=
|
||||
github.com/mattermost/go-i18n v1.11.0 h1:1hLKqn/ZvhZ80OekjVPGYcCrBfMz+YxNNgqS+beL7zE=
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 == "" {
|
||||
return fmt.Errorf("--apikey is required")
|
||||
}
|
||||
if *apiURL == "" {
|
||||
return fmt.Errorf("--apiurl is required")
|
||||
}
|
||||
if *email == "" {
|
||||
return fmt.Errorf("--email is required")
|
||||
}
|
||||
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 b.APIURL == "" {
|
||||
return fmt.Errorf("--apiurl is required")
|
||||
}
|
||||
if b.Email == "" {
|
||||
return fmt.Errorf("--email is required")
|
||||
}
|
||||
if b.Backoff <= 0 {
|
||||
return fmt.Errorf("--backoff must be greater than zero")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue