diff --git a/pkg/internal/server/api/calls_api.go b/pkg/internal/server/api/calls_api.go index 824bbd3..8b50565 100644 --- a/pkg/internal/server/api/calls_api.go +++ b/pkg/internal/server/api/calls_api.go @@ -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) } } diff --git a/pkg/internal/server/api/events_api.go b/pkg/internal/server/api/events_api.go index e2fd694..7452de9 100644 --- a/pkg/internal/server/api/events_api.go +++ b/pkg/internal/server/api/events_api.go @@ -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) } diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index ef3fd14..96a3adc 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -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) diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index 5ddee0e..eed3692 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -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 }