go-ssb-room/roomdb/sqlite/denied.go

152 lines
3.6 KiB
Go
Raw Normal View History

// 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
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
)
// 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
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-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
}
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
}
// 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 {
_, 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
}
// 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)
if err != nil {
return false
}
return true
}
// 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)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
2021-03-19 11:28:14 +00:00
return entry, roomdb.ErrNotFound
}
2021-03-19 11:28:14 +00:00
return entry, err
}
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-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
}
func (dk DeniedKeys) Count(ctx context.Context) (uint, error) {
count, err := models.DeniedKeys().Count(ctx, dk.db)
if err != nil {
return 0, err
}
return uint(count), nil
}
// 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 {
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 {
if errors.Is(err, sql.ErrNoRows) {
2021-03-10 15:44:46 +00:00
return roomdb.ErrNotFound
}
return err
}
2021-03-19 11:28:14 +00:00
_, err = entry.Delete(ctx, dk.db)
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)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
2021-03-10 15:44:46 +00:00
return roomdb.ErrNotFound
}
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
}