2024-03-30 10:21:17 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-03-31 13:19:51 +00:00
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
2024-03-30 10:21:17 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-03-30 10:40:21 +00:00
|
|
|
func listMessage(c *fiber.Ctx) error {
|
2024-05-04 16:39:59 +00:00
|
|
|
user := c.Locals("principal").(models.Account)
|
2024-03-30 10:21:17 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
2024-05-05 14:12:40 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
channel, err = services.GetChannelWithAlias(alias, val.ID)
|
|
|
|
} else {
|
|
|
|
channel, err = services.GetChannelWithAlias(alias)
|
|
|
|
}
|
2024-03-30 10:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-05-04 16:39:59 +00:00
|
|
|
} else if _, _, err := services.GetAvailableChannel(channel.ID, user); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, fmt.Sprintf("you need join the channel before you read the messages: %v", err))
|
2024-03-30 10:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count := services.CountMessage(channel)
|
|
|
|
messages, err := services.ListMessage(channel, take, offset)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": messages,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:39:59 +00:00
|
|
|
func newMessage(c *fiber.Ctx) error {
|
2024-03-30 10:21:17 +00:00
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var data struct {
|
2024-04-06 15:44:09 +00:00
|
|
|
Content string `json:"content"`
|
2024-03-30 10:21:17 +00:00
|
|
|
Attachments []models.Attachment `json:"attachments"`
|
2024-03-31 13:19:51 +00:00
|
|
|
ReplyTo *uint `json:"reply_to"`
|
2024-03-30 10:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
2024-04-06 15:44:09 +00:00
|
|
|
} else if len(data.Attachments) == 0 && len(data.Content) == 0 {
|
|
|
|
return fmt.Errorf("you must write or upload some content in a single message")
|
2024-03-30 10:21:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-03-31 13:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
message := models.Message{
|
|
|
|
Content: data.Content,
|
|
|
|
Metadata: nil,
|
|
|
|
Sender: member,
|
|
|
|
Channel: channel,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: member.ID,
|
|
|
|
Attachments: data.Attachments,
|
|
|
|
Type: models.MessageTypeText,
|
|
|
|
}
|
|
|
|
|
|
|
|
var replyTo models.Message
|
|
|
|
if data.ReplyTo != nil {
|
|
|
|
if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("message to reply was not found: %v", err))
|
|
|
|
} else {
|
|
|
|
message.ReplyTo = &replyTo
|
|
|
|
message.ReplyID = &replyTo.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if message, err = services.NewMessage(message); err != nil {
|
2024-03-30 10:21:17 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(message)
|
|
|
|
}
|
2024-03-30 10:40:21 +00:00
|
|
|
|
|
|
|
func editMessage(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
messageId, _ := c.ParamsInt("messageId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Content string `json:"content" validate:"required"`
|
|
|
|
Attachments []models.Attachment `json:"attachments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:40:21 +00:00
|
|
|
var message models.Message
|
2024-05-05 14:56:37 +00:00
|
|
|
if message, err = services.GetMessageWithPrincipal(channel, member, uint(messageId)); err != nil {
|
2024-03-30 10:40:21 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
message.Content = data.Content
|
|
|
|
message.Attachments = data.Attachments
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
message, err = services.EditMessage(message)
|
2024-03-30 10:40:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMessage(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
messageId, _ := c.ParamsInt("messageId", 0)
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:40:21 +00:00
|
|
|
var message models.Message
|
2024-05-05 14:56:37 +00:00
|
|
|
if message, err = services.GetMessageWithPrincipal(channel, member, uint(messageId)); err != nil {
|
2024-03-30 10:40:21 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
message, err = services.DeleteMessage(message)
|
2024-03-30 10:40:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(message)
|
|
|
|
}
|