✨ New what's new
This commit is contained in:
parent
bf973b3b71
commit
47cced4e75
@ -16,33 +16,28 @@ func getWhatsNew(c *fiber.Ctx) error {
|
||||
}
|
||||
user := c.Locals("user").(authm.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 lookupPivots []int
|
||||
var ignoreRange []uint
|
||||
var channelMembers []models.ChannelMember
|
||||
if err := database.C.Where("account_id = ?", user.ID).Select("id", "channel_id").Find(&channelMembers).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ?", user.ID).
|
||||
Select("id", "channel_id", "reading_anchor").
|
||||
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 {
|
||||
if member.ReadingAnchor == nil {
|
||||
continue
|
||||
}
|
||||
lookupRange = append(lookupRange, member.ChannelID)
|
||||
lookupPivots = append(lookupPivots, *member.ReadingAnchor)
|
||||
ignoreRange = append(ignoreRange, member.ID)
|
||||
}
|
||||
}
|
||||
|
||||
tx := database.C
|
||||
tx = tx.Where("channel_id IN ?", lookupRange)
|
||||
tx = tx.Where("sender_id NOT IN ?", ignoreRange)
|
||||
tx = tx.Where("id > ?", pivot)
|
||||
|
||||
countTx := tx
|
||||
var count int64
|
||||
@ -50,20 +45,18 @@ func getWhatsNew(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
var items []models.Event
|
||||
if err := tx.
|
||||
Limit(take).
|
||||
Order("created_at DESC").
|
||||
Preload("Sender").
|
||||
Preload("Channel").
|
||||
Preload("Channel.Realm").
|
||||
Find(&items).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
var result []struct {
|
||||
ChannelID uint `json:"channel_id"`
|
||||
UnreadMessageCount int `json:"count"`
|
||||
}
|
||||
if err := database.C.Table("channel_members cm").
|
||||
Select("cm.channel_id, COUNT(m.id) AS unread_message_count").
|
||||
Joins("JOIN messages m ON m.channel_id = cm.channel_id").
|
||||
Where("m.id > cm.reading_anchor AND cm.account_id = ?", 1).
|
||||
Group("cm.channel_id").
|
||||
Scan(&result).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user