UNPKG

@uimkit/uikit-react

Version:

<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>

189 lines (184 loc) 8.83 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib = require('tslib'); var invariant = require('../../node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/invariant.js'); require('redux'); var i18n = require('i18next'); require('../../types/models.js'); require('../../types/events.js'); require('../rootReducer.js'); require('../accounts/actions.js'); require('../accounts/selectors.js'); var actions$1 = require('../conversations/actions.js'); require('../../node_modules/.pnpm/lodash.flatten@4.4.0/node_modules/lodash.flatten/index.js'); require('../../node_modules/.pnpm/lodash.some@4.6.0/node_modules/lodash.some/index.js'); var actions = require('./actions.js'); var index = require('../../node_modules/.pnpm/lodash.last@3.0.0/node_modules/lodash.last/index.js'); require('../contacts/actions.js'); /** * 更新本地已经存在的消息 * * @param message * @returns */ var updateLocalMessage = function (message) { return function (dispatch, getState, context) { return tslib.__awaiter(void 0, void 0, void 0, function () { return tslib.__generator(this, function (_a) { dispatch({ type: actions.ConversationActionType.MESSAGE_UPDATE, payload: message, }); return [2 /*return*/]; }); }); }; }; /** * 删除消息(local) * * @param message * @returns */ var deleteLocalMessage = function (message) { return function (dispatch, getState, context) { return tslib.__awaiter(void 0, void 0, void 0, function () { var _a, conversations, messages, accountId, conversationId, conversation, lastMessage; var _b, _c, _d, _e, _f, _g; return tslib.__generator(this, function (_h) { dispatch({ type: actions.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 = index((_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: actions$1.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 tslib.__awaiter(void 0, void 0, void 0, function () { var client, onError, state, request, response, e_2; var _a; return tslib.__generator(this, function (_b) { switch (_b.label) { case 0: client = getState().common.client; onError = context.onError; invariant(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: actions.ConversationActionType.FETCHING_MESSAGE_LIST, payload: request, }); return [4 /*yield*/, client.getMessageList(request)]; case 2: response = _b.sent(); dispatch({ type: actions.ConversationActionType.MESSAGE_LIST_LOAD_MORE_NEWER_FINISHED, payload: { request: request, response: response }, }); return [3 /*break*/, 4]; case 3: e_2 = _b.sent(); dispatch({ type: actions.ConversationActionType.ERROR_FETCHING_MESSAGE_LIST, payload: { request: request, error: e_2 }, }); onError && onError(e_2, i18n.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 tslib.__awaiter(void 0, void 0, void 0, function () { var client, onError, state, request, response, e_3; var _a; return tslib.__generator(this, function (_b) { switch (_b.label) { case 0: client = getState().common.client; onError = context.onError; invariant(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: actions.ConversationActionType.FETCHING_MESSAGE_LIST, payload: request, }); return [4 /*yield*/, client.getMessageList(request)]; case 2: response = _b.sent(); dispatch({ type: actions.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: actions.ConversationActionType.ERROR_FETCHING_MESSAGE_LIST, payload: { request: request, error: e_3 }, }); onError && onError(e_3, i18n.t("default:chat:messages:fetchListError")); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; }; exports.deleteLocalMessage = deleteLocalMessage; exports.fetchConversationHistoryMessages = fetchConversationHistoryMessages; exports.fetchConversationNewMessages = fetchConversationNewMessages; exports.updateLocalMessage = updateLocalMessage; //# sourceMappingURL=commands.js.map