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

refactor: upgrade mgo to mongo driver #243

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions cmd/blacklist-user/blacklist_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
_ "github.com/mdg-iitr/Codephile/conf"
"github.com/mdg-iitr/Codephile/services/auth"
"os"
Expand All @@ -13,7 +13,9 @@ func main() {
fmt.Println("Usage: go run ./blacklist_user <uid>")
os.Exit(1)
}
err := auth.BlacklistUser(bson.ObjectId(os.Args[1]))
// TODO: upgrade driver
id, _ := primitive.ObjectIDFromHex(os.Args[1])
err := auth.BlacklistUser(id)
if err != nil {
fmt.Println(err.Error())
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/delete-user/delete_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"fmt"
"strings"

"github.com/globalsign/mgo/bson"
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
_ "github.com/mdg-iitr/Codephile/conf"

"os"
Expand All @@ -21,7 +23,8 @@ func main() {
fmt.Println("Usage: go run ./delete_user <uid>")
os.Exit(1)
}
user, err := models.GetUser(bson.ObjectIdHex(os.Args[1]))
id, _ := primitive.ObjectIDFromHex(os.Args[1])
user, err := models.GetUser(id)
if err != nil {
panic(err)
}
Expand All @@ -37,7 +40,7 @@ func main() {
sess := db.NewUserCollectionSession()
defer sess.Close()
coll := sess.Collection
err = coll.RemoveId(user.ID)
_, err = coll.DeleteOne(context.TODO(), bson.M{"_id": user.ID})
if err != nil {
panic(err)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/update-users/update_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"context"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
"github.com/mdg-iitr/Codephile/conf"
"github.com/mdg-iitr/Codephile/models"
"github.com/mdg-iitr/Codephile/models/db"
Expand All @@ -17,9 +18,10 @@ func main() {
sess := db.NewUserCollectionSession()
defer sess.Close()
coll := sess.Collection
iter := coll.Find(nil).Select(bson.M{"_id": 1, "verified": 1}).Iter()
cursor, _ := coll.Find(context.TODO(), bson.M{}, options.Find().SetProjection(bson.M{"_id": 1, "verified": 1}))
var user types.User
for iter.Next(&user) {
for cursor.Next(context.TODO()) {
cursor.Decode(&user)
if !user.Verified {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/whitelist-user/whitelist_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
_ "github.com/mdg-iitr/Codephile/conf"
"github.com/mdg-iitr/Codephile/services/auth"
"os"
Expand All @@ -13,9 +13,9 @@ func main() {
fmt.Println("Usage: go run ./blacklist_user <uid>")
os.Exit(1)
}
err := auth.WhitelistUser(bson.ObjectId(os.Args[1]))
id, _ := primitive.ObjectIDFromHex(os.Args[1])
err := auth.WhitelistUser(id)
if err != nil {
fmt.Println(err.Error())
}

}
Binary file removed codephile-backend
Binary file not shown.
2 changes: 1 addition & 1 deletion conf/app.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
appname = .
httpport = ${PORT}
runmode = ${ENVIRONMENT}
runmode = dev
autorender = false
copyrequestbody = true
EnableDocs = true
Expand Down
6 changes: 3 additions & 3 deletions controllers/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controllers
import (
"github.com/astaxie/beego"
"github.com/getsentry/sentry-go"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
"log"
Expand Down Expand Up @@ -43,7 +43,7 @@ func (f *FeedController) ContestsFeed() {
// @Failure 500 server_error
// @router /friend-activity/all [get]
func (f *FeedController) AllFeed() {
uid := f.Ctx.Input.GetData("uid").(bson.ObjectId)
uid := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
feed, err := models.GetAllFeed(uid)
if err != nil {
hub := sentry.GetHubFromContext(f.Ctx.Request.Context())
Expand All @@ -68,7 +68,7 @@ func (f *FeedController) AllFeed() {
// @Failure 500 server_error
// @router /friend-activity [get]
func (f *FeedController) PaginatedFeed() {
uid := f.Ctx.Input.GetData("uid").(bson.ObjectId)
uid := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
before, err := f.GetInt64("before", time.Now().UTC().Unix())
if err != nil {
f.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
Expand Down
29 changes: 16 additions & 13 deletions controllers/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/astaxie/beego"
"github.com/getsentry/sentry-go"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
)
Expand All @@ -25,15 +25,16 @@ type FriendsController struct {
// @Failure 500 server_error
// @router /follow [post]
func (f *FriendsController) FollowUser() {
uid1 := f.Ctx.Input.GetData("uid").(bson.ObjectId)
uid1 := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
uid2 := f.GetString("uid2")
if uid2 == "" || !bson.IsObjectIdHex(uid2) {
if uid2 == "" || !primitive.IsValidObjectID(uid2) {
f.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
f.Data["json"] = errors.BadInputError("Invalid UID")
f.ServeJSON()
return
}
err := models.FollowUser(uid1, bson.ObjectIdHex(uid2))
id2, _ := primitive.ObjectIDFromHex(uid2)
err := models.FollowUser(uid1, id2)
if err != nil {
hub := sentry.GetHubFromContext(f.Ctx.Request.Context())
hub.CaptureException(err)
Expand All @@ -57,16 +58,17 @@ func (f *FriendsController) FollowUser() {
// @Failure 500 server_error
// @router /unfollow [post]
func (f *FriendsController) UnFollowUser() {
userUID := f.Ctx.Input.GetData("uid").(bson.ObjectId)
userUID := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
uid2 := f.GetString("uid2")
if uid2 == "" || !bson.IsObjectIdHex(uid2) {
if uid2 == "" || !primitive.IsValidObjectID(uid2) {
f.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
f.Data["json"] = errors.BadInputError("Invalid UID")
f.ServeJSON()
return
}
err := models.UnFollowUser(userUID, bson.ObjectIdHex(uid2))
if err == mgo.ErrNotFound {
id2, _ := primitive.ObjectIDFromHex(uid2)
err := models.UnFollowUser(userUID, id2)
if err == mongo.ErrNoDocuments {
f.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
f.Data["json"] = errors.NotFoundError("user not found")
f.ServeJSON()
Expand All @@ -93,15 +95,16 @@ func (f *FriendsController) UnFollowUser() {
// @Failure 500 server_error
// @router /compare [get]
func (f *FriendsController) CompareUser() {
uid1 := f.Ctx.Input.GetData("uid").(bson.ObjectId)
uid1 := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
uid2 := f.GetString("uid2")
if uid2 == "" || !bson.IsObjectIdHex(uid2) {
if uid2 == "" || !primitive.IsValidObjectID(uid2) {
f.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
f.Data["json"] = errors.BadInputError("Invalid UID")
f.ServeJSON()
return
}
worldRanks, err := models.CompareUser(uid1, bson.ObjectIdHex(uid2))
id2, _ := primitive.ObjectIDFromHex(uid2)
worldRanks, err := models.CompareUser(uid1, id2)
if err != nil {
hub := sentry.GetHubFromContext(f.Ctx.Request.Context())
hub.CaptureException(err)
Expand All @@ -122,7 +125,7 @@ func (f *FriendsController) CompareUser() {
// @Failure 500 server_error
// @router /following [get]
func (f *FriendsController) GetFollowing() {
uid := f.Ctx.Input.GetData("uid").(bson.ObjectId)
uid := f.Ctx.Input.GetData("uid").(primitive.ObjectID)
following, err := models.GetFollowingUsers(uid)
if err != nil {
hub := sentry.GetHubFromContext(f.Ctx.Request.Context())
Expand Down
18 changes: 9 additions & 9 deletions controllers/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/astaxie/beego"
"github.com/getsentry/sentry-go"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
. "github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
)
Expand All @@ -27,11 +27,11 @@ type GraphController struct {
// @router /activity/:uid [get]
func (g *GraphController) GetActivityGraph() {
uidString := g.GetString(":uid")
var uid bson.ObjectId
if bson.IsObjectIdHex(uidString) {
uid = bson.ObjectIdHex(uidString)
var uid primitive.ObjectID
if primitive.IsValidObjectID(uidString) {
uid, _ = primitive.ObjectIDFromHex(uidString)
} else if uidString == "" {
uid = g.Ctx.Input.GetData("uid").(bson.ObjectId)
uid = g.Ctx.Input.GetData("uid").(primitive.ObjectID)
} else {
g.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
g.Data["json"] = BadInputError("Invalid UID")
Expand Down Expand Up @@ -64,11 +64,11 @@ func (g *GraphController) GetActivityGraph() {
// @router /status/:uid [get]
func (g *GraphController) GetStatusCounts() {
uidString := g.GetString(":uid")
var uid bson.ObjectId
if bson.IsObjectIdHex(uidString) {
uid = bson.ObjectIdHex(uidString)
var uid primitive.ObjectID
if primitive.IsValidObjectID(uidString) {
uid, _ = primitive.ObjectIDFromHex(uidString)
} else if uidString == "" {
uid = g.Ctx.Input.GetData("uid").(bson.ObjectId)
uid = g.Ctx.Input.GetData("uid").(primitive.ObjectID)
} else {
g.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
g.Data["json"] = BadInputError("Invalid UID")
Expand Down
34 changes: 17 additions & 17 deletions controllers/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package controllers
import (
"github.com/astaxie/beego"
"github.com/getsentry/sentry-go"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/bson/primitive"
. "github.com/mdg-iitr/Codephile/conf"
. "github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
Expand All @@ -29,19 +29,19 @@ type SubmissionController struct {
// @router /all/:uid [get]
func (s *SubmissionController) GetAllSubmissions() {
uidString := s.GetString(":uid")
var uid bson.ObjectId
if bson.IsObjectIdHex(uidString) {
uid = bson.ObjectIdHex(uidString)
var uid primitive.ObjectID
if primitive.IsValidObjectID(uidString) {
uid, _ = primitive.ObjectIDFromHex(uidString)
} else if uidString == "" {
uid = s.Ctx.Input.GetData("uid").(bson.ObjectId)
uid = s.Ctx.Input.GetData("uid").(primitive.ObjectID)
} else {
s.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
s.Data["json"] = BadInputError("Invalid UID")
s.ServeJSON()
return
}
subs, err := models.GetAllSubmissions(uid)
if err == mgo.ErrNotFound {
if err == mongo.ErrNoDocuments {
s.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
s.Data["json"] = NotFoundError("User/Submission not found")
s.ServeJSON()
Expand Down Expand Up @@ -72,11 +72,11 @@ func (s *SubmissionController) GetAllSubmissions() {
// @router /:uid [get]
func (s *SubmissionController) PaginatedSubmissions() {
uidString := s.GetString(":uid")
var uid bson.ObjectId
if bson.IsObjectIdHex(uidString) {
uid = bson.ObjectIdHex(uidString)
var uid primitive.ObjectID
if primitive.IsValidObjectID(uidString) {
uid, _ = primitive.ObjectIDFromHex(uidString)
} else if uidString == "" {
uid = s.Ctx.Input.GetData("uid").(bson.ObjectId)
uid = s.Ctx.Input.GetData("uid").(primitive.ObjectID)
} else {
s.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
s.Data["json"] = BadInputError("Invalid UID")
Expand All @@ -94,7 +94,7 @@ func (s *SubmissionController) PaginatedSubmissions() {
before = time.Now().UTC().Unix()
}
feed, err := models.GetSubmissions(uid, time.Unix(before, 0))
if err == mgo.ErrNotFound {
if err == mongo.ErrNoDocuments {
s.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
s.Data["json"] = NotFoundError("User not found")
s.ServeJSON()
Expand Down Expand Up @@ -122,7 +122,7 @@ func (s *SubmissionController) PaginatedSubmissions() {
// @Failure 503 Could not save submission, try later
// @router /:site [post]
func (s *SubmissionController) SaveSubmission() {
uid := s.Ctx.Input.GetData("uid").(bson.ObjectId)
uid := s.Ctx.Input.GetData("uid").(primitive.ObjectID)
site := s.GetString(":site")
if !IsSiteValid(site) {
s.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -158,11 +158,11 @@ func (s *SubmissionController) SaveSubmission() {
// @router /:site/:uid/filter [get]
func (s *SubmissionController) FilterSubmission() {
uidString := s.GetString(":uid")
var uid bson.ObjectId
if bson.IsObjectIdHex(uidString) {
uid = bson.ObjectIdHex(uidString)
var uid primitive.ObjectID
if primitive.IsValidObjectID(uidString) {
uid, _ = primitive.ObjectIDFromHex(uidString)
} else if uidString == "" {
uid = s.Ctx.Input.GetData("uid").(bson.ObjectId)
uid = s.Ctx.Input.GetData("uid").(primitive.ObjectID)
} else {
s.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
s.Data["json"] = BadInputError("Invalid UID")
Expand Down
Loading