2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2021-02-08 16:47:42 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2021-02-11 15:43:19 +00:00
|
|
|
"context"
|
2021-02-08 16:47:42 +00:00
|
|
|
"database/sql"
|
2021-02-11 15:43:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-02-15 16:21:06 +00:00
|
|
|
"github.com/friendsofgo/errors"
|
|
|
|
"github.com/mattn/go-sqlite3"
|
2021-02-11 15:43:19 +00:00
|
|
|
"github.com/volatiletech/sqlboiler/v4/boil"
|
|
|
|
"github.com/volatiletech/sqlboiler/v4/queries/qm"
|
2021-02-08 16:47:42 +00:00
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
|
2021-02-11 15:43:19 +00:00
|
|
|
refs "go.mindeco.de/ssb-refs"
|
2021-02-08 16:47:42 +00:00
|
|
|
)
|
|
|
|
|
2021-03-02 16:14:02 +00:00
|
|
|
// compiler assertion to ensure the struct fullfills the interface
|
2021-03-18 16:49:52 +00:00
|
|
|
var _ roomdb.DeniedListService = (*DeniedList)(nil)
|
2021-02-08 16:47:42 +00:00
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
// The DeniedList is backed by the members table
|
|
|
|
type DeniedList struct {
|
2021-02-08 16:47:42 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
2021-02-11 15:43:19 +00:00
|
|
|
|
|
|
|
// Add adds the feed to the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) Add(ctx context.Context, a refs.FeedRef) error {
|
2021-03-02 16:14:02 +00:00
|
|
|
// single insert transaction but this makes it easier to re-use in invites.Consume
|
2021-03-11 07:52:53 +00:00
|
|
|
return transact(al.db, func(tx *sql.Tx) error {
|
|
|
|
return al.add(ctx, tx, a)
|
2021-03-02 16:14:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// this add is not exported and for internal use with transactions.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) add(ctx context.Context, tx *sql.Tx, a refs.FeedRef) error {
|
2021-02-11 15:43:19 +00:00
|
|
|
// TODO: better valid
|
|
|
|
if _, err := refs.ParseFeedRef(a.Ref()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
var entry models.Member
|
2021-02-11 15:43:19 +00:00
|
|
|
entry.PubKey.FeedRef = a
|
2021-03-11 16:57:55 +00:00
|
|
|
|
2021-03-02 16:14:02 +00:00
|
|
|
err := entry.Insert(ctx, tx, boil.Whitelist("pub_key"))
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
2021-02-15 16:21:06 +00:00
|
|
|
var sqlErr sqlite3.Error
|
|
|
|
if errors.As(err, &sqlErr) && sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique {
|
2021-03-10 15:44:46 +00:00
|
|
|
return roomdb.ErrAlreadyAdded{Ref: a}
|
2021-02-15 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
return fmt.Errorf("Denied-list: failed to insert new entry %s: %w - type:%T", entry.PubKey, err, err)
|
2021-02-11 15:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:40:43 +00:00
|
|
|
// HasFeed returns true if a feed is on the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) HasFeed(ctx context.Context, h refs.FeedRef) bool {
|
|
|
|
_, err := models.DeniedLists(qm.Where("pub_key = ?", h.Ref())).One(ctx, al.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:40:43 +00:00
|
|
|
// HasID returns true if a feed is on the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) HasID(ctx context.Context, id int64) bool {
|
|
|
|
_, err := models.FindDeniedList(ctx, al.db, id)
|
2021-02-15 09:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-15 10:02:45 +00:00
|
|
|
// GetByID returns the entry if a feed with that ID is on the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) GetByID(ctx context.Context, id int64) (roomdb.ListEntry, error) {
|
2021-03-10 15:44:46 +00:00
|
|
|
var le roomdb.ListEntry
|
2021-03-18 16:49:52 +00:00
|
|
|
entry, err := models.FindDeniedList(ctx, al.db, id)
|
2021-02-15 10:02:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-03-10 15:44:46 +00:00
|
|
|
return le, roomdb.ErrNotFound
|
2021-02-15 10:02:45 +00:00
|
|
|
}
|
|
|
|
return le, err
|
|
|
|
}
|
|
|
|
|
|
|
|
le.ID = entry.ID
|
|
|
|
le.PubKey = entry.PubKey.FeedRef
|
|
|
|
|
|
|
|
return le, nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:43:19 +00:00
|
|
|
// List returns a list of all the feeds.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) List(ctx context.Context) (roomdb.ListEntries, error) {
|
|
|
|
all, err := models.DeniedLists().All(ctx, al.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
var asRefs = make(roomdb.ListEntries, len(all))
|
2021-03-18 16:49:52 +00:00
|
|
|
for i, Denieded := range all {
|
2021-03-10 15:44:46 +00:00
|
|
|
asRefs[i] = roomdb.ListEntry{
|
2021-03-18 16:49:52 +00:00
|
|
|
ID: Denieded.ID,
|
|
|
|
PubKey: Denieded.PubKey.FeedRef,
|
2021-02-15 09:40:43 +00:00
|
|
|
}
|
2021-02-11 15:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return asRefs, nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:40:43 +00:00
|
|
|
// RemoveFeed removes the feed from the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) RemoveFeed(ctx context.Context, r refs.FeedRef) error {
|
|
|
|
entry, err := models.DeniedLists(qm.Where("pub_key = ?", r.Ref())).One(ctx, al.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
2021-02-15 09:40:43 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-03-10 15:44:46 +00:00
|
|
|
return roomdb.ErrNotFound
|
2021-02-15 09:40:43 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:52:53 +00:00
|
|
|
_, err = entry.Delete(ctx, al.db)
|
2021-02-15 09:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveID removes the feed from the list.
|
2021-03-18 16:49:52 +00:00
|
|
|
func (al DeniedList) RemoveID(ctx context.Context, id int64) error {
|
|
|
|
entry, err := models.FindDeniedList(ctx, al.db, id)
|
2021-02-15 09:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-03-10 15:44:46 +00:00
|
|
|
return roomdb.ErrNotFound
|
2021-02-15 09:40:43 +00:00
|
|
|
}
|
2021-02-11 15:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:52:53 +00:00
|
|
|
_, err = entry.Delete(ctx, al.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|