♻️ Refactored message service

This commit is contained in:
LittleSheep 2024-05-10 21:50:11 +08:00
parent 7e4ea47c61
commit 13fcc64f79
2 changed files with 35 additions and 15 deletions

View File

@ -2,19 +2,12 @@ package models
import "gorm.io/datatypes" import "gorm.io/datatypes"
type MessageType = uint8
const (
MessageTypeText = MessageType(iota)
MessageTypeAudio
)
type Message struct { type Message struct {
BaseModel BaseModel
Content string `json:"content"` Content []byte `json:"content"`
Metadata datatypes.JSONMap `json:"metadata"` Metadata datatypes.JSONMap `json:"metadata"`
Type MessageType `json:"type"` Type string `json:"type"`
Attachments []Attachment `json:"attachments"` Attachments []Attachment `json:"attachments"`
Channel Channel `json:"channel"` Channel Channel `json:"channel"`
Sender ChannelMember `json:"sender"` Sender ChannelMember `json:"sender"`

View File

@ -1,6 +1,8 @@
package server package server
import ( import (
"encoding/base64"
"encoding/json"
"fmt" "fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/database" "git.solsynth.dev/hydrogen/messaging/pkg/database"
"git.solsynth.dev/hydrogen/messaging/pkg/models" "git.solsynth.dev/hydrogen/messaging/pkg/models"
@ -44,7 +46,9 @@ func newMessage(c *fiber.Ctx) error {
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
Content string `json:"content"` Type string `json:"type" validate:"required"`
Content map[string]any `json:"content"`
Metadata map[string]any `json:"metadata"`
Attachments []models.Attachment `json:"attachments"` Attachments []models.Attachment `json:"attachments"`
ReplyTo *uint `json:"reply_to"` ReplyTo *uint `json:"reply_to"`
} }
@ -70,15 +74,24 @@ func newMessage(c *fiber.Ctx) error {
} }
} }
var encodedContent []byte
if raw, err := json.Marshal(data.Content); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err))
} else {
encoder := base64.StdEncoding
encodedContent = make([]byte, encoder.EncodedLen(len(raw)))
encoder.Encode(encodedContent, raw)
}
message := models.Message{ message := models.Message{
Content: data.Content, Content: encodedContent,
Metadata: nil,
Sender: member, Sender: member,
Channel: channel, Channel: channel,
ChannelID: channel.ID, ChannelID: channel.ID,
SenderID: member.ID, SenderID: member.ID,
Metadata: data.Metadata,
Attachments: data.Attachments, Attachments: data.Attachments,
Type: models.MessageTypeText, Type: data.Type,
} }
var replyTo models.Message var replyTo models.Message
@ -104,8 +117,11 @@ func editMessage(c *fiber.Ctx) error {
messageId, _ := c.ParamsInt("messageId", 0) messageId, _ := c.ParamsInt("messageId", 0)
var data struct { var data struct {
Content string `json:"content" validate:"required"` Type string `json:"type" validate:"required"`
Content map[string]any `json:"content"`
Metadata map[string]any `json:"metadata"`
Attachments []models.Attachment `json:"attachments"` Attachments []models.Attachment `json:"attachments"`
ReplyTo *uint `json:"reply_to"`
} }
if err := BindAndValidate(c, &data); err != nil { if err := BindAndValidate(c, &data); err != nil {
@ -132,8 +148,19 @@ func editMessage(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
message.Content = data.Content var encodedContent []byte
if raw, err := json.Marshal(data.Content); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err))
} else {
encoder := base64.StdEncoding
encodedContent = make([]byte, encoder.EncodedLen(len(raw)))
encoder.Encode(encodedContent, raw)
}
message.Attachments = data.Attachments message.Attachments = data.Attachments
message.Metadata = data.Metadata
message.Content = encodedContent
message.Type = data.Type
message, err = services.EditMessage(message) message, err = services.EditMessage(message)
if err != nil { if err != nil {