From 4ffa30b533979a2214eb3996c19435f1cd16bc2a Mon Sep 17 00:00:00 2001
From: Merov Linden <merov@lindenlab.com>
Date: Fri, 9 May 2014 16:48:51 -0700
Subject: [PATCH] DD-22 : WIP : Implement deleteSLMListing() to cover the
 Delete /listing/:listing_id route. Avoid recursive calls in deleteListing()

---
 indra/newview/llmarketplacefunctions.cpp | 121 +++++++++++++++++++----
 indra/newview/llmarketplacefunctions.h   |   5 +-
 2 files changed, 107 insertions(+), 19 deletions(-)

diff --git a/indra/newview/llmarketplacefunctions.cpp b/indra/newview/llmarketplacefunctions.cpp
index a3cc3cd0b49..5aab7aa6d37 100755
--- a/indra/newview/llmarketplacefunctions.cpp
+++ b/indra/newview/llmarketplacefunctions.cpp
@@ -186,8 +186,12 @@ class LLSLMGetListingsResponder : public LLHTTPClient::Responder
             
             LLUUID folder_id(folder_uuid_string);
             LLUUID version_id(version_uuid_string);
-            LLMarketplaceData::instance().addListing(folder_id,listing_id,version_id,is_listed);
-            LLMarketplaceData::instance().setListingURL(folder_id, edit_url);
+            if (folder_id.notNull())
+            {
+                LLMarketplaceData::instance().deleteListing(folder_id,false);
+                LLMarketplaceData::instance().addListing(folder_id,listing_id,version_id,is_listed);
+                LLMarketplaceData::instance().setListingURL(folder_id, edit_url);
+            }
             it++;
         }
     }
@@ -260,17 +264,17 @@ class LLSLMUpdateListingsResponder : public LLHTTPClient::Responder
                               const LLChannelDescriptors& channels,
                               const LLIOPipe::buffer_ptr_t& buffer)
     {
-		if (!isGoodStatus(status))
-		{
-            log_SLM_error("Put listings", status, reason, "", "");
-            return;
-		}
-        
         LLBufferStream istr(channels, buffer.get());
         std::stringstream strstrm;
         strstrm << istr.rdbuf();
         const std::string body = strstrm.str();
         
+		if (!isGoodStatus(status))
+		{
+            log_SLM_error("Put listings", status, reason, "", body);
+            return;
+		}
+        
         Json::Value root;
         Json::Reader reader;
         if (!reader.parse(body,root))
@@ -279,7 +283,7 @@ class LLSLMUpdateListingsResponder : public LLHTTPClient::Responder
             return;
         }
         
-        llinfos << "Merov : Update listing completedRaw : data = " << body << llendl;
+        llinfos << "Merov : Update listing completedRaw : status = " << status << ", reason = " << reason << ", body = " << body << llendl;
 
         // Extract the info from the Json string
         Json::ValueIterator it = root["listings"].begin();
@@ -341,7 +345,7 @@ class LLSLMAssociateListingsResponder : public LLHTTPClient::Responder
             return;
         }
         
-        llinfos << "Merov : Update listing completedRaw : data = " << body << llendl;
+        llinfos << "Merov : Associate listing completedRaw : data = " << body << llendl;
         
         // Extract the info from the Json string
         Json::ValueIterator it = root["listings"].begin();
@@ -375,6 +379,65 @@ class LLSLMAssociateListingsResponder : public LLHTTPClient::Responder
     }
 };
 
+class LLSLMDeleteListingsResponder : public LLHTTPClient::Responder
+{
+	LOG_CLASS(LLSLMDeleteListingsResponder);
+public:
+	
+    LLSLMDeleteListingsResponder() {}
+    
+	virtual void completed(U32 status, const std::string& reason, const LLSD& content) { }
+    
+    virtual void completedRaw(U32 status,
+                              const std::string& reason,
+                              const LLChannelDescriptors& channels,
+                              const LLIOPipe::buffer_ptr_t& buffer)
+    {
+        LLBufferStream istr(channels, buffer.get());
+        std::stringstream strstrm;
+        strstrm << istr.rdbuf();
+        const std::string body = strstrm.str();
+        
+ 		if (!isGoodStatus(status))
+		{
+            log_SLM_error("Delete listings", status, reason, "", body);
+            return;
+		}
+        
+        Json::Value root;
+        Json::Reader reader;
+        if (!reader.parse(body,root))
+        {
+            log_SLM_error("Delete listings", status, "Json parsing failed", reader.getFormatedErrorMessages(), body);
+            return;
+        }
+        
+        llinfos << "Merov : Delete listing completedRaw : data = " << body << llendl;
+        
+        // Extract the info from the Json string
+        Json::ValueIterator it = root["listings"].begin();
+        
+        while (it != root["listings"].end())
+        {
+            Json::Value listing = *it;
+            
+            int listing_id = listing["id"].asInt();
+            std::string folder_uuid_string = listing["inventory_info"]["listing_folder_id"].asString();
+            
+            LLUUID folder_id(folder_uuid_string);
+            
+            // Check that the listing ID is associated to the correct folder
+            LLUUID old_listing = LLMarketplaceData::instance().getListingFolder(listing_id);
+            if (old_listing == folder_id)
+            {
+                LLMarketplaceData::instance().deleteListing(folder_id);
+            }
+            
+            it++;
+        }
+    }
+};
+
 // SLM Responders End
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -1006,6 +1069,18 @@ void LLMarketplaceData::associateSLMListing(const LLUUID& folder_id, S32 listing
 	LLHTTPClient::putRaw(url, data, size, new LLSLMAssociateListingsResponder(), headers);
 }
 
+void LLMarketplaceData::deleteSLMListing(S32 listing_id)
+{
+	LLSD headers = LLSD::emptyMap();
+	headers["Accept"] = "application/json";
+	headers["Content-Type"] = "application/json";
+        
+	// Send request
+    std::string url = getSLMConnectURL("/listing/") + llformat("%d",listing_id);
+    llinfos << "Merov : delete listing : " << url << llendl;
+	LLHTTPClient::del(url, new LLSLMDeleteListingsResponder(), headers);
+}
+
 std::string LLMarketplaceData::getSLMConnectURL(const std::string& route)
 {
     std::string url("");
@@ -1051,20 +1126,27 @@ bool LLMarketplaceData::createListing(const LLUUID& folder_id)
 
 bool LLMarketplaceData::clearListing(const LLUUID& folder_id)
 {
+    if (folder_id.isNull())
+    {
+        // Folder doesn't exists -> exit with error
+        return false;
+    }
+
     // Folder id can be the root of the listing or not so we need to retrieve the root first
     S32 depth = depth_nesting_in_marketplace(folder_id);
-    LLUUID listing_uuid = nested_parent_id(folder_id, depth);
+    LLUUID listing_uuid = (isListed(folder_id) ? folder_id : nested_parent_id(folder_id, depth));
     S32 listing_id = getListingID(listing_uuid);
+   
     if (listing_id == 0)
     {
         // Listing doesn't exists -> exit with error
         return false;
     }
     
-    // Update the SLM Server so that this listing is not active anymore
-    // *TODO : use deleteSLMListing()
-    deleteListing(listing_uuid);
-    updateSLMListing(listing_uuid, listing_id, LLUUID::null, false);
+    llinfos << "Merov : clearListing, folder id = " << folder_id << ", listing uuid = " << listing_uuid << ", listing id = " << listing_id << ", depth = " << depth << llendl;
+    
+    // Update the SLM Server so that this listing is deleted (actually, archived...)
+    deleteSLMListing(listing_id);
     
     return true;
 }
@@ -1139,7 +1221,7 @@ bool LLMarketplaceData::addListing(const LLUUID& folder_id, S32 listing_id, cons
     return true;
 }
 
-bool LLMarketplaceData::deleteListing(const LLUUID& folder_id)
+bool LLMarketplaceData::deleteListing(const LLUUID& folder_id, bool update_slm)
 {
     if (!isListed(folder_id))
     {
@@ -1148,8 +1230,11 @@ bool LLMarketplaceData::deleteListing(const LLUUID& folder_id)
     }
 	mMarketplaceItems.erase(folder_id);
 
-    update_marketplace_category(folder_id);
-    gInventory.notifyObservers();
+    if (update_slm)
+    {
+        update_marketplace_category(folder_id);
+        gInventory.notifyObservers();
+    }
     return true;
 }
 
diff --git a/indra/newview/llmarketplacefunctions.h b/indra/newview/llmarketplacefunctions.h
index c3f7693a675..de860bf9caa 100755
--- a/indra/newview/llmarketplacefunctions.h
+++ b/indra/newview/llmarketplacefunctions.h
@@ -162,6 +162,7 @@ class LLSLMGetListingsResponder;
 class LLSLMCreateListingsResponder;
 class LLSLMUpdateListingsResponder;
 class LLSLMAssociateListingsResponder;
+class LLSLMDeleteListingsResponder;
 
 class LLMarketplaceData
     : public LLSingleton<LLMarketplaceData>
@@ -172,6 +173,7 @@ class LLMarketplaceData
     friend class LLSLMCreateListingsResponder;
     friend class LLSLMUpdateListingsResponder;
     friend class LLSLMAssociateListingsResponder;
+    friend class LLSLMDeleteListingsResponder;
 
 	LLMarketplaceData();
     virtual ~LLMarketplaceData();
@@ -209,7 +211,7 @@ class LLMarketplaceData
     // Modify Marketplace data set  : each method returns true if the function succeeds, false if error
     // Used internally only by SLM Responders when data are received from the SLM Server
     bool addListing(const LLUUID& folder_id, S32 listing_id, const LLUUID& version_id, bool is_listed);
-    bool deleteListing(const LLUUID& folder_id);
+    bool deleteListing(const LLUUID& folder_id, bool update_slm = true);
     bool setListingID(const LLUUID& folder_id, S32 listing_id);
     bool setVersionFolderID(const LLUUID& folder_id, const LLUUID& version_id);
     bool setActivationState(const LLUUID& folder_id, bool activate);
@@ -220,6 +222,7 @@ class LLMarketplaceData
     void createSLMListing(const LLUUID& folder_id);
     void updateSLMListing(const LLUUID& folder_id, S32 listing_id, const LLUUID& version_id, bool is_listed);
     void associateSLMListing(const LLUUID& folder_id, S32 listing_id, const LLUUID& version_id);
+    void deleteSLMListing(S32 listing_id);
     std::string getSLMConnectURL(const std::string& route);
 
     // Handling Marketplace connection and inventory connection
-- 
GitLab