Skip to content

Commit

Permalink
Remove latest tweet ID cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iwataka committed Aug 10, 2018
1 parent f28034c commit c617c09
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 160 deletions.
23 changes: 0 additions & 23 deletions data/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
// processing.
type Cache interface {
utils.Savable
GetLatestTweetID(screenName string) int64
SetLatestTweetID(screenName string, id int64)
GetLatestFavoriteID(screenName string) int64
SetLatestFavoriteID(screenName string, id int64)
GetLatestDMID() int64
Expand All @@ -30,9 +28,6 @@ type Cache interface {
// CacheProperties contains common actual cache variables and is intended to be
// embedded into other structs.
type CacheProperties struct {
// LatestTweetID associates Twitter screen name with the latest tweet
// ID in timeline.
LatestTweetID map[string]int64 `json:"latest_tweet_id" toml:"latest_tweet_id" bson:"latest_tweet_id"`
// LatestFavoriteID associates Twitter screen name with the latest
// tweet ID in favorite list.
LatestFavoriteID map[string]int64 `json:"latest_favorite_id" toml:"lates_favorite_id" bson:"latest_favorite_id"`
Expand All @@ -49,7 +44,6 @@ type CacheProperties struct {

func newCacheProperties() CacheProperties {
return CacheProperties{
make(map[string]int64),
make(map[string]int64),
0,
make(map[string]Action),
Expand Down Expand Up @@ -82,23 +76,6 @@ func NewFileCache(path string) (*FileCache, error) {
return c, nil
}

// GetLatestTweetID returns the latest tweet ID associated with screenName in
// timeline. If there is no ID of screenName in c , this returns 0 (tweet ID
// can't be 0, which is known by Twitter API specification).
func (c *CacheProperties) GetLatestTweetID(screenName string) int64 {
id, exists := c.LatestTweetID[screenName]
if exists {
return id
}
return 0
}

// SetLatestTweetID stores id as the latest tweet ID and associates it with
// screenName.
func (c *CacheProperties) SetLatestTweetID(screenName string, id int64) {
c.LatestTweetID[screenName] = id
}

// GetLatestFavoriteID returns the latest favorite tweet ID of screenName.
// If there is no ID of screenName in c, this returns 0 (tweet ID can't be 0,
// which is known by Twitter API specification).
Expand Down
27 changes: 0 additions & 27 deletions data/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,6 @@ func TestFileCache_Save(t *testing.T) {
require.Error(t, c.Save())
}

func TestFileCache_LatestTweetID(t *testing.T) {
c, err := NewFileCache("cache.json")
if err != nil {
t.Fatal(err)
}
testCacheLatestTweetID(t, c)
}

func TestDBCache_LatestTweetID(t *testing.T) {
t.Skip("You must write mocking test for this")
c, err := NewDBCache(nil, "")
if err != nil {
t.Fatal(err)
}
defer os.Remove("test.db")
testCacheLatestTweetID(t, c)
}

func testCacheLatestTweetID(t *testing.T, c Cache) {
name := "foo"
var tweetID int64
tweetID = 1
c.SetLatestTweetID(name, tweetID)
require.Equal(t, tweetID, c.GetLatestTweetID(name))
require.EqualValues(t, 0, c.GetLatestTweetID("bar"))
}

func TestFileCache_LatestFavoriteID(t *testing.T) {
c, err := NewFileCache("cache.json")
if err != nil {
Expand Down
62 changes: 7 additions & 55 deletions lib/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,12 @@ func (a *TwitterAPI) ProcessFavorites(
slack *SlackAPI,
action data.Action,
) ([]anaconda.Tweet, error) {
latestID := a.cache.GetLatestFavoriteID(name)
v.Set("screen_name", name)
if latestID > 0 {
v.Set("since_id", fmt.Sprintf("%d", latestID))
} else {
// If the latest favorite ID doesn't exist, this fetches just
// the latest tweet and store that ID.
v.Set("count", "1")
}
tweets, err := a.api.GetFavorites(v)
if err != nil {
return nil, utils.WithStack(err)
}
var pp TwitterPostProcessor
if c.ShouldRepeat() {
pp = &TwitterPostProcessorEach{action, a.cache}
} else {
pp = &TwitterPostProcessorTop{action, name, a.cache}
}
result, err := a.processTweets(tweets, c, vision, lang, slack, action, pp)
result, err := a.processTweets(tweets, c, vision, lang, slack, action, a.cache)
if err != nil {
return nil, utils.WithStack(err)
}
Expand All @@ -140,57 +126,21 @@ func (a *TwitterAPI) ProcessSearch(
if err != nil {
return nil, utils.WithStack(err)
}
pp := &TwitterPostProcessorEach{action, a.cache}
result, err := a.processTweets(res.Statuses, c, vision, lang, slack, action, pp)
result, err := a.processTweets(res.Statuses, c, vision, lang, slack, action, a.cache)
if err != nil {
return nil, utils.WithStack(err)
}
return result, utils.WithStack(err)
}

type (
TwitterPostProcessor interface {
Process(anaconda.Tweet, bool) error
}
TwitterPostProcessorTop struct {
action data.Action
screenName string
cache data.Cache
}
TwitterPostProcessorEach struct {
action data.Action
cache data.Cache
}
)

func (p *TwitterPostProcessorTop) Process(t anaconda.Tweet, match bool) error {
id := p.cache.GetLatestTweetID(p.screenName)
if t.Id > id {
p.cache.SetLatestTweetID(p.screenName, t.Id)
}
if match {
ac := p.cache.GetTweetAction(t.Id)
p.cache.SetTweetAction(t.Id, ac.Add(p.action))
}
return nil
}

func (p *TwitterPostProcessorEach) Process(t anaconda.Tweet, match bool) error {
if match {
ac := p.cache.GetTweetAction(t.Id)
p.cache.SetTweetAction(t.Id, ac.Add(p.action))
}
return nil
}

func (a *TwitterAPI) processTweets(
tweets []anaconda.Tweet,
c TweetChecker,
v VisionMatcher,
l LanguageMatcher,
slack *SlackAPI,
action data.Action,
pp TwitterPostProcessor,
cache data.Cache,
) ([]anaconda.Tweet, error) {
result := []anaconda.Tweet{}
// From the oldest to the newest
Expand All @@ -208,7 +158,10 @@ func (a *TwitterAPI) processTweets(
}
result = append(result, t)
}
err = pp.Process(t, match)
if match {
ac := cache.GetTweetAction(t.Id)
cache.SetTweetAction(t.Id, ac.Add(action))
}
if err != nil {
return nil, utils.WithStack(err)
}
Expand Down Expand Up @@ -355,7 +308,6 @@ func (l *TwitterUserListener) Listen(
if err != nil {
return utils.WithStack(err)
}
l.api.cache.SetLatestTweetID(name, c.Id)
}
}
err := l.api.cache.Save()
Expand Down
37 changes: 0 additions & 37 deletions lib/twitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,9 @@ import (
"testing"

"github.com/iwataka/anaconda"
"github.com/iwataka/deep"
"github.com/iwataka/mybot/data"
"github.com/stretchr/testify/require"
)

func TestTwitterPostProcessorEach(t *testing.T) {
action := data.Action{
Twitter: data.TwitterAction{
Collections: []string{"foo"},
},
Slack: data.SlackAction{
Channels: []string{"bar"},
Reactions: []string{},
},
}
action.Twitter.Retweet = true
cache, err := data.NewFileCache("")
require.NoError(t, err)
tweet := anaconda.Tweet{}
tweet.IdStr = "000"
pp := TwitterPostProcessorEach{action, cache}

err = pp.Process(tweet, true)
require.NoError(t, err)
ac := cache.GetTweetAction(tweet.Id)
require.Nil(t, deep.Equal(ac, action))

action2 := data.Action{
Twitter: data.NewTwitterAction(),
Slack: data.NewSlackAction(),
}
action2.Twitter.Favorite = true
pp2 := TwitterPostProcessorEach{action2, cache}

err = pp2.Process(tweet, true)
require.NoError(t, err)
ac2 := cache.GetTweetAction(tweet.Id)
require.True(t, ac2.Twitter.Favorite)
}

func Test_CheckTwitterError(t *testing.T) {
err130 := anaconda.TwitterError{Code: 130}
testCheckTwitterError(t, err130)
Expand Down
18 changes: 0 additions & 18 deletions mocks/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ func (_mr *_MockCacheRecorder) GetLatestImages(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetLatestImages", arg0)
}

func (_m *MockCache) GetLatestTweetID(_param0 string) int64 {
ret := _m.ctrl.Call(_m, "GetLatestTweetID", _param0)
ret0, _ := ret[0].(int64)
return ret0
}

func (_mr *_MockCacheRecorder) GetLatestTweetID(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetLatestTweetID", arg0)
}

func (_m *MockCache) GetTweetAction(_param0 int64) data.Action {
ret := _m.ctrl.Call(_m, "GetTweetAction", _param0)
ret0, _ := ret[0].(data.Action)
Expand Down Expand Up @@ -114,14 +104,6 @@ func (_mr *_MockCacheRecorder) SetLatestFavoriteID(arg0, arg1 interface{}) *gomo
return _mr.mock.ctrl.RecordCall(_mr.mock, "SetLatestFavoriteID", arg0, arg1)
}

func (_m *MockCache) SetLatestTweetID(_param0 string, _param1 int64) {
_m.ctrl.Call(_m, "SetLatestTweetID", _param0, _param1)
}

func (_mr *_MockCacheRecorder) SetLatestTweetID(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "SetLatestTweetID", arg0, arg1)
}

func (_m *MockCache) SetTweetAction(_param0 int64, _param1 data.Action) {
_m.ctrl.Call(_m, "SetTweetAction", _param0, _param1)
}
Expand Down

0 comments on commit c617c09

Please sign in to comment.