Skip to content

Commit

Permalink
Message: Send inverted media (#4700)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Zinchuk <[email protected]>
  • Loading branch information
Ajaxy committed Jun 18, 2024
1 parent 6f79159 commit 47e6743
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 132 deletions.
4 changes: 4 additions & 0 deletions src/api/gramjs/apiBuilders/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ export function buildLocalMessage(
scheduledAt?: number,
sendAs?: ApiPeer,
story?: ApiStory | ApiStorySkipped,
isInvertedMedia?: true,
): ApiMessage {
const localId = getNextLocalMessageId(lastMessageId);
const media = attachment && buildUploadingMedia(attachment);
Expand Down Expand Up @@ -835,6 +836,7 @@ export function buildLocalMessage(
}),
...(scheduledAt && { isScheduled: true }),
isForwardingAllowed: true,
isInvertedMedia,
} satisfies ApiMessage;

const emojiOnlyCount = getEmojiOnlyCountForMessage(message.content, message.groupedId);
Expand Down Expand Up @@ -872,6 +874,7 @@ export function buildLocalForwardedMessage({
senderId,
groupedId,
isInAlbum,
isInvertedMedia,
} = message;

const isAudio = content.audio;
Expand Down Expand Up @@ -912,6 +915,7 @@ export function buildLocalForwardedMessage({
isInAlbum,
isForwardingAllowed: true,
replyInfo,
isInvertedMedia,
...(toThreadId && toChat?.isForum && { isTopicReply: true }),

...(emojiOnlyCount && { emojiOnlyCount }),
Expand Down
8 changes: 8 additions & 0 deletions src/api/gramjs/methods/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export function sendMessage(
sendAs,
shouldUpdateStickerSetOrder,
wasDrafted,
isInvertedMedia,
}: {
chat: ApiChat;
lastMessageId?: number;
Expand All @@ -288,6 +289,7 @@ export function sendMessage(
sendAs?: ApiPeer;
shouldUpdateStickerSetOrder?: boolean;
wasDrafted?: boolean;
isInvertedMedia?: true;
},
onProgress?: ApiOnProgress,
) {
Expand All @@ -306,6 +308,7 @@ export function sendMessage(
scheduledAt,
sendAs,
story,
isInvertedMedia,
);

onUpdate({
Expand Down Expand Up @@ -392,6 +395,7 @@ export function sendMessage(
...(noWebPage && { noWebpage: noWebPage }),
...(sendAs && { sendAs: buildInputPeer(sendAs.id, sendAs.accessHash) }),
...(shouldUpdateStickerSetOrder && { updateStickersetsOrder: shouldUpdateStickerSetOrder }),
...(isInvertedMedia && { invertMedia: isInvertedMedia }),
}), {
shouldThrow: true,
shouldIgnoreUpdates: true,
Expand Down Expand Up @@ -585,6 +589,8 @@ export async function editMessage({

const media = attachment && buildUploadingMedia(attachment);

const isInvertedMedia = text && !attachment?.shouldSendAsFile ? message.isInvertedMedia : undefined;

const newContent = {
...(media || message.content),
...(text && {
Expand All @@ -599,6 +605,7 @@ export async function editMessage({
...message,
content: newContent,
emojiOnlyCount: getEmojiOnlyCountForMessage(newContent, message.groupedId),
isInvertedMedia,
};

onUpdate({
Expand All @@ -624,6 +631,7 @@ export async function editMessage({
id: message.id,
...(isScheduled && { scheduleDate: message.date }),
...(noWebPage && { noWebpage: noWebPage }),
...(isInvertedMedia && { invertMedia: isInvertedMedia }),
}), { shouldThrow: true });
} catch (err) {
if (DEBUG) {
Expand Down
1 change: 1 addition & 0 deletions src/assets/font-icons/move-caption-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/font-icons/move-caption-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 37 additions & 13 deletions src/components/common/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,14 @@ const Composer: FC<OwnProps & StateProps> = ({
sendGrouped = attachmentSettings.shouldSendGrouped,
isSilent,
scheduledAt,
isInvertedMedia,
}: {
attachments: ApiAttachment[];
sendCompressed?: boolean;
sendGrouped?: boolean;
isSilent?: boolean;
scheduledAt?: number;
isInvertedMedia?: true;
}) => {
if (!currentMessageList && !storyId) {
return;
Expand All @@ -905,6 +907,8 @@ const Composer: FC<OwnProps & StateProps> = ({
if (!validateTextLength(text, true)) return;
if (!checkSlowMode()) return;

isInvertedMedia = text && sendCompressed && sendGrouped ? isInvertedMedia : undefined;

if (editingMessage) {
editMessage({
messageList: currentMessageList,
Expand All @@ -922,6 +926,7 @@ const Composer: FC<OwnProps & StateProps> = ({
shouldUpdateStickerSetOrder,
attachments: prepareAttachmentsToSend(attachmentsToSend, sendCompressed),
shouldGroupMessages: sendGrouped,
isInvertedMedia,
});
}

Expand All @@ -935,18 +940,33 @@ const Composer: FC<OwnProps & StateProps> = ({
});
});

const handleSendAttachmentsFromModal = useLastCallback((
sendCompressed: boolean,
sendGrouped: boolean,
isInvertedMedia?: true,
) => {
sendAttachments({
attachments,
sendCompressed,
sendGrouped,
isInvertedMedia,
});
});

const handleSendAttachments = useLastCallback((
sendCompressed: boolean,
sendGrouped: boolean,
isSilent?: boolean,
scheduledAt?: number,
isInvertedMedia?: true,
) => {
sendAttachments({
attachments,
sendCompressed,
sendGrouped,
isSilent,
scheduledAt,
isInvertedMedia,
});
});

Expand Down Expand Up @@ -1059,8 +1079,8 @@ const Composer: FC<OwnProps & StateProps> = ({
if (!args || Object.keys(restArgs).length === 0) {
void handleSend(Boolean(isSilent), scheduledAt);
} else if (args.sendCompressed !== undefined || args.sendGrouped !== undefined) {
const { sendCompressed = false, sendGrouped = false } = args;
void handleSendAttachments(sendCompressed, sendGrouped, isSilent, scheduledAt);
const { sendCompressed = false, sendGrouped = false, isInvertedMedia } = args;
void handleSendAttachments(sendCompressed, sendGrouped, isSilent, scheduledAt, isInvertedMedia);
} else {
sendMessage({
...args,
Expand Down Expand Up @@ -1237,8 +1257,8 @@ const Composer: FC<OwnProps & StateProps> = ({
handleMessageSchedule({ ...additionalArgs, isSilent: true }, scheduledAt, currentMessageList!);
});
} else if (additionalArgs && ('sendCompressed' in additionalArgs || 'sendGrouped' in additionalArgs)) {
const { sendCompressed = false, sendGrouped = false } = additionalArgs;
void handleSendAttachments(sendCompressed, sendGrouped, true);
const { sendCompressed = false, sendGrouped = false, isInvertedMedia } = additionalArgs;
void handleSendAttachments(sendCompressed, sendGrouped, true, undefined, isInvertedMedia);
} else {
void handleSend(true);
}
Expand Down Expand Up @@ -1490,15 +1510,19 @@ const Composer: FC<OwnProps & StateProps> = ({
handleMessageSchedule({}, SCHEDULED_WHEN_ONLINE, currentMessageList!);
});

const handleSendScheduledAttachments = useLastCallback((sendCompressed: boolean, sendGrouped: boolean) => {
requestCalendar((scheduledAt) => {
handleMessageSchedule({ sendCompressed, sendGrouped }, scheduledAt, currentMessageList!);
});
});
const handleSendScheduledAttachments = useLastCallback(
(sendCompressed: boolean, sendGrouped: boolean, isInvertedMedia?: true) => {
requestCalendar((scheduledAt) => {
handleMessageSchedule({ sendCompressed, sendGrouped, isInvertedMedia }, scheduledAt, currentMessageList!);
});
},
);

const handleSendSilentAttachments = useLastCallback((sendCompressed: boolean, sendGrouped: boolean) => {
sendSilent({ sendCompressed, sendGrouped });
});
const handleSendSilentAttachments = useLastCallback(
(sendCompressed: boolean, sendGrouped: boolean, isInvertedMedia?: true) => {
sendSilent({ sendCompressed, sendGrouped, isInvertedMedia });
},
);

const onSend = useMemo(() => {
switch (mainButtonState) {
Expand Down Expand Up @@ -1557,7 +1581,7 @@ const Composer: FC<OwnProps & StateProps> = ({
forceDarkTheme={isInStoryViewer}
onCaptionUpdate={onCaptionUpdate}
onSendSilent={handleSendSilentAttachments}
onSend={handleSendAttachments}
onSend={handleSendAttachmentsFromModal}
onSendScheduled={handleSendScheduledAttachments}
onFileAppend={handleAppendFiles}
onClear={handleClearAttachments}
Expand Down
32 changes: 28 additions & 4 deletions src/components/middle/composer/AttachmentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ export type OwnProps = {
isForCurrentMessageList?: boolean;
forceDarkTheme?: boolean;
onCaptionUpdate: (html: string) => void;
onSend: (sendCompressed: boolean, sendGrouped: boolean) => void;
onSend: (sendCompressed: boolean, sendGrouped: boolean, isInvertedMedia?: true) => void;
onFileAppend: (files: File[], isSpoiler?: boolean) => void;
onAttachmentsUpdate: (attachments: ApiAttachment[]) => void;
onClear: NoneToVoidFunction;
onSendSilent: (sendCompressed: boolean, sendGrouped: boolean) => void;
onSendScheduled: (sendCompressed: boolean, sendGrouped: boolean) => void;
onSendSilent: (sendCompressed: boolean, sendGrouped: boolean, isInvertedMedia?: true) => void;
onSendScheduled: (sendCompressed: boolean, sendGrouped: boolean, isInvertedMedia?: true) => void;
onCustomEmojiSelect: (emoji: ApiSticker) => void;
onRemoveSymbol: VoidFunction;
onEmojiSelect: (emoji: string) => void;
Expand Down Expand Up @@ -167,6 +167,7 @@ const AttachmentModal: FC<OwnProps & StateProps> = ({
(shouldSendCompressed || shouldForceCompression || isInAlbum) && !shouldForceAsFile,
);
const [shouldSendGrouped, setShouldSendGrouped] = useState(attachmentSettings.shouldSendGrouped);
const [isInvertedMedia, setIsInvertedMedia] = useState(attachmentSettings.isInvertedMedia);

const {
handleScroll: handleAttachmentsScroll,
Expand Down Expand Up @@ -253,6 +254,7 @@ const AttachmentModal: FC<OwnProps & StateProps> = ({
if (isOpen) {
setShouldSendCompressed(shouldSuggestCompression ?? attachmentSettings.shouldCompress);
setShouldSendGrouped(attachmentSettings.shouldSendGrouped);
setIsInvertedMedia(attachmentSettings.isInvertedMedia);
}
}, [attachmentSettings, isOpen, shouldSuggestCompression]);

Expand All @@ -273,10 +275,11 @@ const AttachmentModal: FC<OwnProps & StateProps> = ({
if (isOpen) {
const send = ((shouldSchedule || shouldSendScheduled) && isForMessage && !editingMessage) ? onSendScheduled
: isSilent ? onSendSilent : onSend;
send(isSendingCompressed, shouldSendGrouped);
send(isSendingCompressed, shouldSendGrouped, isInvertedMedia);
updateAttachmentSettings({
shouldCompress: shouldSuggestCompression === undefined ? isSendingCompressed : undefined,
shouldSendGrouped,
isInvertedMedia,
});
}
});
Expand Down Expand Up @@ -432,6 +435,14 @@ const AttachmentModal: FC<OwnProps & StateProps> = ({

const isMultiple = renderingAttachments.length > 1;

const canInvertMedia = (() => {
if (isEditing) return false;
if (!hasMedia) return false;
if (!shouldForceAsFile && !shouldForceCompression && !isSendingCompressed) return false;
if (isMultiple && shouldSendGrouped) return false;
return true;
})();

let title = '';
if (areAllPhotos) {
title = lang(isEditing ? 'EditMessageReplacePhoto' : 'PreviewSender.SendPhoto', renderingAttachments.length, 'i');
Expand Down Expand Up @@ -466,6 +477,19 @@ const AttachmentModal: FC<OwnProps & StateProps> = ({
)}
{hasMedia && (
<>
{
canInvertMedia && (!isInvertedMedia ? (
// eslint-disable-next-line react/jsx-no-bind
<MenuItem icon="move-caption-up" onClick={() => setIsInvertedMedia(true)}>
{lang('PreviewSender.MoveTextUp')}
</MenuItem>
) : (
// eslint-disable-next-line react/jsx-no-bind
<MenuItem icon="move-caption-down" onClick={() => setIsInvertedMedia(undefined)}>
{lang(('PreviewSender.MoveTextDown'))}
</MenuItem>
))
}
{
!shouldForceAsFile && !shouldForceCompression && (isSendingCompressed ? (
// eslint-disable-next-line react/jsx-no-bind
Expand Down
1 change: 1 addition & 0 deletions src/global/actions/api/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ async function sendMessage<T extends GlobalState>(global: T, params: {
groupedId?: string;
wasDrafted?: boolean;
lastMessageId?: number;
isInvertedMedia?: true;
}) {
let currentMessageKey: MessageKey | undefined;
const progressCallback = params.attachment ? (progress: number, messageKey: MessageKey) => {
Expand Down
3 changes: 2 additions & 1 deletion src/global/actions/ui/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,15 @@ addActionHandler('requestConfetti', (global, actions, payload): ActionReturnType

addActionHandler('updateAttachmentSettings', (global, actions, payload): ActionReturnType => {
const {
shouldCompress, shouldSendGrouped,
shouldCompress, shouldSendGrouped, isInvertedMedia,
} = payload;

return {
...global,
attachmentSettings: {
shouldCompress: shouldCompress ?? global.attachmentSettings.shouldCompress,
shouldSendGrouped: shouldSendGrouped ?? global.attachmentSettings.shouldSendGrouped,
isInvertedMedia,
},
};
});
Expand Down
1 change: 1 addition & 0 deletions src/global/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const INITIAL_GLOBAL_STATE: GlobalState = {
attachmentSettings: {
shouldCompress: true,
shouldSendGrouped: true,
isInvertedMedia: undefined,
},

scheduledMessages: {
Expand Down
4 changes: 4 additions & 0 deletions src/global/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export type TabState = {
isSilent?: boolean;
sendGrouped?: boolean;
sendCompressed?: boolean;
isInvertedMedia?: true;
};

activeChatFolder: number;
Expand Down Expand Up @@ -823,6 +824,7 @@ export type GlobalState = {
attachmentSettings: {
shouldCompress: boolean;
shouldSendGrouped: boolean;
isInvertedMedia?: true;
};

attachMenu: {
Expand Down Expand Up @@ -1475,6 +1477,7 @@ export interface ActionPayloads {
shouldGroupMessages?: boolean;
messageList?: MessageList;
isReaction?: true; // Reaction to the story are sent in the form of a message
isInvertedMedia?: true;
} & WithTabId;
sendInviteMessages: {
chatId: string;
Expand Down Expand Up @@ -2905,6 +2908,7 @@ export interface ActionPayloads {
updateAttachmentSettings: {
shouldCompress?: boolean;
shouldSendGrouped?: boolean;
isInvertedMedia?: true;
};

updateArchiveSettings: {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/gramjs/tl/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14151,7 +14151,7 @@ namespace Api {
clearDraft?: true;
noforwards?: true;
updateStickersetsOrder?: true;
invertMedia?: true;
isInvertedMedia?: true;
peer: Api.TypeInputPeer;
replyTo?: Api.TypeInputReplyTo;
media: Api.TypeInputMedia;
Expand Down
Loading

0 comments on commit 47e6743

Please sign in to comment.