如何对每个用户的聊天消息进行分组?

用户名

我使用Vue.js构建了群聊消息。我目前正在获取返回如下数组的消息:

"data":[
    {
        "id":1,
        "message":"<p>yo<\/p>",
        "removed":"false",
        "user":{
            "uid":2,
            "metadata":{
                "username":"Testing"
            }
        },
        "post_date":"2018-02-24 14:30"
    },
    {
        "id":2,
        "message":"<p>test<\/p>",
        "removed":"false",
        "user":{
            "uid":1,
            "metadata":{
                "username":"Admin"
            }
        },
        "post_date":"2018-02-24 22:31"
    },
    {
        "id":3,
        "message":"<p>Wassup?<\/p>",
        "removed":"false",
        "user":{
            "uid":1,
            "metadata":{
                "username":"Admin"
            }
        },
        "post_date":"2018-02-24 22:40"
    },
    {
        "id":12,
        "message":"again for testing post date",
        "removed":"false",
        "user":{
            "uid":1,
            "metadata":{
                "username":"Admin"
            }
        },
        "post_date":"2018-03-04 00:59"
    },
    {
        "id":13,
        "message":"Hello!",
        "removed":"false",
        "user":{
            "uid":2,
            "metadata":{
                "username":"Testing"
            }
        },
        "post_date":"2018-03-04 11:13"
    },
    {
        "id":13,
        "message":"<p>Hi!</p>",
        "removed":"false",
        "user":{
            "uid":2,
            "metadata":{
                "username":"Testing"
            }
        },
        "post_date":"2018-03-04 11:13"
    },
],

目前,我只是遍历数据并将每个消息输出到单独的div我更希望在同一用户连续发布多个消息时对消息进行分组。

我将如何处理?应该是选项服务器端(也许是可选group参数?)还是客户端完成?

编辑这是聊天电流的样子:聊天当前的外观

这是所需的外观: 所需的聊天外观

The problem if I group them by the UID/Username, is that the messages need to be output in order. So if User 1 send three messages, then User 2 send two, then User 1 sends another message, all of user 1s message will be grouped together. Rather the User 1s three messages should be grouped, then User 2s, then it should show User 1s last message.

Guru Prasad

I solved this problem for myself pretty recently. Here's a full example.

The core business logic for grouping messages, in the above example, can be found under src/store.js in the addMessage function.

除非您的服务器存储了所有消息,并且您没有某种机制可以确保所有客户端之间的因果顺序,否则我建议您在每个客户端上运行逻辑。这样,您可以确保客户在任何情况下都不会看到邮件跳来跳去。最坏的情况是,邮件将显示为未分组。

确定此消息的算法在收到新消息时运行,因此它在每个客户端上运行,但是您也可以对其进行调整以使其适合您的用例!如下所示(我可能使用了错误的流程图元素..歉意)。

addMessage({ state }, { msg, selfHash }) {
  let addAsNewMsg = true;
  const lastMessage = state.messages.slice(-1)[0];
  if (lastMessage && lastMessage.userHash === msg.userHash) {
    // The last message was also sent by the same user
    // Check if it arrived within the grouping threshold duration (60s)
    const lastMessageTime = moment(lastMessage.time);
    const msgTime = moment(msg.time);
    const diffSeconds = msgTime.diff(lastMessageTime, "seconds");
    if (diffSeconds <= 60) {
      addAsNewMsg = false; // We're going to be appending.. so
      lastMessage.message.push(msg.message);
      // We're going to now update the timestamp and (any) other fields.
      delete msg.message; // Since, we've already added this above
      Object.assign(lastMessage, msg); // Update with any remaining properties
    }
  }
  if (addAsNewMsg) {
    state.messages.push(msg);
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在聊天中对消息进行分组,而不是将每个新消息都放在新部分中?

如何按日期(节)对UITableView(聊天气泡)中的消息进行分组?

如何捕获在聊天中发送消息的用户

如何在用户对话中显示每个用户的最新消息以保持聊天记录?

如何对用户进行分组以向组中的每个用户授予相似的文件权限

如何添加Slack消息按钮以打开与用户的直接聊天?

在Powershell中,如何对每个用户“分组”文件?

如何按日期对消息进行分组?

Ajax用户键入聊天消息

如何对每个用户进行速率限制?

TwitchIO:如何发送聊天消息?

在用户聊天活动中组织消息

转义用户生成的聊天消息,但呈现链接

查询聊天中用户的未读消息

如何基于标题值对消息进行“分组”

如何按父母对“每个做”的输出进行分组?

SQL:如何在分组后对每个 VARCHAR 列进行合并?

我如何使聊天在用户发送消息时自动更新?

如何使用Telethon获取转发消息的频道/聊天/用户名?

如何使用Smack在多用户聊天消息中检索发件人姓名或其JID?

如何在node.js中跟踪用户对特定聊天机器人消息的回复

如何统计每个用户一个频道的消息数?

如何获取每个用户 Mongoose 的未读消息数

SQL - 如何选择所有聊天并按最近的消息对其进行排序

如何与组织外部的用户聊天

MYSQL在一条语句中进行多对多选择(聊天,最新消息,用户名)

如何使用smack 4.1.8+在同一侦听器上侦听单用户聊天和多用户聊天的传入消息

如何对用户输入的字符串进行分组

如何使用用户定义的功能进行分组?