/
helpers.go
103 lines (88 loc) · 2.95 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package sql
import (
"errors"
"strings"
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
"berty.tech/core/pkg/errorcodes"
"github.com/jinzhu/gorm"
)
func ContactByID(db *gorm.DB, id string) (*entity.Contact, error) {
var contact entity.Contact
return &contact, db.First(&contact, "ID = ?", id).Error
}
func FindContact(db *gorm.DB, input *entity.Contact) (*entity.Contact, error) {
if input.ID != "" {
return ContactByID(db, input.ID)
}
// FIXME: support looking-up by sigchain
return nil, errors.New("not enough information to search contact")
}
func ConversationByID(db *gorm.DB, id string) (*entity.Conversation, error) {
var conversation entity.Conversation
return &conversation, db.First(&conversation, "ID = ?", id).Error
}
func MembersByConversationID(db *gorm.DB, conversationID string) ([]*entity.ConversationMember, error) {
var members []*entity.ConversationMember
return members, db.
Where(entity.ConversationMember{ConversationID: conversationID}).
Find(&members).
Error
}
func MergeConversations(db *gorm.DB, conversations []entity.Conversation) (*entity.Conversation, error) {
logger().Debug("MERGE_CONVERSATION")
// chosse the conversation to save
merge := conversations[0]
for i := range conversations {
if strings.Compare(merge.ID, conversations[i].ID) > 0 {
merge = conversations[i]
}
}
// move all messages to that conversation
for i := range conversations {
if err := db.Model(&p2p.Event{}).Where(&p2p.Event{
ConversationID: conversations[i].ID,
Kind: p2p.Kind_ConversationNewMessage,
}).Update(&p2p.Event{
ConversationID: merge.ID,
}).Error; err != nil {
return nil, err
}
}
// remove other conversations
for i := range conversations {
if conversations[i].ID != merge.ID {
db.Where(&entity.ConversationMember{
ConversationID: conversations[i].ID,
}).Delete(&entity.ConversationMember{})
db.Delete(conversations[i])
}
}
return &merge, nil
}
func CreateConversation(db *gorm.DB, conversation *entity.Conversation) (*entity.Conversation, error) {
// save conversation
if err := db.Set("gorm:association_autoupdate", true).Save(conversation).Error; err != nil {
return nil, errorcodes.ErrDbCreate.Wrap(err)
}
// load new conversation again, to preload associations
conversation, err := ConversationByID(db, conversation.ID)
if err != nil {
return nil, errorcodes.ErrDb.Wrap(err)
}
// check if that is 1-1 conversation
// if len(conversation.Members) <= 2 {
// logger().Debug("CREATE_CONVERSATION")
// // check if there already a 1-1 conversation
// conversations := []entity.Conversation{}
// logger().Debug(fmt.Sprintf("GET_MEMBERS %+v", conversation.Members))
// db.Model(&entity.Conversation{}).Find(conversations).Error; err != nil {
// logger().Error(err.Error())
// }
// logger().Debug(fmt.Sprintf("CONVERSATIONS %+v", conversations))
// if len(conversations) >= 1 {
// return MergeConversations(db, conversations)
// }
// }
return conversation, nil
}