mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/micro/micro/v3/service/errors"
|
|
"github.com/micro/micro/v3/service/logger"
|
|
pb "github.com/micro/services/streams/proto"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RecentMessages returns the most recent messages in a group of conversations. By default the
|
|
// most messages retrieved per conversation is 25, however this can be overriden using the
|
|
// limit_per_conversation option
|
|
func (s *Streams) RecentMessages(ctx context.Context, req *pb.RecentMessagesRequest, rsp *pb.RecentMessagesResponse) error {
|
|
// validate the request
|
|
if len(req.ConversationIds) == 0 {
|
|
return ErrMissingConversationIDs
|
|
}
|
|
|
|
limit := DefaultLimit
|
|
if req.LimitPerConversation != nil {
|
|
limit = int(req.LimitPerConversation.Value)
|
|
}
|
|
|
|
// query the database
|
|
var msgs []Message
|
|
err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
for _, id := range req.ConversationIds {
|
|
var cms []Message
|
|
if err := tx.Where(&Message{ConversationID: id}).Order("sent_at DESC").Limit(limit).Find(&cms).Error; err != nil {
|
|
return err
|
|
}
|
|
msgs = append(msgs, cms...)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
logger.Errorf("Error reading messages: %v", err)
|
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
|
}
|
|
|
|
// serialize the response
|
|
rsp.Messages = make([]*pb.Message, len(msgs))
|
|
for i, m := range msgs {
|
|
rsp.Messages[i] = m.Serialize()
|
|
}
|
|
return nil
|
|
}
|