@@ -1054,55 +1054,93 @@ PrivacyListProxy.prototype = {
10541054 } ,
10551055
10561056 create : function ( list , callback ) {
1057- var iq , self = this ,
1058- userId , userJid ,
1059- userAction , userMuc ,
1060- listObj = { } ,
1061- listKeys = [ ] ;
1062-
1063- iq = $iq ( {
1064- from : connection . jid ,
1065- type : 'set' ,
1066- id : connection . getUniqueId ( 'edit' )
1067- } ) . c ( 'query' , {
1068- xmlns : Strophe . NS . PRIVACY_LIST
1069- } ) . c ( 'list' , {
1070- name : list . name
1071- } ) ;
1072-
1073- $ ( list . items ) . each ( function ( e , i ) {
1074- listObj [ i . user_id ] = i . action ;
1075- } ) ;
1057+ var self = this ,
1058+ userId , userJid , userMuc ,
1059+ userAction ,
1060+ mutualBlock ,
1061+ listPrivacy = { } ,
1062+ listUserId = [ ] ;
1063+
1064+ /** Filled listPrivacys */
1065+ for ( var i = list . items . length - 1 ; i >= 0 ; i -- ) {
1066+ var user = list . items [ i ] ;
1067+
1068+ listPrivacy [ user . user_id ] = {
1069+ action : user . action ,
1070+ mutualBlock : user . mutualBlock === true ? true : false
1071+ } ;
1072+ }
10761073
1077- listKeys = Object . keys ( listObj ) ;
1074+ /** Filled listUserId */
1075+ listUserId = Object . keys ( listPrivacy ) ;
10781076
1079- for ( var index = 0 , i = 0 , len = listKeys . length ; index < len ; index ++ , i = i + 2 ) {
1080- userId = listKeys [ index ] ;
1081- userAction = listObj [ userId ] ;
1082- userJid = self . helpers . jidOrUserId ( parseInt ( userId , 10 ) ) ;
1083- userMuc = self . helpers . getUserNickWithMucDomain ( userId ) ;
1077+ var iqParams = {
1078+ type : 'set' ,
1079+ from : connection . jid ,
1080+ id : connection . getUniqueId ( 'edit' )
1081+ } ,
1082+ iq = $iq ( iqParams ) . c ( 'query' , {
1083+ xmlns : Strophe . NS . PRIVACY_LIST
1084+ } ) . c ( 'list' , {
1085+ name : list . name
1086+ } ) ;
10841087
1088+ function createPrivacyItem ( iq , params ) {
10851089 iq . c ( 'item' , {
10861090 type : 'jid' ,
1087- value : userJid ,
1088- action : userAction ,
1089- order : i + 1
1090- } ) . c ( 'message' , {
1091- } ) . up ( ) . c ( 'presence-in' , {
1092- } ) . up ( ) . c ( 'presence-out' , {
1093- } ) . up ( ) . c ( 'iq' , {
1094- } ) . up ( ) . up ( ) ;
1091+ value : params . jidOrMuc ,
1092+ action : params . userAction ,
1093+ order : params . order
1094+ } ) . c ( 'message' , { } )
1095+ . up ( ) . c ( 'presence-in' , { } )
1096+ . up ( ) . c ( 'presence-out' , { } )
1097+ . up ( ) . c ( 'iq' , { } )
1098+ . up ( ) . up ( ) ;
1099+ return iq ;
1100+ }
10951101
1102+ function createPrivacyItemMutal ( iq , params ) {
10961103 iq . c ( 'item' , {
10971104 type : 'jid' ,
1098- value : userMuc ,
1099- action : userAction ,
1100- order : i + 2
1101- } ) . c ( 'message' , {
1102- } ) . up ( ) . c ( 'presence-in' , {
1103- } ) . up ( ) . c ( 'presence-out' , {
1104- } ) . up ( ) . c ( 'iq' , {
1105- } ) . up ( ) . up ( ) ;
1105+ value : params . jidOrMuc ,
1106+ action : params . userAction ,
1107+ order : params . order
1108+ } ) . up ( ) ;
1109+
1110+ return iq ;
1111+ }
1112+
1113+ for ( var index = 0 , i = 0 , len = listUserId . length ; index < len ; index ++ , i = i + 2 ) {
1114+ userId = listUserId [ index ] ;
1115+ mutualBlock = listPrivacy [ userId ] . mutualBlock ;
1116+
1117+ userAction = listPrivacy [ userId ] . action ;
1118+ userJid = self . helpers . jidOrUserId ( parseInt ( userId , 10 ) ) ;
1119+ userMuc = self . helpers . getUserNickWithMucDomain ( userId ) ;
1120+
1121+ if ( mutualBlock && userAction === 'deny' ) {
1122+ iq = createPrivacyItemMutal ( iq , {
1123+ order : i + 1 ,
1124+ jidOrMuc : userJid ,
1125+ userAction : userAction
1126+ } ) ;
1127+ iq = createPrivacyItemMutal ( iq , {
1128+ order : i + 2 ,
1129+ jidOrMuc : userMuc ,
1130+ userAction : userAction
1131+ } ) . up ( ) . up ( ) ;
1132+ } else {
1133+ iq = createPrivacyItem ( iq , {
1134+ order : i + 1 ,
1135+ jidOrMuc : userJid ,
1136+ userAction : userAction
1137+ } ) ;
1138+ iq = createPrivacyItem ( iq , {
1139+ order : i + 2 ,
1140+ jidOrMuc : userMuc ,
1141+ userAction : userAction
1142+ } ) ;
1143+ }
11061144 }
11071145
11081146 connection . sendIQ ( iq , function ( stanzaResult ) {
@@ -1115,6 +1153,7 @@ PrivacyListProxy.prototype = {
11151153 callback ( Utils . getError ( 408 ) ) ;
11161154 }
11171155 } ) ;
1156+
11181157 } ,
11191158
11201159 update : function ( list , callback ) {
0 commit comments