UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

799 lines 24.3 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useChannelActions = exports.getOtherUserInDirectChannel = exports.getNotificationErrorOptions = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _react = require("react"); var _hooks = require("../../components/Notifications/hooks"); var _contexts = require("../../contexts"); var _useStableCallback = require("../useStableCallback"); var getOtherUserInDirectChannel = channel => { var members = Object.values(channel.state.members); return members.length === 2 ? members.find(member => { var _member$user; return ((_member$user = member.user) == null ? void 0 : _member$user.id) !== channel.getClient().userID; }) : undefined; }; exports.getOtherUserInDirectChannel = getOtherUserInDirectChannel; var getNotificationError = error => { if (error instanceof Error) return error; if (typeof error === 'string') return new Error(error); if (error && typeof error === 'object' && 'message' in error) { var message = error.message; if (typeof message === 'string') return new Error(message); } return undefined; }; var getNotificationErrorOptions = error => { var originalError = getNotificationError(error); return originalError ? { originalError } : {}; }; exports.getNotificationErrorOptions = getNotificationErrorOptions; var useChannelActions = channel => { var _useChatContext = (0, _contexts.useChatContext)(), client = _useChatContext.client; var _useNotificationApi = (0, _hooks.useNotificationApi)(), addNotification = _useNotificationApi.addNotification; var _useTranslationContex = (0, _contexts.useTranslationContext)(), t = _useTranslationContex.t; var ownUserId = client.userID; var pin = (0, _useStableCallback.useStableCallback)(function () { var _ref = (0, _asyncToGenerator2.default)(function* (options) { try { if (!channel) { return; } yield channel.pin(); addNotification({ message: t('Channel pinned'), options: { severity: 'success', type: 'api:channel:pin:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel pinned status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:pin:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x) { return _ref.apply(this, arguments); }; }()); var unpin = (0, _useStableCallback.useStableCallback)(function () { var _ref2 = (0, _asyncToGenerator2.default)(function* (options) { try { if (!channel) { return; } yield channel.unpin(); addNotification({ message: t('Channel unpinned'), options: { severity: 'success', type: 'api:channel:unpin:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel pinned status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:pin:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x2) { return _ref2.apply(this, arguments); }; }()); var archive = (0, _useStableCallback.useStableCallback)(function () { var _ref3 = (0, _asyncToGenerator2.default)(function* (options) { try { if (!channel) { return; } yield channel.archive(); addNotification({ message: t('Channel archived'), options: { severity: 'success', type: 'api:channel:archive:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel archive status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:archive:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x3) { return _ref3.apply(this, arguments); }; }()); var unarchive = (0, _useStableCallback.useStableCallback)(function () { var _ref4 = (0, _asyncToGenerator2.default)(function* (options) { try { if (!channel) { return; } yield channel.unarchive(); addNotification({ message: t('Channel unarchived'), options: { severity: 'success', type: 'api:channel:unarchive:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel archive status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:archive:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x4) { return _ref4.apply(this, arguments); }; }()); var leave = (0, _useStableCallback.useStableCallback)(function () { var _ref5 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } if (ownUserId) { try { yield channel.removeMembers([ownUserId]); addNotification({ message: t('Left channel'), options: { severity: 'success', type: 'api:channel:leave:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to leave channel'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:leave:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } } }); return function (_x5) { return _ref5.apply(this, arguments); }; }()); var removeMembers = (0, _useStableCallback.useStableCallback)(function () { var _ref6 = (0, _asyncToGenerator2.default)(function* (memberIds, options) { if (!channel) { return; } try { yield channel.removeMembers(memberIds); addNotification({ message: t('{{count}} members removed', { count: memberIds.length }), options: { severity: 'success', type: 'api:channel:remove-members:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to remove members'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:remove-members:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x6, _x7) { return _ref6.apply(this, arguments); }; }()); var addMembers = (0, _useStableCallback.useStableCallback)(function () { var _ref7 = (0, _asyncToGenerator2.default)(function* (memberIds, options) { if (!channel) { return; } try { yield channel.addMembers(memberIds); addNotification({ message: t('{{count}} members added', { count: memberIds.length }), options: { severity: 'success', type: 'api:channel:add-members:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to add members'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:add-members:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x8, _x9) { return _ref7.apply(this, arguments); }; }()); var deleteChannel = (0, _useStableCallback.useStableCallback)(function () { var _ref8 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } try { yield channel.delete(); addNotification({ message: t('Channel deleted'), options: { severity: 'success', type: 'api:channel:delete:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to delete channel'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:delete:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x0) { return _ref8.apply(this, arguments); }; }()); var muteUser = (0, _useStableCallback.useStableCallback)(function () { var _ref9 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } var otherUser = getOtherUserInDirectChannel(channel); try { var _otherUser$user; if (otherUser != null && (_otherUser$user = otherUser.user) != null && _otherUser$user.id) { yield client.muteUser(otherUser.user.id); addNotification({ message: t('{{ user }} has been muted', { user: otherUser.user.name || otherUser.user.id }), options: { severity: 'success', type: 'api:user:mute:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } } catch (error) { addNotification({ message: t('Error muting a user ...'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:user:mute:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x1) { return _ref9.apply(this, arguments); }; }()); var unmuteUser = (0, _useStableCallback.useStableCallback)(function () { var _ref0 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } var otherUser = getOtherUserInDirectChannel(channel); try { var _otherUser$user2; if (otherUser != null && (_otherUser$user2 = otherUser.user) != null && _otherUser$user2.id) { yield client.unmuteUser(otherUser.user.id); addNotification({ message: t('{{ user }} has been unmuted', { user: otherUser.user.name || otherUser.user.id }), options: { severity: 'success', type: 'api:user:unmute:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } } catch (error) { addNotification({ message: t('Error unmuting a user ...'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:user:unmute:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x10) { return _ref0.apply(this, arguments); }; }()); var muteChannel = (0, _useStableCallback.useStableCallback)(function () { var _ref1 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } try { yield channel.mute(); addNotification({ message: t('Channel muted'), options: { severity: 'success', type: 'api:channel:mute:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel mute status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:mute:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x11) { return _ref1.apply(this, arguments); }; }()); var unmuteChannel = (0, _useStableCallback.useStableCallback)(function () { var _ref10 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } try { yield channel.unmute(); addNotification({ message: t('Channel unmuted'), options: { severity: 'success', type: 'api:channel:unmute:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel mute status'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:mute:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x12) { return _ref10.apply(this, arguments); }; }()); var blockUser = (0, _useStableCallback.useStableCallback)(function () { var _ref11 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } var otherUser = getOtherUserInDirectChannel(channel); try { var _otherUser$user3; if (otherUser != null && (_otherUser$user3 = otherUser.user) != null && _otherUser$user3.id) { yield client.blockUser(otherUser.user.id); addNotification({ message: t('User blocked'), options: { severity: 'success', type: 'api:user:block:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } } catch (error) { addNotification({ message: t('Failed to block user'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:user:block:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x13) { return _ref11.apply(this, arguments); }; }()); var updateName = (0, _useStableCallback.useStableCallback)(function () { var _ref12 = (0, _asyncToGenerator2.default)(function* (name, options) { if (!channel) { return; } try { if (name.trim() === '') { yield channel.updatePartial({ unset: ['name'] }); } else { yield channel.updatePartial({ set: { name } }); } addNotification({ message: t('Channel name updated'), options: { severity: 'success', type: 'api:channel:update-name:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel name'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:update-name:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x14, _x15) { return _ref12.apply(this, arguments); }; }()); var updateImage = (0, _useStableCallback.useStableCallback)(function () { var _ref13 = (0, _asyncToGenerator2.default)(function* (image, options, doFileUploadRequest) { if (!channel) { return; } try { if (image === null) { yield channel.updatePartial({ unset: ['image'] }); } else { var _ref14 = doFileUploadRequest ? yield doFileUploadRequest(image) : yield client.uploadImage(image.uri, image.name, image.type), file = _ref14.file; yield channel.updatePartial({ set: { image: file } }); } addNotification({ message: t('Channel image updated'), options: { severity: 'success', type: 'api:channel:update-image:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } catch (error) { addNotification({ message: t('Failed to update channel image'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:channel:update-image:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x16, _x17, _x18) { return _ref13.apply(this, arguments); }; }()); var unblockUser = (0, _useStableCallback.useStableCallback)(function () { var _ref15 = (0, _asyncToGenerator2.default)(function* (options) { if (!channel) { return; } var otherUser = getOtherUserInDirectChannel(channel); try { var _otherUser$user4; if (otherUser != null && (_otherUser$user4 = otherUser.user) != null && _otherUser$user4.id) { yield client.unBlockUser(otherUser.user.id); addNotification({ message: t('User unblocked'), options: { severity: 'success', type: 'api:user:unblock:success' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onSuccess == null || options.onSuccess(); } } catch (error) { addNotification({ message: t('Failed to block user'), options: { ...getNotificationErrorOptions(error), severity: 'error', type: 'api:user:block:failed' }, origin: { context: { channel }, emitter: 'ChannelActions' } }); options == null || options.onFailure == null || options.onFailure(error); } }); return function (_x19) { return _ref15.apply(this, arguments); }; }()); return (0, _react.useMemo)(() => ({ addMembers, removeMembers, pin, unpin, archive, unarchive, leave, deleteChannel, updateName, updateImage, muteUser, unmuteUser, muteChannel, unmuteChannel, blockUser, unblockUser }), [addMembers, removeMembers, pin, unpin, archive, unarchive, leave, deleteChannel, updateName, updateImage, muteUser, unmuteUser, muteChannel, unmuteChannel, blockUser, unblockUser]); }; exports.useChannelActions = useChannelActions; //# sourceMappingURL=useChannelActions.js.map