@uimkit/uikit-react
Version:
<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>
182 lines (179 loc) • 8.54 kB
JavaScript
import { __awaiter, __generator } from 'tslib';
import invariant_1 from '../../node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/invariant.js';
import 'redux';
import { t } from 'i18next';
import '../../types/models.js';
import '../../types/events.js';
import '../rootReducer.js';
import '../accounts/actions.js';
import '../accounts/selectors.js';
import { ConversationListActionType } from '../conversations/actions.js';
import '../../node_modules/.pnpm/lodash.flatten@4.4.0/node_modules/lodash.flatten/index.js';
import '../../node_modules/.pnpm/lodash.some@4.6.0/node_modules/lodash.some/index.js';
import { ConversationActionType } from './actions.js';
import lodash_last from '../../node_modules/.pnpm/lodash.last@3.0.0/node_modules/lodash.last/index.js';
import '../contacts/actions.js';
/**
* 更新本地已经存在的消息
*
* @param message
* @returns
*/
var updateLocalMessage = function (message) {
return function (dispatch, getState, context) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
dispatch({
type: ConversationActionType.MESSAGE_UPDATE,
payload: message,
});
return [2 /*return*/];
});
}); };
};
/**
* 删除消息(local)
*
* @param message
* @returns
*/
var deleteLocalMessage = function (message) {
return function (dispatch, getState, context) { return __awaiter(void 0, void 0, void 0, function () {
var _a, conversations, messages, accountId, conversationId, conversation, lastMessage;
var _b, _c, _d, _e, _f, _g;
return __generator(this, function (_h) {
dispatch({
type: ConversationActionType.MESSAGE_DELETED,
payload: message,
});
_a = getState(), conversations = _a.conversations, messages = _a.messages;
accountId = message.account;
conversationId = message.conversation_id;
conversation = (_c = (_b = conversations[accountId]) === null || _b === void 0 ? void 0 : _b.conversations) === null || _c === void 0 ? void 0 : _c.find(function (it) { return it.id === conversationId; });
if (((_d = conversation === null || conversation === void 0 ? void 0 : conversation.last_message) === null || _d === void 0 ? void 0 : _d.id) === message.id) {
lastMessage = lodash_last((_f = (_e = messages[conversationId]) === null || _e === void 0 ? void 0 : _e.messages) !== null && _f !== void 0 ? _f : []);
conversation.last_message = lastMessage;
conversation.active_at = (_g = lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.sent_at) !== null && _g !== void 0 ? _g : conversation.created_at;
dispatch({
type: ConversationListActionType.CONVERSATION_UPDATE,
payload: conversation
});
}
return [2 /*return*/];
});
}); };
};
/**
* 查询新消息
*
* @param conversationId 会话ID
*/
var fetchConversationNewMessages = function (conversationId, limit) {
if (limit === void 0) { limit = 20; }
return function (dispatch, getState, context) { return __awaiter(void 0, void 0, void 0, function () {
var client, onError, state, request, response, e_2;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
client = getState().common.client;
onError = context.onError;
invariant_1(client, "requires client");
state = getState().messages[conversationId];
if ((state === null || state === void 0 ? void 0 : state.loadingMore) || (state === null || state === void 0 ? void 0 : state.loadingMoreNewer))
return [2 /*return*/];
request = {
conversation_id: conversationId,
direction: 'before',
cursor: (_a = state === null || state === void 0 ? void 0 : state.prevCursor) !== null && _a !== void 0 ? _a : '',
limit: limit,
};
console.log("loadMoreNewer: cursor: ".concat(state === null || state === void 0 ? void 0 : state.prevCursor, ", limit: ").concat(limit));
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
dispatch({
type: ConversationActionType.FETCHING_MESSAGE_LIST,
payload: request,
});
return [4 /*yield*/, client.getMessageList(request)];
case 2:
response = _b.sent();
dispatch({
type: ConversationActionType.MESSAGE_LIST_LOAD_MORE_NEWER_FINISHED,
payload: { request: request, response: response },
});
return [3 /*break*/, 4];
case 3:
e_2 = _b.sent();
dispatch({
type: ConversationActionType.ERROR_FETCHING_MESSAGE_LIST,
payload: { request: request, error: e_2 },
});
onError && onError(e_2, t("default:chat:messages:fetchListError"));
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
};
/**
* 查询历史消息
*
* @param conversationId 会话ID
*/
var fetchConversationHistoryMessages = function (conversationId, limit) {
if (limit === void 0) { limit = 20; }
return function (dispatch, getState, context) { return __awaiter(void 0, void 0, void 0, function () {
var client, onError, state, request, response, e_3;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
client = getState().common.client;
onError = context.onError;
invariant_1(client, "requires client");
state = getState().messages[conversationId];
if ((state === null || state === void 0 ? void 0 : state.loadingMore) || (state === null || state === void 0 ? void 0 : state.loadingMoreNewer))
return [2 /*return*/];
request = {
conversation_id: conversationId,
direction: 'after',
cursor: (_a = state === null || state === void 0 ? void 0 : state.nextCursor) !== null && _a !== void 0 ? _a : '',
limit: limit,
};
console.log("loadMore: cursor: ".concat(state === null || state === void 0 ? void 0 : state.nextCursor, ", limit: ").concat(limit));
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
dispatch({
type: ConversationActionType.FETCHING_MESSAGE_LIST,
payload: request,
});
return [4 /*yield*/, client.getMessageList(request)];
case 2:
response = _b.sent();
dispatch({
type: ConversationActionType.MESSAGE_LIST_LOAD_MORE_FINISHED,
conversation: { id: conversationId },
payload: {
messages: response.data,
hasMore: response.extra.has_next,
nextCursor: response.extra.end_cursor,
}
});
return [3 /*break*/, 4];
case 3:
e_3 = _b.sent();
dispatch({
type: ConversationActionType.ERROR_FETCHING_MESSAGE_LIST,
payload: { request: request, error: e_3 },
});
onError && onError(e_3, t("default:chat:messages:fetchListError"));
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
};
export { deleteLocalMessage, fetchConversationHistoryMessages, fetchConversationNewMessages, updateLocalMessage };
//# sourceMappingURL=commands.js.map