2021-10-08 12:39:31 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
|
|
|
|
//
|
2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
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
|
|
|
|
2022-11-07 09:18:13 +00:00
|
|
|
refs "github.com/ssbc/go-ssb-refs"
|
|
|
|
"github.com/ssbc/go-ssb-room/v2/roomdb"
|
|
|
|
"github.com/ssbc/go-ssb-room/v2/roomdb/sqlite/models"
|
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-19 11:28:14 +00:00
|
|
|
var _ roomdb.DeniedKeysService = (*DeniedKeys)(nil)
|
2021-02-08 16:47:42 +00:00
|
|
|
|
2021-03-19 11:28:14 +00:00
|
|
|
// The DeniedKeys is backed by the members table
|
|
|
|
type DeniedKeys 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-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) Add(ctx context.Context, a refs.FeedRef, comment string) error {
|
2021-02-11 15:43:19 +00:00
|
|
|
// TODO: better valid
|
2022-11-07 09:18:13 +00:00
|
|
|
if _, err := refs.ParseFeedRef(a.String()); err != nil {
|
2021-02-11 15:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-19 11:28:14 +00:00
|
|
|
var entry models.DeniedKey
|
2021-02-11 15:43:19 +00:00
|
|
|
entry.PubKey.FeedRef = a
|
2021-03-19 11:28:14 +00:00
|
|
|
entry.Comment = comment
|
2021-03-11 16:57:55 +00:00
|
|
|
|
2021-03-19 11:28:14 +00:00
|
|
|
err := entry.Insert(ctx, dk.db, boil.Whitelist("pub_key", "comment"))
|
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-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) HasFeed(ctx context.Context, h refs.FeedRef) bool {
|
2022-11-07 09:18:13 +00:00
|
|
|
_, err := models.DeniedKeys(qm.Where("pub_key = ?", h.String())).One(ctx, dk.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-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) HasID(ctx context.Context, id int64) bool {
|
|
|
|
_, err := models.FindDeniedKey(ctx, dk.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-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) GetByID(ctx context.Context, id int64) (roomdb.ListEntry, error) {
|
|
|
|
var entry roomdb.ListEntry
|
|
|
|
found, err := models.FindDeniedKey(ctx, dk.db, id)
|
2021-02-15 10:02:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-03-19 11:28:14 +00:00
|
|
|
return entry, roomdb.ErrNotFound
|
2021-02-15 10:02:45 +00:00
|
|
|
}
|
2021-03-19 11:28:14 +00:00
|
|
|
return entry, err
|
2021-02-15 10:02:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 11:28:14 +00:00
|
|
|
entry.ID = found.ID
|
|
|
|
entry.PubKey = found.PubKey.FeedRef
|
|
|
|
entry.Comment = found.Comment
|
|
|
|
entry.CreatedAt = found.CreatedAt
|
|
|
|
return entry, nil
|
2021-02-15 10:02:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 15:43:19 +00:00
|
|
|
// List returns a list of all the feeds.
|
2021-03-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) List(ctx context.Context) ([]roomdb.ListEntry, error) {
|
|
|
|
all, err := models.DeniedKeys().All(ctx, dk.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-19 11:28:14 +00:00
|
|
|
n := len(all)
|
|
|
|
|
|
|
|
var lst = make([]roomdb.ListEntry, n)
|
|
|
|
for i, entry := range all {
|
|
|
|
lst[i].ID = entry.ID
|
|
|
|
lst[i].PubKey = entry.PubKey.FeedRef
|
|
|
|
lst[i].Comment = entry.Comment
|
|
|
|
lst[i].CreatedAt = entry.CreatedAt
|
2021-02-11 15:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 11:28:14 +00:00
|
|
|
return lst, nil
|
2021-02-11 15:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:21:26 +00:00
|
|
|
func (dk DeniedKeys) Count(ctx context.Context) (uint, error) {
|
2021-03-29 14:33:15 +00:00
|
|
|
count, err := models.DeniedKeys().Count(ctx, dk.db)
|
|
|
|
if err != nil {
|
2021-03-29 15:21:26 +00:00
|
|
|
return 0, err
|
2021-03-29 14:33:15 +00:00
|
|
|
}
|
2021-03-29 15:21:26 +00:00
|
|
|
return uint(count), nil
|
2021-03-29 14:33:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:40:43 +00:00
|
|
|
// RemoveFeed removes the feed from the list.
|
2021-03-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) RemoveFeed(ctx context.Context, r refs.FeedRef) error {
|
2022-11-07 09:18:13 +00:00
|
|
|
entry, err := models.DeniedKeys(qm.Where("pub_key = ?", r.String())).One(ctx, dk.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-19 11:28:14 +00:00
|
|
|
_, err = entry.Delete(ctx, dk.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-19 11:28:14 +00:00
|
|
|
func (dk DeniedKeys) RemoveID(ctx context.Context, id int64) error {
|
|
|
|
entry, err := models.FindDeniedKey(ctx, dk.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-19 11:28:14 +00:00
|
|
|
_, err = entry.Delete(ctx, dk.db)
|
2021-02-11 15:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|