Skip to content

Commit

Permalink
Merge upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Nov 15, 2023
2 parents 453e514 + ae8de47 commit 9c054fa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
16 changes: 11 additions & 5 deletions pkg/signalmeow/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"sync"

"github.com/vector-im/mautrix-signal/pkg/libsignalgo"
"github.com/vector-im/mautrix-signal/pkg/signalmeow/web"
Expand Down Expand Up @@ -33,11 +34,16 @@ func (d *DeviceData) BasicAuthCreds() (string, string) {
// and other data that is used to communicate with the Signal servers and other clients.
type DeviceConnection struct {
// cached data (not persisted)
SenderCertificate *libsignalgo.SenderCertificate
GroupCredentials *GroupCredentials
GroupCache *GroupCache
ProfileCache *ProfileCache
GroupCallCache *map[string]bool
SenderCertificate *libsignalgo.SenderCertificate
GroupCredentials *GroupCredentials
GroupCache *GroupCache
ProfileCache *ProfileCache
GroupCallCache *map[string]bool
LastContactRequestTime *int64

// mutexes
EncryptionMutex sync.Mutex

// Network interfaces
AuthedWS *web.SignalWebsocket
UnauthedWS *web.SignalWebsocket
Expand Down
2 changes: 1 addition & 1 deletion pkg/signalmeow/receiving.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func StartReceiveLoops(ctx context.Context, d *Device) (chan SignalConnectionSta
return
case <-initialConnectChan:
zlog.Info().Msg("Both websockets connected, sending contacts sync request")
sendContactSyncRequest(ctx, d)
SendContactSyncRequest(ctx, d)
return
}
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/signalmeow/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func howManyOtherDevicesDoWeHave(ctx context.Context, d *Device) int {
}

func buildMessagesToSend(ctx context.Context, d *Device, recipientUuid string, content *signalpb.Content, unauthenticated bool) ([]MyMessage, error) {
// We need to prevent multiple encryption operations from happening at once, or else ratchets can race
d.Connection.EncryptionMutex.Lock()
defer d.Connection.EncryptionMutex.Unlock()

messages := []MyMessage{}

addresses, sessionRecords, err := d.SessionStoreExtras.AllSessionsForUUID(recipientUuid, ctx)
Expand Down Expand Up @@ -332,14 +336,23 @@ func syncMessageFromReadReceiptMessage(receiptMessage *signalpb.ReceiptMessage,
}
}

func sendContactSyncRequest(ctx context.Context, d *Device) error {
groupRequest := syncMessageForContactRequest()
func SendContactSyncRequest(ctx context.Context, d *Device) error {
currentUnixTime := time.Now().Unix()
lastRequestTime := d.Connection.LastContactRequestTime
// If we've requested in the last minute, don't request again
if lastRequestTime != nil && currentUnixTime-*lastRequestTime < 60 {
zlog.Warn().Msgf("Not sending contact sync request, already sent %v seconds ago", currentUnixTime-*lastRequestTime)
return nil
}

groupRequest := syncMessageForContactRequest()
_, err := sendContent(ctx, d, d.Data.AciUuid, uint64(currentUnixTime), groupRequest, 0)
if err != nil {
zlog.Err(err).Msg("Failed to send contact sync request message to myself (%v)")
return err
}
return err
d.Connection.LastContactRequestTime = &currentUnixTime
return nil
}

func TypingMessage(isTyping bool) *SignalContent {
Expand Down Expand Up @@ -634,7 +647,7 @@ func sendContent(
zlog.Err(err).Msg("Error getting profile key")
useUnidentifiedSender = false
// Try to self heal by requesting contact sync, though this is slow and not guaranteed to help
sendContactSyncRequest(ctx, d)
SendContactSyncRequest(ctx, d)
}
var accessKey *libsignalgo.AccessKey
if profileKey != nil {
Expand Down
6 changes: 3 additions & 3 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,10 @@ func (portal *Portal) handleSignalMessages(portalMessage portalSignalMessage) {
if err := portal.CreateMatrixRoom(portalMessage.user, nil); err != nil {
portal.log.Error().Err(err).Msg("Failed to create portal room")
return
} else {
portal.log.Info().Msgf("Created matrix room: %s", portal.MXID)
ensureGroupPuppetsAreJoinedToPortal(context.Background(), portalMessage.user, portal)
}
portal.log.Info().Msgf("Created matrix room: %s", portal.MXID)
ensureGroupPuppetsAreJoinedToPortal(context.Background(), portalMessage.user, portal)
signalmeow.SendContactSyncRequest(context.TODO(), portalMessage.user.SignalDevice)
}

//intent := portal.getMessageIntent(portalMessage.user, portalMessage.sender)
Expand Down

0 comments on commit 9c054fa

Please sign in to comment.