Skip to content

Commit

Permalink
refactor: Remove imports based on isomorphic logic - 3 (#29071)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Apr 27, 2023
1 parent 69662f4 commit bcd54b7
Show file tree
Hide file tree
Showing 26 changed files with 221 additions and 104 deletions.
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji-emojione/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import '../lib/rocketchat';
import './lib';
import './emojione-sprites.css';
49 changes: 49 additions & 0 deletions apps/meteor/app/emoji-emojione/client/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Meteor } from 'meteor/meteor';

import { emoji } from '../../emoji/client';
import { getEmojiConfig, isSetNotNull } from '../lib/rocketchat';
import { getUserPreference } from '../../utils/client';

const config = getEmojiConfig();

emoji.packages.emojione = config.emojione as any;
if (emoji.packages.emojione) {
emoji.packages.emojione.sprites = config.sprites;
emoji.packages.emojione.emojisByCategory = config.emojisByCategory;
emoji.packages.emojione.emojiCategories = config.emojiCategories;
emoji.packages.emojione.toneList = config.toneList;

emoji.packages.emojione.render = config.render;
emoji.packages.emojione.renderPicker = config.renderPicker;

// RocketChat.emoji.list is the collection of emojis from all emoji packages
for (const key in config.emojione.emojioneList) {
if (config.emojione.emojioneList.hasOwnProperty(key)) {
const currentEmoji = config.emojione.emojioneList[key];
// @ts-expect-error - emojione types
currentEmoji.emojiPackage = 'emojione';
emoji.list[key] = currentEmoji;

// @ts-expect-error - emojione types
if (currentEmoji.shortnames) {
// @ts-expect-error - emojione types
currentEmoji.shortnames.forEach((shortname: string) => {
emoji.list[shortname] = currentEmoji;
});
}
}
}

// Additional settings -- ascii emojis
Meteor.startup(function () {
Tracker.autorun(async function () {
if ((await isSetNotNull(() => emoji.packages.emojione)) && emoji.packages.emojione) {
if (await isSetNotNull(() => getUserPreference(Meteor.userId() as string, 'convertAsciiEmoji'))) {
emoji.packages.emojione.ascii = await getUserPreference(Meteor.userId() as string, 'convertAsciiEmoji');
} else {
emoji.packages.emojione.ascii = true;
}
}
});
});
}
9 changes: 0 additions & 9 deletions apps/meteor/app/emoji-emojione/lib/emojioneRender.js

This file was deleted.

9 changes: 9 additions & 0 deletions apps/meteor/app/emoji-emojione/lib/emojioneRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import emojione from 'emojione';

export function emojioneRender(message: string): string {
return emojione.toImage(message);
}

export function emojioneRenderFromShort(message: string): string {
return emojione.shortnameToImage(message);
}
55 changes: 13 additions & 42 deletions apps/meteor/app/emoji-emojione/lib/rocketchat.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import emojione from 'emojione';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import mem from 'mem';

import { emojioneRender, emojioneRenderFromShort } from './emojioneRender';
import { emojisByCategory, emojiCategories, toneList } from './emojiPicker';
import { emoji } from '../../emoji';
import { getUserPreference } from '../../utils';

// TODO remove fix below when issue is solved: https://github.com/joypixels/emojione/issues/617

Expand Down Expand Up @@ -260,50 +256,25 @@ emojione.emojioneList[':asterisk_symbol:'] = {
};
})(emojione);

emoji.packages.emojione = emojione;
emoji.packages.emojione.sprites = true;
emoji.packages.emojione.emojisByCategory = emojisByCategory;
emoji.packages.emojione.emojiCategories = emojiCategories;
emoji.packages.emojione.toneList = toneList;

emoji.packages.emojione.render = emojioneRender;
emoji.packages.emojione.renderPicker = emojioneRenderFromShort;
export function getEmojiConfig() {
return {
emojione,
emojisByCategory,
emojiCategories,
toneList,
render: emojioneRender,
renderPicker: emojioneRenderFromShort,
sprites: true,
};
}

// http://stackoverflow.com/a/26990347 function isSet() from Gajus
function isSetNotNull(fn) {
export async function isSetNotNull(fn) {
let value;
try {
value = fn();
value = await fn();
} catch (e) {
value = null;
}
return value !== null && value !== undefined;
}

// RocketChat.emoji.list is the collection of emojis from all emoji packages
for (const key in emojione.emojioneList) {
if (emojione.emojioneList.hasOwnProperty(key)) {
const currentEmoji = emojione.emojioneList[key];
currentEmoji.emojiPackage = 'emojione';
emoji.list[key] = currentEmoji;

if (currentEmoji.shortnames) {
currentEmoji.shortnames.forEach((shortname) => {
emoji.list[shortname] = currentEmoji;
});
}
}
}

// Additional settings -- ascii emojis
Meteor.startup(function () {
Tracker.autorun(function () {
if (isSetNotNull(() => emoji.packages.emojione)) {
if (isSetNotNull(() => getUserPreference(Meteor.userId(), 'convertAsciiEmoji'))) {
emoji.packages.emojione.ascii = getUserPreference(Meteor.userId(), 'convertAsciiEmoji');
} else {
emoji.packages.emojione.ascii = true;
}
}
});
});
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji-emojione/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import '../lib/rocketchat';
import './lib';
import './callbacks';
47 changes: 47 additions & 0 deletions apps/meteor/app/emoji-emojione/server/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Meteor } from 'meteor/meteor';

import { emoji } from '../../emoji/server';
import { getEmojiConfig, isSetNotNull } from '../lib/rocketchat';
import { getUserPreference } from '../../utils/server';

const config = getEmojiConfig();

emoji.packages.emojione = config.emojione as any;
if (emoji.packages.emojione) {
emoji.packages.emojione.sprites = config.sprites;
emoji.packages.emojione.emojisByCategory = config.emojisByCategory;
emoji.packages.emojione.emojiCategories = config.emojiCategories;
emoji.packages.emojione.toneList = config.toneList;

emoji.packages.emojione.render = config.render;
emoji.packages.emojione.renderPicker = config.renderPicker;

// RocketChat.emoji.list is the collection of emojis from all emoji packages
for (const key in config.emojione.emojioneList) {
if (config.emojione.emojioneList.hasOwnProperty(key)) {
const currentEmoji = config.emojione.emojioneList[key];
// @ts-expect-error - emojione types
currentEmoji.emojiPackage = 'emojione';
emoji.list[key] = currentEmoji;

// @ts-expect-error - emojione types
if (currentEmoji.shortnames) {
// @ts-expect-error - emojione types
currentEmoji.shortnames.forEach((shortname: string) => {
emoji.list[shortname] = currentEmoji;
});
}
}
}

// Additional settings -- ascii emojis
Meteor.startup(async function () {
if ((await isSetNotNull(() => emoji.packages.emojione)) && emoji.packages.emojione) {
if (await isSetNotNull(() => getUserPreference(Meteor.userId() as string, 'convertAsciiEmoji'))) {
emoji.packages.emojione.ascii = await getUserPreference(Meteor.userId() as string, 'convertAsciiEmoji');
} else {
emoji.packages.emojione.ascii = true;
}
}
});
}
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji/client/emojiParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isIE11 } from '../../../client/lib/utils/isIE11';
import { emoji } from '../lib/rocketchat';
import { emoji } from './lib';

/*
* emojiParser is a function that will replace emojis
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji/client/emojiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import $ from 'jquery';

import { t } from '../../utils/client';
import { EmojiPicker } from './lib/EmojiPicker';
import { emoji } from '../lib/rocketchat';
import { emoji } from './lib';
import { withThrottling } from '../../../lib/utils/highOrderFunctions';
import { baseURI } from '../../../client/lib/baseURI';
import './emojiPicker.html';
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { EmojiPicker } from './lib/EmojiPicker';
export { emoji } from '../lib/rocketchat';
export { emoji } from './lib';
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { emojioneRender } from '../../emoji-emojione/lib/emojioneRender';
import type { EmojiPackage } from '../lib/rocketchat';

let EmojiPicker;
const removeFromRecent = (emoji) => {
let EmojiPicker: any;
const removeFromRecent = (emoji: string) => {
if (!EmojiPicker) {
// since this function will be only called client side, the import needs to happen here
({ EmojiPicker } = require('../client/lib/EmojiPicker'));
({ EmojiPicker } = require('./lib/EmojiPicker'));
}
EmojiPicker.removeFromRecent(emoji.replace(/(^:|:$)/g, ''));
};

export const emoji = {
export const emoji: EmojiPackage = {
packages: {
base: {
emojiCategories: [{ key: 'recent', i18n: 'Frequently_Used' }],
Expand All @@ -25,7 +26,11 @@ export const emoji = {
return;
}
const correctPackage = emoji.list[emojiToRender].emojiPackage;
return emoji.packages[correctPackage].renderPicker(emojiToRender);
if (!correctPackage) {
return;
}

return emoji.packages[correctPackage]?.renderPicker(emojiToRender);
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji/client/lib/EmojiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';

import { emoji } from '../../lib/rocketchat';
import { emoji } from '../lib';
import { updateRecentEmoji } from '../emojiPicker';
import { withDebouncing } from '../../../../lib/utils/highOrderFunctions';

Expand Down
8 changes: 0 additions & 8 deletions apps/meteor/app/emoji/index.js

This file was deleted.

31 changes: 31 additions & 0 deletions apps/meteor/app/emoji/lib/rocketchat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type EmojiPackage = {
packages: {
base: {
emojiCategories: Array<{ key: string; i18n: string }>;
categoryIndex: number;
emojisByCategory: Record<string, unknown>;
toneList: Record<string, unknown>;
render: (message: string) => string;
renderPicker: (emojiToRender: string) => void;
ascii?: boolean;
};
emojione?: {
sprites: boolean;
emojisByCategory: Record<string, unknown>;
emojiCategories: Array<{ key: string; i18n: string }>;
toneList: Record<string, unknown>;
render: (message: string) => string;
renderPicker: (emojiToRender: string) => void;
ascii?: boolean;
};
};
list: Record<
string,
{ emojiPackage?: keyof NonNullable<EmojiPackage['packages']> } & {
unicode: string[];
fname: string;
uc: string;
isCanonical: boolean;
}
>;
};
2 changes: 1 addition & 1 deletion apps/meteor/app/emoji/server/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { emoji } from '../lib/rocketchat';
export { emoji } from './lib';
28 changes: 28 additions & 0 deletions apps/meteor/app/emoji/server/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { EmojiPackage } from '../lib/rocketchat';

export const emoji: EmojiPackage = {
packages: {
base: {
emojiCategories: [{ key: 'recent', i18n: 'Frequently_Used' }],
categoryIndex: 0,
emojisByCategory: {
recent: [],
},
toneList: {},
render: (message: string) => message,
renderPicker(emojiToRender) {
if (!emoji.list[emojiToRender]) {
return;
}
const correctPackage = emoji.list[emojiToRender].emojiPackage;
if (!correctPackage) {
return;
}

return emoji.packages[correctPackage]?.renderPicker(emojiToRender);
},
},
},
/** @type {Record<string, unknown>} */
list: {},
};
5 changes: 0 additions & 5 deletions apps/meteor/app/models/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/meteor/app/utils/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion apps/meteor/client/lib/utils/renderEmoji.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { emoji } from '../../../app/emoji/lib/rocketchat';
import { emoji } from '../../../app/emoji/client/lib';

const emojiList = emoji.list as Record<string, { emojiPackage: string }>;
const emojiPackages = emoji.packages as Record<string, { render(emojiName: string): string }>;
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../ee/definition';
import '../ee/client/ecdh';
import './polyfills';

import '../lib/oauthRedirectUri';
import '../lib/oauthRedirectUriClient';
import './lib/meteorCallWrapper';
import './importPackages';

Expand Down
20 changes: 20 additions & 0 deletions apps/meteor/lib/oauthRedirectUriClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { OAuth } from 'meteor/oauth';

const { _redirectUri } = OAuth;

OAuth._redirectUri = (serviceName: string, config: any, params: unknown, absoluteUrlOptions: unknown): string => {
const ret = _redirectUri(serviceName, config, params, absoluteUrlOptions);

// DEPRECATED: Remove in v5.0.0
// Meteor 2.3 removed ?close from redirect uri so we need to add it back to not break old oauth clients
// https://github.com/meteor/meteor/commit/b5b7306bedc3e8eb241e64efb1e281925aa75dd3#diff-59244f4e0176cb1beed2e287924e97dc7ae2c0cc51494ce121a85d8937d116a5L11
if (!config?.loginStyle && !ret.includes('close')) {
console.warn(
`Automatically added ?close to 'redirect_uri' for ${serviceName}, this behavior will be removed in v5.0.0.\n` +
"Please update your OAuth config to accept both with and without ?close as the 'redirect_uri'.",
);
return `${ret + (ret.includes('?') ? '&' : '?')}close`;
}

return ret;
};
Loading

0 comments on commit bcd54b7

Please sign in to comment.