Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/containers/MessageActions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface IMessageActionsProps {
editInit: (messageId: string) => void;
reactionInit: (messageId: string) => void;
onReactionPress: (shortname: IEmoji, messageId: string) => void;
replyInit: (message: TAnyMessageModel, mention: boolean) => void;
replyInit: (messageId: string) => void;
quoteInit: (messageId: string) => void;
isMasterDetail: boolean;
isReadOnly: boolean;
Expand Down Expand Up @@ -172,9 +172,9 @@ const MessageActions = React.memo(

const getPermalink = (message: TAnyMessageModel) => getPermalinkMessage(message);

const handleReply = (message: TAnyMessageModel) => {
const handleReply = (messageId: string) => {
logEvent(events.ROOM_MSG_ACTION_REPLY);
replyInit(message, true);
replyInit(messageId);
};

const handleEdit = (messageId: string) => {
Expand Down Expand Up @@ -270,7 +270,7 @@ const MessageActions = React.memo(
name: getRoomTitle(room),
t: room.t,
roomUserId: getUidDirectMessage(room),
replyInDM: message
replyInDM: message.id
};
Navigation.replace('RoomView', params);
}
Expand Down Expand Up @@ -382,7 +382,7 @@ const MessageActions = React.memo(
options.push({
title: I18n.t('Reply_in_Thread'),
icon: 'threads',
onPress: () => handleReply(message)
onPress: () => handleReply(message.id)
});
}

Expand Down
22 changes: 11 additions & 11 deletions app/containers/MessageComposer/MessageComposer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test('send message', async () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('test');
expect(onSendMessage).toHaveBeenCalledWith('test', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand Down Expand Up @@ -158,7 +158,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('**');
expect(onSendMessage).toHaveBeenCalledWith('**', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -177,7 +177,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('*test*');
expect(onSendMessage).toHaveBeenCalledWith('*test*', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -192,7 +192,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('__');
expect(onSendMessage).toHaveBeenCalledWith('__', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -211,7 +211,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('_test_');
expect(onSendMessage).toHaveBeenCalledWith('_test_', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -226,7 +226,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('~~');
expect(onSendMessage).toHaveBeenCalledWith('~~', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -245,7 +245,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('~test~');
expect(onSendMessage).toHaveBeenCalledWith('~test~', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -260,7 +260,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('``');
expect(onSendMessage).toHaveBeenCalledWith('``', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -279,7 +279,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('`test`');
expect(onSendMessage).toHaveBeenCalledWith('`test`', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -294,7 +294,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('``````');
expect(onSendMessage).toHaveBeenCalledWith('``````', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});

Expand All @@ -313,7 +313,7 @@ describe('Markdown', () => {
await fireEvent.press(screen.getByTestId('message-composer-send'));
});
expect(onSendMessage).toHaveBeenCalledTimes(1);
expect(onSendMessage).toHaveBeenCalledWith('```test```');
expect(onSendMessage).toHaveBeenCalledWith('```test```', undefined);
expect(screen.toJSON()).toMatchSnapshot();
});
});
Expand Down
18 changes: 12 additions & 6 deletions app/containers/MessageComposer/MessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { useBackHandler } from '@react-native-community/hooks';
import { Q } from '@nozbe/watermelondb';

import { useRoomContext } from '../../views/RoomView/context';
import { Autocomplete, Toolbar, EmojiSearchbar, ComposerInput, Left, Right } from './components';
import { Autocomplete, Toolbar, EmojiSearchbar, ComposerInput, Left, Right, Quotes, SendThreadToChannel } from './components';
import { MIN_HEIGHT, NO_CANNED_RESPONSES, TIMEOUT_CLOSE_EMOJI_KEYBOARD } from './constants';
import { MessageInnerContext, useMessageComposerApi, useShowEmojiKeyboard, useShowEmojiSearchbar } from './context';
import {
MessageInnerContext,
useAlsoSendThreadToChannel,
useMessageComposerApi,
useShowEmojiKeyboard,
useShowEmojiSearchbar
} from './context';
import { IAutocompleteItemProps, IComposerInput, ITrackingView } from './interfaces';
import { isIOS } from '../../lib/methods/helpers';
import shortnameToUnicode from '../../lib/methods/helpers/shortnameToUnicode';
Expand All @@ -24,7 +30,6 @@ import log, { events, logEvent } from '../../lib/methods/helpers/log';
import { fetchIsAllOrHere, prepareQuoteMessage } from './helpers';
import Navigation from '../../lib/navigation/appNavigation';
import { emitter } from './emitter';
import { Quotes } from './components/Quotes';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -54,6 +59,7 @@ export const MessageComposer = ({ forwardedRef }: { forwardedRef: any }): ReactE
const { rid, tmid, action, selectedMessages, editRequest, onSendMessage } = useRoomContext();
const showEmojiKeyboard = useShowEmojiKeyboard();
const showEmojiSearchbar = useShowEmojiSearchbar();
const alsoSendThreadToChannel = useAlsoSendThreadToChannel();
const { setKeyboardHeight, openSearchEmojiKeyboard, closeEmojiKeyboard, closeSearchEmojiKeyboard } = useMessageComposerApi();
const isMasterDetail = useAppSelector(state => state.app.isMasterDetail);

Expand Down Expand Up @@ -121,8 +127,7 @@ export const MessageComposer = ({ forwardedRef }: { forwardedRef: any }): ReactE
const messageWithoutCommand = textFromInput.replace(/([^\s]+)/, '').trim();
const [{ appId }] = slashCommand;
const triggerId = generateTriggerId(appId);
await Services.runSlashCommand(command, rid, messageWithoutCommand, triggerId, tmid); // || messageTmid);
// replyCancel();
await Services.runSlashCommand(command, rid, messageWithoutCommand, triggerId, tmid);
} catch (e) {
log(e);
}
Expand All @@ -131,7 +136,7 @@ export const MessageComposer = ({ forwardedRef }: { forwardedRef: any }): ReactE
}

// Text message
onSendMessage(textFromInput);
onSendMessage(textFromInput, alsoSendThreadToChannel);
};

const onKeyboardItemSelected = (_keyboardId: string, params: { eventType: EventTypes; emoji: IEmoji }) => {
Expand Down Expand Up @@ -283,6 +288,7 @@ export const MessageComposer = ({ forwardedRef }: { forwardedRef: any }): ReactE
<Quotes />
<Toolbar />
<EmojiSearchbar />
<SendThreadToChannel />
</View>
);
}, []);
Expand Down
5 changes: 3 additions & 2 deletions app/containers/MessageComposer/components/ComposerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export const ComposerInput = memo(
const selectionRef = React.useRef<IInputSelection>(defaultSelection);
const dispatch = useDispatch();
const subscription = useSubscription(rid);
let placeholder = 'Message ';
if (subscription) {
// TODO: i18n
let placeholder = tmid ? 'Add thread reply' : 'Message ';
Comment thread
diegolmello marked this conversation as resolved.
Resolved
if (subscription && !tmid) {
placeholder += subscription.t === 'd' ? '@' : '#';
placeholder += getRoomTitle(subscription);
}
Expand Down
89 changes: 89 additions & 0 deletions app/containers/MessageComposer/components/SendThreadToChannel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
import { Text } from 'react-native';
import React, { useEffect, useRef } from 'react';
import { Subscription } from 'rxjs';
import { Q } from '@nozbe/watermelondb';

import { useRoomContext } from '../../../views/RoomView/context';
import { useAlsoSendThreadToChannel, useMessageComposerApi } from '../context';
import { CustomIcon } from '../../CustomIcon';
import { useTheme } from '../../../theme';
import sharedStyles from '../../../views/Styles';
import I18n from '../../../i18n';
import { useAppSelector } from '../../../lib/hooks';
import database from '../../../lib/database';
import { compareServerVersion } from '../../../lib/methods/helpers';

export const SendThreadToChannel = (): React.ReactElement | null => {
const alsoSendThreadToChannel = useAlsoSendThreadToChannel();
const { setAlsoSendThreadToChannel } = useMessageComposerApi();
const { tmid } = useRoomContext();
const { colors } = useTheme();
const subscription = useRef<Subscription>();
const alsoSendThreadToChannelUserPref = useAppSelector(state => state.login.user.alsoSendThreadToChannel);
const serverVersion = useAppSelector(state => state.server.version);

useEffect(() => {
if (!tmid) {
return;
}

if (compareServerVersion(serverVersion, 'lowerThan', '5.0.0')) {
setAlsoSendThreadToChannel(false);
return;
}

if (alsoSendThreadToChannelUserPref === 'always') {
setAlsoSendThreadToChannel(true);
return;
}
if (alsoSendThreadToChannelUserPref === 'never') {
setAlsoSendThreadToChannel(false);
return;
}

/**
* "default" sends a to channel only in the first message of the thread.
* We check if the thread exists by observing/subscribing to the query with tmid.
* If it doesn't exist, it means that this is the first message of the thread. So it's true.
* Otherwise, it's false.
* */
if (alsoSendThreadToChannelUserPref === 'default') {
const db = database.active;
const observable = db.get('threads').query(Q.where('tmid', tmid)).observe();

subscription.current = observable.subscribe(result => {
setAlsoSendThreadToChannel(!result.length);
});
}

return () => {
subscription.current?.unsubscribe();
};
}, [tmid, alsoSendThreadToChannelUserPref, serverVersion, setAlsoSendThreadToChannel]);

if (!tmid) {
return null;
}

return (
<TouchableWithoutFeedback
style={{
flexDirection: 'row',
alignItems: 'center'
}}
onPress={() => setAlsoSendThreadToChannel(!alsoSendThreadToChannel)}
testID='composer-send-to-channel'
>
<CustomIcon
testID={alsoSendThreadToChannel ? 'send-to-channel-checked' : 'send-to-channel-unchecked'}
name={alsoSendThreadToChannel ? 'checkbox-checked' : 'checkbox-unchecked'}
size={24}
color={alsoSendThreadToChannel ? colors.buttonBackgroundPrimaryDefault : colors.strokeDark}
/>
<Text style={{ fontSize: 14, marginLeft: 8, ...sharedStyles.textRegular, color: colors.fontSecondaryInfo }}>
{I18n.t('Messagebox_Send_to_channel')}
</Text>
</TouchableWithoutFeedback>
);
};
2 changes: 2 additions & 0 deletions app/containers/MessageComposer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './ComposerInput';
export * from './EmojiSearchbar';
export * from './Toolbar';
export * from './Unfocused';
export * from './Quotes';
export * from './SendThreadToChannel';
19 changes: 16 additions & 3 deletions app/containers/MessageComposer/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type TMessageComposerContextApi = {
setFocused(focused: boolean): void;
setMicOrSend(micOrSend: TMicOrSend): void;
setMarkdownToolbar(showMarkdownToolbar: boolean): void;
setAlsoSendThreadToChannel(alsoSendThreadToChannel: boolean): void;
};

const FocusedContext = createContext<State['focused']>({} as State['focused']);
Expand All @@ -22,6 +23,7 @@ const ShowEmojiKeyboardContext = createContext<State['showEmojiKeyboard']>({} as
const ShowEmojiSearchbarContext = createContext<State['showEmojiSearchbar']>({} as State['showEmojiSearchbar']);
const KeyboardHeightContext = createContext<State['keyboardHeight']>({} as State['keyboardHeight']);
const TrackingViewHeightContext = createContext<State['trackingViewHeight']>({} as State['trackingViewHeight']);
const AlsoSendThreadToChannelContext = createContext<State['alsoSendThreadToChannel']>({} as State['alsoSendThreadToChannel']);
const MessageComposerContextApi = createContext<TMessageComposerContextApi>({} as TMessageComposerContextApi);

export const useMessageComposerApi = (): TMessageComposerContextApi => useContext(MessageComposerContextApi);
Expand All @@ -32,6 +34,7 @@ export const useShowEmojiKeyboard = (): State['showEmojiKeyboard'] => useContext
export const useShowEmojiSearchbar = (): State['showEmojiSearchbar'] => useContext(ShowEmojiSearchbarContext);
export const useKeyboardHeight = (): State['keyboardHeight'] => useContext(KeyboardHeightContext);
export const useTrackingViewHeight = (): State['trackingViewHeight'] => useContext(TrackingViewHeightContext);
export const useAlsoSendThreadToChannel = (): State['alsoSendThreadToChannel'] => useContext(AlsoSendThreadToChannelContext);

// TODO: rename
type TMessageInnerContext = {
Expand All @@ -56,6 +59,7 @@ type State = {
keyboardHeight: number;
micOrSend: TMicOrSend;
showMarkdownToolbar: boolean;
alsoSendThreadToChannel: boolean;
};

type Actions =
Expand All @@ -69,7 +73,8 @@ type Actions =
| { type: 'openSearchEmojiKeyboard' }
| { type: 'closeSearchEmojiKeyboard' }
| { type: 'setMicOrSend'; micOrSend: TMicOrSend }
| { type: 'setMarkdownToolbar'; showMarkdownToolbar: boolean };
| { type: 'setMarkdownToolbar'; showMarkdownToolbar: boolean }
| { type: 'setAlsoSendThreadToChannel'; alsoSendThreadToChannel: boolean };

const reducer = (state: State, action: Actions): State => {
switch (action.type) {
Expand All @@ -95,6 +100,8 @@ const reducer = (state: State, action: Actions): State => {
return { ...state, micOrSend: action.micOrSend };
case 'setMarkdownToolbar':
return { ...state, showMarkdownToolbar: action.showMarkdownToolbar };
case 'setAlsoSendThreadToChannel':
return { ...state, alsoSendThreadToChannel: action.alsoSendThreadToChannel };
}
};

Expand All @@ -121,6 +128,9 @@ export const MessageComposerProvider = ({ children }: { children: ReactElement }

const setMarkdownToolbar = (showMarkdownToolbar: boolean) => dispatch({ type: 'setMarkdownToolbar', showMarkdownToolbar });

const setAlsoSendThreadToChannel = (alsoSendThreadToChannel: boolean) =>
dispatch({ type: 'setAlsoSendThreadToChannel', alsoSendThreadToChannel });

return {
setFocused,
setKeyboardHeight,
Expand All @@ -130,7 +140,8 @@ export const MessageComposerProvider = ({ children }: { children: ReactElement }
openSearchEmojiKeyboard,
closeSearchEmojiKeyboard,
setMicOrSend,
setMarkdownToolbar
setMarkdownToolbar,
setAlsoSendThreadToChannel
};
}, []);

Expand All @@ -142,7 +153,9 @@ export const MessageComposerProvider = ({ children }: { children: ReactElement }
<KeyboardHeightContext.Provider value={state.keyboardHeight}>
<TrackingViewHeightContext.Provider value={state.trackingViewHeight}>
<ShowMarkdownToolbarContext.Provider value={state.showMarkdownToolbar}>
<MicOrSendContext.Provider value={state.micOrSend}>{children}</MicOrSendContext.Provider>
<AlsoSendThreadToChannelContext.Provider value={state.alsoSendThreadToChannel}>
<MicOrSendContext.Provider value={state.micOrSend}>{children}</MicOrSendContext.Provider>
</AlsoSendThreadToChannelContext.Provider>
</ShowMarkdownToolbarContext.Provider>
</TrackingViewHeightContext.Provider>
</KeyboardHeightContext.Provider>
Expand Down
Loading