The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/app/ui-utils/client/lib/RoomHistoryManager.js

292 lines
7.9 KiB

Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Blaze } from 'meteor/blaze';
import { UserRoles, RoomRoles, ChatMessage, ChatSubscription, ChatRoom } from '../../../models';
Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
import _ from 'underscore';
import { RoomManager } from './RoomManager';
import { readMessage } from './readMessages';
export const upsertMessage = ({ msg: { _id, ...msg }, subscription }) => {
Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
const userId = msg.u && msg.u._id;
if (subscription && subscription.ignored && subscription.ignored.indexOf(userId) > -1) {
msg.ignored = true;
}
const roles = [
(userId && UserRoles.findOne(userId, { fields: { roles: 1 } })) || {},
(userId && RoomRoles.findOne({ rid: msg.rid, 'u._id': userId })) || {},
].map((e) => e.roles);
msg.roles = _.union.apply(_.union, roles);
if (msg.t === 'e2e' && !msg.file) {
msg.e2e = 'pending';
}
return ChatMessage.upsert({ _id }, msg);
Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
};
function upsertMessageBulk({ msgs, subscription }) {
const { queries } = ChatMessage;
ChatMessage.queries = [];
msgs.forEach((msg, index) => {
if (index === msgs.length - 1) {
ChatMessage.queries = queries;
}
upsertMessage({ msg, subscription });
});
}
Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
export const RoomHistoryManager = new class {
constructor() {
this.defaultLimit = 50;
this.histories = {};
}
getRoom(rid) {
if ((this.histories[rid] == null)) {
this.histories[rid] = {
hasMore: new ReactiveVar(true),
hasMoreNext: new ReactiveVar(false),
isLoading: new ReactiveVar(false),
unreadNotLoaded: new ReactiveVar(0),
firstUnread: new ReactiveVar,
loaded: undefined,
};
}
return this.histories[rid];
}
getMore(rid, limit) {
let ts;
if (limit == null) { limit = this.defaultLimit; }
const room = this.getRoom(rid);
if (room.hasMore.curValue !== true) {
return;
}
room.isLoading.set(true);
// ScrollListener.setLoader true
const lastMessage = ChatMessage.findOne({ rid }, { sort: { ts: 1 } });
// lastMessage ?= ChatMessage.findOne({rid: rid}, {sort: {ts: 1}})
if (lastMessage != null) {
({ ts } = lastMessage);
} else {
ts = undefined;
}
let ls = undefined;
let typeName = undefined;
const subscription = ChatSubscription.findOne({ rid });
if (subscription != null) {
({ ls } = subscription);
typeName = subscription.t + subscription.name;
} else {
const curRoomDoc = ChatRoom.findOne({ _id: rid });
typeName = (curRoomDoc != null ? curRoomDoc.t : undefined) + (curRoomDoc != null ? curRoomDoc.name : undefined);
}
Meteor.call('loadHistory', rid, ts, limit, ls, function(err, result) {
if (err) {
return;
}
let previousHeight;
const { messages = [] } = result;
room.unreadNotLoaded.set(result.unreadNotLoaded);
room.firstUnread.set(result.firstUnread);
const wrapper = $('.messages-box .wrapper').get(0);
if (wrapper != null) {
previousHeight = wrapper.scrollHeight;
}
upsertMessageBulk({
msgs: messages.filter((msg) => msg.t !== 'command'),
subscription,
});
Convert rocketchat-ui-sidenav to main module structure (#13098) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Move some functions to rocketchat:ui-utils * Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils * Remove dependency between lazy-load and lib * Change imports of renderMessageBody from ui-message to ui-utils * Add import of main ready from ui-utils * Convert rocketchat-ui-sidenav to main module structure * Add imports of toolbarSearch from ui-sidenav * Remove toolbarSearch from eslintrc globals * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix import missed objects inside RocketChat namespace * Fix lint * Remove duplicated code inside rocketchat:lib * Remove unused file
7 years ago
if (wrapper) {
const heightDiff = wrapper.scrollHeight - previousHeight;
wrapper.scrollTop += heightDiff;
}
Meteor.defer(() => {
readMessage.refreshUnreadMark(rid, true);
return RoomManager.updateMentionsMarksOfRoom(typeName);
});
room.isLoading.set(false);
if (room.loaded == null) { room.loaded = 0; }
room.loaded += messages.length;
if (messages.length < limit) {
return room.hasMore.set(false);
}
});
}
getMoreNext(rid, limit) {
if (limit == null) { limit = this.defaultLimit; }
const room = this.getRoom(rid);
if (room.hasMoreNext.curValue !== true) {
return;
}
const instance = Blaze.getView($('.messages-box .wrapper')[0]).templateInstance();
instance.atBottom = false;
room.isLoading.set(true);
const lastMessage = ChatMessage.findOne({ rid }, { sort: { ts: -1 } });
let typeName = undefined;
const subscription = ChatSubscription.findOne({ rid });
if (subscription != null) {
// const { ls } = subscription;
typeName = subscription.t + subscription.name;
} else {
const curRoomDoc = ChatRoom.findOne({ _id: rid });
typeName = (curRoomDoc != null ? curRoomDoc.t : undefined) + (curRoomDoc != null ? curRoomDoc.name : undefined);
}
const { ts } = lastMessage;
if (ts) {
return Meteor.call('loadNextMessages', rid, ts, limit, function(err, result) {
for (const msg of Array.from((result != null ? result.messages : undefined) || [])) {
if (msg.t !== 'command') {
upsertMessage({ msg, subscription });
}
}
Meteor.defer(() => RoomManager.updateMentionsMarksOfRoom(typeName));
room.isLoading.set(false);
if (room.loaded == null) { room.loaded = 0; }
room.loaded += result.messages.length;
if (result.messages.length < limit) {
room.hasMoreNext.set(false);
}
});
}
}
getSurroundingMessages(message, limit) {
if (limit == null) { limit = this.defaultLimit; }
if (!(message != null ? message.rid : undefined)) {
return;
}
const instance = Blaze.getView($('.messages-box .wrapper')[0]).templateInstance();
if (ChatMessage.findOne(message._id)) {
const wrapper = $('.messages-box .wrapper');
const msgElement = $(`#${ message._id }`, wrapper);
if (msgElement.length === 0) {
return;
}
const pos = (wrapper.scrollTop() + msgElement.offset().top) - (wrapper.height() / 2);
wrapper.animate({
scrollTop: pos,
}, 500);
msgElement.addClass('highlight');
setTimeout(function() {
const messages = wrapper[0];
return instance.atBottom = messages.scrollTop >= (messages.scrollHeight - messages.clientHeight);
});
return setTimeout(() => msgElement.removeClass('highlight'), 500);
} else {
const room = this.getRoom(message.rid);
room.isLoading.set(true);
ChatMessage.remove({ rid: message.rid });
let typeName = undefined;
const subscription = ChatSubscription.findOne({ rid: message.rid });
if (subscription) {
// const { ls } = subscription;
typeName = subscription.t + subscription.name;
} else {
const curRoomDoc = ChatRoom.findOne({ _id: message.rid });
typeName = (curRoomDoc != null ? curRoomDoc.t : undefined) + (curRoomDoc != null ? curRoomDoc.name : undefined);
}
return Meteor.call('loadSurroundingMessages', message, limit, function(err, result) {
if (!result || !result.messages) {
return;
}
for (const msg of Array.from(result.messages)) {
if (msg.t !== 'command') {
upsertMessage({ msg, subscription });
}
}
Meteor.defer(function() {
readMessage.refreshUnreadMark(message.rid, true);
RoomManager.updateMentionsMarksOfRoom(typeName);
const wrapper = $('.messages-box .wrapper');
const msgElement = $(`#${ message._id }`, wrapper);
const pos = (wrapper.scrollTop() + msgElement.offset().top) - (wrapper.height() / 2);
wrapper.animate({
scrollTop: pos,
}, 500);
msgElement.addClass('highlight');
setTimeout(function() {
room.isLoading.set(false);
const messages = wrapper[0];
instance.atBottom = !result.moreAfter && (messages.scrollTop >= (messages.scrollHeight - messages.clientHeight));
return 500;
});
return setTimeout(() => msgElement.removeClass('highlight'), 500);
});
if (room.loaded == null) { room.loaded = 0; }
room.loaded += result.messages.length;
room.hasMore.set(result.moreBefore);
return room.hasMoreNext.set(result.moreAfter);
});
}
}
hasMore(rid) {
const room = this.getRoom(rid);
return room.hasMore.get();
}
hasMoreNext(rid) {
const room = this.getRoom(rid);
return room.hasMoreNext.get();
}
getMoreIfIsEmpty(rid) {
const room = this.getRoom(rid);
if (room.loaded === undefined) {
return this.getMore(rid);
}
}
isLoading(rid) {
const room = this.getRoom(rid);
return room.isLoading.get();
}
clear(rid) {
ChatMessage.remove({ rid });
if (this.histories[rid] != null) {
this.histories[rid].hasMore.set(true);
this.histories[rid].isLoading.set(false);
return this.histories[rid].loaded = undefined;
}
}
};