admindb: add GetByID to AllowListService

This commit is contained in:
Henry 2021-02-15 11:02:45 +01:00
parent ecc11f8bc4
commit d802294418
3 changed files with 102 additions and 1 deletions

View File

@ -20,7 +20,7 @@ type AuthFallbackService interface {
// AuthWithSSBService defines functions needed for the challange/response system of sign-in with ssb
type AuthWithSSBService interface{}
// AllowListService deals with changing the privacy modes and managing the allow/deny lists of the room
// AllowListService changes the lists of people that are allowed to get into the room
type AllowListService interface {
// Add adds the feed to the list.
Add(context.Context, refs.FeedRef) error
@ -31,6 +31,9 @@ type AllowListService interface {
// HasFeed returns true if a feed is on the list.
HasID(context.Context, int64) bool
// GetByID returns the list entry for that ID or an error
GetByID(context.Context, int64) (ListEntry, error)
// List returns a list of all the feeds.
List(context.Context) (ListEntries, error)

View File

@ -22,6 +22,20 @@ type FakeAllowListService struct {
addReturnsOnCall map[int]struct {
result1 error
}
GetByIDStub func(context.Context, int64) (admindb.ListEntry, error)
getByIDMutex sync.RWMutex
getByIDArgsForCall []struct {
arg1 context.Context
arg2 int64
}
getByIDReturns struct {
result1 admindb.ListEntry
result2 error
}
getByIDReturnsOnCall map[int]struct {
result1 admindb.ListEntry
result2 error
}
HasFeedStub func(context.Context, refs.FeedRef) bool
hasFeedMutex sync.RWMutex
hasFeedArgsForCall []struct {
@ -149,6 +163,71 @@ func (fake *FakeAllowListService) AddReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeAllowListService) GetByID(arg1 context.Context, arg2 int64) (admindb.ListEntry, error) {
fake.getByIDMutex.Lock()
ret, specificReturn := fake.getByIDReturnsOnCall[len(fake.getByIDArgsForCall)]
fake.getByIDArgsForCall = append(fake.getByIDArgsForCall, struct {
arg1 context.Context
arg2 int64
}{arg1, arg2})
stub := fake.GetByIDStub
fakeReturns := fake.getByIDReturns
fake.recordInvocation("GetByID", []interface{}{arg1, arg2})
fake.getByIDMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeAllowListService) GetByIDCallCount() int {
fake.getByIDMutex.RLock()
defer fake.getByIDMutex.RUnlock()
return len(fake.getByIDArgsForCall)
}
func (fake *FakeAllowListService) GetByIDCalls(stub func(context.Context, int64) (admindb.ListEntry, error)) {
fake.getByIDMutex.Lock()
defer fake.getByIDMutex.Unlock()
fake.GetByIDStub = stub
}
func (fake *FakeAllowListService) GetByIDArgsForCall(i int) (context.Context, int64) {
fake.getByIDMutex.RLock()
defer fake.getByIDMutex.RUnlock()
argsForCall := fake.getByIDArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeAllowListService) GetByIDReturns(result1 admindb.ListEntry, result2 error) {
fake.getByIDMutex.Lock()
defer fake.getByIDMutex.Unlock()
fake.GetByIDStub = nil
fake.getByIDReturns = struct {
result1 admindb.ListEntry
result2 error
}{result1, result2}
}
func (fake *FakeAllowListService) GetByIDReturnsOnCall(i int, result1 admindb.ListEntry, result2 error) {
fake.getByIDMutex.Lock()
defer fake.getByIDMutex.Unlock()
fake.GetByIDStub = nil
if fake.getByIDReturnsOnCall == nil {
fake.getByIDReturnsOnCall = make(map[int]struct {
result1 admindb.ListEntry
result2 error
})
}
fake.getByIDReturnsOnCall[i] = struct {
result1 admindb.ListEntry
result2 error
}{result1, result2}
}
func (fake *FakeAllowListService) HasFeed(arg1 context.Context, arg2 refs.FeedRef) bool {
fake.hasFeedMutex.Lock()
ret, specificReturn := fake.hasFeedReturnsOnCall[len(fake.hasFeedArgsForCall)]
@ -466,6 +545,8 @@ func (fake *FakeAllowListService) Invocations() map[string][][]interface{} {
defer fake.invocationsMutex.RUnlock()
fake.addMutex.RLock()
defer fake.addMutex.RUnlock()
fake.getByIDMutex.RLock()
defer fake.getByIDMutex.RUnlock()
fake.hasFeedMutex.RLock()
defer fake.hasFeedMutex.RUnlock()
fake.hasIDMutex.RLock()

View File

@ -59,6 +59,23 @@ func (l AllowList) HasID(ctx context.Context, id int64) bool {
return true
}
// GetByID returns the entry if a feed with that ID is on the list.
func (l AllowList) GetByID(ctx context.Context, id int64) (admindb.ListEntry, error) {
var le admindb.ListEntry
entry, err := models.FindAllowList(ctx, l.db, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return le, admindb.ErrNotFound
}
return le, err
}
le.ID = entry.ID
le.PubKey = entry.PubKey.FeedRef
return le, nil
}
// List returns a list of all the feeds.
func (l AllowList) List(ctx context.Context) (admindb.ListEntries, error) {
all, err := models.AllowLists().All(ctx, l.db)