TAnyMessageModel -> TAnyMessage on MessageActions

This commit is contained in:
Diego Mello 2022-11-29 14:36:11 -03:00
parent edc67d424b
commit 0b7c075560
2 changed files with 35 additions and 30 deletions

View File

@ -10,12 +10,12 @@ import { useFrequentlyUsedEmoji } from '../../lib/hooks';
import CustomEmoji from '../EmojiPicker/CustomEmoji'; import CustomEmoji from '../EmojiPicker/CustomEmoji';
import { useDimensions } from '../../dimensions'; import { useDimensions } from '../../dimensions';
import sharedStyles from '../../views/Styles'; import sharedStyles from '../../views/Styles';
import { IEmoji, TAnyMessageModel } from '../../definitions'; import { IEmoji, TAnyMessage } from '../../definitions';
import Touch from '../Touch'; import Touch from '../Touch';
export interface IHeader { export interface IHeader {
handleReaction: (emoji: IEmoji, message: TAnyMessageModel) => void; handleReaction: (emoji: IEmoji, message: TAnyMessage) => void;
message: TAnyMessageModel; message: TAnyMessage;
isMasterDetail: boolean; isMasterDetail: boolean;
} }

View File

@ -5,6 +5,7 @@ import { connect } from 'react-redux';
import moment from 'moment'; import moment from 'moment';
import database from '../../lib/database'; import database from '../../lib/database';
import { getMessageById } from '../../lib/database/services/Message';
import I18n from '../../i18n'; import I18n from '../../i18n';
import log, { logEvent } from '../../lib/methods/helpers/log'; import log, { logEvent } from '../../lib/methods/helpers/log';
import Navigation from '../../lib/navigation/appNavigation'; import Navigation from '../../lib/navigation/appNavigation';
@ -15,7 +16,7 @@ import { showConfirmationAlert } from '../../lib/methods/helpers/info';
import { TActionSheetOptionsItem, useActionSheet, ACTION_SHEET_ANIMATION_DURATION } from '../ActionSheet'; import { TActionSheetOptionsItem, useActionSheet, ACTION_SHEET_ANIMATION_DURATION } from '../ActionSheet';
import Header, { HEADER_HEIGHT, IHeader } from './Header'; import Header, { HEADER_HEIGHT, IHeader } from './Header';
import events from '../../lib/methods/helpers/log/events'; import events from '../../lib/methods/helpers/log/events';
import { IApplicationState, IEmoji, ILoggedUser, TAnyMessageModel, TSubscriptionModel } from '../../definitions'; import { IApplicationState, IEmoji, ILoggedUser, TAnyMessage, TSubscriptionModel } from '../../definitions';
import { getPermalinkMessage } from '../../lib/methods'; import { getPermalinkMessage } from '../../lib/methods';
import { getRoomTitle, getUidDirectMessage, hasPermission } from '../../lib/methods/helpers'; import { getRoomTitle, getUidDirectMessage, hasPermission } from '../../lib/methods/helpers';
import { Services } from '../../lib/services'; import { Services } from '../../lib/services';
@ -24,10 +25,10 @@ export interface IMessageActionsProps {
room: TSubscriptionModel; room: TSubscriptionModel;
tmid?: string; tmid?: string;
user: Pick<ILoggedUser, 'id'>; user: Pick<ILoggedUser, 'id'>;
editInit: (message: TAnyMessageModel) => void; editInit: (message: TAnyMessage) => void;
reactionInit: (message: TAnyMessageModel) => void; reactionInit: (message: TAnyMessage) => void;
onReactionPress: (shortname: IEmoji, messageId: string) => void; onReactionPress: (shortname: IEmoji, messageId: string) => void;
replyInit: (message: TAnyMessageModel, mention: boolean) => void; replyInit: (message: TAnyMessage, mention: boolean) => void;
isMasterDetail: boolean; isMasterDetail: boolean;
isReadOnly: boolean; isReadOnly: boolean;
Message_AllowDeleting?: boolean; Message_AllowDeleting?: boolean;
@ -46,7 +47,7 @@ export interface IMessageActionsProps {
} }
export interface IMessageActions { export interface IMessageActions {
showMessageActions: (message: TAnyMessageModel) => Promise<void>; showMessageActions: (message: TAnyMessage) => Promise<void>;
} }
const MessageActions = React.memo( const MessageActions = React.memo(
@ -109,9 +110,9 @@ const MessageActions = React.memo(
} }
}; };
const isOwn = (message: TAnyMessageModel) => message.u && message.u._id === user.id; const isOwn = (message: TAnyMessage) => message.u && message.u._id === user.id;
const allowEdit = (message: TAnyMessageModel) => { const allowEdit = (message: TAnyMessage) => {
if (isReadOnly) { if (isReadOnly) {
return false; return false;
} }
@ -135,7 +136,7 @@ const MessageActions = React.memo(
return true; return true;
}; };
const allowDelete = (message: TAnyMessageModel) => { const allowDelete = (message: TAnyMessage) => {
if (isReadOnly) { if (isReadOnly) {
return false; return false;
} }
@ -166,19 +167,19 @@ const MessageActions = React.memo(
return true; return true;
}; };
const getPermalink = (message: TAnyMessageModel) => getPermalinkMessage(message); const getPermalink = (message: TAnyMessage) => getPermalinkMessage(message);
const handleReply = (message: TAnyMessageModel) => { const handleReply = (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_REPLY); logEvent(events.ROOM_MSG_ACTION_REPLY);
replyInit(message, true); replyInit(message, true);
}; };
const handleEdit = (message: TAnyMessageModel) => { const handleEdit = (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_EDIT); logEvent(events.ROOM_MSG_ACTION_EDIT);
editInit(message); editInit(message);
}; };
const handleCreateDiscussion = (message: TAnyMessageModel) => { const handleCreateDiscussion = (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_DISCUSSION); logEvent(events.ROOM_MSG_ACTION_DISCUSSION);
const params = { message, channel: room, showCloseModal: true }; const params = { message, channel: room, showCloseModal: true };
if (isMasterDetail) { if (isMasterDetail) {
@ -188,7 +189,7 @@ const MessageActions = React.memo(
} }
}; };
const handleUnread = async (message: TAnyMessageModel) => { const handleUnread = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_UNREAD); logEvent(events.ROOM_MSG_ACTION_UNREAD);
const { id: messageId, ts } = message; const { id: messageId, ts } = message;
const { rid } = room; const { rid } = room;
@ -213,7 +214,7 @@ const MessageActions = React.memo(
} }
}; };
const handlePermalink = async (message: TAnyMessageModel) => { const handlePermalink = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_PERMALINK); logEvent(events.ROOM_MSG_ACTION_PERMALINK);
try { try {
const permalink = await getPermalink(message); const permalink = await getPermalink(message);
@ -224,13 +225,13 @@ const MessageActions = React.memo(
} }
}; };
const handleCopy = async (message: TAnyMessageModel) => { const handleCopy = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_COPY); logEvent(events.ROOM_MSG_ACTION_COPY);
await Clipboard.setString((message?.attachments?.[0]?.description || message.msg) ?? ''); await Clipboard.setString((message?.attachments?.[0]?.description || message.msg) ?? '');
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') }); EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
}; };
const handleShare = async (message: TAnyMessageModel) => { const handleShare = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_SHARE); logEvent(events.ROOM_MSG_ACTION_SHARE);
try { try {
const permalink = await getPermalink(message); const permalink = await getPermalink(message);
@ -242,12 +243,12 @@ const MessageActions = React.memo(
} }
}; };
const handleQuote = (message: TAnyMessageModel) => { const handleQuote = (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_QUOTE); logEvent(events.ROOM_MSG_ACTION_QUOTE);
replyInit(message, false); replyInit(message, false);
}; };
const handleReplyInDM = async (message: TAnyMessageModel) => { const handleReplyInDM = async (message: TAnyMessage) => {
if (message?.u?.username) { if (message?.u?.username) {
const result = await Services.createDirectMessage(message.u.username); const result = await Services.createDirectMessage(message.u.username);
if (result.success) { if (result.success) {
@ -264,7 +265,7 @@ const MessageActions = React.memo(
} }
}; };
const handleStar = async (message: TAnyMessageModel) => { const handleStar = async (message: TAnyMessage) => {
logEvent(message.starred ? events.ROOM_MSG_ACTION_UNSTAR : events.ROOM_MSG_ACTION_STAR); logEvent(message.starred ? events.ROOM_MSG_ACTION_UNSTAR : events.ROOM_MSG_ACTION_STAR);
try { try {
await Services.toggleStarMessage(message.id, message.starred as boolean); // TODO: reevaluate `message.starred` type on IMessage await Services.toggleStarMessage(message.id, message.starred as boolean); // TODO: reevaluate `message.starred` type on IMessage
@ -275,7 +276,7 @@ const MessageActions = React.memo(
} }
}; };
const handlePin = async (message: TAnyMessageModel) => { const handlePin = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_PIN); logEvent(events.ROOM_MSG_ACTION_PIN);
try { try {
await Services.togglePinMessage(message.id, message.pinned as boolean); // TODO: reevaluate `message.pinned` type on IMessage await Services.togglePinMessage(message.id, message.pinned as boolean); // TODO: reevaluate `message.pinned` type on IMessage
@ -295,7 +296,7 @@ const MessageActions = React.memo(
hideActionSheet(); hideActionSheet();
}; };
const handleReadReceipt = (message: TAnyMessageModel) => { const handleReadReceipt = (message: TAnyMessage) => {
if (isMasterDetail) { if (isMasterDetail) {
Navigation.navigate('ModalStackNavigator', { screen: 'ReadReceiptsView', params: { messageId: message.id } }); Navigation.navigate('ModalStackNavigator', { screen: 'ReadReceiptsView', params: { messageId: message.id } });
} else { } else {
@ -303,14 +304,18 @@ const MessageActions = React.memo(
} }
}; };
const handleToggleTranslation = async (message: TAnyMessageModel) => { const handleToggleTranslation = async (message: TAnyMessage) => {
try { try {
if (!room.autoTranslateLanguage) { if (!room.autoTranslateLanguage) {
return; return;
} }
const db = database.active; const db = database.active;
const messageRecord = await getMessageById(message.id);
if (!messageRecord) {
return;
}
await db.write(async () => { await db.write(async () => {
await message.update(m => { await messageRecord.update(m => {
m.autoTranslate = !m.autoTranslate; m.autoTranslate = !m.autoTranslate;
m._updatedAt = new Date(); m._updatedAt = new Date();
}); });
@ -324,7 +329,7 @@ const MessageActions = React.memo(
} }
}; };
const handleReport = async (message: TAnyMessageModel) => { const handleReport = async (message: TAnyMessage) => {
logEvent(events.ROOM_MSG_ACTION_REPORT); logEvent(events.ROOM_MSG_ACTION_REPORT);
try { try {
await Services.reportMessage(message.id); await Services.reportMessage(message.id);
@ -335,7 +340,7 @@ const MessageActions = React.memo(
} }
}; };
const handleDelete = (message: TAnyMessageModel) => { const handleDelete = (message: TAnyMessage) => {
showConfirmationAlert({ showConfirmationAlert({
message: I18n.t('You_will_not_be_able_to_recover_this_message'), message: I18n.t('You_will_not_be_able_to_recover_this_message'),
confirmationText: I18n.t('Delete'), confirmationText: I18n.t('Delete'),
@ -351,7 +356,7 @@ const MessageActions = React.memo(
}); });
}; };
const getOptions = (message: TAnyMessageModel) => { const getOptions = (message: TAnyMessage) => {
const options: TActionSheetOptionsItem[] = []; const options: TActionSheetOptionsItem[] = [];
const videoConfBlock = message.t === 'videoconf'; const videoConfBlock = message.t === 'videoconf';
@ -487,7 +492,7 @@ const MessageActions = React.memo(
return options; return options;
}; };
const showMessageActions = async (message: TAnyMessageModel) => { const showMessageActions = async (message: TAnyMessage) => {
logEvent(events.ROOM_SHOW_MSG_ACTIONS); logEvent(events.ROOM_SHOW_MSG_ACTIONS);
await getPermissions(); await getPermissions();
showActionSheet({ showActionSheet({