Compare commits

..

49 Commits

Author SHA1 Message Date
7d5980a453 Подгонка regexp к серверной валидации 2026-04-11 21:38:24 +02:00
5193ceb071 Валидация вводимого имени пользователя 2026-04-11 21:35:34 +02:00
7434e16252 Исправление высоты текста у ссылки 2026-04-11 21:05:48 +02:00
c762e527c2 Исправление нестабильности HTTP соединения 2026-04-11 21:03:32 +02:00
3291def79b Исправление устаревшего имени в UserButton 2026-04-11 11:57:12 +02:00
032f92f8a2 Исправление индикатора непрочитанных в диалогах группы при синхронизаци 2026-04-11 11:51:21 +02:00
RoyceDa
f4592d03b0 Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 3m41s
2026-04-10 18:56:13 +02:00
RoyceDa
6554483939 С binary на hex 2026-04-10 18:52:58 +02:00
RoyceDa
ba12db3c72 OPUS сборка 2026-04-10 17:54:48 +02:00
RoyceDa
b596d36543 Базовая версия голосовых сообщений и аудиоплеер. Кодирование OPUS 2026-04-10 17:20:44 +02:00
RoyceDa
93ef692eb5 Подготовка голосовых сообщений 2026-04-09 16:53:57 +02:00
RoyceDa
8fdfe9b786 Исправдение failed to decompress data 2026-04-09 16:36:23 +02:00
RoyceDa
547ac89987 Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 4m4s
2026-04-08 23:05:03 +02:00
RoyceDa
130ad9c35a Исправление парсинга emoji и составных emoji (skin tones) 2026-04-08 22:58:49 +02:00
RoyceDa
adfc6add6f Исправление битых вложений в группах (на декодинг) 2026-04-08 21:55:59 +02:00
RoyceDa
cf29cecfd6 Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 5m58s
2026-04-04 18:59:52 +02:00
RoyceDa
779c265851 Поднятие версии, outline
All checks were successful
SP Builds / build (push) Successful in 4m25s
2026-04-04 18:34:18 +02:00
RoyceDa
8ac952071d Фикс протокола 2026-04-04 18:23:24 +02:00
RoyceDa
e1f5cb7eb8 Перевод звонка в активную стадию 2026-04-04 18:18:05 +02:00
RoyceDa
30f2c90015 Фикс ассиметричного обмена 2026-04-04 18:07:24 +02:00
RoyceDa
a341aedd8d Фикс бесконечного обмена ключами 2026-04-04 18:04:11 +02:00
RoyceDa
a9164c7087 Фикс обмена ключами 2026-04-04 18:01:52 +02:00
RoyceDa
04dd23dd5c Фикс обмена ключами 2026-04-04 18:01:09 +02:00
RoyceDa
5979c31120 Таймаут вызова 2026-04-04 17:46:38 +02:00
RoyceDa
c8c85991c7 Исправление протокола для правильного END_CALL 2026-04-04 17:19:43 +02:00
RoyceDa
c052fdae41 Реализация нового протокола звонков 2026-04-04 16:48:26 +02:00
RoyceDa
3492a881cc Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 3m36s
2026-04-02 18:31:46 +02:00
RoyceDa
febeb58778 Фикс ICE кандидатов
Some checks failed
SP Builds / build (push) Has been cancelled
2026-04-02 17:57:38 +02:00
RoyceDa
93e4898bec Правильный deviceId 2026-04-02 17:38:34 +02:00
RoyceDa
de7a00f37a Обновление протокола звонков без авторизации с сохранением защиты 2026-04-02 17:26:38 +02:00
RoyceDa
7b3dd6c566 Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 3m38s
2026-04-01 16:31:31 +02:00
RoyceDa
70af076248 Исправлен цвет аватарки при ее отсутствии в профиле 2026-04-01 16:31:27 +02:00
RoyceDa
92c9dc03c9 Время звонка начинается тогда, когда начинается аудио-дорожка, а не тогда, когда установлено соединение с SFU 2026-04-01 16:28:41 +02:00
RoyceDa
7e8d086a74 Исправление встречных звонков 2026-04-01 14:39:45 +02:00
RoyceDa
0a0c810105 Защита от принятия звонка принятого на другом устройстве 2026-04-01 14:21:11 +02:00
RoyceDa
8fbfb4fa5c Поднятие версии
All checks were successful
SP Builds / build (push) Successful in 3m43s
2026-03-30 19:53:08 +02:00
RoyceDa
2b9e28ee4a Исправление системных звуков в звонке
Some checks failed
SP Builds / build (push) Has been cancelled
2026-03-30 19:41:06 +02:00
RoyceDa
d2a506119c Исправление невозможности выбора сообщений в диалоге 2026-03-30 19:37:17 +02:00
RoyceDa
269f66fdc5 Улучшение CI/CD 2026-03-29 17:03:30 +02:00
RoyceDa
5113d18d70 Фикс дергания при старте интерфейса
Some checks failed
Linux Kernel Build / build (arm64, arm64) (push) Failing after 2m40s
Linux Kernel Build / build (x64, x86_64) (push) Successful in 3m4s
MacOS Kernel Build / build (arm64) (push) Successful in 6m9s
SP Builds / build (push) Successful in 3m53s
Windows Kernel Build / build (push) Successful in 12m15s
MacOS Kernel Build / build (x64) (push) Successful in 6m56s
2026-03-29 16:09:28 +02:00
RoyceDa
cd2dee21ab Поднятие версии kernel 2026-03-29 16:07:02 +02:00
RoyceDa
1b14463dbb Поднятие версии kernel 2026-03-29 16:06:44 +02:00
RoyceDa
d23ca97be9 Поднятие версии.
Some checks failed
Linux Kernel Build / build (x64, x86_64) (push) Has been cancelled
Linux Kernel Build / build (arm64, arm64) (push) Has been cancelled
MacOS Kernel Build / build (x64) (push) Has been cancelled
MacOS Kernel Build / build (arm64) (push) Has been cancelled
SP Builds / build (push) Has been cancelled
Windows Kernel Build / build (push) Has been cancelled
2026-03-29 15:58:19 +02:00
RoyceDa
519aa8802f Улучшение протокола, чистка кода 2026-03-29 15:45:08 +02:00
RoyceDa
2f2a0b5376 Передача транспортного сервера в контекст, в базу, и в кэш 2026-03-29 14:58:52 +02:00
RoyceDa
61e83bdd43 Унификация кодирования chacha_key_plain теперь он в hex, а не в utf8 с потерей данных 2026-03-28 17:52:13 +02:00
RoyceDa
f5bfa153b6 Приведение chacha_key_plain к hex для транспортировки 2026-03-28 17:20:03 +02:00
RoyceDa
81f5e66c56 Дополнительные доменные зоны 2026-03-28 17:07:51 +02:00
RoyceDa
aaa4b4283a Новая система пересылки сообщений без трансляции и транскодирования вложений 2026-03-28 17:07:43 +02:00
35 changed files with 1682 additions and 309 deletions

View File

@@ -43,7 +43,7 @@ jobs:
mkdir -p dist/builds/linux/x64 mkdir -p dist/builds/linux/x64
mkdir -p dist/builds/linux/${{ matrix.out_dir }} mkdir -p dist/builds/linux/${{ matrix.out_dir }}
npx electron-vite build npx electron-vite build
npx electron-builder --linux --$ARCH npx electron-builder --linux --${{ matrix.arch }}
- name: Check if files exist - name: Check if files exist
run: | run: |

View File

@@ -22,6 +22,7 @@ import { DialogStateProvider } from './providers/DialogStateProvider.tsx/DialogS
import { DeviceConfirm } from './views/DeviceConfirm/DeviceConfirm'; import { DeviceConfirm } from './views/DeviceConfirm/DeviceConfirm';
import { SystemAccountProvider } from './providers/SystemAccountsProvider/SystemAccountsProvider'; import { SystemAccountProvider } from './providers/SystemAccountsProvider/SystemAccountsProvider';
import { DeviceProvider } from './providers/DeviceProvider/DeviceProvider'; import { DeviceProvider } from './providers/DeviceProvider/DeviceProvider';
import { PlayerProvider } from './providers/PlayerProvider/PlayerProvider';
window.Buffer = Buffer; window.Buffer = Buffer;
export default function App() { export default function App() {
@@ -58,6 +59,7 @@ export default function App() {
<Topbar></Topbar> <Topbar></Topbar>
<ContextMenuProvider> <ContextMenuProvider>
<ImageViwerProvider> <ImageViwerProvider>
<PlayerProvider>
<AvatarProvider> <AvatarProvider>
<Routes> <Routes>
<Route path="/" element={ <Route path="/" element={
@@ -71,6 +73,7 @@ export default function App() {
<Route path="/deviceconfirm" element={<DeviceConfirm />} /> <Route path="/deviceconfirm" element={<DeviceConfirm />} />
</Routes> </Routes>
</AvatarProvider> </AvatarProvider>
</PlayerProvider>
</ImageViwerProvider> </ImageViwerProvider>
</ContextMenuProvider> </ContextMenuProvider>
</Box> </Box>

View File

@@ -60,7 +60,7 @@ export function ActionAvatar(props : ActionAvatarProps) {
size={120} size={120}
radius={120} radius={120}
mx="auto" mx="auto"
bg={'#fff'} bg={avatars.length > 0 ? '#fff' : undefined}
name={props.title.trim() || props.publicKey} name={props.title.trim() || props.publicKey}
color={'initials'} color={'initials'}
src={avatars.length > 0 ? src={avatars.length > 0 ?

View File

@@ -1,7 +1,7 @@
import { useDialog } from "@/app/providers/DialogProvider/useDialog"; import { useDialog } from "@/app/providers/DialogProvider/useDialog";
import { useRosettaColors } from "@/app/hooks/useRosettaColors"; import { useRosettaColors } from "@/app/hooks/useRosettaColors";
import { Box, Divider, Flex, Menu, Popover, Text, Transition, useComputedColorScheme } from "@mantine/core"; import { Box, Divider, Flex, Menu, Popover, Text, Transition, useComputedColorScheme } from "@mantine/core";
import { IconBarrierBlock, IconCamera, IconDoorExit, IconFile, IconMoodSmile, IconPaperclip, IconSend } from "@tabler/icons-react"; import { IconBarrierBlock, IconCamera, IconDoorExit, IconFile, IconMicrophone, IconMoodSmile, IconPaperclip, IconSend, IconTrash } from "@tabler/icons-react";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { useBlacklist } from "@/app/providers/BlacklistProvider/useBlacklist"; import { useBlacklist } from "@/app/providers/BlacklistProvider/useBlacklist";
import { filePrapareForNetworkTransfer, generateRandomKey, imagePrepareForNetworkTransfer } from "@/app/utils/utils"; import { filePrapareForNetworkTransfer, generateRandomKey, imagePrepareForNetworkTransfer } from "@/app/utils/utils";
@@ -25,7 +25,8 @@ import { AnimatedButton } from "../AnimatedButton/AnimatedButton";
import { useUserCacheFunc } from "@/app/providers/InformationProvider/useUserCacheFunc"; import { useUserCacheFunc } from "@/app/providers/InformationProvider/useUserCacheFunc";
import { MentionList, Mention } from "../MentionList/MentionList"; import { MentionList, Mention } from "../MentionList/MentionList";
import { useDrafts } from "@/app/providers/DialogProvider/useDrafts"; import { useDrafts } from "@/app/providers/DialogProvider/useDrafts";
import { useVoiceMessage } from "./useVoiceMessage";
import { VoiceRecorder } from "../VoiceRecorder/VoiceRecorder";
export function DialogInput() { export function DialogInput() {
const colors = useRosettaColors(); const colors = useRosettaColors();
@@ -47,6 +48,7 @@ export function DialogInput() {
const [mentionList, setMentionList] = useState<Mention[]>([]); const [mentionList, setMentionList] = useState<Mention[]>([]);
const mentionHandling = useRef<string>(""); const mentionHandling = useRef<string>("");
const {getDraft, saveDraft} = useDrafts(dialog); const {getDraft, saveDraft} = useDrafts(dialog);
const {start, stop, isRecording, duration, waves, getAudioBlob, interpolateCompressWaves} = useVoiceMessage();
const avatars = useAvatars( const avatars = useAvatars(
@@ -64,6 +66,15 @@ export function DialogInput() {
}] }]
], [], true); ], [], true);
const hasText = message.trim().length > 0;
const showSendIcon = hasText || attachments.length > 0 || isRecording;
const onMicroClick = () => {
if(!isRecording) {
start();
}
};
const fileDialog = useFileDialog({ const fileDialog = useFileDialog({
multiple: false, multiple: false,
//naccept: '*', //naccept: '*',
@@ -93,10 +104,6 @@ export function DialogInput() {
transport: { transport: {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
},
encoding: {
encoded_for: dialog,
encoder: publicKey
} }
}]); }]);
} }
@@ -128,10 +135,6 @@ export function DialogInput() {
transport: { transport: {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
},
encoding: {
encoded_for: dialog,
encoder: publicKey
} }
}]); }]);
if(editableDivRef.current){ if(editableDivRef.current){
@@ -196,8 +199,28 @@ export function DialogInput() {
mentionHandling.current = username; mentionHandling.current = username;
} }
const send = () => { const send = async () => {
if(blocked || (message.trim() == "" && attachments.length <= 0)) { if(blocked || (message.trim() == "" && attachments.length <= 0 && !isRecording)){
return;
}
if(isRecording){
const audioBlob = getAudioBlob();
stop();
if(!audioBlob){
return;
}
sendMessage("", [
{
blob: Buffer.from(await audioBlob.arrayBuffer()).toString('hex'),
id: generateRandomKey(8),
type: AttachmentType.VOICE,
preview: duration + "::" + interpolateCompressWaves(35).join(","),
transport: {
transport_server: "",
transport_tag: ""
}
}
]);
return; return;
} }
sendMessage(message, attachments); sendMessage(message, attachments);
@@ -250,10 +273,6 @@ export function DialogInput() {
transport: { transport: {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
},
encoding: {
encoded_for: dialog,
encoder: publicKey
} }
}]); }]);
if(editableDivRef.current){ if(editableDivRef.current){
@@ -298,10 +317,6 @@ export function DialogInput() {
transport: { transport: {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
},
encoding: {
encoded_for: dialog,
encoder: publicKey
} }
}]); }]);
} }
@@ -340,10 +355,6 @@ export function DialogInput() {
transport: { transport: {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
},
encoding: {
encoded_for: dialog,
encoder: publicKey
} }
}]); }]);
} }
@@ -385,6 +396,12 @@ export function DialogInput() {
{!blocked && {!blocked &&
<Flex h={'100%'} p={'xs'} direction={'row'} bg={colors.boxColor}> <Flex h={'100%'} p={'xs'} direction={'row'} bg={colors.boxColor}>
<Flex w={25} mt={10} justify={'center'}> <Flex w={25} mt={10} justify={'center'}>
{isRecording && (
<IconTrash onClick={stop} style={{
cursor: 'pointer'
}} color={colors.error} stroke={1.5} size={25}></IconTrash>
)}
{!isRecording && (
<Menu width={150} withArrow> <Menu width={150} withArrow>
<Menu.Target> <Menu.Target>
<IconPaperclip stroke={1.5} style={{ <IconPaperclip stroke={1.5} style={{
@@ -405,12 +422,14 @@ export function DialogInput() {
} onClick={onClickCamera}>Avatar {hasGroup(dialog) && 'group'}</Menu.Item>} } onClick={onClickCamera}>Avatar {hasGroup(dialog) && 'group'}</Menu.Item>}
</Menu.Dropdown> </Menu.Dropdown>
</Menu> </Menu>
)}
</Flex> </Flex>
<Flex <Flex
w={'calc(100% - (25px + 50px + var(--mantine-spacing-xs)))'} w={'calc(100% - (25px + 50px + var(--mantine-spacing-xs)))'}
maw={'calc(100% - (25px + 50px + var(--mantine-spacing-xs)))'} maw={'calc(100% - (25px + 50px + var(--mantine-spacing-xs)))'}
align={'center'} align={'center'}
> >
{!isRecording && <>
<RichTextInput <RichTextInput
ref={editableDivRef} ref={editableDivRef}
style={{ style={{
@@ -427,19 +446,17 @@ export function DialogInput() {
}} }}
placeholder="Type message..." placeholder="Type message..."
autoFocus autoFocus
//ref={textareaRef}
//onPaste={onPaste}
//maxLength={2500}
//w={'100%'}
//h={'100%'}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onChange={setMessage} onChange={setMessage}
onPaste={onPaste} onPaste={onPaste}
//dangerouslySetInnerHTML={{__html: message}}
></RichTextInput> ></RichTextInput>
</>}
{isRecording && <>
<VoiceRecorder duration={duration} waves={waves}></VoiceRecorder>
</>}
</Flex> </Flex>
<Flex mt={10} w={'calc(50px + var(--mantine-spacing-xs))'} gap={'xs'}> <Flex mt={10} w={'calc(50px + var(--mantine-spacing-xs))'} gap={'xs'}>
{!isRecording && <>
<Popover withArrow> <Popover withArrow>
<Popover.Target> <Popover.Target>
<IconMoodSmile color={colors.chevrons.active} stroke={1.5} size={25} style={{ <IconMoodSmile color={colors.chevrons.active} stroke={1.5} size={25} style={{
@@ -455,9 +472,42 @@ export function DialogInput() {
/> />
</Popover.Dropdown> </Popover.Dropdown>
</Popover> </Popover>
<IconSend stroke={1.5} color={message.trim() == "" && attachments.length <= 0 ? colors.chevrons.active : colors.brandColor} onClick={send} style={{ </>}
<Box pos="relative" ml={isRecording ? 35 : 0} w={25} h={25}>
<Transition mounted={showSendIcon} transition="pop" duration={180} timingFunction="ease">
{(styles) => (
<IconSend
stroke={1.5}
color={colors.brandColor}
onClick={send}
style={{
...styles,
position: 'absolute',
inset: 0,
cursor: 'pointer' cursor: 'pointer'
}} size={25}></IconSend> }}
size={25}
/>
)}
</Transition>
<Transition mounted={!showSendIcon} transition="pop" duration={180} timingFunction="ease">
{(styles) => (
<IconMicrophone
stroke={1.5}
color={colors.chevrons.active}
onClick={onMicroClick}
style={{
...styles,
position: 'absolute',
inset: 0,
cursor: 'pointer'
}}
size={25}
/>
)}
</Transition>
</Box>
</Flex> </Flex>
</Flex>} </Flex>}
{blocked && <Box mih={62} bg={colors.boxColor}> {blocked && <Box mih={62} bg={colors.boxColor}>

View File

@@ -0,0 +1,273 @@
import { useState, useRef, useCallback, useEffect } from "react";
export function useVoiceMessage(): {
isRecording: boolean;
isPaused: boolean;
duration: number;
waves: number[];
start: () => Promise<void>;
stop: () => void;
pause: () => void;
play: () => void;
error: string | null;
getAudioBlob: () => Blob | null;
interpolateCompressWaves: (targetLength: number) => number[];
} {
const [isRecording, setIsRecording] = useState(false);
const [isPaused, setIsPaused] = useState(false);
const [duration, setDuration] = useState(0);
const [waves, setWaves] = useState<number[]>([]);
const [error, setError] = useState<string | null>(null);
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
const chunksRef = useRef<Blob[]>([]);
const streamRef = useRef<MediaStream | null>(null);
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
const waveTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
const audioContextRef = useRef<AudioContext | null>(null);
const analyserRef = useRef<AnalyserNode | null>(null);
const sourceRef = useRef<MediaStreamAudioSourceNode | null>(null);
const waveDataRef = useRef<Uint8Array<ArrayBuffer> | null>(null);
const clearTimer = useCallback(() => {
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
}, []);
const stopWaveLoop = useCallback(() => {
if (waveTimerRef.current) {
clearInterval(waveTimerRef.current);
waveTimerRef.current = null;
}
}, []);
const startTimer = useCallback(() => {
if (timerRef.current) return;
timerRef.current = setInterval(() => {
setDuration((prev) => prev + 1);
}, 1000);
}, []);
const startWaveLoop = useCallback(() => {
stopWaveLoop();
const analyser = analyserRef.current;
if (!analyser) return;
if (!waveDataRef.current || waveDataRef.current.length !== analyser.frequencyBinCount) {
waveDataRef.current = new Uint8Array(new ArrayBuffer(analyser.frequencyBinCount));
}
const MAX_WAVES = 120;
const tick = () => {
if (!analyserRef.current || !waveDataRef.current) return;
analyserRef.current.getByteFrequencyData(waveDataRef.current);
let peak = 0;
for (let i = 0; i < waveDataRef.current.length; i++) {
const v = waveDataRef.current[i];
if (v > peak) peak = v;
}
const bar = peak / 255;
setWaves((prev) => {
const next = [...prev, bar];
return next.length > MAX_WAVES ? next.slice(next.length - MAX_WAVES) : next;
});
};
tick();
waveTimerRef.current = setInterval(tick, 300);
}, [stopWaveLoop]);
const cleanupAudio = useCallback(() => {
stopWaveLoop();
sourceRef.current?.disconnect();
sourceRef.current = null;
analyserRef.current = null;
waveDataRef.current = null;
if (audioContextRef.current) {
audioContextRef.current.close();
audioContextRef.current = null;
}
streamRef.current?.getTracks().forEach((track) => track.stop());
streamRef.current = null;
}, [stopWaveLoop]);
const start = useCallback(async () => {
try {
setError(null);
setDuration(0);
setWaves([]);
chunksRef.current = [];
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
streamRef.current = stream;
const audioContext = new AudioContext();
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
analyser.smoothingTimeConstant = 0;
analyser.minDecibels = -100;
analyser.maxDecibels = -10;
const source = audioContext.createMediaStreamSource(stream);
source.connect(analyser);
audioContextRef.current = audioContext;
analyserRef.current = analyser;
sourceRef.current = source;
// Выбираем лучший поддерживаемый кодек
const preferredTypes = [
"audio/webm;codecs=opus",
"audio/ogg;codecs=opus",
"audio/webm",
];
const mimeType = preferredTypes.find((t) => MediaRecorder.isTypeSupported(t)) ?? "";
const mediaRecorder = new MediaRecorder(stream, {
...(mimeType ? { mimeType } : {}),
audioBitsPerSecond: 32_000,
});
mediaRecorderRef.current = mediaRecorder;
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) chunksRef.current.push(e.data);
};
mediaRecorder.onstop = () => {
cleanupAudio();
};
mediaRecorder.start(100);
setIsRecording(true);
setIsPaused(false);
startTimer();
startWaveLoop();
} catch (err) {
setError("Could not start voice recording. Please check microphone permissions.");
console.error("Voice recording error:", err);
}
}, [startTimer, startWaveLoop, cleanupAudio]);
const stop = useCallback(() => {
if (mediaRecorderRef.current && isRecording) {
mediaRecorderRef.current.stop();
mediaRecorderRef.current = null;
setIsRecording(false);
setIsPaused(false);
clearTimer();
stopWaveLoop();
}
}, [isRecording, clearTimer, stopWaveLoop]);
const pause = useCallback(() => {
if (mediaRecorderRef.current && mediaRecorderRef.current.state === "recording") {
mediaRecorderRef.current.pause();
setIsPaused(true);
clearTimer();
stopWaveLoop();
}
}, [clearTimer, stopWaveLoop]);
const play = useCallback(() => {
if (mediaRecorderRef.current && mediaRecorderRef.current.state === "paused") {
mediaRecorderRef.current.resume();
setIsPaused(false);
startTimer();
startWaveLoop();
}
}, [startTimer, startWaveLoop]);
useEffect(() => {
return () => {
clearTimer();
stopWaveLoop();
if (mediaRecorderRef.current && mediaRecorderRef.current?.state !== "inactive") {
mediaRecorderRef.current.stop();
}
cleanupAudio();
};
}, [clearTimer, stopWaveLoop, cleanupAudio]);
const getAudioBlob = useCallback((): Blob | null => {
if (chunksRef.current.length === 0) return null;
const mimeType = mediaRecorderRef.current?.mimeType ?? "audio/webm;codecs=opus";
return new Blob(chunksRef.current, { type: mimeType });
}, []);
const interpolateCompressWaves = useCallback((targetLength: number) => {
if (targetLength <= 0) return [];
if (waves.length === 0) return Array(targetLength).fill(0);
if (waves.length === targetLength) return waves;
if (waves.length > targetLength) {
const compressed: number[] = [];
const bucketSize = waves.length / targetLength;
for (let i = 0; i < targetLength; i++) {
const start = Math.floor(i * bucketSize);
const end = Math.max(start + 1, Math.floor((i + 1) * bucketSize));
let max = 0;
for (let j = start; j < end && j < waves.length; j++) {
if (waves[j] > max) max = waves[j];
}
compressed.push(max);
}
return compressed;
}
if (targetLength === 1) return [waves[0]];
const stretched: number[] = [];
const lastSourceIndex = waves.length - 1;
for (let i = 0; i < targetLength; i++) {
const position = (i * lastSourceIndex) / (targetLength - 1);
const left = Math.floor(position);
const right = Math.min(Math.ceil(position), lastSourceIndex);
if (left === right) {
stretched.push(waves[left]);
continue;
}
const t = position - left;
const value = waves[left] * (1 - t) + waves[right] * t;
stretched.push(value);
}
return stretched;
}, [waves]);
return {
isRecording,
isPaused,
duration,
waves,
start,
stop,
pause,
play,
error,
getAudioBlob,
interpolateCompressWaves,
};
}

View File

@@ -17,6 +17,8 @@ import { useDialogContextMenu } from "@/app/hooks/useDialogContextMenu";
import { useDialogMute } from "@/app/providers/DialogStateProvider.tsx/useDialogMute"; import { useDialogMute } from "@/app/providers/DialogStateProvider.tsx/useDialogMute";
import { useDialogPin } from "@/app/providers/DialogStateProvider.tsx/useDialogPin"; import { useDialogPin } from "@/app/providers/DialogStateProvider.tsx/useDialogPin";
import { useMentions } from "@/app/providers/DialogStateProvider.tsx/useMentions"; import { useMentions } from "@/app/providers/DialogStateProvider.tsx/useMentions";
import { useProtocolState } from "@/app/providers/ProtocolProvider/useProtocolState";
import { ProtocolState } from "@/app/providers/ProtocolProvider/ProtocolProvider";
export interface DialogProps extends DialogRow { export interface DialogProps extends DialogRow {
onClickDialog: (dialog: string) => void; onClickDialog: (dialog: string) => void;
@@ -54,6 +56,7 @@ export function GroupDialog(props : DialogProps) {
const currentDialogColor = computedTheme == 'dark' ? '#2a6292' :'#438fd1'; const currentDialogColor = computedTheme == 'dark' ? '#2a6292' :'#438fd1';
const {openContextMenu} = useDialogContextMenu(); const {openContextMenu} = useDialogContextMenu();
const {isMentioned} = useMentions(); const {isMentioned} = useMentions();
const [protocolState] = useProtocolState();
useEffect(() => { useEffect(() => {
@@ -156,7 +159,7 @@ export function GroupDialog(props : DialogProps) {
{!loading && (lastMessage.delivered == DeliveredMessageState.ERROR || (!isMessageDeliveredByTime(lastMessage.timestamp, lastMessage.attachments.length) && lastMessage.delivered != DeliveredMessageState.DELIVERED)) && ( {!loading && (lastMessage.delivered == DeliveredMessageState.ERROR || (!isMessageDeliveredByTime(lastMessage.timestamp, lastMessage.attachments.length) && lastMessage.delivered != DeliveredMessageState.DELIVERED)) && (
<IconAlertCircle stroke={3} size={15} color={colors.error}></IconAlertCircle> <IconAlertCircle stroke={3} size={15} color={colors.error}></IconAlertCircle>
)} )}
{unreaded > 0 && !lastMessageFromMe && !isMentioned(props.dialog_id) && <Badge {unreaded > 0 && !lastMessageFromMe && protocolState != ProtocolState.SYNCHRONIZATION && !isMentioned(props.dialog_id) && <Badge
color={isInCurrentDialog ? 'white' : (isMuted ? colors.chevrons.active : colors.brandColor)} color={isInCurrentDialog ? 'white' : (isMuted ? colors.chevrons.active : colors.brandColor)}
c={isInCurrentDialog ? colors.brandColor : 'white'} c={isInCurrentDialog ? colors.brandColor : 'white'}
size={'sm'} circle={unreaded < 10}>{unreaded > 99 ? '99+' : unreaded}</Badge>} size={'sm'} circle={unreaded < 10}>{unreaded > 99 ? '99+' : unreaded}</Badge>}

View File

@@ -9,6 +9,7 @@ import { AttachmentError } from "../AttachmentError/AttachmentError";
import { MessageAvatar } from "./MessageAvatar"; import { MessageAvatar } from "./MessageAvatar";
import { MessageProps } from "../Messages/Message"; import { MessageProps } from "../Messages/Message";
import { MessageCall } from "./MessageCall"; import { MessageCall } from "./MessageCall";
import { MessageVoice } from "./MessageVoice";
export interface MessageAttachmentsProps { export interface MessageAttachmentsProps {
attachments: Attachment[]; attachments: Attachment[];
@@ -54,6 +55,8 @@ export function MessageAttachments(props: MessageAttachmentsProps) {
return <MessageAvatar {...attachProps} key={index}></MessageAvatar> return <MessageAvatar {...attachProps} key={index}></MessageAvatar>
case AttachmentType.CALL: case AttachmentType.CALL:
return <MessageCall {...attachProps} key={index}></MessageCall> return <MessageCall {...attachProps} key={index}></MessageCall>
case AttachmentType.VOICE:
return <MessageVoice {...attachProps} key={index}></MessageVoice>
default: default:
return <AttachmentError key={index}></AttachmentError>; return <AttachmentError key={index}></AttachmentError>;
} }

View File

@@ -150,7 +150,7 @@ export function MessageImage(props: AttachmentProps) {
</Box> </Box>
</Flex> </Flex>
</Portal>} </Portal>}
{(props.delivered == DeliveredMessageState.ERROR || (props.delivered != DeliveredMessageState.DELIVERED && {(props.delivered == DeliveredMessageState.ERROR || error || (props.delivered != DeliveredMessageState.DELIVERED &&
!isMessageDeliveredByTime(props.timestamp || 0, props.attachments.length) !isMessageDeliveredByTime(props.timestamp || 0, props.attachments.length)
)) && ( )) && (
<Overlay center h={'100%'} radius={8} color="#000" opacity={0.85}> <Overlay center h={'100%'} radius={8} color="#000" opacity={0.85}>

View File

@@ -5,6 +5,7 @@ import { ReplyedMessage } from "../ReplyedMessage/ReplyedMessage";
import { IconX } from "@tabler/icons-react"; import { IconX } from "@tabler/icons-react";
import { useSetting } from "@/app/providers/SettingsProvider/useSetting"; import { useSetting } from "@/app/providers/SettingsProvider/useSetting";
import { modals } from "@mantine/modals"; import { modals } from "@mantine/modals";
import { MessageReply } from "@/app/providers/DialogProvider/useReplyMessages";
export function MessageReplyMessages(props: AttachmentProps) { export function MessageReplyMessages(props: AttachmentProps) {
const colors = useRosettaColors(); const colors = useRosettaColors();
@@ -12,9 +13,7 @@ export function MessageReplyMessages(props: AttachmentProps) {
('showAlertInReplyMessages', true); ('showAlertInReplyMessages', true);
const [bgInReplyMessages] = useSetting<string> const [bgInReplyMessages] = useSetting<string>
('bgInReplyMessages', ''); ('bgInReplyMessages', '');
const reply = JSON.parse(props.attachment.blob); const reply = JSON.parse(props.attachment.blob) as MessageReply[];
//console.info("Mreply", reply);
const closeAlert = () => { const closeAlert = () => {
modals.openConfirmModal({ modals.openConfirmModal({
@@ -40,8 +39,8 @@ export function MessageReplyMessages(props: AttachmentProps) {
{reply.length <= 0 && {reply.length <= 0 &&
<Skeleton h={50} w={'100%'}></Skeleton> <Skeleton h={50} w={'100%'}></Skeleton>
} }
{reply.map((msg, index) => ( {reply.map((msg : MessageReply, index) => (
<ReplyedMessage parent={props.parent} chacha_key_plain={props.chacha_key_plain} key={index} messageReply={msg}></ReplyedMessage> <ReplyedMessage parent={props.parent} chacha_key_plain={msg.chacha_key_plain} key={index} messageReply={msg}></ReplyedMessage>
))} ))}
{showAlertInReplyMessages && <Alert style={{ {showAlertInReplyMessages && <Alert style={{
borderTopLeftRadius: 0, borderTopLeftRadius: 0,

View File

@@ -0,0 +1,267 @@
import { DownloadStatus, useAttachment } from "@/app/providers/AttachmentProvider/useAttachment";
import { AttachmentProps } from "./MessageAttachments";
import { Avatar, Box, Flex, Text, useMantineTheme } from "@mantine/core";
import { AnimatedRoundedProgress } from "../AnimatedRoundedProgress/AnimatedRoundedProgress";
import { IconArrowDown, IconPlayerPauseFilled, IconPlayerPlayFilled, IconX } from "@tabler/icons-react";
import { DeliveredMessageState } from "@/app/providers/DialogProvider/DialogProvider";
import { useRosettaColors } from "@/app/hooks/useRosettaColors";
import { useMemo, useRef } from "react";
import { usePlayerContext } from "@/app/providers/PlayerProvider/usePlayerContext";
const WAVE_BARS = 40;
const BAR_WIDTH = 2;
const BAR_GAP = 2;
const MIN_BAR_HEIGHT = 4;
const MAX_BAR_HEIGHT = 24;
function normalizeWaves(source: number[], targetLength: number): number[] {
if (targetLength <= 0) return [];
if (source.length === 0) return Array(targetLength).fill(0);
if (source.length === targetLength) return source;
if (source.length > targetLength) {
const compressed: number[] = [];
const bucketSize = source.length / targetLength;
for (let i = 0; i < targetLength; i++) {
const start = Math.floor(i * bucketSize);
const end = Math.max(start + 1, Math.floor((i + 1) * bucketSize));
let max = 0;
for (let j = start; j < end && j < source.length; j++) {
if (source[j] > max) max = source[j];
}
compressed.push(max);
}
return compressed;
}
if (targetLength === 1) return [source[0]];
const stretched: number[] = [];
const lastSourceIndex = source.length - 1;
for (let i = 0; i < targetLength; i++) {
const position = (i * lastSourceIndex) / (targetLength - 1);
const left = Math.floor(position);
const right = Math.min(Math.ceil(position), lastSourceIndex);
if (left === right) {
stretched.push(source[left]);
continue;
}
const t = position - left;
stretched.push(source[left] * (1 - t) + source[right] * t);
}
return stretched;
}
function formatTime(seconds: number) {
const s = Math.max(0, Math.floor(seconds));
const m = Math.floor(s / 60).toString().padStart(2, "0");
const r = (s % 60).toString().padStart(2, "0");
return `${m}:${r}`;
}
export function MessageVoice(props: AttachmentProps) {
const { downloadPercentage, downloadStatus, uploadedPercentage, download, getPreview } = useAttachment(
props.attachment,
props.parent,
);
const theme = useMantineTheme();
const colors = useRosettaColors();
const preview = getPreview() || "";
const [durationPart = "0", wavesPart = ""] = preview.split("::");
const previewDuration = Number.parseInt(durationPart, 10) || 0;
const rawWaves = useMemo(
() =>
wavesPart
.split(",")
.map((s) => Number.parseFloat(s))
.filter((n) => Number.isFinite(n) && n >= 0),
[wavesPart]
);
const waves = useMemo(() => normalizeWaves(rawWaves, WAVE_BARS), [rawWaves]);
const peak = useMemo(() => {
const max = Math.max(...waves, 0);
return max > 0 ? max : 1;
}, [waves]);
const isUploading =
props.delivered === DeliveredMessageState.WAITING &&
uploadedPercentage > 0 &&
uploadedPercentage < 100;
const error = downloadStatus === DownloadStatus.ERROR;
const waveformWidth = WAVE_BARS * BAR_WIDTH + (WAVE_BARS - 1) * BAR_GAP;
const waveformRef = useRef<HTMLDivElement | null>(null);
const {
playAudio,
pause,
duration: currentDuration,
playing,
setDuration,
totalDuration,
currentMessageId,
} = usePlayerContext();
const messageId = String((props.parent as any)?.id ?? (props.attachment as any)?.messageId ?? props.attachment.id);
const isCurrentTrack = currentMessageId === messageId;
const fullDuration = Math.max(isCurrentTrack && totalDuration > 0 ? totalDuration : previewDuration, 1);
const safeCurrent = isCurrentTrack ? currentDuration : 0;
const playbackProgress = Math.max(0, Math.min(1, safeCurrent / fullDuration));
const createAudioBlob = () => new Blob([Buffer.from(props.attachment.blob, "hex")], { type: "audio/webm;codecs=opus" });
const ensureStarted = (seekToSec?: number) => {
const blob = createAudioBlob();
playAudio("Voice Message", "", blob, messageId);
if (typeof seekToSec === "number") {
requestAnimationFrame(() => setDuration(seekToSec));
}
};
const handleMainAction = () => {
if (error) return;
if (downloadStatus !== DownloadStatus.DOWNLOADED) {
download();
return;
}
if (!isCurrentTrack) {
ensureStarted();
return;
}
if (playing) {
pause();
return;
}
ensureStarted(Math.max(0, safeCurrent));
};
const handleSeek = (e: React.MouseEvent<HTMLDivElement>) => {
if (error || downloadStatus !== DownloadStatus.DOWNLOADED) return;
const rect = waveformRef.current?.getBoundingClientRect();
if (!rect || rect.width <= 0) return;
const x = e.clientX - rect.left;
const progress = Math.max(0, Math.min(1, x / rect.width));
const seekTo = progress * fullDuration;
if (!isCurrentTrack) {
ensureStarted(seekTo);
return;
}
setDuration(seekTo);
};
const timeText =
isCurrentTrack && safeCurrent > 0
? `-${formatTime(Math.max(0, fullDuration - safeCurrent))}`
: formatTime(fullDuration);
return (
<Flex gap="sm" align="center">
<Avatar
bg={error ? colors.error : colors.brandColor}
size={40}
style={{ cursor: "pointer", position: "relative" }}
onClick={handleMainAction}
>
{!error && (
<>
{downloadStatus === DownloadStatus.DOWNLOADING && (
<div style={{ position: "absolute", top: 0, left: 0 }}>
<AnimatedRoundedProgress size={40} value={Math.max(1, downloadPercentage)} />
</div>
)}
{isUploading && (
<div style={{ position: "absolute", top: 0, left: 0 }}>
<AnimatedRoundedProgress color="#fff" size={40} value={uploadedPercentage} />
</div>
)}
{downloadStatus !== DownloadStatus.DOWNLOADED && <IconArrowDown color="white" size={22} />}
{downloadStatus === DownloadStatus.DOWNLOADED && !isUploading &&
(isCurrentTrack && playing ? (
<IconPlayerPauseFilled color="white" size={22} />
) : (
<IconPlayerPlayFilled color="white" size={22} />
))}
</>
)}
{(error || isUploading) && <IconX color="white" size={22} />}
</Avatar>
<Flex direction="column">
<Box
ref={waveformRef}
w={waveformWidth}
h={32}
onClick={handleSeek}
style={{ overflow: "hidden", cursor: "pointer" }}
>
<Flex h="100%" align="center" gap={BAR_GAP} wrap="nowrap">
{waves.map((value, index) => {
const normalized = Math.max(0, Math.min(1, value / peak));
const height = Math.max(
MIN_BAR_HEIGHT,
Math.min(MAX_BAR_HEIGHT, MIN_BAR_HEIGHT + normalized * (MAX_BAR_HEIGHT - MIN_BAR_HEIGHT))
);
const passed = playbackProgress * waves.length - index;
const fillPercent = Math.max(0, Math.min(1, passed));
const inactiveColor = theme.colors.gray[4];
const activeColor = colors.brandColor;
let background = inactiveColor;
if (fillPercent >= 1) {
background = activeColor;
} else if (fillPercent > 0) {
background = `linear-gradient(90deg, ${activeColor} 0%, ${activeColor} ${fillPercent * 100}%, ${inactiveColor} ${fillPercent * 100}%, ${inactiveColor} 100%)`;
}
return (
<Box
key={index}
w={BAR_WIDTH}
h={height}
style={{
flex: `0 0 ${BAR_WIDTH}px`,
borderRadius: 999,
background,
}}
/>
);
})}
</Flex>
</Box>
<Text size="xs" c="dimmed">
{timeText}
</Text>
</Flex>
</Flex>
);
}

View File

@@ -104,7 +104,10 @@ export function Message(props: MessageProps) {
message: props.message, message: props.message,
attachments: props.attachments.filter(a => a.type != AttachmentType.MESSAGES), attachments: props.attachments.filter(a => a.type != AttachmentType.MESSAGES),
message_id: props.message_id, message_id: props.message_id,
/**
* Кодируем в hex чтобы было удобнее передавать по сети
*/
chacha_key_plain: props.chacha_key_plain
}; };
const avatars = useAvatars(user.publicKey); const avatars = useAvatars(user.publicKey);
@@ -126,6 +129,9 @@ export function Message(props: MessageProps) {
if (props.replyed) { if (props.replyed) {
return false; return false;
} }
if(props.chacha_key_plain == ""){
return false;
}
if (messageReply.attachments.find((v) => ATTACHMENTS_NOT_ALLOWED_TO_REPLY.includes(v.type))) { if (messageReply.attachments.find((v) => ATTACHMENTS_NOT_ALLOWED_TO_REPLY.includes(v.type))) {
return false; return false;
} }
@@ -215,7 +221,7 @@ export function Message(props: MessageProps) {
fontSize: '13px', fontSize: '13px',
color: messageStyle == MessageStyle.BUBBLES ? (computedTheme == 'light' ? (props.parent?.from_me ? 'white' : 'black') : 'white') : (computedTheme == 'light' ? 'black' : 'white') color: messageStyle == MessageStyle.BUBBLES ? (computedTheme == 'light' ? (props.parent?.from_me ? 'white' : 'black') : 'white') : (computedTheme == 'light' ? 'black' : 'white')
}} ml={props.avatar_no_render ? 50 : undefined} onDoubleClick={(e) => e.stopPropagation()}> }} ml={props.avatar_no_render ? 50 : undefined} onDoubleClick={(e) => e.stopPropagation()}>
<TextParser performanceEntityLimit={ENTITY_LIMITS_TO_PARSE_IN_MESSAGE} oversizeIfTextSmallerThan={1} text={props.message.trim() + props.chacha_key_plain}></TextParser> <TextParser performanceEntityLimit={ENTITY_LIMITS_TO_PARSE_IN_MESSAGE} oversizeIfTextSmallerThan={1} text={props.message.trim()}></TextParser>
</Box> </Box>
</Flex> </Flex>
</Flex> </Flex>

View File

@@ -24,6 +24,8 @@ export interface SettingsInputDefaultProps {
mt?: StyleProp<MantineSpacing>; mt?: StyleProp<MantineSpacing>;
rightSection?: ReactNode; rightSection?: ReactNode;
type?: HTMLInputTypeAttribute; type?: HTMLInputTypeAttribute;
onErrorStateChange?: (error: boolean) => void;
regexp?: RegExp;
} }
export interface SettingsInputGroupProps { export interface SettingsInputGroupProps {
mt?: StyleProp<MantineSpacing>; mt?: StyleProp<MantineSpacing>;
@@ -260,7 +262,6 @@ function SettingsInputClickable(
function SettingsInputDefault(props : SettingsInputDefaultProps) { function SettingsInputDefault(props : SettingsInputDefaultProps) {
const colors = useRosettaColors(); const colors = useRosettaColors();
const input = useRef<any>(undefined); const input = useRef<any>(undefined);
const onClick = (e : MouseEvent) => { const onClick = (e : MouseEvent) => {
e.stopPropagation(); e.stopPropagation();
if(!props.disabled){ if(!props.disabled){
@@ -268,6 +269,19 @@ function SettingsInputDefault(props : SettingsInputDefaultProps) {
return; return;
} }
} }
const onChange = (e) => {
let value = e.target.value;
if(props.regexp && !props.regexp.test(value)) {
props.onErrorStateChange && props.onErrorStateChange(true);
props.onChange && props.onChange(e);
}else{
props.onErrorStateChange && props.onErrorStateChange(false);
console.info('fa');
props.onChange && props.onChange(e);
}
}
return (<> return (<>
<Paper mt={props.mt} style={props.style} withBorder styles={{ <Paper mt={props.mt} style={props.style} withBorder styles={{
root: { root: {
@@ -298,7 +312,7 @@ function SettingsInputDefault(props : SettingsInputDefaultProps) {
{!props.rightSection && ( {!props.rightSection && (
<Input type={props.type} defaultValue={!props.onChange ? props.value : undefined} value={!props.onChange ? undefined : props.value} ref={input} disabled={props.disabled} onClick={(e) => { <Input type={props.type} defaultValue={!props.onChange ? props.value : undefined} value={!props.onChange ? undefined : props.value} ref={input} disabled={props.disabled} onClick={(e) => {
onClick(e) onClick(e)
}} onChange={props.onChange} variant={'unstyled'} spellCheck={false} color="gray" classNames={{ }} onChange={onChange} variant={'unstyled'} spellCheck={false} color="gray" classNames={{
input: classes.input input: classes.input
}} placeholder={props.placeholder}></Input>) }} placeholder={props.placeholder}></Input>)
} }

View File

@@ -42,6 +42,20 @@ export function TextParser(props: TextParserProps) {
const theme = useMantineTheme(); const theme = useMantineTheme();
let entityCount = 0; let entityCount = 0;
const UNICODE_EMOJI_SEQUENCE_REGEX =
/(?:\p{Regional_Indicator}{2}|[0-9#*]\uFE0F?\u20E3|\p{Extended_Pictographic}(?:\uFE0F|\uFE0E)?(?:\p{Emoji_Modifier})?(?:\u200D\p{Extended_Pictographic}(?:\uFE0F|\uFE0E)?(?:\p{Emoji_Modifier})?)*)/u;
const UNICODE_EMOJI_SEQUENCE_REGEX_GLOBAL = new RegExp(
UNICODE_EMOJI_SEQUENCE_REGEX.source,
"gu"
);
const toUnified = (value: string): string =>
Array.from(value)
.map((ch) => ch.codePointAt(0)?.toString(16))
.filter(Boolean)
.join("-");
const formatRules : FormatRule[] = [ const formatRules : FormatRule[] = [
{ {
pattern: [ pattern: [
@@ -58,7 +72,7 @@ export function TextParser(props: TextParserProps) {
return <Anchor style={{ return <Anchor style={{
userSelect: 'auto', userSelect: 'auto',
color: props.__reserved_2 ? theme.colors.blue[2] : undefined color: props.__reserved_2 ? theme.colors.blue[2] : undefined
}} fz={14} href={match.startsWith('http') ? match : 'https://' + match} target="_blank" rel="noopener noreferrer">{match}</Anchor>; }} fz={13} href={match.startsWith('http') ? match : 'https://' + match} target="_blank" rel="noopener noreferrer">{match}</Anchor>;
}, },
flush: (match: string) => { flush: (match: string) => {
return <>{match}</>; return <>{match}</>;
@@ -120,17 +134,20 @@ export function TextParser(props: TextParserProps) {
} }
}, },
{ {
// unicode emojis // unicode emojis (including composite sequences)
pattern: [/\p{Emoji_Presentation}/u], pattern: [UNICODE_EMOJI_SEQUENCE_REGEX],
render: (match: string) => { render: (match: string) => {
let textWithoutEmojis = props.text.replace(/\p{Emoji_Presentation}/gu, ''); const textWithoutEmojis = props.text.replace(UNICODE_EMOJI_SEQUENCE_REGEX_GLOBAL, "");
const unified = toUnified(match);
if (textWithoutEmojis.length <= (props.oversizeIfTextSmallerThan ?? 0)) { if (textWithoutEmojis.length <= (props.oversizeIfTextSmallerThan ?? 0)) {
return <Emoji size={40} unified={match.codePointAt(0)?.toString(16) || ''}></Emoji>; return <Emoji size={40} unified={unified}></Emoji>;
} }
return <Emoji unified={match.codePointAt(0)?.toString(16) || ''}></Emoji>;
return <Emoji unified={unified}></Emoji>;
}, },
flush: (match: string) => { flush: (match: string) => {
return <Emoji unified={match.codePointAt(0)?.toString(16) || ''}></Emoji>; return <Emoji unified={toUnified(match)}></Emoji>;
} }
}, },
{ {

View File

@@ -5,15 +5,23 @@ import { useNavigate } from 'react-router-dom';
import { useUserInformation } from '@/app/providers/InformationProvider/useUserInformation'; import { useUserInformation } from '@/app/providers/InformationProvider/useUserInformation';
import { usePublicKey } from '@/app/providers/AccountProvider/usePublicKey'; import { usePublicKey } from '@/app/providers/AccountProvider/usePublicKey';
import { useAvatars } from '@/app/providers/AvatarProvider/useAvatars'; import { useAvatars } from '@/app/providers/AvatarProvider/useAvatars';
import { useEffect } from 'react';
export function UserButton() { export function UserButton() {
const navigate = useNavigate(); const navigate = useNavigate();
const publicKey = usePublicKey(); const publicKey = usePublicKey();
const [userInfo] = useUserInformation(publicKey); const [userInfo, _, forceUpdateUserInformation] = useUserInformation(publicKey);
const avatars = useAvatars(publicKey); const avatars = useAvatars(publicKey);
const loading = userInfo.publicKey !== publicKey; const loading = userInfo.publicKey !== publicKey;
useEffect(() => {
/**
* Обновляем информацию о пользователе принудительно при рендере левого меню
*/
forceUpdateUserInformation();
}, []);
return ( return (
<UnstyledButton p={'sm'} className={classes.user} onClick={() => navigate("/main/profile/me")}> <UnstyledButton p={'sm'} className={classes.user} onClick={() => navigate("/main/profile/me")}>
<Group> <Group>

View File

@@ -0,0 +1,278 @@
import { Box, Flex, Text, useMantineTheme } from "@mantine/core";
import { useEffect, useRef, useState } from "react";
interface VoiceRecorderProps {
duration: number;
waves: number[];
}
type AnimatedBar = {
id: number;
value: number;
entered: boolean;
};
const VISIBLE_BARS = 50;
const BAR_WIDTH = 3;
const BAR_GAP = 2;
const STEP_PX = BAR_WIDTH + BAR_GAP;
const COMPONENT_HEIGHT = 45;
const MAX_BAR_HEIGHT = 28;
const MIN_BAR_HEIGHT = 4;
export function VoiceRecorder(props: VoiceRecorderProps) {
const theme = useMantineTheme();
const [bars, setBars] = useState<AnimatedBar[]>([]);
const [subShift, setSubShift] = useState(0);
const prevLengthRef = useRef(0);
const prevWavesRef = useRef<number[]>([]);
const idRef = useRef(0);
const enterFrameRef = useRef<number | null>(null);
const scrollFrameRef = useRef<number | null>(null);
const lastAppendAtRef = useRef<number | null>(null);
const appendIntervalRef = useRef(120);
const barsLengthRef = useRef(0);
const formatDuration = (seconds: number) => {
const mins = Math.floor(seconds / 60)
.toString()
.padStart(2, "0");
const secs = (seconds % 60).toString().padStart(2, "0");
return `${mins}:${secs}`;
};
useEffect(() => {
barsLengthRef.current = bars.length;
}, [bars.length]);
useEffect(() => {
if (props.waves.length === 0) {
setBars([]);
setSubShift(0);
prevLengthRef.current = 0;
prevWavesRef.current = [];
lastAppendAtRef.current = null;
return;
}
if (props.waves.length < prevLengthRef.current) {
const resetBars = props.waves.slice(-VISIBLE_BARS).map((value) => ({
id: idRef.current++,
value,
entered: true,
}));
setBars(resetBars);
setSubShift(0);
prevLengthRef.current = props.waves.length;
prevWavesRef.current = props.waves;
lastAppendAtRef.current = performance.now();
return;
}
const prevWaves = prevWavesRef.current;
let appended: number[] = [];
// Обычный режим: длина выросла
if (props.waves.length > prevLengthRef.current) {
appended = props.waves.slice(prevLengthRef.current);
} else if (props.waves.length === prevLengthRef.current && props.waves.length > 0) {
// Rolling buffer: длина та же, но данные сдвигаются
let changed = false;
if (prevWaves.length !== props.waves.length) {
changed = true;
} else {
for (let i = 0; i < props.waves.length; i++) {
if (props.waves[i] !== prevWaves[i]) {
changed = true;
break;
}
}
}
if (changed) {
appended = [props.waves[props.waves.length - 1]];
}
}
if (appended.length > 0) {
const now = performance.now();
if (lastAppendAtRef.current != null) {
const dt = now - lastAppendAtRef.current;
const perBar = dt / appended.length;
appendIntervalRef.current = appendIntervalRef.current * 0.7 + perBar * 0.3;
}
lastAppendAtRef.current = now;
setSubShift(0);
const newIds: number[] = [];
setBars((prev) => {
const next = [...prev];
appended.forEach((value) => {
const id = idRef.current++;
newIds.push(id);
next.push({
id,
value,
entered: false,
});
});
return next.slice(-VISIBLE_BARS);
});
if (enterFrameRef.current) {
cancelAnimationFrame(enterFrameRef.current);
}
enterFrameRef.current = requestAnimationFrame(() => {
setBars((prev) => {
const ids = new Set(newIds);
return prev.map((bar) => (ids.has(bar.id) ? { ...bar, entered: true } : bar));
});
});
}
prevLengthRef.current = props.waves.length;
prevWavesRef.current = props.waves;
}, [props.waves]);
useEffect(() => {
const tick = () => {
const startedAt = lastAppendAtRef.current;
if (startedAt != null) {
const elapsed = performance.now() - startedAt;
const interval = Math.max(16, appendIntervalRef.current);
const progress = Math.min(1, elapsed / interval);
const smoothShift = barsLengthRef.current >= VISIBLE_BARS ? -progress * STEP_PX : 0;
setSubShift(smoothShift);
} else {
setSubShift(0);
}
scrollFrameRef.current = requestAnimationFrame(tick);
};
scrollFrameRef.current = requestAnimationFrame(tick);
return () => {
if (scrollFrameRef.current) {
cancelAnimationFrame(scrollFrameRef.current);
}
};
}, []);
useEffect(() => {
return () => {
if (enterFrameRef.current) {
cancelAnimationFrame(enterFrameRef.current);
}
if (scrollFrameRef.current) {
cancelAnimationFrame(scrollFrameRef.current);
}
};
}, []);
const waveformWidth = VISIBLE_BARS * BAR_WIDTH + (VISIBLE_BARS - 1) * BAR_GAP;
return (
<Flex
direction="row"
h={COMPONENT_HEIGHT}
mih={COMPONENT_HEIGHT}
mah={COMPONENT_HEIGHT}
align="center"
justify="center"
gap="xs"
px={6}
>
<Text size="xs" c="dimmed" w={36}>
{formatDuration(props.duration)}
</Text>
<Box
w={waveformWidth}
h={COMPONENT_HEIGHT}
style={{
overflow: "hidden",
}}
>
<Flex
h="100%"
align="center"
gap={BAR_GAP}
wrap="nowrap"
style={{ transform: `translateX(${subShift}px)` }}
>
{Array.from({ length: VISIBLE_BARS }).map((_, index) => {
const bar = bars[index];
if (!bar) {
return (
<Box
key={`empty-${index}`}
w={BAR_WIDTH}
h={MIN_BAR_HEIGHT}
style={{
flex: `0 0 ${BAR_WIDTH}px`,
borderRadius: 999,
background: theme.colors.gray[3],
opacity: 0.22,
}}
/>
);
}
const normalized = Math.max(0, Math.min(1, bar.value));
const height = Math.max(
MIN_BAR_HEIGHT,
Math.min(MAX_BAR_HEIGHT, MIN_BAR_HEIGHT + normalized * (MAX_BAR_HEIGHT - MIN_BAR_HEIGHT))
);
const isLast = index === bars.length - 1;
const isNearTail = index >= bars.length - 3;
return (
<Box
key={bar.id}
w={BAR_WIDTH}
h={height}
style={{
flex: `0 0 ${BAR_WIDTH}px`,
alignSelf: "center",
borderRadius: 999,
background: isLast
? `linear-gradient(180deg, ${theme.colors.blue[3]} 0%, ${theme.colors.blue[5]} 100%)`
: `linear-gradient(180deg, ${theme.colors.blue[4]} 0%, ${theme.colors.blue[6]} 100%)`,
boxShadow: isLast
? `0 0 10px ${theme.colors.blue[4]}55`
: isNearTail
? `0 0 6px ${theme.colors.blue[4]}22`
: "none",
transform: bar.entered ? "scaleY(1)" : "scaleY(0.18)",
transformOrigin: "center center",
transition: [
"height 260ms cubic-bezier(0.2, 0.8, 0.2, 1)",
"transform 260ms cubic-bezier(0.2, 0.8, 0.2, 1)",
"opacity 220ms ease",
"box-shadow 220ms ease",
].join(", "),
willChange: "height, transform, opacity",
}}
/>
);
})}
</Flex>
</Box>
</Flex>
);
}

View File

@@ -57,5 +57,7 @@ export const ALLOWED_DOMAINS_ZONES = [
'fm', 'fm',
'tv', 'tv',
'im', 'im',
'sc' 'sc',
'su',
'by'
]; ];

View File

@@ -36,7 +36,7 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
const publicKey = usePublicKey(); const publicKey = usePublicKey();
const privatePlain = usePrivatePlain(); const privatePlain = usePrivatePlain();
const {updateAttachmentInDialogCache} = useDialogsCache(); const {updateAttachmentInDialogCache} = useDialogsCache();
const {info} = useConsoleLogger('useAttachment'); const { info, error } = useConsoleLogger('useAttachment');
const {updateAttachmentsInMessagesByAttachmentId} = useDialog(); const {updateAttachmentsInMessagesByAttachmentId} = useDialog();
const {getDownloadsPath} = useCore(); const {getDownloadsPath} = useCore();
const {hasGroup} = useGroups(); const {hasGroup} = useGroups();
@@ -53,6 +53,7 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
} }
const calcDownloadStatus = async () => { const calcDownloadStatus = async () => {
console.info("ds", attachment);
if (downloadStatus == DownloadStatus.DOWNLOADED) { if (downloadStatus == DownloadStatus.DOWNLOADED) {
return; return;
} }
@@ -136,12 +137,12 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
downloadedBlob = await downloadFile(attachment.id, downloadedBlob = await downloadFile(attachment.id,
downloadTag, attachment.transport.transport_server); downloadTag, attachment.transport.transport_server);
} catch (e) { } catch (e) {
console.info(e); error("Error downloading attachment: " + attachment.id);
info("Error downloading attachment: " + attachment.id);
setDownloadStatus(DownloadStatus.ERROR); setDownloadStatus(DownloadStatus.ERROR);
return; return;
} }
setDownloadStatus(DownloadStatus.DECRYPTING); setDownloadStatus(DownloadStatus.DECRYPTING);
console.info("decoding with key " + parentMessage.chacha_key_plain);
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex')); //console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
const decrypted = await decodeWithPassword(parentMessage.chacha_key_plain, downloadedBlob); const decrypted = await decodeWithPassword(parentMessage.chacha_key_plain, downloadedBlob);
setDownloadTag(""); setDownloadTag("");

View File

@@ -9,20 +9,18 @@ import { useDialogsList } from "../DialogListProvider/useDialogsList";
import { useDatabase } from "../DatabaseProvider/useDatabase"; import { useDatabase } from "../DatabaseProvider/useDatabase";
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger"; import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
import { useDialogsCache } from "../DialogProvider/useDialogsCache"; import { useDialogsCache } from "../DialogProvider/useDialogsCache";
import { DialogContext } from "../DialogProvider/DialogProvider"; import { AttachmentMeta, DialogContext } from "../DialogProvider/DialogProvider";
import { useTransportServer } from "../TransportProvider/useTransportServer"; import { useTransportServer } from "../TransportProvider/useTransportServer";
import { usePublicKey } from "../AccountProvider/usePublicKey";
export function usePrepareAttachment() { export function usePrepareAttachment() {
const intervalsRef = useRef<NodeJS.Timeout>(null); const intervalsRef = useRef<NodeJS.Timeout>(null);
const {uploadFile} = useTransport(); const {uploadFile} = useTransport();
const {updateDialog} = useDialogsList(); const {updateDialog} = useDialogsList();
const {runQuery} = useDatabase(); const {runQuery, getQuery} = useDatabase();
const {info} = useConsoleLogger('usePrepareAttachment'); const {info, error} = useConsoleLogger('usePrepareAttachment');
const {getDialogCache} = useDialogsCache(); const {getDialogCache} = useDialogsCache();
const context = useContext(DialogContext); const context = useContext(DialogContext);
const transportServer = useTransportServer(); const transportServer = useTransportServer();
const publicKey = usePublicKey();
const updateTimestampInDialogCache = (dialog : string, message_id: string) => { const updateTimestampInDialogCache = (dialog : string, message_id: string) => {
const dialogCache = getDialogCache(dialog); const dialogCache = getDialogCache(dialog);
@@ -37,6 +35,79 @@ export function usePrepareAttachment() {
} }
} }
/**
* Обновляет транспортный сервер в кэше, чтобы поддерживать его в актуальном состоянии после загрузки
*/
const updateAttachmentTransportInCache = (dialog: string, message_id : string, attachment: Attachment) => {
const dialogCache = getDialogCache(dialog);
if(dialogCache == null){
return;
}
for(let i = 0; i < dialogCache.length; i++){
if(dialogCache[i].message_id == message_id){
for(let j = 0; j < dialogCache[i].attachments.length; j++){
if(dialogCache[i].attachments[j].id == attachment.id){
dialogCache[i].attachments[j].transport = attachment.transport;
}
}
}
}
}
/**
* Обновляет транспорт в базе после загрузки вложения (нам нужно сохранить транспорт)
*/
const updateAttachmentTransportInDatabase = async (message_id : string, attachment: Attachment) => {
let message = await getQuery(`SELECT attachments FROM messages WHERE message_id = ?`, [message_id]);
console.info(message)
if(!message){
return;
}
if(message.attachments == '[]'){
return;
}
let meta : AttachmentMeta[] = JSON.parse(message.attachments);
for(let i = 0; i < meta.length; i++){
if(meta[i].id == attachment.id){
meta[i].transport = attachment.transport;
}
}
await runQuery(`UPDATE messages SET attachments = ? WHERE message_id = ?`, [JSON.stringify(meta), message_id]);
}
/**
* Обновляет вложение в стейте сообщений
*/
const updateAttachmentTransportInContext = (message_id: string, attachment : Attachment) => {
if(context == null || !context){
/**
* Если этот диалог сейчас не открыт
*/
return;
}
context.setMessages((prev) => {
return prev.map((value) => {
if(value.message_id != message_id){
return value;
}
for(let i = 0; i < value.attachments.length; i++){
if(value.attachments[i].id != attachment.id){
return value;
}
value.attachments[i].transport = attachment.transport;
return value;
}
return value;
})
});
}
const updateTransportAfterUploading = async (dialog: string, message_id : string, attachment: Attachment) => {
updateAttachmentTransportInCache(dialog, message_id, attachment);
updateAttachmentTransportInDatabase(message_id, attachment);
updateAttachmentTransportInContext(message_id, attachment);
}
/** /**
* Обновляет временную метку в сообщении, пока вложения отправляются, * Обновляет временную метку в сообщении, пока вложения отправляются,
* потому что если этого не делать, то сообщение может быть помечено как * потому что если этого не делать, то сообщение может быть помечено как
@@ -85,7 +156,7 @@ export function usePrepareAttachment() {
* а так же из-за надежности доставки файлов через HTTP * а так же из-за надежности доставки файлов через HTTP
* @param attachments Attachments to prepare for sending * @param attachments Attachments to prepare for sending
*/ */
const prepareAttachmentsToSend = async (message_id: string, dialog: string, password: string, attachments : Attachment[], rePrepared : boolean = false) : Promise<Attachment[]> => { const prepareAttachmentsToSend = async (message_id: string, dialog: string, password: string, attachments : Attachment[]) : Promise<Attachment[]> => {
if(attachments.length <= 0){ if(attachments.length <= 0){
return []; return [];
} }
@@ -101,9 +172,11 @@ export function usePrepareAttachment() {
continue; continue;
} }
if(attachment.type == AttachmentType.MESSAGES){ if(attachment.type == AttachmentType.MESSAGES){
let reply : MessageReply[] = JSON.parse(attachment.blob) let reply : MessageReply[] = JSON.parse(attachment.blob);
for(let j = 0; j < reply.length; j++){ for(let j = 0; j < reply.length; j++){
reply[j].attachments = await prepareAttachmentsToSend(message_id, dialog, password, reply[j].attachments, true); for(let k = 0; k < reply[j].attachments.length; k++){
reply[j].attachments[k].blob = "";
}
} }
prepared.push({ prepared.push({
...attachment, ...attachment,
@@ -119,39 +192,35 @@ export function usePrepareAttachment() {
const blurhash = await base64ImageToBlurhash(attachment.blob); const blurhash = await base64ImageToBlurhash(attachment.blob);
attachment.preview = blurhash; attachment.preview = blurhash;
} }
if(rePrepared && (attachment.encoding.encoded_for == dialog || attachment.encoding.encoder == dialog)){
/**
* Это пересланное сообщение и оно уже закодировано для этого диалога, или закодировано отправителем, значит не нужно его кодировать и загружать заново
*/
prepared.push({
...attachment,
blob: ""
});
continue;
}
doTimestampUpdateImMessageWhileAttachmentsSend(message_id, dialog); doTimestampUpdateImMessageWhileAttachmentsSend(message_id, dialog);
const content = await encodeWithPassword(password, attachment.blob); const content = await encodeWithPassword(password, attachment.blob);
const upid = attachment.id; const upid = attachment.id;
info(`Uploading attachment with upid: ${upid}`); info(`Uploading attachment with upid: ${upid}`);
info(`Attachment content length: ${content.length}`); info(`Attachment content length: ${content.length}`);
let tag = await uploadFile(upid, content); let tag = await uploadFile(upid, content).catch(() => {
info(`Uploaded attachment with upid: ${upid}, received tag: ${tag}`); error(`Network error while uploading attachment ${upid}`);
if(intervalsRef.current != null){ });
clearInterval(intervalsRef.current); if(!tag){
/**
* При ошибке загрузки по сети
*/
stopUpdateTimeInUpAttachment();
console.info("stop upd")
continue;
} }
prepared.push({ info(`Uploaded attachment with upid: ${upid}, received tag: ${tag}`);
stopUpdateTimeInUpAttachment();
const preparedAttachment : Attachment = {
...attachment, ...attachment,
transport: { transport: {
transport_server: transportServer || "", transport_server: transportServer || "",
transport_tag: tag transport_tag: tag
}, },
encoding: {
encoded_for: dialog,
encoder: publicKey
},
preview: attachment.preview, preview: attachment.preview,
blob: "" blob: ""
}); };
await updateTransportAfterUploading(dialog, message_id, preparedAttachment);
prepared.push(preparedAttachment);
} }
return prepared; return prepared;
}catch(e){ }catch(e){
@@ -159,6 +228,12 @@ export function usePrepareAttachment() {
} }
} }
const stopUpdateTimeInUpAttachment = () => {
if(intervalsRef.current != null){
clearInterval(intervalsRef.current);
}
}
return { return {
prepareAttachmentsToSend prepareAttachmentsToSend
} }

View File

@@ -84,6 +84,13 @@ export function CallProvider(props : CallProviderProps) {
const mutedRef = useRef<boolean>(false); const mutedRef = useRef<boolean>(false);
const soundRef = useRef<boolean>(true); const soundRef = useRef<boolean>(true);
const {sendMessage} = useDeattachedSender(); const {sendMessage} = useDeattachedSender();
const hasRemoteTrackRef = useRef<boolean>(false);
/**
* Используются для входа в звонок
*/
const callSessionIdRef = useRef<string>("");
const callTokenRef = useRef<string>("");
const {playSound, stopSound, stopLoopSound} = useSound(); const {playSound, stopSound, stopLoopSound} = useSound();
const {setWindowPriority} = useWindow(); const {setWindowPriority} = useWindow();
@@ -169,7 +176,6 @@ export function CallProvider(props : CallProviderProps) {
* Другая сторона отправила нам ICE кандидата для установления WebRTC соединения * Другая сторона отправила нам ICE кандидата для установления WebRTC соединения
*/ */
const candidate = JSON.parse(packet.getSdpOrCandidate()); const candidate = JSON.parse(packet.getSdpOrCandidate());
console.info(candidate);
if(peerConnectionRef.current?.remoteDescription == null){ if(peerConnectionRef.current?.remoteDescription == null){
/** /**
* Удаленное описание еще не установлено, буферизуем кандидата, чтобы добавить его после установки удаленного описания * Удаленное описание еще не установлено, буферизуем кандидата, чтобы добавить его после установки удаленного описания
@@ -210,16 +216,14 @@ export function CallProvider(props : CallProviderProps) {
openCallsModal("The connection with the user was lost. The call has ended.") openCallsModal("The connection with the user was lost. The call has ended.")
end(); end();
} }
if(activeCall){ if(signalType == SignalType.RINGING_TIMEOUT) {
/** /**
* У нас уже есть активный звонок, игнорируем все сигналы, кроме сигналов от текущего звонка * Другой стороне был отправлен сигнал звонка, но она не ответила на него в течении определенного времени
*/ */
if(packet.getSrc() != activeCall && packet.getSrc() != publicKey){ openCallsModal("The user did not answer the call in time. Please try again later.");
console.info("Received signal from " + packet.getSrc() + " but active call is with " + activeCall + ", ignoring"); end();
info("Received signal for another call, ignoring");
return; return;
} }
}
if(signalType == SignalType.END_CALL){ if(signalType == SignalType.END_CALL){
/** /**
* Сбросили звонок * Сбросили звонок
@@ -231,52 +235,24 @@ export function CallProvider(props : CallProviderProps) {
/** /**
* Нам поступает звонок * Нам поступает звонок
*/ */
if(callState != CallState.ENDED){
/**
* У нас уже есть активный звонок, отправляем сигнал другой стороне, что линия занята
*/
return;
}
callSessionIdRef.current = packet.getCallId();
callTokenRef.current = packet.getJoinToken();
setWindowPriority(true); setWindowPriority(true);
playSound("ringtone.mp3", true); playSound("ringtone.mp3", true);
setActiveCall(packet.getSrc()); setActiveCall(packet.getSrc());
setCallState(CallState.INCOMING); setCallState(CallState.INCOMING);
setShowCallView(true); setShowCallView(true);
} }
if(signalType == SignalType.KEY_EXCHANGE && roleRef.current == CallRole.CALLER){ if(signalType == SignalType.KEY_EXCHANGE){
console.info("EXCHANGE SIGNAL RECEIVED, CALLER ROLE");
/**
* Другая сторона сгенерировала ключи для сессии и отправила нам публичную часть,
* теперь мы можем создать общую секретную сессию для шифрования звонка
*/
const sharedPublic = packet.getSharedPublic();
if(!sharedPublic){
info("Received key exchange signal without shared public key");
return;
}
const sessionKeys = generateSessionKeys();
const computedSharedSecret = nacl.box.before(Buffer.from(sharedPublic, 'hex'), sessionKeys.secretKey);
sharedSecretRef.current = Buffer.from(computedSharedSecret).toString('hex');
info("Generated shared secret for call session: " + sharedSecretRef.current);
/**
* Нам нужно отправить свой публичный ключ другой стороне, чтобы она тоже могла создать общую секретную сессию
*/
const signalPacket = new PacketSignalPeer();
signalPacket.setSrc(publicKey);
signalPacket.setDst(packet.getSrc());
signalPacket.setSignalType(SignalType.KEY_EXCHANGE);
signalPacket.setSharedPublic(Buffer.from(sessionKeys.publicKey).toString('hex'));
send(signalPacket);
setCallState(CallState.WEB_RTC_EXCHANGE);
/**
* Создаем комнату на сервере SFU, комнату создает звонящий
*/
let webRtcSignal = new PacketSignalPeer();
webRtcSignal.setSignalType(SignalType.CREATE_ROOM);
webRtcSignal.setSrc(publicKey);
webRtcSignal.setDst(packet.getSrc());
send(webRtcSignal);
}
if(signalType == SignalType.KEY_EXCHANGE && roleRef.current == CallRole.CALLEE){
console.info("EXCHANGE SIGNAL RECEIVED, CALLEE ROLE"); console.info("EXCHANGE SIGNAL RECEIVED, CALLEE ROLE");
/** /**
* Мы отправили свою публичную часть ключа другой стороне, * Другая сторона отправила нам ключи, теперь отправляем ей свои для генерации общего секрета
* теперь мы получили ее публичную часть и можем создать общую
* секретную сессию для шифрования звонка
*/ */
const sharedPublic = packet.getSharedPublic(); const sharedPublic = packet.getSharedPublic();
if(!sharedPublic){ if(!sharedPublic){
@@ -287,17 +263,53 @@ export function CallProvider(props : CallProviderProps) {
info("Received key exchange signal but session keys are not generated"); info("Received key exchange signal but session keys are not generated");
return; return;
} }
const computedSharedSecret = nacl.box.before(Buffer.from(sharedPublic, 'hex'), sessionKeys.secretKey); const computedSharedSecret = nacl.box.before(Buffer.from(sharedPublic, 'hex'), sessionKeys.secretKey);
sharedSecretRef.current = Buffer.from(computedSharedSecret).toString('hex'); sharedSecretRef.current = Buffer.from(computedSharedSecret).toString('hex');
info("Generated shared secret for call session: " + sharedSecretRef.current); info("Generated shared secret for call session: " + sharedSecretRef.current);
setCallState(CallState.WEB_RTC_EXCHANGE); setCallState(CallState.WEB_RTC_EXCHANGE);
}
if(signalType == SignalType.CREATE_ROOM) { if(roleRef.current == CallRole.CALLER){
/** /**
* Создана комната для обмена WebRTC потоками * Вызывающий уже отправил ключ, сессия сгенерирована, сообщаем серверу что звонок активен
*/ */
roomIdRef.current = packet.getRoomId(); const activeSignal = new PacketSignalPeer();
info("WebRTC room created with id: " + packet.getRoomId()); activeSignal.setSrc(publicKey);
activeSignal.setDst(activeCall);
activeSignal.setSignalType(SignalType.ACTIVE);
send(activeSignal);
return;
}
const signalPacket = new PacketSignalPeer();
signalPacket.setSrc(publicKey);
signalPacket.setDst(activeCall);
signalPacket.setSignalType(SignalType.KEY_EXCHANGE);
signalPacket.setSharedPublic(Buffer.from(sessionKeys.publicKey).toString('hex'));
send(signalPacket);
}
if(signalType == SignalType.ACCEPT){
/**
* Другая сторона приняла наш звонок, комната на SFU создалась, нужно сгенерировать ключи
*/
const keys = generateSessionKeys();
const signalPacket = new PacketSignalPeer();
signalPacket.setSrc(publicKey);
signalPacket.setDst(activeCall);
signalPacket.setSignalType(SignalType.KEY_EXCHANGE);
signalPacket.setSharedPublic(Buffer.from(keys.publicKey).toString('hex'));
send(signalPacket);
}
if(signalType == SignalType.ACTIVE) {
if(!sessionKeys){
/**
* Сервер может отправить CREATE_ROOM сигнал, даже если мы приняли звонок на другом устройстве, по этому проверяем,
* на этом ли устройстве звонок принят посредством проверки наличия сгенерированных ключей шифрования
*/
stopLoopSound();
stopSound();
end();
return;
}
/** /**
* Нужно отправить свой SDP оффер другой стороне, чтобы установить WebRTC соединение * Нужно отправить свой SDP оффер другой стороне, чтобы установить WebRTC соединение
*/ */
@@ -323,7 +335,11 @@ export function CallProvider(props : CallProviderProps) {
peerConnectionRef.current.onconnectionstatechange = () => { peerConnectionRef.current.onconnectionstatechange = () => {
console.info("Peer connection state changed: " + peerConnectionRef.current?.connectionState); console.info("Peer connection state changed: " + peerConnectionRef.current?.connectionState);
if(peerConnectionRef.current?.connectionState == "connected"){ if(peerConnectionRef.current?.connectionState == "connected"){
setCallState(CallState.ACTIVE); /**
* WebRTC соединение установлено, звонок активен, останавливаем все остальные звуки
* системы
*/
tryActivateCall();
info("WebRTC connection established, call is active"); info("WebRTC connection established, call is active");
} }
} }
@@ -338,7 +354,8 @@ export function CallProvider(props : CallProviderProps) {
* При получении медиа-трека с другой стороны * При получении медиа-трека с другой стороны
*/ */
if(remoteAudioRef.current && event.streams[0]){ if(remoteAudioRef.current && event.streams[0]){
console.info(event.streams); hasRemoteTrackRef.current = true;
tryActivateCall();
remoteAudioRef.current.srcObject = event.streams[0]; remoteAudioRef.current.srcObject = event.streams[0];
remoteAudioRef.current.muted = !soundRef.current; remoteAudioRef.current.muted = !soundRef.current;
void remoteAudioRef.current.play().catch((e) => { void remoteAudioRef.current.play().catch((e) => {
@@ -375,6 +392,15 @@ export function CallProvider(props : CallProviderProps) {
} }
}, [activeCall, sessionKeys, duration]); }, [activeCall, sessionKeys, duration]);
const tryActivateCall = () => {
if(hasRemoteTrackRef.current && peerConnectionRef.current?.connectionState == "connected"){
stopLoopSound();
stopSound();
setCallState(CallState.ACTIVE);
info("Call is now active");
}
}
const openCallsModal = (text : string) => { const openCallsModal = (text : string) => {
modals.open({ modals.open({
centered: true, centered: true,
@@ -384,7 +410,9 @@ export function CallProvider(props : CallProviderProps) {
{text} {text}
</Text> </Text>
<Flex align={'center'} justify={'flex-end'}> <Flex align={'center'} justify={'flex-end'}>
<Button color={'red'} variant={'subtle'} onClick={() => modals.closeAll()} mt="md"> <Button style={{
outline: 'none'
}} color={'red'} variant={'subtle'} onClick={() => modals.closeAll()} mt="md">
Close Close
</Button> </Button>
</Flex> </Flex>
@@ -426,14 +454,20 @@ export function CallProvider(props : CallProviderProps) {
const packetSignal = new PacketSignalPeer(); const packetSignal = new PacketSignalPeer();
packetSignal.setSrc(publicKey); packetSignal.setSrc(publicKey);
packetSignal.setDst(activeCall); packetSignal.setDst(activeCall);
packetSignal.setCallId(callSessionIdRef.current);
packetSignal.setJoinToken(callTokenRef.current);
packetSignal.setSignalType(SignalType.END_CALL); packetSignal.setSignalType(SignalType.END_CALL);
send(packetSignal); send(packetSignal);
end(); end();
} }
const end = () => { const end = () => {
stopLoopSound(); if(callState == CallState.ACTIVE){
stopSound(); /**
* Только если звонок был активен воспроизводим звуки
*/
playSound("end_call.mp3");
}
if (remoteAudioRef.current) { if (remoteAudioRef.current) {
remoteAudioRef.current.pause(); remoteAudioRef.current.pause();
remoteAudioRef.current.srcObject = null; remoteAudioRef.current.srcObject = null;
@@ -442,7 +476,6 @@ export function CallProvider(props : CallProviderProps) {
setDuration(0); setDuration(0);
durationIntervalRef.current && clearInterval(durationIntervalRef.current); durationIntervalRef.current && clearInterval(durationIntervalRef.current);
setWindowPriority(false); setWindowPriority(false);
playSound("end_call.mp3");
peerConnectionRef.current?.close(); peerConnectionRef.current?.close();
peerConnectionRef.current = null; peerConnectionRef.current = null;
roomIdRef.current = ""; roomIdRef.current = "";
@@ -455,6 +488,8 @@ export function CallProvider(props : CallProviderProps) {
setDuration(0); setDuration(0);
setMutedState(false); setMutedState(false);
setSoundState(true); setSoundState(true);
stopLoopSound();
stopSound();
roleRef.current = null; roleRef.current = null;
} }
@@ -476,10 +511,6 @@ export function CallProvider(props : CallProviderProps) {
transport_server: "", transport_server: "",
transport_tag: "" transport_tag: ""
}, },
encoding: {
encoded_for: "",
encoder: ""
},
blob: "" blob: ""
}], true); }], true);
} }
@@ -495,15 +526,22 @@ export function CallProvider(props : CallProviderProps) {
stopLoopSound(); stopLoopSound();
stopSound(); stopSound();
/** /**
* Звонок принят, генерируем ключи для сессии и отправляем их другой стороне для установления защищенного канала связи * Звонок принят, генерируем свой ключ для будующего обмена
*/
generateSessionKeys();
/**
* Отправляем сигнал что звонок принят другой стороне, чтобы она могла начать обмен ключами и установку соединения
*/ */
const keys = generateSessionKeys();
const signalPacket = new PacketSignalPeer(); const signalPacket = new PacketSignalPeer();
signalPacket.setSrc(publicKey); signalPacket.setSrc(publicKey);
signalPacket.setDst(activeCall); signalPacket.setDst(activeCall);
signalPacket.setSignalType(SignalType.KEY_EXCHANGE); signalPacket.setCallId(callSessionIdRef.current);
signalPacket.setSharedPublic(Buffer.from(keys.publicKey).toString('hex')); signalPacket.setJoinToken(callTokenRef.current);
signalPacket.setSignalType(SignalType.ACCEPT);
send(signalPacket); send(signalPacket);
/**
* Устанавливаем состояние звонка и стадию обмена ключами
*/
setCallState(CallState.KEY_EXCHANGE); setCallState(CallState.KEY_EXCHANGE);
roleRef.current = CallRole.CALLEE; roleRef.current = CallRole.CALLEE;
} }

View File

@@ -1,7 +1,7 @@
import { chacha20Decrypt, decodeWithPassword, decrypt, generateMd5 } from '@/app/workers/crypto/crypto'; import { chacha20Decrypt, decodeWithPassword, decrypt, generateMd5 } from '@/app/workers/crypto/crypto';
import { useDatabase } from '@/app/providers/DatabaseProvider/useDatabase'; import { useDatabase } from '@/app/providers/DatabaseProvider/useDatabase';
import { createContext, useEffect, useRef, useState } from 'react'; import { createContext, useEffect, useRef, useState } from 'react';
import { Attachment, AttachmentEncoding, AttachmentTransport, AttachmentType, PacketMessage } from '@/app/providers/ProtocolProvider/protocol/packets/packet.message'; import { Attachment, AttachmentTransport, AttachmentType, PacketMessage } from '@/app/providers/ProtocolProvider/protocol/packets/packet.message';
import { usePrivatePlain } from '../AccountProvider/usePrivatePlain'; import { usePrivatePlain } from '../AccountProvider/usePrivatePlain';
import { usePublicKey } from '../AccountProvider/usePublicKey'; import { usePublicKey } from '../AccountProvider/usePublicKey';
import { PacketRead } from '@/app/providers/ProtocolProvider/protocol/packets/packet.read'; import { PacketRead } from '@/app/providers/ProtocolProvider/protocol/packets/packet.read';
@@ -47,7 +47,6 @@ export interface AttachmentMeta {
type: AttachmentType; type: AttachmentType;
preview: string; preview: string;
transport: AttachmentTransport; transport: AttachmentTransport;
encoding: AttachmentEncoding;
} }
export interface Message { export interface Message {
@@ -221,7 +220,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если это сообщение от нас, то проверяем, есть ли внутри chacha_key * Если это сообщение от нас, то проверяем, есть ли внутри chacha_key
*/ */
try{ try{
decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key), 'binary').toString('utf-8'); decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key), 'binary').toString('hex');
}catch(e) { }catch(e) {
decryptKey = ""; decryptKey = "";
} }
@@ -232,20 +231,24 @@ export function DialogProvider(props: DialogProviderProps) {
* сообщение пришло нам в результате синхронизации и его нужно расшифровать, если chacha_key нет, * сообщение пришло нам в результате синхронизации и его нужно расшифровать, если chacha_key нет,
* значит сообщение отправлено с нашего устройства, и зашифровано на стороне отправки (plain_message) * значит сообщение отправлено с нашего устройства, и зашифровано на стороне отправки (plain_message)
*/ */
decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key.replace("sync:", "")), 'binary').toString('utf-8'); decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key.replace("sync:", "")), 'binary').toString('hex');
} }
if(hasGroup(props.dialog)){ if(hasGroup(props.dialog)){
/** /**
* Если это групповое сообщение, то получаем ключ группы * Если это групповое сообщение, то получаем ключ группы
*/ */
decryptKey = await getGroupKey(props.dialog); decryptKey = await getGroupKey(props.dialog);
/**
* Приводим к HEX так как этого требует формат расшифровки вложений в приложении
*/
decryptKey = Buffer.from(decryptKey).toString('hex');
} }
if(!message.from_me && !hasGroup(props.dialog)){ if(!message.from_me && !hasGroup(props.dialog)){
/** /**
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,
@@ -482,7 +485,7 @@ export function DialogProvider(props: DialogProviderProps) {
const attachment = packet.getAttachments()[i]; const attachment = packet.getAttachments()[i];
attachments.push({ attachments.push({
...attachment, ...attachment,
blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob) : "" blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob) : ""
}); });
} }
@@ -492,7 +495,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: 0, //сообщение прочитано readed: 0, //сообщение прочитано
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: 1, //сообщение от нас from_me: 1, //сообщение от нас
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -570,7 +573,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: 0, readed: 0,
chacha_key: groupKey, chacha_key: Buffer.from(groupKey).toString('hex'),
from_me: 1, from_me: 1,
plain_message: decryptedContent, plain_message: decryptedContent,
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -636,7 +639,7 @@ export function DialogProvider(props: DialogProviderProps) {
const attachment = packet.getAttachments()[i]; const attachment = packet.getAttachments()[i];
attachments.push({ attachments.push({
...attachment, ...attachment,
blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob) : "" blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob) : ""
}); });
} }
console.info(attachments); console.info(attachments);
@@ -646,7 +649,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -724,7 +727,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: groupKey, chacha_key: Buffer.from(groupKey).toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: decryptedContent, plain_message: decryptedContent,
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -798,7 +801,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,
@@ -883,7 +886,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,
@@ -969,8 +972,7 @@ export function DialogProvider(props: DialogProviderProps) {
blob: blob, blob: blob,
type: meta.type, type: meta.type,
preview: meta.preview, preview: meta.preview,
transport: meta.transport, transport: meta.transport
encoding: meta.encoding
}); });
} }
return attachments; return attachments;

View File

@@ -106,7 +106,8 @@ export function useDeattachedSender() {
attachmentsMeta.push({ attachmentsMeta.push({
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview preview: attachment.preview,
transport: attachment.transport
}); });
if(attachment.type == AttachmentType.FILE){ if(attachment.type == AttachmentType.FILE){
/** /**
@@ -132,7 +133,7 @@ export function useDeattachedSender() {
|| publicKey == dialog) { || publicKey == dialog) {
return; return;
} }
let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('utf-8'), attachemnts); let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('hex'), attachemnts);
if(attachemnts.length <= 0 && message.trim() == ""){ if(attachemnts.length <= 0 && message.trim() == ""){
runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]); runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]);
updateDialog(dialog); updateDialog(dialog);

View File

@@ -96,14 +96,13 @@ export function useDialog() : {
* же сообщений (смотреть problem_sync.md) * же сообщений (смотреть problem_sync.md)
*/ */
const aesChachaKey = await encodeWithPassword(privatePlain, key.toString('binary')); const aesChachaKey = await encodeWithPassword(privatePlain, key.toString('binary'));
setMessages((prev : Message[]) => ([...prev, { setMessages((prev : Message[]) => ([...prev, {
from_public_key: publicKey, from_public_key: publicKey,
to_public_key: dialog, to_public_key: dialog,
content: content, content: content,
timestamp: Date.now(), timestamp: Date.now(),
readed: publicKey == dialog ? 1 : 0, readed: publicKey == dialog ? 1 : 0,
chacha_key: aesChachaKey, chacha_key: key.toString('hex'),
from_me: 1, from_me: 1,
plain_message: message, plain_message: message,
delivered: publicKey == dialog ? DeliveredMessageState.DELIVERED : DeliveredMessageState.WAITING, delivered: publicKey == dialog ? DeliveredMessageState.DELIVERED : DeliveredMessageState.WAITING,
@@ -119,8 +118,7 @@ export function useDialog() : {
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview, preview: attachment.preview,
transport: attachment.transport, transport: attachment.transport
encoding: attachment.encoding
}); });
if(attachment.type == AttachmentType.FILE){ if(attachment.type == AttachmentType.FILE){
/** /**
@@ -147,10 +145,13 @@ export function useDialog() : {
return; return;
} }
//98acbbc68f4b2449daf0a39d1b3eab9a3056da5d45b811bbc903e214c21d39643394980231e1a89c811830d870f3354184319665327ca8bd
console.info("Sending key for message ", key.toString('hex')); console.info("Sending key for message ", key.toString('hex'));
let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('utf-8'), attachemnts); console.info(attachemnts);
if(attachemnts.length <= 0 && message.trim() == ""){ let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('hex'), attachemnts);
if(preparedToNetworkSendAttachements.length < attachemnts.length){
/**
* Если не удалось нормально загрузить все вложения - тогда не отправляем сообщение
*/
runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]); runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]);
updateDialog(dialog); updateDialog(dialog);
return; return;

View File

@@ -130,7 +130,6 @@ export function useDialogFiber() {
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview, preview: attachment.preview,
encoding: attachment.encoding,
transport: attachment.transport transport: attachment.transport
}); });
} }
@@ -141,7 +140,7 @@ export function useDialogFiber() {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: groupKey, chacha_key: Buffer.from(groupKey).toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: decryptedContent, plain_message: decryptedContent,
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -279,7 +278,7 @@ export function useDialogFiber() {
* Этот тип вложения приходит сразу в blob и не нуждается * Этот тип вложения приходит сразу в blob и не нуждается
* в последующем скачивании * в последующем скачивании
*/ */
const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob); const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob);
writeFile(`m/${await generateMd5(attachment.id + publicKey)}`, writeFile(`m/${await generateMd5(attachment.id + publicKey)}`,
Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary')); Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary'));
messageAttachments[nextLength - 1].blob = decryptedBlob; messageAttachments[nextLength - 1].blob = decryptedBlob;
@@ -289,7 +288,6 @@ export function useDialogFiber() {
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview, preview: attachment.preview,
encoding: attachment.encoding,
transport: attachment.transport transport: attachment.transport
}); });
} }
@@ -300,7 +298,7 @@ export function useDialogFiber() {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,

View File

@@ -19,7 +19,7 @@ export interface MessageReply {
message: string; message: string;
attachments: Attachment[]; attachments: Attachment[];
message_id: string; message_id: string;
chacha_key_plain: string;
} }
export function useReplyMessages() { export function useReplyMessages() {
@@ -54,7 +54,6 @@ export function useReplyMessages() {
} }
replyMessages.messages.push(message); replyMessages.messages.push(message);
const sortedByTime = replyMessages.messages.sort((a, b) => a.timestamp - b.timestamp); const sortedByTime = replyMessages.messages.sort((a, b) => a.timestamp - b.timestamp);
setReplyMessages({ setReplyMessages({
publicKey: dialog, publicKey: dialog,
messages: sortedByTime messages: sortedByTime

View File

@@ -181,7 +181,7 @@ export function useSynchronize() {
* Этот тип вложения приходит сразу в blob и не нуждается * Этот тип вложения приходит сразу в blob и не нуждается
* в последующем скачивании * в последующем скачивании
*/ */
const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob); const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob);
writeFile(`m/${await generateMd5(attachment.id + publicKey)}`, writeFile(`m/${await generateMd5(attachment.id + publicKey)}`,
Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary')); Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary'));
messageAttachments[nextLength - 1].blob = decryptedBlob; messageAttachments[nextLength - 1].blob = decryptedBlob;
@@ -191,7 +191,6 @@ export function useSynchronize() {
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview, preview: attachment.preview,
encoding: attachment.encoding,
transport: attachment.transport transport: attachment.transport
}); });
} }
@@ -202,7 +201,7 @@ export function useSynchronize() {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: 1, //сообщение прочитано readed: 1, //сообщение прочитано
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: 1, //сообщение от нас from_me: 1, //сообщение от нас
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -375,7 +374,6 @@ export function useSynchronize() {
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview, preview: attachment.preview,
encoding: attachment.encoding,
transport: attachment.transport transport: attachment.transport
}); });
} }

View File

@@ -0,0 +1,300 @@
import { createContext, useEffect, useRef, useState } from "react";
export interface PlayerContextValue {
playAudio: (
artist: string,
title: string,
audio: string | Blob | File,
messageId?: string | null
) => void;
playing: boolean;
pause: () => void;
resume: () => void;
stop: () => void;
setDuration: (duration: number) => void;
duration: number;
totalDuration: number;
currentMessageId: string | null;
lastMessageId: string | null;
lastError: string | null;
}
export const PlayerContext = createContext<PlayerContextValue | null>(null);
interface PlayerProviderProps {
children: React.ReactNode;
}
export function PlayerProvider(props: PlayerProviderProps) {
const audioRef = useRef<HTMLAudioElement>(null);
const objectUrlRef = useRef<string | null>(null);
const rafTimeUpdateRef = useRef<number | null>(null);
const isLoadingRef = useRef(false);
const isSeekingRef = useRef(false);
const durationRef = useRef(0);
const totalDurationRef = useRef(0);
const isPlayingRef = useRef(false);
const [isPlaying, setIsPlaying] = useState(false);
const [duration, setDurationState] = useState(0);
const [totalDuration, setTotalDuration] = useState(0);
const [lastError, setLastError] = useState<string | null>(null);
const [currentMessageId, setCurrentMessageId] = useState<string | null>(null);
const [lastMessageId, setLastMessageId] = useState<string | null>(null);
const commitPlaying = (next: boolean) => {
if (isPlayingRef.current === next) return;
isPlayingRef.current = next;
setIsPlaying(next);
};
const commitDuration = (next: number) => {
const safe = Number.isFinite(next) && next >= 0 ? next : 0;
if (Math.abs(safe - durationRef.current) < 0.033) return;
durationRef.current = safe;
setDurationState(safe);
};
const commitTotalDuration = (next: number) => {
const safe = Number.isFinite(next) && next > 0 ? next : 0;
if (Math.abs(safe - totalDurationRef.current) < 0.05) return;
totalDurationRef.current = safe;
setTotalDuration(safe);
};
const decodeMediaError = (err: MediaError | null) => {
if (!err) return "Unknown media error";
switch (err.code) {
case MediaError.MEDIA_ERR_ABORTED:
return "Playback aborted";
case MediaError.MEDIA_ERR_NETWORK:
return "Network error while loading audio";
case MediaError.MEDIA_ERR_DECODE:
return "Audio decode error";
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
return "Audio source is not supported";
default:
return `Unknown media error (${err.code})`;
}
};
useEffect(() => {
const audio = audioRef.current;
if (!audio) return;
const onPlay = () => {
if (isLoadingRef.current) return;
commitPlaying(true);
};
const onPause = () => {
if (isLoadingRef.current) return;
commitPlaying(false);
};
const onEnded = () => {
commitPlaying(false);
durationRef.current = 0;
setDurationState(0);
setCurrentMessageId(null);
};
const onTimeUpdate = () => {
if (isLoadingRef.current) return;
if (isSeekingRef.current) return;
if (rafTimeUpdateRef.current != null) return;
rafTimeUpdateRef.current = requestAnimationFrame(() => {
rafTimeUpdateRef.current = null;
if (!isLoadingRef.current && !isSeekingRef.current) {
commitDuration(audio.currentTime || 0);
}
});
};
const onLoadedMetadata = () => commitTotalDuration(audio.duration);
const onDurationChange = () => commitTotalDuration(audio.duration);
const onSeeked = () => {
if (isSeekingRef.current) {
isSeekingRef.current = false;
if (!isLoadingRef.current) commitDuration(audio.currentTime || 0);
return;
}
if (isLoadingRef.current) return;
commitDuration(audio.currentTime || 0);
};
const onCanPlay = () => {
if (isLoadingRef.current) isLoadingRef.current = false;
};
const onError = (_e: Event) => {
const message = decodeMediaError(audio.error);
setLastError(message);
console.error("Audio playback error", {
message,
mediaError: audio.error,
currentSrc: audio.currentSrc,
readyState: audio.readyState,
networkState: audio.networkState,
});
};
audio.addEventListener("play", onPlay);
audio.addEventListener("pause", onPause);
audio.addEventListener("ended", onEnded);
audio.addEventListener("timeupdate", onTimeUpdate);
audio.addEventListener("loadedmetadata", onLoadedMetadata);
audio.addEventListener("durationchange", onDurationChange);
audio.addEventListener("seeked", onSeeked);
audio.addEventListener("canplay", onCanPlay);
audio.addEventListener("error", onError);
return () => {
audio.removeEventListener("play", onPlay);
audio.removeEventListener("pause", onPause);
audio.removeEventListener("ended", onEnded);
audio.removeEventListener("timeupdate", onTimeUpdate);
audio.removeEventListener("loadedmetadata", onLoadedMetadata);
audio.removeEventListener("durationchange", onDurationChange);
audio.removeEventListener("seeked", onSeeked);
audio.removeEventListener("canplay", onCanPlay);
audio.removeEventListener("error", onError);
if (rafTimeUpdateRef.current != null) {
cancelAnimationFrame(rafTimeUpdateRef.current);
rafTimeUpdateRef.current = null;
}
};
}, []);
useEffect(() => {
return () => {
if (objectUrlRef.current) {
URL.revokeObjectURL(objectUrlRef.current);
objectUrlRef.current = null;
}
};
}, []);
const playAudio = (
artist: string,
title: string,
audio: string | Blob | File,
messageId?: string | null
) => {
const el = audioRef.current;
if (!el) return;
// чтобы не было warning о неиспользуемых args при строгих правилах
void artist;
void title;
setLastError(null);
if (objectUrlRef.current) {
URL.revokeObjectURL(objectUrlRef.current);
objectUrlRef.current = null;
}
const audioSrc = typeof audio === "string" ? audio : URL.createObjectURL(audio);
if (typeof audio !== "string") {
objectUrlRef.current = audioSrc;
}
isLoadingRef.current = true;
isSeekingRef.current = false;
el.src = audioSrc;
durationRef.current = 0;
const msgId = messageId ?? null;
setCurrentMessageId(msgId);
if (msgId) setLastMessageId(msgId);
isPlayingRef.current = true;
setIsPlaying(true);
const prevDuration = durationRef.current;
requestAnimationFrame(() => {
if (durationRef.current === prevDuration) {
setDurationState(0);
}
});
void el.play().catch((err) => {
isLoadingRef.current = false;
commitPlaying(false);
setLastError(err instanceof Error ? err.message : "play() failed");
});
};
const pause = () => {
const el = audioRef.current;
if (!el) return;
el.pause();
};
const resume = () => {
const el = audioRef.current;
if (!el) return;
commitPlaying(true);
void el.play().catch((err) => {
commitPlaying(false);
setLastError(err instanceof Error ? err.message : "resume() failed");
});
};
const stop = () => {
const el = audioRef.current;
if (!el) return;
isLoadingRef.current = true;
el.pause();
el.currentTime = 0;
isLoadingRef.current = false;
durationRef.current = 0;
setDurationState(0);
commitPlaying(false);
setCurrentMessageId(null);
};
const setDuration = (sec: number) => {
const el = audioRef.current;
if (!el) return;
isSeekingRef.current = true;
el.currentTime = Math.max(0, sec);
commitDuration(el.currentTime || 0);
};
return (
<PlayerContext.Provider
value={{
playAudio,
playing: isPlaying,
pause,
resume,
stop,
setDuration,
duration,
totalDuration,
currentMessageId,
lastMessageId,
lastError,
}}
>
{props.children}
<audio ref={audioRef} />
</PlayerContext.Provider>
);
}

View File

@@ -0,0 +1,10 @@
import { useContext } from "react";
import { PlayerContext, PlayerContextValue } from "./PlayerProvider";
export function usePlayerContext() : PlayerContextValue {
const context = useContext(PlayerContext);
if (!context) {
throw new Error("useAudioPlayer must be used within a PlayerProvider");
}
return context;
}

View File

@@ -6,7 +6,8 @@ export enum AttachmentType {
MESSAGES = 1, MESSAGES = 1,
FILE = 2, FILE = 2,
AVATAR = 3, AVATAR = 3,
CALL = 4 CALL = 4,
VOICE = 5
} }
/** /**
@@ -17,19 +18,6 @@ export interface AttachmentTransport {
transport_server: string; transport_server: string;
} }
/**
* Информация о кодировке вложения
*/
export interface AttachmentEncoding {
/**
* Для кого вложение закодировано (для какого диалога)
*/
encoded_for: string;
/**
* Кто кодировал вложение
*/
encoder: string;
}
export interface Attachment { export interface Attachment {
id: string; id: string;
@@ -37,7 +25,6 @@ export interface Attachment {
type: AttachmentType; type: AttachmentType;
preview: string; preview: string;
transport: AttachmentTransport; transport: AttachmentTransport;
encoding: AttachmentEncoding;
} }
export class PacketMessage extends Packet { export class PacketMessage extends Packet {
@@ -79,11 +66,7 @@ export class PacketMessage extends Packet {
transport_tag: stream.readString(), transport_tag: stream.readString(),
transport_server: stream.readString() transport_server: stream.readString()
} }
const encoding : AttachmentEncoding = { this.attachments.push({id, preview, type, blob, transport});
encoded_for: stream.readString(),
encoder: stream.readString()
}
this.attachments.push({id, preview, type, blob, transport, encoding});
} }
this.aesChachaKey = stream.readString(); this.aesChachaKey = stream.readString();
} }
@@ -106,8 +89,6 @@ export class PacketMessage extends Packet {
stream.writeInt8(this.attachments[i].type); stream.writeInt8(this.attachments[i].type);
stream.writeString(this.attachments[i].transport.transport_tag); stream.writeString(this.attachments[i].transport.transport_tag);
stream.writeString(this.attachments[i].transport.transport_server); stream.writeString(this.attachments[i].transport.transport_server);
stream.writeString(this.attachments[i].encoding.encoded_for);
stream.writeString(this.attachments[i].encoding.encoder);
} }
stream.writeString(this.aesChachaKey); stream.writeString(this.aesChachaKey);
return stream; return stream;

View File

@@ -6,9 +6,14 @@ export enum SignalType {
KEY_EXCHANGE = 1, KEY_EXCHANGE = 1,
ACTIVE_CALL = 2, ACTIVE_CALL = 2,
END_CALL = 3, END_CALL = 3,
CREATE_ROOM = 4, /**
* Переведен в стадию активного, значит комната на SFU уже создана и можно начинать обмен сигналами WebRTC
*/
ACTIVE = 4,
END_CALL_BECAUSE_PEER_DISCONNECTED = 5, END_CALL_BECAUSE_PEER_DISCONNECTED = 5,
END_CALL_BECAUSE_BUSY = 6 END_CALL_BECAUSE_BUSY = 6,
ACCEPT = 7,
RINGING_TIMEOUT = 8
} }
/** /**
@@ -28,12 +33,8 @@ export class PacketSignalPeer extends Packet {
private signalType: SignalType = SignalType.CALL; private signalType: SignalType = SignalType.CALL;
/** private callId: string = "";
* Используется если SignalType == CREATE_ROOM, private joinToken: string = "";
* для идентификации комнаты на SFU сервере, в которой будет происходить обмен сигналами
* WebRTC для установления P2P соединения между участниками звонка
*/
private roomId: string = "";
public getPacketId(): number { public getPacketId(): number {
@@ -42,7 +43,9 @@ export class PacketSignalPeer extends Packet {
public _receive(stream: Stream): void { public _receive(stream: Stream): void {
this.signalType = stream.readInt8(); this.signalType = stream.readInt8();
if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY || this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){ if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY
|| this.signalType == SignalType.RINGING_TIMEOUT
|| this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){
return; return;
} }
this.src = stream.readString(); this.src = stream.readString();
@@ -50,8 +53,9 @@ export class PacketSignalPeer extends Packet {
if(this.signalType == SignalType.KEY_EXCHANGE){ if(this.signalType == SignalType.KEY_EXCHANGE){
this.sharedPublic = stream.readString(); this.sharedPublic = stream.readString();
} }
if(this.signalType == SignalType.CREATE_ROOM){ if(this.signalType == SignalType.CALL || this.signalType == SignalType.ACCEPT || this.signalType == SignalType.END_CALL){
this.roomId = stream.readString(); this.callId = stream.readString();
this.joinToken = stream.readString();
} }
} }
@@ -59,7 +63,9 @@ export class PacketSignalPeer extends Packet {
const stream = new Stream(); const stream = new Stream();
stream.writeInt16(this.getPacketId()); stream.writeInt16(this.getPacketId());
stream.writeInt8(this.signalType); stream.writeInt8(this.signalType);
if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY || this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){ if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY
|| this.signalType == SignalType.RINGING_TIMEOUT
|| this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){
return stream; return stream;
} }
stream.writeString(this.src); stream.writeString(this.src);
@@ -67,8 +73,9 @@ export class PacketSignalPeer extends Packet {
if(this.signalType == SignalType.KEY_EXCHANGE){ if(this.signalType == SignalType.KEY_EXCHANGE){
stream.writeString(this.sharedPublic); stream.writeString(this.sharedPublic);
} }
if(this.signalType == SignalType.CREATE_ROOM){ if(this.signalType == SignalType.CALL || this.signalType == SignalType.ACCEPT || this.signalType == SignalType.END_CALL){
stream.writeString(this.roomId); stream.writeString(this.callId);
stream.writeString(this.joinToken);
} }
return stream; return stream;
} }
@@ -105,12 +112,20 @@ export class PacketSignalPeer extends Packet {
this.src = src; this.src = src;
} }
public getRoomId(): string { public getCallId(): string {
return this.roomId; return this.callId;
} }
public setRoomId(roomId: string) { public setCallId(callId: string) {
this.roomId = roomId; this.callId = callId;
}
public getJoinToken(): string {
return this.joinToken;
}
public setJoinToken(joinToken: string) {
this.joinToken = joinToken;
} }
} }

View File

@@ -1,8 +1,8 @@
export const SERVERS = [ export const SERVERS = [
//'wss://cdn.rosetta-im.com', //'wss://cdn.rosetta-im.com',
'ws://10.211.55.2:3000', //'ws://10.211.55.2:3000',
//'ws://192.168.6.82:3000', //'ws://192.168.6.82:3000',
//'wss://wss.rosetta.im' 'wss://wss.rosetta.im'
]; ];
export function selectServer(): string { export function selectServer(): string {

View File

@@ -17,6 +17,8 @@ export const constructLastMessageTextByAttachments = (attachment: string) => {
return "$a=Avatar"; return "$a=Avatar";
case AttachmentType.CALL: case AttachmentType.CALL:
return "$a=Call"; return "$a=Call";
case AttachmentType.VOICE:
return "$a=Voice message";
default: default:
return "[Unsupported attachment]"; return "[Unsupported attachment]";
} }

View File

@@ -1,8 +1,7 @@
export const APP_VERSION = "1.1.4"; export const APP_VERSION = "1.2.2";
export const CORE_MIN_REQUIRED_VERSION = "1.5.4"; export const CORE_MIN_REQUIRED_VERSION = "1.5.5";
export const RELEASE_NOTICE = ` export const RELEASE_NOTICE = `
**Обновление v1.1.4** :emoji_1f631: **Обновление v1.2.2** :emoji_1f631:
- Улучшено отображение звонка - Поддержка записи и прослушивания голосовых сообщений
- Убрана возможность зума в окне приложения
`; `;

View File

@@ -1,4 +1,4 @@
import { ColorSwatch, Text, useComputedColorScheme } from "@mantine/core"; import { Button, ColorSwatch, Flex, Text, useComputedColorScheme } from "@mantine/core";
import { SettingsInput } from "@/app/components/SettingsInput/SettingsInput"; import { SettingsInput } from "@/app/components/SettingsInput/SettingsInput";
import { useState } from "react"; import { useState } from "react";
import { Breadcrumbs } from "@/app/components/Breadcrumbs/Breadcrumbs"; import { Breadcrumbs } from "@/app/components/Breadcrumbs/Breadcrumbs";
@@ -17,6 +17,7 @@ import { SettingsIcon } from "@/app/components/SettingsIcon/SettingsIcon";
import { IconBrush, IconHomeCog, IconLogout, IconRefresh } from "@tabler/icons-react"; import { IconBrush, IconHomeCog, IconLogout, IconRefresh } from "@tabler/icons-react";
import { useLogout } from "@/app/providers/AccountProvider/useLogout"; import { useLogout } from "@/app/providers/AccountProvider/useLogout";
import { RosettaPower } from "@/app/components/RosettaPower/RosettaPower"; import { RosettaPower } from "@/app/components/RosettaPower/RosettaPower";
import { modals } from "@mantine/modals";
export function MyProfile() { export function MyProfile() {
const publicKey = usePublicKey(); const publicKey = usePublicKey();
@@ -28,8 +29,34 @@ export function MyProfile() {
const navigate = useNavigate(); const navigate = useNavigate();
const send = useSender(); const send = useSender();
const logout = useLogout(); const logout = useLogout();
const [usernameError, setUsernameError] = useState(false);
const openProfileModal = (text : string) => {
modals.open({
centered: true,
children: (
<>
<Text size="sm">
{text}
</Text>
<Flex align={'center'} justify={'flex-end'}>
<Button style={{
outline: 'none'
}} color={'red'} variant={'subtle'} onClick={() => modals.closeAll()} mt="md">
Close
</Button>
</Flex>
</>
),
withCloseButton: false
});
}
const saveProfileData = () => { const saveProfileData = () => {
if(usernameError) {
openProfileModal("You enter invalid username. Username must be a latin chars in lowercase.");
return;
}
let packet = new PacketUserInfo(); let packet = new PacketUserInfo();
packet.setUsername(username); packet.setUsername(username);
packet.setTitle(title); packet.setTitle(title);
@@ -70,10 +97,13 @@ export function MyProfile() {
<SettingsInput.Default <SettingsInput.Default
hit="Username" hit="Username"
value={username} value={username}
onErrorStateChange={(error) => setUsernameError(error)}
placeholder="ex. freddie871" placeholder="ex. freddie871"
onChange={(e) => setUsername(e.target.value)} onChange={(e) => setUsername(e.target.value)}
regexp={new RegExp(/^([a-z][a-z0-9_]{4,15})?$/)}
></SettingsInput.Default> ></SettingsInput.Default>
</SettingsInput.Group> </SettingsInput.Group>
{usernameError && <Text c={'red'} fz={10} pl={'xs'} mt={3}>Invalid username.</Text>}
<SettingsInput.Copy mt={'sm'} hit="Public Key" value={ <SettingsInput.Copy mt={'sm'} hit="Public Key" value={
publicKey publicKey
} placeholder="Public"></SettingsInput.Copy> } placeholder="Public"></SettingsInput.Copy>

View File

@@ -28,8 +28,8 @@ export function createPreloaderWindow() {
export function createAppWindow(preloaderWindow?: BrowserWindow): void { export function createAppWindow(preloaderWindow?: BrowserWindow): void {
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 900, width: 385,
height: 670, height: 555,
minWidth: 385, minWidth: 385,
minHeight: 555, minHeight: 555,
show: false, show: false,

View File

@@ -1,6 +1,6 @@
{ {
"name": "Rosetta", "name": "Rosetta",
"version": "1.5.4", "version": "1.5.5",
"description": "Rosetta Messenger", "description": "Rosetta Messenger",
"main": "./out/main/main.js", "main": "./out/main/main.js",
"license": "MIT", "license": "MIT",