Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some dependencies inside rocketchat-lib/client #13210

Merged
merged 4 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions packages/rocketchat-lib/client/CustomTranslations.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { TAPi18n } from 'meteor/tap:i18n';
import { TAPi18next } from 'meteor/tap:i18n';
import { applyCustomTranslations } from 'meteor/rocketchat:utils';
import { Tracker } from 'meteor/tracker';

RocketChat.applyCustomTranslations = function applyCustomTranslations() {
let CustomTranslations = RocketChat.settings.get('Custom_Translations');
if (typeof CustomTranslations === 'string' && CustomTranslations.trim() !== '') {
try {
CustomTranslations = JSON.parse(CustomTranslations);

for (const lang in CustomTranslations) {
if (CustomTranslations.hasOwnProperty(lang)) {
const translations = CustomTranslations[lang];
TAPi18next.addResourceBundle(lang, 'project', translations);
}
}
TAPi18n._language_changed_tracker.changed();
} catch (e) {
console.error('Invalid setting Custom_Translations', e);
}
}
};
RocketChat.applyCustomTranslations = applyCustomTranslations;

Meteor.startup(function() {
Tracker.autorun(function() {
// Re apply translations if tap language was changed
Session.get(TAPi18n._loaded_lang_session_key);
RocketChat.applyCustomTranslations();
applyCustomTranslations();
});
});
7 changes: 4 additions & 3 deletions packages/rocketchat-lib/client/OAuthProxy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import _ from 'underscore';
import { OAuth } from 'meteor/oauth';
import { settings } from 'meteor/rocketchat:settings';

OAuth.launchLogin = _.wrap(OAuth.launchLogin, function(func, options) {
const proxy = RocketChat.settings.get('Accounts_OAuth_Proxy_services').replace(/\s/g, '').split(',');
const proxy = settings.get('Accounts_OAuth_Proxy_services').replace(/\s/g, '').split(',');
if (proxy.includes(options.loginService)) {
const redirect_uri = options.loginUrl.match(/(&redirect_uri=)([^&]+|$)/)[2];
options.loginUrl = options.loginUrl.replace(/(&redirect_uri=)([^&]+|$)/, `$1${ encodeURIComponent(RocketChat.settings.get('Accounts_OAuth_Proxy_host')) }/oauth_redirect`);
options.loginUrl = options.loginUrl.replace(/(&redirect_uri=)([^&]+|$)/, `$1${ encodeURIComponent(settings.get('Accounts_OAuth_Proxy_host')) }/oauth_redirect`);
options.loginUrl = options.loginUrl.replace(/(&state=)([^&]+|$)/, `$1${ redirect_uri }!$2`);
options.loginUrl = `${ RocketChat.settings.get('Accounts_OAuth_Proxy_host') }/redirect/${ encodeURIComponent(options.loginUrl) }`;
options.loginUrl = `${ settings.get('Accounts_OAuth_Proxy_host') }/redirect/${ encodeURIComponent(options.loginUrl) }`;
}

return func(options);
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/client/UserDeleted.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { ChatMessage } from 'meteor/rocketchat:models';
import { Notifications } from 'meteor/rocketchat:notifications';

Meteor.startup(function() {
RocketChat.Notifications.onLogged('Users:Deleted', ({ userId }) =>
Notifications.onLogged('Users:Deleted', ({ userId }) =>
ChatMessage.remove({
'u._id': userId,
})
Expand Down
19 changes: 11 additions & 8 deletions packages/rocketchat-lib/client/defaultTabBars.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Session } from 'meteor/session';
import { TabBar } from 'meteor/rocketchat:ui-utils';
import { Rooms } from 'meteor/rocketchat:models';
import { hasAllPermission } from 'meteor/rocketchat:authorization';

RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['channel', 'group', 'direct'],
id: 'rocket-search',
i18nTitle: 'Search_Messages',
Expand All @@ -9,7 +12,7 @@ RocketChat.TabBar.addButton({
order: 1,
});

RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['direct'],
id: 'user-info',
i18nTitle: 'User_Info',
Expand All @@ -18,7 +21,7 @@ RocketChat.TabBar.addButton({
order: 2,
});

RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['channel', 'group'],
id: 'members-list',
i18nTitle: 'Members_List',
Expand All @@ -27,19 +30,19 @@ RocketChat.TabBar.addButton({
order: 2,
condition() {
const rid = Session.get('openedRoom');
const room = RocketChat.models.Rooms.findOne({
const room = Rooms.findOne({
_id: rid,
});

if (!room || !room.broadcast) {
return true;
}

return RocketChat.authz.hasAllPermission('view-broadcast-member-list', rid);
return hasAllPermission('view-broadcast-member-list', rid);
},
});

RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['channel', 'group'],
id: 'addUsers',
i18nTitle: 'Add_users',
Expand All @@ -49,7 +52,7 @@ RocketChat.TabBar.addButton({
});


RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['channel', 'group', 'direct'],
id: 'uploaded-files-list',
i18nTitle: 'Room_uploaded_file_list',
Expand All @@ -58,7 +61,7 @@ RocketChat.TabBar.addButton({
order: 3,
});

RocketChat.TabBar.addButton({
TabBar.addButton({
groups: ['channel', 'group', 'direct'],
id: 'keyboard-shortcut-list',
i18nTitle: 'Keyboard_Shortcuts_Title',
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/client/lib/LoginPresence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { settings } from 'meteor/rocketchat:settings';

export const LoginPresence = {
awayTime: 600000, // 10 minutes
Expand All @@ -17,7 +18,7 @@ export const LoginPresence = {
disconnect() {
const status = Meteor.status();
if (status && status.status !== 'offline') {
if (!Meteor.userId() && RocketChat.settings.get('Accounts_AllowAnonymousRead') !== true) {
if (!Meteor.userId() && settings.get('Accounts_AllowAnonymousRead') !== true) {
Meteor.disconnect();
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/rocketchat-lib/client/lib/formatDate.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { Meteor } from 'meteor/meteor';
import { getUserPreference } from 'meteor/rocketchat:utils';
import { settings } from 'meteor/rocketchat:settings';
import moment from 'moment';

export const formatTime = (time) => {
switch (RocketChat.getUserPreference(Meteor.userId(), 'clockMode', false)) {
switch (getUserPreference(Meteor.userId(), 'clockMode', false)) {
case 1:
return moment(time).format('h:mm A');
case 2:
return moment(time).format('H:mm');
default:
return moment(time).format(RocketChat.settings.get('Message_TimeFormat'));
return moment(time).format(settings.get('Message_TimeFormat'));
}
};

export const formatDateAndTime = (time) => {
switch (RocketChat.getUserPreference(Meteor.userId(), 'clockMode', false)) {
switch (getUserPreference(Meteor.userId(), 'clockMode', false)) {
case 1:
return moment(time).format('MMMM D, Y h:mm A');
case 2:
return moment(time).format('MMMM D, Y H:mm');
default:
return moment(time).format(RocketChat.settings.get('Message_TimeAndDateFormat'));
return moment(time).format(settings.get('Message_TimeAndDateFormat'));
}
};

export const formatDate = (time) => moment(time).format(RocketChat.settings.get('Message_DateFormat'));
export const formatDate = (time) => moment(time).format(settings.get('Message_DateFormat'));
17 changes: 10 additions & 7 deletions packages/rocketchat-lib/client/lib/openRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { Tracker } from 'meteor/tracker';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { Session } from 'meteor/session';
import { RoomManager, fireGlobalEvent, readMessage, RoomHistoryManager } from 'meteor/rocketchat:ui-utils';
import { ChatSubscription } from 'meteor/rocketchat:models';
import { RoomManager, fireGlobalEvent, readMessage, RoomHistoryManager, Layout } from 'meteor/rocketchat:ui-utils';
import { ChatSubscription, Rooms } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import { callbacks } from 'meteor/rocketchat:callbacks';
import { roomTypes, handleError } from 'meteor/rocketchat:utils';
import _ from 'underscore';

export let currentTracker = undefined;
Expand All @@ -15,21 +18,21 @@ openRoom = function(type, name) {
return Meteor.defer(() =>
currentTracker = Tracker.autorun(function(c) {
const user = Meteor.user();
if ((user && user.username == null) || (user == null && RocketChat.settings.get('Accounts_AllowAnonymousRead') === false)) {
if ((user && user.username == null) || (user == null && settings.get('Accounts_AllowAnonymousRead') === false)) {
BlazeLayout.render('main');
return;
}

if (RoomManager.open(type + name).ready() !== true) {
BlazeLayout.render('main', { modal: RocketChat.Layout.isEmbedded(), center: 'loading' });
BlazeLayout.render('main', { modal: Layout.isEmbedded(), center: 'loading' });
return;
}
if (currentTracker) {
currentTracker = undefined;
}
c.stop();

const room = RocketChat.roomTypes.findRoom(type, name, user);
const room = roomTypes.findRoom(type, name, user);
if (room == null) {
if (type === 'd') {
Meteor.call('createDirectMessage', name, function(error) {
Expand All @@ -48,7 +51,7 @@ openRoom = function(type, name) {
Session.set('roomNotFound', { type, name, error });
return BlazeLayout.render('main', { center: 'roomNotFound' });
} else {
RocketChat.models.Rooms.upsert({ _id: record._id }, _.omit(record, '_id'));
Rooms.upsert({ _id: record._id }, _.omit(record, '_id'));
RoomManager.close(type + name);
return openRoom(type, name);
}
Expand Down Expand Up @@ -93,7 +96,7 @@ openRoom = function(type, name) {
RoomHistoryManager.getSurroundingMessages(msg);
}

return RocketChat.callbacks.run('enter-room', sub);
return callbacks.run('enter-room', sub);
})
);
};
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/client/lib/roomExit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Blaze } from 'meteor/blaze';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { callbacks } from 'meteor/rocketchat:callbacks';
import { currentTracker } from './openRoom';

this.roomExit = function() {
Expand All @@ -11,7 +12,7 @@ this.roomExit = function() {
templateData && templateData.tabBar && templateData.tabBar.close();
}
}
RocketChat.callbacks.run('roomExit');
callbacks.run('roomExit');
BlazeLayout.render('main', {
center: 'none',
});
Expand Down
8 changes: 5 additions & 3 deletions packages/rocketchat-lib/client/lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { t } from 'meteor/rocketchat:utils';
import { modal } from 'meteor/rocketchat:ui-utils';
import { settings } from 'meteor/rocketchat:settings';
import { hasRole } from 'meteor/rocketchat:authorization';

Meteor.startup(function() {
Tracker.autorun(function(c) {
const siteUrl = RocketChat.settings.get('Site_Url');
const siteUrl = settings.get('Site_Url');
if (!siteUrl || (Meteor.userId() == null)) {
return;
}
if (RocketChat.authz.hasRole(Meteor.userId(), 'admin') === false || Meteor.settings.public.sandstorm) {
if (hasRole(Meteor.userId(), 'admin') === false || Meteor.settings.public.sandstorm) {
return c.stop();
}
Meteor.setTimeout(function() {
Expand All @@ -36,7 +38,7 @@ Meteor.startup(function() {
});
}
}, 100);
const documentDomain = RocketChat.settings.get('Document_Domain');
const documentDomain = settings.get('Document_Domain');
if (documentDomain) {
window.document.domain = documentDomain;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/client/lib/startup/commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { slashCommands } from 'meteor/rocketchat:utils';

// Track logins and when they login, get the commands
(() => {
Expand All @@ -10,7 +11,7 @@ import { Tracker } from 'meteor/tracker';
if (oldUserId === null && newUserId) {
RocketChat.API.v1.get('commands.list').then(function _loadedCommands(result) {
result.commands.forEach((command) => {
RocketChat.slashCommands.commands[command.command] = command;
slashCommands.commands[command.command] = command;
});
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/rocketchat-lib/client/lib/userRoles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { UserRoles, RoomRoles, ChatMessage } from 'meteor/rocketchat:models';
import { handleError } from 'meteor/rocketchat:utils';
import { Notifications } from 'meteor/rocketchat:notifications';

Meteor.startup(function() {
Tracker.autorun(function() {
Expand All @@ -15,7 +17,7 @@ Meteor.startup(function() {
}
});

RocketChat.Notifications.onLogged('roles-change', function(role) {
Notifications.onLogged('roles-change', function(role) {
if (role.type === 'added') {
if (role.scope) {
RoomRoles.upsert({ rid: role.scope, 'u._id': role.u._id }, { $setOnInsert: { u: role.u }, $addToSet: { roles: role._id } });
Expand Down
13 changes: 8 additions & 5 deletions packages/rocketchat-lib/client/methods/sendMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { TimeSync } from 'meteor/mizzao:timesync';
import { ChatMessage } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import { callbacks } from 'meteor/rocketchat:callbacks';
import { promises } from 'meteor/rocketchat:promises';
import s from 'underscore.string';

Meteor.methods({
Expand All @@ -14,17 +17,17 @@ Meteor.methods({
_id: Meteor.userId(),
username: user.username,
};
if (RocketChat.settings.get('UI_Use_Real_Name')) {
if (settings.get('UI_Use_Real_Name')) {
message.u.name = user.name;
}
message.temp = true;
if (RocketChat.settings.get('Message_Read_Receipt_Enabled')) {
if (settings.get('Message_Read_Receipt_Enabled')) {
message.unread = true;
}
message = RocketChat.callbacks.run('beforeSaveMessage', message);
RocketChat.promises.run('onClientMessageReceived', message).then(function(message) {
message = callbacks.run('beforeSaveMessage', message);
promises.run('onClientMessageReceived', message).then(function(message) {
ChatMessage.insert(message);
return RocketChat.callbacks.run('afterSaveMessage', message);
return callbacks.run('afterSaveMessage', message);
});
},
});
5 changes: 3 additions & 2 deletions packages/rocketchat-lib/client/views/customFieldsForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
import { Template } from 'meteor/templating';
import { settings } from 'meteor/rocketchat:settings';

Template.customFieldsForm.helpers({
new() {
Expand Down Expand Up @@ -52,10 +53,10 @@ Template.customFieldsForm.onCreated(function() {
this.formData = (currentData && currentData.formData) || {};

Tracker.autorun(() => {
const Accounts_CustomFields = RocketChat.settings.get('Accounts_CustomFields');
const Accounts_CustomFields = settings.get('Accounts_CustomFields');
if (typeof Accounts_CustomFields === 'string' && Accounts_CustomFields.trim() !== '') {
try {
this.customFields.set(JSON.parse(RocketChat.settings.get('Accounts_CustomFields')));
this.customFields.set(JSON.parse(settings.get('Accounts_CustomFields')));
} catch (e) {
console.error('Invalid JSON for Accounts_CustomFields');
}
Expand Down
19 changes: 2 additions & 17 deletions packages/rocketchat-lib/lib/getUserPreference.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/**
* Tries to retrieve the user preference falling back to a default system
* value or to a default value if it is passed as argument
*/
RocketChat.getUserPreference = function(user, key, defaultValue = undefined) {
let preference;
if (typeof user === typeof '') {
user = RocketChat.models.Users.findOne(user, { fields: { [`settings.preferences.${ key }`]: 1 } });
}
if (user && user.settings && user.settings.preferences &&
user.settings.preferences.hasOwnProperty(key)) {
preference = user.settings.preferences[key];
} else if (defaultValue === undefined) {
preference = RocketChat.settings.get(`Accounts_Default_User_Preferences_${ key }`);
}
import { getUserPreference } from 'meteor/rocketchat:utils';

return preference !== undefined ? preference : defaultValue;
};
RocketChat.getUserPreference = getUserPreference;
Loading