Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ linters:
- depguard
- wsl
- testpackage
- gomodguard # deprecated
settings:
gocritic:
enable-all: true
Expand Down
13 changes: 7 additions & 6 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ func snapshotSubscriber(sub *Subscriber) *Subscriber {
}

out := &Subscriber{
ID: sub.ID,
API: sub.API,
Contact: sub.Contact,
Events: snapshotEvents(sub.Events),
Admin: sub.Admin,
Ignored: sub.Ignored,
ID: sub.ID,
API: sub.API,
Contact: sub.Contact,
Events: snapshotEvents(sub.Events),
Admin: sub.Admin,
Ignored: sub.Ignored,
FirstSeen: sub.FirstSeen,
}

if sub.Meta != nil {
Expand Down
42 changes: 33 additions & 9 deletions subscriber.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package subscribe

import "time"

/**************************
* Subscriber Methods *
**************************/
Expand All @@ -19,10 +21,11 @@ func (s *Subscribe) CreateSub(contact, api string, admin, ignore bool) *Subscrib
}

s.Subscribers = append(s.Subscribers, &Subscriber{
Contact: contact,
API: api,
Admin: admin,
Ignored: ignore,
Contact: contact,
API: api,
Admin: admin,
Ignored: ignore,
FirstSeen: time.Now().UTC(),
Events: &Events{
Map: make(map[string]*Rules),
},
Expand Down Expand Up @@ -50,11 +53,12 @@ func (s *Subscribe) CreateSubWithID(subID int64, contact, api string, admin, ign
}

sub := &Subscriber{
ID: subID,
Contact: contact,
API: api,
Admin: admin,
Ignored: ignore,
ID: subID,
Contact: contact,
API: api,
Admin: admin,
Ignored: ignore,
FirstSeen: time.Now().UTC(),
Events: &Events{
Map: make(map[string]*Rules),
},
Expand All @@ -64,6 +68,26 @@ func (s *Subscribe) CreateSubWithID(subID int64, contact, api string, admin, ign
return sub
}

// DeleteSubscriber removes a subscriber by ID + API. Returns ErrSubscriberNotFound if missing.
func (s *Subscribe) DeleteSubscriber(subID int64, api string) error {
if subID == 0 {
return ErrSubscriberNotFound
}

s.mu.Lock()
defer s.mu.Unlock()

for i, sub := range s.Subscribers {
if sub.ID == subID && sub.API == api {
s.Subscribers = append(s.Subscribers[:i], s.Subscribers[i+1:]...)

return nil
}
}
Comment on lines +80 to +86

return ErrSubscriberNotFound
}

/* Convenience methods to access specific types of subscribers. */

// GetSubscriber gets a subscriber based on their contact info.
Expand Down
18 changes: 18 additions & 0 deletions subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,35 @@ func TestCreateSubWithID(t *testing.T) {
assert.EqualValues(t, 10, first.ID)
assert.True(t, first.Admin)
assert.False(t, first.Ignored)
assert.False(t, first.FirstSeen.IsZero())
assert.Len(t, sub.Subscribers, 1)

seen := first.FirstSeen
second := sub.CreateSubWithID(10, "contact-new", "api", false, true)
require.NotNil(t, second)
assert.Same(t, first, second)
assert.False(t, second.Admin)
assert.True(t, second.Ignored)
assert.Equal(t, "contact", second.Contact)
assert.Equal(t, seen, second.FirstSeen)
assert.Len(t, sub.Subscribers, 1)
}

func TestDeleteSubscriber(t *testing.T) {
t.Parallel()

sub := &Subscribe{Events: new(Events)}
sub.CreateSubWithID(10, "a", "api", false, false)
sub.CreateSubWithID(20, "b", "api", false, false)

require.NoError(t, sub.DeleteSubscriber(10, "api"))
assert.Len(t, sub.Subscribers, 1)
assert.EqualValues(t, 20, sub.Subscribers[0].ID)

assert.Equal(t, ErrSubscriberNotFound, sub.DeleteSubscriber(10, "api"))
assert.Equal(t, ErrSubscriberNotFound, sub.DeleteSubscriber(0, "api"))
}

func TestGetSubscriberByID(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 7 additions & 5 deletions subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ func TestCheckAPI(t *testing.T) {

assertions := assert.New(t)

const event = "event"

subscriber := &Subscribe{Events: new(Events)}
assertions.True(subscriber.checkAPI("test_string"), "an empty slice must always return true")

subscriber.EnableAPIs = []string{"event", "test_string"}
subscriber.EnableAPIs = []string{event, "test_string"}
assertions.True(subscriber.checkAPI("test_string://event"), "test_string is an allowed api prefix")

subscriber.EnableAPIs = []string{"event", "any"}
subscriber.EnableAPIs = []string{event, "any"}
assertions.True(subscriber.checkAPI("test_string"), "any as an allowed value must return true")

subscriber.EnableAPIs = []string{"event", "all"}
subscriber.EnableAPIs = []string{event, "all"}
assertions.True(subscriber.checkAPI("test_string"), "all as an allowed value must return true")

subscriber.EnableAPIs = []string{"event", "test_string"}
subscriber.EnableAPIs = []string{event, "test_string"}
assertions.True(subscriber.checkAPI("test_string"), "test_string is an allowed api")

subscriber.EnableAPIs = []string{"event", "test_string2"}
subscriber.EnableAPIs = []string{event, "test_string2"}
assertions.False(subscriber.checkAPI("test_string"), "test_string is not an allowed api")
}

Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Subscriber struct {
Admin bool `json:"isAdmin"`
// Ignored will exclude a user from GetSubscribers().
Ignored bool `json:"ignored"`
// FirstSeen is when this subscriber record was first created. Zero if unknown (legacy rows).
FirstSeen time.Time `json:"firstSeen,omitzero"`
}

// Events represents the map of tracked global Events.
Expand Down
Loading