diff --git a/dist/web/chatkit.js b/dist/web/chatkit.js index 8bf5868..b6cd7c0 100644 --- a/dist/web/chatkit.js +++ b/dist/web/chatkit.js @@ -5684,6 +5684,70 @@ function TokenProvider() { this.url = url; }; +var parseBasicRoom = function parseBasicRoom(data) { + return { + createdAt: data.created_at, + createdByUserId: data.created_by_id, + deletedAt: data.deletedAt, + id: data.id, + isPrivate: data.private, + name: data.name, + updatedAt: data.updated_at, + userIds: data.member_user_ids + }; +}; + +var parseUser = function parseUser(data) { + return { + avatarURL: data.avatar_url, + createdAt: data.created_at, + customData: data.custom_data, + id: data.id, + name: data.name, + updatedAt: data.updated_at + }; +}; + +var parsePresence = function parsePresence(data) { + return { + lastSeenAt: data.last_seen_at, + state: contains$1(data.state, ['online', 'offline']) ? data.state : 'unknown', + userId: data.user_id + }; +}; + +var parseBasicMessage = function parseBasicMessage(data) { + return { + id: data.id, + senderId: data.user_id, + roomId: data.room_id, + text: data.text, + createdAt: data.created_at, + updatedAt: data.updated_at, + attachment: data.attachment && parseMessageAttachment(data.attachment) + }; +}; + +var parseFetchedAttachment = function parseFetchedAttachment(data) { + return { + file: { + name: data.file.name, + bytes: data.file.bytes, + lastModified: data.file.last_modified + }, + link: data.resource_link, + ttl: data.ttl + }; +}; + +var parseMessageAttachment = function parseMessageAttachment(data) { + return { + link: data.resource_link, + type: data.type, + fetchRequired: extractQueryParams(data.resource_link).chatkit_link === 'true' + }; +}; + var Store = function Store() { var _this = this; @@ -5744,58 +5808,6 @@ var Store = function Store() { }; }; -var parseBasicRoom = function parseBasicRoom(data) { - return { - createdAt: data.created_at, - createdByUserId: data.created_by_id, - deletedAt: data.deletedAt, - id: data.id, - isPrivate: data.private, - name: data.name, - updatedAt: data.updated_at, - userIds: data.member_user_ids - }; -}; - -var parseUser = function parseUser(data) { - return { - avatarURL: data.avatar_url, - createdAt: data.created_at, - customData: data.custom_data, - id: data.id, - name: data.name, - updatedAt: data.updated_at - }; -}; - -var parsePresence = function parsePresence(data) { - return { - lastSeenAt: data.last_seen_at, - state: contains$1(data.state, ['online', 'offline']) ? data.state : 'unknown', - userId: data.user_id - }; -}; - -var parseBasicMessage = function parseBasicMessage(data) { - return { - id: data.id, - senderId: data.user_id, - roomId: data.room_id, - text: data.text, - createdAt: data.created_at, - updatedAt: data.updated_at, - attachment: data.attachment && parseAttachment(data.attachment) - }; -}; - -var parseAttachment = function parseAttachment(data) { - return { - link: data.resource_link, - type: data.type, - fetchRequired: extractQueryParams(data.resource_link).chatkit_link === 'true' - }; -}; - var UserStore = function UserStore(_ref) { var _this = this; @@ -6617,7 +6629,21 @@ var CurrentUser = function () { return room; }); }).catch(function (err) { - return _this.logger.error('error subscribing to room ' + roomId, err); + _this.logger.warn('error subscribing to room ' + roomId + ':', err); + throw err; + }); + }; + + this.fetchAttachment = function (url) { + return _this.filesInstance.tokenProvider.fetchToken().then(function (token) { + return pusherPlatform_4({ + method: 'GET', + headers: { Authorization: 'Bearer ' + token }, + url: url + }); + }).then(pipe(JSON.parse, parseFetchedAttachment)).catch(function (err) { + _this.logger.warn('error fetching attachment:', err); + throw err; }); }; diff --git a/src/current-user.js b/src/current-user.js index e7a21bb..9379db6 100644 --- a/src/current-user.js +++ b/src/current-user.js @@ -1,3 +1,4 @@ +import { sendRawRequest } from 'pusher-platform' import { chain, compose, @@ -19,10 +20,14 @@ import { typeCheckObj, urlEncode } from './utils' +import { + parseBasicMessage, + parseBasicRoom, + parseFetchedAttachment +} from './parsers' import { Store } from './store' import { UserStore } from './user-store' import { RoomStore } from './room-store' -import { parseBasicRoom, parseBasicMessage } from './parsers' import { TypingIndicators } from './typing-indicators' import { UserSubscription } from './user-subscription' import { PresenceSubscription } from './presence-subscription' @@ -256,10 +261,24 @@ export class CurrentUser { }) return this.joinRoom(roomId) .then(room => this.roomSubscriptions[roomId].connect().then(() => room)) - .catch(err => this.logger.error( - `error subscribing to room ${roomId}`, - err - )) + .catch(err => { + this.logger.warn(`error subscribing to room ${roomId}:`, err) + throw err + }) + } + + fetchAttachment = url => { + return this.filesInstance.tokenProvider.fetchToken() + .then(token => sendRawRequest({ + method: 'GET', + headers: { Authorization: `Bearer ${token}` }, + url + })) + .then(pipe(JSON.parse, parseFetchedAttachment)) + .catch(err => { + this.logger.warn(`error fetching attachment:`, err) + throw err + }) } /* internal */ diff --git a/src/parsers.js b/src/parsers.js index d478acd..026e043 100644 --- a/src/parsers.js +++ b/src/parsers.js @@ -35,10 +35,20 @@ export const parseBasicMessage = data => ({ text: data.text, createdAt: data.created_at, updatedAt: data.updated_at, - attachment: data.attachment && parseAttachment(data.attachment) + attachment: data.attachment && parseMessageAttachment(data.attachment) }) -const parseAttachment = data => ({ +export const parseFetchedAttachment = data => ({ + file: { + name: data.file.name, + bytes: data.file.bytes, + lastModified: data.file.last_modified + }, + link: data.resource_link, + ttl: data.ttl +}) + +const parseMessageAttachment = data => ({ link: data.resource_link, type: data.type, fetchRequired: extractQueryParams(data.resource_link).chatkit_link === 'true'