Skip to content

Commit c558273

Browse files
Marcos Spessatto Defendisampaiodiego
authored andcommitted
[IMPROVE] Replace fullEmojiData publication by REST (#15901)
1 parent 6ff1ed8 commit c558273

File tree

11 files changed

+124
-54
lines changed

11 files changed

+124
-54
lines changed

app/api/server/lib/emoji-custom.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EmojiCustom } from '../../../models/server/raw';
2+
3+
export async function findEmojisCustom({ query = {}, pagination: { offset, count, sort } }) {
4+
const cursor = EmojiCustom.find(query, {
5+
sort: sort || { name: 1 },
6+
skip: offset,
7+
limit: count,
8+
});
9+
10+
const total = await cursor.count();
11+
12+
const emojis = await cursor.toArray();
13+
14+
return {
15+
emojis,
16+
count: emojis.length,
17+
offset,
18+
total,
19+
};
20+
}

app/api/server/v1/emoji-custom.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Busboy from 'busboy';
33

44
import { EmojiCustom } from '../../../models';
55
import { API } from '../api';
6+
import { findEmojisCustom } from '../lib/emoji-custom';
67

78
// DEPRECATED
89
// Will be removed after v3.0.0
@@ -51,6 +52,22 @@ API.v1.addRoute('emoji-custom.list', { authRequired: true }, {
5152
},
5253
});
5354

55+
API.v1.addRoute('emoji-custom.all', { authRequired: true }, {
56+
get() {
57+
const { offset, count } = this.getPaginationItems();
58+
const { sort, query } = this.parseJsonQuery();
59+
60+
return API.v1.success(Promise.await(findEmojisCustom({
61+
query,
62+
pagination: {
63+
offset,
64+
count,
65+
sort,
66+
},
67+
})));
68+
},
69+
});
70+
5471
API.v1.addRoute('emoji-custom.create', { authRequired: true }, {
5572
post() {
5673
Meteor.runAsUser(this.userId, () => {

app/emoji-custom/client/admin/adminEmoji.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
<form class="search-form" role="form">
1010
<div class="rc-input__wrapper">
1111
<div class="rc-input__icon">
12-
{{#if isReady}}
13-
{{> icon block="rc-input__icon-svg" icon="magnifier" }}
14-
{{else}}
12+
{{#if isLoading}}
1513
{{> loading }}
14+
{{else}}
15+
{{> icon block="rc-input__icon-svg" icon="magnifier" }}
1616
{{/if}}
1717
</div>
1818
<input id="emoji-filter" type="text" class="rc-input__element"
@@ -59,11 +59,11 @@
5959
<tr class="table-no-click">
6060
<td>{{_ "No_results_found_for"}} {{.}}</td>
6161
</tr>
62-
{{/with}} {{/each}} {{#unless isReady}}
62+
{{/with}} {{/each}} {{#if isLoading}}
6363
<tr class="table-no-click">
6464
<td class="table-loading-td">{{> loading}}</td>
6565
</tr>
66-
{{/unless}}
66+
{{/if}}
6767
</tbody>
6868
{{/table}}
6969
{{/unless}}

app/emoji-custom/client/admin/adminEmoji.js

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,24 @@ import { ReactiveVar } from 'meteor/reactive-var';
22
import { Tracker } from 'meteor/tracker';
33
import { FlowRouter } from 'meteor/kadira:flow-router';
44
import { Template } from 'meteor/templating';
5-
import s from 'underscore.string';
5+
import _ from 'underscore';
66

7-
import { EmojiCustom } from '../../../models';
87
import { RocketChatTabBar, SideNav, TabBar } from '../../../ui-utils';
8+
import { APIClient } from '../../../utils/client';
9+
10+
const LIST_SIZE = 50;
11+
const DEBOUNCE_TIME_TO_SEARCH_IN_MS = 500;
912

1013
Template.adminEmoji.helpers({
1114
searchText() {
1215
const instance = Template.instance();
1316
return instance.filter && instance.filter.get();
1417
},
15-
isReady() {
16-
if (Template.instance().ready != null) {
17-
return Template.instance().ready.get();
18-
}
19-
return undefined;
20-
},
2118
customemoji() {
22-
return Template.instance().customemoji();
19+
return Template.instance().emojis.get();
2320
},
2421
isLoading() {
25-
if (Template.instance().ready != null) {
26-
if (!Template.instance().ready.get()) {
27-
return 'btn-loading';
28-
}
29-
}
30-
},
31-
hasMore() {
32-
if (Template.instance().limit != null) {
33-
if (typeof Template.instance().customemoji === 'function') {
34-
return Template.instance().limit.get() === Template.instance().customemoji().length;
35-
}
36-
}
37-
return false;
22+
return Template.instance().isLoading.get();
3823
},
3924
flexData() {
4025
return {
@@ -48,26 +33,32 @@ Template.adminEmoji.helpers({
4833
if ((currentTarget.offsetHeight + currentTarget.scrollTop) < (currentTarget.scrollHeight - 100)) {
4934
return;
5035
}
51-
if (instance.limit.get() > instance.customemoji().length) {
52-
return false;
36+
const emojis = instance.emojis.get();
37+
if (instance.total.get() > emojis.length) {
38+
instance.offset.set(instance.offset.get() + LIST_SIZE);
5339
}
54-
instance.limit.set(instance.limit.get() + 50);
5540
};
5641
},
5742
onTableItemClick() {
5843
const instance = Template.instance();
5944
return function({ _id }) {
60-
instance.tabBarData.set(EmojiCustom.findOne({ _id }));
45+
instance.tabBarData.set({
46+
emoji: instance.emojis.get().find((emoji) => emoji._id === _id),
47+
onSuccess: instance.onSuccessCallback,
48+
});
6149
instance.tabBar.open('admin-emoji-info');
6250
};
6351
},
6452
});
6553

66-
Template.adminEmoji.onCreated(function() {
54+
Template.adminEmoji.onCreated(async function() {
6755
const instance = this;
68-
this.limit = new ReactiveVar(50);
56+
this.emojis = new ReactiveVar([]);
57+
this.offset = new ReactiveVar(0);
58+
this.total = new ReactiveVar(0);
6959
this.filter = new ReactiveVar('');
70-
this.ready = new ReactiveVar(false);
60+
this.query = new ReactiveVar({});
61+
this.isLoading = new ReactiveVar(false);
7162

7263
this.tabBar = new RocketChatTabBar();
7364
this.tabBar.showGroup(FlowRouter.current().route.name);
@@ -90,27 +81,40 @@ Template.adminEmoji.onCreated(function() {
9081
template: 'adminEmojiInfo',
9182
order: 2,
9283
});
93-
94-
this.autorun(function() {
95-
const limit = instance.limit != null ? instance.limit.get() : 0;
96-
const subscription = instance.subscribe('fullEmojiData', '', limit);
97-
instance.ready.set(subscription.ready());
84+
this.onSuccessCallback = () => {
85+
this.offset.set(0);
86+
return this.loadEmojis(this.query.get(), this.offset.get());
87+
};
88+
this.tabBarData.set({
89+
onSuccess: instance.onSuccessCallback,
9890
});
9991

100-
this.customemoji = function() {
101-
const filter = instance.filter != null ? s.trim(instance.filter.get()) : '';
92+
this.loadEmojis = _.debounce(async (query, offset) => {
93+
this.isLoading.set(true);
94+
const { emojis, total } = await APIClient.v1.get(`emoji-custom.all?count=${ LIST_SIZE }&offset=${ offset }&query=${ JSON.stringify(query) }`);
95+
this.total.set(total);
96+
if (offset === 0) {
97+
this.emojis.set(emojis);
98+
} else {
99+
this.emojis.set(this.emojis.get().concat(emojis));
100+
}
101+
this.isLoading.set(false);
102+
}, DEBOUNCE_TIME_TO_SEARCH_IN_MS);
102103

103-
let query = {};
104+
this.autorun(() => {
105+
this.filter.get();
106+
this.offset.set(0);
107+
});
104108

109+
this.autorun(() => {
110+
const filter = this.filter.get() && this.filter.get().trim();
111+
const offset = this.offset.get();
105112
if (filter) {
106-
const filterReg = new RegExp(s.escapeRegExp(filter), 'i');
107-
query = { $or: [{ name: filterReg }, { aliases: filterReg }] };
113+
const regex = { $regex: filter, $options: 'i' };
114+
return this.loadEmojis({ $or: [{ name: regex }, { aliases: regex }] }, offset);
108115
}
109-
110-
const limit = instance.limit != null ? instance.limit.get() : 0;
111-
112-
return EmojiCustom.find(query, { limit, sort: { name: 1 } }).fetch();
113-
};
116+
return this.loadEmojis({}, offset);
117+
});
114118
});
115119

116120
Template.adminEmoji.onRendered(() =>

app/emoji-custom/client/admin/emojiEdit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Template.emojiEdit.onCreated(function() {
5858
}
5959

6060
this.tabBar = Template.currentData().tabBar;
61+
this.onSuccess = Template.currentData().onSuccess;
6162

6263
this.cancel = (form, name) => {
6364
form.reset();
@@ -143,6 +144,7 @@ Template.emojiEdit.onCreated(function() {
143144
} else {
144145
toastr.success(t('Custom_Emoji_Added_Successfully'));
145146
}
147+
this.onSuccess();
146148

147149
this.cancel(form, emojiData.name);
148150
}

app/emoji-custom/client/admin/emojiInfo.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Template.emojiInfo.helpers({
2929
return {
3030
tabBar: this.tabBar,
3131
emoji: instance.emoji.get(),
32+
onSuccess: instance.onSuccess,
3233
back(name) {
3334
instance.editingEmoji.set();
3435

@@ -76,6 +77,7 @@ Template.emojiInfo.events({
7677
timer: 2000,
7778
showConfirmButton: false,
7879
});
80+
instance.onSuccess();
7981

8082
instance.tabBar.close();
8183
});
@@ -86,13 +88,13 @@ Template.emojiInfo.events({
8688
'click .edit-emoji'(e, instance) {
8789
e.stopPropagation();
8890
e.preventDefault();
89-
9091
instance.editingEmoji.set(instance.emoji.get()._id);
9192
},
9293
});
9394

9495
Template.emojiInfo.onCreated(function() {
9596
this.emoji = new ReactiveVar();
97+
this.onSuccess = Template.currentData().onSuccess;
9698

9799
this.editingEmoji = new ReactiveVar();
98100

@@ -108,7 +110,7 @@ Template.emojiInfo.onCreated(function() {
108110
});
109111

110112
this.autorun(() => {
111-
const data = Template.currentData();
113+
const data = Template.currentData().emoji;
112114
const emoji = this.emoji.get();
113115
if (emoji != null && emoji.name != null) {
114116
this.loadedName.set(emoji.name);
@@ -118,7 +120,7 @@ Template.emojiInfo.onCreated(function() {
118120
});
119121

120122
this.autorun(() => {
121-
const data = Template.currentData();
123+
const data = Template.currentData().emoji;
122124
this.emoji.set(data);
123125
});
124126
});

app/emoji-custom/server/publications/fullEmojiData.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import s from 'underscore.string';
44
import { EmojiCustom } from '../../../models';
55

66
Meteor.publish('fullEmojiData', function(filter, limit) {
7+
console.warn('The publication "fullEmojiData" is deprecated and will be removed after version v3.0.0');
78
if (!this.userId) {
89
return this.ready();
910
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { BaseRaw } from './BaseRaw';
2+
3+
export class EmojiCustomRaw extends BaseRaw {
4+
5+
}

app/models/server/raw/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import LivechatExternalMessagesModel from '../models/LivechatExternalMessages';
2828
import { LivechatExternalMessageRaw } from './LivechatExternalMessages';
2929
import LivechatVisitorsModel from '../models/LivechatVisitors';
3030
import { LivechatVisitorsRaw } from './LivechatVisitors';
31+
import EmojiCustomModel from '../models/EmojiCustom';
32+
import { EmojiCustomRaw } from './EmojiCustom';
3133
import WebdavAccountsModel from '../models/WebdavAccounts';
3234
import { WebdavAccountsRaw } from './WebdavAccounts';
3335
import OAuthAppsModel from '../models/OAuthApps';
@@ -54,6 +56,7 @@ export const LivechatRooms = new LivechatRoomsRaw(LivechatRoomsModel.model.rawCo
5456
export const Messages = new MessagesRaw(MessagesModel.model.rawCollection());
5557
export const LivechatExternalMessage = new LivechatExternalMessageRaw(LivechatExternalMessagesModel.model.rawCollection());
5658
export const LivechatVisitors = new LivechatVisitorsRaw(LivechatVisitorsModel.model.rawCollection());
59+
export const EmojiCustom = new EmojiCustomRaw(EmojiCustomModel.model.rawCollection());
5760
export const WebdavAccounts = new WebdavAccountsRaw(WebdavAccountsModel.model.rawCollection());
5861
export const OAuthApps = new OAuthAppsRaw(OAuthAppsModel.model.rawCollection());
5962
export const CustomSounds = new CustomSoundsRaw(CustomSoundsModel.model.rawCollection());

app/reactions/client/methods/setReaction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Meteor } from 'meteor/meteor';
22
import _ from 'underscore';
33

4-
import { Messages, Rooms, Subscriptions, EmojiCustom } from '../../../models';
4+
import { Messages, Rooms, Subscriptions } from '../../../models';
55
import { callbacks } from '../../../callbacks';
66
import { emoji } from '../../../emoji';
77

@@ -34,7 +34,7 @@ Meteor.methods({
3434
return false;
3535
}
3636

37-
if (!emoji.list[reaction] && EmojiCustom.findByNameOrAlias(reaction).count() === 0) {
37+
if (!emoji.list[reaction]) {
3838
return false;
3939
}
4040

0 commit comments

Comments
 (0)