Hooks: Make sure that original task is properly detected

On modify event uses UUID of the original task being modified
to determine which line should be interpreted as modification
of the task.

This was achieved by searching by a substring in a specific JSON
format, which, consenquently, failed on JSON strings of other (valid)
formats.
This commit is contained in:
Tomas Babej 2015-01-21 07:59:27 +01:00 committed by Paul Beckingham
parent 5e90510530
commit 8683574b18
2 changed files with 27 additions and 2 deletions

View file

@ -317,7 +317,6 @@ void Hooks::onModify (const Task& before, std::vector <Task>& tasks)
{ {
// Prepare invariants. // Prepare invariants.
std::string beforeJSON = before.composeJSON (); std::string beforeJSON = before.composeJSON ();
std::string uuidPattern = "\"uuid\":\"" + before.get ("uuid") + "\"";
// Convert vector of tasks to a vector of strings. // Convert vector of tasks to a vector of strings.
std::vector <std::string> input; std::vector <std::string> input;
@ -345,7 +344,7 @@ void Hooks::onModify (const Task& before, std::vector <Task>& tasks)
{ {
if (status == 0) if (status == 0)
{ {
if (line->find (uuidPattern) != std::string::npos) if (JSONContainsUUID((*line), before.get("uuid")))
input[1] = *line; // [1] original' input[1] = *line; // [1] original'
else else
input.push_back (*line); // [n > 1] extras input.push_back (*line); // [n > 1] extras
@ -460,6 +459,31 @@ bool Hooks::isJSON (const std::string& input) const
return false; return false;
} }
////////////////////////////////////////////////////////////////////////////////
// This method assumes that input has already been validated with isJSON method
bool Hooks::JSONContainsUUID(const std::string& input, const std::string& uuid) const
{
bool foundUUID = false;
// Parse the whole thing.
json::object* root_obj = (json::object*) (json::parse (input));
// For each object element...
json_object_iter i;
for (i = root_obj->_data.begin ();
i != root_obj->_data.end ();
++i)
{
// If the attribute is a recognized column.
std::string type = Task::attributes[i->first];
if (type == "string" && i->first == "uuid" &&
json::decode (unquoteText (i->second->dump ())) == uuid)
foundUUID = true;
}
return foundUUID;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Hooks::callHookScript ( int Hooks::callHookScript (
const std::string& script, const std::string& script,

View file

@ -52,6 +52,7 @@ public:
private: private:
std::vector <std::string> scripts (const std::string&); std::vector <std::string> scripts (const std::string&);
bool isJSON (const std::string&) const; bool isJSON (const std::string&) const;
bool JSONContainsUUID (const std::string&, const std::string&) const;
int callHookScript (const std::string&, const std::vector <std::string>&, std::vector <std::string>&); int callHookScript (const std::string&, const std::vector <std::string>&, std::vector <std::string>&);
private: private: