Browse Source

searchMessage

dong_dl 1 year ago
parent
commit
7b46f350db

+ 2 - 0
inc/proto/ProtoParser.h

@@ -198,6 +198,7 @@ public:
     std::string getMessage(std::string id);
     std::string getMessage(std::string id);
     std::string getGroupMembers(std::string id);
     std::string getGroupMembers(std::string id);
     std::string searchConversation(std::string keyword, std::string types, std::string lines);
     std::string searchConversation(std::string keyword, std::string types, std::string lines);
+    std::string searchMessage(std::string converstaion, std::string keyword);
 
 
     std::string getAllMessages() const;
     std::string getAllMessages() const;
     std::string getAllUsers() const;
     std::string getAllUsers() const;
@@ -412,6 +413,7 @@ EMSCRIPTEN_BINDINGS(Proto)
         .function("getMessage", &ProtoParser::getMessage)
         .function("getMessage", &ProtoParser::getMessage)
         .function("getGroupMembers", &ProtoParser::getGroupMembers)
         .function("getGroupMembers", &ProtoParser::getGroupMembers)
         .function("searchConversation", &ProtoParser::searchConversation)
         .function("searchConversation", &ProtoParser::searchConversation)
+        .function("searchMessage", &ProtoParser::searchMessage)
         .function("setOnConnected", &ProtoParser::setOnConnected)
         .function("setOnConnected", &ProtoParser::setOnConnected)
         .function("setOnMessage", &ProtoParser::setOnMessage)
         .function("setOnMessage", &ProtoParser::setOnMessage)
         .function("setOnNotify", &ProtoParser::setOnNotify)
         .function("setOnNotify", &ProtoParser::setOnNotify)

+ 19 - 0
src/proto/ProtoParser.cpp

@@ -402,6 +402,25 @@ std::string ProtoParser::pingPack()
     return str;
     return str;
 }
 }
 
 
+std::string ProtoParser::searchMessage(std::string converstaion, std::string keyword)
+{
+    rapidjson::StringBuffer                    buffer;
+    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+    writer.StartArray();
+    Conversation conv;
+    if (parseJson(converstaion, conv))
+    {
+        p->searchMessage(conv, keyword.c_str(), [&](const auto &item) {
+            toJson(writer, item, _uid.c_str());
+            return true;
+         });
+    }
+
+    writer.EndArray();
+    std::string ret = buffer.GetString();
+    return ret;
+}
+
 std::string ProtoParser::searchConversation(std::string keyword, std::string types, std::string lines)
 std::string ProtoParser::searchConversation(std::string keyword, std::string types, std::string lines)
 {
 {
     rapidjson::StringBuffer                    buffer;
     rapidjson::StringBuffer                    buffer;

+ 23 - 0
src/proto/ProtoParserPrivate.cpp

@@ -752,6 +752,29 @@ int ProtoParserPrivate::searchGroups(const char *keyword, std::function<bool(con
 #endif
 #endif
 }
 }
 
 
+int ProtoParserPrivate::searchMessage(const Conversation &conv, const char *keyword, std::function<bool(const Message &item)> cb)
+{
+    std::lock_guard<std::recursive_mutex> l(_lock);
+#ifdef USE_ALL_SQLITE3
+    // TODO:
+#else
+    for (auto &it : _messages)
+    {
+        if (it.conversation().target() == conv.target() && it.conversation().line() == conv.line() && it.conversation().type() == conv.type())
+        {
+            if (strstr(it.content().searchable_content().c_str(), keyword) != nullptr)
+            {
+                if (!cb(it))
+                {
+                    break;
+                }
+            }
+        }
+    }
+    return 0;
+#endif
+}
+
 int ProtoParserPrivate::getUnreadFriendRequestCount(const char *uid)
 int ProtoParserPrivate::getUnreadFriendRequestCount(const char *uid)
 {
 {
     std::lock_guard<std::recursive_mutex> l(_lock);
     std::lock_guard<std::recursive_mutex> l(_lock);

+ 1 - 0
src/proto/ProtoParserPrivate.h

@@ -22,6 +22,7 @@ public:
 
 
     int searchFriends(const char *keyword, std::function<bool(const Friend &item)> cb);
     int searchFriends(const char *keyword, std::function<bool(const Friend &item)> cb);
     int searchGroups(const char *keyword, std::function<bool(const GroupInfo &item)> cb);
     int searchGroups(const char *keyword, std::function<bool(const GroupInfo &item)> cb);
+    int searchMessage(const Conversation &conv, const char *keyword, std::function<bool(const Message &item)> cb);
 
 
     int getUnreadFriendRequestCount(const char *uid);
     int getUnreadFriendRequestCount(const char *uid);
 
 

+ 14 - 0
src/proto/jsonUtils.cpp

@@ -1035,6 +1035,20 @@ bool parseJson(std::string &str, PullMessageRequest &par)
     return true;
     return true;
 }
 }
 
 
+bool parseJson(std::string& str, Conversation& par)
+{
+    rapidjson::Document doc;
+    if (str.length() <= 0 || doc.Parse(str.c_str()).HasParseError())
+    {
+        MqttLog(LOG_ERROR, LOG_TYPE_DATA, "parse failed: %d", doc.GetParseError());
+        return false;
+    }
+    par.set_line(doc["line"].GetInt());
+    par.set_target(doc["target"].GetString());
+    par.set_type(doc["type"].GetInt());
+    return true;
+}
+
 bool parseJson(std::string &str, ModifyUserSettingReq &par)
 bool parseJson(std::string &str, ModifyUserSettingReq &par)
 {
 {
     rapidjson::Document doc;
     rapidjson::Document doc;

+ 1 - 0
src/proto/jsonUtils.h

@@ -56,6 +56,7 @@ bool parseJson(std::string &str, LoadRemoteMessages &par);
 bool parseJson(std::string &str, ModifyMyInfoRequest &par);
 bool parseJson(std::string &str, ModifyMyInfoRequest &par);
 bool parseJson(std::string &str, PullMessageRequest &par);
 bool parseJson(std::string &str, PullMessageRequest &par);
 bool parseJson(std::string &str, ModifyUserSettingReq &par);
 bool parseJson(std::string &str, ModifyUserSettingReq &par);
+bool parseJson(std::string &str, Conversation &par);
 void parseIntArray(const rapidjson::Value &json, std::set<int> &ret);
 void parseIntArray(const rapidjson::Value &json, std::set<int> &ret);
 void parseIntArray(const std::string &json, std::set<int> &ret);
 void parseIntArray(const std::string &json, std::set<int> &ret);
 void parseInt64Array(const rapidjson::Value &json, std::set<int64_t> &ret);
 void parseInt64Array(const rapidjson::Value &json, std::set<int64_t> &ret);