Messaging/pkg/internal/http/api/whats_new_api.go

70 lines
1.8 KiB
Go
Raw Normal View History

2024-09-03 12:30:47 +00:00
package api
import (
"fmt"
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-09-11 15:58:02 +00:00
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models"
2024-09-03 12:30:47 +00:00
"github.com/gofiber/fiber/v2"
)
func getWhatsNew(c *fiber.Ctx) error {
2024-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-09-03 12:30:47 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.Account)
2024-09-03 12:30:47 +00:00
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 ignoreRange []uint
2024-09-03 12:30:47 +00:00
var channelMembers []models.ChannelMember
if err := database.C.Where("account_id = ?", user.ID).Select("id", "channel_id").Find(&channelMembers).Error; err != nil {
2024-09-03 12:30:47 +00:00
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)
ignoreRange = append(ignoreRange, member.ID)
2024-09-03 12:30:47 +00:00
}
}
tx = tx.Where("channel_id IN ?", lookupRange)
tx = tx.Where("sender_id NOT IN ?", ignoreRange)
2024-09-03 12:30:47 +00:00
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 := tx.
2024-09-03 12:30:47 +00:00
Limit(take).
Order("created_at DESC").
Preload("Sender").
Preload("Channel").
2024-09-03 14:54:11 +00:00
Preload("Channel.Realm").
2024-09-03 12:30:47 +00:00
Find(&items).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}