rcs-sdk
Version:
封装客户端与平台间的相互调用功能。
99 lines • 4.39 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConversation = exports.getAllConversationList = void 0;
const constants_1 = require("../constants");
const util_1 = require("../util");
const rcs_data_1 = require("rcs-data");
const nanoid_1 = require("nanoid");
const debug = require('debug')('rcssdk:srvapi:conversation');
const CachedConversationId = new Map();
function storeConversationId(id, uri1, uri2) {
CachedConversationId.set([uri1.toString(), uri2.toString()], id);
}
function getOrNewConversationId(uri1, uri2) {
if (CachedConversationId.size > 0) {
let matchedId;
const iter = CachedConversationId.entries();
while (1) {
let { value, done } = iter.next();
if (done)
break;
let [key, id] = value;
if ((key[0] === uri1.toString() && key[1] === uri2.toString()) ||
(key[0] === uri2.toString() && key[1] === uri1.toString())) {
matchedId = id;
break;
}
}
if (matchedId)
return matchedId;
}
let newId = (0, nanoid_1.nanoid)();
storeConversationId(newId, uri1, uri2);
return newId;
}
function getAllConversationList(user, startContributionId, count = 20) {
return __awaiter(this, void 0, void 0, function* () {
let data;
if ((0, util_1.runAsMock)('conversation')) {
const { mockFilePathConversationList } = this.config;
data = yield (0, util_1.getMockData)(mockFilePathConversationList);
}
else {
try {
const post = { startContributionId, count };
if (user instanceof rcs_data_1.NameAddrHeader) {
post.user = user.value;
}
else {
post.user = user;
}
const cmd = `${this.config.msgApiServer}/bot/v1/getAllConversationList?access_token=${this.accessToken.value}`;
data = yield util_1.request
.post(cmd, post)
.then((r) => {
if (r['errorCode'] != 0) {
return Promise.reject({ message: r['errorMessage'] });
}
return r.data;
});
}
catch (error) {
return (0, util_1.newSdkResult)(constants_1.RequestFunc.GETALLCONVERSATIONLIST, null, 1001, error.message);
}
}
let convs;
if (data && Array.isArray(data) && data.length) {
convs = data.map((json) => {
let latestMsg = rcs_data_1.Parser.parseJSON(json);
let conv = rcs_data_1.RcsConversation.build(user, latestMsg);
storeConversationId(conv.id, conv.viewer.uri, conv.peer.uri);
return conv;
});
}
let sdkrst = (0, util_1.newSdkResult)(constants_1.RequestFunc.GETALLCONVERSATIONLIST, convs);
return Promise.resolve(sdkrst);
});
}
exports.getAllConversationList = getAllConversationList;
function createConversation(viewer, peer) {
if (typeof viewer === 'string')
viewer = rcs_data_1.NameAddrHeader.parse(viewer);
if (typeof peer === 'string')
peer = rcs_data_1.NameAddrHeader.parse(peer);
let id = getOrNewConversationId(viewer.uri, peer.uri);
let conv = rcs_data_1.RcsConversation.create(id, viewer, peer);
let sdkrst = (0, util_1.newSdkResult)(constants_1.RequestFunc.CREATECONVERSATION, conv);
return Promise.resolve(sdkrst);
}
exports.createConversation = createConversation;
//# sourceMappingURL=conversation.js.map