add: chat service
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/chat/rpc/internal/svc"
|
||||
"juwan-backend/app/chat/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchChatSessionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchChatSessionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchChatSessionsLogic {
|
||||
return &SearchChatSessionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchChatSessionsLogic) SearchChatSessions(in *pb.SearchChatSessionsReq) (*pb.SearchChatSessionsResp, error) {
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.RLock()
|
||||
defer store.Mu.RUnlock()
|
||||
|
||||
var results []*pb.ChatSessions
|
||||
for _, s := range store.Sessions {
|
||||
if in.Type != nil && s.Type != *in.Type {
|
||||
continue
|
||||
}
|
||||
if in.UserId != nil {
|
||||
found := false
|
||||
for _, p := range s.Participants {
|
||||
if p == *in.UserId {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
}
|
||||
results = append(results, s)
|
||||
}
|
||||
|
||||
limit := in.GetLimit()
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
offset := in.GetPage() * limit
|
||||
if offset >= int64(len(results)) {
|
||||
return &pb.SearchChatSessionsResp{}, nil
|
||||
}
|
||||
end := offset + limit
|
||||
if end > int64(len(results)) {
|
||||
end = int64(len(results))
|
||||
}
|
||||
|
||||
return &pb.SearchChatSessionsResp{ChatSessions: results[offset:end]}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user