2024-06-27 14:38:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-11 11:00:49 +00:00
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-06-27 20:34:49 +00:00
|
|
|
func getEvent(c *fiber.Ctx) error {
|
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
id, _ := c.ParamsInt("eventId")
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
event, err := services.GetEvent(channel, uint(id))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(event)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
func listEvent(c *fiber.Ctx) error {
|
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
count := services.CountEvent(channel)
|
2024-06-27 20:34:49 +00:00
|
|
|
events, err := services.ListEvent(channel, take, offset)
|
2024-06-27 14:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
2024-06-27 20:34:49 +00:00
|
|
|
"data": events,
|
2024-06-27 14:38:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRawEvent(c *fiber.Ctx) error {
|
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreateMessagingRawEvent", true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Uuid string `json:"uuid" validate:"required"`
|
|
|
|
Type string `json:"type" validate:"required"`
|
|
|
|
Body map[string]any `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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
if val, ok := c.Locals("realm").(models.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())
|
|
|
|
} else if member.PowerLevel < 0 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to send message")
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:49 +00:00
|
|
|
event := models.Event{
|
2024-06-27 14:38:18 +00:00
|
|
|
Uuid: data.Uuid,
|
|
|
|
Body: data.Body,
|
|
|
|
Type: data.Type,
|
|
|
|
Sender: member,
|
|
|
|
Channel: channel,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: member.ID,
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:49 +00:00
|
|
|
if event, err = services.NewEvent(event); err != nil {
|
2024-06-27 14:38:18 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:49 +00:00
|
|
|
return c.JSON(event)
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|