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