Skip to content
This repository has been archived by the owner on Nov 12, 2018. It is now read-only.

WIP: 保存历史记录 #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Common.MENTION_MENU_HINT_TEXT = "选择回复的人:";
Common.MESSAGE_PREVENT_RECALL = "阻止了一次撤回";
Common.EMOJI_MAXIUM_SIZE = 120;

module.exports = Common;
module.exports = Common;
43 changes: 43 additions & 0 deletions src/inject/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Injector {
});
$rootScope.shareMenu = ShareMenu.inject;
$rootScope.mentionMenu = Injector.mentionMenu.inject;

setTimeout(() => {
console.log('watch currentUser');
angular.element('#chatArea').scope().$watch('currentUser', self.restoreChatContent.bind(self));
})
}]);
return angularBootstrapReal.apply(angular, arguments);
} : angularBootstrapReal,
Expand Down Expand Up @@ -102,6 +107,8 @@ class Injector {
Injector.lock(msg, 'MMDigest', Common.MESSAGE_PREVENT_RECALL);
break;
}

this.saveHistory(msg);
});
return value;
}
Expand All @@ -116,6 +123,42 @@ class Injector {
}
return value;
}

restoreChatContent(user) {
const scope = angular.element('#chatArea').scope();
if (!scope.chatContent || scope.chatContent.length === 0) {
const his = this.getHistory(user);
for (let i in his) {
his[i].MMUnread = false;
scope.chatContent.push(his[i]);
}
}
}

getHistory(user) {
let his = localStorage.getItem(user);
if (!his) {
return [];
}
return JSON.parse(his);
}

saveHistory(msg) {
if (!msg) return;

setTimeout(() => {
const user = msg.MMPeerUserName;
if (!user) return;

const his = this.getHistory(user);
his.push(msg);
if (his.length > 20) {
his = his.splice(his.length - 20);
}
console.log(msg);
localStorage.setItem(user, JSON.stringify(his));
});
}
}

new Injector().init();