Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Apr 16, 2019
2 parents 295f9dc + 270f513 commit 3bc2440
Show file tree
Hide file tree
Showing 22 changed files with 841 additions and 210 deletions.
5 changes: 4 additions & 1 deletion app/api/server/helpers/getUserInfo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { settings } from '../../../settings';
import { getUserPreference } from '../../../utils';
import { getUserPreference, getURL } from '../../../utils';
import { API } from '../api';

const getInfoFromUserObject = (user) => {
Expand Down Expand Up @@ -54,6 +54,9 @@ API.helperMethods.set('getUserInfo', function _getUserInfo(user) {
};
const verifiedEmail = isVerifiedEmail();
me.email = verifiedEmail ? verifiedEmail.address : undefined;

me.avatarUrl = getURL(`/avatar/${ me.username }`, { cdn: false, full: true });

me.settings = {
preferences: getUserPreferences(),
};
Expand Down
85 changes: 85 additions & 0 deletions app/apps/lib/misc/transformMappedData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import cloneDeep from 'lodash.clonedeep';

/**
* Transforms a `data` source object to another object,
* essentially applying a to -> from mapping provided by
* `map`. It does not mutate the `data` object.
*
* It also inserts in the `transformedObject` a new property
* called `_unmappedProperties_` which contains properties from
* the original `data` that have not been mapped to its transformed
* counterpart. E.g.:
*
* ```javascript
* const data = { _id: 'abcde123456', size: 10 };
* const map = { id: '_id' }
*
* transformMappedData(data, map);
* // { id: 'abcde123456', _unmappedProperties_: { size: 10 } }
* ```
*
* In order to compute the unmapped properties, this function will
* ignore any property on `data` that has been named on the "from" part
* of the `map`, and will consider properties not mentioned as unmapped.
*
* You can also define the "from" part as a function, so you can derive a
* new value for your property from the original `data`. This function will
* receive a copy of the original `data` for it to calculate the value
* for its "to" field. Please note that in this case `transformMappedData`
* will not be able to determine the source field from your map, so it won't
* ignore any field you've used to derive your new value. For that, you're
* going to need to delete the value from the received parameter. E.g:
*
* ```javascript
* const data = { _id: 'abcde123456', size: 10 };
*
* // It will look like the `size` property is not mapped
* const map = {
* id: '_id',
* newSize: (data) => data.size + 10
* };
*
* transformMappedData(data, map);
* // { id: 'abcde123456', newSize: 20, _unmappedProperties_: { size: 10 } }
*
* // You need to explicitly remove it from the original `data`
* const map = {
* id: '_id',
* newSize: (data) => {
* const result = data.size + 10;
* delete data.size;
* return result;
* }
* };
*
* transformMappedData(data, map);
* // { id: 'abcde123456', newSize: 20, _unmappedProperties_: {} }
* ```
*
* @param Object data The data to be transformed
* @param Object map The map with transformations to be applied
*
* @returns Object The data after transformations have been applied
*/

export const transformMappedData = (data, map) => {
const originalData = cloneDeep(data);
const transformedData = {};

Object.entries(map).forEach(([to, from]) => {
if (typeof from === 'function') {
const result = from(originalData);

if (typeof result !== 'undefined') {
transformedData[to] = result;
}
} else if (typeof from === 'string' && typeof originalData[from] !== 'undefined') {
transformedData[to] = originalData[from];
delete originalData[from];
}
});

transformedData._unmappedProperties_ = originalData;

return transformedData;
};
4 changes: 0 additions & 4 deletions app/apps/server/bridges/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ export class AppHttpBridge {
}

async call(info) {
if (typeof info.request.timeout !== 'number' || info.request.timeout > 500) {
info.request.timeout = 500;
}

if (!info.request.content && typeof info.request.data === 'object') {
info.request.content = JSON.stringify(info.request.data);
}
Expand Down
194 changes: 106 additions & 88 deletions app/apps/server/converters/messages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Random } from 'meteor/random';
import { Messages, Rooms, Users } from '../../../models';
import { transformMappedData } from '../../lib/misc/transformMappedData';

export class AppMessagesConverter {
constructor(orch) {
Expand All @@ -17,42 +18,55 @@ export class AppMessagesConverter {
return undefined;
}

const room = this.orch.getConverters().get('rooms').convertById(msgObj.rid);
const map = {
id: '_id',
reactions: 'reactions',
parseUrls: 'parseUrls',
text: 'msg',
createdAt: 'ts',
updatedAt: '_updatedAt',
editedAt: 'editedAt',
emoji: 'emoji',
avatarUrl: 'avatar',
alias: 'alias',
customFields: 'customFields',
groupable: 'groupable',
room: (message) => {
const result = this.orch.getConverters().get('rooms').convertById(message.rid);
delete message.rid;
return result;
},
editor: (message) => {
const { editedBy } = message;
delete message.editedBy;

if (!editedBy) {
return undefined;
}

let sender;
if (msgObj.u && msgObj.u._id) {
sender = this.orch.getConverters().get('users').convertById(msgObj.u._id);
return this.orch.getConverters().get('users').convertById(editedBy._id);
},
attachments: (message) => {
const result = this._convertAttachmentsToApp(message.attachments);
delete message.attachments;
return result;
},
sender: (message) => {
let result;

if (message.u && message.u._id) {
result = this.orch.getConverters().get('users').convertById(message.u._id);
} else {
result = this.orch.getConverters().get('users').convertToApp(message.u);
}

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

let editor;
if (msgObj.editedBy) {
editor = this.orch.getConverters().get('users').convertById(msgObj.editedBy._id);
}

const attachments = this._convertAttachmentsToApp(msgObj.attachments);

return {
id: msgObj._id,
room,
sender,
text: msgObj.msg,
createdAt: msgObj.ts,
updatedAt: msgObj._updatedAt,
editor,
editedAt: msgObj.editedAt,
emoji: msgObj.emoji,
avatarUrl: msgObj.avatar,
alias: msgObj.alias,
customFields: msgObj.customFields,
groupable: msgObj.groupable,
attachments,
reactions: msgObj.reactions,
parseUrls: msgObj.parseUrls,
return result;
},
};

return transformMappedData(msgObj, map);
}

convertAppMessage(message) {
Expand Down Expand Up @@ -96,7 +110,7 @@ export class AppMessagesConverter {

const attachments = this._convertAppAttachments(message.attachments);

return {
const newMessage = {
_id: message.id || Random.id(),
rid: room._id,
u,
Expand All @@ -114,14 +128,16 @@ export class AppMessagesConverter {
reactions: message.reactions,
parseUrls: message.parseUrls,
};

return Object.assign(newMessage, message._unmappedProperties_);
}

_convertAppAttachments(attachments) {
if (typeof attachments === 'undefined' || !Array.isArray(attachments)) {
return undefined;
}

return attachments.map((attachment) => ({
return attachments.map((attachment) => Object.assign({
collapsed: attachment.collapsed,
color: attachment.color,
text: attachment.text,
Expand Down Expand Up @@ -150,67 +166,69 @@ export class AppMessagesConverter {
actions: attachment.actions,
type: attachment.type,
description: attachment.description,
})).map((a) => {
Object.keys(a).forEach((k) => {
if (typeof a[k] === 'undefined') {
delete a[k];
}
});

return a;
});
}, attachment._unmappedProperties_));
}

_convertAttachmentsToApp(attachments) {
if (typeof attachments === 'undefined' || !Array.isArray(attachments)) {
return undefined;
}

return attachments.map((attachment) => {
let author;
if (attachment.author_name || attachment.author_link || attachment.author_icon) {
author = {
name: attachment.author_name,
link: attachment.author_link,
icon: attachment.author_icon,
};
}

let title;
if (attachment.title || attachment.title_link || attachment.title_link_download) {
title = {
value: attachment.title,
link: attachment.title_link,
displayDownloadLink: attachment.title_link_download,
};
}
const map = {
collapsed: 'collapsed',
color: 'color',
text: 'text',
timestampLink: 'message_link',
thumbnailUrl: 'thumb_url',
imageDimensions: 'image_dimensions',
imagePreview: 'image_preview',
imageUrl: 'image_url',
imageType: 'image_type',
imageSize: 'image_size',
audioUrl: 'audio_url',
audioType: 'audio_type',
audioSize: 'audio_size',
videoUrl: 'video_url',
videoType: 'video_type',
videoSize: 'video_size',
fields: 'fields',
actionButtonsAlignment: 'button_alignment',
actions: 'actions',
type: 'type',
description: 'description',
author: (attachment) => {
const {
author_name: name,
author_link: link,
author_icon: icon,
} = attachment;

delete attachment.author_name;
delete attachment.author_link;
delete attachment.author_icon;

return { name, link, icon };
},
title: (attachment) => {
const {
title: value,
title_link: link,
title_link_download: displayDownloadLink,
} = attachment;

delete attachment.title;
delete attachment.title_link;
delete attachment.title_link_download;

return { value, link, displayDownloadLink };
},
timestamp: (attachment) => {
const result = new Date(attachment.ts);
delete attachment.ts;
return result;
},
};

return {
collapsed: attachment.collapsed,
color: attachment.color,
text: attachment.text,
timestamp: new Date(attachment.ts),
timestampLink: attachment.message_link,
thumbnailUrl: attachment.thumb_url,
author,
title,
imageDimensions: attachment.image_dimensions,
imagePreview: attachment.image_preview,
imageUrl: attachment.image_url,
imageType: attachment.image_type,
imageSize: attachment.image_size,
audioUrl: attachment.audio_url,
audioType: attachment.audio_type,
audioSize: attachment.audio_size,
videoUrl: attachment.video_url,
videoType: attachment.video_type,
videoSize: attachment.video_size,
fields: attachment.fields,
actionButtonsAlignment: attachment.button_alignment,
actions: attachment.actions,
type: attachment.type,
description: attachment.description,
};
});
return attachments.map((attachment) => transformMappedData(attachment, map));
}
}
Loading

0 comments on commit 3bc2440

Please sign in to comment.