|
|
|
@ -1,16 +1,23 @@ |
|
|
|
|
import _ from 'underscore'; |
|
|
|
|
import { Meteor } from 'meteor/meteor'; |
|
|
|
|
import { ReactiveVar } from 'meteor/reactive-var'; |
|
|
|
|
import { Template } from 'meteor/templating'; |
|
|
|
|
import { Mongo } from 'meteor/mongo'; |
|
|
|
|
|
|
|
|
|
import { StarredMessage } from '../lib/StarredMessage'; |
|
|
|
|
import { messageContext } from '../../../ui-utils/client/lib/messageContext'; |
|
|
|
|
import { Messages } from '../../../models/client'; |
|
|
|
|
import { upsertMessageBulk } from '../../../ui-utils/client/lib/RoomHistoryManager'; |
|
|
|
|
import { APIClient } from '../../../utils/client'; |
|
|
|
|
|
|
|
|
|
const LIMIT_DEFAULT = 50; |
|
|
|
|
|
|
|
|
|
Template.starredMessages.helpers({ |
|
|
|
|
hasMessages() { |
|
|
|
|
return Template.instance().cursor.count() > 0; |
|
|
|
|
return Template.instance().messages.find().count(); |
|
|
|
|
}, |
|
|
|
|
messages() { |
|
|
|
|
return Template.instance().cursor; |
|
|
|
|
const instance = Template.instance(); |
|
|
|
|
return instance.messages.find({}, { limit: instance.limit.get(), sort: { ts: -1 } }); |
|
|
|
|
}, |
|
|
|
|
message() { |
|
|
|
|
return _.extend(this, { actionContext: 'starred' }); |
|
|
|
@ -23,24 +30,49 @@ Template.starredMessages.helpers({ |
|
|
|
|
|
|
|
|
|
Template.starredMessages.onCreated(function() { |
|
|
|
|
this.rid = this.data.rid; |
|
|
|
|
|
|
|
|
|
this.cursor = StarredMessage.find({ |
|
|
|
|
rid: this.data.rid, |
|
|
|
|
}, { |
|
|
|
|
sort: { |
|
|
|
|
ts: -1, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
this.messages = new Mongo.Collection(null); |
|
|
|
|
this.hasMore = new ReactiveVar(true); |
|
|
|
|
this.limit = new ReactiveVar(50); |
|
|
|
|
this.limit = new ReactiveVar(LIMIT_DEFAULT); |
|
|
|
|
|
|
|
|
|
this.autorun(() => { |
|
|
|
|
const sub = this.subscribe('starredMessages', this.data.rid, this.limit.get()); |
|
|
|
|
if (sub.ready()) { |
|
|
|
|
if (this.cursor.count() < this.limit.get()) { |
|
|
|
|
return this.hasMore.set(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
const query = { |
|
|
|
|
_hidden: { $ne: true }, |
|
|
|
|
'starred._id': Meteor.userId(), |
|
|
|
|
rid: this.rid, |
|
|
|
|
_updatedAt: { |
|
|
|
|
$gt: new Date(), |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
this.cursor && this.cursor.stop(); |
|
|
|
|
|
|
|
|
|
this.limit.set(LIMIT_DEFAULT); |
|
|
|
|
|
|
|
|
|
this.cursor = Messages.find(query).observe({ |
|
|
|
|
added: ({ _id, ...message }) => { |
|
|
|
|
this.messages.upsert({ _id }, message); |
|
|
|
|
}, |
|
|
|
|
changed: ({ _id, ...message }) => { |
|
|
|
|
this.messages.upsert({ _id }, message); |
|
|
|
|
}, |
|
|
|
|
removed: ({ _id }) => { |
|
|
|
|
this.messages.remove({ _id }); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.autorun(async () => { |
|
|
|
|
const limit = this.limit.get(); |
|
|
|
|
const { messages, total } = await APIClient.v1.get(`chat.getStarredMessages?roomId=${ this.rid }&count=${ limit }`); |
|
|
|
|
|
|
|
|
|
upsertMessageBulk({ msgs: messages }, this.messages); |
|
|
|
|
|
|
|
|
|
this.hasMore.set(total > limit); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
Template.mentionsFlexTab.onDestroyed(function() { |
|
|
|
|
this.cursor.stop(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
Template.starredMessages.events({ |
|
|
|
|