无法使用 Strope.js 和 Ejabberd 以及 XEP-0313 检索聊天历史记录:消息存档管理 (MAM)

问题描述 投票:0回答:1

我目前正在开发一个基于 Web 的聊天应用程序,使用 Strope.js 作为 XMPP 客户端库,使用 Ejabberd 作为 XMPP 服务器。我正在尝试使用 XEP-0313:消息存档管理 (MAM) 实现对检索聊天历史记录的支持,但我遇到了困难。

我的目标是获取特定用户的聊天记录并将其显示在聊天界面中。但是,我当前的实现似乎只返回消息计数,而不是实际的消息内容。

这是我用于检索聊天历史记录的代码片段:

const iq = $iq({
  type: 'set',
  id: 'juliet1',
}).c('query', {
  xmlns: 'urn:xmpp:mam:2'
}).c('x', {
  xmlns: 'jabber:x:data',
  type: 'submit'
}).c('field', {
  var: 'FORM_TYPE',
  type: 'hidden'
}).c('value').t('urn:xmpp:mam:2').up().up().c('field', {
  var: 'with'
}).c('value').t(jid).up().tree();

connection.sendIQ(iq, function(response) {
   const message = response.getElementsByTagName('result');
   console.log(response);
   messages.forEach(message => {
      const body = Strophe.getText(message.querySelector("body"));
      const from = message.getAttribute('from');
      displayMessage(from, body);
   });
   return true;
});

我面临的问题是,response.getElementsByTagName('result') 不会返回任何消息,当控制台记录响应时,我只得到 -

<iq xmlns="jabber:client" xml:lang="en" to="dev@localhost/1178459894898988032171235" from="dev@localhost" type="result" id="juliet1">
  <fin xmlns="urn:xmpp:mam:2" complete="true">
    <set xmlns="http://jabber.org/protocol/rsm">
      <count>1</count>
      <first>1707854878326932</first>
      <last>1707854878326932</last>
    </set>
  </fin>
</iq>

我仔细检查了我的 XMPP 服务器配置,它似乎可以正确支持 MAM。

我想知道我的代码是否有问题,或者是否有更好的方法来使用 Strope.js 和 Ejabberd 检索聊天历史记录。任何见解或建议将不胜感激。

Strope.js 版本:1.6.0

Ejabberd 版本:23.10.0

ejabberd.yml

mod_mam:
  ## Mnesia is limited to 2GB, better to use an SQL backend
  ## For small servers SQLite is a good fit and is very easy
  ## to configure. Uncomment this when you have SQL configured:
  ## db_type: sql
  assume_mam_usage: true
  default: always

提前感谢您的帮助!

注意: - 我已经在 Gajim XML 控制台中测试了查询,它返回了消息。

javascript xmpp ejabberd strophe strophe.js
1个回答
0
投票

我最初缺乏检索聊天记录的知识。然而,我后来发现了一些插件,可以让我从 MAM 检索聊天消息。

我创建了一个函数,通过传递特定用户的 JID 来检索消息

get_chat_history: function (jid) {
    const q = {
      onMessage: function (message) {
          let msg = $(message).find("forwarded > message");
          let body = msg.find("body").text();        
          return true;
      },
      onComplete: function (response) {
          scroll_chat();
      },
      with: jid,
      max: "100",
      before: "",
    };
    
    connection.mam.query(Strophe.getBareJidFromJid(connection.jid), q);
}

在上面的函数中

  • onMessage 检索消息的历史记录。
  • 当所有消息都已获取时,会调用
  • onComplete。允许进行任何必要的操作(在我的例子中滚动到底部)

用于实现此目的的插件是:

尽管不使用插件也可以实现相同的效果:

get_chat_history: function (jid) {
    const id = connection.getUniqueId();
    let mamQuery = $iq({ type: "set", id: id })
      .c("query", {
        xmlns: "urn:xmpp:mam:2",
      })
      .c("x", { xmlns: "jabber:x:data", type: "submit" });
    mamQuery
      .c("field", { var: "FORM_TYPE", type: "hidden" })
      .c("value")
      .t("urn:xmpp:mam:2")
      .up()
      .up();
    mamQuery.c("field", { var: "with" }).c("value").t(jid).up().up();

    mamQuery
      .up()
      .c("set", { xmlns: "http://jabber.org/protocol/rsm" })
      .c("max", {}, "100")
      .c("before");

    connection.sendIQ(mamQuery);
}, 

添加处理程序以处理历史消息的检索

connection.addHandler(
    function (iq) {
        // iq will have the messages  
    },
    "urn:xmpp:mam:2",
    "message",
    null
);
© www.soinside.com 2019 - 2024. All rights reserved.