Skip to content

Commit

Permalink
Fix the Apps and Livechats not getting along well with each other (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
graywolf336 authored and sampaiodiego committed Apr 27, 2018
1 parent 751d07d commit 7ba14d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/rocketchat-apps/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ Package.onUse(function(api) {
Npm.depends({
'busboy': '0.2.13',
'@rocket.chat/apps-engine': '0.5.11',
'@rocket.chat/apps-ts-definition': '0.9.6'
'@rocket.chat/apps-ts-definition': '0.9.8'
});
22 changes: 18 additions & 4 deletions packages/rocketchat-apps/server/converters/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class AppMessagesConverter {
let sender;
if (msgObj.u && msgObj.u._id) {
sender = this.orch.getConverters().get('users').convertById(msgObj.u._id);

if (!sender) {
sender = this.orch.getConverters().get('users').convertToApp(msgObj.u);
}
}

let editor;
Expand Down Expand Up @@ -59,10 +63,20 @@ export class AppMessagesConverter {
let u;
if (message.sender && message.sender.id) {
const user = RocketChat.models.Users.findOneById(message.sender.id);
u = {
_id: user._id,
username: user.username
};

if (user) {
u = {
_id: user._id,
username: user.username,
name: user.name
};
} else {
u = {
_id: message.sender.id,
username: message.sender.username,
name: message.sender.name
};
}
}

let editedBy;
Expand Down
15 changes: 10 additions & 5 deletions packages/rocketchat-apps/server/converters/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ export class AppUsersConverter {
convertById(userId) {
const user = RocketChat.models.Users.findOneById(userId);

return this._convertToApp(user);
return this.convertToApp(user);
}

convertByUsername(username) {
const user = RocketChat.models.Users.findOneByUsername(username);

return this._convertToApp(user);
return this.convertToApp(user);
}

_convertToApp(user) {
convertToApp(user) {
if (!user) {
return undefined;
}

const type = this._convertUserTypeToEnum(user.type);
const status = this._convertStatusConnectionToEnum(user.username, user._id, user.status);
const statusConnection = this._convertStatusConnectionToEnum(user.username, user._id, user.statusConnection);

return {
Expand All @@ -34,7 +33,7 @@ export class AppUsersConverter {
isEnabled: user.active,
name: user.name,
roles: user.roles,
status,
status: user.status,
statusConnection,
utcOffset: user.utcOffset,
createdAt: user.createdAt,
Expand All @@ -49,6 +48,9 @@ export class AppUsersConverter {
return UserType.USER;
case 'bot':
return UserType.BOT;
case '':
case undefined:
return UserType.UNKNOWN;
default:
console.warn(`A new user type has been added that the Apps don't know about? "${ type }"`);
return type.toUpperCase();
Expand All @@ -65,6 +67,9 @@ export class AppUsersConverter {
return UserStatusConnection.AWAY;
case 'busy':
return UserStatusConnection.BUSY;
case undefined:
// This is needed for Livechat guests and Rocket.Cat user.
return UserStatusConnection.UNDEFINED;
default:
console.warn(`The user ${ username } (${ userId }) does not have a valid status (offline, online, away, or busy). It is currently: "${ status }"`);
return !status ? UserStatusConnection.OFFLINE : status.toUpperCase();
Expand Down

0 comments on commit 7ba14d1

Please sign in to comment.