Skip to content

Commit bb51c8e

Browse files
committed
use a new group conversation id method
1 parent 1a9317b commit bb51c8e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

conversation.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/json"
66
"fmt"
77
"time"
8+
9+
"github.com/gofrs/uuid/v5"
810
)
911

1012
type ParticipantSessionView struct {
@@ -40,16 +42,27 @@ func CreateContactConversation(ctx context.Context, participantID string, user *
4042
UserId: participantID,
4143
},
4244
}
43-
return CreateConversation(ctx, "CONTACT", UniqueConversationId(participantID, user.UserId), "", "", participants, user)
45+
return createConversation(ctx, "CONTACT", UniqueConversationId(participantID, user.UserId), "", "", participants, "", user)
46+
}
47+
48+
func CreateGroupConversation(ctx context.Context, name, announcement string, participants []Participant, user *SafeUser) (*Conversation, error) {
49+
pids := make([]string, len(participants))
50+
for i, p := range participants {
51+
pids[i] = p.UserId
52+
}
53+
randomId := uuid.Must(uuid.NewV4()).String()
54+
conversationId := GroupConversationId(user.UserId, name, pids, randomId)
55+
return createConversation(ctx, "GROUP", conversationId, name, announcement, participants, randomId, user)
4456
}
4557

46-
func CreateConversation(ctx context.Context, category, conversationId string, name, announcement string, participants []Participant, user *SafeUser) (*Conversation, error) {
58+
func createConversation(ctx context.Context, category, conversationId string, name, announcement string, participants []Participant, randomId string, user *SafeUser) (*Conversation, error) {
4759
params, err := json.Marshal(map[string]any{
4860
"category": category,
4961
"conversation_id": conversationId,
5062
"name": name,
5163
"announcement": announcement,
5264
"participants": participants,
65+
"random_id": randomId,
5366
})
5467
if err != nil {
5568
return nil, err

utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bot
33
import (
44
"crypto/md5"
55
"io"
6+
"slices"
67
"strings"
78

89
"github.com/gofrs/uuid/v5"
@@ -38,6 +39,18 @@ func UniqueConversationId(userId, recipientId string) string {
3839
return id.String()
3940
}
4041

42+
func GroupConversationId(ownerId, groupName string, participants []string, randomId string) string {
43+
randomId = uuid.Must(uuid.FromString(randomId)).String()
44+
gid := UniqueConversationId(ownerId, groupName)
45+
gid = UniqueConversationId(gid, randomId)
46+
47+
slices.Sort(participants)
48+
for _, p := range participants {
49+
gid = UniqueConversationId(gid, p)
50+
}
51+
return gid
52+
}
53+
4154
func Chunked(source []any, size int) [][]any {
4255
var result [][]any
4356
index := 0

0 commit comments

Comments
 (0)