Skip to content

Commit

Permalink
fix_: revert group_id -> chat_id change
Browse files Browse the repository at this point in the history
  • Loading branch information
alwx committed Apr 29, 2024
1 parent d43c9e7 commit 53a98f7
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 470 deletions.
2 changes: 1 addition & 1 deletion protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message
syncMessage := peersyncing.SyncMessage{
Type: syncMessageType,
ID: types.Hex2Bytes(rawMessage.ID),
ChatID: []byte(chat.ID),
GroupID: []byte(chat.ID),
Payload: wrappedMessage,
Timestamp: m.transport.GetCurrentTime() / 1000,
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/messenger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2457,7 +2457,7 @@ func (m *Messenger) addPeersyncingMessage(chat *Chat, msg *v1protocol.StatusMess
syncMessage := peersyncing.SyncMessage{
Type: syncMessageType,
ID: msg.ApplicationLayer.ID,
ChatID: []byte(chat.ID),
GroupID: []byte(chat.ID),
Payload: msg.EncryptionLayer.Payload,
Timestamp: uint64(msg.TransportLayer.Message.Timestamp),
}
Expand Down
14 changes: 7 additions & 7 deletions protocol/messenger_peersyncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func (m *Messenger) sendDatasyncOffersForCommunities() error {
if len(availableMessagesMap) == 0 {
continue
}
for chatID, m := range availableMessagesMap {
datasyncMessage.GroupOffers = append(datasyncMessage.GroupOffers, &datasyncproto.Offer{GroupId: types.Hex2Bytes(chatID), MessageIds: m})
for chatID, messageIDs := range availableMessagesMap {
datasyncMessage.GroupOffers = append(datasyncMessage.GroupOffers, &datasyncproto.Offer{GroupId: types.Hex2Bytes(chatID), MessageIds: messageIDs})
}
payload, err := proto.Marshal(datasyncMessage)
if err != nil {
Expand Down Expand Up @@ -189,8 +189,8 @@ func (m *Messenger) sendDatasyncOffersForChats() error {
if len(availableMessagesMap) == 0 {
continue
}
for _, message := range availableMessagesMap {
datasyncMessage.GroupOffers = append(datasyncMessage.GroupOffers, &datasyncproto.Offer{GroupId: chatIDBytes, MessageIds: message})
for _, messageIDs := range availableMessagesMap {
datasyncMessage.GroupOffers = append(datasyncMessage.GroupOffers, &datasyncproto.Offer{GroupId: chatIDBytes, MessageIds: messageIDs})
}
payload, err := proto.Marshal(datasyncMessage)
if err != nil {
Expand Down Expand Up @@ -229,7 +229,7 @@ func (m *Messenger) OnDatasyncOffer(response *common.HandleMessageResponse) erro
var offeredMessages []peersyncing.SyncMessage

for _, o := range offers {
offeredMessages = append(offeredMessages, peersyncing.SyncMessage{ChatID: o.GroupID, ID: o.MessageID})
offeredMessages = append(offeredMessages, peersyncing.SyncMessage{GroupID: o.GroupID, ID: o.MessageID})
}

messagesToFetch, err := m.peersyncing.OnOffer(offeredMessages)
Expand Down Expand Up @@ -276,7 +276,7 @@ func (m *Messenger) OnDatasyncOffer(response *common.HandleMessageResponse) erro
func (m *Messenger) canSyncMessageWith(message peersyncing.SyncMessage, peer *ecdsa.PublicKey) (bool, error) {
switch message.Type {
case peersyncing.SyncMessageCommunityType:
chat, ok := m.allChats.Load(string(message.ChatID))
chat, ok := m.allChats.Load(string(message.GroupID))
if !ok {
return false, nil
}
Expand All @@ -287,7 +287,7 @@ func (m *Messenger) canSyncMessageWith(message peersyncing.SyncMessage, peer *ec

return m.canSyncCommunityMessageWith(chat, community, peer)
case peersyncing.SyncMessageOneToOneType:
chat, ok := m.allChats.Load(string(message.ChatID))
chat, ok := m.allChats.Load(string(message.GroupID))
if !ok {
return false, nil
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/messenger_peersyncing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *MessengerPeersyncingSuite) TestCanSyncMessageWith() {

syncMessage := peersyncing.SyncMessage{
ID: []byte("test-id"),
ChatID: []byte(chat.ID),
GroupID: []byte(chat.ID),
Type: peersyncing.SyncMessageCommunityType,
Payload: []byte("some-payload"),
Timestamp: 1,
Expand Down Expand Up @@ -342,7 +342,7 @@ func (s *MessengerPeersyncingSuite) TestCanSyncOneToOneMessageWith() {

syncMessage := peersyncing.SyncMessage{
ID: []byte("test-id"),
ChatID: []byte(chat.ID),
GroupID: []byte(chat.ID),
Type: peersyncing.SyncMessageOneToOneType,
Payload: []byte("some-payload"),
Timestamp: chat.LastClockValue,
Expand Down
712 changes: 277 additions & 435 deletions protocol/migrations/migrations.go

Large diffs are not rendered by default.

This file was deleted.

2 changes: 1 addition & 1 deletion protocol/peersyncing/peersyncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p *PeerSyncing) AvailableMessagesMapByChatIDs(groupIDs [][]byte, limit int
}
availableMessagesMap := make(map[string][][]byte)
for _, m := range availableMessages {
chatID := types.Bytes2Hex(m.ChatID)
chatID := types.Bytes2Hex(m.GroupID)
availableMessagesMap[chatID] = append(availableMessagesMap[chatID], m.ID)
}
return availableMessagesMap, err
Expand Down
14 changes: 7 additions & 7 deletions protocol/peersyncing/peersyncing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *PeerSyncingSuite) TestBasic() {

syncMessage := SyncMessage{
ID: []byte("test-id"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test"),
Timestamp: 1,
Expand All @@ -49,7 +49,7 @@ func (s *PeerSyncingSuite) TestBasic() {
s.Require().NoError(err)
s.Require().Len(allMessages, 1)

byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{syncMessage.ChatID}, 10)
byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{syncMessage.GroupID}, 10)

s.Require().NoError(err)
s.Require().Len(byChatID, 1)
Expand All @@ -61,7 +61,7 @@ func (s *PeerSyncingSuite) TestBasic() {

newSyncMessage := SyncMessage{
ID: []byte("test-id-2"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test-2"),
Timestamp: 2,
Expand All @@ -78,31 +78,31 @@ func (s *PeerSyncingSuite) TestOrderAndLimit() {

syncMessage1 := SyncMessage{
ID: []byte("test-id-1"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test"),
Timestamp: 1,
}

syncMessage2 := SyncMessage{
ID: []byte("test-id-2"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test"),
Timestamp: 2,
}

syncMessage3 := SyncMessage{
ID: []byte("test-id-3"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test"),
Timestamp: 3,
}

syncMessage4 := SyncMessage{
ID: []byte("test-id-4"),
ChatID: testCommunityID,
GroupID: testCommunityID,
Type: SyncMessageCommunityType,
Payload: []byte("test"),
Timestamp: 4,
Expand Down
4 changes: 2 additions & 2 deletions protocol/peersyncing/sync_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ type SyncMessageType int
type SyncMessage struct {
ID []byte
Type SyncMessageType
ChatID []byte
GroupID []byte
Payload []byte
Timestamp uint64
}

var ErrSyncMessageNotValid = errors.New("sync message not valid")

func (s *SyncMessage) Valid() error {
valid := len(s.ID) != 0 && s.Type != SyncMessageNoType && len(s.ChatID) != 0 && len(s.Payload) != 0 && s.Timestamp != 0
valid := len(s.ID) != 0 && s.Type != SyncMessageNoType && len(s.GroupID) != 0 && len(s.Payload) != 0 && s.Timestamp != 0
if !valid {
return ErrSyncMessageNotValid
}
Expand Down
18 changes: 9 additions & 9 deletions protocol/peersyncing/sync_message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func (p *SyncMessageSQLitePersistence) Add(message SyncMessage) error {
if err := message.Valid(); err != nil {
return err
}
_, err := p.db.Exec(`INSERT INTO peersyncing_messages (id, type, chat_id, payload, timestamp) VALUES (?, ?, ?, ?, ?)`, message.ID, message.Type, message.ChatID, message.Payload, message.Timestamp)
_, err := p.db.Exec(`INSERT INTO peersyncing_messages (id, type, group_id, payload, timestamp) VALUES (?, ?, ?, ?, ?)`, message.ID, message.Type, message.GroupID, message.Payload, message.Timestamp)
return err
}

func (p *SyncMessageSQLitePersistence) All() ([]SyncMessage, error) {
var messages []SyncMessage
rows, err := p.db.Query(`SELECT id, type, chat_id, payload, timestamp FROM peersyncing_messages`)
rows, err := p.db.Query(`SELECT id, type, group_id, payload, timestamp FROM peersyncing_messages`)
if err != nil {
return nil, err
}
Expand All @@ -43,7 +43,7 @@ func (p *SyncMessageSQLitePersistence) All() ([]SyncMessage, error) {
for rows.Next() {
var m SyncMessage

err := rows.Scan(&m.ID, &m.Type, &m.ChatID, &m.Payload, &m.Timestamp)
err := rows.Scan(&m.ID, &m.Type, &m.GroupID, &m.Payload, &m.Timestamp)
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,7 @@ func (p *SyncMessageSQLitePersistence) ByChatIDs(ids [][]byte, limit int) ([]Syn
queryArgs = append(queryArgs, limit)

inVector := strings.Repeat("?, ", len(ids)-1) + "?"
query := "SELECT id, type, chat_id, payload, timestamp FROM peersyncing_messages WHERE chat_id IN (" + inVector + ") ORDER BY timestamp DESC LIMIT ?" // nolint: gosec
query := "SELECT id, type, group_id, payload, timestamp FROM peersyncing_messages WHERE group_id IN (" + inVector + ") ORDER BY timestamp DESC LIMIT ?" // nolint: gosec

var messages []SyncMessage
rows, err := p.db.Query(query, queryArgs...)
Expand All @@ -78,7 +78,7 @@ func (p *SyncMessageSQLitePersistence) ByChatIDs(ids [][]byte, limit int) ([]Syn
for rows.Next() {
var m SyncMessage

err := rows.Scan(&m.ID, &m.Type, &m.ChatID, &m.Payload, &m.Timestamp)
err := rows.Scan(&m.ID, &m.Type, &m.GroupID, &m.Payload, &m.Timestamp)
if err != nil {
return nil, err
}
Expand All @@ -99,7 +99,7 @@ func (p *SyncMessageSQLitePersistence) Complement(messages []SyncMessage) ([]Syn
}

inVector := strings.Repeat("?, ", len(ids)-1) + "?"
query := "SELECT id, type, chat_id, payload, timestamp FROM peersyncing_messages WHERE id IN (" + inVector + ")" // nolint: gosec
query := "SELECT id, type, group_id, payload, timestamp FROM peersyncing_messages WHERE id IN (" + inVector + ")" // nolint: gosec

availableMessages := make(map[string]SyncMessage)
rows, err := p.db.Query(query, ids...)
Expand All @@ -112,7 +112,7 @@ func (p *SyncMessageSQLitePersistence) Complement(messages []SyncMessage) ([]Syn
for rows.Next() {
var m SyncMessage

err := rows.Scan(&m.ID, &m.Type, &m.ChatID, &m.Payload, &m.Timestamp)
err := rows.Scan(&m.ID, &m.Type, &m.GroupID, &m.Payload, &m.Timestamp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (p *SyncMessageSQLitePersistence) ByMessageIDs(ids [][]byte) ([]SyncMessage
}

inVector := strings.Repeat("?, ", len(ids)-1) + "?"
query := "SELECT id, type, chat_id, payload, timestamp FROM peersyncing_messages WHERE id IN (" + inVector + ")" // nolint: gosec
query := "SELECT id, type, group_id, payload, timestamp FROM peersyncing_messages WHERE id IN (" + inVector + ")" // nolint: gosec

var messages []SyncMessage
rows, err := p.db.Query(query, queryArgs...)
Expand All @@ -156,7 +156,7 @@ func (p *SyncMessageSQLitePersistence) ByMessageIDs(ids [][]byte) ([]SyncMessage
for rows.Next() {
var m SyncMessage

err := rows.Scan(&m.ID, &m.Type, &m.ChatID, &m.Payload, &m.Timestamp)
err := rows.Scan(&m.ID, &m.Type, &m.GroupID, &m.Payload, &m.Timestamp)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 53a98f7

Please sign in to comment.