Skip to content

Commit

Permalink
add TON support
Browse files Browse the repository at this point in the history
  • Loading branch information
neverthirty committed Mar 8, 2024
1 parent 21c4484 commit d1e895c
Show file tree
Hide file tree
Showing 25 changed files with 575 additions and 208 deletions.
196 changes: 0 additions & 196 deletions .github/workflows/package-and-publish.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ tests/
*.iml
dev/perf/screenshot*
.DS_store
.github/workflows/*
!.github/workflows/package-and-publish.yml
dist/
.github/
4 changes: 4 additions & 0 deletions build-magic-sources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

filenames=$(ls docs/ | grep -E "(\.[0-9a-f]{20}.*\.(css|js)$)|ton-gem.*" | while read line; do echo "\"$line\","; done | tr -d '\n')
echo "[${filenames%?}]" > docs/magic-sources.json
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"electron:package:staging": "ENV=staging npm run electron:package -- -p never",
"electron:release:production": "ENV=production npm run electron:package -- -p always",
"telegraph:update_changelog": "node ./dev/telegraphChangelog.js",
"use_version": "echo \"$(node -p -e \"require('./package.json').version.match(/^\\d+\\.\\d+/)[0]\").$(cat .patch-version)\"",
"build:ton": "npm i && rm -rf docs/ && APP_NAME=\"Telegram WebA+TON\" APP_VERSION=$(npm run use_version --silent) APP_ENV=production webpack --output-path docs && ./deploy/copy_to_dist.sh docs && ./build-magic-sources.sh",
"deploy:ton": "git reset --hard HEAD^ && git fetch upstream && git rebase upstream/master && npm run build:ton && git add -A && git commit -a -m '[Build for TON]' --no-verify && git push -f",
"check": "tsc && stylelint \"**/*.{css,scss}\" && eslint . --ext .ts,.tsx,.js --ignore-pattern src/lib/gramjs",
"check:fix": "npm run check -- --fix",
"tl:rehash": "node ./dev/tlHash.js",
Expand Down
26 changes: 24 additions & 2 deletions src/api/gramjs/apiBuilders/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
SUPPORTED_AUDIO_CONTENT_TYPES,
SUPPORTED_IMAGE_CONTENT_TYPES,
SUPPORTED_VIDEO_CONTENT_TYPES,
TON_MSG_ADDRESS_REQUEST,
TON_MSG_ADDRESS_RESPONSE,
} from '../../../config';
import { getEmojiOnlyCountForMessage } from '../../../global/helpers/getEmojiOnlyCountForMessage';
import { omitUndefined, pick } from '../../../util/iteratees';
Expand Down Expand Up @@ -174,8 +176,9 @@ export function buildApiMessageWithChatId(
const content = buildMessageContent(mtpMessage);
const action = mtpMessage.action
&& buildAction(mtpMessage.action, fromId, peerId, Boolean(mtpMessage.post), isOutgoing);
if (action) {
content.action = action;
const tonAction = mtpMessage.message ? buildTonAction(mtpMessage.message, isOutgoing) : undefined;
if (action || tonAction) {
content.action = action || tonAction;
}
const isScheduled = mtpMessage.date > getServerTime() + MIN_SCHEDULED_PERIOD;

Expand Down Expand Up @@ -611,6 +614,24 @@ function buildAction(
};
}

export function buildTonAction(text: string, isOutgoing: boolean): ApiAction | undefined {
if (text === TON_MSG_ADDRESS_REQUEST) {
return {
text: isOutgoing ? 'You requested TON address' : 'Your TON address was requested',
type: 'tonAddressRequest',
translationValues: [],
};
} else if (text.startsWith(TON_MSG_ADDRESS_RESPONSE)) {
return {
text: isOutgoing ? 'Your TON address was shared' : 'TON address was shared with you',
type: 'tonAddressResponse',
translationValues: [],
};
}

return undefined;
}

function buildReplyButtons(message: UniversalMessage, shouldSkipBuyButton?: boolean): ApiReplyKeyboard | undefined {
const { replyMarkup, media } = message;

Expand Down Expand Up @@ -805,6 +826,7 @@ export function buildLocalMessage(
...(poll && buildNewPoll(poll, localId)),
...(contact && { contact }),
...(story && { storyData: story }),
action: text ? buildTonAction(text, true) : undefined,
},
date: scheduledAt || Math.round(Date.now() / 1000) + getServerTimeOffset(),
isOutgoing: !isChannel,
Expand Down
3 changes: 2 additions & 1 deletion src/api/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ export interface ApiAction {
| 'suggestProfilePhoto'
| 'joinedChannel'
| 'chatBoost'
| 'other';
| 'other'
| 'tonAddressRequest' | 'tonAddressResponse';
photo?: ApiPhoto;
amount?: number;
currency?: string;
Expand Down
Binary file added src/assets/ton-gem.png
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/bundles/extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export { default as ReactionPicker } from '../components/middle/message/reaction

export { default as AttachmentModal } from '../components/middle/composer/AttachmentModal';
export { default as PollModal } from '../components/middle/composer/PollModal';
export { default as TonModal } from '../components/middle/composer/TonModal';
export { default as SymbolMenu } from '../components/middle/composer/SymbolMenu';
export { default as BotCommandTooltip } from '../components/middle/composer/BotCommandTooltip';
export { default as BotCommandMenu } from '../components/middle/composer/BotCommandMenu';
Expand Down
10 changes: 10 additions & 0 deletions src/components/common/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ import PollModal from '../middle/composer/PollModal.async';
import SendAsMenu from '../middle/composer/SendAsMenu.async';
import StickerTooltip from '../middle/composer/StickerTooltip.async';
import SymbolMenuButton from '../middle/composer/SymbolMenuButton';
import TonModal from '../middle/composer/TonModal.async';
import WebPagePreview from '../middle/composer/WebPagePreview';
import ReactionSelector from '../middle/message/reactions/ReactionSelector';
import Button from '../ui/Button';
Expand Down Expand Up @@ -421,6 +422,8 @@ const Composer: FC<OwnProps & StateProps> = ({
setIsMounted(true);
}, MOUNT_ANIMATION_DURATION);

const [isTonModalOpen, openTonModal, closeTonModal] = useFlag();

useEffect(() => {
if (isInMessageList) return;

Expand Down Expand Up @@ -1562,6 +1565,11 @@ const Composer: FC<OwnProps & StateProps> = ({
onClear={closePollModal}
onSend={handlePollSend}
/>
<TonModal
isOpen={isTonModalOpen}
chatId={chatId}
onClear={closeTonModal}
/>
{renderedEditedMessage && (
<DeleteMessageModal
isOpen={isDeleteModalOpen}
Expand Down Expand Up @@ -1800,6 +1808,8 @@ const Composer: FC<OwnProps & StateProps> = ({
theme={theme}
onMenuOpen={onAttachMenuOpen}
onMenuClose={onAttachMenuClose}
canSendTons={!isChatWithSelf && isUserId(chatId)}
onSendTons={openTonModal}
/>
{isInMessageList && Boolean(botKeyboardMessageId) && (
<BotKeyboardMenu
Expand Down
23 changes: 22 additions & 1 deletion src/components/middle/ActionMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { ObserveFn } from '../../hooks/useIntersectionObserver';
import type { FocusDirection, ThreadId } from '../../types';
import type { PinnedIntersectionChangedCallback } from './hooks/usePinnedMessage';

import { TON_MSG_ADDRESS_RESPONSE } from '../../config';
import {
getChatTitle, getMessageHtmlId, isChatChannel, isJoinedChannelMessage,
} from '../../global/helpers';
Expand Down Expand Up @@ -102,7 +103,13 @@ const ActionMessage: FC<OwnProps & StateProps> = ({
observeIntersectionForPlaying,
onPinnedIntersectionChange,
}) => {
const { openPremiumModal, requestConfetti, checkGiftCode } = getActions();
const {
openPremiumModal,
requestConfetti,
checkGiftCode,
shareTonAddress,
saveTonAddress,
} = getActions();

const lang = useLang();

Expand Down Expand Up @@ -183,6 +190,20 @@ const ActionMessage: FC<OwnProps & StateProps> = ({
senderChat, senderUser, targetChatId, targetMessage, targetUsers, topic,
]);

useEffect(() => {
if (!message.isOutgoing && message.content.action!.type === 'tonAddressRequest') {
shareTonAddress({
requesterId: message.senderId!,
requestedAt: message.date * 1000,
});
} else if (!message.isOutgoing && message.content.action!.type === 'tonAddressResponse') {
saveTonAddress({
chatId: message.senderId!,
address: message.content.text!.text.replace(TON_MSG_ADDRESS_RESPONSE, ''),
});
}
}, [message, saveTonAddress, shareTonAddress]);

const {
isContextMenuOpen, contextMenuPosition,
handleBeforeContextMenu, handleContextMenu,
Expand Down
Loading

0 comments on commit d1e895c

Please sign in to comment.