将消息数组拆分为对话

时间:2015-09-23 10:37:58

标签: javascript arrays angularjs lodash

我正在为网站构建评论/消息系统,我遇到了有关消息的问题。

当我去我网站上出售的物品时,我收到了物品上的所有信息。我被允许看到所有消息,因为该项目的人只能给我发消息。

问题是将消息分成对话,因为我不希望消息混合在一起。

每条消息都有一个发件人,其id指向用户,还有一个接收者,也带有id。

我尝试将其拆分为包含来自一位发件人的所有邮件的子阵列,但这不包括我已回复的邮件。

var messages = data.reduce(function (buckets, item) {
    if (!buckets[item._sender._id]) {
        buckets[item._sender._id] = [];
    }
    buckets[item._sender._id].push(item);
    return buckets;
}, {});

$scope.messagesBucket = _.values(messages);

如果我控制台记录messagesBucket,它看起来像这样

[Array[x], Array[y]] // each array is a conversation
  >0: Array[z] // each conversation is array of messages, but they don't include the ones i sent, which is my problem
  >1: Array[q]
...

所以我的问题是我不知道如何将所有消息分成对话。

使用lodash是一个奖励!

编辑(消息数据):

[
{
"_id": "5602858bb1f8f5ee05078ee0",
"_item": "5602858bb1f8f5ee05078ed9",
"content": "Det var billigt.",

"_sender": {
"_id": "56028589b1f8f5ee05078ec2",
"firstName": "Ole",
"lastName": "Wedel",
"image": "URL"
},

"_receiver": {
"_id": "56028589b1f8f5ee05078ec1",
"firstName": "Knud",
"lastName": "Larsen",
"image": "URL"
},
"__v": 0,
"timestamp": "2015-09-03T10:57:15.918Z"
},

{
"_id": "5602858bb1f8f5ee05078ee1",
"_item": "5602858bb1f8f5ee05078ed9",
"content": "Jeg vil lige tænke over det",

"_sender": {
"_id": "56028589b1f8f5ee05078ebe",
"firstName": "Oscar",
"lastName": "Andersen",
"image": "URL"
},

"_receiver": {
"_id": "56028589b1f8f5ee05078ec1",
"firstName": "Knud",
"lastName": "Larsen",
"image": "URL"
},
"__v": 0,
"timestamp": "2015-04-14T10:57:15.918Z"
},

{
"_id": "5602858bb1f8f5ee05078ee3",
"_item": "5602858bb1f8f5ee05078ed9",
"content": "Det går nok. Jeg har også andre jeg kan sælge til.",

"_sender": {
"_id": "56028589b1f8f5ee05078ec3",
"firstName": "Troels",
"lastName": "Sørensen",
"image": "URL"
},

"_receiver": {
"_id": "56028589b1f8f5ee05078ec1",
"firstName": "Knud",
"lastName": "Larsen",
"image": "URL"
},
"__v": 0,
"timestamp": "2014-11-12T11:57:15.918Z"
},

{
"_id": "5602858bb1f8f5ee05078eed",
"_item": "5602858bb1f8f5ee05078ed9",
"content": "Arh, jeg bliver nok nødt til at springe fra...",

"_sender": {
"_id": "56028589b1f8f5ee05078ec1",
"firstName": "Knud",
"lastName": "Larsen",
"image": "URL"
},

"_receiver": {
"_id": "56028589b1f8f5ee05078ec2",
"firstName": "Ole",
"lastName": "Wedel",
"image": "URL"
},
"__v": 0,
"timestamp": "2014-05-22T10:57:15.919Z"
}
]

2 个答案:

答案 0 :(得分:0)

因此,简而言之,您需要按每个唯一的发送方 - 接收方对对邮件进行分组。 “唯一对”表示A + B与B + A相同;在我的代码中,我对它们进行排序,以便该对始终采用相同的形式。

这样做:

function groupConversations(messages) {
    return _.groupBy(messages, function(msg) {
        return _.sortBy([msg._sender._id, msg._receiver._id]).join(' ');
    })
}

答案 1 :(得分:0)

感谢您的回复,我用一段时间后用以下代码解决了我的问题

function getItemMessages() {
            api.getItemMessages($scope.item._id)
                .success(function (data, status) {
                    if (status === 200) {
                        // push all messages into seperate conversations
                        var conversations = data.reduce(function (buckets, message) {
                            // if the message is from the creator, don't add it to it's own bucket
                            if (message._sender._id === $scope.user._id) return buckets;
                            if (!buckets[message._sender._id]) {
                                buckets[message._sender._id] = [];
                            }
                            buckets[message._sender._id].push(message);
                            return buckets;
                        }, {});

                        // find the messages by the owner, and push them into the conversation it belongs to
                        var keys = _.keys(conversations);
                        for (var i = 0; i < data.length; i++) {
                            if (data[i]._sender._id === $scope.user._id) {
                                var index = keys.indexOf(data[i]._receiver._id);
                                var key = keys[index];
                                var conversation = _.get(conversations, key);
                                conversation.push(data[i]);
                            }
                        }

                        $scope.messagesBucket = _.values(conversations);
                        if ($scope.messagesBucket.length > 0) {
                            $scope.changeCon($scope.messagesBucket[0]);
                        }
                    }
                });
        }