Channel history

This commit is contained in:
LittleSheep 2024-03-30 18:21:17 +08:00
parent d5093d7b9c
commit 8e31675317
8 changed files with 150 additions and 21 deletions

View File

@ -27,9 +27,7 @@ type Attachment struct {
Type AttachmentType `json:"type"` Type AttachmentType `json:"type"`
ExternalUrl string `json:"external_url"` ExternalUrl string `json:"external_url"`
Author Account `json:"author"` Author Account `json:"author"`
ArticleID *uint `json:"article_id"` MessageID *uint `json:"message_id"`
MomentID *uint `json:"moment_id"`
CommentID *uint `json:"comment_id"`
AuthorID uint `json:"author_id"` AuthorID uint `json:"author_id"`
} }

View File

@ -16,6 +16,7 @@ type Message struct {
Content string `json:"content"` Content string `json:"content"`
Metadata datatypes.JSONMap `json:"metadata"` Metadata datatypes.JSONMap `json:"metadata"`
Type MessageType `json:"type"` Type MessageType `json:"type"`
Attachments []Attachment `json:"attachments"`
ChannelID uint `json:"channel_id"` ChannelID uint `json:"channel_id"`
SenderID uint `json:"sender_id"` SenderID uint `json:"sender_id"`
} }

View File

@ -8,11 +8,11 @@ import (
) )
func getChannel(c *fiber.Ctx) error { func getChannel(c *fiber.Ctx) error {
id, _ := c.ParamsInt("channelId", 0) alias := c.Params("channel")
var channel models.Channel var channel models.Channel
if err := database.C.Where(&models.Channel{ if err := database.C.Where(&models.Channel{
BaseModel: models.BaseModel{ID: uint(id)}, Alias: alias,
}).First(&channel).Error; err != nil { }).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }

View File

@ -0,0 +1,52 @@
package server
import (
"git.solsynth.dev/hydrogen/messaging/pkg/models"
"git.solsynth.dev/hydrogen/messaging/pkg/services"
"github.com/gofiber/fiber/v2"
)
func getMessageHistory(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
alias := c.Params("channel")
channel, err := services.GetChannelWithAlias(alias)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
count := services.CountMessage(channel)
messages, err := services.ListMessage(channel, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": messages,
})
}
func newTextMessage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
alias := c.Params("channel")
var data struct {
Content string `json:"content" validate:"required"`
Attachments []models.Attachment `json:"attachments"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var message models.Message
if channel, member, err := services.GetAvailableChannelWithAlias(alias, user); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if message, err = services.NewTextMessage(data.Content, member, channel, data.Attachments...); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(message)
}

View File

@ -79,9 +79,11 @@ func NewServer() {
channels.Get("/", listChannel) channels.Get("/", listChannel)
channels.Get("/me", authMiddleware, listOwnedChannel) channels.Get("/me", authMiddleware, listOwnedChannel)
channels.Get("/me/available", authMiddleware, listAvailableChannel) channels.Get("/me/available", authMiddleware, listAvailableChannel)
channels.Get("/:channelId", getChannel) channels.Get("/:channel", getChannel)
channels.Get("/:channel/messages", getMessageHistory)
channels.Get("/:channelId/members", listChannelMembers) channels.Get("/:channelId/members", listChannelMembers)
channels.Post("/", authMiddleware, createChannel) channels.Post("/", authMiddleware, createChannel)
channels.Post("/:channel/messages", authMiddleware, newTextMessage)
channels.Post("/:channelId/invite", authMiddleware, inviteChannel) channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
channels.Post("/:channelId/kick", authMiddleware, kickChannel) channels.Post("/:channelId/kick", authMiddleware, kickChannel)
channels.Put("/:channelId", authMiddleware, editChannel) channels.Put("/:channelId", authMiddleware, editChannel)

View File

@ -7,10 +7,51 @@ import (
"github.com/samber/lo" "github.com/samber/lo"
) )
func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.ChannelMember, error) { func GetChannel(id uint) (models.Channel, error) {
var channel models.Channel
if err := database.C.Where(models.Channel{
BaseModel: models.BaseModel{ID: id},
}).First(&channel).Error; err != nil {
return channel, err
}
return channel, nil
}
func GetChannelWithAlias(alias string) (models.Channel, error) {
var channel models.Channel
if err := database.C.Where(models.Channel{
Alias: alias,
}).First(&channel).Error; err != nil {
return channel, err
}
return channel, nil
}
func GetAvailableChannelWithAlias(alias string, user models.Account) (models.Channel, models.ChannelMember, error) {
var err error
var member models.ChannelMember var member models.ChannelMember
var channel models.Channel var channel models.Channel
if err := database.C.Where("id = ?", id).First(&channel).Error; err != nil { if channel, err = GetChannelWithAlias(alias); err != nil {
return channel, member, err
}
if err := database.C.Where(models.ChannelMember{
AccountID: user.ID,
ChannelID: channel.ID,
}).First(&member).Error; err != nil {
return channel, member, fmt.Errorf("channel principal not found: %v", err.Error())
}
return channel, member, nil
}
func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.ChannelMember, error) {
var err error
var member models.ChannelMember
var channel models.Channel
if channel, err = GetChannel(id); err != nil {
return channel, member, err return channel, member, err
} }

View File

@ -24,8 +24,16 @@ func DealCommand(task models.UnifiedCommand, user models.Account) *models.Unifie
var req struct { var req struct {
ChannelID uint `json:"channel_id"` ChannelID uint `json:"channel_id"`
Content string `json:"content"` Content string `json:"content"`
Attachments []models.Attachment `json:"attachments"`
} }
models.FitStruct(task.Payload, &req) models.FitStruct(task.Payload, &req)
if len(req.Content) == 0 {
return &models.UnifiedCommand{
Action: "error",
Message: "content cannot be empty",
}
}
if channel, member, err := GetAvailableChannel(req.ChannelID, user); err != nil { if channel, member, err := GetAvailableChannel(req.ChannelID, user); err != nil {
return lo.ToPtr(models.UnifiedCommandFromError(err)) return lo.ToPtr(models.UnifiedCommandFromError(err))

View File

@ -5,12 +5,39 @@ import (
"git.solsynth.dev/hydrogen/messaging/pkg/models" "git.solsynth.dev/hydrogen/messaging/pkg/models"
) )
func NewTextMessage(content string, sender models.ChannelMember, channel models.Channel) (models.Message, error) { func CountMessage(channel models.Channel) int64 {
var count int64
if err := database.C.Where(models.Message{
ChannelID: channel.ID,
}).Model(&models.Message{}).Count(&count).Error; err != nil {
return 0
} else {
return count
}
}
func ListMessage(channel models.Channel, take int, offset int) ([]models.Message, error) {
if take > 100 {
take = 100
}
var messages []models.Message
if err := database.C.Where(models.Message{
ChannelID: channel.ID,
}).Limit(take).Offset(offset).Find(&messages).Error; err != nil {
return messages, err
} else {
return messages, nil
}
}
func NewTextMessage(content string, sender models.ChannelMember, channel models.Channel, attachments ...models.Attachment) (models.Message, error) {
message := models.Message{ message := models.Message{
Content: content, Content: content,
Metadata: nil, Metadata: nil,
ChannelID: channel.ID, ChannelID: channel.ID,
SenderID: sender.ID, SenderID: sender.ID,
Attachments: attachments,
Type: models.MessageTypeText, Type: models.MessageTypeText,
} }