twilio-chat
Advanced tools
Comparing version 0.11.1-alpha.2 to 0.11.1-alpha.3
@@ -233,3 +233,2 @@ 'use strict'; | ||
.then(entity => { | ||
//TODO: Some nice getter here? | ||
const messagesObjectName = entity.value.messages; | ||
@@ -456,3 +455,3 @@ const rosterObjectName = entity.value.roster; | ||
* Returns messages from channel using paginator interface | ||
* @param {Number} [pageSize=100] Number of messages to return in single chunk. | ||
* @param {Number} [pageSize=30] Number of messages to return in single chunk. | ||
* @param {Number} [anchor] - Index of newest Message to fetch. From the end by default. | ||
@@ -497,6 +496,6 @@ * @returns {Promise<Paginator<Message>>} page of messages | ||
/** | ||
* Get unread messages count | ||
* Get unconsumed messages count | ||
* @returns {Promise<integer>} | ||
*/ | ||
getUnreadMessagesCount() { | ||
getUnconsumedMessagesCount() { | ||
return this._session.getSessionLinks() | ||
@@ -507,5 +506,5 @@ .then(links => new UriBuilder(links.myChannelsUrl).arg('ChannelSid', this.sid).build()) | ||
if (response.body.channels.length) { | ||
return response.body.channels[0].unread_messages_count; | ||
return response.body.channels[0].unread_messages_count || null; | ||
} | ||
return null; | ||
throw new Error('Channel not found'); | ||
}); | ||
@@ -569,3 +568,3 @@ } | ||
sendMessage(messageBody, messageAttributes) { | ||
return this._messagesEntity.send(messageBody, messageAttributes); | ||
return this._messagesEntity.send(messageBody, messageAttributes).then(response => response.messageId); | ||
} | ||
@@ -572,0 +571,0 @@ |
@@ -7,3 +7,2 @@ 'use strict'; | ||
const Message = require('../message'); | ||
const Paginator = require('../paginator'); | ||
@@ -135,6 +134,31 @@ function isForward(direction) { | ||
direction = direction || 'backwards'; | ||
return this._getMessages(pageSize, anchor, direction) | ||
.then(messages => new Paginator(messages, pageSize, anchor, direction, this.getMessages.bind(this))); | ||
return this._getMessages(pageSize, anchor, direction); | ||
} | ||
_wrapPaginator(page, op) { | ||
// We should swap next and prev page here, because of misfit of Sync and Chat paging conceptions | ||
return op(page.items).then(items => ({ | ||
items: items.sort((x, y) => { | ||
console.log(typeof x.index, x.index, typeof y.index, y.index); | ||
return x.index - y.index; | ||
}), | ||
hasPrevPage: page.hasNextPage, | ||
hasNextPage: page.hasPrevPage, | ||
prevPage: () => page.nextPage().then(x => this._wrapPaginator(x, op)), | ||
nextPage: () => page.prevPage().then(x => this._wrapPaginator(x, op)) | ||
})); | ||
} | ||
_upsertMessage(index, value) { | ||
let cachedMessage = this._messagesByIndex.get(index); | ||
if (cachedMessage) { | ||
return cachedMessage; | ||
} | ||
let message = new Message(this.channel, index, value); | ||
this._messagesByIndex.set(message.index, message); | ||
message.on('updated', () => this.emit('messageUpdated', message)); | ||
return message; | ||
} | ||
/** | ||
@@ -144,51 +168,27 @@ * Returns last messages from channel | ||
* @param {String} [anchor] Most early message id which is already known, or 'end' by default | ||
* @returns {Promise<Array<Message>>} last page of messages by default | ||
* @returns {Promise<Paginator<Message>>} last page of messages by default | ||
* @private | ||
*/ | ||
_getMessages(pageSize, anchor, direction) { | ||
pageSize = pageSize || 100; | ||
anchor = (typeof anchor !== 'undefined') ? anchor : 'end'; | ||
direction = direction || 'backwards'; | ||
let count = pageSize; | ||
pageSize = pageSize || 30; | ||
let order = direction === 'backwards' ? 'desc' : 'asc'; | ||
if (anchor !== 'end') { | ||
count++; | ||
} | ||
if (anchor !== 'end') { pageSize++; } | ||
return this.subscribe().then(messagesList => | ||
messagesList.getItems({ from: anchor !== 'end' ? anchor : void (0), pageSize: count })) | ||
.then((messagesPage) => { | ||
let messages = []; | ||
messagesPage.items.forEach(item => { | ||
let message = new Message(this.channel, item.index, item.value); | ||
let dedupMessage = this._messagesByIndex.get(message.index); | ||
if (!dedupMessage) { | ||
this._messagesByIndex.set(message.index, message); | ||
message.on('updated', () => this.emit('messageUpdated', message)); | ||
dedupMessage = message; | ||
} | ||
messages.push(dedupMessage); | ||
}); | ||
// Remove the matched anchor | ||
return this.subscribe() | ||
.then(messagesList => messagesList.getItems({ from: anchor !== 'end' ? anchor : void (0), pageSize, order })) | ||
.then(page => { | ||
if (anchor !== 'end') { | ||
if (isForward(direction)) { messages.shift(); } | ||
else { messages.pop(); } | ||
if (isForward(direction)) { page.items.shift(); } | ||
else { page.items.pop(); } | ||
} | ||
this._updateSortedMessages(); | ||
return messages; | ||
}); | ||
return page; | ||
}) | ||
.then(page => this._wrapPaginator(page | ||
, items => Promise.all(items.map(item => this._upsertMessage(item.index, item.value)))) | ||
); | ||
} | ||
_updateSortedMessages() { | ||
let uniqueValues = []; | ||
this._messagesByIndex.forEach(message => { uniqueValues.push(message); }); | ||
uniqueValues.sort((message1, message2) => message1.index - message2.index); | ||
Array.prototype.splice.apply(this._sortedMessages, | ||
[0, this._sortedMessages.length].concat(uniqueValues)); | ||
} | ||
} | ||
module.exports = Messages; |
'use strict'; | ||
const inherits = require('util').inherits; | ||
/** | ||
@@ -12,16 +10,18 @@ * @class | ||
*/ | ||
function SessionError(message, code) { | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.code = code; | ||
class SessionError extends Error { | ||
constructor(message, code) { | ||
super(); | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = (new Error()).stack; | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.code = code; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = (new Error()).stack; | ||
} | ||
} | ||
} | ||
inherits(SessionError, Error); | ||
module.exports = SessionError; |
{ | ||
"name": "twilio-chat", | ||
"version": "0.11.1-alpha.2", | ||
"version": "0.11.1-alpha.3", | ||
"description": "A library for Twilio IP messaging", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1803239
69
25632