Record more things and bug fixes

This commit is contained in:
LittleSheep 2024-06-28 04:34:49 +08:00
parent ba974b13be
commit 590adc8fc3
4 changed files with 56 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/spf13/viper"
)
@ -71,6 +72,16 @@ func startCall(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
_, _ = services.NewEvent(models.Event{
Uuid: uuid.NewString(),
Body: map[string]any{},
Type: "calls.start",
Channel: channel,
Sender: membership,
ChannelID: channel.ID,
SenderID: membership.ID,
})
return c.JSON(call)
}
}
@ -107,6 +118,16 @@ func endCall(c *fiber.Ctx) error {
if call, err := services.EndCall(call); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
_, _ = services.NewEvent(models.Event{
Uuid: uuid.NewString(),
Body: map[string]any{"last": call.EndedAt.Unix() - call.CreatedAt.Unix()},
Type: "calls.end",
Channel: channel,
Sender: membership,
ChannelID: channel.ID,
SenderID: membership.ID,
})
return c.JSON(call)
}
}

View File

@ -9,6 +9,35 @@ import (
"github.com/gofiber/fiber/v2"
)
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)
}
func listEvent(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
@ -32,14 +61,14 @@ func listEvent(c *fiber.Ctx) error {
}
count := services.CountEvent(channel)
messages, err := services.ListEvent(channel, take, offset)
events, err := services.ListEvent(channel, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": messages,
"data": events,
})
}
@ -79,7 +108,7 @@ func newRawEvent(c *fiber.Ctx) error {
}
}
message := models.Event{
event := models.Event{
Uuid: data.Uuid,
Body: data.Body,
Type: data.Type,
@ -89,9 +118,9 @@ func newRawEvent(c *fiber.Ctx) error {
SenderID: member.ID,
}
if message, err = services.NewEvent(message); err != nil {
if event, err = services.NewEvent(event); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(message)
return c.JSON(event)
}

View File

@ -34,6 +34,7 @@ func MapAPIs(app *fiber.App) {
channels.Delete("/:channel/members/me", leaveChannel)
channels.Get("/:channel/events", listEvent)
channels.Get("/:channel/events/:eventId", getEvent)
channels.Post("/:channel/events", newRawEvent)
channels.Post("/:channel/messages", newMessageEvent)

View File

@ -138,44 +138,15 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
}
func EditEvent(event models.Event) (models.Event, error) {
var members []models.ChannelMember
if err := database.C.Save(&event).Error; err != nil {
return event, err
} else if err = database.C.Where(models.ChannelMember{
ChannelID: event.ChannelID,
}).Find(&members).Error; err == nil {
event, _ = GetEvent(models.Channel{
BaseModel: models.BaseModel{ID: event.Channel.ID},
}, event.ID)
for _, member := range members {
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "events.update",
Payload: event,
})
}
}
return event, nil
}
func DeleteEvent(event models.Event) (models.Event, error) {
prev, _ := GetEvent(models.Channel{
BaseModel: models.BaseModel{ID: event.Channel.ID},
}, event.ID)
var members []models.ChannelMember
if err := database.C.Delete(&event).Error; err != nil {
return event, err
} else if err = database.C.Where(models.ChannelMember{
ChannelID: event.ChannelID,
}).Find(&members).Error; err == nil {
for _, member := range members {
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "events.burnt",
Payload: prev,
})
}
}
return event, nil
}