#include "contact/contact_manager.h"
#include <memory>
+#include <set>
#include "common/converter.h"
#include "common/picojson.h"
void ContactManagerListenerCallback(const char* view_uri, char* changes,
void* user_data) {
+ LoggerD("ContactManagerListenerCallback");
+
(void)view_uri;
if (nullptr == changes) {
return;
}
+ SLoggerD("view_uri: %s", view_uri);
+ SLoggerD("changes: %s", changes);
+
JsonValue result{JsonObject{}};
JsonObject& result_obj = result.get<JsonObject>();
result_obj.insert(std::make_pair(std::string("listenerId"),
std::unique_ptr<char, void (*)(char*)> tmp(strdup(changes),
[](char* p) { free(p); });
+ // 'changes' may contain repeated values, we need to filter it
+ std::set<int> added_ids;
+ std::set<int> updated_ids;
+ std::set<int> removed_ids;
+
char* token = strtok(tmp.get(), kTokenDelimiter);
while (token) {
if (IsNumeric(token)) {
int person_id = atoi(token);
switch (type) {
case CONTACTS_CHANGE_INSERTED: {
- added.push_back(JsonValue{JsonObject{}});
- PlatformResult status = ContactManagerGetInternal(
- person_id, &added.back().get<JsonObject>());
- if (status.IsError()) {
- LoggerE("Caught exception in listener callback: %s",
- status.message().c_str());
- return;
+ if (added_ids.find(person_id) == added_ids.end()) {
+ added.push_back(JsonValue{JsonObject{}});
+ PlatformResult status = ContactManagerGetInternal(
+ person_id, &added.back().get<JsonObject>());
+ if (status.IsError()) {
+ LoggerE("Caught exception in listener callback: %s",
+ status.message().c_str());
+ return;
+ }
+ added_ids.insert(person_id);
}
break;
}
case CONTACTS_CHANGE_UPDATED: {
- updated.push_back(JsonValue{JsonObject{}});
- PlatformResult status = ContactManagerGetInternal(
- person_id, &updated.back().get<JsonObject>());
- if (status.IsError()) {
- LoggerE("Caught exception in listener callback: %s",
- status.message().c_str());
- return;
+ if (updated_ids.find(person_id) == updated_ids.end()) {
+ updated.push_back(JsonValue{JsonObject{}});
+ PlatformResult status = ContactManagerGetInternal(
+ person_id, &updated.back().get<JsonObject>());
+ if (status.IsError()) {
+ LoggerE("Caught exception in listener callback: %s",
+ status.message().c_str());
+ return;
+ }
+ updated_ids.insert(person_id);
}
break;
}
case CONTACTS_CHANGE_DELETED: {
- std::string id_str{std::to_string(person_id)};
- removed.push_back(JsonValue{id_str.c_str()});
+ if (removed_ids.find(person_id) == removed_ids.end()) {
+ removed.push_back(JsonValue{std::to_string(person_id)});
+ removed_ids.insert(person_id);
+ }
+ break;
+ }
+ default: {
break;
}
- default: {}
}
}
}