From 75f5eb14565f2a4eb50480d33091ffbb25612fc4 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 3 Sep 2024 20:30:47 +0800 Subject: [PATCH] :sparkles: Whats new API --- pkg/internal/server/api/index.go | 2 + pkg/internal/server/api/whats_new_api.go | 64 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 pkg/internal/server/api/whats_new_api.go diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 7b69c03..e0131ee 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -46,5 +46,7 @@ func MapAPIs(app *fiber.App, baseURL string) { channels.Delete("/:channel/calls/ongoing/participant", kickParticipantInCall) channels.Post("/:channel/calls/ongoing/token", exchangeCallToken) } + + api.Get("/whats-new", getWhatsNew) } } diff --git a/pkg/internal/server/api/whats_new_api.go b/pkg/internal/server/api/whats_new_api.go new file mode 100644 index 0000000..5a77b52 --- /dev/null +++ b/pkg/internal/server/api/whats_new_api.go @@ -0,0 +1,64 @@ +package api + +import ( + "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" + "github.com/gofiber/fiber/v2" +) + +func getWhatsNew(c *fiber.Ctx) error { + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + + pivot := c.QueryInt("pivot", 0) + if pivot < 0 { + return fiber.NewError(fiber.StatusBadRequest, "pivot must be greater than zero") + } + + take := c.QueryInt("take", 10) + if take > 100 { + take = 100 + } + + tx := database.C + + var lookupRange []uint + var channelMembers []models.ChannelMember + if err := database.C.Where("account_id = ?", user.ID).Select("channel_id").Find(&channelMembers).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to get channel identity of you: %v", err)) + } else { + for _, member := range channelMembers { + lookupRange = append(lookupRange, member.ChannelID) + } + } + + tx = tx.Where("channel_id IN ?", lookupRange) + tx = tx.Where("id > ?", pivot) + + countTx := tx + var count int64 + if err := countTx.Model(&models.Event{}).Count(&count).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + var items []models.Event + if err := database.C. + Where("channel_id IN ?", lookupRange). + Limit(take). + Order("created_at DESC"). + Preload("Sender"). + Preload("Sender.Account"). + Find(&items).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + return c.JSON(fiber.Map{ + "count": count, + "data": items, + }) + +}