Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINOR] Convert bson.D to bson.M where possible. #193

Merged
merged 3 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions database/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ func (db *DB) APIKeyDelete(ctx context.Context, user User, akID primitive.Object

// APIKeyByKey returns a specific API key.
func (db *DB) APIKeyByKey(ctx context.Context, key string) (APIKeyRecord, error) {
filter := bson.M{"key": key}
sr := db.staticAPIKeys.FindOne(ctx, filter)
sr := db.staticAPIKeys.FindOne(ctx, bson.M{"key": key})
if sr.Err() != nil {
return APIKeyRecord{}, sr.Err()
}
Expand All @@ -202,8 +201,7 @@ func (db *DB) APIKeyByKey(ctx context.Context, key string) (APIKeyRecord, error)

// APIKeyGet returns a specific API key.
func (db *DB) APIKeyGet(ctx context.Context, akID primitive.ObjectID) (APIKeyRecord, error) {
filter := bson.M{"_id": akID}
sr := db.staticAPIKeys.FindOne(ctx, filter)
sr := db.staticAPIKeys.FindOne(ctx, bson.M{"_id": akID})
if sr.Err() != nil {
return APIKeyRecord{}, sr.Err()
}
Expand Down
13 changes: 6 additions & 7 deletions database/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type DownloadResponse struct {
// DownloadByID fetches a single download from the DB.
func (db *DB) DownloadByID(ctx context.Context, id primitive.ObjectID) (*Download, error) {
var d Download
filter := bson.D{{"_id", id}}
sr := db.staticDownloads.FindOne(ctx, filter)
sr := db.staticDownloads.FindOne(ctx, bson.M{"_id": id})
err := sr.Decode(&d)
if err != nil {
return nil, err
Expand Down Expand Up @@ -129,13 +128,13 @@ func (db *DB) downloadsBy(ctx context.Context, matchStage bson.D, offset, pageSi
// DownloadRecent returns the most recent download of the given skylink.
func (db *DB) DownloadRecent(ctx context.Context, uID primitive.ObjectID, skylinkID primitive.ObjectID) (*Download, error) {
updatedAtThreshold := time.Now().UTC().Add(-1 * DownloadUpdateWindow)
filter := bson.D{
{"user_id", uID},
{"skylink_id", skylinkID},
{"updated_at", bson.D{{"$gt", updatedAtThreshold}}},
filter := bson.M{
"user_id": uID,
"skylink_id": skylinkID,
"updated_at": bson.M{"$gt": updatedAtThreshold},
}
opts := options.FindOneOptions{
Sort: bson.D{{"updated_at", -1}},
Sort: bson.M{"updated_at": -1},
}
sr := db.staticDownloads.FindOne(ctx, filter, &opts)
var d Download
Expand Down
3 changes: 1 addition & 2 deletions database/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ func (db *DB) PurgeEmailCollection(ctx context.Context) (int64, error) {
if build.Release != "testing" {
return 0, nil
}
filter := bson.M{}
dr, err := db.staticEmails.DeleteMany(ctx, filter)
dr, err := db.staticEmails.DeleteMany(ctx, bson.M{})
if err != nil {
return 0, err
}
Expand Down
40 changes: 20 additions & 20 deletions database/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,103 +12,103 @@ var (
Schema = map[string][]mongo.IndexModel{
collUsers: {
{
Keys: bson.D{{"sub", 1}},
Keys: bson.M{"sub": 1},
Options: options.Index().SetName("sub_unique").SetUnique(true),
},
},
collSkylinks: {
{
Keys: bson.D{{"skylink", 1}},
Keys: bson.M{"skylink": 1},
Options: options.Index().SetName("skylink_unique").SetUnique(true),
},
},
collUploads: {
{
Keys: bson.D{{"user_id", 1}},
Keys: bson.M{"user_id": 1},
Options: options.Index().SetName("user_id"),
},
{
Keys: bson.D{{"skylink_id", 1}},
Keys: bson.M{"skylink_id": 1},
Options: options.Index().SetName("skylink_id"),
},
},
collDownloads: {
{
Keys: bson.D{{"user_id", 1}},
Keys: bson.M{"user_id": 1},
Options: options.Index().SetName("user_id"),
},
{
Keys: bson.D{{"skylink_id", 1}},
Keys: bson.M{"skylink_id": 1},
Options: options.Index().SetName("skylink_id"),
},
},
collRegistryReads: {
{
Keys: bson.D{{"user_id", 1}},
Keys: bson.M{"user_id": 1},
Options: options.Index().SetName("user_id"),
},
},
collRegistryWrites: {
{
Keys: bson.D{{"user_id", 1}},
Keys: bson.M{"user_id": 1},
Options: options.Index().SetName("user_id"),
},
},
collEmails: {
{
Keys: bson.D{{"failed_attempts", 1}},
Keys: bson.M{"failed_attempts": 1},
Options: options.Index().SetName("failed_attempts"),
},
{
Keys: bson.D{{"locked_by", 1}},
Keys: bson.M{"locked_by": 1},
Options: options.Index().SetName("locked_by"),
},
{
Keys: bson.D{{"sent_at", 1}},
Keys: bson.M{"sent_at": 1},
Options: options.Index().SetName("sent_at"),
},
{
Keys: bson.D{{"sent_by", 1}},
Keys: bson.M{"sent_by": 1},
Options: options.Index().SetName("sent_by"),
},
},
collChallenges: {
{
Keys: bson.D{{"challenge", 1}},
Keys: bson.M{"challenge": 1},
Options: options.Index().SetName("challenge"),
},
{
Keys: bson.D{{"type", 1}},
Keys: bson.M{"type": 1},
Options: options.Index().SetName("type"),
},
{
Keys: bson.D{{"expires_at", 1}},
Keys: bson.M{"expires_at": 1},
Options: options.Index().SetName("expires_at"),
},
},
collUnconfirmedUserUpdates: {
{
Keys: bson.D{{"challenge_id", 1}},
Keys: bson.M{"challenge_id": 1},
Options: options.Index().SetName("challenge_id"),
},
{
Keys: bson.D{{"expires_at", 1}},
Keys: bson.M{"expires_at": 1},
Options: options.Index().SetName("expires_at"),
},
},
collConfiguration: {
{
Keys: bson.D{{"key", 1}},
Keys: bson.M{"key": 1},
Options: options.Index().SetName("key_unique").SetUnique(true),
},
},
collAPIKeys: {
{
Keys: bson.D{{"key", 1}},
Keys: bson.M{"key": 1},
Options: options.Index().SetName("key_unique").SetUnique(true),
},
{
Keys: bson.D{{"user_id", 1}},
Keys: bson.M{"user_id": 1},
Options: options.Index().SetName("user_id"),
},
},
Expand Down
5 changes: 2 additions & 3 deletions database/skylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {
Skylink: skylinkHash,
}
// Try to find the skylink in the database.
filter := bson.D{{"skylink", skylinkHash}}
filter := bson.M{"skylink": skylinkHash}
upsert := bson.M{"$setOnInsert": bson.M{"skylink": skylinkHash}}
opts := options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)
sr := db.staticSkylinks.FindOneAndUpdate(ctx, filter, upsert, opts)
Expand All @@ -62,8 +62,7 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {

// SkylinkByID finds a skylink by its ID.
func (db *DB) SkylinkByID(ctx context.Context, id primitive.ObjectID) (*Skylink, error) {
filter := bson.D{{"_id", id}}
sr := db.staticSkylinks.FindOne(ctx, filter)
sr := db.staticSkylinks.FindOne(ctx, bson.M{"_id": id})
var sl Skylink
err := sr.Decode(&sl)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions database/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ type UploadResponse struct {
// UploadByID fetches a single upload from the DB.
func (db *DB) UploadByID(ctx context.Context, id primitive.ObjectID) (*Upload, error) {
var d Upload
filter := bson.D{{"_id", id}}
sr := db.staticUploads.FindOne(ctx, filter)
sr := db.staticUploads.FindOne(ctx, bson.M{"_id": id})
err := sr.Decode(&d)
if err != nil {
return nil, err
Expand Down Expand Up @@ -105,10 +104,10 @@ func (db *DB) UnpinUploads(ctx context.Context, skylink Skylink, user User) (int
if user.ID.IsZero() {
return 0, errors.New("invalid user")
}
filter := bson.D{
{"skylink_id", skylink.ID},
{"user_id", user.ID},
{"unpinned", false},
filter := bson.M{
"skylink_id": skylink.ID,
"user_id": user.ID,
"unpinned": false,
}
update := bson.M{"$set": bson.M{"unpinned": true}}
ur, err := db.staticUploads.UpdateMany(ctx, filter, update)
Expand Down
15 changes: 6 additions & 9 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ func (db *DB) UserByEmail(ctx context.Context, email string) (*User, error) {

// UserByID finds a user by their ID.
func (db *DB) UserByID(ctx context.Context, id primitive.ObjectID) (*User, error) {
filter := bson.D{{"_id", id}}
c, err := db.staticUsers.Find(ctx, filter)
c, err := db.staticUsers.Find(ctx, bson.M{"_id": id})
if err != nil {
return nil, errors.AddContext(err, "failed to Find")
}
Expand Down Expand Up @@ -219,8 +218,7 @@ func (db *DB) UserByRecoveryToken(ctx context.Context, token string) (*User, err

// UserByStripeID finds a user by their Stripe customer id.
func (db *DB) UserByStripeID(ctx context.Context, id string) (*User, error) {
filter := bson.D{{"stripe_id", id}}
c, err := db.staticUsers.Find(ctx, filter)
c, err := db.staticUsers.Find(ctx, bson.M{"stripe_id": id})
if err != nil {
return nil, errors.AddContext(err, "failed to Find")
}
Expand Down Expand Up @@ -450,7 +448,7 @@ func (db *DB) UserDelete(ctx context.Context, u *User) error {
return errors.AddContext(ErrUserNotFound, "user struct not fully initialised")
}
// Delete all data associated with this user.
filter := bson.D{{"user_id", u.ID}}
filter := bson.M{"user_id": u.ID}
_, err := db.staticDownloads.DeleteMany(ctx, filter)
if err != nil {
return errors.AddContext(err, "failed to delete user downloads")
Expand All @@ -471,12 +469,12 @@ func (db *DB) UserDelete(ctx context.Context, u *User) error {
if err != nil {
return errors.AddContext(err, "failed to delete user API keys")
}
_, err = db.staticUnconfirmedUserUpdates.DeleteMany(ctx, bson.D{{"sub", u.Sub}})
_, err = db.staticUnconfirmedUserUpdates.DeleteMany(ctx, bson.M{"sub": u.Sub})
if err != nil {
return errors.AddContext(err, "failed to delete user unconfirmed updates")
}
// Delete the actual user.
filter = bson.D{{"_id", u.ID}}
filter = bson.M{"_id": u.ID}
dr, err := db.staticUsers.DeleteOne(ctx, filter)
if err != nil {
return errors.AddContext(err, "failed to Delete")
Expand Down Expand Up @@ -582,8 +580,7 @@ func (db *DB) Ping(ctx context.Context) error {
// managedUsersByField finds all users that have a given field value.
// The calling method is responsible for the validation of the value.
func (db *DB) managedUsersByField(ctx context.Context, fieldName, fieldValue string) ([]*User, error) {
filter := bson.M{fieldName: fieldValue}
c, err := db.staticUsers.Find(ctx, filter)
c, err := db.staticUsers.Find(ctx, bson.M{fieldName: fieldValue})
if err != nil {
return nil, errors.AddContext(err, "failed to find user")
}
Expand Down