Modify the code logic and add a mongo svc context

This commit is contained in:
wwweww
2026-04-25 08:29:25 +08:00
parent 5a99baafd0
commit 2ec2075c16
6 changed files with 103 additions and 265 deletions
+24 -244
View File
@@ -1,11 +1,5 @@
package chatcore
import (
"errors"
"sync"
"time"
)
type SessionType string
const (
@@ -22,247 +16,33 @@ const (
)
type Session struct {
Id int64 `json:"id"`
Type SessionType `json:"type"`
Name string `json:"name"`
CreatorId int64 `json:"creatorId"`
Participants []int64 `json:"participants"`
LastMessage string `json:"lastMessage"`
LastMessageAt int64 `json:"lastMessageAt"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
Id int64 `json:"id" bson:"_id"`
Type SessionType `json:"type" bson:"type"`
Name string `json:"name" bson:"name"`
CreatorId int64 `json:"creatorId" bson:"creatorId"`
Participants []int64 `json:"participants" bson:"participants"`
LastMessage string `json:"lastMessage" bson:"lastMessage"`
LastMessageAt int64 `json:"lastMessageAt" bson:"lastMessageAt"`
CreatedAt int64 `json:"createdAt" bson:"createdAt"`
UpdatedAt int64 `json:"updatedAt" bson:"updatedAt"`
}
type Message struct {
Id int64 `json:"id"`
SessionId int64 `json:"sessionId"`
SenderId int64 `json:"senderId"`
Type MessageType `json:"type"`
Content string `json:"content"`
CreatedAt int64 `json:"createdAt"`
Id int64 `json:"id" bson:"_id"`
SessionId int64 `json:"sessionId" bson:"sessionId"`
SenderId int64 `json:"senderId" bson:"senderId"`
Type MessageType `json:"type" bson:"type"`
Content string `json:"content" bson:"content"`
CreatedAt int64 `json:"createdAt" bson:"createdAt"`
}
type Store struct {
mu sync.RWMutex
nextSessionID int64
nextMessageID int64
Sessions map[int64]*Session
Messages map[int64]*Message
SessionMessages map[int64][]int64 // sessionId -> []messageId
}
func NewStore() *Store {
return &Store{
nextSessionID: 1000,
nextMessageID: 1000,
Sessions: make(map[int64]*Session),
Messages: make(map[int64]*Message),
SessionMessages: make(map[int64][]int64),
}
}
func (s *Store) CreateSession(typ SessionType, name string, creatorId int64, participants []int64) (*Session, error) {
if creatorId <= 0 {
return nil, errors.New("creatorId is required")
}
s.mu.Lock()
defer s.mu.Unlock()
now := time.Now().Unix()
ps := append([]int64(nil), participants...)
hasCreator := false
for _, p := range ps {
if p == creatorId {
hasCreator = true
break
}
}
if !hasCreator {
ps = append(ps, creatorId)
}
s.nextSessionID++
session := &Session{
Id: s.nextSessionID,
Type: typ,
Name: name,
CreatorId: creatorId,
Participants: ps,
CreatedAt: now,
UpdatedAt: now,
}
s.Sessions[session.Id] = session
return session, nil
}
func (s *Store) GetSession(id int64) (*Session, error) {
s.mu.RLock()
defer s.mu.RUnlock()
session, ok := s.Sessions[id]
if !ok {
return nil, errors.New("session not found")
}
return session, nil
}
func (s *Store) IsParticipant(sessionId, userId int64) bool {
s.mu.RLock()
defer s.mu.RUnlock()
session, ok := s.Sessions[sessionId]
if !ok {
return false
}
for _, p := range session.Participants {
if p == userId {
return true
}
}
return false
}
func (s *Store) ListUserSessions(userId int64, page, limit int) []*Session {
s.mu.RLock()
defer s.mu.RUnlock()
var results []*Session
for _, sess := range s.Sessions {
for _, p := range sess.Participants {
if p == userId {
results = append(results, sess)
break
}
}
}
offset := page * limit
if offset >= len(results) {
return nil
}
end := offset + limit
if end > len(results) {
end = len(results)
}
return results[offset:end]
}
func (s *Store) AddParticipant(sessionId, userId int64) error {
s.mu.Lock()
defer s.mu.Unlock()
session, ok := s.Sessions[sessionId]
if !ok {
return errors.New("session not found")
}
for _, p := range session.Participants {
if p == userId {
return nil
}
}
session.Participants = append(session.Participants, userId)
session.UpdatedAt = time.Now().Unix()
return nil
}
func (s *Store) RemoveParticipant(sessionId, userId int64) error {
s.mu.Lock()
defer s.mu.Unlock()
session, ok := s.Sessions[sessionId]
if !ok {
return errors.New("session not found")
}
filtered := make([]int64, 0, len(session.Participants))
for _, p := range session.Participants {
if p != userId {
filtered = append(filtered, p)
}
}
session.Participants = filtered
session.UpdatedAt = time.Now().Unix()
return nil
}
func (s *Store) AddMessage(sessionId, senderId int64, msgType MessageType, content string) (*Message, error) {
if sessionId <= 0 {
return nil, errors.New("sessionId is required")
}
if senderId <= 0 {
return nil, errors.New("senderId is required")
}
if content == "" {
return nil, errors.New("content is required")
}
s.mu.Lock()
defer s.mu.Unlock()
session, ok := s.Sessions[sessionId]
if !ok {
return nil, errors.New("session not found")
}
now := time.Now().Unix()
if msgType == "" {
msgType = MessageTypeText
}
s.nextMessageID++
msg := &Message{
Id: s.nextMessageID,
SessionId: sessionId,
SenderId: senderId,
Type: msgType,
Content: content,
CreatedAt: now,
}
s.Messages[msg.Id] = msg
s.SessionMessages[sessionId] = append(s.SessionMessages[sessionId], msg.Id)
session.LastMessage = content
session.LastMessageAt = now
session.UpdatedAt = now
return msg, nil
}
func (s *Store) GetMessages(sessionId int64, page, limit int) []*Message {
s.mu.RLock()
defer s.mu.RUnlock()
msgIDs := s.SessionMessages[sessionId]
var results []*Message
for _, id := range msgIDs {
if msg, ok := s.Messages[id]; ok {
results = append(results, msg)
}
}
if limit <= 0 {
limit = 50
}
offset := page * limit
if offset >= len(results) {
return nil
}
end := offset + limit
if end > len(results) {
end = len(results)
}
return results[offset:end]
}
func (s *Store) DeleteSession(id int64) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.Sessions, id)
for _, msgId := range s.SessionMessages[id] {
delete(s.Messages, msgId)
}
delete(s.SessionMessages, id)
type Store interface {
CreateSession(typ SessionType, name string, creatorId int64, participants []int64) (*Session, error)
GetSession(id int64) (*Session, error)
ListUserSessions(userId int64, page, limit int) []*Session
AddParticipant(sessionId, userId int64) error
RemoveParticipant(sessionId, userId int64) error
DeleteSession(id int64)
AddMessage(sessionId, senderId int64, msgType MessageType, content string) (*Message, error)
GetMessages(sessionId int64, page, limit int) []*Message
}