Skip to content

Commit

Permalink
✨ Add report feature
Browse files Browse the repository at this point in the history
  • Loading branch information
펜타곤 authored and 펜타곤 committed Apr 1, 2024
1 parent aec7acc commit 29d4eb7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 46 deletions.
20 changes: 10 additions & 10 deletions controllers/authctrl/login.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package authctrl

import (
"log"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -22,7 +22,7 @@ func LoginCtrl(c *fiber.Ctx) error {
}
profile, err := gauth.GetProfile(code)
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
} else if !profile.EmailVerified || !strings.HasSuffix(profile.Email, "@dimigo.hs.kr") {
return c.Status(401).SendString("Invalid Email Address")
Expand All @@ -38,14 +38,14 @@ func LoginCtrl(c *fiber.Ctx) error {
)
err = mgm.Coll(newUser).Create(newUser)
if err != nil {
log.Println("Error while creating user")
log.Println(err)
log.Error("Error while creating user")
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}
foundUser = newUser
} else {
log.Println("Error while finding user")
log.Println(err)
log.Error("Error while finding user")
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}
}
Expand All @@ -58,14 +58,14 @@ func LoginCtrl(c *fiber.Ctx) error {
foundUser.RefreshTokens = append(foundUser.RefreshTokens, random.RandString(32))
err = mgm.Coll(foundUser).Update(foundUser)
if err != nil {
log.Println("Error while updating user")
log.Println(err)
log.Error("Error while updating user")
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}
accessToken, err := crypt.CreateJWT(crypt.JWTClaims{UserID: foundUser.ID.Hex(), Type: "auth"})
if err != nil {
log.Println("Error while creating JWT")
log.Println(err)
log.Error("Error while creating JWT")
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}

Expand Down
12 changes: 2 additions & 10 deletions controllers/imagectrl/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ package imagectrl

import (
"github.com/gofiber/fiber/v2"
"pentag.kr/dimimonster/utils/validator"
)

func DownloadImageCtrl(c *fiber.Ctx) error {
imageID := c.Params("id")
if !isHex(imageID) {
if !validator.IsHex(imageID) {
return c.Status(fiber.StatusBadRequest).SendString("Bad Request")
}
c.Set("Content-Type", "image/jpeg")
return c.SendFile("./data/original/" + imageID + ".jpg")
}

func isHex(s string) bool {
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
return true
}
7 changes: 4 additions & 3 deletions controllers/imagectrl/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"pentag.kr/dimimonster/middleware"
"pentag.kr/dimimonster/models"
"pentag.kr/dimimonster/utils/validator"
)

func GetLikeCountCtrl(c *fiber.Ctx) error {
imageID := c.Params("id")
if !isHex(imageID) {
if !validator.IsHex(imageID) {
return c.Status(400).SendString("Bad Request")
}
image := models.Image{}
Expand All @@ -28,7 +29,7 @@ func GetLikeCountCtrl(c *fiber.Ctx) error {

func AddLikeCtrl(c *fiber.Ctx) error {
imageID := c.Params("id")
if !isHex(imageID) {
if !validator.IsHex(imageID) {
return c.Status(400).SendString("Bad Request")
}
userID := middleware.GetUserIDFromMiddleware(c)
Expand All @@ -54,7 +55,7 @@ func AddLikeCtrl(c *fiber.Ctx) error {

func DeleteLikeCtrl(c *fiber.Ctx) error {
imageID := c.Params("id")
if !isHex(imageID) {
if !validator.IsHex(imageID) {
return c.Status(400).SendString("Bad Request")
}
userID := middleware.GetUserIDFromMiddleware(c)
Expand Down
5 changes: 2 additions & 3 deletions controllers/imagectrl/recent.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package imagectrl

import (
"log"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
Expand All @@ -15,7 +14,7 @@ func GetRecentImageCtrl(c *fiber.Ctx) error {
images := []models.Image{}
err := mgm.Coll(&models.Image{}).SimpleFind(&images, &bson.M{}, options.Find().SetSort(bson.M{"created_at": -1}).SetLimit(1))
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}

Expand Down
15 changes: 8 additions & 7 deletions controllers/imagectrl/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package imagectrl
import (
"image/jpeg"
"io"
"log"

"os"
"unicode/utf8"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/kamva/mgm/v3"
"github.com/nfnt/resize"
"pentag.kr/dimimonster/config"
Expand Down Expand Up @@ -87,7 +88,7 @@ func UploadImage(c *fiber.Ctx) error {
newImage := models.NewImage(userID, description, location)
err = mgm.Coll(newImage).Create(newImage)
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
Expand All @@ -96,7 +97,7 @@ func UploadImage(c *fiber.Ctx) error {

thumbnailFile, err := os.Create("./data/thumbnail/" + fileID + ".jpg")
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
Expand All @@ -105,14 +106,14 @@ func UploadImage(c *fiber.Ctx) error {

err = jpeg.Encode(thumbnailFile, image, &jpeg.Options{Quality: 70})
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
}
originalFile, err := os.Create("./data/original/" + fileID + ".jpg")
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
Expand All @@ -121,14 +122,14 @@ func UploadImage(c *fiber.Ctx) error {
file.Seek(0, 0)
fileBytes, err := io.ReadAll(file)
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
}
_, err = originalFile.Write(fileBytes)
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
})
Expand Down
8 changes: 4 additions & 4 deletions controllers/imagectrl/weeklybest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package imagectrl
import (
"context"
"encoding/base64"
"log"
"os"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -54,13 +54,13 @@ func GetWeeklyBestCtrl(c *fiber.Ctx) error {

cursor, err := mgm.Coll(&models.Image{}).Aggregate(context.Background(), pipeline)
if err != nil {
log.Println(err)
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}

var images []weeklyAggregate
if err = cursor.All(context.Background(), &images); err != nil {
log.Println(err)
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}
var result []ImageResponse
Expand All @@ -69,7 +69,7 @@ func GetWeeklyBestCtrl(c *fiber.Ctx) error {
var base64String string
thumbnailfile, err := os.ReadFile("./data/thumbnail/" + image.HexID + ".jpg")
if err != nil {
log.Println(err)
log.Error(err)
base64String = ""
} else {
base64String = "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(thumbnailfile)
Expand Down
42 changes: 39 additions & 3 deletions controllers/reportctrl/send.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package reportctrl

import "github.com/gofiber/fiber/v2"
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/kamva/mgm/v3"
"pentag.kr/dimimonster/middleware"
"pentag.kr/dimimonster/models"
"pentag.kr/dimimonster/utils/validator"
)

type ReportRequest struct {
TargetID string `json:"target-id" validate:"required"`
Category int `json:"category" validate:"required"`
Reason string `json:"reason" validate:"required"`
Reason string `json:"reason" validate:"required, max=300"`
Token string `json:"token" validate:"required"`
}

func SendReportCtrl(c *fiber.Ctx) error {
body := ReportRequest{}
if errArr := validator.ParseAndValidate(c, body); errArr != nil {
return c.Status(400).JSON(fiber.Map{
"error": errArr,
})
}
userID := middleware.GetUserIDFromMiddleware(c)

if !validator.IsHex(body.TargetID) {
return c.Status(400).SendString("Bad Request")
}
// if !crypt.RecaptchaCheck(body.Token, "image_report") {
// return c.Status(425).SendString("Recaptcha Failed")
// }

newReport, err := models.NewReport(
body.TargetID,
userID,
body.Reason,
)
if err != nil {
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}

err = mgm.Coll(newReport).Create(newReport)
if err != nil {
log.Error(err)
return c.Status(500).SendString("Internal Server Error")
}

return c.SendString("Send Report")
}
6 changes: 3 additions & 3 deletions models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ type Report struct {
TargetImageID string `json:"target_image_id" bson:"target_image_id"`
TargetOwnerID string `json:"target_owner_id" bson:"target_owner_id"`
ReporterID string `json:"reporter_id" bson:"reporter_id"`
Category int `json:"category" bson:"category"`
Reason string `json:"reason" bson:"reason"`
Status string `json:"status" bson:"status"`
Secret string `json:"secret" bson:"secret"`
}

func NewReport(targetImageID string, reporterID string, category int, reason string) (*Report, error) {
func NewReport(targetImageID string, reporterID string, reason string) (*Report, error) {
foundImage := &Image{}
err := mgm.Coll(foundImage).FindByID(targetImageID, foundImage)
if err != nil {
Expand All @@ -26,8 +26,8 @@ func NewReport(targetImageID string, reporterID string, category int, reason str
TargetImageID: targetImageID,
TargetOwnerID: foundImage.OwnerID,
ReporterID: reporterID,
Category: category,
Reason: reason,
Status: "pending",
Secret: random.RandString(32),
}, nil
}
18 changes: 18 additions & 0 deletions routers/report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package routers

import (
"github.com/gofiber/fiber/v2"
"pentag.kr/dimimonster/controllers/reportctrl"
"pentag.kr/dimimonster/middleware"
)

func initReportRouter(router fiber.Router) {
reportRouter := router.Group("/report")
reportRouter.Use(middleware.AuthMiddleware)

reportRouter.Post("/send",
func(c *fiber.Ctx) error {
return reportctrl.SendReportCtrl(c)
},
)
}
1 change: 1 addition & 0 deletions routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func InitRouter(app *fiber.App) {
apiRoter := app.Group("/api")
initAuthRouter(apiRoter)
initImageRouter(apiRoter)
initReportRouter(apiRoter)

apiRoter.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
Expand Down
5 changes: 2 additions & 3 deletions utils/crypt/recaptcha.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package crypt

import (
"log"

"github.com/go-resty/resty/v2"
"github.com/gofiber/fiber/v2/log"
"pentag.kr/dimimonster/config"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func RecaptchaCheck(token string, action string) bool {
SetResult(&RecaptchaResBody{}).
Post("https://recaptchaenterprise.googleapis.com/v1/projects/dimi-monster/assessments?key=" + config.RecaptchaAPIKey)
if err != nil {
log.Println(err)
log.Error(err)
return false
}
result := resp.Result().(*RecaptchaResBody)
Expand Down
10 changes: 10 additions & 0 deletions utils/validator/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package validator

func IsHex(s string) bool {
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
return true
}

0 comments on commit 29d4eb7

Please sign in to comment.