diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3093957..e67ef81 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,4 +26,4 @@ tests-config-pkg: tests-docbuf-pkg: stage: test script: - - go test ./internal/docbuf + - go test ./pkg/docbuf diff --git a/internal/state/events.go b/internal/state/events.go new file mode 100644 index 0000000..a24b91f --- /dev/null +++ b/internal/state/events.go @@ -0,0 +1,175 @@ +package state + +import ( + "errors" + "time" + "fmt" + + "gitlab.com/whom/bingobot/internal/logging" +) + +type VoteEvent map[string]string + +const ( + VoteMissingKeyError = "vote data not found: " + VoteCreatedKey = "created" + VoteRequesterKey = "requester" + VoteActionKey = "action" + + VoteResultKey = "result" + VoteResultPass = "pass" + VoteResultFail = "fail" + VoteResultTie = "tie" + VoteResultTimeout = "timeout" + VoteBadResultError = "vote has invalid result: " + VoteNotFinishedError = "vote has result but isnt finished" + VoteMissingResultError = "vote finished but missing result" + + VoteStatusKey = "status" + VoteStatusInProgress = "in_progress" + VoteStatusFinalized = "finalized" + VoteStatusTimeout = "timed_out" + VoteBadStatusError = "vote has invalid status: " + + VeryOldVote = "1990-01-01T00:00:00Z" +) + +func (ve VoteEvent) Type() EventType { + return Vote +} + +func (ve VoteEvent) Time() time.Time { + t, e := time.Parse(time.RFC3339, ve[VoteCreatedKey]) + if e != nil { + // we have a corrupted event + // return old time so that this event gets + // pruned from cache + logging.Warn( + "pruning corrupted vote event", + "event", + fmt.Sprintf("%+v", ve), + ) + tooOld, _ := time.Parse( + time.RFC3339, + VeryOldVote, + ) + return tooOld + } + return t +} + +func (ve VoteEvent) Data() map[string]string { + return map[string]string(ve) +} + +func (ve VoteEvent) Validate() error { + // make sure action, requester, and created are set + for _, key := range []string{ + VoteActionKey, + VoteRequesterKey, + VoteCreatedKey, + VoteStatusKey, + } { + if _, found := ve[key]; !found { + return errors.New(VoteMissingKeyError + key) + } + } + + status := ve[VoteStatusKey] + if status != VoteStatusTimeout && + status != VoteStatusInProgress && + status != VoteStatusFinalized { + return errors.New(VoteBadStatusError + status) + } + + result, hasResult := ve[VoteResultKey] + if hasResult && status == VoteStatusInProgress { + return errors.New(VoteNotFinishedError) + } + if status != VoteStatusInProgress && !hasResult { + return errors.New(VoteMissingResultError) + } + + if hasResult && + (result != VoteResultPass && + result != VoteResultFail && + result != VoteResultTie && + result != VoteResultTimeout) { + + return errors.New(VoteBadResultError + result) + } + + return nil +} + +type UserEvent struct { + uid string + created time.Time +} + +const ( + UserEventUserKey = "user" + UserEventCreatedKey = "created" + UserEventBadUserError = "event has bad user" +) + +func (ue UserEvent) Time() time.Time { + return ue.created +} + +func (ue UserEvent) Data() map[string]string { + return map[string]string{ + UserEventUserKey: ue.uid, + UserEventCreatedKey: ue.created.Format(time.RFC3339), + } +} + +func (ue UserEvent) Validate() error { + // empty for now, we may do some validation later. + return nil +} + +type ChallengeEvent struct { + UserEvent +} + +func (ce ChallengeEvent) Type() EventType { + return Challenge +} + +func NewChallengeEvent(user string) ChallengeEvent { + return ChallengeEvent{UserEvent{ + uid: user, + created: time.Now(), + }} +} + +type RestorationEvent struct { + UserEvent +} + +func (re RestorationEvent) Type() EventType { + return Restoration +} + +func NewRestorationEvent(user string) RestorationEvent { + return RestorationEvent{UserEvent{ + uid: user, + created: time.Now(), + }} +} + +type UserActiveEvent struct { + UserEvent +} + +func (ua UserActiveEvent) Type() EventType { + return UserActive +} + +func NewUserActiveEvent(user string) UserActiveEvent { + return UserActiveEvent{UserEvent{ + uid: user, + created: time.Now(), + }} +} diff --git a/internal/state/state.go b/internal/state/state.go index 1325546..4986991 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -15,7 +15,7 @@ import ( "errors" "encoding/json" - "gitlab.com/whom/bingobot/internal/docbuf" + "gitlab.com/whom/bingobot/pkg/docbuf" "gitlab.com/whom/bingobot/internal/logging" ) @@ -394,168 +394,3 @@ func GetMatchingEvents( return matches, err } -type VoteEvent map[string]string - -const ( - VoteMissingKeyError = "vote data not found: " - VoteCreatedKey = "created" - VoteRequesterKey = "requester" - VoteActionKey = "action" - - VoteResultKey = "result" - VoteResultPass = "pass" - VoteResultFail = "fail" - VoteResultTie = "tie" - VoteResultTimeout = "timeout" - VoteBadResultError = "vote has invalid result: " - VoteNotFinishedError = "vote has result but isnt finished" - VoteMissingResultError = "vote finished but missing result" - - VoteStatusKey = "status" - VoteStatusInProgress = "in_progress" - VoteStatusFinalized = "finalized" - VoteStatusTimeout = "timed_out" - VoteBadStatusError = "vote has invalid status: " - - VeryOldVote = "1990-01-01T00:00:00Z" -) - -func (ve VoteEvent) Type() EventType { - return Vote -} - -func (ve VoteEvent) Time() time.Time { - t, e := time.Parse(time.RFC3339, ve[VoteCreatedKey]) - if e != nil { - // we have a corrupted event - // return old time so that this event gets - // pruned from cache - logging.Warn( - "pruning corrupted vote event", - "event", - fmt.Sprintf("%+v", ve), - ) - tooOld, _ := time.Parse( - time.RFC3339, - VeryOldVote, - ) - return tooOld - } - return t -} - -func (ve VoteEvent) Data() map[string]string { - return map[string]string(ve) -} - -func (ve VoteEvent) Validate() error { - // make sure action, requester, and created are set - for _, key := range []string{ - VoteActionKey, - VoteRequesterKey, - VoteCreatedKey, - VoteStatusKey, - } { - if _, found := ve[key]; !found { - return errors.New(VoteMissingKeyError + key) - } - } - - status := ve[VoteStatusKey] - if status != VoteStatusTimeout && - status != VoteStatusInProgress && - status != VoteStatusFinalized { - return errors.New(VoteBadStatusError + status) - } - - result, hasResult := ve[VoteResultKey] - if hasResult && status == VoteStatusInProgress { - return errors.New(VoteNotFinishedError) - } - if status != VoteStatusInProgress && !hasResult { - return errors.New(VoteMissingResultError) - } - - if hasResult && - (result != VoteResultPass && - result != VoteResultFail && - result != VoteResultTie && - result != VoteResultTimeout) { - - return errors.New(VoteBadResultError + result) - } - - return nil -} - -type UserEvent struct { - uid string - created time.Time -} - -const ( - UserEventUserKey = "user" - UserEventCreatedKey = "created" - UserEventBadUserError = "event has bad user" -) - -func (ue UserEvent) Time() time.Time { - return ue.created -} - -func (ue UserEvent) Data() map[string]string { - return map[string]string{ - UserEventUserKey: ue.uid, - UserEventCreatedKey: ue.created.Format(time.RFC3339), - } -} - -func (ue UserEvent) Validate() error { - // empty for now, we may do some validation later. - return nil -} - -type ChallengeEvent struct { - UserEvent -} - -func (ce ChallengeEvent) Type() EventType { - return Challenge -} - -func NewChallengeEvent(user string) ChallengeEvent { - return ChallengeEvent{UserEvent{ - uid: user, - created: time.Now(), - }} -} - -type RestorationEvent struct { - UserEvent -} - -func (re RestorationEvent) Type() EventType { - return Restoration -} - -func NewRestorationEvent(user string) RestorationEvent { - return RestorationEvent{UserEvent{ - uid: user, - created: time.Now(), - }} -} - -type UserActiveEvent struct { - UserEvent -} - -func (ua UserActiveEvent) Type() EventType { - return UserActive -} - -func NewUserActiveEvent(user string) UserActiveEvent { - return UserActiveEvent{UserEvent{ - uid: user, - created: time.Now(), - }} -} diff --git a/internal/state/state_test.go b/internal/state/state_test.go index e6d4073..20800f3 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "gitlab.com/whom/bingobot/internal/docbuf" + "gitlab.com/whom/bingobot/pkg/docbuf" "gitlab.com/whom/bingobot/internal/logging" ) diff --git a/internal/docbuf/docbuf.go b/pkg/docbuf/docbuf.go similarity index 100% rename from internal/docbuf/docbuf.go rename to pkg/docbuf/docbuf.go diff --git a/internal/docbuf/docbuf_test.go b/pkg/docbuf/docbuf_test.go similarity index 100% rename from internal/docbuf/docbuf_test.go rename to pkg/docbuf/docbuf_test.go diff --git a/internal/docbuf/read_write_seek_string.go b/pkg/docbuf/read_write_seek_string.go similarity index 100% rename from internal/docbuf/read_write_seek_string.go rename to pkg/docbuf/read_write_seek_string.go diff --git a/internal/docbuf/read_write_seek_string_test.go b/pkg/docbuf/read_write_seek_string_test.go similarity index 100% rename from internal/docbuf/read_write_seek_string_test.go rename to pkg/docbuf/read_write_seek_string_test.go