From 0e60f7f2f6fb125fa6032f8e2c0cd8a5e848ae73 Mon Sep 17 00:00:00 2001 From: boreq Date: Wed, 29 Nov 2023 17:31:36 +0100 Subject: [PATCH] Remove code which supported events without certain fields Old "tweet created" events didn't have event and created at fields. Now that a week has passed they were all dropped. --- service/adapters/sqlite/publisher.go | 2 +- service/app/app.go | 22 ++++-------------- service/app/handler_send_tweet.go | 23 +++++-------------- service/app/handler_send_tweet_test.go | 23 +++---------------- service/ports/sqlitepubsub/tweet_created.go | 17 ++------------ .../ports/sqlitepubsub/tweet_created_test.go | 17 ++------------ 6 files changed, 18 insertions(+), 86 deletions(-) diff --git a/service/adapters/sqlite/publisher.go b/service/adapters/sqlite/publisher.go index 41e380e..68f06bf 100644 --- a/service/adapters/sqlite/publisher.go +++ b/service/adapters/sqlite/publisher.go @@ -48,7 +48,7 @@ type TweetCreatedEventTransport struct { AccountID string `json:"accountID"` Tweet TweetTransport `json:"tweet"` Event []byte `json:"event"` - CreatedAt *time.Time `json:"createdAt"` + CreatedAt time.Time `json:"createdAt"` } type TweetTransport struct { diff --git a/service/app/app.go b/service/app/app.go index 2482261..348451a 100644 --- a/service/app/app.go +++ b/service/app/app.go @@ -214,8 +214,8 @@ func (t TwitterAccountDetails) ProfileImageURL() string { type TweetCreatedEvent struct { accountID accounts.AccountID tweet domain.Tweet - createdAt *time.Time - event *domain.Event + createdAt time.Time + event domain.Event } func NewTweetCreatedEvent( @@ -223,20 +223,6 @@ func NewTweetCreatedEvent( tweet domain.Tweet, createdAt time.Time, event domain.Event, -) TweetCreatedEvent { - return TweetCreatedEvent{ - accountID: accountID, - tweet: tweet, - createdAt: &createdAt, - event: &event, - } -} - -func NewTweetCreatedEventFromHistory( - accountID accounts.AccountID, - tweet domain.Tweet, - createdAt *time.Time, - event *domain.Event, ) TweetCreatedEvent { return TweetCreatedEvent{ accountID: accountID, @@ -254,11 +240,11 @@ func (t TweetCreatedEvent) Tweet() domain.Tweet { return t.tweet } -func (t TweetCreatedEvent) CreatedAt() *time.Time { +func (t TweetCreatedEvent) CreatedAt() time.Time { return t.createdAt } -func (t TweetCreatedEvent) Event() *domain.Event { +func (t TweetCreatedEvent) Event() domain.Event { return t.event } diff --git a/service/app/handler_send_tweet.go b/service/app/handler_send_tweet.go index 88c5e99..1291367 100644 --- a/service/app/handler_send_tweet.go +++ b/service/app/handler_send_tweet.go @@ -14,17 +14,13 @@ const ( dropEventsIfNotPostedFor = 7 * 24 * time.Hour ) -var ( - whenEventWasAddedToTweetCreatedEvent = time.Date(2023, time.November, 17, 0, 0, 0, 0, time.UTC) -) - type SendTweet struct { accountID accounts.AccountID tweet domain.Tweet - event *domain.Event + event domain.Event } -func NewSendTweet(accountID accounts.AccountID, tweet domain.Tweet, event *domain.Event) SendTweet { +func NewSendTweet(accountID accounts.AccountID, tweet domain.Tweet, event domain.Event) SendTweet { return SendTweet{ accountID: accountID, tweet: tweet, @@ -40,7 +36,7 @@ func (s SendTweet) Tweet() domain.Tweet { return s.tweet } -func (s SendTweet) Event() *domain.Event { +func (s SendTweet) Event() domain.Event { return s.event } @@ -77,16 +73,9 @@ func (h *SendTweetHandler) Handle(ctx context.Context, cmd SendTweet) (err error WithField("tweet", cmd.tweet.Text()). Message("attempting to post a tweet") - if cmd.event != nil { - dropEventIfPostedBefore := h.currentTimeProvider.GetCurrentTime().Add(-dropEventsIfNotPostedFor) - if cmd.event.CreatedAt().Before(dropEventIfPostedBefore) { - return nil - } - } else { - dropEventIfItIsNilAndCurrentTimeIsAfter := whenEventWasAddedToTweetCreatedEvent.Add(dropEventsIfNotPostedFor) - if h.currentTimeProvider.GetCurrentTime().After(dropEventIfItIsNilAndCurrentTimeIsAfter) { - return nil - } + dropEventIfPostedBefore := h.currentTimeProvider.GetCurrentTime().Add(-dropEventsIfNotPostedFor) + if cmd.event.CreatedAt().Before(dropEventIfPostedBefore) { + return nil } var userTokens *accounts.TwitterUserTokens diff --git a/service/app/handler_send_tweet_test.go b/service/app/handler_send_tweet_test.go index 30b7b69..5cd1397 100644 --- a/service/app/handler_send_tweet_test.go +++ b/service/app/handler_send_tweet_test.go @@ -5,7 +5,6 @@ import ( "time" "github.com/planetary-social/nos-crossposting-service/cmd/crossposting-service/di" - "github.com/planetary-social/nos-crossposting-service/internal" "github.com/planetary-social/nos-crossposting-service/internal/fixtures" "github.com/planetary-social/nos-crossposting-service/service/app" "github.com/planetary-social/nos-crossposting-service/service/domain" @@ -18,31 +17,15 @@ func TestSendTweetHandler_CorrectlyDropsOldEvents(t *testing.T) { Name string CurrentTime time.Time - Event *domain.Event + Event domain.Event ShouldPostTweet bool }{ - { - Name: "old_events_are_dropped_after_a_week_since_code_change_passes", - - CurrentTime: date(2023, time.November, 25), - Event: nil, - - ShouldPostTweet: false, - }, - { - Name: "old_events_are_not_dropped_before_a_week_since_code_change_passes", - - CurrentTime: date(2023, time.November, 23), - Event: nil, - - ShouldPostTweet: true, - }, { Name: "new_events_are_dropped_after_a_week_since_they_were_created", CurrentTime: date(2023, time.November, 28), - Event: internal.Pointer(fixtures.SomeEventWithCreatedAt(date(2023, time.November, 20))), + Event: fixtures.SomeEventWithCreatedAt(date(2023, time.November, 20)), ShouldPostTweet: false, }, @@ -50,7 +33,7 @@ func TestSendTweetHandler_CorrectlyDropsOldEvents(t *testing.T) { Name: "new_events_are_not_dropped_before_a_week_since_they_were_created", CurrentTime: date(2023, time.November, 27), - Event: internal.Pointer(fixtures.SomeEventWithCreatedAt(date(2023, time.November, 20))), + Event: fixtures.SomeEventWithCreatedAt(date(2023, time.November, 20)), ShouldPostTweet: true, }, diff --git a/service/ports/sqlitepubsub/tweet_created.go b/service/ports/sqlitepubsub/tweet_created.go index 0684f00..45ee20e 100644 --- a/service/ports/sqlitepubsub/tweet_created.go +++ b/service/ports/sqlitepubsub/tweet_created.go @@ -67,9 +67,9 @@ func (s *TweetCreatedEventSubscriber) handleMessage(ctx context.Context, msg *sq tweet := domain.NewTweet(transport.Tweet.Text) - event, err := s.getEvent(transport) + event, err := domain.NewEventFromRaw(transport.Event) if err != nil { - return errors.Wrap(err, "error getting the event from transport") + return errors.Wrap(err, "error loading the event") } cmd := app.NewSendTweet(accountID, tweet, event) @@ -80,16 +80,3 @@ func (s *TweetCreatedEventSubscriber) handleMessage(ctx context.Context, msg *sq return nil } - -func (s *TweetCreatedEventSubscriber) getEvent(transport sqlite.TweetCreatedEventTransport) (*domain.Event, error) { - if len(transport.Event) == 0 { - return nil, nil - } - - event, err := domain.NewEventFromRaw(transport.Event) - if err != nil { - return nil, errors.Wrap(err, "error creating a domain event") - } - - return &event, nil -} diff --git a/service/ports/sqlitepubsub/tweet_created_test.go b/service/ports/sqlitepubsub/tweet_created_test.go index 009b365..2034eea 100644 --- a/service/ports/sqlitepubsub/tweet_created_test.go +++ b/service/ports/sqlitepubsub/tweet_created_test.go @@ -26,15 +26,6 @@ func TestTweetCreatedEventSubscriber_CanHandleOldAndNewEvents(t *testing.T) { Payload string ExpectedCommand app.SendTweet }{ - { - Name: "old", - Payload: `{"accountID": "someAccountID", "tweet": {"text": "someTweetText"}}`, - ExpectedCommand: app.NewSendTweet( - accounts.MustNewAccountID("someAccountID"), - domain.NewTweet("someTweetText"), - nil, - ), - }, { Name: "new", Payload: fmt.Sprintf( @@ -45,7 +36,7 @@ func TestTweetCreatedEventSubscriber_CanHandleOldAndNewEvents(t *testing.T) { ExpectedCommand: app.NewSendTweet( accounts.MustNewAccountID("someAccountID"), domain.NewTweet("someTweetText"), - &event, + event, ), }, } @@ -76,11 +67,7 @@ func TestTweetCreatedEventSubscriber_CanHandleOldAndNewEvents(t *testing.T) { call := calls[0] assert.Equal(t, call.AccountID(), testCase.ExpectedCommand.AccountID()) assert.Equal(t, call.Tweet(), testCase.ExpectedCommand.Tweet()) - if testCase.ExpectedCommand.Event() == nil { - require.Nil(t, call.Event()) - } else { - assert.Equal(t, call.Event().Raw(), testCase.ExpectedCommand.Event().Raw()) - } + assert.Equal(t, call.Event().Raw(), testCase.ExpectedCommand.Event().Raw()) } }, 1*time.Second, 100*time.Millisecond) })