Skip to content

Commit

Permalink
save && restore chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed Jun 10, 2016
1 parent f289ba4 commit 594693e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"electron-packager": "^7.0.0",
"electron-prebuilt": "^1.1.0",
"localforage": "1.4.2",
"pinyin": "^2.7.1"
}
}
17 changes: 17 additions & 0 deletions src/handlers/history_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const localforage = require('localforage');

class HistoryManager {
saveHistory(peerUserName, oMessage) {
localforage.getItem(peerUserName).then(function(/*Array*/history){
history = history || [];
history.push(oMessage);
localforage.setItem(peerUserName, history);
});
}

getHistory(peerUserName) {
return localforage.getItem(peerUserName);
}
}

module.exports = HistoryManager;
50 changes: 50 additions & 0 deletions src/inject/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ShareMenu = require('./share_menu');
const MentionMenu = require('./mention_menu');
const BadgeCount = require('./badge_count');
const Common = require("../common");
const HistoryManager = require('../handlers/history_manager');


class Injector {
Expand All @@ -17,6 +18,8 @@ class Injector {
webFrame.setZoomLevelLimits(1, 1);

new MenuHandler().create();

this.historyManager = new HistoryManager();
}

initAngularInjection() {
Expand All @@ -42,13 +45,60 @@ class Injector {
});
$rootScope.shareMenu = ShareMenu.inject;
$rootScope.mentionMenu = MentionMenu.inject;

$rootScope.$on('root:pageInit:success', function(){
$rootScope.$on("message:add:success", function(e, oMessage){
var oMessage2 = angular.copy(oMessage);
oMessage2.MMActualSender = self.getStableUserId(oMessage.MMActualSender);
oMessage2.MMPeerUserName = self.getStableUserId(oMessage.MMPeerUserName);
self.historyManager.saveHistory(self.getStableUserId(oMessage.MMPeerUserName), oMessage2);
});

$(document).on('click', '.chat_list .chat_item', function(e){
self.restoreChatHistory($(e.target).scope().chatContact.UserName);
});
});

}]);
return angularBootstrapReal.apply(angular, arguments);
} : angularBootstrapReal,
set: (real) => (angularBootstrapReal = real)
});
}

getStableUserId(userName){
const contact = window._contacts[userName];
return `${contact.NickName}_&&_${contact.RemarkName}`;
}

getActualSender(stableUserId){
for (let userName in window._contacts) {
let contact = window._contacts[userName];
if(`${contact.NickName}_&&_${contact.RemarkName}` === stableUserId){
return contact.UserName
}
}
}

restoreChatHistory(userName) {
if(!userName){
return;
}
let self = this;
const scope = angular.element('#chatArea').scope();
if (!scope.chatContent || scope.chatContent.length === 0) {
self.historyManager.getHistory(self.getStableUserId(userName)).then(function(history){
history = history || [];
history.forEach(function(oMessage){
oMessage.MMStatus = 0;
oMessage.MMActualSender = self.getActualSender(oMessage.MMActualSender);
oMessage.MMPeerUserName = self.getActualSender(oMessage.MMPeerUserName);
});
scope.chatContent = history;
});
}
}

initInjectBundle() {
let initModules = ()=> {
if (!window.$) {
Expand Down

10 comments on commit 594693e

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

保存图片类型的消息时有问题,debug显示是localforage这个lib有bug,准备替换掉它,考虑使用 leveldb 做存储。

@eleveni386
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有一个bug. 我在发送信息之后. 聊天窗口上没立刻显示出我发送得信息. 在旁边得联系人窗口上有信息显示. 我需要切换联系人. 然后再切换回来. 这时我得聊天窗口才显示出来我发送得信息.

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,已知bug,我还没有搞清楚原因。

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restoreChatHistory这里的逻辑也不完善,没有处理scope.chatContent.length > 0 同时存在历史记录的情况。应该merge这2个Array。

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从群里单挑一个人聊天时,也会出现问题。scope 被重置了,需要更新引用。

@dongyuwei
Copy link
Owner Author

@dongyuwei dongyuwei commented on 594693e Jun 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eleveni386 上班路上想明白了,秘密在 window._chatContent 里。web微信的所有消息都存储在此。今晚回去有时间再修正一下。

@eleveni386
Copy link

@eleveni386 eleveni386 commented on 594693e Jun 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongyuwei 太好了. 期待能修复. 另外, 我发现我在npm run build:linux之后, 执行electronic-wechat二进制文件会一直卡在splash, 和我没安装localforage依赖时得情景一样. 不知道是怎么回事. 而我直接在工程目录下执行 npm start, 就能很快得打开main windows, 跳过splash.
你微信多少? 方便交流一下.:D

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eleveni386 你打开DevTools看看Console 日志有什么异常吗?

@dongyuwei
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eleveni386 我的最新commit已经解决了上面所描述的几个bug,欢迎试用。我的微信id 154136125, 欢迎来交流 :)

@eleveni386
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongyuwei 实在太棒了. 微信已加你, 确认一下吧.

Please sign in to comment.