Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了消息漫游rest接口【getMessages】 #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# Changelog

## v1.8.1 @ 2018-5-21
* [sdk] 添加获取漫游消息API
```
* conn.getMessages(options)
```
通过getMessages方法,你可以获取某用户(群组)的历史消息记录,每次执行此方法可以获取指定条数的消息
* queue 会话用户username
* group 是否是群组,默认false
* count 获取消息数量 默认一次获取20条
* start 起始消息id,默认为-1, 从头开始
* success 成功回调
* fail 失败回调

```javascript
// 获取漫游消息接口演示
conn.getMessages({
queue: "sunylt",
group: false,
count: 20,
success: function(res) {
console.log(res) // 如果获取到的结果为[],则表示结束
},
fail: function(err) {
console.log(err)
}
})
```

## v1.8.0 @ 2018-5-21
* [sdk] 解决无法申请入群
* [sdk] 解决无法邀请好友
Expand Down
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easemob-websdk",
"version": "1.8.0",
"version": "1.8.1",
"description": "Easemob IM websdk",
"main": "index.js",
"scripts": {
Expand Down
176 changes: 163 additions & 13 deletions sdk/src/connection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _version = '1.4.13';
var _version = '1.8.1';
var _code = require('./status').code;
var _utils = require('./utils').utils;
var _msg = require('./message');
Expand All @@ -8,9 +8,10 @@ var Queue = require('./queue').Queue;
var CryptoJS = require('crypto-js');
var _ = require('underscore');

var Strophe = window.Strophe
var Strophe = window.Strophe;
var isStropheLog;
var stropheConn = null
var stropheConn = null;
var mr_cache = {};

window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;

Expand Down Expand Up @@ -1523,7 +1524,7 @@ connection.prototype.handleIqRoster = function (e) {
return true;
};

connection.prototype.handleMessage = function (msginfo) {
connection.prototype.handleMessage = function (msginfo, ignoreCallback) {
var self = this;
if (this.isClosed()) {
return;
Expand Down Expand Up @@ -1624,7 +1625,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onEmojiMessage(msg);
!ignoreCallback && this.onEmojiMessage(msg);
} else {
var msg = {
id: id
Expand All @@ -1639,7 +1640,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onTextMessage(msg);
!ignoreCallback && this.onTextMessage(msg);
}
break;
case 'img':
Expand Down Expand Up @@ -1672,7 +1673,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onPictureMessage(msg);
!ignoreCallback && this.onPictureMessage(msg);
break;
case 'audio':
var msg = {
Expand All @@ -1695,7 +1696,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onAudioMessage(msg);
!ignoreCallback && this.onAudioMessage(msg);
break;
case 'file':
var msg = {
Expand All @@ -1716,7 +1717,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onFileMessage(msg);
!ignoreCallback && this.onFileMessage(msg);
break;
case 'loc':
var msg = {
Expand All @@ -1734,7 +1735,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onLocationMessage(msg);
!ignoreCallback && this.onLocationMessage(msg);
break;
case 'video':
var msg = {
Expand All @@ -1755,7 +1756,7 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onVideoMessage(msg);
!ignoreCallback && this.onVideoMessage(msg);
break;
case 'cmd':
var msg = {
Expand All @@ -1770,11 +1771,11 @@ connection.prototype.handleMessage = function (msginfo) {
msg.error = errorBool;
msg.errorText = errorText;
msg.errorCode = errorCode;
this.onCmdMessage(msg);
!ignoreCallback && this.onCmdMessage(msg);
break;
}
;
if (self.delivery) {
if (self.delivery && !ignoreCallback) {
var msgId = self.getUniqueId();
var bodyId = msg.id;
var deliverMessage = new WebIM.message('delivery', msgId);
Expand All @@ -1784,6 +1785,10 @@ connection.prototype.handleMessage = function (msginfo) {
});
self.send(deliverMessage.body);
}
if (ignoreCallback) {
msg.message_type = type
return msg
}
} catch (e) {
this.onError({
type: _code.WEBIM_CONNCTION_CALLBACK_INNER_ERROR
Expand Down Expand Up @@ -2131,6 +2136,151 @@ connection.prototype.joinPublicGroup = function (options) {
this.context.stropheConn.sendIQ(iq.tree(), suc, errorFn);
};

/**
* 获取对话历史消息
* @param {Object} options
* @param {String} options.queue
* @param {Function} options.success
* @param {Funciton} options.fail
*/
connection.prototype.getMessages = function(options) {
var conn = this
if (!options.queue) {
conn.onError({
type: "",
msg: "queue is not specified"
});
return;
}

var count = options.count || 20

function _readCacheMessages() {
conn._fetchMessages({
count: count,
isGroup: options.group ? true: false,
queue: options.queue,
success: function(data) {
var length = data.msgs.length
if (length >= count || data.is_last) {
options.success(_utils.reverse(data.msgs.splice(0, count)))
} else {
_readCacheMessages()
}
}
})
}
_readCacheMessages()
};

/**
* 获取对话历史消息
* @param {Object} options
* @param {String} options.queue
* @param {Function} options.success
* @param {Funciton} options.fail
*/
connection.prototype._fetchMessages = function(options) {
var conn = this,
token = options.accessToken || this.context.accessToken

if (!_utils.isCanSetRequestHeader) {
conn.onError({
type: _code.WEBIM_CONNCTION_NOT_SUPPORT_CHATROOM_ERROR
});
return;
}

if (token) {
var apiUrl = this.apiUrl;
var appName = this.context.appName;
var orgName = this.context.orgName;

if (!appName || !orgName) {
conn.onError({
type: _code.WEBIM_CONNCTION_AUTH_ERROR
});
return;
}

var queue = options.queue
var _dataQueue = mr_cache[queue] || (mr_cache[queue] = {msgs: []})

var suc = function (res, xhr) {

if (res && res.data) {
var data = res.data,
msgs = data.msgs,
length = msgs.length;

_dataQueue.is_last = data.is_last;
_dataQueue.next_key = data.next_key;

for (var i = 0; i < length; i++) {

// 将xml消息字符串转成xml对象
var xmlMsg = Strophe.xmlHtmlNode(msgs[i]).getElementsByTagName("message")[0];

// 将xml对象转换成json消息,true参数,只会消息进行处理,不触发事件
var msgObj = conn.handleMessage(xmlMsg, true)
msgObj && _dataQueue.msgs.push(msgObj);
}

typeof options.success === 'function' && options.success(_dataQueue);
}
};

var error = function (res, xhr, msg) {
if (res.error && res.error_description) {
conn.onError({
type: _code.WEBIM_CONNCTION_LOAD_CHATROOM_ERROR,
msg: res.error_description,
data: res,
xhr: xhr
});
}
};

var userId = this.context.userId;
var start = -1

// 无历史消息或者缓存消息足够不再加载
if (_dataQueue.msgs.length >= options.count || _dataQueue.is_last) {
typeof options.success === 'function' && options.success(_dataQueue);
return;
}

// 根据上一次拉取返回的last_key 进行本次消息拉取
if (_dataQueue && _dataQueue.next_key) {
start = _dataQueue.next_key
}

var suffix = options.isGroup ? "@conference.easemob.com" : "@easemob.com";
var data = {
queue: queue + suffix,
start: start,
end: -1
};

var opts = {
url: apiUrl + '/' + orgName + '/' + appName + '/users/' + userId + '/messageroaming',
dataType: 'json',
type: 'POST',
headers: {'Authorization': 'Bearer ' + token},
data: JSON.stringify(data),
success: suc || _utils.emptyfn,
error: error || _utils.emptyfn
};

_utils.ajax(opts);

} else {
conn.onError({
type: _code.WEBIM_CONNCTION_TOKEN_NOT_ASSIGN_ERROR
});
}
};

connection.prototype.listRooms = function (options) {
var iq = $iq({
to: options.server || 'conference.' + this.domain,
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,18 @@
allCookie[name] = value;
}
return allCookie;
},

reverse: function(array) {
var newArray = []
if (Array.prototype.reverse) {
newArray = array.reverse()
} else {
for (var i = 0; i < array.length; i++) {
newArray.unshift(array[i])
}
}
return newArray
}
};

Expand Down