Documentation ¶
Index ¶
- Constants
- Variables
- type AllowListedSkylink
- type BlockedSkylink
- type DB
- func (db *DB) BlockedHashes(ctx context.Context, sort, skip, limit int) ([]BlockedSkylink, bool, error)
- func (db *DB) Close(ctx context.Context) error
- func (db *DB) CreateAllowListedSkylink(ctx context.Context, skylink *AllowListedSkylink) error
- func (db *DB) CreateBlockedSkylink(ctx context.Context, skylink *BlockedSkylink) error
- func (db *DB) CreateBlockedSkylinkBulk(ctx context.Context, skylinks []BlockedSkylink) (int, error)
- func (db *DB) FindByHash(ctx context.Context, hash Hash) (*BlockedSkylink, error)
- func (db *DB) HashesToBlock(ctx context.Context, from time.Time) ([]Hash, error)
- func (db *DB) HashesToRetry(ctx context.Context) ([]Hash, error)
- func (db *DB) IsAllowListed(ctx context.Context, hash crypto.Hash) (bool, error)
- func (db *DB) MarkFailed(ctx context.Context, hashes []Hash) error
- func (db *DB) MarkInvalid(ctx context.Context, hashes []Hash) error
- func (db *DB) MarkSucceeded(ctx context.Context, hashes []Hash) error
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) Purge(ctx context.Context) error
- type Hash
- type Reporter
Constants ¶
const ( // MongoDefaultTimeout is the timeout for the context used in testing // whenever a context is sent to mongo MongoDefaultTimeout = time.Minute )
Variables ¶
var ( // ErrDuplicateKey is returned when an insert is attempted that violates the // unique constraint on a certain field. ErrDuplicateKey = errors.New("E11000 duplicate key") // ErrIndexCreateFailed is returned when an error occurred when trying to // ensure an index ErrIndexCreateFailed = errors.New("failed to create index") // ErrIndexDropFailed is returned when an error occurred when trying to // drop an index ErrIndexDropFailed = errors.New("failed to drop an index") // ErrNoDocumentsFound is returned when a database operation completes // successfully but it doesn't find or affect any documents. ErrNoDocumentsFound = errors.New("no documents") // ErrNoEntriesUpdated is returned when no entries were updated after an // update was performed. ErrNoEntriesUpdated = errors.New("no entries updated") // ErrSkylinkExists is returned when we try to add a skylink to the database // and it already exists there. ErrSkylinkExists = errors.New("skylink already exists") // ServerUID is a random string that uniquely identifies the server ServerUID string // True is a helper value, so we can pass a *bool to MongoDB's methods. True = true )
Functions ¶
This section is empty.
Types ¶
type AllowListedSkylink ¶
type AllowListedSkylink struct { ID primitive.ObjectID `bson:"_id,omitempty"` Hash Hash `bson:"hash"` Description string `bson:"description"` TimestampAdded time.Time `bson:"timestamp_added"` }
AllowListedSkylink is a skylink that is allow listed and thus prohibited from ever being blocked.
type BlockedSkylink ¶
type BlockedSkylink struct { ID primitive.ObjectID `bson:"_id,omitempty"` Failed bool `bson:"failed"` Hash Hash `bson:"hash"` Invalid bool `bson:"invalid"` Reporter Reporter `bson:"reporter"` Reverted bool `bson:"reverted"` RevertedTags []string `bson:"reverted_tags"` Tags []string `bson:"tags"` TimestampAdded time.Time `bson:"timestamp_added"` TimestampReverted time.Time `bson:"timestamp_reverted"` }
BlockedSkylink is a skylink blocked by an external request.
func (*BlockedSkylink) Validate ¶
func (bsl *BlockedSkylink) Validate() error
Validate is a small helper function that ensures the required properties are set on the BlockedSkylink object.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB holds a connection to the database, as well as helpful shortcuts to collections and utilities.
NOTE: update the 'Purge' method when adding new collections
func New ¶
func New(ctx context.Context, uri string, creds options.Credential, logger *logrus.Logger) (*DB, error)
New creates a new database connection.
func NewCustomDB ¶
func NewCustomDB(ctx context.Context, uri string, dbName string, creds options.Credential, logger *logrus.Logger) (*DB, error)
NewCustomDB creates a new database connection to a database with a custom name.
func (*DB) BlockedHashes ¶
func (db *DB) BlockedHashes(ctx context.Context, sort, skip, limit int) ([]BlockedSkylink, bool, error)
BlockedHashes allows to pass a skip and limit parameter and returns an array of blocked hashes alongside a boolean that indicates whether there's more documents after the current 'page'.
func (*DB) CreateAllowListedSkylink ¶
func (db *DB) CreateAllowListedSkylink(ctx context.Context, skylink *AllowListedSkylink) error
CreateAllowListedSkylink creates a new allowlisted skylink. If the skylink already exists it does nothing and returns without failure.
func (*DB) CreateBlockedSkylink ¶
func (db *DB) CreateBlockedSkylink(ctx context.Context, skylink *BlockedSkylink) error
CreateBlockedSkylink creates a new skylink. If the skylink already exists it returns ErrSkylinkExists.
func (*DB) CreateBlockedSkylinkBulk ¶
CreateBlockedSkylinkBulk creates new blocked skylinks in bulk. It returns the number of created entries.
func (*DB) FindByHash ¶
FindByHash fetches the DB record that corresponds to the given hash from the database.
func (*DB) HashesToBlock ¶
HashesToBlock sweeps the database for unblocked hashes after the given timestamp.
func (*DB) HashesToRetry ¶
HashesToRetry returns all hashes that failed to get blocked the first time around. This is a retry mechanism to ensure we keep retrying to block those hashes, but at the same try 'unblock' the main block loop in order for it to run smoothly.
func (*DB) IsAllowListed ¶
IsAllowListed returns whether the given skylink is on the allow list.
func (*DB) MarkFailed ¶
MarkFailed will mark the given documents as failed
func (*DB) MarkInvalid ¶
MarkInvalid will mark the given documents as invalid
func (*DB) MarkSucceeded ¶
MarkSucceeded will toggle the failed flag for all documents in the given list of hashes that are currently marked as failed.
type Hash ¶
Hash is a struct that embeds the crypto.Hash, allowing us to implement the bsoncodec ValueMarshaler interfaces.
func DiffHashes ¶
DiffHashes is a helper function that returns an array of hashes that are part of the base array but are not present in any of the other arrays.
func NewHash ¶
func NewHash(sl skymodules.Skylink) Hash
NewHash returns the Hash of the given skylink.
func (Hash) MarshalBSONValue ¶
MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.
type Reporter ¶
type Reporter struct { Name string `bson:"name"` Email string `bson:"email"` OtherContact string `bson:"other_contact"` Sub string `bson:"sub,omitempty"` Unauthenticated bool `bson:"unauthenticated,omitempty"` }
Reporter is a person who reported that a given skylink should be blocked.