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/client/components/header/headerRoom.js

184 lines
5.3 KiB

import toastr from 'toastr';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { t, roomTypes, handleError } from '../../../../utils';
import { TabBar, fireGlobalEvent, call } from '../../../../ui-utils';
import { ChatSubscription, Rooms, ChatRoom } from '../../../../models';
import { settings } from '../../../../settings';
import { emoji } from '../../../../emoji';
import { Markdown } from '../../../../markdown/client';
import { hasAllPermission } from '../../../../authorization';
const isSubscribed = (_id) => ChatSubscription.find({ rid: _id }).count() > 0;
const favoritesEnabled = () => settings.get('Favorite_Rooms');
const isDiscussion = ({ _id }) => {
const room = ChatRoom.findOne({ _id });
return !!(room && room.prid);
};
Template.headerRoom.helpers({
back() {
return Template.instance().data.back;
},
avatarBackground() {
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData) { return ''; }
return roomTypes.getSecondaryRoomName(roomData.t, roomData) || roomTypes.getRoomName(roomData.t, roomData);
},
buttons() {
return TabBar.getButtons();
},
isDiscussion() {
return isDiscussion(Template.instance().data);
},
isTranslated() {
const sub = ChatSubscription.findOne({ rid: this._id }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1 } });
return settings.get('AutoTranslate_Enabled') && ((sub != null ? sub.autoTranslate : undefined) === true) && (sub.autoTranslateLanguage != null);
},
state() {
const sub = ChatSubscription.findOne({ rid: this._id }, { fields: { f: 1 } });
if (((sub != null ? sub.f : undefined) != null) && sub.f && favoritesEnabled()) { return ' favorite-room'; }
return 'empty';
},
favoriteLabel() {
const sub = ChatSubscription.findOne({ rid: this._id }, { fields: { f: 1 } });
if (((sub != null ? sub.f : undefined) != null) && sub.f && favoritesEnabled()) { return 'Unfavorite'; }
return 'Favorite';
},
isDirect() {
return Rooms.findOne(this._id).t === 'd';
},
roomName() {
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData) { return ''; }
return roomTypes.getRoomName(roomData.t, roomData);
},
secondaryName() {
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData) { return ''; }
return roomTypes.getSecondaryRoomName(roomData.t, roomData);
},
roomTopic() {
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData || !roomData.topic) { return ''; }
let roomTopic = Markdown.parse(roomData.topic.replace(/\n/mg, ' '));
// ' to apostrophe (') for emojis such as :')
roomTopic = roomTopic.replace(/'/g, '\'');
roomTopic = Object.keys(emoji.packages).reduce((topic, emojiPackage) => emoji.packages[emojiPackage].render(topic), roomTopic);
// apostrophe (') back to '
return roomTopic.replace(/\'/g, ''');
},
roomIcon() {
const roomData = Session.get(`roomData${ this._id }`);
if (!(roomData != null ? roomData.t : undefined)) { return ''; }
return roomTypes.getIcon(roomData);
},
tokenAccessChannel() {
return Template.instance().hasTokenpass.get();
},
encryptionState() {
const room = ChatRoom.findOne(this._id);
return (room && room.encrypted) && 'encrypted';
},
userStatus() {
const roomData = Session.get(`roomData${ this._id }`);
return roomTypes.getUserStatus(roomData.t, this._id) || t('offline');
},
showToggleFavorite() {
return !isDiscussion(Template.instance().data) && isSubscribed(this._id) && favoritesEnabled();
},
fixedHeight() {
return Template.instance().data.fixedHeight;
},
fullpage() {
return Template.instance().data.fullpage;
},
isChannel() {
return Template.instance().currentChannel != null;
},
isSection() {
return Template.instance().data.sectionName != null;
},
});
Template.headerRoom.events({
'click .iframe-toolbar .js-iframe-action'(e) {
fireGlobalEvent('click-toolbar-button', { id: this.id });
e.currentTarget.querySelector('button').blur();
return false;
},
'click .rc-header__toggle-favorite'(event) {
event.stopPropagation();
event.preventDefault();
return Meteor.call(
'toggleFavorite',
this._id,
!$(event.currentTarget).hasClass('favorite-room'),
(err) => err && handleError(err)
);
},
'click .js-open-parent-channel'(event, t) {
event.preventDefault();
const { prid } = t.currentChannel;
FlowRouter.goToRoomById(prid);
},
'click .js-toggle-encryption'(event) {
event.stopPropagation();
event.preventDefault();
const room = ChatRoom.findOne(this._id);
if (hasAllPermission('edit-room', this._id)) {
call('saveRoomSettings', this._id, 'encrypted', !(room && room.encrypted)).then(() => {
toastr.success(
t('Encrypted_setting_changed_successfully')
);
});
}
},
});
Template.headerRoom.onCreated(function() {
this.currentChannel = (this.data && this.data._id && Rooms.findOne(this.data._id)) || undefined;
this.hasTokenpass = new ReactiveVar(false);
if (settings.get('API_Tokenpass_URL') !== '') {
Meteor.call('getChannelTokenpass', this.data._id, (error, result) => {
if (!error) {
this.hasTokenpass.set(!!(result && result.tokens && result.tokens.length > 0));
}
});
}
});