new error codes, code refactor, added getMessages function
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"os"
|
||||
"relay-server/config"
|
||||
@@ -158,12 +159,13 @@ func Login(c *fiber.Ctx) error {
|
||||
func ValidateToken(c *fiber.Ctx) error {
|
||||
username, ok := c.Locals("username").(string)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid token: missing username", nil)
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid token: missing username", fmt.Errorf("missing username: %v", c.Locals("username")))
|
||||
}
|
||||
|
||||
userID, ok := c.Locals("userID").(string)
|
||||
userIDVal := c.Locals("userID")
|
||||
userID, ok := userIDVal.(uuid.UUID)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid token: missing user ID", nil)
|
||||
return helpers.NewError(helpers.ErrUnauthorized, "unauthorized", fmt.Errorf("missing/invalid userID type: %T, value: %v\n", userIDVal, userIDVal))
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
|
||||
@@ -35,19 +35,10 @@ func DeleteContact(c *fiber.Ctx) error {
|
||||
|
||||
func InsertContact(c *fiber.Ctx) error {
|
||||
type params struct {
|
||||
ContactUsername string `params:"contact_username"`
|
||||
ContactUsername string `params:"contactUsername"`
|
||||
}
|
||||
|
||||
userIDVal := c.Locals("userID")
|
||||
userIDStr, ok := userIDVal.(string)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInternal, "Internal server error", nil)
|
||||
}
|
||||
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid user ID format", err)
|
||||
}
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
p := new(params)
|
||||
if err := c.ParamsParser(p); err != nil {
|
||||
@@ -76,16 +67,7 @@ func InsertContact(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func GetContacts(c *fiber.Ctx) error {
|
||||
userIDVal := c.Locals("userID")
|
||||
userIDStr, ok := userIDVal.(string)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInternal, "Invalid user session", nil)
|
||||
}
|
||||
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid user ID format", err)
|
||||
}
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
contacts, err := database.GetContacts(database.DB, userID)
|
||||
if err != nil {
|
||||
|
||||
41
handlers/messages.go
Normal file
41
handlers/messages.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"relay-server/database"
|
||||
"relay-server/helpers"
|
||||
)
|
||||
|
||||
func GetMessages(c *fiber.Ctx) error {
|
||||
type params struct {
|
||||
conversationID uuid.UUID `params:"conversationID"`
|
||||
}
|
||||
type query struct {
|
||||
limit int `query:"limit"`
|
||||
cursor int `query:"cursor"`
|
||||
}
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
p := new(params)
|
||||
q := new(query)
|
||||
if err := c.ParamsParser(p); err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid params", err)
|
||||
}
|
||||
if err := c.QueryParser(q); err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid query", err)
|
||||
}
|
||||
if p.conversationID == uuid.Nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "conversation ID is empty", nil)
|
||||
}
|
||||
|
||||
messages, err := database.GetMessages(database.DB, userID, p.conversationID, q.limit, q.cursor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
return c.JSON(fiber.Map{"message": "No messages found"})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{"messages": messages})
|
||||
}
|
||||
Reference in New Issue
Block a user