From b62263595234de888fedd591bd5e70819b944bfa Mon Sep 17 00:00:00 2001 From: Simon Grim Date: Wed, 18 Nov 2015 16:59:19 +0500 Subject: [PATCH] rewrite processDmConversation() to avoid appending of double posts in DMs --- js/twister_directmsg.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/js/twister_directmsg.js b/js/twister_directmsg.js index cb3bf67..d71a793 100644 --- a/js/twister_directmsg.js +++ b/js/twister_directmsg.js @@ -78,24 +78,29 @@ function requestDmConversation(postboard, dm_screenname) { ); } -function processDmConversation(postboard, dm_screenname, dmData) { - var lastId = undefined; - - if (dmData[dm_screenname]) { - var dmList = dmData[dm_screenname]; - if (dmList.length) { - for (var i = 0; i < dmList.length; i++) { - var dmItem = postToElemDM(dmList[i], defaultScreenName, dm_screenname) - .attr('data-id', dmList[i].id) - .appendTo(postboard) +function processDmConversation(stream, peerAlias, posts) { + var streamItems = stream.children(); + var streamPostsIDs = []; + + for (var i = 0; i < streamItems.length; i++) { + streamPostsIDs.push(parseInt(streamItems.eq(i).attr('data-id'))); + } + + if (posts[peerAlias] && posts[peerAlias].length) { + for (var i = 0; i < posts[peerAlias].length; i++) { + if (streamPostsIDs.indexOf(posts[peerAlias][i].id) === -1) { + var lastPostID = posts[peerAlias][i].id; + postToElemDM(posts[peerAlias][i], defaultScreenName, peerAlias) + .attr('data-id', lastPostID) + .appendTo(stream) ; - lastId = dmList[i].id; + streamPostsIDs.push(lastPostID); } - $.MAL.dmChatListLoaded(postboard); } + $.MAL.dmChatListLoaded(stream); } - if (typeof lastId !== 'undefined') - resetNewDMsCountForUser(dm_screenname, lastId); + if (typeof lastPostID !== 'undefined') + resetNewDMsCountForUser(peerAlias, lastPostID); } function directMsgSubmit(e) {