Skip to content

Commit

Permalink
Fix inifite-loop problem occurred when Twitter API not working
Browse files Browse the repository at this point in the history
  • Loading branch information
iwataka committed May 6, 2022
1 parent 2eec3e3 commit 34014e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
5 changes: 4 additions & 1 deletion core/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ func (l *SlackListener) Start(ctx context.Context, outChan chan<- interface{}) (

for {
select {
case msg := <-rtm.IncomingEvents:
case msg, ok := <-rtm.IncomingEvents:
if !ok {
return nil
}
e := l.processMsgEvent(msg, outChan)
if e != nil {
err = utils.WithStack(e)
Expand Down
54 changes: 29 additions & 25 deletions core/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewTwitterAPIWithAuth(auth oauth.OAuthCreds, config Config, cache data.Cach
var api models.TwitterAPI
if len(at) > 0 && len(ats) > 0 {
api = anaconda.NewTwitterApi(at, ats)
api.SetLogger(anaconda.BasicLogger)
}
return NewTwitterAPI(api, config, cache)
}
Expand Down Expand Up @@ -354,7 +355,10 @@ func (a *TwitterAPI) ListenUsers(
func (l *TwitterUserListener) Listen(ctx context.Context, outChan chan<- interface{}) error {
for {
select {
case msg := <-l.stream.C:
case msg, ok := <-l.stream.C:
if !ok {
return nil
}
err := l.processMessage(msg, outChan)
if err != nil {
return utils.WithStack(err)
Expand Down Expand Up @@ -434,30 +438,30 @@ func (a *TwitterAPI) ListenMyself(v url.Values) (*TwitterDMListener, error) {
}

func (l *TwitterDMListener) Listen(ctx context.Context, outChan chan<- interface{}) error {
// TODO: Twitter User Stream API has been retired, so I temporarily disable this feature.
// Later I completely remove this feature.
// https://developer.twitter.com/en/docs/twitter-api/enterprise/account-activity-api/migration/us-ss-migration-guide
return nil
// for {
// select {
// case msg := <-l.stream.C:
// switch c := msg.(type) {
// case anaconda.DirectMessage:
// outChan <- NewReceivedEvent(TwitterEventType, "DM", c)
// // TODO: Handle direct messages in the same way as the other sources
// id := l.api.cache.GetLatestDMID()
// if id < c.Id {
// l.api.cache.SetLatestDMID(c.Id)
// }
// err := l.api.cache.Save()
// if err != nil {
// return utils.WithStack(err)
// }
// }
// case <-ctx.Done():
// return nil
// }
// }
// TODO: Twitter User Stream API has been retired, so this feature should be removed
for {
select {
case msg, ok := <-l.stream.C:
if !ok {
return nil
}
switch c := msg.(type) {
case anaconda.DirectMessage:
outChan <- NewReceivedEvent(TwitterEventType, "DM", c)
// TODO: Handle direct messages in the same way as the other sources
id := l.api.cache.GetLatestDMID()
if id < c.Id {
l.api.cache.SetLatestDMID(c.Id)
}
err := l.api.cache.Save()
if err != nil {
return utils.WithStack(err)
}
}
case <-ctx.Done():
return nil
}
}
}

func (l *TwitterDMListener) Stop() {
Expand Down
12 changes: 12 additions & 0 deletions mocks/twitter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions models/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TwitterAPI interface {
UserStream(url.Values) *anaconda.Stream
GetUsersShow(string, url.Values) (anaconda.User, error)
GetUserSearch(string, url.Values) ([]anaconda.User, error)
SetLogger(anaconda.Logger)
}

type TwitterActionProperties struct {
Expand Down

0 comments on commit 34014e3

Please sign in to comment.