Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
fetchAttachment
Browse files Browse the repository at this point in the history
  • Loading branch information
callum-oakley committed Feb 21, 2018
1 parent 9ee073a commit 3564f6f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 60 deletions.
132 changes: 79 additions & 53 deletions dist/web/chatkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
});
};

Expand Down
29 changes: 24 additions & 5 deletions src/current-user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sendRawRequest } from 'pusher-platform'
import {
chain,
compose,
Expand All @@ -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'
Expand Down Expand Up @@ -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 */
Expand Down
14 changes: 12 additions & 2 deletions src/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 3564f6f

Please sign in to comment.