1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 01:02:23 -05:00

Be smarter with fetching history

This commit is contained in:
Lance Stout 2014-01-07 09:08:21 -08:00
parent 60abb1ec60
commit 5945858b4a

View File

@ -42,6 +42,7 @@ module.exports = HumanModel.define({
avatar: 'string', avatar: 'string',
avatarSource: 'string', avatarSource: 'string',
lastInteraction: 'date', lastInteraction: 'date',
lastHistoryFetch: 'date',
lastSentMessage: 'object', lastSentMessage: 'object',
lockedResource: 'string', lockedResource: 'string',
offlineStatus: ['string', false, ''], offlineStatus: ['string', false, ''],
@ -281,22 +282,33 @@ module.exports = HumanModel.define({
var self = this; var self = this;
app.whenConnected(function () { app.whenConnected(function () {
var filter = { var filter = {
'with': self.jid,
rsm: {
count: 20, count: 20,
before: true, before: true
}
}; };
var lastMessage = self.messages.last(); var lastMessage = self.messages.last();
if (lastMessage && lastMessage.archivedId) { if (lastMessage && lastMessage.archivedId) {
filter.after = lastMessage.archivedId; filter.rsm.after = lastMessage.archivedId;
} }
client.getHistory({ if (self.lastHistoryFetch && !isNaN(self.lastHistoryFetch.valueOf())) {
with: self.jid, if (self.lastInteraction > self.lastHistoryFetch) {
start: self.lastInteraction, filter.start = self.lastInteraction;
rsm: filter } else {
}, function (err, res) { filter.start = self.lastHistoryFetch;
}
} else {
filter.end = new Date(Date.now());
}
client.getHistory(filter, function (err, res) {
if (err) return; if (err) return;
self.lastHistoryFetch = new Date(Date.now());
var results = res.mamQuery.results || []; var results = res.mamQuery.results || [];
results.reverse(); results.reverse();
results.forEach(function (result) { results.forEach(function (result) {