@@ -14,6 +14,7 @@ import (
1414 "encoding/hex"
1515 "errors"
1616 "fmt"
17+ "slices"
1718 "sort"
1819 "strconv"
1920 "strings"
@@ -1171,6 +1172,46 @@ func (cli *Client) encryptMessageForDevices(
11711172 includeIdentity := false
11721173 participantNodes := make ([]waBinary.Node , 0 , len (allDevices ))
11731174 var retryDevices , retryEncryptionIdentities []types.JID
1175+ jidLidMap := make (map [types.JID ]types.JID )
1176+ usyncJids := make ([]types.JID , 0 )
1177+ usyncDeviceList := make (map [types.JID ][]uint16 , 0 )
1178+
1179+ for _ , jid := range allDevices {
1180+ if jid .Server == types .DefaultUserServer {
1181+ lidForPN , err := cli .Store .LIDs .GetLIDForPN (ctx , jid )
1182+ if err != nil {
1183+ cli .Log .Warnf ("Failed to get LID for %s: %v" , jid , err )
1184+ } else if ! lidForPN .IsEmpty () {
1185+ jidLidMap [jid ] = lidForPN
1186+ } else {
1187+ if ! slices .Contains (usyncJids , jid .ToNonAD ()) {
1188+ usyncJids = append (usyncJids , jid .ToNonAD ())
1189+ }
1190+
1191+ if _ , ok := usyncDeviceList [jid .ToNonAD ()]; ok {
1192+ usyncDeviceList [jid .ToNonAD ()] = append (usyncDeviceList [jid .ToNonAD ()], jid .Device )
1193+ }
1194+ }
1195+ }
1196+ }
1197+
1198+ if len (usyncJids ) > 0 {
1199+ info , err := cli .GetUserInfo (usyncJids )
1200+ if err != nil {
1201+ cli .Log .Warnf ("Failed to get LID info from USync, err: %v" , err )
1202+ }
1203+ for jid , userInfo := range info {
1204+ if ! userInfo .LID .IsEmpty () {
1205+ for _ , deviceID := range usyncDeviceList [jid ] {
1206+ jid .Device = deviceID
1207+ lid := userInfo .LID
1208+ lid .Device = deviceID
1209+ jidLidMap [jid ] = lid
1210+ }
1211+ }
1212+ }
1213+ }
1214+
11741215 for _ , jid := range allDevices {
11751216 plaintext := msgPlaintext
11761217 if (jid .User == ownJID .User || jid .User == ownLID .User ) && dsmPlaintext != nil {
@@ -1181,10 +1222,8 @@ func (cli *Client) encryptMessageForDevices(
11811222 }
11821223 encryptionIdentity := jid
11831224 if jid .Server == types .DefaultUserServer {
1184- lidForPN , err := cli .Store .LIDs .GetLIDForPN (ctx , jid )
1185- if err != nil {
1186- cli .Log .Warnf ("Failed to get LID for %s: %v" , jid , err )
1187- } else if ! lidForPN .IsEmpty () {
1225+ lidForPN := jidLidMap [jid ]
1226+ if ! lidForPN .IsEmpty () {
11881227 cli .migrateSessionStore (ctx , jid , lidForPN )
11891228 encryptionIdentity = lidForPN
11901229 }
0 commit comments