@@ -1171,6 +1171,53 @@ func (cli *Client) encryptMessageForDevices(
11711171 includeIdentity := false
11721172 participantNodes := make ([]waBinary.Node , 0 , len (allDevices ))
11731173 var retryDevices , retryEncryptionIdentities []types.JID
1174+ jidLidMap := make (map [types.JID ]types.JID )
1175+ usyncDeviceList := make (map [types.JID ][]uint16 ) // bare JID -> device IDs
1176+
1177+ for _ , jid := range allDevices {
1178+ if jid .Server == types .DefaultUserServer {
1179+ lidForPN , err := cli .Store .LIDs .GetLIDForPN (ctx , jid )
1180+ if err != nil {
1181+ cli .Log .Warnf ("Failed to get LID for %s: %v" , jid , err )
1182+ }
1183+
1184+ if ! lidForPN .IsEmpty () {
1185+ jidLidMap [jid ] = lidForPN
1186+ } else {
1187+ bare := jid .ToNonAD ()
1188+ usyncDeviceList [bare ] = append (usyncDeviceList [bare ], jid .Device )
1189+ }
1190+
1191+ }
1192+ }
1193+
1194+ if len (usyncDeviceList ) > 0 {
1195+ usyncJids := make ([]types.JID , 0 , len (usyncDeviceList ))
1196+ for bare := range usyncDeviceList {
1197+ usyncJids = append (usyncJids , bare )
1198+ }
1199+
1200+ info , err := cli .GetUserInfo (usyncJids )
1201+ if err != nil {
1202+ cli .Log .Warnf ("Failed to get LID info from USync, err: %v" , err )
1203+ } else {
1204+ for bare , userInfo := range info {
1205+ if userInfo .LID .IsEmpty () {
1206+ continue
1207+ }
1208+ for _ , deviceID := range usyncDeviceList [bare ] {
1209+ jid := bare
1210+ jid .Device = deviceID
1211+
1212+ lid := userInfo .LID
1213+ lid .Device = deviceID
1214+
1215+ jidLidMap [jid ] = lid
1216+ }
1217+ }
1218+ }
1219+ }
1220+
11741221 for _ , jid := range allDevices {
11751222 plaintext := msgPlaintext
11761223 if (jid .User == ownJID .User || jid .User == ownLID .User ) && dsmPlaintext != nil {
@@ -1179,14 +1226,12 @@ func (cli *Client) encryptMessageForDevices(
11791226 }
11801227 plaintext = dsmPlaintext
11811228 }
1229+
11821230 encryptionIdentity := jid
11831231 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 () {
1188- cli .migrateSessionStore (ctx , jid , lidForPN )
1189- encryptionIdentity = lidForPN
1232+ if lid , ok := jidLidMap [jid ]; ok && ! lid .IsEmpty () {
1233+ cli .migrateSessionStore (ctx , jid , lid )
1234+ encryptionIdentity = lid
11901235 }
11911236 }
11921237
0 commit comments