55import java .util .concurrent .BlockingQueue ;
66
77import org .apache .log4j .Logger ;
8+ import org .buddycloud .channelserver .Configuration ;
89import org .buddycloud .channelserver .channel .ChannelManager ;
10+ import org .buddycloud .channelserver .db .ClosableIteratorImpl ;
911import org .buddycloud .channelserver .db .exception .NodeStoreException ;
1012import org .buddycloud .channelserver .packetprocessor .iq .namespace .pubsub .JabberPubsub ;
1113import org .buddycloud .channelserver .packetprocessor .iq .namespace .pubsub .PubSubElementProcessorAbstract ;
1214import org .buddycloud .channelserver .pubsub .affiliation .Affiliations ;
15+ import org .buddycloud .channelserver .pubsub .model .GlobalItemID ;
1316import org .buddycloud .channelserver .pubsub .model .NodeAffiliation ;
1417import org .buddycloud .channelserver .pubsub .model .NodeItem ;
1518import org .buddycloud .channelserver .pubsub .model .NodeSubscription ;
19+ import org .buddycloud .channelserver .pubsub .model .impl .GlobalItemIDImpl ;
1620import org .buddycloud .channelserver .pubsub .subscription .Subscriptions ;
1721import org .buddycloud .channelserver .utils .node .item .payload .Buddycloud ;
1822import org .dom4j .Element ;
@@ -30,17 +34,14 @@ public class ItemDelete extends PubSubElementProcessorAbstract {
3034
3135 private static final Logger LOGGER = Logger .getLogger (ItemDelete .class );
3236
33- private final BlockingQueue <Packet > outQueue ;
34- private final ChannelManager channelManager ;
35-
36- private String itemId ;
37+ private GlobalItemID itemId ;
3738 private NodeItem nodeItem ;
3839 private Element parsedPayload ;
3940
4041 public ItemDelete (BlockingQueue <Packet > outQueue ,
4142 ChannelManager channelManager ) {
42- this .outQueue = outQueue ;
43- this .channelManager = channelManager ;
43+ this .setOutQueue ( outQueue ) ;
44+ this .setChannelManager ( channelManager ) ;
4445 }
4546
4647 @ Override
@@ -52,22 +53,24 @@ public void process(Element elm, JID actor, IQ reqIQ, Element rsm)
5253 response = IQ .createResultIQ (request );
5354 node = element .attributeValue ("node" );
5455 this .actor = actor ;
55- if (null == this .actor ) this .actor = request .getFrom ();
56-
56+ if (null == this .actor ) {
57+ this .actor = request .getFrom ();
58+ }
5759 if (!channelManager .isLocalNode (node )) {
5860 makeRemoteRequest ();
5961 return ;
6062 }
6163
6264 try {
63- if (!validNodeProvided () || !nodeExists () || !itemIdProvided ()
65+ if (!validNodeProvided () || !nodeExists () || !itemIdProvided ()
6466 || !itemExists () || !validPayload () || !canDelete ()) {
6567 outQueue .put (response );
6668 return ;
6769 }
6870 deleteItem ();
6971 outQueue .put (response );
70- sendNotifications ();
72+ deleteReplies ();
73+ sendNotifications (node , itemId );
7174 return ;
7275 } catch (NodeStoreException e ) {
7376 logger .error (e );
@@ -79,33 +82,56 @@ public void process(Element elm, JID actor, IQ reqIQ, Element rsm)
7982 PacketError .Condition .bad_request );
8083 } catch (IllegalArgumentException e ) {
8184 logger .error (e );
82- setErrorCondition (PacketError .Type .modify , PacketError .Condition .bad_request );
85+ setErrorCondition (PacketError .Type .modify ,
86+ PacketError .Condition .bad_request );
8387 }
8488 outQueue .put (response );
8589 }
8690
87- private void sendNotifications () throws NodeStoreException {
91+ private void deleteReplies () throws NodeStoreException {
92+ if (null != nodeItem .getInReplyTo ()) {
93+ return ;
94+ }
95+ ClosableIteratorImpl <NodeItem > replies = channelManager
96+ .getNodeItemReplies (node , itemId .getItemID (), null , -1 );
97+ NodeItem reply = null ;
98+ while (replies .hasNext ()) {
99+ reply = replies .next ();
100+ channelManager .deleteNodeItemById (reply .getNodeId (), reply .getId ());
101+
102+ sendNotifications (
103+ node ,
104+ new GlobalItemIDImpl (new JID (this .getServerDomain ()), reply
105+ .getNodeId (), reply .getId ()));
106+ }
107+ }
108+
109+ private void sendNotifications (String node , GlobalItemID itemId )
110+ throws NodeStoreException {
88111 try {
89112 String notify = request .getElement ().element ("pubsub" )
90113 .element ("retract" ).attributeValue ("notify" );
91114
92- if ((notify != null ) && (notify .equals ("false" ) || notify .equals ("0" ))) {
115+ if ((notify != null )
116+ && (notify .equals ("false" ) || notify .equals ("0" ))) {
93117 return ;
94118 }
95119 ResultSet <NodeSubscription > subscriptions = channelManager
96120 .getNodeSubscriptionListeners (node );
97- Message notification = getNotificationMessage ();
121+ Message notification = getNotificationMessage (node , itemId );
98122
99123 for (NodeSubscription subscription : subscriptions ) {
100- logger .debug ("Subscription [node: " + subscription .getNodeId () + ", listener: "
101- + subscription .getListener () + ", subscription: " + subscription .getSubscription () + "]" );
124+ logger .debug ("Subscription [node: " + subscription .getNodeId ()
125+ + ", listener: " + subscription .getListener ()
126+ + ", subscription: " + subscription .getSubscription ()
127+ + "]" );
102128 if (subscription .getSubscription ().equals (
103129 Subscriptions .subscribed )) {
104130 notification .setTo (subscription .getListener ());
105131 outQueue .put (notification .createCopy ());
106132 }
107133 }
108-
134+
109135 Collection <JID > admins = getAdminUsers ();
110136 for (JID admin : admins ) {
111137 notification .setTo (admin );
@@ -120,21 +146,22 @@ private void sendNotifications() throws NodeStoreException {
120146 }
121147 }
122148
123- private Message getNotificationMessage () {
149+ private Message getNotificationMessage (String node , GlobalItemID itemId ) {
124150 Message notification = new Message ();
125151 notification .setType (Message .Type .headline );
126- notification .getElement ().addAttribute ("remote-server-discover" , "false" );
152+ notification .getElement ().addAttribute ("remote-server-discover" ,
153+ "false" );
127154 Element event = notification .addChildElement ("event" ,
128155 JabberPubsub .NS_PUBSUB_EVENT );
129156 Element items = event .addElement ("items" );
130157 items .addAttribute ("node" , node );
131158 Element retract = items .addElement ("retract" );
132- retract .addAttribute ("id" , itemId );
159+ retract .addAttribute ("id" , itemId . getItemID () );
133160 return notification ;
134161 }
135162
136163 private void deleteItem () throws NodeStoreException {
137- channelManager .deleteNodeItemById (node , itemId );
164+ channelManager .deleteNodeItemById (node , itemId . getItemID () );
138165 }
139166
140167 private boolean canDelete () throws NodeStoreException {
@@ -156,8 +183,8 @@ private boolean userOwnsItem() {
156183 }
157184
158185 private boolean userManagesNode () throws NodeStoreException {
159- return channelManager .getNodeMembership (node ,
160- actor ). getAffiliation () .canAuthorize ();
186+ return channelManager .getNodeMembership (node , actor ). getAffiliation ()
187+ .canAuthorize ();
161188 }
162189
163190 private boolean validPayload () {
@@ -179,7 +206,7 @@ private boolean validPayload() {
179206 }
180207
181208 private boolean itemExists () throws NodeStoreException {
182- nodeItem = channelManager .getNodeItem (node , itemId );
209+ nodeItem = channelManager .getNodeItem (node , itemId . getItemID () );
183210 if (nodeItem != null ) {
184211 return true ;
185212 }
@@ -189,9 +216,16 @@ private boolean itemExists() throws NodeStoreException {
189216 }
190217
191218 private boolean itemIdProvided () {
192- itemId = request .getElement ().element ("pubsub" ).element ("retract" )
219+ String id = request .getElement ().element ("pubsub" ).element ("retract" )
193220 .element ("item" ).attributeValue ("id" );
194- if (itemId != null && !itemId .equals ("" )) {
221+
222+ if ((id != null ) && !id .isEmpty ()) {
223+ if (true == GlobalItemIDImpl .isGlobalId (id )) {
224+ itemId = GlobalItemIDImpl .fromBuddycloudString (id );
225+ } else {
226+ itemId = new GlobalItemIDImpl (new JID (this .getServerDomain ()),
227+ node , id );
228+ }
195229 return true ;
196230 }
197231 response .setType (IQ .Type .error );
@@ -235,17 +269,15 @@ private boolean validNodeProvided() {
235269 response .setChildElement (error );
236270 return false ;
237271 }
238-
272+
239273 private void makeRemoteRequest () throws InterruptedException {
240274 request .setTo (new JID (node .split ("/" )[2 ]).getDomain ());
241- request .getElement ()
242- .element ("pubsub" )
243- .addElement ("actor" , Buddycloud .NS )
244- .addText (actor .toBareJID ());
245- outQueue .put (request );
275+ request .getElement ().element ("pubsub" )
276+ .addElement ("actor" , Buddycloud .NS ).addText (actor .toBareJID ());
277+ outQueue .put (request );
246278 }
247279
248280 public boolean accept (Element elm ) {
249281 return elm .getName ().equals ("retract" );
250282 }
251- }
283+ }
0 commit comments