2024-06-27 14:38:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-08-07 10:34:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-11-02 05:24:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/services"
|
2024-06-27 14:38:18 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMessageEvent(c *fiber.Ctx) error {
|
2024-11-02 05:23:27 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-06-27 14:38:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-11-02 05:23:27 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-06-27 14:38:18 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Uuid string `json:"uuid" validate:"required"`
|
|
|
|
Type string `json:"type" validate:"required"`
|
|
|
|
Body models.EventMessageBody `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(data.Uuid) < 36 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "message uuid was not valid")
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:39:38 +00:00
|
|
|
data.Body.Text = strings.TrimSpace(data.Body.Text)
|
2024-06-27 14:54:35 +00:00
|
|
|
if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 {
|
2024-08-07 10:39:38 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "empty message was not allowed")
|
2024-06-27 14:54:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
|
2024-06-27 14:38:18 +00:00
|
|
|
} else {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID)
|
2024-08-01 04:03:34 +00:00
|
|
|
}
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-08-01 04:03:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else if member.PowerLevel < 0 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "unable to send message, access denied")
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var parsed map[string]any
|
|
|
|
raw, _ := jsoniter.Marshal(data.Body)
|
|
|
|
_ = jsoniter.Unmarshal(raw, &parsed)
|
|
|
|
|
|
|
|
event := models.Event{
|
|
|
|
Uuid: data.Uuid,
|
|
|
|
Body: parsed,
|
|
|
|
Type: data.Type,
|
|
|
|
Sender: member,
|
|
|
|
Channel: channel,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: member.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if event, err = services.NewEvent(event); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editMessageEvent(c *fiber.Ctx) error {
|
2024-11-02 05:23:27 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-06-27 14:38:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-11-02 05:23:27 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-06-27 14:38:18 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
messageId, _ := c.ParamsInt("messageId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Uuid string `json:"uuid" validate:"required"`
|
|
|
|
Type string `json:"type" validate:"required"`
|
|
|
|
Body models.EventMessageBody `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:54:35 +00:00
|
|
|
if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "you cannot send an empty message")
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
|
2024-06-27 14:38:18 +00:00
|
|
|
} else {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID)
|
2024-08-01 04:03:34 +00:00
|
|
|
}
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-08-01 04:03:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var event models.Event
|
|
|
|
if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
event, err = services.EditMessage(event, data.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMessageEvent(c *fiber.Ctx) error {
|
2024-11-02 05:23:27 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-06-27 14:38:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-11-02 05:23:27 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-06-27 14:38:18 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
messageId, _ := c.ParamsInt("messageId", 0)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
|
2024-06-27 14:38:18 +00:00
|
|
|
} else {
|
2024-10-09 15:56:24 +00:00
|
|
|
channel, member, err = services.GetChannelIdentity(alias, user.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var event models.Event
|
|
|
|
if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
event, err = services.DeleteMessage(event)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(event)
|
|
|
|
}
|