Skip to content

Commit

Permalink
Remove code which supported events without certain fields
Browse files Browse the repository at this point in the history
Old "tweet created" events didn't have event and created at fields. Now
that a week has passed they were all dropped.
  • Loading branch information
boreq committed Nov 30, 2023
1 parent 9228315 commit 0e60f7f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 86 deletions.
2 changes: 1 addition & 1 deletion service/adapters/sqlite/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 4 additions & 18 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,15 @@ 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(
accountID accounts.AccountID,
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,
Expand All @@ -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
}

Expand Down
23 changes: 6 additions & 17 deletions service/app/handler_send_tweet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
23 changes: 3 additions & 20 deletions service/app/handler_send_tweet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,39 +17,23 @@ 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,
},
{
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,
},
Expand Down
17 changes: 2 additions & 15 deletions service/ports/sqlitepubsub/tweet_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
17 changes: 2 additions & 15 deletions service/ports/sqlitepubsub/tweet_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -45,7 +36,7 @@ func TestTweetCreatedEventSubscriber_CanHandleOldAndNewEvents(t *testing.T) {
ExpectedCommand: app.NewSendTweet(
accounts.MustNewAccountID("someAccountID"),
domain.NewTweet("someTweetText"),
&event,
event,
),
},
}
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 0e60f7f

Please sign in to comment.