Skip to content

Commit

Permalink
[OTHER] More Listeners for Apps & Utilize Promises inside Apps (#10335)
Browse files Browse the repository at this point in the history
* Add support for more listeners and reorder the calling stack on sendMessage

* Improve the message converters and fix message listeners

* Allow updating of an app. Add buttons to view logs and update app. Fix various issues related to the apps

* Whoops, name instead of fname for rooms returned name

* Bump the versions of the Rocket.Chat Apps system

* Add the missing 'msgs' property on rooms back

* Fix the sendMessage parseUrls not correctly being checked

* Update the apps dependencies and use the new property on message attachments

* Convert over to using the promises everywhere

* Bump the apps packages versions

* Make all of the App Enabled status be simply Enabled and nothing more

* Add the debug npm script to the package.json
  • Loading branch information
graywolf336 authored Apr 18, 2018
1 parent abb0bd6 commit 2592990
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 160 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
],
"scripts": {
"start": "meteor npm i && meteor",
"debug": "meteor run --inspect",
"lint": "eslint .",
"lint-fix": "eslint . --fix",
"stylelint": "stylelint packages/**/*.css",
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ An orchestrator is the file/class which is responsible for orchestrating (starti
A bridge is a file/class which is responsible for bridging the Rocket.Chat system's data and the App system's data. They are implementations of the interfaces inside of the Rocket.Chat Apps-engine project `src/server/bridges`. They allow the two systems to talk to each other (hince the name bridge, as they "bridge the gap").

## What is a "Converter"?
A converter does what the name implies, it handles converting from one system's data type into the other's.
A converter does what the name implies, it handles converting from one system's data type into the other's. **Note**: This causes a schema to be forced on the rooms and messages.
24 changes: 22 additions & 2 deletions packages/rocketchat-apps/client/admin/appInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ Template.appInstall.onCreated(function() {
instance.file = new ReactiveVar('');
instance.isInstalling = new ReactiveVar(false);
instance.appUrl = new ReactiveVar('');
instance.isUpdatingId = new ReactiveVar('');

// Allow passing in a url as a query param to show installation of
if (FlowRouter.getQueryParam('url')) {
instance.appUrl.set(FlowRouter.getQueryParam('url'));
FlowRouter.setQueryParams({ url: null });
}

if (FlowRouter.getQueryParam('isUpdatingId')) {
instance.isUpdatingId.set(FlowRouter.getQueryParam('isUpdatingId'));
}
});

Template.appInstall.events({
Expand All @@ -55,13 +60,21 @@ Template.appInstall.events({
if (url) {
try {
t.isInstalling.set(true);
const result = await RocketChat.API.post('apps', { url });
let result;

if (t.isUpdatingId.get()) {
result = await RocketChat.API.post(`apps/${ t.isUpdatingId.get() }`, { url });
} else {
result = await RocketChat.API.post('apps', { url });
}

FlowRouter.go(`/admin/apps/${ result.app.id }`);
} catch (err) {
console.warn('err', err);
} finally {
t.isInstalling.set(false);
}

return;
}

Expand All @@ -85,7 +98,14 @@ Template.appInstall.events({

t.isInstalling.set(true);
try {
const result = await RocketChat.API.upload('apps', data);
let result;

if (t.isUpdatingId.get()) {
result = await RocketChat.API.upload(`apps/${ t.isUpdatingId.get() }`, data);
} else {
result = await RocketChat.API.upload('apps', data);
}

FlowRouter.go(`/admin/apps/${ result.app.id }`);
} catch (err) {
console.warn('err', err);
Expand Down
10 changes: 7 additions & 3 deletions packages/rocketchat-apps/client/admin/appLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ Template.appLogs.helpers({
return moment(date).format('L LTS');
},
jsonStringify(data) {
let value = '';

if (!data) {
return '';
return value;
} else if (typeof data === 'object') {
return hljs.highlight('json', JSON.stringify(data, null, 2)).value;
value = hljs.highlight('json', JSON.stringify(data, null, 2)).value;
} else {
return hljs.highlight('json', data).value;
value = hljs.highlight('json', data).value;
}

return value.replace(/\\\\n/g, '<br>');
}
});

Expand Down
8 changes: 5 additions & 3 deletions packages/rocketchat-apps/client/admin/appManage.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<div class="rc-apps-details__content">
<div class="rc-apps-details__row">
<div class="rc-apps-details__name">{{name}}</div>
<div class="rc-apps-details__version">v {{version}}</div>
<div class="rc-apps-details__version">v{{version}}</div>
</div>
<div class="rc-apps-details__row">{{description}}</div>
<div class="rc-apps-details__row">
Expand All @@ -37,13 +37,13 @@
{{#if author.homepage}}
<a href="{{author.homepage}}" class="rc-apps-details__item">
{{> icon icon='permalink'}}
author homepage
{{_ "App_author_homepage"}}
</a>
{{/if}}
{{#if author.support}}
<a href="{{author.support}}" class="rc-apps-details__item">
{{> icon icon='at'}}
support url
{{_ "App_support_url"}}
</a>
{{/if}}
</div>
Expand Down Expand Up @@ -384,6 +384,8 @@
<button class="rc-button rc-button--secondary js-cancel">{{ _ "Cancel" }}</button>
<button class="rc-button rc-button--primary js-save {{#if saving}} loading{{/if}}" disabled='{{disabled}}'>{{ _ "Save" }}</button>
<button class="rc-button rc-button--cancel js-uninstall">{{ _ "Uninstall" }}</button>
<button class="rc-button rc-button--secondary js-update">{{_ "Update" }}</button>
<button class="rc-button rc-button--secondary js-view-logs">{{_ "View_Logs" }}</button>
</div>
</div>
{{else if hasError}}
Expand Down
8 changes: 6 additions & 2 deletions packages/rocketchat-apps/client/admin/appManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Template.appManage.onCreated(function() {
function _morphSettings(settings) {
Object.keys(settings).forEach((k) => {
settings[k].i18nPlaceholder = settings[k].i18nPlaceholder || ' ';
settings[k].value = settings[k].value !== undefined ? settings[k].value : settings[k].packageValue;
settings[k].value = settings[k].value !== undefined && settings[k].value !== null ? settings[k].value : settings[k].packageValue;
settings[k].oldValue = settings[k].value;
settings[k].hasChanged = false;
});
Expand Down Expand Up @@ -208,7 +208,11 @@ Template.appManage.events({
}
},

'click .logs': (e, t) => {
'click .js-update': (e, t) => {
FlowRouter.go(`/admin/app/install?isUpdatingId=${ t.id.get() }`);
},

'click .js-view-logs': (e, t) => {
FlowRouter.go(`/admin/apps/${ t.id.get() }/logs`);
},

Expand Down
10 changes: 5 additions & 5 deletions packages/rocketchat-apps/client/admin/apps.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<table class="rc-table">
<thead class="rc-table-head">
<tr class="rc-table-tr">
<td class="rc-table-td">Name</td>
<td class="rc-table-td">Version</td>
<td class="rc-table-td">Status</td>
<td class="rc-table-td">{{_ "Name"}}</td>
<td class="rc-table-td">{{_ "Version"}}</td>
<td class="rc-table-td">{{_ "Status"}}</td>
<td class="rc-table-td"></td>
</tr>
</thead>
Expand All @@ -33,7 +33,7 @@
</td>
<td class="rc-table-td rc-table-td--users">{{version}}</td>
<td class="rc-table-td">
{{status}}
{{parseStatus status}}
</td>
<td class="rc-table-td">
<button class="rc-button rc-button--nude rc-tooltip manage" aria-label="{{_ "Manage_the_App"}}">
Expand All @@ -44,7 +44,7 @@
{{else}}
<tr class="rc-table-tr" data-name="{{name}}">
<td class="rc-table-td rc-table-td--name">
you dont have any app installed :/
{{_ "There_are_no_applications_installed"}}
</td>
</tr>
{{/each}}
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-apps/client/admin/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Template.apps.helpers({
},
apps() {
return Template.instance().apps.get();
},
parseStatus(status) {
return t(`App_status_${ status }`);
}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-apps/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ Package.onUse(function(api) {

Npm.depends({
'busboy': '0.2.13',
'@rocket.chat/apps-engine': '0.4.8',
'@rocket.chat/apps-ts-definition': '0.7.15'
'@rocket.chat/apps-engine': '0.5.11',
'@rocket.chat/apps-ts-definition': '0.9.6'
});
16 changes: 8 additions & 8 deletions packages/rocketchat-apps/server/bridges/activation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ export class AppActivationBridge {
this.orch = orch;
}

appAdded(app) {
this.orch.getNotifier().appAdded(app.getID());
async appAdded(app) {
await this.orch.getNotifier().appAdded(app.getID());
}

appUpdated(app) {
this.orch.getNotifier().appUpdated(app.getID());
async appUpdated(app) {
await this.orch.getNotifier().appUpdated(app.getID());
}

appRemoved(app) {
this.orch.getNotifier().appRemoved(app.getID());
async appRemoved(app) {
await this.orch.getNotifier().appRemoved(app.getID());
}

appStatusChanged(app, status) {
this.orch.getNotifier().appStatusUpdated(app.getID(), status);
async appStatusChanged(app, status) {
await this.orch.getNotifier().appStatusUpdated(app.getID(), status);
}
}
6 changes: 3 additions & 3 deletions packages/rocketchat-apps/server/bridges/environmental.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class AppEnvironmentalVariableBridge {
this.allowed = ['NODE_ENV', 'ROOT_URL', 'INSTANCE_IP'];
}

getValueByName(envVarName, appId) {
async getValueByName(envVarName, appId) {
console.log(`The App ${ appId } is getting the environmental variable value ${ envVarName }.`);

if (this.isReadable(envVarName, appId)) {
Expand All @@ -14,13 +14,13 @@ export class AppEnvironmentalVariableBridge {
throw new Error(`The environmental variable "${ envVarName }" is not readable.`);
}

isReadable(envVarName, appId) {
async isReadable(envVarName, appId) {
console.log(`The App ${ appId } is checking if the environmental variable is readable ${ envVarName }.`);

return this.allowed.includes(envVarName.toUpperCase());
}

isSet(envVarName, appId) {
async isSet(envVarName, appId) {
console.log(`The App ${ appId } is checking if the environmental variable is set ${ envVarName }.`);

if (this.isReadable(envVarName, appId)) {
Expand Down
10 changes: 5 additions & 5 deletions packages/rocketchat-apps/server/bridges/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export class AppHttpBridge {

console.log(`The App ${ info.appId } is requesting from the outter webs:`, info);

try {
return HTTP.call(info.method, info.url, info.request);
} catch (e) {
return e.response;
}
return new Promise((resolve, reject) => {
HTTP.call(info.method, info.url, info.request, (e, result) => {
return e ? reject(e.response) : resolve(result);
});
});
}
}
32 changes: 28 additions & 4 deletions packages/rocketchat-apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@ export class AppListenerBridge {
this.orch = orch;
}

messageEvent(inte, message) {
async messageEvent(inte, message) {
const msg = this.orch.getConverters().get('messages').convertMessage(message);
return this.orch.getManager().getListenerManager().executeListener(inte, msg);
const result = await this.orch.getManager().getListenerManager().executeListener(inte, msg);

if (typeof result === 'boolean') {
return result;
} else {
return this.orch.getConverters().get('messages').convertAppMessage(result);
}
// try {

// } catch (e) {
// console.log(`${ e.name }: ${ e.message }`);
// console.log(e.stack);
// }
}

roomEvent(inte, room) {
async roomEvent(inte, room) {
const rm = this.orch.getConverters().get('rooms').convertRoom(room);
return this.orch.getManager().getListenerManager().executeListener(inte, rm);
const result = await this.orch.getManager().getListenerManager().executeListener(inte, rm);

if (typeof result === 'boolean') {
return result;
} else {
return this.orch.getConverters().get('rooms').convertAppRoom(result);
}
// try {

// } catch (e) {
// console.log(`${ e.name }: ${ e.message }`);
// console.log(e.stack);
// }
}
}
19 changes: 6 additions & 13 deletions packages/rocketchat-apps/server/bridges/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class AppMessageBridge {
this.orch = orch;
}

create(message, appId) {
async create(message, appId) {
console.log(`The App ${ appId } is creating a new message.`);

let msg = this.orch.getConverters().get('messages').convertAppMessage(message);
Expand All @@ -15,13 +15,13 @@ export class AppMessageBridge {
return msg._id;
}

getById(messageId, appId) {
async getById(messageId, appId) {
console.log(`The App ${ appId } is getting the message: "${ messageId }"`);

return this.orch.getConverters().get('messages').convertById(messageId);
}

update(message, appId) {
async update(message, appId) {
console.log(`The App ${ appId } is updating a message.`);

if (!message.editor) {
Expand All @@ -38,7 +38,7 @@ export class AppMessageBridge {
RocketChat.updateMessage(msg, editor);
}

notifyUser(user, message, appId) {
async notifyUser(user, message, appId) {
console.log(`The App ${ appId } is notifying a user.`);

const msg = this.orch.getConverters().get('messages').convertAppMessage(message);
Expand All @@ -51,16 +51,9 @@ export class AppMessageBridge {
}));
}

notifyRoom(room, message, appId) {
async notifyRoom(room, message, appId) {
console.log(`The App ${ appId } is notifying a room's users.`);

const msg = this.orch.getConverters().get('messages').convertAppMessage(message);

RocketChat.Notifications.notifyUsersOfRoom(room.id, 'message', Object.assign(msg, {
_id: Random.id(),
ts: new Date(),
u: undefined,
editor: undefined
}));
throw new Error('Not implemented yet.');
}
}
Loading

0 comments on commit 2592990

Please sign in to comment.