Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ab2ac4813a | |||
|
0c0a341f1e |
|||
|
f74ca52f4f |
|||
|
|
94f11f60f6 |
||
|
|
f5461bbd42 |
||
|
7c9d9529b4 |
|||
|
8a04b327fc |
|||
|
0a304e44f2 |
|||
|
094cb910aa |
|||
|
cf17365c8f |
|||
|
aa34bd962b |
|||
|
1d1b304bcf |
|||
|
|
0fbedf6733 | ||
|
96b81d30b1 |
|||
|
edcf0e15dd |
|||
|
5056f084b4 |
|||
|
ede0199858 |
|||
|
88a094c8bf |
|||
|
e43e03ff12 |
|||
|
97270abed8 |
|||
|
2a55c87df1 |
|||
|
|
57c7ceb27a | ||
|
|
d0918ff384 | ||
| 0ac20f09e4 | |||
| 73db7e6b2b | |||
|
|
ac1bdf5ac2 |
58 changed files with 4393 additions and 2095 deletions
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
[env]
|
||||||
|
MTP_TYPE_MAPS = { value = "mtp-type-maps/type-maps.yaml", relative = true }
|
||||||
|
|
@ -4,7 +4,6 @@ on:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
NIX_CONFIG: experimental-features = nix-command flakes
|
|
||||||
FORGEJO_TOKEN: ""
|
FORGEJO_TOKEN: ""
|
||||||
GITHUB_TOKEN: ""
|
GITHUB_TOKEN: ""
|
||||||
|
|
||||||
|
|
@ -14,7 +13,6 @@ jobs:
|
||||||
name: Build web
|
name: Build web
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- run: nix profile add nixpkgs#nodejs_24
|
|
||||||
- uses: https://data.forgejo.org/actions/checkout@v4
|
- uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
@ -28,7 +26,6 @@ jobs:
|
||||||
name: Build desktop
|
name: Build desktop
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- run: nix profile add nixpkgs#nodejs_24
|
|
||||||
- uses: https://data.forgejo.org/actions/checkout@v4
|
- uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
@ -37,12 +34,23 @@ jobs:
|
||||||
- run: nix develop .#electron --command pnpm run build:packages
|
- run: nix develop .#electron --command pnpm run build:packages
|
||||||
- run: nix develop .#electron --command pnpm run build:desktop
|
- run: nix develop .#electron --command pnpm run build:desktop
|
||||||
|
|
||||||
mobile:
|
native-mtp:
|
||||||
if: ${{ github.actor == 'rasensprenger' }}
|
if: ${{ github.actor == 'rasensprenger' }}
|
||||||
name: Build mobile
|
name: Test native MTP
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- run: nix profile add nixpkgs#nodejs_24
|
- run: nix profile add nixpkgs#nodejs_24
|
||||||
|
- uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
- run: git submodule update --init --recursive
|
||||||
|
- run: nix develop .#electron --command bash -lc 'cd apps/tauri/src-tauri && cargo test'
|
||||||
|
|
||||||
|
mobile:
|
||||||
|
if: ${{ github.actor == 'rasensprenger' }}
|
||||||
|
name: Build mobile
|
||||||
|
runs-on: nixos
|
||||||
|
steps:
|
||||||
- uses: https://data.forgejo.org/actions/checkout@v4
|
- uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,15 @@
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- dev
|
- dev
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- flake.nix
|
- flake.nix
|
||||||
|
|
||||||
env:
|
|
||||||
NIX_CONFIG: experimental-features = nix-command flakes
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-web:
|
build-web:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -39,9 +34,6 @@ jobs:
|
||||||
build-mobile:
|
build-mobile:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -62,8 +54,6 @@ jobs:
|
||||||
KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
|
KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
|
||||||
KEYSTORE_PROPERTIES: ${{ secrets.ANDROID_KEYSTORE_PROPERTIES }}
|
KEYSTORE_PROPERTIES: ${{ secrets.ANDROID_KEYSTORE_PROPERTIES }}
|
||||||
run: |
|
run: |
|
||||||
nix profile add nixpkgs#gnused
|
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
if [ -z "$KEYSTORE_BASE64" ]; then
|
if [ -z "$KEYSTORE_BASE64" ]; then
|
||||||
|
|
@ -128,9 +118,6 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
target: [linux]
|
target: [linux]
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -181,9 +168,6 @@ jobs:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
needs: [build-web, build-mobile, build-desktop]
|
needs: [build-web, build-mobile, build-desktop]
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,10 @@ on:
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- flake.nix
|
- flake.nix
|
||||||
|
|
||||||
env:
|
|
||||||
NIX_CONFIG: experimental-features = nix-command flakes
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-web:
|
build-web:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -40,9 +34,6 @@ jobs:
|
||||||
build-mobile:
|
build-mobile:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -63,8 +54,6 @@ jobs:
|
||||||
KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
|
KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
|
||||||
KEYSTORE_PROPERTIES: ${{ secrets.ANDROID_KEYSTORE_PROPERTIES }}
|
KEYSTORE_PROPERTIES: ${{ secrets.ANDROID_KEYSTORE_PROPERTIES }}
|
||||||
run: |
|
run: |
|
||||||
nix profile add nixpkgs#gnused
|
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
if [ -z "$KEYSTORE_BASE64" ]; then
|
if [ -z "$KEYSTORE_BASE64" ]; then
|
||||||
|
|
@ -129,9 +118,6 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
target: [linux]
|
target: [linux]
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -180,9 +166,6 @@ jobs:
|
||||||
runs-on: nixos
|
runs-on: nixos
|
||||||
needs: [build-web, build-mobile, build-desktop]
|
needs: [build-web, build-mobile, build-desktop]
|
||||||
steps:
|
steps:
|
||||||
- name: Install node
|
|
||||||
run: nix profile add nixpkgs#nodejs_24
|
|
||||||
|
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: https://data.forgejo.org/actions/checkout@v4
|
uses: https://data.forgejo.org/actions/checkout@v4
|
||||||
|
|
||||||
|
|
@ -328,58 +311,3 @@ jobs:
|
||||||
"$API/repos/$REPO/releases/$release_id"
|
"$API/repos/$REPO/releases/$release_id"
|
||||||
done < "$DELETE_RELEASES"
|
done < "$DELETE_RELEASES"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
- name: Update root flake release hash
|
|
||||||
env:
|
|
||||||
TAG: ${{ steps.version.outputs.tag }}
|
|
||||||
run: |
|
|
||||||
nix develop .#electron --command bash <<'EOF'
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
DEB="$(find releases -maxdepth 1 -type f -name 'Tensamin-*-linux-amd64.deb' -print -quit)"
|
|
||||||
test -n "$DEB"
|
|
||||||
|
|
||||||
HASH="$(node -e 'const fs = require("fs"); const crypto = require("crypto"); const file = process.argv[1]; console.log("sha256-" + crypto.createHash("sha256").update(fs.readFileSync(file)).digest("base64"));' "$DEB")"
|
|
||||||
export HASH
|
|
||||||
|
|
||||||
node -e '
|
|
||||||
const fs = require("fs");
|
|
||||||
const version = process.env.TAG;
|
|
||||||
const hash = process.env.HASH;
|
|
||||||
let content = fs.readFileSync("flake.nix", "utf8");
|
|
||||||
content = content.replace(/version = "[^"]+";/, `version = "${version}";`);
|
|
||||||
content = content.replace(/x86_64DebHash = "sha256-[^"]+";/, `x86_64DebHash = "${hash}";`);
|
|
||||||
fs.writeFileSync("flake.nix", content);
|
|
||||||
'
|
|
||||||
|
|
||||||
if git diff --quiet -- flake.nix; then
|
|
||||||
echo "flake.nix already has the current release hash on main."
|
|
||||||
else
|
|
||||||
git add flake.nix
|
|
||||||
git -c user.name="forgejo-actions" -c user.email="forgejo-actions@localhost" commit -m "(qol): update release flake hash"
|
|
||||||
git push
|
|
||||||
fi
|
|
||||||
|
|
||||||
git fetch origin dev
|
|
||||||
git worktree add ../dev-flake-update origin/dev
|
|
||||||
cd ../dev-flake-update
|
|
||||||
|
|
||||||
node -e '
|
|
||||||
const fs = require("fs");
|
|
||||||
const version = process.env.TAG;
|
|
||||||
const hash = process.env.HASH;
|
|
||||||
let content = fs.readFileSync("flake.nix", "utf8");
|
|
||||||
content = content.replace(/version = "[^"]+";/, `version = "${version}";`);
|
|
||||||
content = content.replace(/x86_64DebHash = "sha256-[^"]+";/, `x86_64DebHash = "${hash}";`);
|
|
||||||
fs.writeFileSync("flake.nix", content);
|
|
||||||
'
|
|
||||||
|
|
||||||
if git diff --quiet -- flake.nix; then
|
|
||||||
echo "flake.nix already has the current release hash on dev."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
git add flake.nix
|
|
||||||
git -c user.name="forgejo-actions" -c user.email="forgejo-actions@localhost" commit -m "(qol): update release flake hash"
|
|
||||||
git push origin HEAD:dev
|
|
||||||
EOF
|
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 22 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 5.6 KiB |
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 37 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 41 KiB |
|
|
@ -22,9 +22,10 @@ export function subscribeTuFileLaunch(listener: (file: File) => void) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function isStandalone() {
|
function isInstalledPwa() {
|
||||||
return (
|
return (
|
||||||
window.matchMedia("(display-mode: standalone)").matches ||
|
window.matchMedia("(display-mode: standalone)").matches ||
|
||||||
|
window.matchMedia("(display-mode: window-controls-overlay)").matches ||
|
||||||
(navigator as Navigator & { standalone?: boolean }).standalone === true
|
(navigator as Navigator & { standalone?: boolean }).standalone === true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -60,10 +61,9 @@ async function enablePush() {
|
||||||
await setDatabaseEntry("keys", "push-subscription", subscription.toJSON());
|
await setDatabaseEntry("keys", "push-subscription", subscription.toJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PwaRuntime() {
|
function InstalledPwaRuntime() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
isTauri() ||
|
|
||||||
!("serviceWorker" in navigator) ||
|
!("serviceWorker" in navigator) ||
|
||||||
!["http:", "https:"].includes(window.location.protocol)
|
!["http:", "https:"].includes(window.location.protocol)
|
||||||
) {
|
) {
|
||||||
|
|
@ -142,7 +142,6 @@ export default function PwaRuntime() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
!isStandalone() ||
|
|
||||||
!("Notification" in window) ||
|
!("Notification" in window) ||
|
||||||
Notification.permission !== "default" ||
|
Notification.permission !== "default" ||
|
||||||
localStorage.getItem("pwa-push-hint")
|
localStorage.getItem("pwa-push-hint")
|
||||||
|
|
@ -171,7 +170,6 @@ export default function PwaRuntime() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
isStandalone() &&
|
|
||||||
"Notification" in window &&
|
"Notification" in window &&
|
||||||
Notification.permission === "granted" &&
|
Notification.permission === "granted" &&
|
||||||
import.meta.env.VITE_WEB_PUSH_PUBLIC_KEY
|
import.meta.env.VITE_WEB_PUSH_PUBLIC_KEY
|
||||||
|
|
@ -184,3 +182,8 @@ export default function PwaRuntime() {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default function PwaRuntime() {
|
||||||
|
if (isTauri() || !isInstalledPwa()) return null;
|
||||||
|
return <InstalledPwaRuntime />;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,7 @@ export function tensaminPwa(): Plugin[] {
|
||||||
filename: "serviceWorker.ts",
|
filename: "serviceWorker.ts",
|
||||||
injectRegister: null,
|
injectRegister: null,
|
||||||
registerType: "prompt",
|
registerType: "prompt",
|
||||||
|
buildBase: "/",
|
||||||
manifestFilename: "manifest.json",
|
manifestFilename: "manifest.json",
|
||||||
includeAssets: ["favicon.ico", "icons/*.png"],
|
includeAssets: ["favicon.ico", "icons/*.png"],
|
||||||
manifest: {
|
manifest: {
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
[env]
|
|
||||||
MTP_TYPE_MAPS = { value = "../../mtp-type-maps/type-maps.yaml", relative = true }
|
|
||||||
296
apps/tauri/src-tauri/Cargo.lock
generated
296
apps/tauri/src-tauri/Cargo.lock
generated
|
|
@ -674,12 +674,6 @@ dependencies = [
|
||||||
"crossbeam-utils",
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "const-oid"
|
|
||||||
version = "0.9.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "const-oid"
|
name = "const-oid"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
|
|
@ -889,7 +883,6 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"cpufeatures 0.2.17",
|
"cpufeatures 0.2.17",
|
||||||
"curve25519-dalek-derive",
|
"curve25519-dalek-derive",
|
||||||
"digest 0.10.7",
|
|
||||||
"fiat-crypto 0.2.9",
|
"fiat-crypto 0.2.9",
|
||||||
"rustc_version",
|
"rustc_version",
|
||||||
"subtle",
|
"subtle",
|
||||||
|
|
@ -974,25 +967,14 @@ dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "der"
|
|
||||||
version = "0.7.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb"
|
|
||||||
dependencies = [
|
|
||||||
"const-oid 0.9.6",
|
|
||||||
"pem-rfc7468 0.7.0",
|
|
||||||
"zeroize",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a69dedd701da44b0536442edf09c81a64b0ab97a7a4a5e3d1971f00027cbc63d"
|
checksum = "a69dedd701da44b0536442edf09c81a64b0ab97a7a4a5e3d1971f00027cbc63d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"const-oid 0.10.2",
|
"const-oid",
|
||||||
"pem-rfc7468 1.0.0",
|
"pem-rfc7468",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1057,7 +1039,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
|
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"block-buffer 0.12.1",
|
"block-buffer 0.12.1",
|
||||||
"const-oid 0.10.2",
|
"const-oid",
|
||||||
"crypto-common 0.2.2",
|
"crypto-common 0.2.2",
|
||||||
"ctutils",
|
"ctutils",
|
||||||
]
|
]
|
||||||
|
|
@ -1204,38 +1186,14 @@ version = "1.0.20"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555"
|
checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ed25519"
|
|
||||||
version = "2.2.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53"
|
|
||||||
dependencies = [
|
|
||||||
"pkcs8 0.10.2",
|
|
||||||
"signature 2.2.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ed25519"
|
name = "ed25519"
|
||||||
version = "3.0.0"
|
version = "3.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "29fcf32e6c73d1079f83ab4d782de2d81620346a5f38c6237a86a22f8368980a"
|
checksum = "29fcf32e6c73d1079f83ab4d782de2d81620346a5f38c6237a86a22f8368980a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pkcs8 0.11.0",
|
"pkcs8",
|
||||||
"signature 3.0.0",
|
"signature",
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ed25519-dalek"
|
|
||||||
version = "2.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9"
|
|
||||||
dependencies = [
|
|
||||||
"curve25519-dalek 4.1.3",
|
|
||||||
"ed25519 2.2.3",
|
|
||||||
"serde",
|
|
||||||
"sha2 0.10.9",
|
|
||||||
"subtle",
|
|
||||||
"zeroize",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1245,10 +1203,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de"
|
checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"curve25519-dalek 5.0.0",
|
"curve25519-dalek 5.0.0",
|
||||||
"ed25519 3.0.0",
|
"ed25519",
|
||||||
"serde",
|
"serde",
|
||||||
"sha2 0.11.0",
|
"sha2 0.11.0",
|
||||||
"signature 3.0.0",
|
"signature",
|
||||||
"subtle",
|
"subtle",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
@ -2737,14 +2695,14 @@ version = "0.1.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "add6b9d92e496f16f4526d68ff29da1483aba4b119baeab8bed3b9e3544a6f3d"
|
checksum = "add6b9d92e496f16f4526d68ff29da1483aba4b119baeab8bed3b9e3544a6f3d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"const-oid 0.10.2",
|
"const-oid",
|
||||||
"crypto-common 0.2.2",
|
"crypto-common 0.2.2",
|
||||||
"ctutils",
|
"ctutils",
|
||||||
"hybrid-array",
|
"hybrid-array",
|
||||||
"module-lattice",
|
"module-lattice",
|
||||||
"pkcs8 0.11.0",
|
"pkcs8",
|
||||||
"shake",
|
"shake",
|
||||||
"signature 3.0.0",
|
"signature",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -2786,72 +2744,50 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp"
|
name = "mtp"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mtp-client",
|
"mtp-client",
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-codec",
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-common",
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-crypto",
|
||||||
"mtp-host",
|
"mtp-host",
|
||||||
"mtp-transport 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-transport",
|
||||||
"mtp-type-map 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-type-map",
|
||||||
"mtp-webserver",
|
"mtp-webserver",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-client"
|
name = "mtp-client"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-codec",
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-common",
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-crypto",
|
||||||
"mtp-transport 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-transport",
|
||||||
"rand 0.10.2",
|
"rand 0.10.2",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-codec"
|
name = "mtp-codec"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.23.1",
|
"base64 0.23.1",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-common",
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-crypto",
|
||||||
"mtp-type-map 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-type-map",
|
||||||
"rand 0.10.2",
|
"rand 0.10.2",
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-codec"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b#b067614a684eb1856bc5db7b3fd82148c036ce6b"
|
|
||||||
dependencies = [
|
|
||||||
"base64 0.23.1",
|
|
||||||
"byteorder",
|
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"mtp-type-map 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"rand 0.10.2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-common"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
|
||||||
dependencies = [
|
|
||||||
"quinn",
|
|
||||||
"rustls",
|
|
||||||
"thiserror 2.0.19",
|
"thiserror 2.0.19",
|
||||||
"wtransport",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-common"
|
name = "mtp-common"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b#b067614a684eb1856bc5db7b3fd82148c036ce6b"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quinn",
|
"quinn",
|
||||||
"rustls",
|
"rustls",
|
||||||
|
|
@ -2861,18 +2797,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-crypto"
|
name = "mtp-crypto"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.23.1",
|
"base64 0.22.1",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"ed25519-dalek 2.2.0",
|
"ed25519-dalek",
|
||||||
"getrandom 0.4.3",
|
"getrandom 0.4.3",
|
||||||
"hkdf",
|
"hkdf",
|
||||||
"ml-dsa",
|
"ml-dsa",
|
||||||
"mlkem-tls",
|
"mlkem-tls",
|
||||||
"rand 0.10.2",
|
"rand 0.10.2",
|
||||||
"rand_core 0.10.1",
|
"rand_core 0.6.4",
|
||||||
"rustls",
|
"rustls",
|
||||||
"serde",
|
"serde",
|
||||||
"sha2 0.11.0",
|
"sha2 0.11.0",
|
||||||
|
|
@ -2882,89 +2818,45 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-crypto"
|
name = "mtp-host"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b#b067614a684eb1856bc5db7b3fd82148c036ce6b"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.23.1",
|
"mtp-codec",
|
||||||
"chacha20poly1305",
|
"mtp-common",
|
||||||
"ed25519-dalek 3.0.0",
|
"mtp-crypto",
|
||||||
"getrandom 0.4.3",
|
"mtp-transport",
|
||||||
"hkdf",
|
|
||||||
"ml-dsa",
|
|
||||||
"rand 0.10.2",
|
"rand 0.10.2",
|
||||||
"rand_core 0.10.1",
|
|
||||||
"rustls",
|
|
||||||
"sha2 0.11.0",
|
|
||||||
"thiserror 2.0.19",
|
"thiserror 2.0.19",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
"wtransport",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mtp-transport"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"mtp-codec",
|
||||||
|
"mtp-common",
|
||||||
|
"mtp-crypto",
|
||||||
|
"rand 0.10.2",
|
||||||
|
"rcgen",
|
||||||
|
"rustls",
|
||||||
|
"rustls-native-certs",
|
||||||
|
"sha2 0.11.0",
|
||||||
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
"wtransport",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-host"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
|
||||||
dependencies = [
|
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"mtp-transport 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"rand 0.10.2",
|
|
||||||
"tokio",
|
|
||||||
"tracing",
|
|
||||||
"wtransport",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-transport"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
|
||||||
dependencies = [
|
|
||||||
"async-trait",
|
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
|
||||||
"rcgen",
|
|
||||||
"rustls",
|
|
||||||
"rustls-native-certs",
|
|
||||||
"sha2 0.11.0",
|
|
||||||
"tokio",
|
|
||||||
"tracing",
|
|
||||||
"wtransport",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-transport"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b#b067614a684eb1856bc5db7b3fd82148c036ce6b"
|
|
||||||
dependencies = [
|
|
||||||
"async-trait",
|
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"rcgen",
|
|
||||||
"rustls",
|
|
||||||
"rustls-native-certs",
|
|
||||||
"sha2 0.11.0",
|
|
||||||
"tokio",
|
|
||||||
"tracing",
|
|
||||||
"wtransport",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-type-map"
|
name = "mtp-type-map"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"serde_yaml",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mtp-type-map"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b#b067614a684eb1856bc5db7b3fd82148c036ce6b"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
|
|
@ -2972,8 +2864,8 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mtp-webserver"
|
name = "mtp-webserver"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22#7182272e3edb0079d7b74b45b2f62a298d2fee22"
|
source = "git+https://git.methanium.net/Methanium/mtp.git?rev=a5c8d4f0c898c78351e9d54124886c86e789a22a#a5c8d4f0c898c78351e9d54124886c86e789a22a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|
@ -2984,13 +2876,12 @@ dependencies = [
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-util",
|
"hyper-util",
|
||||||
"mtp-codec 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-codec",
|
||||||
"mtp-common 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-common",
|
||||||
"mtp-crypto 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-crypto",
|
||||||
"mtp-host",
|
"mtp-host",
|
||||||
"mtp-transport 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=7182272e3edb0079d7b74b45b2f62a298d2fee22)",
|
"mtp-transport",
|
||||||
"quinn",
|
"quinn",
|
||||||
"rand 0.10.2",
|
|
||||||
"rustls",
|
"rustls",
|
||||||
"thiserror 2.0.19",
|
"thiserror 2.0.19",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
@ -3492,15 +3383,6 @@ dependencies = [
|
||||||
"serde_core",
|
"serde_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pem-rfc7468"
|
|
||||||
version = "0.7.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412"
|
|
||||||
dependencies = [
|
|
||||||
"base64ct",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pem-rfc7468"
|
name = "pem-rfc7468"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
|
@ -3586,24 +3468,14 @@ dependencies = [
|
||||||
"futures-io",
|
"futures-io",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pkcs8"
|
|
||||||
version = "0.10.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
|
|
||||||
dependencies = [
|
|
||||||
"der 0.7.10",
|
|
||||||
"spki 0.7.3",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pkcs8"
|
name = "pkcs8"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7"
|
checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"der 0.8.1",
|
"der",
|
||||||
"spki 0.8.0",
|
"spki",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -4592,15 +4464,6 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "signature"
|
|
||||||
version = "2.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
|
|
||||||
dependencies = [
|
|
||||||
"rand_core 0.6.4",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signature"
|
name = "signature"
|
||||||
version = "3.0.0"
|
version = "3.0.0"
|
||||||
|
|
@ -4709,16 +4572,6 @@ dependencies = [
|
||||||
"system-deps",
|
"system-deps",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "spki"
|
|
||||||
version = "0.7.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d"
|
|
||||||
dependencies = [
|
|
||||||
"base64ct",
|
|
||||||
"der 0.7.10",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spki"
|
name = "spki"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|
@ -4726,7 +4579,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1d9efca8738c78ee9484207732f728b1ef517bbb1833d6fc0879ca898a522f6f"
|
checksum = "1d9efca8738c78ee9484207732f728b1ef517bbb1833d6fc0879ca898a522f6f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64ct",
|
"base64ct",
|
||||||
"der 0.8.1",
|
"der",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -5289,7 +5142,6 @@ dependencies = [
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"jni 0.22.4",
|
"jni 0.22.4",
|
||||||
"mtp",
|
"mtp",
|
||||||
"mtp-transport 0.2.0 (git+https://git.methanium.net/methanium/mtp.git?rev=b067614a684eb1856bc5db7b3fd82148c036ce6b)",
|
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,7 @@ serde_json = "1"
|
||||||
base64 = "0.22"
|
base64 = "0.22"
|
||||||
reqwest = { version = "0.13", default-features = false, features = ["json", "rustls"] }
|
reqwest = { version = "0.13", default-features = false, features = ["json", "rustls"] }
|
||||||
tokio = { version = "1", features = ["rt-multi-thread", "sync", "time"] }
|
tokio = { version = "1", features = ["rt-multi-thread", "sync", "time"] }
|
||||||
mtp = { git = "https://git.methanium.net/methanium/mtp.git", rev = "7182272e3edb0079d7b74b45b2f62a298d2fee22", features = ["client", "crypto"] }
|
mtp = { git = "https://git.methanium.net/Methanium/mtp.git", rev = "a5c8d4f0c898c78351e9d54124886c86e789a22a", features = ["client", "crypto"] }
|
||||||
mtp-transport = { git = "https://git.methanium.net/methanium/mtp.git", rev = "b067614a684eb1856bc5db7b3fd82148c036ce6b" }
|
|
||||||
webpki-root-certs = "1"
|
webpki-root-certs = "1"
|
||||||
tauri-plugin-deep-link = "2"
|
tauri-plugin-deep-link = "2"
|
||||||
tauri-plugin-notification = "2"
|
tauri-plugin-notification = "2"
|
||||||
|
|
@ -36,6 +35,11 @@ version = "2"
|
||||||
features = []
|
features = []
|
||||||
default-features = true
|
default-features = true
|
||||||
|
|
||||||
|
[target.'cfg(not(target_os = "android"))'.dependencies.tauri]
|
||||||
|
version = "2"
|
||||||
|
features = []
|
||||||
|
default-features = true
|
||||||
|
|
||||||
[target.'cfg(target_os = "android")'.dependencies]
|
[target.'cfg(target_os = "android")'.dependencies]
|
||||||
jni = "0.22"
|
jni = "0.22"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
use lib::log;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mobile_lib::run();
|
mobile_lib::run();
|
||||||
log("Test test 123")
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::sync::{
|
||||||
atomic::{AtomicBool, AtomicU64, Ordering},
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
||||||
Arc, Mutex, OnceLock, RwLock,
|
Arc, Mutex, OnceLock, RwLock,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use base64::{
|
use base64::{
|
||||||
engine::general_purpose::{STANDARD, STANDARD_NO_PAD},
|
engine::general_purpose::{STANDARD, STANDARD_NO_PAD},
|
||||||
|
|
@ -16,6 +16,7 @@ use mtp::crypto::{
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
use tauri::{AppHandle, Emitter};
|
use tauri::{AppHandle, Emitter};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
const EVENT_NAME: &str = "mtp://event";
|
const EVENT_NAME: &str = "mtp://event";
|
||||||
const DISCONNECTED: u8 = 0;
|
const DISCONNECTED: u8 = 0;
|
||||||
|
|
@ -24,6 +25,9 @@ const CONNECTED: u8 = 2;
|
||||||
const CHAT_SECRET_SALT: &[u8] = b"tensamin-chat-secret-v1";
|
const CHAT_SECRET_SALT: &[u8] = b"tensamin-chat-secret-v1";
|
||||||
const CHAT_MESSAGE_SALT: &[u8] = b"tensamin-chat-message-v1";
|
const CHAT_MESSAGE_SALT: &[u8] = b"tensamin-chat-message-v1";
|
||||||
const CHAT_SECRET_SCHEME: &str = "mtp-chat-secret-kem-chacha20poly1305-hkdf-sha256-v1";
|
const CHAT_SECRET_SCHEME: &str = "mtp-chat-secret-kem-chacha20poly1305-hkdf-sha256-v1";
|
||||||
|
const INITIAL_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
|
||||||
|
const MAX_BUFFERED_INITIAL_FRAMES: usize = 1_000;
|
||||||
|
const NOTIFICATION_QUEUE_CAPACITY: usize = 32;
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
const ROOT_YE_PEM: &[u8] = b"-----BEGIN CERTIFICATE-----\n\
|
const ROOT_YE_PEM: &[u8] = b"-----BEGIN CERTIFICATE-----\n\
|
||||||
MIIB2TCCAWCgAwIBAgIRAKQCa6LvbHwg1AR+XmWmk4AwCgYIKoZIzj0EAwMwLjEL\n\
|
MIIB2TCCAWCgAwIBAgIRAKQCa6LvbHwg1AR+XmWmk4AwCgYIKoZIzj0EAwMwLjEL\n\
|
||||||
|
|
@ -95,7 +99,7 @@ enum MtpEvent {
|
||||||
pub struct MtpManager {
|
pub struct MtpManager {
|
||||||
runtime: tokio::runtime::Runtime,
|
runtime: tokio::runtime::Runtime,
|
||||||
config: RwLock<Option<MtpConfig>>,
|
config: RwLock<Option<MtpConfig>>,
|
||||||
connection: RwLock<Option<Arc<MTPConnection>>>,
|
connection: RwLock<Option<Arc<ManagedConnection>>>,
|
||||||
snapshot: RwLock<MtpSnapshot>,
|
snapshot: RwLock<MtpSnapshot>,
|
||||||
generation: AtomicU64,
|
generation: AtomicU64,
|
||||||
enabled: AtomicBool,
|
enabled: AtomicBool,
|
||||||
|
|
@ -104,6 +108,46 @@ pub struct MtpManager {
|
||||||
start_lock: Mutex<()>,
|
start_lock: Mutex<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RequestIdAllocator {
|
||||||
|
next: AtomicU64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RequestIdAllocator {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
next: AtomicU64::new(1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next(&self) -> Result<u32, String> {
|
||||||
|
let value = self.next.fetch_add(1, Ordering::Relaxed);
|
||||||
|
u32::try_from(value)
|
||||||
|
.map_err(|_| "MTP request ID space exhausted for this connection".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ManagedConnection {
|
||||||
|
mtp: Arc<MTPConnection>,
|
||||||
|
request_ids: RequestIdAllocator,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ManagedConnection {
|
||||||
|
async fn next_request_id(&self) -> Result<u32, String> {
|
||||||
|
let id = self.request_ids.next();
|
||||||
|
if id.is_err() {
|
||||||
|
self.mtp.sender.close().await;
|
||||||
|
}
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PreparedConnection {
|
||||||
|
connection: MTPConnection,
|
||||||
|
request_ids: RequestIdAllocator,
|
||||||
|
initial_state: Value,
|
||||||
|
buffered_frames: Vec<CommunicationValue>,
|
||||||
|
}
|
||||||
|
|
||||||
static MANAGER: OnceLock<MtpManager> = OnceLock::new();
|
static MANAGER: OnceLock<MtpManager> = OnceLock::new();
|
||||||
|
|
||||||
pub fn manager() -> &'static MtpManager {
|
pub fn manager() -> &'static MtpManager {
|
||||||
|
|
@ -149,7 +193,7 @@ impl MtpManager {
|
||||||
.take()
|
.take()
|
||||||
{
|
{
|
||||||
self.runtime
|
self.runtime
|
||||||
.spawn(async move { connection.sender.close().await });
|
.spawn(async move { connection.mtp.sender.close().await });
|
||||||
}
|
}
|
||||||
self.set_snapshot(MtpSnapshot {
|
self.set_snapshot(MtpSnapshot {
|
||||||
generation,
|
generation,
|
||||||
|
|
@ -171,7 +215,7 @@ impl MtpManager {
|
||||||
.take()
|
.take()
|
||||||
{
|
{
|
||||||
self.runtime
|
self.runtime
|
||||||
.spawn(async move { connection.sender.close().await });
|
.spawn(async move { connection.mtp.sender.close().await });
|
||||||
}
|
}
|
||||||
self.set_snapshot(MtpSnapshot {
|
self.set_snapshot(MtpSnapshot {
|
||||||
generation: self.generation.load(Ordering::SeqCst),
|
generation: self.generation.load(Ordering::SeqCst),
|
||||||
|
|
@ -231,20 +275,16 @@ impl MtpManager {
|
||||||
self.enabled.load(Ordering::SeqCst) && self.generation.load(Ordering::SeqCst) == generation
|
self.enabled.load(Ordering::SeqCst) && self.generation.load(Ordering::SeqCst) == generation
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request(
|
async fn request(&self, type_name: &str, data: Value) -> Result<Value, String> {
|
||||||
&self,
|
|
||||||
type_name: &str,
|
|
||||||
data: Value,
|
|
||||||
id: Option<u32>,
|
|
||||||
) -> Result<Value, String> {
|
|
||||||
let connection = self
|
let connection = self
|
||||||
.connection
|
.connection
|
||||||
.read()
|
.read()
|
||||||
.map_err(|_| "MTP connection lock is unavailable")?
|
.map_err(|_| "MTP connection lock is unavailable")?
|
||||||
.clone()
|
.clone()
|
||||||
.ok_or_else(|| "MTP is not connected".to_string())?;
|
.ok_or_else(|| "MTP is not connected".to_string())?;
|
||||||
let request = json_to_frame(type_name, data, id)?;
|
let request = json_to_frame(type_name, data, connection.next_request_id().await?)?;
|
||||||
let response = connection
|
let response = connection
|
||||||
|
.mtp
|
||||||
.request(&request, None)
|
.request(&request, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
|
|
@ -261,9 +301,12 @@ async fn supervise(generation: u64) {
|
||||||
manager.log(2, "Starting native MTP connection", None);
|
manager.log(2, "Starting native MTP connection", None);
|
||||||
android_status("Connecting");
|
android_status("Connecting");
|
||||||
match connect(&config).await {
|
match connect(&config).await {
|
||||||
Ok((connection, state)) => {
|
Ok(prepared) => {
|
||||||
delay = Duration::from_secs(1);
|
delay = Duration::from_secs(1);
|
||||||
let connection = Arc::new(connection);
|
let connection = Arc::new(ManagedConnection {
|
||||||
|
mtp: Arc::new(prepared.connection),
|
||||||
|
request_ids: prepared.request_ids,
|
||||||
|
});
|
||||||
let stale = {
|
let stale = {
|
||||||
let _guard = manager.start_lock.lock().expect("start lock poisoned");
|
let _guard = manager.start_lock.lock().expect("start lock poisoned");
|
||||||
let mut current = manager
|
let mut current = manager
|
||||||
|
|
@ -279,22 +322,47 @@ async fn supervise(generation: u64) {
|
||||||
generation,
|
generation,
|
||||||
ready_state: CONNECTED,
|
ready_state: CONNECTED,
|
||||||
identified: true,
|
identified: true,
|
||||||
state: Some(state),
|
state: Some(prepared.initial_state),
|
||||||
error: None,
|
error: None,
|
||||||
});
|
});
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if stale {
|
if stale {
|
||||||
connection.sender.close().await;
|
connection.mtp.sender.close().await;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
android_status("Connected");
|
android_status("Connected");
|
||||||
manager.log(2, "Native MTP connection established", None);
|
manager.log(2, "Native MTP connection established", None);
|
||||||
|
|
||||||
|
let (notification_tx, mut notification_rx) =
|
||||||
|
mpsc::channel(NOTIFICATION_QUEUE_CAPACITY);
|
||||||
|
let notification_connection = connection.clone();
|
||||||
|
let notification_config = config.clone();
|
||||||
|
let notification_worker = tokio::spawn(async move {
|
||||||
|
while let Some(frame) = notification_rx.recv().await {
|
||||||
|
if !manager.is_current(generation) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if let Err(error) = notify_message(
|
||||||
|
¬ification_config,
|
||||||
|
notification_connection.clone(),
|
||||||
|
&frame,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
eprintln!("failed to create background message notification: {error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for frame in prepared.buffered_frames {
|
||||||
|
handle_push(generation, ¬ification_tx, frame).await;
|
||||||
|
}
|
||||||
|
|
||||||
while manager.is_current(generation) {
|
while manager.is_current(generation) {
|
||||||
match connection.receive().await {
|
match connection.mtp.receive().await {
|
||||||
Ok(frame) => handle_push(generation, connection.clone(), frame).await,
|
Ok(frame) => handle_push(generation, ¬ification_tx, frame).await,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let _guard = manager.start_lock.lock().expect("start lock poisoned");
|
let _guard = manager.start_lock.lock().expect("start lock poisoned");
|
||||||
if manager.is_current(generation) {
|
if manager.is_current(generation) {
|
||||||
|
|
@ -310,6 +378,13 @@ async fn supervise(generation: u64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
drop(notification_tx);
|
||||||
|
notification_worker.abort();
|
||||||
|
if let Err(error) = notification_worker.await {
|
||||||
|
if !error.is_cancelled() {
|
||||||
|
eprintln!("background notification worker failed: {error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
let mut current = manager
|
let mut current = manager
|
||||||
.connection
|
.connection
|
||||||
.write()
|
.write()
|
||||||
|
|
@ -339,12 +414,21 @@ async fn supervise(generation: u64) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
android_status("Reconnecting");
|
android_status("Reconnecting");
|
||||||
tokio::time::sleep(delay).await;
|
tokio::time::sleep(jittered_retry_delay(delay)).await;
|
||||||
delay = (delay * 2).min(Duration::from_secs(60));
|
delay = (delay * 2).min(Duration::from_secs(60));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect(config: &MtpConfig) -> Result<(MTPConnection, Value), String> {
|
fn jittered_retry_delay(delay: Duration) -> Duration {
|
||||||
|
let entropy = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.map(|duration| duration.subsec_nanos())
|
||||||
|
.unwrap_or_default();
|
||||||
|
let percent = 80 + entropy % 41;
|
||||||
|
delay.mul_f64(percent as f64 / 100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect(config: &MtpConfig) -> Result<PreparedConnection, String> {
|
||||||
let (url, public_key) = resolve_endpoint(config)
|
let (url, public_key) = resolve_endpoint(config)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| format!("endpoint discovery failed: {error}"))?;
|
.map_err(|error| format!("endpoint discovery failed: {error}"))?;
|
||||||
|
|
@ -381,39 +465,10 @@ async fn connect(config: &MtpConfig) -> Result<(MTPConnection, Value), String> {
|
||||||
.map_err(|error| format!("transport authentication failed: {error}"))?;
|
.map_err(|error| format!("transport authentication failed: {error}"))?;
|
||||||
manager().log(2, "Native MTP authentication completed", None);
|
manager().log(2, "Native MTP authentication completed", None);
|
||||||
|
|
||||||
let connected = CommunicationValue::new(CommunicationType::ClientConnected)
|
let (state, buffered_frames) = await_initial_state(&connection).await?;
|
||||||
.add_typed_default(
|
let request_ids = RequestIdAllocator::new();
|
||||||
DataType::SessionId,
|
let (initial_state, ack) = prepare_initial_state_ack(&state, &request_ids)?;
|
||||||
DataValue::UnsignedNumber(current_millis() as u128),
|
let response = tokio::time::timeout(INITIAL_SYNC_TIMEOUT, connection.request(&ack, None))
|
||||||
)
|
|
||||||
.add_typed_default(DataType::VersionNumber, DataValue::UnsignedNumber(0))
|
|
||||||
.add_typed_default(DataType::CacheValid, DataValue::BoolFalse)
|
|
||||||
.add_typed_default(DataType::CacheSchemaVersion, DataValue::UnsignedNumber(0));
|
|
||||||
let state = tokio::time::timeout(
|
|
||||||
Duration::from_secs(30),
|
|
||||||
connection.request(&connected, None),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| "initial state synchronization timed out".to_string())?
|
|
||||||
.map_err(|error| format!("initial state synchronization failed: {error}"))?;
|
|
||||||
if !state.is_type(CommunicationType::ClientStateSync) {
|
|
||||||
return Err(format!(
|
|
||||||
"expected ClientStateSync, received {}",
|
|
||||||
state.get_type_name().unwrap_or("unknown")
|
|
||||||
));
|
|
||||||
}
|
|
||||||
let session_id = state
|
|
||||||
.get_data(DataType::SessionId)
|
|
||||||
.as_number()
|
|
||||||
.ok_or("ClientStateSync omitted SessionId")?;
|
|
||||||
let version = state
|
|
||||||
.get_data(DataType::VersionNumber)
|
|
||||||
.as_number()
|
|
||||||
.ok_or("ClientStateSync omitted VersionNumber")?;
|
|
||||||
let ack = CommunicationValue::new(CommunicationType::ClientStateAck)
|
|
||||||
.add_typed_default(DataType::SessionId, number_to_data(session_id))
|
|
||||||
.add_typed_default(DataType::VersionNumber, number_to_data(version));
|
|
||||||
let response = tokio::time::timeout(Duration::from_secs(30), connection.request(&ack, None))
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| "state acknowledgement timed out".to_string())?
|
.map_err(|_| "state acknowledgement timed out".to_string())?
|
||||||
.map_err(|error| format!("state acknowledgement failed: {error}"))?;
|
.map_err(|error| format!("state acknowledgement failed: {error}"))?;
|
||||||
|
|
@ -423,7 +478,182 @@ async fn connect(config: &MtpConfig) -> Result<(MTPConnection, Value), String> {
|
||||||
{
|
{
|
||||||
return Err(format!("ClientStateAck failed: {response}"));
|
return Err(format!("ClientStateAck failed: {response}"));
|
||||||
}
|
}
|
||||||
Ok((connection, frame_data_to_json(&state)?))
|
Ok(PreparedConnection {
|
||||||
|
connection,
|
||||||
|
request_ids,
|
||||||
|
initial_state,
|
||||||
|
buffered_frames,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare_initial_state_ack(
|
||||||
|
state: &CommunicationValue,
|
||||||
|
request_ids: &RequestIdAllocator,
|
||||||
|
) -> Result<(Value, CommunicationValue), String> {
|
||||||
|
let initial_state = frame_data_to_json(state)?;
|
||||||
|
validate_client_state_sync(&initial_state)?;
|
||||||
|
let data = initial_state
|
||||||
|
.as_object()
|
||||||
|
.ok_or("ClientStateSync payload is not an object")?;
|
||||||
|
let session_id = required_integer(data, "SessionId")?;
|
||||||
|
let version = required_integer(data, "VersionNumber")?;
|
||||||
|
let ack = CommunicationValue::new(communication_type("ClientStateAck")?)
|
||||||
|
.with_id(request_ids.next()?)
|
||||||
|
.add_typed_default(DataType::SessionId, number_to_data(session_id))
|
||||||
|
.add_typed_default(DataType::VersionNumber, number_to_data(version));
|
||||||
|
Ok((initial_state, ack))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_client_state_sync(state: &Value) -> Result<(), String> {
|
||||||
|
let data = state
|
||||||
|
.as_object()
|
||||||
|
.ok_or("ClientStateSync payload is not an object")?;
|
||||||
|
if required_integer(data, "SessionId")? <= 0 {
|
||||||
|
return Err("ClientStateSync SessionId must be a positive integer".into());
|
||||||
|
}
|
||||||
|
for field in ["VersionNumber", "CacheSchemaVersion"] {
|
||||||
|
if required_integer(data, field)? < 0 {
|
||||||
|
return Err(format!("ClientStateSync {field} must be nonnegative"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match data.get("SyncMode").and_then(Value::as_str) {
|
||||||
|
Some("full" | "delta") => {}
|
||||||
|
_ => return Err("ClientStateSync SyncMode must be 'full' or 'delta'".into()),
|
||||||
|
}
|
||||||
|
for field in ["Contacts", "Communities", "Calls", "Messages"] {
|
||||||
|
if !data.get(field).is_some_and(Value::is_array) {
|
||||||
|
return Err(format!("ClientStateSync {field} must be an array"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for field in ["DeletedMessageIds", "DeletedContactIds"] {
|
||||||
|
if let Some(value) = data.get(field) {
|
||||||
|
let values = value
|
||||||
|
.as_array()
|
||||||
|
.ok_or_else(|| format!("ClientStateSync {field} must be an array"))?;
|
||||||
|
if values.iter().any(|value| !value.is_number()) {
|
||||||
|
return Err(format!("ClientStateSync {field} must contain numbers"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
validate_object_array(data, "Communities", |_| Ok(()))?;
|
||||||
|
validate_object_array(data, "Contacts", validate_contact)?;
|
||||||
|
validate_object_array(data, "Calls", validate_call)?;
|
||||||
|
validate_object_array(data, "Messages", validate_message)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_object_array(
|
||||||
|
data: &Map<String, Value>,
|
||||||
|
field: &str,
|
||||||
|
validate: impl Fn(&Map<String, Value>) -> Result<(), String>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let values = data
|
||||||
|
.get(field)
|
||||||
|
.and_then(Value::as_array)
|
||||||
|
.ok_or_else(|| format!("ClientStateSync {field} must be an array"))?;
|
||||||
|
for value in values {
|
||||||
|
let object = value
|
||||||
|
.as_object()
|
||||||
|
.ok_or_else(|| format!("ClientStateSync {field} entries must be objects"))?;
|
||||||
|
validate(object)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_contact(contact: &Map<String, Value>) -> Result<(), String> {
|
||||||
|
if !contact.get("UserId").is_some_and(Value::is_number) {
|
||||||
|
return Err("ClientStateSync contact omitted numeric UserId".into());
|
||||||
|
}
|
||||||
|
if let Some(messages) = contact.get("Messages") {
|
||||||
|
let messages = messages
|
||||||
|
.as_array()
|
||||||
|
.ok_or("ClientStateSync contact Messages must be an array")?;
|
||||||
|
for message in messages {
|
||||||
|
validate_message(
|
||||||
|
message
|
||||||
|
.as_object()
|
||||||
|
.ok_or("ClientStateSync contact message must be an object")?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_call(call: &Map<String, Value>) -> Result<(), String> {
|
||||||
|
if !call.get("CallId").is_some_and(Value::is_string) {
|
||||||
|
return Err("ClientStateSync call omitted string CallId".into());
|
||||||
|
}
|
||||||
|
let members = call
|
||||||
|
.get("CallMembers")
|
||||||
|
.and_then(Value::as_array)
|
||||||
|
.ok_or("ClientStateSync call omitted CallMembers array")?;
|
||||||
|
if members.iter().any(|member| !member.is_number()) {
|
||||||
|
return Err("ClientStateSync CallMembers must contain numbers".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_message(message: &Map<String, Value>) -> Result<(), String> {
|
||||||
|
for field in ["SenderId", "SendTime"] {
|
||||||
|
if !message.get(field).is_some_and(Value::is_number) {
|
||||||
|
return Err(format!("ClientStateSync message omitted numeric {field}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let content = message
|
||||||
|
.get("Content")
|
||||||
|
.and_then(Value::as_str)
|
||||||
|
.ok_or("ClientStateSync message omitted string Content")?;
|
||||||
|
STANDARD
|
||||||
|
.decode(content)
|
||||||
|
.or_else(|_| STANDARD_NO_PAD.decode(content))
|
||||||
|
.map_err(|_| "ClientStateSync message Content must be base64".to_string())?;
|
||||||
|
if let Some(state) = message.get("MessageState") {
|
||||||
|
match state.as_str() {
|
||||||
|
Some("read" | "received" | "sent" | "sending" | "awaiting") => {}
|
||||||
|
_ => return Err("ClientStateSync message has invalid MessageState".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn required_integer(data: &Map<String, Value>, field: &str) -> Result<i128, String> {
|
||||||
|
let value = data
|
||||||
|
.get(field)
|
||||||
|
.ok_or_else(|| format!("ClientStateSync omitted {field}"))?;
|
||||||
|
if let Some(value) = value.as_i64() {
|
||||||
|
return Ok(value as i128);
|
||||||
|
}
|
||||||
|
value
|
||||||
|
.as_u64()
|
||||||
|
.map(|value| value as i128)
|
||||||
|
.ok_or_else(|| format!("ClientStateSync {field} must be an integer"))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn await_initial_state(
|
||||||
|
connection: &MTPConnection,
|
||||||
|
) -> Result<(CommunicationValue, Vec<CommunicationValue>), String> {
|
||||||
|
let mut buffered = Vec::new();
|
||||||
|
let deadline = tokio::time::Instant::now() + INITIAL_SYNC_TIMEOUT;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let frame = tokio::time::timeout_at(deadline, connection.receive())
|
||||||
|
.await
|
||||||
|
.map_err(|_| "initial state synchronization timed out".to_string())?
|
||||||
|
.map_err(|error| format!("initial state synchronization failed: {error}"))?;
|
||||||
|
|
||||||
|
if frame.is_type(CommunicationType::ErrorNoIota) {
|
||||||
|
return Err("No Iota is currently connected".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
if frame.get_type_name() == Some("ClientStateSync") {
|
||||||
|
return Ok((frame, buffered));
|
||||||
|
}
|
||||||
|
|
||||||
|
if buffered.len() == MAX_BUFFERED_INITIAL_FRAMES {
|
||||||
|
return Err("initial state synchronization buffered too many frames".into());
|
||||||
|
}
|
||||||
|
buffered.push(frame);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn resolve_endpoint(config: &MtpConfig) -> Result<(String, String), String> {
|
async fn resolve_endpoint(config: &MtpConfig) -> Result<(String, String), String> {
|
||||||
|
|
@ -465,7 +695,11 @@ async fn resolve_endpoint(config: &MtpConfig) -> Result<(String, String), String
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_push(generation: u64, connection: Arc<MTPConnection>, frame: CommunicationValue) {
|
async fn handle_push(
|
||||||
|
generation: u64,
|
||||||
|
notification_tx: &mpsc::Sender<CommunicationValue>,
|
||||||
|
frame: CommunicationValue,
|
||||||
|
) {
|
||||||
let manager = manager();
|
let manager = manager();
|
||||||
if let Ok(message) = frame_to_json(&frame) {
|
if let Ok(message) = frame_to_json(&frame) {
|
||||||
manager.emit(MtpEvent::Message {
|
manager.emit(MtpEvent::Message {
|
||||||
|
|
@ -478,7 +712,7 @@ async fn handle_push(generation: u64, connection: Arc<MTPConnection>, frame: Com
|
||||||
{
|
{
|
||||||
if let Some(partner_id) = frame
|
if let Some(partner_id) = frame
|
||||||
.get_data(DataType::ChatPartnerId)
|
.get_data(DataType::ChatPartnerId)
|
||||||
.as_number()
|
.and_then(DataValue::as_number)
|
||||||
.and_then(|value| u64::try_from(value).ok())
|
.and_then(|value| u64::try_from(value).ok())
|
||||||
{
|
{
|
||||||
if let Err(error) = android_cancel_notification(partner_id) {
|
if let Err(error) = android_cancel_notification(partner_id) {
|
||||||
|
|
@ -487,40 +721,39 @@ async fn handle_push(generation: u64, connection: Arc<MTPConnection>, frame: Com
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if frame.is_type(CommunicationType::MessageLive) && !manager.ui_visible.load(Ordering::SeqCst) {
|
if frame.is_type(CommunicationType::MessageLive) && !manager.ui_visible.load(Ordering::SeqCst) {
|
||||||
if let Err(error) = notify_message(connection, &frame).await {
|
if notification_tx.try_send(frame).is_err() {
|
||||||
eprintln!("failed to create background message notification: {error}");
|
eprintln!("background message notification queue is full");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn notify_message(
|
async fn notify_message(
|
||||||
connection: Arc<MTPConnection>,
|
config: &MtpConfig,
|
||||||
|
connection: Arc<ManagedConnection>,
|
||||||
frame: &CommunicationValue,
|
frame: &CommunicationValue,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let sender_id = frame
|
let sender_id = frame
|
||||||
.get_data(DataType::SenderId)
|
.get_data(DataType::SenderId)
|
||||||
.as_number()
|
.and_then(DataValue::as_number)
|
||||||
.and_then(|value| u64::try_from(value).ok())
|
.and_then(|value| u64::try_from(value).ok())
|
||||||
.ok_or("MessageLive omitted SenderId")?;
|
.ok_or("MessageLive omitted SenderId")?;
|
||||||
let message = frame.get_data(DataType::Message);
|
let message = frame
|
||||||
let content = container_value(message, DataType::Content)
|
.get_data(DataType::Message)
|
||||||
|
.ok_or("MessageLive omitted Message")?;
|
||||||
|
let content = container_value_by_name(message, "Content")
|
||||||
.and_then(DataValue::as_str)
|
.and_then(DataValue::as_str)
|
||||||
.ok_or("MessageLive omitted Content")?;
|
.ok_or("MessageLive omitted Content")?;
|
||||||
let config = manager()
|
|
||||||
.config
|
|
||||||
.read()
|
|
||||||
.expect("config lock poisoned")
|
|
||||||
.clone()
|
|
||||||
.ok_or("missing config")?;
|
|
||||||
let keyring_bytes = decode_browser_base64(&config.keyring)?;
|
let keyring_bytes = decode_browser_base64(&config.keyring)?;
|
||||||
let keyring = Keyring::from_bytes(&keyring_bytes).map_err(|error| error.to_string())?;
|
let keyring = Keyring::from_bytes(&keyring_bytes).map_err(|error| error.to_string())?;
|
||||||
let chat_id = derive_chat_id(config.user_id, sender_id);
|
let chat_id = derive_chat_id(config.user_id, sender_id);
|
||||||
let secret_id = format!("chat:{chat_id}:main");
|
let secret_id = format!("chat:{chat_id}:main");
|
||||||
let secret_request = CommunicationValue::new(CommunicationType::GetChatSecret)
|
let secret_request = CommunicationValue::new(CommunicationType::GetChatSecret)
|
||||||
|
.with_id(connection.next_request_id().await?)
|
||||||
.add_typed_default(DataType::UserId, DataValue::Str(config.user_id.to_string()))
|
.add_typed_default(DataType::UserId, DataValue::Str(config.user_id.to_string()))
|
||||||
.add_typed_default(DataType::ChatId, DataValue::Str(chat_id.clone()))
|
.add_typed_default(DataType::ChatId, DataValue::Str(chat_id.clone()))
|
||||||
.add_typed_default(DataType::SecretId, DataValue::Str(secret_id.clone()));
|
.add_typed_default(DataType::SecretId, DataValue::Str(secret_id.clone()));
|
||||||
let secret = connection
|
let secret = connection
|
||||||
|
.mtp
|
||||||
.request(&secret_request, None)
|
.request(&secret_request, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
|
|
@ -532,7 +765,7 @@ async fn notify_message(
|
||||||
}
|
}
|
||||||
let version = secret
|
let version = secret
|
||||||
.get_data(DataType::VersionNumber)
|
.get_data(DataType::VersionNumber)
|
||||||
.as_number()
|
.and_then(DataValue::as_number)
|
||||||
.ok_or("missing secret version")?;
|
.ok_or("missing secret version")?;
|
||||||
let encrypted_secret = secret
|
let encrypted_secret = secret
|
||||||
.get_bytes(DataType::EncryptedSecret)
|
.get_bytes(DataType::EncryptedSecret)
|
||||||
|
|
@ -560,11 +793,14 @@ async fn notify_message(
|
||||||
.decrypt(&ciphertext, b"")
|
.decrypt(&ciphertext, b"")
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
|
|
||||||
let user_request = CommunicationValue::new(CommunicationType::GetUserData).add_typed_default(
|
let user_request = CommunicationValue::new(CommunicationType::GetUserData)
|
||||||
DataType::UserId,
|
.with_id(connection.next_request_id().await?)
|
||||||
DataValue::UnsignedNumber(sender_id as u128),
|
.add_typed_default(
|
||||||
);
|
DataType::UserId,
|
||||||
|
DataValue::UnsignedNumber(sender_id as u128),
|
||||||
|
);
|
||||||
let user = connection
|
let user = connection
|
||||||
|
.mtp
|
||||||
.request(&user_request, None)
|
.request(&user_request, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
|
|
@ -652,17 +888,17 @@ fn container_value(value: &DataValue, field: DataType) -> Option<&DataValue> {
|
||||||
value.get_field(id)
|
value.get_field(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_to_frame(
|
fn container_value_by_name<'a>(value: &'a DataValue, field: &str) -> Option<&'a DataValue> {
|
||||||
type_name: &str,
|
container_value(value, DataType::from_name(field)?)
|
||||||
data: Value,
|
}
|
||||||
id: Option<u32>,
|
|
||||||
) -> Result<CommunicationValue, String> {
|
fn communication_type(name: &str) -> Result<CommunicationType, String> {
|
||||||
let comm_type = CommunicationType::from_name(type_name)
|
CommunicationType::from_name(name).ok_or_else(|| format!("unknown communication type: {name}"))
|
||||||
.ok_or_else(|| format!("unknown communication type: {type_name}"))?;
|
}
|
||||||
let mut frame = CommunicationValue::new(comm_type);
|
|
||||||
if let Some(id) = id {
|
fn json_to_frame(type_name: &str, data: Value, id: u32) -> Result<CommunicationValue, String> {
|
||||||
frame = frame.with_id(id);
|
let comm_type = communication_type(type_name)?;
|
||||||
}
|
let mut frame = CommunicationValue::new(comm_type).with_id(id);
|
||||||
let Value::Object(fields) = data else {
|
let Value::Object(fields) = data else {
|
||||||
return Err("MTP request data must be an object".into());
|
return Err("MTP request data must be an object".into());
|
||||||
};
|
};
|
||||||
|
|
@ -737,8 +973,8 @@ fn is_bytes_field(field: &str) -> bool {
|
||||||
|
|
||||||
fn frame_to_json(frame: &CommunicationValue) -> Result<Value, String> {
|
fn frame_to_json(frame: &CommunicationValue) -> Result<Value, String> {
|
||||||
let mut result = Map::new();
|
let mut result = Map::new();
|
||||||
if frame.get_id() != 0 {
|
if let Some(id) = frame.id() {
|
||||||
result.insert("id".into(), Value::from(frame.get_id()));
|
result.insert("id".into(), Value::from(id));
|
||||||
}
|
}
|
||||||
result.insert(
|
result.insert(
|
||||||
"type".into(),
|
"type".into(),
|
||||||
|
|
@ -751,7 +987,10 @@ fn frame_to_json(frame: &CommunicationValue) -> Result<Value, String> {
|
||||||
fn frame_data_to_json(frame: &CommunicationValue) -> Result<Value, String> {
|
fn frame_data_to_json(frame: &CommunicationValue) -> Result<Value, String> {
|
||||||
let map = frame.type_map().cloned().unwrap_or_else(TypeMap::latest);
|
let map = frame.type_map().cloned().unwrap_or_else(TypeMap::latest);
|
||||||
let mut result = Map::new();
|
let mut result = Map::new();
|
||||||
for (id, value) in frame.data() {
|
let entries = frame
|
||||||
|
.data()
|
||||||
|
.ok_or("MTP frame payload is not a data container")?;
|
||||||
|
for (id, value) in entries {
|
||||||
let name = map
|
let name = map
|
||||||
.data_type_name(id.0)
|
.data_type_name(id.0)
|
||||||
.ok_or_else(|| format!("unknown data type id: {}", id.0))?;
|
.ok_or_else(|| format!("unknown data type id: {}", id.0))?;
|
||||||
|
|
@ -801,16 +1040,15 @@ fn number_to_json(value: i128) -> Result<Value, String> {
|
||||||
.map_err(|_| "number exceeds JSON range".into())
|
.map_err(|_| "number exceeds JSON range".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_millis() -> u64 {
|
|
||||||
std::time::SystemTime::now()
|
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
|
||||||
.unwrap_or_default()
|
|
||||||
.as_millis() as u64
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{decode_browser_base64, decode_sdk_bytes};
|
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataValue};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
container_value_by_name, decode_browser_base64, decode_sdk_bytes, frame_to_json,
|
||||||
|
jittered_retry_delay, json_to_frame, prepare_initial_state_ack, RequestIdAllocator,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn browser_base64_accepts_file_whitespace_and_missing_padding() {
|
fn browser_base64_accepts_file_whitespace_and_missing_padding() {
|
||||||
|
|
@ -823,11 +1061,137 @@ mod tests {
|
||||||
assert_eq!(decode_sdk_bytes("0x01:02-ff").unwrap(), [1, 2, 255]);
|
assert_eq!(decode_sdk_bytes("0x01:02-ff").unwrap(), [1, 2, 255]);
|
||||||
assert_eq!(decode_sdk_bytes("AQI=").unwrap(), [1, 2]);
|
assert_eq!(decode_sdk_bytes("AQI=").unwrap(), [1, 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn request_ids_are_nonzero_and_monotonic() {
|
||||||
|
let ids = RequestIdAllocator::new();
|
||||||
|
|
||||||
|
assert_eq!(ids.next().unwrap(), 1);
|
||||||
|
assert_eq!(ids.next().unwrap(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_jitter_stays_within_policy_bounds() {
|
||||||
|
let delay = jittered_retry_delay(std::time::Duration::from_secs(10));
|
||||||
|
|
||||||
|
assert!(delay >= std::time::Duration::from_secs(8));
|
||||||
|
assert!(delay <= std::time::Duration::from_secs(12));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn json_content_uses_content_wire_type() {
|
||||||
|
let frame = json_to_frame(
|
||||||
|
"MessageEdit",
|
||||||
|
json!({
|
||||||
|
"Content": "ciphertext",
|
||||||
|
"ChatPartnerId": 42,
|
||||||
|
"SendTime": 10,
|
||||||
|
}),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
frame
|
||||||
|
.get_data(DataType::Content)
|
||||||
|
.and_then(DataValue::as_str),
|
||||||
|
Some("ciphertext")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_json_content_uses_content_wire_type() {
|
||||||
|
let frame = json_to_frame(
|
||||||
|
"MessageEdit",
|
||||||
|
json!({
|
||||||
|
"Message": { "Content": "ciphertext" },
|
||||||
|
}),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let message = frame.get_data(DataType::Message).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
container_value_by_name(message, "Content").and_then(DataValue::as_str),
|
||||||
|
Some("ciphertext")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn content_is_exposed_to_frontend() {
|
||||||
|
let frame = CommunicationValue::new(CommunicationType::MessageEditLive)
|
||||||
|
.with_id(1)
|
||||||
|
.add_typed_default(DataType::Content, DataValue::Str("ciphertext".into()));
|
||||||
|
|
||||||
|
let json = frame_to_json(&frame).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(json["data"]["Content"], "ciphertext");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn valid_initial_state() -> CommunicationValue {
|
||||||
|
CommunicationValue::new(communication_type("ClientStateSync").unwrap())
|
||||||
|
.add_typed_default(DataType::SessionId, DataValue::UnsignedNumber(1))
|
||||||
|
.add_typed_default(DataType::VersionNumber, DataValue::UnsignedNumber(0))
|
||||||
|
.add_typed_default(DataType::CacheSchemaVersion, DataValue::UnsignedNumber(0))
|
||||||
|
.add_typed_default(DataType::SyncMode, DataValue::Str("full".into()))
|
||||||
|
.add_typed_default(DataType::Contacts, DataValue::Array(vec![]))
|
||||||
|
.add_typed_default(DataType::Communities, DataValue::Array(vec![]))
|
||||||
|
.add_typed_default(DataType::Calls, DataValue::Array(vec![]))
|
||||||
|
.add_typed_default(DataType::Messages, DataValue::Array(vec![]))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn valid_initial_state_is_prepared_before_ack() {
|
||||||
|
let ids = RequestIdAllocator::new();
|
||||||
|
let (state, ack) = prepare_initial_state_ack(&valid_initial_state(), &ids).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(state["SyncMode"], "full");
|
||||||
|
assert_eq!(ack.get_type_name(), Some("ClientStateAck"));
|
||||||
|
assert_eq!(ack.id(), Some(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn malformed_initial_state_does_not_prepare_ack() {
|
||||||
|
let ids = RequestIdAllocator::new();
|
||||||
|
let malformed = valid_initial_state()
|
||||||
|
.add_typed_default(DataType::SyncMode, DataValue::Str("invalid".into()));
|
||||||
|
|
||||||
|
assert!(prepare_initial_state_ack(&malformed, &ids).is_err());
|
||||||
|
assert_eq!(
|
||||||
|
ids.next().unwrap(),
|
||||||
|
1,
|
||||||
|
"no acknowledgement ID was allocated"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn malformed_nested_initial_state_does_not_prepare_ack() {
|
||||||
|
let ids = RequestIdAllocator::new();
|
||||||
|
let malformed = CommunicationValue::new(communication_type("ClientStateSync").unwrap())
|
||||||
|
.add_typed_default(DataType::SessionId, DataValue::UnsignedNumber(1))
|
||||||
|
.add_typed_default(DataType::VersionNumber, DataValue::UnsignedNumber(0))
|
||||||
|
.add_typed_default(DataType::CacheSchemaVersion, DataValue::UnsignedNumber(0))
|
||||||
|
.add_typed_default(DataType::SyncMode, DataValue::Str("full".into()))
|
||||||
|
.add_typed_default(
|
||||||
|
DataType::Contacts,
|
||||||
|
DataValue::Array(vec![DataValue::Container(vec![])]),
|
||||||
|
)
|
||||||
|
.add_typed_default(DataType::Communities, DataValue::Array(vec![]))
|
||||||
|
.add_typed_default(DataType::Calls, DataValue::Array(vec![]))
|
||||||
|
.add_typed_default(DataType::Messages, DataValue::Array(vec![]));
|
||||||
|
|
||||||
|
assert!(prepare_initial_state_ack(&malformed, &ids).is_err());
|
||||||
|
assert_eq!(
|
||||||
|
ids.next().unwrap(),
|
||||||
|
1,
|
||||||
|
"no acknowledgement ID was allocated"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn mtp_request(type_name: String, data: Value, id: Option<u32>) -> Result<Value, String> {
|
pub async fn mtp_request(type_name: String, data: Value) -> Result<Value, String> {
|
||||||
manager().request(&type_name, data, id).await
|
manager().request(&type_name, data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@
|
||||||
"esbuild": "^0.28.1",
|
"esbuild": "^0.28.1",
|
||||||
"eslint": "^10.8.0",
|
"eslint": "^10.8.0",
|
||||||
"globals": "^17.9.0",
|
"globals": "^17.9.0",
|
||||||
|
"mtp": "*",
|
||||||
"typescript": "~6.0.3",
|
"typescript": "~6.0.3",
|
||||||
"typescript-eslint": "^8.66.0",
|
"typescript-eslint": "^8.66.0",
|
||||||
"vite": "^8.2.1"
|
"vite": "^8.2.1"
|
||||||
|
|
|
||||||
181
flake.nix
181
flake.nix
|
|
@ -17,8 +17,6 @@
|
||||||
systems = [ "x86_64-linux" ];
|
systems = [ "x86_64-linux" ];
|
||||||
forAllSystems = nixpkgs.lib.genAttrs systems;
|
forAllSystems = nixpkgs.lib.genAttrs systems;
|
||||||
version = "0.0.11";
|
version = "0.0.11";
|
||||||
x86_64DebHash = "sha256-E4FGMpCt2ByaN6d+YzglqeA6LbhMOQmPf3UzAj7uZXE=";
|
|
||||||
forgejoBaseUrl = "https://git.methanium.net/tensamin/client/releases/download/${version}";
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
packages = forAllSystems (
|
packages = forAllSystems (
|
||||||
|
|
@ -32,8 +30,6 @@
|
||||||
android_sdk.accept_license = true;
|
android_sdk.accept_license = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
debArtifact = "Tensamin-${version}-linux-amd64.deb";
|
|
||||||
|
|
||||||
electronRuntimeLibs = with pkgs; [
|
electronRuntimeLibs = with pkgs; [
|
||||||
alsa-lib
|
alsa-lib
|
||||||
at-spi2-atk
|
at-spi2-atk
|
||||||
|
|
@ -75,71 +71,130 @@
|
||||||
libXtst
|
libXtst
|
||||||
libxcb
|
libxcb
|
||||||
];
|
];
|
||||||
packageDeb =
|
electron = pkgs.electron;
|
||||||
src:
|
pnpm = pkgs.pnpm;
|
||||||
pkgs.stdenv.mkDerivation {
|
mtpTypeMaps = pkgs.fetchgit {
|
||||||
pname = "tensamin";
|
url = "https://git.methanium.net/tensamin/mtp-type-maps";
|
||||||
inherit version src;
|
rev = "6e5122fe44f793c0e0d3229b3d34145ce17c2d31";
|
||||||
|
hash = "sha256-/4n8F0YLJaLncefKL907P5l+en1CTjpgdFmLBF3gbiQ=";
|
||||||
nativeBuildInputs = with pkgs; [
|
};
|
||||||
autoPatchelfHook
|
mtpSource = pkgs.fetchzip {
|
||||||
dpkg
|
url = "https://git.methanium.net/methanium/mtp/releases/download/0.3.0-dev-c7c7afe/mtp-0.3.0.tgz";
|
||||||
makeWrapper
|
hash = "sha256-XLTa8DxP93Q4hBHRCLUzCPOqkbdb4V3aZxE5Iuq+kW0=";
|
||||||
];
|
};
|
||||||
buildInputs = electronRuntimeLibs;
|
mtpCargoDeps = pkgs.rustPlatform.fetchCargoVendor {
|
||||||
|
src = mtpSource;
|
||||||
dontConfigure = true;
|
hash = "sha256-8MZ65N/EtWPAggal0JkGDx3WSn+LxWQORinkqVbsrys=";
|
||||||
dontBuild = true;
|
};
|
||||||
|
wasmBindgenCliSource = pkgs.fetchCrate {
|
||||||
unpackPhase = ''
|
pname = "wasm-bindgen-cli";
|
||||||
runHook preUnpack
|
version = "0.2.127";
|
||||||
dpkg-deb -x "$src" .
|
hash = "sha256-di+qBAdd7pENLiIB9CoZoab+W5xeDoByMREcCGTSzWo=";
|
||||||
runHook postUnpack
|
};
|
||||||
'';
|
wasmBindgenCli = pkgs.buildWasmBindgenCli {
|
||||||
|
src = wasmBindgenCliSource;
|
||||||
installPhase = ''
|
cargoDeps = pkgs.rustPlatform.fetchCargoVendor {
|
||||||
runHook preInstall
|
src = wasmBindgenCliSource;
|
||||||
|
hash = "sha256-FTv2GZIAQs0ePdIZXIXil7JbZ6kIT05VG6vqC1qNFxQ=";
|
||||||
mkdir -p "$out"
|
|
||||||
cp -r opt "$out/"
|
|
||||||
cp -r usr/* "$out/"
|
|
||||||
|
|
||||||
mkdir -p "$out/bin"
|
|
||||||
makeWrapper "$out/opt/Tensamin/tensamin" "$out/bin/tensamin" \
|
|
||||||
--prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath electronRuntimeLibs}"
|
|
||||||
|
|
||||||
substituteInPlace "$out/share/applications/Tensamin.desktop" \
|
|
||||||
--replace-fail "Exec=/opt/Tensamin/tensamin" "Exec=tensamin"
|
|
||||||
|
|
||||||
runHook postInstall
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
defaultPackage = packageDeb (
|
};
|
||||||
pkgs.fetchurl {
|
desktopItem = pkgs.makeDesktopItem {
|
||||||
url = "${forgejoBaseUrl}/${debArtifact}";
|
name = "tensamin";
|
||||||
hash = x86_64DebHash;
|
desktopName = "Tensamin";
|
||||||
}
|
exec = "tensamin";
|
||||||
);
|
icon = "tensamin";
|
||||||
localDebPath = builtins.getEnv "TENSAMIN_DEB";
|
startupWMClass = "Tensamin";
|
||||||
localPathPackage =
|
categories = [ "Network" ];
|
||||||
if localDebPath == "" then
|
};
|
||||||
pkgs.writeShellScriptBin "tensamin" ''
|
defaultPackage = pkgs.stdenv.mkDerivation (finalAttrs: {
|
||||||
echo "Set TENSAMIN_DEB to a local .deb path and run with --impure." >&2
|
pname = "tensamin";
|
||||||
exit 1
|
inherit version;
|
||||||
''
|
src = self;
|
||||||
else
|
|
||||||
packageDeb (
|
pnpmDeps = pkgs.fetchPnpmDeps {
|
||||||
builtins.path {
|
inherit (finalAttrs) pname version src;
|
||||||
path = localDebPath;
|
inherit pnpm;
|
||||||
name = debArtifact;
|
fetcherVersion = 4;
|
||||||
}
|
hash = "sha256-imP3MTr1YLc28Z9n617m0Wt/6vPirFznzoriKzojlEg=";
|
||||||
);
|
};
|
||||||
|
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
copyDesktopItems
|
||||||
|
makeWrapper
|
||||||
|
nodejs_22
|
||||||
|
pnpm
|
||||||
|
pnpmConfigHook
|
||||||
|
cargo
|
||||||
|
lld
|
||||||
|
rustc
|
||||||
|
wasm-pack
|
||||||
|
wasmBindgenCli
|
||||||
|
binaryen
|
||||||
|
];
|
||||||
|
|
||||||
|
env.ELECTRON_SKIP_BINARY_DOWNLOAD = 1;
|
||||||
|
|
||||||
|
postPatch = ''
|
||||||
|
rm -rf mtp-type-maps
|
||||||
|
ln -s ${mtpTypeMaps} mtp-type-maps
|
||||||
|
|
||||||
|
node -e '
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = "apps/electron/package.json";
|
||||||
|
const pkg = JSON.parse(fs.readFileSync(path, "utf8"));
|
||||||
|
pkg.version = "${version}";
|
||||||
|
fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + "\n");
|
||||||
|
'
|
||||||
|
'';
|
||||||
|
|
||||||
|
buildPhase = ''
|
||||||
|
runHook preBuild
|
||||||
|
|
||||||
|
mkdir -p "$HOME/.cargo"
|
||||||
|
substitute ${mtpCargoDeps}/.cargo/config.toml "$HOME/.cargo/config.toml" \
|
||||||
|
--replace-fail @vendor@ ${mtpCargoDeps}
|
||||||
|
|
||||||
|
pnpm run copy-licenses
|
||||||
|
pnpm run build:packages
|
||||||
|
pnpm run build:web
|
||||||
|
pnpm --dir apps/tauri run gen-icons
|
||||||
|
pnpm --dir apps/electron run build
|
||||||
|
pnpm --dir apps/electron exec electron-builder --dir --linux --publish never \
|
||||||
|
--config.electronDist=${electron.dist} \
|
||||||
|
--config.electronVersion=${electron.version}
|
||||||
|
|
||||||
|
runHook postBuild
|
||||||
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
runHook preInstall
|
||||||
|
|
||||||
|
mkdir -p "$out/lib/tensamin" "$out/bin"
|
||||||
|
cp -r apps/electron/release/linux-unpacked/. "$out/lib/tensamin/"
|
||||||
|
|
||||||
|
makeWrapper "$out/lib/tensamin/tensamin" "$out/bin/tensamin" \
|
||||||
|
--prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath electronRuntimeLibs}"
|
||||||
|
|
||||||
|
install -Dm644 apps/electron/build/icons/icon.png \
|
||||||
|
"$out/share/icons/hicolor/512x512/apps/tensamin.png"
|
||||||
|
|
||||||
|
runHook postInstall
|
||||||
|
'';
|
||||||
|
|
||||||
|
desktopItems = [ desktopItem ];
|
||||||
|
|
||||||
|
meta = {
|
||||||
|
description = "Tensamin desktop client";
|
||||||
|
homepage = "https://git.methanium.net/tensamin/client";
|
||||||
|
mainProgram = "tensamin";
|
||||||
|
platforms = pkgs.lib.platforms.linux;
|
||||||
|
};
|
||||||
|
});
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
default = defaultPackage;
|
default = defaultPackage;
|
||||||
tensamin = defaultPackage;
|
tensamin = defaultPackage;
|
||||||
electron = defaultPackage;
|
electron = defaultPackage;
|
||||||
localPathForDev = localPathPackage;
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit a297dcce60bc6e84696c6a16f5fd510beb2ca643
|
Subproject commit f4e45aa3a3ad0e3c3a257f66857b904a1af7901c
|
||||||
23
package.json
23
package.json
|
|
@ -25,26 +25,27 @@
|
||||||
"start-adb:mobile": "cd apps/tauri && pnpm run start-adb:mobile",
|
"start-adb:mobile": "cd apps/tauri && pnpm run start-adb:mobile",
|
||||||
"dev:desktop": "cd apps/electron && pnpm run dev",
|
"dev:desktop": "cd apps/electron && pnpm run dev",
|
||||||
"build:desktop": "cd apps/electron && pnpm run package",
|
"build:desktop": "cd apps/electron && pnpm run package",
|
||||||
"delete:mobile": "nix develop .#tauri --command node apps/tauri/scripts/delete-mobile.ts"
|
"delete:mobile": "nix develop .#tauri --command node apps/tauri/scripts/delete-mobile.ts",
|
||||||
|
"update-submodules": "git submodule update --remote --force --recursive"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@types/node": "^26.1.2",
|
"@types/node": "^26.2.0",
|
||||||
"@types/react": "^19.2.18",
|
"@types/react": "^19.2.18",
|
||||||
"@types/react-dom": "^19.2.4",
|
"@types/react-dom": "^19.2.4",
|
||||||
"@typescript-eslint/parser": "^8.66.0",
|
"@typescript-eslint/parser": "^8.67.0",
|
||||||
"eslint": "^10.8.0",
|
"eslint": "^10.8.1",
|
||||||
"eslint-plugin-react-hooks": "^7.1.1",
|
"eslint-plugin-react-hooks": "^7.1.1",
|
||||||
"fallow": "^3.14.0",
|
"fallow": "^3.17.0",
|
||||||
"globals": "^17.9.0",
|
"globals": "^17.11.0",
|
||||||
"prettier": "^3.9.6",
|
"prettier": "^3.9.6",
|
||||||
"typescript": "^6.0.3",
|
"typescript": "^6.0.3",
|
||||||
"typescript-eslint": "^8.66.0",
|
"typescript-eslint": "^8.67.0",
|
||||||
"vitest": "^4.1.10"
|
"vitest": "^4.1.11"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@methanium/ui": "*",
|
"@methanium/ui": "https://git.methanium.net/methanium/ui/releases/download/0.0.29/methanium-ui.tgz",
|
||||||
"mtp": "*",
|
"mtp": "https://git.methanium.net/methanium/mtp/releases/download/0.3.0-b331b9f6a3/mtp-0.3.0.tgz",
|
||||||
"sonner": "^2.0.7"
|
"sonner": "^2.0.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
packages/cache/src/sync.tsx
vendored
18
packages/cache/src/sync.tsx
vendored
|
|
@ -21,8 +21,7 @@ export function removeMissingContactSnapshots<T extends { UserId: number }>(
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CacheSync() {
|
export default function CacheSync() {
|
||||||
const { addInterceptor, contextReady, freshContacts, subscribePush } =
|
const { addInterceptor, contextReady, freshContacts, subscribe } = useMTP();
|
||||||
useMTP();
|
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
const [accountId, setAccountId] = useState(0);
|
const [accountId, setAccountId] = useState(0);
|
||||||
const queueRef = useRef(Promise.resolve());
|
const queueRef = useRef(Promise.resolve());
|
||||||
|
|
@ -321,10 +320,19 @@ export default function CacheSync() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!accountId || !contextReady) return;
|
if (!accountId || !contextReady) return;
|
||||||
return subscribePush((message) => {
|
const handleMessage = (message: ProtocolMessage) => {
|
||||||
void enqueue(() => synchronizePush(message));
|
void enqueue(() => synchronizePush(message));
|
||||||
});
|
};
|
||||||
}, [accountId, contextReady, enqueue, subscribePush, synchronizePush]);
|
const unsubscribers = [
|
||||||
|
subscribe("GetStates", handleMessage),
|
||||||
|
subscribe("MessageLive", handleMessage),
|
||||||
|
subscribe("MessageEditLive", handleMessage),
|
||||||
|
subscribe("MessageDeleteLive", handleMessage),
|
||||||
|
subscribe("MessageState", handleMessage),
|
||||||
|
subscribe("MessageReactionLive", handleMessage),
|
||||||
|
];
|
||||||
|
return () => unsubscribers.forEach((unsubscribe) => unsubscribe());
|
||||||
|
}, [accountId, contextReady, enqueue, subscribe, synchronizePush]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@
|
||||||
"deepfilternet3-noise-filter": "1.3.0",
|
"deepfilternet3-noise-filter": "1.3.0",
|
||||||
"livekit-client": "^2.21.0",
|
"livekit-client": "^2.21.0",
|
||||||
"lucide-react": "^1.29.0",
|
"lucide-react": "^1.29.0",
|
||||||
|
"mtp": "*",
|
||||||
"react": "^19.2.8",
|
"react": "^19.2.8",
|
||||||
"react-dom": "^19.2.8",
|
"react-dom": "^19.2.8",
|
||||||
"recharts": "^3.10.1",
|
"recharts": "^3.10.1",
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ export function getMediaShareAdapter(): MediaShareAdapter {
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
MediaShareAdapter,
|
MediaShareAdapter,
|
||||||
MediaShareCapabilities,
|
|
||||||
MediaShareKind,
|
MediaShareKind,
|
||||||
MediaShareRequest,
|
MediaShareRequest,
|
||||||
MediaShareSession,
|
MediaShareSession,
|
||||||
|
|
|
||||||
|
|
@ -1223,7 +1223,7 @@ export const useCall = create<{
|
||||||
export function useInitializeCall() {
|
export function useInitializeCall() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const { send, subscribePush } = useMTP();
|
const { send, subscribe } = useMTP();
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
const { insertCall } = useSession();
|
const { insertCall } = useSession();
|
||||||
const { get } = useUser();
|
const { get } = useUser();
|
||||||
|
|
@ -1313,14 +1313,9 @@ export function useInitializeCall() {
|
||||||
|
|
||||||
// listen to call invites
|
// listen to call invites
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return subscribePush(async (message) => {
|
return subscribe("CallInvite", async ({ data }) => {
|
||||||
if (message.type !== "CallInvite") return;
|
const { CallId, CallSecret, SenderId } = data;
|
||||||
|
if (!CallId || !CallSecret || !SenderId) return;
|
||||||
const { CallId, CallSecret, SenderId } = message.data as {
|
|
||||||
CallId: string;
|
|
||||||
CallSecret: ProtocolCallSecret;
|
|
||||||
SenderId: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (SenderId === Number(await load("user_id"))) {
|
if (SenderId === Number(await load("user_id"))) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -1337,7 +1332,7 @@ export function useInitializeCall() {
|
||||||
SenderId,
|
SenderId,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}, [load, subscribePush, showCallingScreen]);
|
}, [load, subscribe, showCallingScreen]);
|
||||||
|
|
||||||
// get callId from url
|
// get callId from url
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,7 @@ export async function fetchReplyMessage({
|
||||||
|
|
||||||
export default function Provider({ children }: { children: ReactNode }) {
|
export default function Provider({ children }: { children: ReactNode }) {
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
const { send, subscribePush } = useMTP();
|
const { send, subscribe } = useMTP();
|
||||||
const { get: getUser } = useUser();
|
const { get: getUser } = useUser();
|
||||||
const { moveUserIdToTop } = useSession();
|
const { moveUserIdToTop } = useSession();
|
||||||
|
|
||||||
|
|
@ -912,124 +912,47 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
// Get live updates for message states
|
// Get live updates for message states
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return subscribePush((message) => {
|
const unsubscribeEdit = subscribe("MessageEditLive", ({ data }) => {
|
||||||
if (message.type === "MessageEditLive") {
|
if (!currentChatSecret) return;
|
||||||
if (!currentChatSecret) return;
|
if (data.ChatPartnerId !== userIdValue) {
|
||||||
|
|
||||||
const rawData = message.data as {
|
|
||||||
ChatPartnerId: unknown;
|
|
||||||
SendTime: unknown;
|
|
||||||
Content: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const chatPartnerId = Number(rawData.ChatPartnerId);
|
|
||||||
const sendTime = Number(rawData.SendTime);
|
|
||||||
|
|
||||||
if (!Number.isFinite(chatPartnerId) || !Number.isFinite(sendTime)) {
|
|
||||||
log(
|
|
||||||
3,
|
|
||||||
"chat",
|
|
||||||
"yellow",
|
|
||||||
"Cancel message edit update due to invalid data",
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chatPartnerId !== userIdValue) {
|
|
||||||
log(
|
|
||||||
3,
|
|
||||||
"chat",
|
|
||||||
"yellow",
|
|
||||||
"Cancel message edit update due to user ID mismatch",
|
|
||||||
{
|
|
||||||
expected: userIdValue,
|
|
||||||
received: chatPartnerId,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void decryptChatText(currentChatSecret, rawData.Content)
|
|
||||||
.then((content) => {
|
|
||||||
editMessage(sendTime, { Content: content, Edited: true });
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
log(1, "chat", "red", "Failed to decrypt message edit", err, {
|
|
||||||
SendTime: sendTime,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.type === "MessageReactionLive") {
|
|
||||||
const rawData = message.data as {
|
|
||||||
ChatPartnerId: unknown;
|
|
||||||
SendTime: unknown;
|
|
||||||
Reaction: string;
|
|
||||||
SenderId: unknown;
|
|
||||||
Accepted: boolean;
|
|
||||||
};
|
|
||||||
const chatPartnerId = Number(rawData.ChatPartnerId);
|
|
||||||
const sendTime = Number(rawData.SendTime);
|
|
||||||
const senderId = Number(rawData.SenderId);
|
|
||||||
|
|
||||||
if (
|
|
||||||
chatPartnerId !== userIdValue ||
|
|
||||||
!Number.isFinite(sendTime) ||
|
|
||||||
!Number.isFinite(senderId)
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
applyLiveReaction(
|
|
||||||
sendTime,
|
|
||||||
rawData.Reaction,
|
|
||||||
senderId,
|
|
||||||
rawData.Accepted,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.type === "MessageDeleteLive") {
|
|
||||||
const data = message.data as {
|
|
||||||
ChatPartnerId: number;
|
|
||||||
SendTime: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (data.ChatPartnerId !== userIdValue) return;
|
|
||||||
|
|
||||||
removeMessage(data.SendTime);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.type !== "MessageState") return;
|
|
||||||
|
|
||||||
const rawData = message.data as {
|
|
||||||
ChatPartnerId: unknown;
|
|
||||||
SendTime: unknown;
|
|
||||||
MessageState: RawMessage["MessageState"];
|
|
||||||
};
|
|
||||||
|
|
||||||
const nextState = {
|
|
||||||
ChatPartnerId: Number(rawData.ChatPartnerId),
|
|
||||||
SendTime: Number(rawData.SendTime),
|
|
||||||
MessageState: rawData.MessageState,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (
|
|
||||||
!Number.isFinite(nextState.ChatPartnerId) ||
|
|
||||||
!Number.isFinite(nextState.SendTime)
|
|
||||||
) {
|
|
||||||
log(
|
log(
|
||||||
3,
|
3,
|
||||||
"chat",
|
"chat",
|
||||||
"yellow",
|
"yellow",
|
||||||
"Cancel message state update due to invalid data",
|
"Cancel message edit update due to user ID mismatch",
|
||||||
|
{
|
||||||
|
expected: userIdValue,
|
||||||
|
received: data.ChatPartnerId,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextState.ChatPartnerId !== userIdValue) {
|
void decryptChatText(currentChatSecret, data.Content)
|
||||||
|
.then((content) => {
|
||||||
|
editMessage(data.SendTime, { Content: content, Edited: true });
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
log(1, "chat", "red", "Failed to decrypt message edit", err, {
|
||||||
|
SendTime: data.SendTime,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const unsubscribeReaction = subscribe("MessageReactionLive", ({ data }) => {
|
||||||
|
if (data.ChatPartnerId !== userIdValue) return;
|
||||||
|
applyLiveReaction(
|
||||||
|
data.SendTime,
|
||||||
|
data.Reaction,
|
||||||
|
data.SenderId,
|
||||||
|
data.Accepted,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
const unsubscribeDelete = subscribe("MessageDeleteLive", ({ data }) => {
|
||||||
|
if (data.ChatPartnerId !== userIdValue) return;
|
||||||
|
removeMessage(data.SendTime);
|
||||||
|
});
|
||||||
|
const unsubscribeState = subscribe("MessageState", ({ data }) => {
|
||||||
|
if (data.ChatPartnerId !== userIdValue) {
|
||||||
log(
|
log(
|
||||||
3,
|
3,
|
||||||
"chat",
|
"chat",
|
||||||
|
|
@ -1037,22 +960,27 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
"Cancel message state update due to user ID mismatch",
|
"Cancel message state update due to user ID mismatch",
|
||||||
{
|
{
|
||||||
expected: userIdValue,
|
expected: userIdValue,
|
||||||
received: nextState.ChatPartnerId,
|
received: data.ChatPartnerId,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
editMessage(data.SendTime, {
|
||||||
editMessage(nextState.SendTime, {
|
MessageState: data.MessageState,
|
||||||
MessageState: nextState.MessageState,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
return () => {
|
||||||
|
unsubscribeEdit();
|
||||||
|
unsubscribeReaction();
|
||||||
|
unsubscribeDelete();
|
||||||
|
unsubscribeState();
|
||||||
|
};
|
||||||
}, [
|
}, [
|
||||||
currentChatSecret,
|
currentChatSecret,
|
||||||
applyLiveReaction,
|
applyLiveReaction,
|
||||||
editMessage,
|
editMessage,
|
||||||
removeMessage,
|
removeMessage,
|
||||||
subscribePush,
|
subscribe,
|
||||||
userIdValue,
|
userIdValue,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
"build": "tsc -p tsconfig.json --noEmit"
|
"build": "tsc -p tsconfig.json --noEmit"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"mtp": "*",
|
||||||
"react": "^19.2.8",
|
"react": "^19.2.8",
|
||||||
"react-dom": "^19.2.8"
|
"react-dom": "^19.2.8"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,13 @@ export function deriveCallSecretId(callId: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ownKemPublicKeyFromKeyring(keyring: string): Uint8Array {
|
export function ownKemPublicKeyFromKeyring(keyring: string): Uint8Array {
|
||||||
return crypto.keyringToKeys(keyring).kemPublicKey;
|
return crypto.keyringToKeys({ value: keyring, encoding: "base64" })
|
||||||
|
.kemPublicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function kemPublicKeyFromPublicKeyBundle(publicKey: string): Uint8Array {
|
export function kemPublicKeyFromPublicKeyBundle(publicKey: string): Uint8Array {
|
||||||
return crypto.publicKeyBundleToKeys(publicKey).kemPublicKey;
|
return crypto.publicKeyBundleToKeys({ value: publicKey, encoding: "base64" })
|
||||||
|
.kemPublicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function wrapCallSecret(args: {
|
export async function wrapCallSecret(args: {
|
||||||
|
|
@ -72,7 +74,10 @@ export async function unwrapCallSecret(args: {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ownKeys = crypto.keyringToKeys(args.keyring);
|
const ownKeys = crypto.keyringToKeys({
|
||||||
|
value: args.keyring,
|
||||||
|
encoding: "base64",
|
||||||
|
});
|
||||||
const sharedSecret = crypto.decapsulate(
|
const sharedSecret = crypto.decapsulate(
|
||||||
ownKeys.kemSecretKey,
|
ownKeys.kemSecretKey,
|
||||||
args.kemCiphertext,
|
args.kemCiphertext,
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,13 @@ export function randomChatSecret(): Uint8Array {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ownKemPublicKeyFromKeyring(keyring: string): Uint8Array {
|
export function ownKemPublicKeyFromKeyring(keyring: string): Uint8Array {
|
||||||
return crypto.keyringToKeys(keyring).kemPublicKey;
|
return crypto.keyringToKeys({ value: keyring, encoding: "base64" })
|
||||||
|
.kemPublicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function kemPublicKeyFromPublicKeyBundle(publicKey: string): Uint8Array {
|
export function kemPublicKeyFromPublicKeyBundle(publicKey: string): Uint8Array {
|
||||||
return crypto.publicKeyBundleToKeys(publicKey).kemPublicKey;
|
return crypto.publicKeyBundleToKeys({ value: publicKey, encoding: "base64" })
|
||||||
|
.kemPublicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function wrapChatSecret(args: {
|
export async function wrapChatSecret(args: {
|
||||||
|
|
@ -79,7 +81,10 @@ export async function unwrapChatSecret(args: {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ownKeys = crypto.keyringToKeys(args.keyring);
|
const ownKeys = crypto.keyringToKeys({
|
||||||
|
value: args.keyring,
|
||||||
|
encoding: "base64",
|
||||||
|
});
|
||||||
const sharedSecret = crypto.decapsulate(
|
const sharedSecret = crypto.decapsulate(
|
||||||
ownKeys.kemSecretKey,
|
ownKeys.kemSecretKey,
|
||||||
args.kemCiphertext,
|
args.kemCiphertext,
|
||||||
|
|
|
||||||
30
packages/markdown/package.json
Normal file
30
packages/markdown/package.json
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name": "@tensamin/markdown",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
"./text": "./src/text.tsx",
|
||||||
|
"./input": "./src/input.tsx",
|
||||||
|
"./emoji": "./src/emoji.tsx"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"format": "pnpm exec prettier --write .",
|
||||||
|
"lint": "eslint src",
|
||||||
|
"build": "tsc -p tsconfig.json --noEmit"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@codemirror/autocomplete": "^6.20.3",
|
||||||
|
"@codemirror/commands": "^6.10.4",
|
||||||
|
"@codemirror/lang-markdown": "^6.5.2",
|
||||||
|
"@codemirror/language": "^6.12.4",
|
||||||
|
"@codemirror/state": "^6.7.1",
|
||||||
|
"@codemirror/view": "^6.43.8",
|
||||||
|
"@methanium/ui": "*",
|
||||||
|
"@twemoji/api": "^17.0.3",
|
||||||
|
"emojibase-data": "^17.0.0",
|
||||||
|
"lucide-react": "^1.30.0",
|
||||||
|
"react": "^19.2.8",
|
||||||
|
"react-dom": "^19.2.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
50
packages/markdown/src/emoji.tsx
Normal file
50
packages/markdown/src/emoji.tsx
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import twemoji from "@twemoji/api";
|
||||||
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@methanium/ui";
|
||||||
|
import { resolveEmoji } from "./emojiData";
|
||||||
|
|
||||||
|
export {
|
||||||
|
emojis,
|
||||||
|
findEmojiShortcodes,
|
||||||
|
normalizeShortcode,
|
||||||
|
resolveEmoji,
|
||||||
|
searchEmojis,
|
||||||
|
} from "./emojiData";
|
||||||
|
export type { EmojiDefinition } from "./emojiData";
|
||||||
|
|
||||||
|
export function getEmojiUrl(shortcode: string): string | undefined {
|
||||||
|
const emoji = resolveEmoji(shortcode);
|
||||||
|
return emoji ? `${twemoji.base}svg/${emoji.hexcode}.svg` : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Emoji({
|
||||||
|
className = "h-6 w-6",
|
||||||
|
shortcode,
|
||||||
|
tooltip = true,
|
||||||
|
}: {
|
||||||
|
className?: string;
|
||||||
|
shortcode: string;
|
||||||
|
tooltip?: boolean;
|
||||||
|
}) {
|
||||||
|
const emoji = resolveEmoji(shortcode);
|
||||||
|
if (!emoji) return <span>{shortcode}</span>;
|
||||||
|
|
||||||
|
const image = (
|
||||||
|
<img
|
||||||
|
alt={emoji.shortcode}
|
||||||
|
className={className}
|
||||||
|
decoding="async"
|
||||||
|
draggable={false}
|
||||||
|
loading="lazy"
|
||||||
|
src={`${twemoji.base}svg/${emoji.hexcode}.svg`}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!tooltip) return image;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger render={image} />
|
||||||
|
<TooltipContent sideOffset={8}>{emoji.shortcode}</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
}
|
||||||
94
packages/markdown/src/emojiData.ts
Normal file
94
packages/markdown/src/emojiData.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
import shortcodeData from "emojibase-data/en/shortcodes/joypixels.json";
|
||||||
|
|
||||||
|
export type EmojiDefinition = {
|
||||||
|
aliases: readonly string[];
|
||||||
|
hexcode: string;
|
||||||
|
name: string;
|
||||||
|
shortcode: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalizeName(value: string) {
|
||||||
|
return value
|
||||||
|
.trim()
|
||||||
|
.replace(/^:+|:+$/g, "")
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const emojis: readonly EmojiDefinition[] = Object.entries(
|
||||||
|
shortcodeData as Record<string, string | string[]>,
|
||||||
|
).map(([hexcode, value]) => {
|
||||||
|
const aliases = Array.isArray(value) ? value : [value];
|
||||||
|
const name = aliases[0];
|
||||||
|
|
||||||
|
return {
|
||||||
|
aliases,
|
||||||
|
hexcode: hexcode.toLowerCase().replaceAll("_", "-"),
|
||||||
|
name,
|
||||||
|
shortcode: `:${name}:`,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const emojiByName = new Map<string, EmojiDefinition>();
|
||||||
|
for (const emoji of emojis) {
|
||||||
|
for (const alias of emoji.aliases) {
|
||||||
|
emojiByName.set(normalizeName(alias), emoji);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveEmoji(value: string): EmojiDefinition | undefined {
|
||||||
|
return emojiByName.get(normalizeName(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function normalizeShortcode(value: string): string | undefined {
|
||||||
|
return resolveEmoji(value)?.shortcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findEmojiShortcodes(value: string) {
|
||||||
|
const matches: Array<{
|
||||||
|
emoji: EmojiDefinition;
|
||||||
|
from: number;
|
||||||
|
to: number;
|
||||||
|
}> = [];
|
||||||
|
let searchFrom = 0;
|
||||||
|
|
||||||
|
while (searchFrom < value.length) {
|
||||||
|
const from = value.indexOf(":", searchFrom);
|
||||||
|
if (from === -1) break;
|
||||||
|
|
||||||
|
const candidate = value.slice(from).match(/^:([a-z0-9_+-]+):/i);
|
||||||
|
if (!candidate) {
|
||||||
|
searchFrom = from + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const emoji = resolveEmoji(candidate[1]);
|
||||||
|
if (!emoji) {
|
||||||
|
// The closing colon may also open the next valid shortcode.
|
||||||
|
searchFrom = from + candidate[0].length - 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const to = from + candidate[0].length;
|
||||||
|
matches.push({ emoji, from, to });
|
||||||
|
searchFrom = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function searchEmojis(query: string): EmojiDefinition[] {
|
||||||
|
const normalizedQuery = normalizeName(query);
|
||||||
|
if (!normalizedQuery) return [...emojis];
|
||||||
|
|
||||||
|
return emojis
|
||||||
|
.map((emoji) => {
|
||||||
|
const names = emoji.aliases.map(normalizeName);
|
||||||
|
const exact = names.includes(normalizedQuery);
|
||||||
|
const prefix = names.some((name) => name.startsWith(normalizedQuery));
|
||||||
|
const contains = names.some((name) => name.includes(normalizedQuery));
|
||||||
|
return { emoji, rank: exact ? 0 : prefix ? 1 : contains ? 2 : 3 };
|
||||||
|
})
|
||||||
|
.filter(({ rank }) => rank < 3)
|
||||||
|
.sort((a, b) => a.rank - b.rank || a.emoji.name.localeCompare(b.emoji.name))
|
||||||
|
.map(({ emoji }) => emoji);
|
||||||
|
}
|
||||||
850
packages/markdown/src/input.tsx
Normal file
850
packages/markdown/src/input.tsx
Normal file
|
|
@ -0,0 +1,850 @@
|
||||||
|
import { markdown } from "@codemirror/lang-markdown";
|
||||||
|
import { syntaxTree } from "@codemirror/language";
|
||||||
|
import {
|
||||||
|
acceptCompletion,
|
||||||
|
autocompletion,
|
||||||
|
completionStatus,
|
||||||
|
pickedCompletion,
|
||||||
|
startCompletion,
|
||||||
|
type Completion,
|
||||||
|
type CompletionContext,
|
||||||
|
type CompletionResult,
|
||||||
|
} from "@codemirror/autocomplete";
|
||||||
|
import {
|
||||||
|
EditorState,
|
||||||
|
EditorSelection,
|
||||||
|
Annotation,
|
||||||
|
Compartment,
|
||||||
|
Prec,
|
||||||
|
Transaction,
|
||||||
|
type Extension,
|
||||||
|
type Range,
|
||||||
|
type SelectionRange,
|
||||||
|
} from "@codemirror/state";
|
||||||
|
import {
|
||||||
|
Decoration,
|
||||||
|
EditorView,
|
||||||
|
keymap,
|
||||||
|
placeholder,
|
||||||
|
ViewPlugin,
|
||||||
|
WidgetType,
|
||||||
|
type DecorationSet,
|
||||||
|
type KeyBinding,
|
||||||
|
type ViewUpdate,
|
||||||
|
} from "@codemirror/view";
|
||||||
|
import {
|
||||||
|
defaultKeymap,
|
||||||
|
history,
|
||||||
|
historyKeymap,
|
||||||
|
indentWithTab,
|
||||||
|
} from "@codemirror/commands";
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
import type { CSSProperties } from "react";
|
||||||
|
import { createRoot, type Root } from "react-dom/client";
|
||||||
|
|
||||||
|
import { collectInlineRanges, ensureMarkdownStyles } from "./markdown";
|
||||||
|
import Emoji, {
|
||||||
|
findEmojiShortcodes,
|
||||||
|
getEmojiUrl,
|
||||||
|
resolveEmoji,
|
||||||
|
searchEmojis,
|
||||||
|
} from "./emoji";
|
||||||
|
|
||||||
|
export const MAX_RENDERED_EMOJI_OPTIONS = 100;
|
||||||
|
|
||||||
|
export type InputController = {
|
||||||
|
focus: () => void;
|
||||||
|
hasFocus: () => boolean;
|
||||||
|
insertText: (text: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InputProps = {
|
||||||
|
ref?: HTMLDivElement;
|
||||||
|
placeholder?: string;
|
||||||
|
value: string;
|
||||||
|
setValue: (value: string) => void;
|
||||||
|
onSubmit?: () => void;
|
||||||
|
invertEnterBehavior?: boolean;
|
||||||
|
styled?: boolean;
|
||||||
|
fontSize?: CSSProperties["fontSize"];
|
||||||
|
paddingX?: CSSProperties["padding"];
|
||||||
|
paddingY?: CSSProperties["padding"];
|
||||||
|
className?: string;
|
||||||
|
emojiFrequencies?: Readonly<Record<string, number>>;
|
||||||
|
onEmojiSelect?: (shortcode: string) => void;
|
||||||
|
autoFocus?: boolean;
|
||||||
|
onControllerChange?: (controller: InputController | null) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
function toCssLength(value: CSSProperties["padding"]): string | undefined {
|
||||||
|
if (value === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeof value === "number" ? `${value}px` : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toCssPadding(
|
||||||
|
vertical: CSSProperties["padding"],
|
||||||
|
horizontal: CSSProperties["padding"],
|
||||||
|
styled: boolean,
|
||||||
|
): string {
|
||||||
|
const defaultVertical = styled ? "0.25rem" : "0";
|
||||||
|
const defaultHorizontal = styled ? "0.625rem" : "0";
|
||||||
|
|
||||||
|
return `${toCssLength(vertical) ?? defaultVertical} ${toCssLength(horizontal) ?? defaultHorizontal}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hiddenTokenDecoration = Decoration.mark({ class: "tm-md-hidden-token" });
|
||||||
|
const strongDecoration = Decoration.mark({ class: "tm-md-strong" });
|
||||||
|
const emDecoration = Decoration.mark({ class: "tm-md-em" });
|
||||||
|
const delDecoration = Decoration.mark({ class: "tm-md-del" });
|
||||||
|
const codeDecoration = Decoration.mark({ class: "tm-md-code" });
|
||||||
|
const linkDecoration = Decoration.mark({ class: "tm-md-link" });
|
||||||
|
const codeLineDecoration = Decoration.line({ class: "tm-md-code-line" });
|
||||||
|
const externalValueSync = Annotation.define<boolean>();
|
||||||
|
const widgetRoots = new WeakMap<HTMLElement, Root>();
|
||||||
|
|
||||||
|
type EmojiRange = {
|
||||||
|
from: number;
|
||||||
|
shortcode: string;
|
||||||
|
to: number;
|
||||||
|
url: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EmojiWidget extends WidgetType {
|
||||||
|
readonly shortcode: string;
|
||||||
|
readonly url: string;
|
||||||
|
|
||||||
|
constructor(shortcode: string, url: string) {
|
||||||
|
super();
|
||||||
|
this.shortcode = shortcode;
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
eq(other: EmojiWidget) {
|
||||||
|
return other.shortcode === this.shortcode && other.url === this.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
toDOM() {
|
||||||
|
const container = document.createElement("span");
|
||||||
|
const root = createRoot(container);
|
||||||
|
root.render(
|
||||||
|
<Emoji className="tm-md-editor-emoji" shortcode={this.shortcode} />,
|
||||||
|
);
|
||||||
|
widgetRoots.set(container, root);
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy(dom: HTMLElement) {
|
||||||
|
widgetRoots.get(dom)?.unmount();
|
||||||
|
widgetRoots.delete(dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
ignoreEvent() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function codeRanges(state: EditorState) {
|
||||||
|
const ranges: Array<{ from: number; to: number }> = [];
|
||||||
|
|
||||||
|
syntaxTree(state).iterate({
|
||||||
|
enter(node) {
|
||||||
|
if (
|
||||||
|
node.name === "InlineCode" ||
|
||||||
|
node.name === "FencedCode" ||
|
||||||
|
node.name === "CodeBlock"
|
||||||
|
) {
|
||||||
|
ranges.push({ from: node.from, to: node.to });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return ranges;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findEmojiRanges(state: EditorState): EmojiRange[] {
|
||||||
|
const document = state.doc.toString();
|
||||||
|
const excluded = codeRanges(state);
|
||||||
|
const ranges: EmojiRange[] = [];
|
||||||
|
|
||||||
|
for (const match of findEmojiShortcodes(document)) {
|
||||||
|
const { from, to } = match;
|
||||||
|
const inCode = excluded.some((range) => from < range.to && to > range.from);
|
||||||
|
const emoji = inCode ? undefined : match.emoji;
|
||||||
|
const url = emoji ? getEmojiUrl(emoji.shortcode) : undefined;
|
||||||
|
|
||||||
|
if (emoji && url) {
|
||||||
|
ranges.push({ from, shortcode: emoji.shortcode, to, url });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ranges;
|
||||||
|
}
|
||||||
|
|
||||||
|
class EmojiPluginValue {
|
||||||
|
decorations: DecorationSet;
|
||||||
|
ranges: EmojiRange[];
|
||||||
|
|
||||||
|
constructor(view: EditorView) {
|
||||||
|
this.ranges = findEmojiRanges(view.state);
|
||||||
|
this.decorations = this.buildDecorations();
|
||||||
|
}
|
||||||
|
|
||||||
|
update(update: ViewUpdate) {
|
||||||
|
if (
|
||||||
|
update.docChanged ||
|
||||||
|
syntaxTree(update.startState) !== syntaxTree(update.state)
|
||||||
|
) {
|
||||||
|
this.ranges = findEmojiRanges(update.state);
|
||||||
|
this.decorations = this.buildDecorations();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildDecorations() {
|
||||||
|
return Decoration.set(
|
||||||
|
this.ranges.map((range) =>
|
||||||
|
Decoration.replace({
|
||||||
|
inclusive: false,
|
||||||
|
widget: new EmojiWidget(range.shortcode, range.url),
|
||||||
|
}).range(range.from, range.to),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const emojiDecorations = ViewPlugin.fromClass(EmojiPluginValue, {
|
||||||
|
decorations: (instance) => instance.decorations,
|
||||||
|
provide: (plugin) =>
|
||||||
|
EditorView.atomicRanges.of(
|
||||||
|
(view) => view.plugin(plugin)?.decorations ?? Decoration.none,
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
function deleteEmoji(view: EditorView, direction: "backward" | "forward") {
|
||||||
|
const ranges = view.plugin(emojiDecorations)?.ranges ?? [];
|
||||||
|
const deletions: Array<{ from: number; to: number }> = [];
|
||||||
|
|
||||||
|
for (const selection of view.state.selection.ranges) {
|
||||||
|
if (selection.empty) {
|
||||||
|
const emoji = ranges.find((range) =>
|
||||||
|
direction === "backward"
|
||||||
|
? selection.from > range.from && selection.from <= range.to
|
||||||
|
: selection.from >= range.from && selection.from < range.to,
|
||||||
|
);
|
||||||
|
if (emoji) deletions.push({ from: emoji.from, to: emoji.to });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let from = selection.from;
|
||||||
|
let to = selection.to;
|
||||||
|
let changed = false;
|
||||||
|
|
||||||
|
for (const emoji of ranges) {
|
||||||
|
if (from < emoji.to && to > emoji.from) {
|
||||||
|
from = Math.min(from, emoji.from);
|
||||||
|
to = Math.max(to, emoji.to);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed) deletions.push({ from, to });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deletions.length === 0) return false;
|
||||||
|
|
||||||
|
const merged = deletions
|
||||||
|
.sort((a, b) => a.from - b.from)
|
||||||
|
.reduce<Array<{ from: number; to: number }>>((result, deletion) => {
|
||||||
|
const previous = result.at(-1);
|
||||||
|
if (previous && deletion.from <= previous.to) {
|
||||||
|
previous.to = Math.max(previous.to, deletion.to);
|
||||||
|
} else {
|
||||||
|
result.push({ ...deletion });
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
view.dispatch({
|
||||||
|
changes: merged.map((range) => ({ from: range.from, to: range.to })),
|
||||||
|
selection: EditorSelection.cursor(merged[0].from),
|
||||||
|
scrollIntoView: true,
|
||||||
|
userEvent: direction === "backward" ? "delete.backward" : "delete.forward",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds markdown styling decorations every time the document or cursor selection changes.
|
||||||
|
* Token delimiters are hidden unless the cursor is currently intersecting that token range.
|
||||||
|
*/
|
||||||
|
const markdownDecorations = ViewPlugin.fromClass(
|
||||||
|
class {
|
||||||
|
decorations: DecorationSet;
|
||||||
|
|
||||||
|
constructor(view: EditorView) {
|
||||||
|
this.decorations = buildDecorations(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
update(update: ViewUpdate) {
|
||||||
|
if (update.docChanged || update.selectionSet || update.viewportChanged) {
|
||||||
|
this.decorations = buildDecorations(update.view);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
decorations: (instance: { decorations: DecorationSet }) =>
|
||||||
|
instance.decorations,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes Input.
|
||||||
|
* @param props Parameter props.
|
||||||
|
* @returns unknown.
|
||||||
|
*/
|
||||||
|
export default function Input(props: InputProps) {
|
||||||
|
ensureMarkdownStyles();
|
||||||
|
|
||||||
|
const shellClassName = props.styled
|
||||||
|
? "min-h-8 w-full min-w-0 rounded-lg border border-input bg-transparent text-base transition-colors outline-none placeholder:text-muted-foreground disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40"
|
||||||
|
: "";
|
||||||
|
|
||||||
|
const elementRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const viewRef = useRef<EditorView | undefined>(undefined);
|
||||||
|
const setValueRef = useRef(props.setValue);
|
||||||
|
const onSubmitRef = useRef<InputProps["onSubmit"]>(props.onSubmit);
|
||||||
|
const onEmojiSelectRef = useRef<InputProps["onEmojiSelect"]>(
|
||||||
|
props.onEmojiSelect,
|
||||||
|
);
|
||||||
|
const invertEnterBehaviorRef = useRef(Boolean(props.invertEnterBehavior));
|
||||||
|
const completionCompartmentRef = useRef<Compartment | null>(null);
|
||||||
|
completionCompartmentRef.current ??= new Compartment();
|
||||||
|
const completionCompartment = completionCompartmentRef.current;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setValueRef.current = props.setValue;
|
||||||
|
onSubmitRef.current = props.onSubmit;
|
||||||
|
onEmojiSelectRef.current = props.onEmojiSelect;
|
||||||
|
invertEnterBehaviorRef.current = Boolean(props.invertEnterBehavior);
|
||||||
|
}, [
|
||||||
|
props.onEmojiSelect,
|
||||||
|
props.onSubmit,
|
||||||
|
props.invertEnterBehavior,
|
||||||
|
props.setValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!elementRef.current) return;
|
||||||
|
|
||||||
|
const state = EditorState.create({
|
||||||
|
doc: props.value,
|
||||||
|
extensions: createEditorExtensions(
|
||||||
|
(value) => {
|
||||||
|
setValueRef.current(value);
|
||||||
|
},
|
||||||
|
() => props.placeholder,
|
||||||
|
() => invertEnterBehaviorRef.current,
|
||||||
|
() => onSubmitRef.current?.(),
|
||||||
|
completionCompartment,
|
||||||
|
props.emojiFrequencies,
|
||||||
|
(shortcode) => onEmojiSelectRef.current?.(shortcode),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
viewRef.current = new EditorView({
|
||||||
|
state,
|
||||||
|
parent: elementRef.current,
|
||||||
|
});
|
||||||
|
props.onControllerChange?.({
|
||||||
|
focus: () => viewRef.current?.contentDOM.focus({ preventScroll: true }),
|
||||||
|
hasFocus: () => viewRef.current?.hasFocus ?? false,
|
||||||
|
insertText: (text) => {
|
||||||
|
const editor = viewRef.current;
|
||||||
|
if (!editor) return;
|
||||||
|
editor.dispatch({
|
||||||
|
...editor.state.replaceSelection(text),
|
||||||
|
annotations: Transaction.userEvent.of("input.type"),
|
||||||
|
scrollIntoView: true,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (props.autoFocus) viewRef.current.focus();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
props.onControllerChange?.(null);
|
||||||
|
viewRef.current?.destroy();
|
||||||
|
viewRef.current = undefined;
|
||||||
|
};
|
||||||
|
// Run once to initialize/destroy the editor instance.
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const editor = viewRef.current;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const next = props.value;
|
||||||
|
const current = editor.state.doc.toString();
|
||||||
|
|
||||||
|
if (next === current) return;
|
||||||
|
|
||||||
|
editor.dispatch({
|
||||||
|
changes: {
|
||||||
|
from: 0,
|
||||||
|
to: current.length,
|
||||||
|
insert: next,
|
||||||
|
},
|
||||||
|
annotations: [
|
||||||
|
externalValueSync.of(true),
|
||||||
|
Transaction.addToHistory.of(false),
|
||||||
|
],
|
||||||
|
filter: false,
|
||||||
|
});
|
||||||
|
}, [props.value]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const editor = viewRef.current;
|
||||||
|
const compartment = completionCompartmentRef.current;
|
||||||
|
if (!editor || !compartment) return;
|
||||||
|
|
||||||
|
const wasActive = completionStatus(editor.state) === "active";
|
||||||
|
editor.dispatch({
|
||||||
|
effects: compartment.reconfigure(
|
||||||
|
createEmojiAutocomplete(props.emojiFrequencies, (shortcode) =>
|
||||||
|
onEmojiSelectRef.current?.(shortcode),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
if (wasActive) startCompletion(editor);
|
||||||
|
}, [props.emojiFrequencies]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={elementRef}
|
||||||
|
className={`tm-md-root ${shellClassName} ${props.className ?? ""}`}
|
||||||
|
style={
|
||||||
|
{
|
||||||
|
fontSize: props.fontSize ?? "1rem",
|
||||||
|
"--tm-md-content-padding": toCssPadding(
|
||||||
|
props.paddingY,
|
||||||
|
props.paddingX,
|
||||||
|
Boolean(props.styled),
|
||||||
|
),
|
||||||
|
} as CSSProperties & {
|
||||||
|
"--tm-md-content-padding"?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes createEditorExtensions.
|
||||||
|
* @param onChange Parameter onChange.
|
||||||
|
* @param getPlaceholder Parameter getPlaceholder.
|
||||||
|
* @param getInvertEnterBehavior Parameter getInvertEnterBehavior.
|
||||||
|
* @param onSubmit Parameter onSubmit.
|
||||||
|
* @returns Extension[].
|
||||||
|
*/
|
||||||
|
function createEditorExtensions(
|
||||||
|
onChange: (value: string) => void,
|
||||||
|
getPlaceholder: () => string | undefined,
|
||||||
|
getInvertEnterBehavior: () => boolean,
|
||||||
|
onSubmit: () => void,
|
||||||
|
completionCompartment: Compartment,
|
||||||
|
emojiFrequencies: Readonly<Record<string, number>> | undefined,
|
||||||
|
onEmojiSelect: (shortcode: string) => void,
|
||||||
|
): Extension[] {
|
||||||
|
const editorKeymap = [
|
||||||
|
...defaultKeymap,
|
||||||
|
...historyKeymap,
|
||||||
|
indentWithTab,
|
||||||
|
] as unknown as readonly KeyBinding[];
|
||||||
|
|
||||||
|
const customEnterKeymap = keymap.of([
|
||||||
|
{
|
||||||
|
key: "Shift-Enter",
|
||||||
|
run: (view) => {
|
||||||
|
if (completionStatus(view.state) === "active") {
|
||||||
|
return acceptCompletion(view);
|
||||||
|
}
|
||||||
|
if (!getInvertEnterBehavior()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
onSubmit();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "Enter",
|
||||||
|
run: (view) => {
|
||||||
|
if (completionStatus(view.state) === "active") {
|
||||||
|
return acceptCompletion(view);
|
||||||
|
}
|
||||||
|
if (getInvertEnterBehavior()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
onSubmit();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
const completionTabKeymap = keymap.of([
|
||||||
|
{
|
||||||
|
key: "Tab",
|
||||||
|
run: (view) =>
|
||||||
|
completionStatus(view.state) === "active"
|
||||||
|
? acceptCompletion(view)
|
||||||
|
: false,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
const emojiDeletionKeymap = keymap.of([
|
||||||
|
{
|
||||||
|
key: "Backspace",
|
||||||
|
run: (view) => deleteEmoji(view, "backward"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "Delete",
|
||||||
|
run: (view) => deleteEmoji(view, "forward"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
return [
|
||||||
|
history(),
|
||||||
|
markdown(),
|
||||||
|
completionCompartment.of(
|
||||||
|
createEmojiAutocomplete(emojiFrequencies, onEmojiSelect),
|
||||||
|
),
|
||||||
|
emojiDecorations,
|
||||||
|
keymap.of(editorKeymap),
|
||||||
|
Prec.highest(completionTabKeymap),
|
||||||
|
Prec.highest(emojiDeletionKeymap),
|
||||||
|
Prec.highest(customEnterKeymap),
|
||||||
|
EditorView.lineWrapping,
|
||||||
|
placeholder(getPlaceholder() ?? ""),
|
||||||
|
EditorView.updateListener.of((update: ViewUpdate) => {
|
||||||
|
if (!update.docChanged) return;
|
||||||
|
if (
|
||||||
|
update.transactions.some(
|
||||||
|
(transaction) => transaction.annotation(externalValueSync) === true,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onChange(update.state.doc.toString());
|
||||||
|
}),
|
||||||
|
EditorView.theme({
|
||||||
|
"&": {
|
||||||
|
fontSize: "inherit",
|
||||||
|
},
|
||||||
|
"&.cm-editor": {
|
||||||
|
width: "100%",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
EditorView.editorAttributes.of({
|
||||||
|
class: "tm-md-editor",
|
||||||
|
spellcheck: "true",
|
||||||
|
"aria-label": "Markdown input",
|
||||||
|
}),
|
||||||
|
markdownDecorations,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function createEmojiAutocomplete(
|
||||||
|
frequencies: Readonly<Record<string, number>> | undefined,
|
||||||
|
onEmojiSelect: (shortcode: string) => void,
|
||||||
|
) {
|
||||||
|
return autocompletion({
|
||||||
|
activateOnTyping: true,
|
||||||
|
addToOptions: [
|
||||||
|
{
|
||||||
|
position: 20,
|
||||||
|
render(completion) {
|
||||||
|
const container = document.createElement("span");
|
||||||
|
createRoot(container).render(
|
||||||
|
<Emoji
|
||||||
|
className="tm-md-completion-emoji"
|
||||||
|
shortcode={completion.label}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
return container;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
maxRenderedOptions: MAX_RENDERED_EMOJI_OPTIONS,
|
||||||
|
override: [createEmojiCompletionSource(frequencies, onEmojiSelect)],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizedFrequencies(
|
||||||
|
frequencies: Readonly<Record<string, number>> | undefined,
|
||||||
|
) {
|
||||||
|
const normalized = new Map<string, number>();
|
||||||
|
for (const [value, frequency] of Object.entries(frequencies ?? {})) {
|
||||||
|
const shortcode = resolveEmoji(value)?.shortcode;
|
||||||
|
if (!shortcode || !Number.isFinite(frequency) || frequency <= 0) continue;
|
||||||
|
normalized.set(shortcode, (normalized.get(shortcode) ?? 0) + frequency);
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createEmojiCompletionSource(
|
||||||
|
frequencies?: Readonly<Record<string, number>>,
|
||||||
|
onEmojiSelect: (shortcode: string) => void = () => undefined,
|
||||||
|
) {
|
||||||
|
const normalized = normalizedFrequencies(frequencies);
|
||||||
|
const maxFrequency = Math.max(0, ...normalized.values());
|
||||||
|
|
||||||
|
return (context: CompletionContext): CompletionResult | null => {
|
||||||
|
const token = context.matchBefore(/:[a-z0-9_+-]*$/i);
|
||||||
|
if (!token) return null;
|
||||||
|
if (
|
||||||
|
codeRanges(context.state).some(
|
||||||
|
(range) => token.from < range.to && token.to > range.from,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const characterBefore = context.state.sliceDoc(
|
||||||
|
Math.max(0, token.from - 1),
|
||||||
|
token.from,
|
||||||
|
);
|
||||||
|
if (characterBefore && /[a-z0-9_]/i.test(characterBefore)) return null;
|
||||||
|
|
||||||
|
const query = token.text.slice(1).toLowerCase();
|
||||||
|
const options: Completion[] = searchEmojis(query)
|
||||||
|
.map((emoji) => {
|
||||||
|
const aliases = emoji.aliases.map((alias) => alias.toLowerCase());
|
||||||
|
const matchedAlias =
|
||||||
|
aliases.find((alias) => alias === query) ??
|
||||||
|
aliases.find((alias) => alias.startsWith(query)) ??
|
||||||
|
aliases.find((alias) => alias.includes(query)) ??
|
||||||
|
emoji.name;
|
||||||
|
const relevance = !query
|
||||||
|
? 0
|
||||||
|
: matchedAlias === query
|
||||||
|
? 80
|
||||||
|
: matchedAlias.startsWith(query)
|
||||||
|
? 40
|
||||||
|
: 0;
|
||||||
|
const frequency = normalized.get(emoji.shortcode) ?? 0;
|
||||||
|
const usage =
|
||||||
|
maxFrequency > 0
|
||||||
|
? (15 * Math.log1p(frequency)) / Math.log1p(maxFrequency)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return {
|
||||||
|
apply(view, completion, from, to) {
|
||||||
|
view.dispatch({
|
||||||
|
annotations: pickedCompletion.of(completion),
|
||||||
|
changes: { from, insert: `${emoji.shortcode} `, to },
|
||||||
|
selection: EditorSelection.cursor(
|
||||||
|
from + emoji.shortcode.length + 1,
|
||||||
|
),
|
||||||
|
});
|
||||||
|
onEmojiSelect(emoji.shortcode);
|
||||||
|
},
|
||||||
|
boost: relevance + usage,
|
||||||
|
displayLabel: emoji.shortcode,
|
||||||
|
label: `:${matchedAlias}:`,
|
||||||
|
type: "text",
|
||||||
|
frequency,
|
||||||
|
relevance,
|
||||||
|
} satisfies Completion & { frequency: number; relevance: number };
|
||||||
|
})
|
||||||
|
.sort(
|
||||||
|
(a, b) =>
|
||||||
|
b.relevance - a.relevance ||
|
||||||
|
b.frequency - a.frequency ||
|
||||||
|
(a.displayLabel ?? a.label).localeCompare(b.displayLabel ?? b.label),
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
from: token.from,
|
||||||
|
options,
|
||||||
|
validFor: /^:[a-z0-9_+-]*$/i,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const emojiCompletionSource = createEmojiCompletionSource();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes buildDecorations.
|
||||||
|
* @param view Parameter view.
|
||||||
|
* @returns DecorationSet.
|
||||||
|
*/
|
||||||
|
function buildDecorations(view: EditorView): DecorationSet {
|
||||||
|
const builder: Range<Decoration>[] = [];
|
||||||
|
const selections = view.state.selection.ranges.map(
|
||||||
|
(range: SelectionRange) => ({
|
||||||
|
from: range.from,
|
||||||
|
to: range.to,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
let codeFenceOpen = false;
|
||||||
|
|
||||||
|
for (
|
||||||
|
let lineNumber = 1;
|
||||||
|
lineNumber <= view.state.doc.lines;
|
||||||
|
lineNumber += 1
|
||||||
|
) {
|
||||||
|
const line = view.state.doc.line(lineNumber);
|
||||||
|
const text = line.text;
|
||||||
|
const lineFrom = line.from;
|
||||||
|
const trimmed = text.trim();
|
||||||
|
|
||||||
|
const fence = text.match(/^```\s*([^`]*)$/);
|
||||||
|
if (fence) {
|
||||||
|
const ticksStart = lineFrom + text.indexOf("```");
|
||||||
|
const ticksEnd = ticksStart + 3;
|
||||||
|
addHiddenToken(builder, selections, { from: ticksStart, to: ticksEnd });
|
||||||
|
if (trimmed.length > 3) {
|
||||||
|
addHiddenToken(builder, selections, {
|
||||||
|
from: ticksEnd,
|
||||||
|
to: line.to,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
codeFenceOpen = !codeFenceOpen;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (codeFenceOpen) {
|
||||||
|
builder.push(codeLineDecoration.range(lineFrom));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const heading = text.match(/^(#{1,6})\s+/);
|
||||||
|
if (heading) {
|
||||||
|
const markerLength = heading[0].length;
|
||||||
|
addHiddenToken(builder, selections, {
|
||||||
|
from: lineFrom,
|
||||||
|
to: lineFrom + markerLength,
|
||||||
|
});
|
||||||
|
|
||||||
|
const level = heading[1].length;
|
||||||
|
const headingClass = Decoration.mark({
|
||||||
|
class: `tm-md-heading tm-md-h${String(level)}`,
|
||||||
|
});
|
||||||
|
const contentFrom = lineFrom + markerLength;
|
||||||
|
if (contentFrom < line.to) {
|
||||||
|
builder.push(headingClass.range(contentFrom, line.to));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const quote = text.match(/^>\s?/);
|
||||||
|
if (quote) {
|
||||||
|
addHiddenToken(builder, selections, {
|
||||||
|
from: lineFrom,
|
||||||
|
to: lineFrom + quote[0].length,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const unordered = text.match(/^(\s*)([-+*])\s+(?:\[( |x|X)\]\s+)?/);
|
||||||
|
if (unordered) {
|
||||||
|
const markerStart = lineFrom + unordered[1].length;
|
||||||
|
const markerEnd = markerStart + unordered[2].length + 1;
|
||||||
|
addHiddenToken(builder, selections, { from: markerStart, to: markerEnd });
|
||||||
|
|
||||||
|
const checkbox = unordered[0].match(/\[( |x|X)\]\s+$/);
|
||||||
|
if (checkbox) {
|
||||||
|
const checkboxStart = lineFrom + unordered[0].lastIndexOf("[");
|
||||||
|
addHiddenToken(builder, selections, {
|
||||||
|
from: checkboxStart,
|
||||||
|
to: checkboxStart + checkbox[0].length,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ordered = text.match(/^(\s*)(\d+\.)\s+/);
|
||||||
|
if (ordered) {
|
||||||
|
const markerStart = lineFrom + ordered[1].length;
|
||||||
|
addHiddenToken(builder, selections, {
|
||||||
|
from: markerStart,
|
||||||
|
to: markerStart + ordered[2].length + 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/.test(trimmed)) {
|
||||||
|
if (lineFrom < line.to) {
|
||||||
|
builder.push(
|
||||||
|
Decoration.mark({ class: "tm-md-hr" }).range(lineFrom, line.to),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableSeparator = /^\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)+\|?$/.test(
|
||||||
|
text,
|
||||||
|
);
|
||||||
|
if (tableSeparator) {
|
||||||
|
if (lineFrom < line.to) {
|
||||||
|
builder.push(
|
||||||
|
Decoration.mark({ class: "tm-md-del" }).range(lineFrom, line.to),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { styleRanges, tokenRanges } = collectInlineRanges(text, lineFrom);
|
||||||
|
|
||||||
|
for (const range of styleRanges) {
|
||||||
|
if (range.from >= range.to) continue;
|
||||||
|
|
||||||
|
if (range.className === "tm-md-strong") {
|
||||||
|
builder.push(strongDecoration.range(range.from, range.to));
|
||||||
|
} else if (range.className === "tm-md-em") {
|
||||||
|
builder.push(emDecoration.range(range.from, range.to));
|
||||||
|
} else if (range.className === "tm-md-del") {
|
||||||
|
builder.push(delDecoration.range(range.from, range.to));
|
||||||
|
} else if (range.className === "tm-md-code") {
|
||||||
|
builder.push(codeDecoration.range(range.from, range.to));
|
||||||
|
} else if (range.className === "tm-md-link") {
|
||||||
|
builder.push(linkDecoration.range(range.from, range.to));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const token of tokenRanges) {
|
||||||
|
addHiddenToken(builder, selections, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Decoration.set(builder, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keeps markdown syntax visible only when user selection intersects the token.
|
||||||
|
* This preserves cursor predictability and cross-token selection while still hiding syntax during reading.
|
||||||
|
*/
|
||||||
|
function addHiddenToken(
|
||||||
|
builder: Range<Decoration>[],
|
||||||
|
selections: ReadonlyArray<{ from: number; to: number }>,
|
||||||
|
token: {
|
||||||
|
from: number;
|
||||||
|
to: number;
|
||||||
|
},
|
||||||
|
): void {
|
||||||
|
if (token.from >= token.to) return;
|
||||||
|
|
||||||
|
const overlapsSelection = selections.some((selection) => {
|
||||||
|
const selectionFrom = Math.min(selection.from, selection.to);
|
||||||
|
const selectionTo = Math.max(selection.from, selection.to);
|
||||||
|
|
||||||
|
if (selectionFrom === selectionTo) {
|
||||||
|
return selectionFrom >= token.from && selectionFrom <= token.to;
|
||||||
|
}
|
||||||
|
|
||||||
|
return selectionFrom < token.to && selectionTo > token.from;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (overlapsSelection) return;
|
||||||
|
builder.push(hiddenTokenDecoration.range(token.from, token.to));
|
||||||
|
}
|
||||||
815
packages/markdown/src/markdown.tsx
Normal file
815
packages/markdown/src/markdown.tsx
Normal file
|
|
@ -0,0 +1,815 @@
|
||||||
|
import {
|
||||||
|
Fragment,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
type ReactElement,
|
||||||
|
type ReactNode,
|
||||||
|
} from "react";
|
||||||
|
import Emoji from "./emoji";
|
||||||
|
import { findEmojiShortcodes } from "./emojiData";
|
||||||
|
import { Check } from "lucide-react";
|
||||||
|
|
||||||
|
type InlineNode =
|
||||||
|
| { type: "text"; value: string }
|
||||||
|
| { type: "emoji"; shortcode: string }
|
||||||
|
| { type: "strong"; value: string }
|
||||||
|
| { type: "em"; value: string }
|
||||||
|
| { type: "del"; value: string }
|
||||||
|
| { type: "code"; value: string }
|
||||||
|
| { type: "link"; label: string; href: string }
|
||||||
|
| { type: "image"; alt: string; src: string };
|
||||||
|
|
||||||
|
type InlineDecorationRange = {
|
||||||
|
from: number;
|
||||||
|
to: number;
|
||||||
|
className: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type InlineTokenRange = {
|
||||||
|
from: number;
|
||||||
|
to: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ListItem = {
|
||||||
|
text: string;
|
||||||
|
checked: boolean | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type TableBlock = {
|
||||||
|
type: "table";
|
||||||
|
headers: string[];
|
||||||
|
rows: string[][];
|
||||||
|
};
|
||||||
|
|
||||||
|
type MarkdownBlock =
|
||||||
|
| {
|
||||||
|
type: "paragraph";
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: "heading";
|
||||||
|
level: number;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: "hr";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: "blockquote";
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: "code";
|
||||||
|
language: string;
|
||||||
|
code: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: "list";
|
||||||
|
ordered: boolean;
|
||||||
|
items: ListItem[];
|
||||||
|
}
|
||||||
|
| TableBlock;
|
||||||
|
|
||||||
|
const INLINE_TOKEN_REGEX =
|
||||||
|
/!\[([^\]]*)\]\(([^)\s]+(?:\s+"[^"]*")?)\)|\[([^\]]+)\]\(([^)\s]+(?:\s+"[^"]*")?)\)|`([^`\n]+)`|~~([^~\n]+)~~|\*\*([^*\n]+)\*\*|__([^_\n]+)__|\*([^*\n]+)\*|(?<![a-zA-Z0-9:])_([^_\n]+)_(?![a-zA-Z0-9:])/g;
|
||||||
|
|
||||||
|
function CopiedIndicator({
|
||||||
|
block,
|
||||||
|
visible,
|
||||||
|
}: {
|
||||||
|
block: boolean;
|
||||||
|
visible: boolean;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
className={`pointer-events-none inline-flex align-middle text-foreground transition-opacity duration-200 ease-out ${block ? "mt-3 shrink-0" : "ml-1"} ${visible ? "opacity-100" : "opacity-0"}`}
|
||||||
|
aria-live="polite"
|
||||||
|
aria-hidden={!visible}
|
||||||
|
>
|
||||||
|
<Check className="size-3.5" />
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function CopyableCode({
|
||||||
|
block = false,
|
||||||
|
language,
|
||||||
|
value,
|
||||||
|
}: {
|
||||||
|
block?: boolean;
|
||||||
|
language?: string;
|
||||||
|
value: string;
|
||||||
|
}) {
|
||||||
|
const [copied, setCopied] = useState(false);
|
||||||
|
const copiedTimer = useRef<ReturnType<typeof setTimeout> | undefined>(
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
clearTimeout(copiedTimer.current);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
async function copy() {
|
||||||
|
await navigator.clipboard.writeText(value);
|
||||||
|
setCopied(true);
|
||||||
|
clearTimeout(copiedTimer.current);
|
||||||
|
copiedTimer.current = setTimeout(() => setCopied(false), 1200);
|
||||||
|
}
|
||||||
|
|
||||||
|
const code = (
|
||||||
|
<code
|
||||||
|
className={block ? "" : "bg-card border p-0.5 rounded text-sm"}
|
||||||
|
data-language={language}
|
||||||
|
role="button"
|
||||||
|
tabIndex={0}
|
||||||
|
onClick={() => void copy()}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key !== "Enter" && event.key !== " ") return;
|
||||||
|
event.preventDefault();
|
||||||
|
void copy();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{value}
|
||||||
|
</code>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (block) {
|
||||||
|
return (
|
||||||
|
<div className="flex min-w-0 items-start gap-1">
|
||||||
|
<pre className="tm-md-pre min-w-0 flex-1">{code}</pre>
|
||||||
|
<CopiedIndicator block visible={copied} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{code}
|
||||||
|
<CopiedIndicator block={false} visible={copied} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes parseInlineNodes.
|
||||||
|
* @param input Parameter input.
|
||||||
|
* @returns InlineNode[].
|
||||||
|
*/
|
||||||
|
function parseInlineNodes(input: string): InlineNode[] {
|
||||||
|
const nodes: InlineNode[] = [];
|
||||||
|
|
||||||
|
let cursor = 0;
|
||||||
|
let match = INLINE_TOKEN_REGEX.exec(input);
|
||||||
|
|
||||||
|
while (match) {
|
||||||
|
const index = match.index;
|
||||||
|
const raw = match[0];
|
||||||
|
|
||||||
|
if (index > cursor) {
|
||||||
|
nodes.push(...parseEmojiText(input.slice(cursor, index)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match[1] !== undefined && match[2] !== undefined) {
|
||||||
|
nodes.push({ type: "image", alt: match[1], src: normalizeUrl(match[2]) });
|
||||||
|
} else if (match[3] !== undefined && match[4] !== undefined) {
|
||||||
|
nodes.push({
|
||||||
|
type: "link",
|
||||||
|
label: match[3],
|
||||||
|
href: normalizeUrl(match[4]),
|
||||||
|
});
|
||||||
|
} else if (match[5] !== undefined) {
|
||||||
|
nodes.push({ type: "code", value: match[5] });
|
||||||
|
} else if (match[6] !== undefined) {
|
||||||
|
nodes.push({ type: "del", value: match[6] });
|
||||||
|
} else if (match[7] !== undefined || match[8] !== undefined) {
|
||||||
|
nodes.push({ type: "strong", value: match[7] ?? match[8] ?? "" });
|
||||||
|
} else if (match[9] !== undefined || match[10] !== undefined) {
|
||||||
|
nodes.push({ type: "em", value: match[9] ?? match[10] ?? "" });
|
||||||
|
} else {
|
||||||
|
nodes.push(...parseEmojiText(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor = index + raw.length;
|
||||||
|
match = INLINE_TOKEN_REGEX.exec(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cursor < input.length) {
|
||||||
|
nodes.push(...parseEmojiText(input.slice(cursor)));
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE_TOKEN_REGEX.lastIndex = 0;
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseEmojiText(input: string): InlineNode[] {
|
||||||
|
const nodes: InlineNode[] = [];
|
||||||
|
let cursor = 0;
|
||||||
|
|
||||||
|
for (const match of findEmojiShortcodes(input)) {
|
||||||
|
if (match.from > cursor) {
|
||||||
|
nodes.push({ type: "text", value: input.slice(cursor, match.from) });
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes.push({ type: "emoji", shortcode: match.emoji.shortcode });
|
||||||
|
cursor = match.to;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cursor < input.length) {
|
||||||
|
nodes.push({ type: "text", value: input.slice(cursor) });
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes collectInlineRanges.
|
||||||
|
* @param input Parameter input.
|
||||||
|
* @param offset Parameter offset.
|
||||||
|
* @returns {
|
||||||
|
styleRanges: InlineDecorationRange[];
|
||||||
|
tokenRanges: InlineTokenRange[];
|
||||||
|
}.
|
||||||
|
*/
|
||||||
|
export function collectInlineRanges(
|
||||||
|
input: string,
|
||||||
|
offset = 0,
|
||||||
|
): {
|
||||||
|
styleRanges: InlineDecorationRange[];
|
||||||
|
tokenRanges: InlineTokenRange[];
|
||||||
|
} {
|
||||||
|
const styleRanges: InlineDecorationRange[] = [];
|
||||||
|
const tokenRanges: InlineTokenRange[] = [];
|
||||||
|
|
||||||
|
let match = INLINE_TOKEN_REGEX.exec(input);
|
||||||
|
|
||||||
|
while (match) {
|
||||||
|
const raw = match[0];
|
||||||
|
const start = offset + match.index;
|
||||||
|
const end = start + raw.length;
|
||||||
|
|
||||||
|
if (match[1] !== undefined && match[2] !== undefined) {
|
||||||
|
const openLength = 2;
|
||||||
|
const closeLength = raw.endsWith(")") ? 1 : 0;
|
||||||
|
|
||||||
|
const imageEnd = start + openLength + match[1].length;
|
||||||
|
tokenRanges.push({ from: start, to: start + openLength });
|
||||||
|
tokenRanges.push({ from: imageEnd, to: imageEnd + 1 });
|
||||||
|
|
||||||
|
const srcStart = imageEnd + 1;
|
||||||
|
const srcEnd = end - closeLength;
|
||||||
|
tokenRanges.push({ from: srcStart, to: srcStart + 1 });
|
||||||
|
tokenRanges.push({ from: srcEnd, to: srcEnd + closeLength });
|
||||||
|
} else if (match[3] !== undefined && match[4] !== undefined) {
|
||||||
|
const label = match[3];
|
||||||
|
const labelStart = start + 1;
|
||||||
|
const labelEnd = labelStart + label.length;
|
||||||
|
|
||||||
|
tokenRanges.push({ from: start, to: start + 1 });
|
||||||
|
tokenRanges.push({ from: labelEnd, to: labelEnd + 1 });
|
||||||
|
tokenRanges.push({ from: labelEnd + 1, to: labelEnd + 2 });
|
||||||
|
tokenRanges.push({ from: end - 1, to: end });
|
||||||
|
|
||||||
|
styleRanges.push({
|
||||||
|
from: labelStart,
|
||||||
|
to: labelEnd,
|
||||||
|
className: "tm-md-link",
|
||||||
|
});
|
||||||
|
} else if (match[5] !== undefined) {
|
||||||
|
const codeStart = start + 1;
|
||||||
|
const codeEnd = end - 1;
|
||||||
|
|
||||||
|
tokenRanges.push({ from: start, to: start + 1 });
|
||||||
|
tokenRanges.push({ from: end - 1, to: end });
|
||||||
|
styleRanges.push({
|
||||||
|
from: codeStart,
|
||||||
|
to: codeEnd,
|
||||||
|
className: "tm-md-code",
|
||||||
|
});
|
||||||
|
} else if (match[6] !== undefined) {
|
||||||
|
const contentStart = start + 2;
|
||||||
|
const contentEnd = end - 2;
|
||||||
|
|
||||||
|
tokenRanges.push({ from: start, to: start + 2 });
|
||||||
|
tokenRanges.push({ from: end - 2, to: end });
|
||||||
|
styleRanges.push({
|
||||||
|
from: contentStart,
|
||||||
|
to: contentEnd,
|
||||||
|
className: "tm-md-del",
|
||||||
|
});
|
||||||
|
} else if (match[7] !== undefined || match[8] !== undefined) {
|
||||||
|
const contentStart = start + 2;
|
||||||
|
const contentEnd = end - 2;
|
||||||
|
|
||||||
|
tokenRanges.push({ from: start, to: start + 2 });
|
||||||
|
tokenRanges.push({ from: end - 2, to: end });
|
||||||
|
styleRanges.push({
|
||||||
|
from: contentStart,
|
||||||
|
to: contentEnd,
|
||||||
|
className: "tm-md-strong",
|
||||||
|
});
|
||||||
|
} else if (match[9] !== undefined || match[10] !== undefined) {
|
||||||
|
const contentStart = start + 1;
|
||||||
|
const contentEnd = end - 1;
|
||||||
|
|
||||||
|
tokenRanges.push({ from: start, to: start + 1 });
|
||||||
|
tokenRanges.push({ from: end - 1, to: end });
|
||||||
|
styleRanges.push({
|
||||||
|
from: contentStart,
|
||||||
|
to: contentEnd,
|
||||||
|
className: "tm-md-em",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
match = INLINE_TOKEN_REGEX.exec(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE_TOKEN_REGEX.lastIndex = 0;
|
||||||
|
return { styleRanges, tokenRanges };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes parseMarkdownBlocks.
|
||||||
|
* @param markdown Parameter markdown.
|
||||||
|
* @returns MarkdownBlock[].
|
||||||
|
*/
|
||||||
|
export function parseMarkdownBlocks(markdown: string): MarkdownBlock[] {
|
||||||
|
const lines = markdown.replace(/\r\n/g, "\n").split("\n");
|
||||||
|
const blocks: MarkdownBlock[] = [];
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
while (index < lines.length) {
|
||||||
|
const line = lines[index];
|
||||||
|
|
||||||
|
if (!line.trim()) {
|
||||||
|
index += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const codeFence = line.match(/^```\s*([^`]*)$/);
|
||||||
|
if (codeFence) {
|
||||||
|
const language = (codeFence[1] ?? "").trim();
|
||||||
|
const codeLines: string[] = [];
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
while (index < lines.length && !/^```\s*$/.test(lines[index])) {
|
||||||
|
codeLines.push(lines[index]);
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index < lines.length) {
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks.push({ type: "code", language, code: codeLines.join("\n") });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/.test(line.trim())) {
|
||||||
|
blocks.push({ type: "hr" });
|
||||||
|
index += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const heading = line.match(/^(#{1,6})\s+(.+)$/);
|
||||||
|
if (heading) {
|
||||||
|
blocks.push({
|
||||||
|
type: "heading",
|
||||||
|
level: heading[1].length,
|
||||||
|
text: heading[2],
|
||||||
|
});
|
||||||
|
index += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const quote = line.match(/^>\s?(.*)$/);
|
||||||
|
if (quote) {
|
||||||
|
const quoteLines: string[] = [quote[1]];
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
while (index < lines.length) {
|
||||||
|
const next = lines[index].match(/^>\s?(.*)$/);
|
||||||
|
if (!next) break;
|
||||||
|
quoteLines.push(next[1]);
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks.push({ type: "blockquote", text: quoteLines.join("\n") });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableCandidate = readTable(lines, index);
|
||||||
|
if (tableCandidate) {
|
||||||
|
blocks.push(tableCandidate.block);
|
||||||
|
index = tableCandidate.nextIndex;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const unordered = line.match(/^\s*[-*+]\s+(.*)$/);
|
||||||
|
const ordered = line.match(/^\s*\d+\.\s+(.*)$/);
|
||||||
|
if (unordered || ordered) {
|
||||||
|
const orderedList = Boolean(ordered);
|
||||||
|
const items: ListItem[] = [];
|
||||||
|
|
||||||
|
while (index < lines.length) {
|
||||||
|
const current = lines[index];
|
||||||
|
const match = orderedList
|
||||||
|
? current.match(/^\s*\d+\.\s+(.*)$/)
|
||||||
|
: current.match(/^\s*[-*+]\s+(.*)$/);
|
||||||
|
|
||||||
|
if (!match) break;
|
||||||
|
|
||||||
|
const task = match[1].match(/^\[( |x|X)\]\s+(.*)$/);
|
||||||
|
if (task) {
|
||||||
|
items.push({
|
||||||
|
text: task[2],
|
||||||
|
checked: task[1].toLowerCase() === "x",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
items.push({ text: match[1], checked: null });
|
||||||
|
}
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks.push({ type: "list", ordered: orderedList, items });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const paragraphLines = [line];
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
while (
|
||||||
|
index < lines.length &&
|
||||||
|
lines[index].trim() &&
|
||||||
|
!/^(#{1,6})\s+/.test(lines[index]) &&
|
||||||
|
!/^```\s*/.test(lines[index]) &&
|
||||||
|
!/^>\s?/.test(lines[index]) &&
|
||||||
|
!/^\s*[-*+]\s+/.test(lines[index]) &&
|
||||||
|
!/^\s*\d+\.\s+/.test(lines[index]) &&
|
||||||
|
!/^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/.test(lines[index].trim())
|
||||||
|
) {
|
||||||
|
paragraphLines.push(lines[index]);
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks.push({ type: "paragraph", text: paragraphLines.join("\n") });
|
||||||
|
}
|
||||||
|
|
||||||
|
return blocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes renderInline.
|
||||||
|
* @param nodes Parameter nodes.
|
||||||
|
* @returns React.ReactNode[].
|
||||||
|
*/
|
||||||
|
function renderInline(nodes: InlineNode[]): ReactNode[] {
|
||||||
|
return nodes.map((node, index) => {
|
||||||
|
if (node.type === "text") {
|
||||||
|
return node.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "emoji") {
|
||||||
|
return (
|
||||||
|
<Emoji key={index} className="tm-md-emoji" shortcode={node.shortcode} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "strong") {
|
||||||
|
return (
|
||||||
|
<strong key={index} className="tm-md-strong">
|
||||||
|
{renderInline(parseEmojiText(node.value))}
|
||||||
|
</strong>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "em") {
|
||||||
|
return (
|
||||||
|
<em key={index} className="tm-md-em">
|
||||||
|
{renderInline(parseEmojiText(node.value))}
|
||||||
|
</em>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "del") {
|
||||||
|
return (
|
||||||
|
<del key={index} className="tm-md-del">
|
||||||
|
{renderInline(parseEmojiText(node.value))}
|
||||||
|
</del>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "code") {
|
||||||
|
return <CopyableCode key={index} value={node.value} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === "link") {
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
key={index}
|
||||||
|
className="tm-md-link"
|
||||||
|
href={node.href}
|
||||||
|
target="_blank"
|
||||||
|
rel="noreferrer"
|
||||||
|
>
|
||||||
|
{renderInline(parseEmojiText(node.label))}
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<img
|
||||||
|
key={index}
|
||||||
|
className="tm-md-image"
|
||||||
|
src={node.src}
|
||||||
|
alt={node.alt}
|
||||||
|
loading="lazy"
|
||||||
|
decoding="async"
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes renderBlocks.
|
||||||
|
* @param blocks Parameter blocks.
|
||||||
|
* @returns React.ReactElement.
|
||||||
|
*/
|
||||||
|
export function renderBlocks(blocks: MarkdownBlock[]): ReactElement {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{blocks.map((block, blockIndex) => {
|
||||||
|
if (block.type === "heading") {
|
||||||
|
const className = `tm-md-heading tm-md-h${String(block.level)}`;
|
||||||
|
if (block.level === 1)
|
||||||
|
return (
|
||||||
|
<h1 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h1>
|
||||||
|
);
|
||||||
|
if (block.level === 2)
|
||||||
|
return (
|
||||||
|
<h2 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h2>
|
||||||
|
);
|
||||||
|
if (block.level === 3)
|
||||||
|
return (
|
||||||
|
<h3 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h3>
|
||||||
|
);
|
||||||
|
if (block.level === 4)
|
||||||
|
return (
|
||||||
|
<h4 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h4>
|
||||||
|
);
|
||||||
|
if (block.level === 5)
|
||||||
|
return (
|
||||||
|
<h5 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
return (
|
||||||
|
<h6 key={blockIndex} className={className}>
|
||||||
|
{renderInline(parseInlineNodes(block.text))}
|
||||||
|
</h6>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.type === "blockquote") {
|
||||||
|
return (
|
||||||
|
<blockquote key={blockIndex} className="tm-md-blockquote">
|
||||||
|
{block.text.split("\n").map((line, lineIndex) => (
|
||||||
|
<p key={lineIndex}>{renderInline(parseInlineNodes(line))}</p>
|
||||||
|
))}
|
||||||
|
</blockquote>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.type === "code") {
|
||||||
|
return (
|
||||||
|
<CopyableCode
|
||||||
|
key={blockIndex}
|
||||||
|
block
|
||||||
|
language={block.language}
|
||||||
|
value={block.code}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.type === "list") {
|
||||||
|
const Tag = block.ordered ? "ol" : "ul";
|
||||||
|
return (
|
||||||
|
<Tag
|
||||||
|
key={blockIndex}
|
||||||
|
className={block.ordered ? "tm-md-ol" : "tm-md-ul"}
|
||||||
|
>
|
||||||
|
{block.items.map((item, itemIndex) => (
|
||||||
|
<li key={itemIndex} className="tm-md-li">
|
||||||
|
{item.checked !== null ? (
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={item.checked}
|
||||||
|
disabled
|
||||||
|
className="tm-md-checkbox"
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
<span>{renderInline(parseInlineNodes(item.text))}</span>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</Tag>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.type === "table") {
|
||||||
|
return (
|
||||||
|
<div key={blockIndex} className="tm-md-table-wrap">
|
||||||
|
<table className="tm-md-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{block.headers.map((header, headerIndex) => (
|
||||||
|
<th key={headerIndex}>
|
||||||
|
{renderInline(parseInlineNodes(header))}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{block.rows.map((row, rowIndex) => (
|
||||||
|
<tr key={rowIndex}>
|
||||||
|
{row.map((cell, cellIndex) => (
|
||||||
|
<td key={cellIndex}>
|
||||||
|
{renderInline(parseInlineNodes(cell))}
|
||||||
|
</td>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.type === "hr") {
|
||||||
|
return <hr key={blockIndex} className="tm-md-hr" />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<p key={blockIndex}>
|
||||||
|
{block.text.split("\n").map((line, lineIndex) => (
|
||||||
|
<Fragment key={lineIndex}>
|
||||||
|
{lineIndex > 0 ? <br /> : null}
|
||||||
|
{renderInline(parseInlineNodes(line))}
|
||||||
|
</Fragment>
|
||||||
|
))}
|
||||||
|
</p>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes normalizeUrl.
|
||||||
|
* @param input Parameter input.
|
||||||
|
* @returns string.
|
||||||
|
*/
|
||||||
|
function normalizeUrl(input: string): string {
|
||||||
|
const value = input.trim();
|
||||||
|
if (/^(https?:|mailto:|tel:|\/)/i.test(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "#";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes splitTableRow.
|
||||||
|
* @param row Parameter row.
|
||||||
|
* @returns string[].
|
||||||
|
*/
|
||||||
|
function splitTableRow(row: string): string[] {
|
||||||
|
const cleaned = row.trim().replace(/^\|/, "").replace(/\|$/, "");
|
||||||
|
return cleaned.split("|").map((cell) => cell.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes readTable.
|
||||||
|
* @param lines Parameter lines.
|
||||||
|
* @param index Parameter index.
|
||||||
|
* @returns { block: TableBlock; nextIndex: number } | null.
|
||||||
|
*/
|
||||||
|
function readTable(
|
||||||
|
lines: string[],
|
||||||
|
index: number,
|
||||||
|
): { block: TableBlock; nextIndex: number } | null {
|
||||||
|
const header = lines[index] ?? "";
|
||||||
|
const separator = lines[index + 1] ?? "";
|
||||||
|
|
||||||
|
if (!header.includes("|") || !separator.includes("|")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const separatorCells = splitTableRow(separator);
|
||||||
|
const isSeparator = separatorCells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
||||||
|
if (!isSeparator) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const headers = splitTableRow(header);
|
||||||
|
const rows: string[][] = [];
|
||||||
|
let cursor = index + 2;
|
||||||
|
|
||||||
|
while (cursor < lines.length && lines[cursor].includes("|")) {
|
||||||
|
rows.push(splitTableRow(lines[cursor]));
|
||||||
|
cursor += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
block: { type: "table", headers, rows },
|
||||||
|
nextIndex: cursor,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const markdownStyles = `
|
||||||
|
.tm-md-root { color: var(--foreground); line-height: 1.65; font-size: 1rem; }
|
||||||
|
.tm-md-heading { margin: 0.2rem 0 0.35rem; font-weight: 700; line-height: 1.25; }
|
||||||
|
.tm-md-h1 { font-size: 1.65rem; }
|
||||||
|
.tm-md-h2 { font-size: 1.45rem; }
|
||||||
|
.tm-md-h3 { font-size: 1.25rem; }
|
||||||
|
.tm-md-h4 { font-size: 1.1rem; }
|
||||||
|
.tm-md-h5 { font-size: 1rem; }
|
||||||
|
.tm-md-h6 { font-size: 0.95rem; opacity: 0.9; }
|
||||||
|
.tm-md-blockquote { margin: 0.45rem 0; padding-left: 0.75rem; opacity: 0.95; }
|
||||||
|
.tm-md-blockquote p { margin: 0.2rem 0; }
|
||||||
|
.tm-md-pre { margin: 0.45rem 0; padding: 0.65rem 0.75rem; border-radius: 0.5rem; background: var(--muted); overflow-x: auto; }
|
||||||
|
.tm-md-code, .tm-md-codeblock { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 0.87em; cursor: pointer; }
|
||||||
|
.tm-md-code { padding: 0.08rem 0.32rem; border: 1px solid var(--border); border-radius: 0.28rem; background: var(--muted); }
|
||||||
|
.tm-md-codeblock { display: block; }
|
||||||
|
.tm-md-code:focus-visible, .tm-md-codeblock:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
|
||||||
|
.tm-md-strong { font-weight: 700; }
|
||||||
|
.tm-md-em { font-style: italic; }
|
||||||
|
.tm-md-del { text-decoration: line-through; }
|
||||||
|
.tm-md-link { color: var(--primary); text-decoration: underline; text-underline-offset: 0.14rem; }
|
||||||
|
.tm-md-image { display: block; max-width: 100%; border-radius: 0.4rem; margin: 0.5rem 0; }
|
||||||
|
.tm-md-emoji { display: inline-block; width: 1.15em; height: 1.15em; vertical-align: -0.18em; }
|
||||||
|
.tm-md-ul, .tm-md-ol { margin: 0.3rem 0 0.35rem 1.2rem; padding: 0; }
|
||||||
|
.tm-md-li { margin: 0.2rem 0; }
|
||||||
|
.tm-md-checkbox { margin-right: 0.5rem; vertical-align: middle; }
|
||||||
|
.tm-md-table-wrap { overflow-x: auto; margin: 0.45rem 0; }
|
||||||
|
.tm-md-table { border-collapse: collapse; width: 100%; min-width: 16rem; }
|
||||||
|
.tm-md-table th, .tm-md-table td { padding: 0.4rem 0.5rem; text-align: left; }
|
||||||
|
.tm-md-table th { background: var(--muted); font-weight: 600; }
|
||||||
|
.tm-md-hr { margin: 0.55rem 0; }
|
||||||
|
|
||||||
|
.cm-editor.tm-md-editor { border-radius: inherit; background: transparent; caret-color: var(--foreground); }
|
||||||
|
.cm-editor.tm-md-editor.cm-focused { outline: none; box-shadow: none; }
|
||||||
|
.cm-editor.tm-md-editor .cm-scroller { font-family: inherit; line-height: 1.55; max-height: 30vh; overflow-y: auto; overflow-x: hidden; }
|
||||||
|
.cm-editor.tm-md-editor .cm-content { caret-color: var(--foreground); }
|
||||||
|
.cm-editor.tm-md-editor .cm-content { padding: var(--tm-md-content-padding, 0.25rem 0.625rem); min-height: 2rem; }
|
||||||
|
.cm-editor.tm-md-editor .cm-line { padding: 0; color: var(--foreground); }
|
||||||
|
.cm-editor.tm-md-editor .tm-md-editor-emoji { display: inline-block; width: 1.15em; height: 1.15em; vertical-align: -0.18em; object-fit: contain; pointer-events: none; }
|
||||||
|
.cm-editor.tm-md-editor .tm-md-hidden-token { color: transparent; opacity: 0; font-size: inherit; }
|
||||||
|
.cm-editor.tm-md-editor .tm-md-code-line { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; background: var(--muted); border-radius: 0.3rem; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete { min-width: 18rem; max-width: min(26rem, calc(100vw - 1rem)); overflow: hidden; border: 1px solid var(--border); border-radius: var(--radius); background: var(--popover); color: var(--popover-foreground); box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1); font-family: "Public Sans Variable", sans-serif; font-size: 0.875rem; }
|
||||||
|
.cm-editor.tm-md-editor .cm-tooltip.cm-tooltip-autocomplete > ul { max-height: min(20rem, 45vh); padding: 0.25rem; font-family: "Public Sans Variable", sans-serif; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul::-webkit-scrollbar { width: 6px; height: 6px; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul::-webkit-scrollbar-track { background: transparent; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul::-webkit-scrollbar-thumb { border-radius: 9999px; background: var(--border); }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul > li { display: flex; min-height: 2.25rem; align-items: center; border-radius: calc(var(--radius) * 0.8); padding: 0.3rem 0.5rem; color: var(--popover-foreground); }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul > li:hover,
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete > ul > li[aria-selected] { background: var(--accent); color: var(--accent-foreground); }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete .cm-completionIcon { display: none; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete .cm-completionLabel { overflow: hidden; text-overflow: ellipsis; }
|
||||||
|
.cm-tooltip.cm-tooltip-autocomplete .cm-completionMatchedText { color: inherit; text-decoration: none; font-weight: 600; }
|
||||||
|
.cm-tooltip-autocomplete .tm-md-completion-emoji { display: inline-block; width: 1.35rem; height: 1.35rem; flex: 0 0 auto; margin-right: 0.5rem; vertical-align: middle; }
|
||||||
|
`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes ensureMarkdownStyles.
|
||||||
|
* @param none This function has no parameters.
|
||||||
|
* @returns void.
|
||||||
|
*/
|
||||||
|
export function ensureMarkdownStyles(): void {
|
||||||
|
if (typeof document === "undefined") return;
|
||||||
|
|
||||||
|
const styleId = "tensamin-markdown-styles";
|
||||||
|
let style = document.getElementById(styleId) as HTMLStyleElement | null;
|
||||||
|
|
||||||
|
if (!style) {
|
||||||
|
style = document.createElement("style");
|
||||||
|
style.id = styleId;
|
||||||
|
document.head.appendChild(style);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (style.textContent !== markdownStyles) {
|
||||||
|
style.textContent = markdownStyles;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
packages/markdown/src/text.tsx
Normal file
30
packages/markdown/src/text.tsx
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { useMemo, type CSSProperties } from "react";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ensureMarkdownStyles,
|
||||||
|
parseMarkdownBlocks,
|
||||||
|
renderBlocks,
|
||||||
|
} from "./markdown";
|
||||||
|
|
||||||
|
export type TextProps = {
|
||||||
|
value: string;
|
||||||
|
fontSize?: CSSProperties["fontSize"];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes Text.
|
||||||
|
* @param props Parameter props.
|
||||||
|
* @returns unknown.
|
||||||
|
*/
|
||||||
|
export default function Text(props: TextProps) {
|
||||||
|
ensureMarkdownStyles();
|
||||||
|
|
||||||
|
const blocks = useMemo(() => parseMarkdownBlocks(props.value), [props.value]);
|
||||||
|
const renderedBlocks = useMemo(() => renderBlocks(blocks), [blocks]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="tm-md-root" style={{ fontSize: props.fontSize }}>
|
||||||
|
{renderedBlocks}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
1
packages/markdown/todo.md
Normal file
1
packages/markdown/todo.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
- Improve the Input box
|
||||||
12
packages/markdown/tsconfig.json
Normal file
12
packages/markdown/tsconfig.json
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noEmit": true
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
|
|
@ -9,18 +9,16 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"format": "pnpm exec prettier --write .",
|
"format": "pnpm exec prettier --write .",
|
||||||
"lint": "eslint src --ext .ts,.tsx",
|
"lint": "eslint src --ext .ts,.tsx",
|
||||||
"build": "tsc -p tsconfig.json --noEmit"
|
"test": "vitest run --passWithNoTests",
|
||||||
|
"build": "pnpm run test && tsc -p tsconfig.json --noEmit"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@methanium/ui": "*",
|
"@methanium/ui": "*",
|
||||||
"@tauri-apps/api": "^2.11.1",
|
"@tauri-apps/api": "^2.11.1",
|
||||||
"@tensamin/crypto": "workspace:*",
|
|
||||||
"@tensamin/shared": "workspace:*",
|
"@tensamin/shared": "workspace:*",
|
||||||
"@tensamin/storage": "workspace:*",
|
"@tensamin/storage": "workspace:*",
|
||||||
"mtp": "*",
|
"mtp": "*",
|
||||||
"react": "^19.2.8",
|
"react": "^19.2.8"
|
||||||
"react-dom": "^19.2.8",
|
|
||||||
"zod": "^4.4.3"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^10.8.0"
|
"eslint": "^10.8.0"
|
||||||
|
|
|
||||||
529
packages/mtp/src/browser.tsx
Normal file
529
packages/mtp/src/browser.tsx
Normal file
|
|
@ -0,0 +1,529 @@
|
||||||
|
import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
|
||||||
|
import { toast as sonnerToast } from "@methanium/ui";
|
||||||
|
import { base64ToBytes, ConnectionState, MTPClient } from "mtp";
|
||||||
|
import createAsyncQueue from "@tensamin/shared/asyncQueue";
|
||||||
|
import {
|
||||||
|
mtp as mtpSchemas,
|
||||||
|
type Calls,
|
||||||
|
type Communities,
|
||||||
|
type Contacts,
|
||||||
|
} from "@tensamin/shared/data";
|
||||||
|
import { log } from "@tensamin/shared/log";
|
||||||
|
import { useStorage } from "@tensamin/storage/context";
|
||||||
|
|
||||||
|
import {
|
||||||
|
type BoundSendFn,
|
||||||
|
MTPContext,
|
||||||
|
type MTPContextType,
|
||||||
|
type ProtocolMessage,
|
||||||
|
removeMissingContacts,
|
||||||
|
useMessageHandlers,
|
||||||
|
} from "./mtpContext";
|
||||||
|
import {
|
||||||
|
DISCOVERY_TIMEOUT,
|
||||||
|
INITIAL_SYNC_TIMEOUT,
|
||||||
|
RECONNECT_JITTER,
|
||||||
|
RECONNECT_LONG_INTERVAL,
|
||||||
|
RECONNECT_RESET,
|
||||||
|
RECONNECT_TRIES,
|
||||||
|
RETRY_INTERVAL,
|
||||||
|
STATE_ACK_TIMEOUT,
|
||||||
|
} from "./values";
|
||||||
|
|
||||||
|
type BrowserMtpClient = Awaited<ReturnType<typeof createBrowserClient>>;
|
||||||
|
|
||||||
|
function createBrowserClient(
|
||||||
|
options: Omit<Parameters<typeof MTPClient.create>[0], "schemas">,
|
||||||
|
) {
|
||||||
|
return MTPClient.create({
|
||||||
|
...options,
|
||||||
|
schemas: mtpSchemas,
|
||||||
|
throwProtocolErrors: true,
|
||||||
|
onValidationError: (error) => {
|
||||||
|
log(1, "mtp", "red", "Failed to validate push message", error);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function abortError(signal: AbortSignal): Error {
|
||||||
|
return signal.reason instanceof Error
|
||||||
|
? signal.reason
|
||||||
|
: new Error("Initial state synchronization was cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
function withDeadline<T>(
|
||||||
|
promise: Promise<T>,
|
||||||
|
timeoutMs: number,
|
||||||
|
timeoutMessage: string,
|
||||||
|
signal: AbortSignal,
|
||||||
|
): Promise<T> {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
if (signal.aborted) {
|
||||||
|
reject(abortError(signal));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let settled = false;
|
||||||
|
const finish = (complete: () => void) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
clearTimeout(timeout);
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
complete();
|
||||||
|
};
|
||||||
|
const timeout = setTimeout(
|
||||||
|
() => finish(() => reject(new Error(timeoutMessage))),
|
||||||
|
timeoutMs,
|
||||||
|
);
|
||||||
|
const onAbort = () => finish(() => reject(abortError(signal)));
|
||||||
|
signal.addEventListener("abort", onAbort, { once: true });
|
||||||
|
promise.then(
|
||||||
|
(value) => finish(() => resolve(value)),
|
||||||
|
(error: unknown) => finish(() => reject(error)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function completeInitialSynchronization(
|
||||||
|
client: BrowserMtpClient,
|
||||||
|
subscribe: MTPContextType["subscribe"],
|
||||||
|
signal: AbortSignal,
|
||||||
|
syncTimeoutMs = INITIAL_SYNC_TIMEOUT,
|
||||||
|
ackTimeoutMs = STATE_ACK_TIMEOUT,
|
||||||
|
): Promise<ProtocolMessage<"ClientStateSync">> {
|
||||||
|
const stateSync = new Promise<ProtocolMessage<"ClientStateSync">>(
|
||||||
|
(resolve, reject) => {
|
||||||
|
let unsubscribeStateSync = () => {};
|
||||||
|
let unsubscribeNoIota = () => {};
|
||||||
|
const cleanup = () => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
unsubscribeStateSync();
|
||||||
|
unsubscribeNoIota();
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
};
|
||||||
|
const onAbort = () => {
|
||||||
|
cleanup();
|
||||||
|
reject(abortError(signal));
|
||||||
|
};
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
cleanup();
|
||||||
|
reject(new Error("Initial state synchronization timed out"));
|
||||||
|
}, syncTimeoutMs);
|
||||||
|
signal.addEventListener("abort", onAbort, { once: true });
|
||||||
|
if (signal.aborted) {
|
||||||
|
onAbort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsubscribeStateSync = subscribe("ClientStateSync", (message) => {
|
||||||
|
cleanup();
|
||||||
|
resolve(message);
|
||||||
|
});
|
||||||
|
unsubscribeNoIota = subscribe("ErrorNoIota", () => {
|
||||||
|
cleanup();
|
||||||
|
reject(new Error("No Iota is currently connected"));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const [, state] = await Promise.all([
|
||||||
|
withDeadline(
|
||||||
|
client.auth(),
|
||||||
|
syncTimeoutMs,
|
||||||
|
"MTP authentication timed out",
|
||||||
|
signal,
|
||||||
|
),
|
||||||
|
stateSync,
|
||||||
|
]);
|
||||||
|
await withDeadline(
|
||||||
|
client.request("ClientStateAck", {
|
||||||
|
SessionId: state.data.SessionId,
|
||||||
|
VersionNumber: state.data.VersionNumber,
|
||||||
|
}),
|
||||||
|
ackTimeoutMs,
|
||||||
|
"State acknowledgement timed out",
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
if (signal.aborted) throw abortError(signal);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
function protocolErrorDetails(error: unknown) {
|
||||||
|
if (typeof error !== "object" || error === null || !("type" in error)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const protocolError = error as {
|
||||||
|
id?: unknown;
|
||||||
|
type?: unknown;
|
||||||
|
frame?: unknown;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
id: protocolError.id,
|
||||||
|
type: protocolError.type,
|
||||||
|
frame: protocolError.frame,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function BrowserProvider(props: {
|
||||||
|
children: ReactNode;
|
||||||
|
blockConnection?: boolean;
|
||||||
|
}) {
|
||||||
|
const { load } = useStorage();
|
||||||
|
const [readyState, setReadyState] = useState<number>(
|
||||||
|
ConnectionState.Disconnected,
|
||||||
|
);
|
||||||
|
const [identified, setIdentified] = useState(false);
|
||||||
|
const [identifying, setIdentifying] = useState(false);
|
||||||
|
const [freshCommunities, setFreshCommunities] = useState<Communities>([]);
|
||||||
|
const [freshContacts, setFreshContacts] = useState<Contacts>([]);
|
||||||
|
const [freshCalls, setFreshCalls] = useState<Calls>([]);
|
||||||
|
const clientRef = useRef<BrowserMtpClient | null>(null);
|
||||||
|
const { addInterceptor, attachSubscriptions, interceptorsRef, subscribe } =
|
||||||
|
useMessageHandlers();
|
||||||
|
const connected = readyState === ConnectionState.Connected;
|
||||||
|
|
||||||
|
const [mtpUrl, setMtpUrl] = useState<string | null>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
load("omega_url").then(setMtpUrl);
|
||||||
|
}, [load]);
|
||||||
|
|
||||||
|
const send: BoundSendFn = useMemo(
|
||||||
|
() => async (type, data, options) => {
|
||||||
|
const client = clientRef.current;
|
||||||
|
if (!client) throw new Error("mtp is not connected");
|
||||||
|
const response = await client.request(type, data, options);
|
||||||
|
if (response.type === "GetStates") {
|
||||||
|
setFreshContacts((contacts) =>
|
||||||
|
removeMissingContacts(
|
||||||
|
contacts,
|
||||||
|
response as ProtocolMessage<"GetStates">,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const resolveConnectionRef = useRef(() => {});
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mtpUrl) return;
|
||||||
|
let attempts = 0;
|
||||||
|
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
let reconnectResetTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
let reconnectScheduled = false;
|
||||||
|
let disposed = false;
|
||||||
|
let connectionGeneration = 0;
|
||||||
|
let cleanupConnection = () => {};
|
||||||
|
|
||||||
|
const clearReconnectTimer = () => {
|
||||||
|
if (!reconnectTimer) return;
|
||||||
|
clearTimeout(reconnectTimer);
|
||||||
|
reconnectTimer = null;
|
||||||
|
reconnectScheduled = false;
|
||||||
|
};
|
||||||
|
const clearReconnectResetTimer = () => {
|
||||||
|
if (!reconnectResetTimer) return;
|
||||||
|
clearTimeout(reconnectResetTimer);
|
||||||
|
reconnectResetTimer = null;
|
||||||
|
};
|
||||||
|
const scheduleReconnect = (error: unknown) => {
|
||||||
|
if (disposed || reconnectScheduled) return;
|
||||||
|
attempts += 1;
|
||||||
|
const shortRetry = attempts <= RECONNECT_TRIES;
|
||||||
|
if (!shortRetry) {
|
||||||
|
log(0, "mtp", "red", "Reconnection attempts exhausted", error);
|
||||||
|
sonnerToast.error("Connection failed", {
|
||||||
|
id: "mtp-connection-toast",
|
||||||
|
description:
|
||||||
|
error instanceof Error
|
||||||
|
? `${error.message.split(":")[0]}. Retrying in the background.`
|
||||||
|
: "Connection lost. Retrying in the background.",
|
||||||
|
icon: null,
|
||||||
|
duration: Infinity,
|
||||||
|
closeButton: true,
|
||||||
|
promise: null,
|
||||||
|
} as unknown as Parameters<typeof sonnerToast.error>[1]);
|
||||||
|
} else {
|
||||||
|
sonnerToast.loading(
|
||||||
|
`Reconnecting to server... (attempt ${attempts} of ${RECONNECT_TRIES})`,
|
||||||
|
{ id: "mtp-connection-toast" },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const baseDelay = shortRetry ? RETRY_INTERVAL : RECONNECT_LONG_INTERVAL;
|
||||||
|
const jitter = 1 + (Math.random() * 2 - 1) * RECONNECT_JITTER;
|
||||||
|
reconnectScheduled = true;
|
||||||
|
reconnectTimer = setTimeout(
|
||||||
|
() => {
|
||||||
|
reconnectScheduled = false;
|
||||||
|
reconnectTimer = null;
|
||||||
|
void connect();
|
||||||
|
},
|
||||||
|
Math.round(baseDelay * jitter),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function connect() {
|
||||||
|
if (disposed || props.blockConnection) return;
|
||||||
|
const generation = ++connectionGeneration;
|
||||||
|
let client: BrowserMtpClient | null = null;
|
||||||
|
let failed = false;
|
||||||
|
let connectionReady = false;
|
||||||
|
let detachSubscriptions = () => {};
|
||||||
|
let unsubscribeNoIota = () => {};
|
||||||
|
const attemptAbort = new AbortController();
|
||||||
|
const cleanup = () => {
|
||||||
|
attemptAbort.abort(
|
||||||
|
new Error("Initial state synchronization was cancelled"),
|
||||||
|
);
|
||||||
|
unsubscribeNoIota();
|
||||||
|
detachSubscriptions();
|
||||||
|
client?.disconnect();
|
||||||
|
if (clientRef.current === client) clientRef.current = null;
|
||||||
|
clearReconnectResetTimer();
|
||||||
|
if (generation === connectionGeneration) {
|
||||||
|
setReadyState(ConnectionState.Disconnected);
|
||||||
|
setIdentified(false);
|
||||||
|
setIdentifying(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cleanupConnection = cleanup;
|
||||||
|
try {
|
||||||
|
setIdentified(false);
|
||||||
|
setIdentifying(false);
|
||||||
|
const [userId, keyring] = await Promise.all([
|
||||||
|
load("user_id"),
|
||||||
|
load("mtp_keyring"),
|
||||||
|
]);
|
||||||
|
if (!userId || !keyring) throw new Error("Missing login credentials");
|
||||||
|
const forcedOmikronUrl = await load("forced_omikron_url");
|
||||||
|
const forcedOmikronPublicKey = await load("forced_omikron_public_key");
|
||||||
|
let url = null;
|
||||||
|
let omikronPublicKey = null;
|
||||||
|
if (forcedOmikronUrl && forcedOmikronPublicKey) {
|
||||||
|
url = forcedOmikronUrl;
|
||||||
|
omikronPublicKey = forcedOmikronPublicKey;
|
||||||
|
} else {
|
||||||
|
log(2, "mtp", "purple", "Fetching Omikron data.");
|
||||||
|
const data = await fetch(`${mtpUrl}api/get/omikron/${userId}`, {
|
||||||
|
signal: AbortSignal.any([
|
||||||
|
attemptAbort.signal,
|
||||||
|
AbortSignal.timeout(DISCOVERY_TIMEOUT),
|
||||||
|
]),
|
||||||
|
});
|
||||||
|
if (data.status === 404) {
|
||||||
|
throw new Error("No Omikron assignment is currently available");
|
||||||
|
}
|
||||||
|
if (!data.ok)
|
||||||
|
throw new Error(`Omikron discovery failed: HTTP ${data.status}`);
|
||||||
|
const omikronData = (await data.json()) as {
|
||||||
|
ip_address: string;
|
||||||
|
port: number;
|
||||||
|
public_key: string;
|
||||||
|
};
|
||||||
|
if (
|
||||||
|
!omikronData.ip_address ||
|
||||||
|
!omikronData.port ||
|
||||||
|
!omikronData.public_key
|
||||||
|
) {
|
||||||
|
throw new Error("Invalid Omikron data");
|
||||||
|
}
|
||||||
|
url = `https://${omikronData.ip_address}:${omikronData.port}`;
|
||||||
|
omikronPublicKey = omikronData.public_key;
|
||||||
|
}
|
||||||
|
if (!url || !omikronPublicKey) {
|
||||||
|
throw new Error("Missing Omikron URL or Public Key");
|
||||||
|
}
|
||||||
|
log(2, "mtp", "green", "Connecting to: " + url);
|
||||||
|
client = await createBrowserClient({
|
||||||
|
url,
|
||||||
|
credentials: { clientId: userId, keyring: base64ToBytes(keyring) },
|
||||||
|
hostPublicKey: { value: omikronPublicKey, encoding: "base64" },
|
||||||
|
descriptor: "client",
|
||||||
|
pings: true,
|
||||||
|
logger: (event) => {
|
||||||
|
if (event.type === "state") {
|
||||||
|
if (generation !== connectionGeneration) return;
|
||||||
|
const state = client?.state ?? ConnectionState.Disconnected;
|
||||||
|
setReadyState(state);
|
||||||
|
if (
|
||||||
|
state === ConnectionState.Disconnected &&
|
||||||
|
clientRef.current === client &&
|
||||||
|
!failed
|
||||||
|
) {
|
||||||
|
failed = true;
|
||||||
|
const error = new Error("MTP connection lost");
|
||||||
|
attemptAbort.abort(error);
|
||||||
|
if (connectionReady) {
|
||||||
|
cleanup();
|
||||||
|
scheduleReconnect(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.type !== "Pong" && event.type !== "Ping") {
|
||||||
|
log(
|
||||||
|
2,
|
||||||
|
"mtp",
|
||||||
|
event.type === "state"
|
||||||
|
? "purple"
|
||||||
|
: event.direction === "recv"
|
||||||
|
? "cyan"
|
||||||
|
: event.direction === "send"
|
||||||
|
? "gray"
|
||||||
|
: "blue",
|
||||||
|
event.type === "state"
|
||||||
|
? event.data
|
||||||
|
: event.direction === "recv"
|
||||||
|
? "< " + event.type
|
||||||
|
: event.direction === "send"
|
||||||
|
? "> " + event.type
|
||||||
|
: event.type,
|
||||||
|
event,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (disposed || generation !== connectionGeneration) {
|
||||||
|
client.disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const activeClient = client;
|
||||||
|
clientRef.current = activeClient;
|
||||||
|
detachSubscriptions = attachSubscriptions(activeClient);
|
||||||
|
unsubscribeNoIota = subscribe("ErrorNoIota", () => {
|
||||||
|
if (clientRef.current !== activeClient || failed) return;
|
||||||
|
failed = true;
|
||||||
|
const error = new Error("No Iota is currently connected");
|
||||||
|
attemptAbort.abort(error);
|
||||||
|
cleanup();
|
||||||
|
scheduleReconnect(error);
|
||||||
|
});
|
||||||
|
setReadyState(activeClient.state);
|
||||||
|
setIdentifying(true);
|
||||||
|
const finalResponse = await completeInitialSynchronization(
|
||||||
|
activeClient,
|
||||||
|
subscribe,
|
||||||
|
attemptAbort.signal,
|
||||||
|
);
|
||||||
|
if (disposed || clientRef.current !== activeClient) return;
|
||||||
|
setFreshContacts(finalResponse.data.Contacts);
|
||||||
|
setFreshCommunities(finalResponse.data.Communities);
|
||||||
|
setFreshCalls(finalResponse.data.Calls);
|
||||||
|
connectionReady = true;
|
||||||
|
setIdentifying(false);
|
||||||
|
setIdentified(true);
|
||||||
|
clearReconnectTimer();
|
||||||
|
clearReconnectResetTimer();
|
||||||
|
reconnectResetTimer = setTimeout(() => {
|
||||||
|
attempts = 0;
|
||||||
|
reconnectResetTimer = null;
|
||||||
|
}, RECONNECT_RESET * 1_000);
|
||||||
|
resolveConnectionRef.current?.();
|
||||||
|
} catch (connectError) {
|
||||||
|
if (disposed || generation !== connectionGeneration) {
|
||||||
|
client?.disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
failed = true;
|
||||||
|
cleanup();
|
||||||
|
const message =
|
||||||
|
connectError instanceof Error
|
||||||
|
? connectError.message
|
||||||
|
: String(connectError ?? "Unknown error");
|
||||||
|
log(
|
||||||
|
0,
|
||||||
|
"mtp",
|
||||||
|
"red",
|
||||||
|
`Connection/authentication attempt failed: ${message}`,
|
||||||
|
protocolErrorDetails(connectError) ?? connectError,
|
||||||
|
);
|
||||||
|
scheduleReconnect(connectError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void connect();
|
||||||
|
return () => {
|
||||||
|
disposed = true;
|
||||||
|
clearReconnectTimer();
|
||||||
|
clearReconnectResetTimer();
|
||||||
|
cleanupConnection();
|
||||||
|
setReadyState(ConnectionState.Disconnected);
|
||||||
|
setIdentified(false);
|
||||||
|
setIdentifying(false);
|
||||||
|
sonnerToast.dismiss("mtp-connection-toast");
|
||||||
|
};
|
||||||
|
}, [attachSubscriptions, load, mtpUrl, props.blockConnection, subscribe]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return subscribe("ErrorNoIota", () => {
|
||||||
|
setIdentified(false);
|
||||||
|
setIdentifying(false);
|
||||||
|
sonnerToast.error("We couldn't reach your Iota", {
|
||||||
|
description:
|
||||||
|
"Check your network connection and try restarting your Iota",
|
||||||
|
icon: null,
|
||||||
|
duration: Infinity,
|
||||||
|
closeButton: true,
|
||||||
|
});
|
||||||
|
resolveConnectionRef.current?.();
|
||||||
|
});
|
||||||
|
}, [subscribe]);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() =>
|
||||||
|
subscribe("GetStates", (message) => {
|
||||||
|
setFreshContacts((contacts) =>
|
||||||
|
removeMissingContacts(contacts, message),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
[subscribe],
|
||||||
|
);
|
||||||
|
|
||||||
|
const loadingDescription = useMemo(() => {
|
||||||
|
if (!mtpUrl) return "Loading connection details";
|
||||||
|
if (readyState === ConnectionState.Connecting || !connected) {
|
||||||
|
return "Establishing transport channel";
|
||||||
|
}
|
||||||
|
if (identifying || !identified) return "Waiting for authenticated session";
|
||||||
|
return "Loading...";
|
||||||
|
}, [connected, identified, identifying, readyState, mtpUrl]);
|
||||||
|
const contextReady = connected && identified && mtpUrl !== null;
|
||||||
|
const mtpRef = useMemo(() => createAsyncQueue<{ send: typeof send }>(), []);
|
||||||
|
useEffect(() => {
|
||||||
|
if (connected && identified && mtpUrl) {
|
||||||
|
mtpRef.set({ send });
|
||||||
|
}
|
||||||
|
}, [connected, identified, mtpUrl, send, mtpRef]);
|
||||||
|
|
||||||
|
const sendQueued: BoundSendFn = useMemo(
|
||||||
|
() => async (type, data, options) => {
|
||||||
|
const mtp = await mtpRef.get();
|
||||||
|
const response = await mtp.send(type, data, options);
|
||||||
|
for (const interceptor of interceptorsRef.current) {
|
||||||
|
void Promise.resolve(interceptor({ type, data, response })).catch(
|
||||||
|
(error) => {
|
||||||
|
log(1, "mtp", "yellow", "MTP interceptor failed", error, { type });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
[interceptorsRef, mtpRef],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<MTPContext.Provider
|
||||||
|
value={{
|
||||||
|
send: sendQueued,
|
||||||
|
subscribe,
|
||||||
|
addInterceptor,
|
||||||
|
readyState,
|
||||||
|
identified,
|
||||||
|
freshContacts,
|
||||||
|
freshCommunities,
|
||||||
|
freshCalls,
|
||||||
|
contextReady,
|
||||||
|
loadingDescription,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</MTPContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
import { describe, expect, it } from "vitest";
|
|
||||||
|
|
||||||
import { isPushType, validateResponse } from "./context";
|
|
||||||
|
|
||||||
describe("MTP protocol dispatch", () => {
|
|
||||||
it("preserves protocol errors for the request layer", () => {
|
|
||||||
const error = validateResponse("GetStates", {
|
|
||||||
id: 12,
|
|
||||||
type: "ErrorInternal",
|
|
||||||
data: { ErrorType: "temporary" },
|
|
||||||
});
|
|
||||||
expect(error.type).toBe("ErrorInternal");
|
|
||||||
expect(error.id).toBe(12);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("recognizes initial and live presence pushes", () => {
|
|
||||||
expect(isPushType("GetStates")).toBe(true);
|
|
||||||
expect(isPushType("ClientChanged")).toBe(true);
|
|
||||||
expect(isPushType("UnknownMessage")).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,957 +1,41 @@
|
||||||
import {
|
import { type ReactNode, useContext, useEffect, useState } from "react";
|
||||||
createContext,
|
import { isTauri } from "@tauri-apps/api/core";
|
||||||
type ReactNode,
|
|
||||||
useCallback,
|
|
||||||
useContext,
|
|
||||||
useEffect,
|
|
||||||
useMemo,
|
|
||||||
useRef,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
import { invoke, isTauri } from "@tauri-apps/api/core";
|
|
||||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
|
||||||
import { MTPClient } from "mtp";
|
import { MTPClient } from "mtp";
|
||||||
import { type z } from "zod";
|
|
||||||
import { ConnectionState } from "mtp";
|
|
||||||
import createAsyncQueue from "@tensamin/shared/asyncQueue";
|
|
||||||
import { toast as sonnerToast } from "@methanium/ui";
|
|
||||||
|
|
||||||
import {
|
import { BrowserProvider } from "./browser";
|
||||||
type Calls,
|
import { MTPContext, type MTPContextType } from "./mtpContext";
|
||||||
type Communities,
|
import { TauriProvider } from "./tauri";
|
||||||
type Contacts,
|
|
||||||
mtp as schemas,
|
|
||||||
type MTP as Schemas,
|
|
||||||
} from "@tensamin/shared/data";
|
|
||||||
import { log } from "@tensamin/shared/log";
|
|
||||||
import { ProtocolError } from "@tensamin/shared/errors";
|
|
||||||
import { useStorage } from "@tensamin/storage/context";
|
|
||||||
|
|
||||||
import { RECONNECT_RESET, RECONNECT_TRIES, RETRY_INTERVAL } from "./values";
|
|
||||||
|
|
||||||
function base64ToUint8Array(b64: string) {
|
|
||||||
const bin = atob(b64);
|
|
||||||
const out = new Uint8Array(bin.length);
|
|
||||||
|
|
||||||
for (let i = 0; i < bin.length; i++) {
|
|
||||||
out[i] = bin.charCodeAt(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ProtocolMessage<
|
|
||||||
T extends keyof Schemas & string = keyof Schemas & string,
|
|
||||||
> = {
|
|
||||||
id?: number;
|
|
||||||
type: T | string;
|
|
||||||
data: z.infer<Schemas[T]["response"]>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type BoundSendFn = <T extends keyof Schemas & string>(
|
|
||||||
type: T,
|
|
||||||
data?: z.infer<Schemas[T]["request"]>,
|
|
||||||
options?: { id?: number },
|
|
||||||
) => Promise<ProtocolMessage<T>>;
|
|
||||||
|
|
||||||
export type PushHandler = (message: ProtocolMessage) => void | Promise<void>;
|
|
||||||
|
|
||||||
const PUSH_TYPES = [
|
|
||||||
"MessageLive",
|
|
||||||
"MessageEditLive",
|
|
||||||
"MessageReactionLive",
|
|
||||||
"MessageDeleteLive",
|
|
||||||
"MessageState",
|
|
||||||
"CallInvite",
|
|
||||||
"GetStates",
|
|
||||||
"ClientChanged",
|
|
||||||
"ErrorNoIota",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export function isPushType(type: string): boolean {
|
|
||||||
return (PUSH_TYPES as readonly string[]).includes(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeMissingContacts(
|
|
||||||
contacts: Contacts,
|
|
||||||
message: ProtocolMessage,
|
|
||||||
): Contacts {
|
|
||||||
if (message.type !== "GetStates") return contacts;
|
|
||||||
const data = message.data as { MissingUserIds?: unknown };
|
|
||||||
if (!Array.isArray(data.MissingUserIds)) return contacts;
|
|
||||||
const missing = new Set(
|
|
||||||
data.MissingUserIds.filter(
|
|
||||||
(userId): userId is number => typeof userId === "number",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return contacts.filter((contact) => !missing.has(contact.UserId));
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MTPExchange = {
|
|
||||||
type: keyof Schemas & string;
|
|
||||||
data: unknown;
|
|
||||||
response: ProtocolMessage;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type MTPInterceptor = (exchange: MTPExchange) => void | Promise<void>;
|
|
||||||
|
|
||||||
type ContextType = {
|
|
||||||
send: BoundSendFn;
|
|
||||||
subscribe: <T extends keyof Schemas & string>(
|
|
||||||
type: T,
|
|
||||||
handler: (message: ProtocolMessage<T>) => void,
|
|
||||||
) => () => void;
|
|
||||||
subscribePush: (handler: PushHandler) => () => void;
|
|
||||||
addInterceptor: (interceptor: MTPInterceptor) => () => void;
|
|
||||||
readyState: number;
|
|
||||||
identified: boolean;
|
|
||||||
freshContacts: Contacts;
|
|
||||||
freshCommunities: Communities;
|
|
||||||
freshCalls: Calls;
|
|
||||||
contextReady: boolean;
|
|
||||||
loadingDescription: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const MTPContext = createContext<ContextType | undefined>(undefined);
|
|
||||||
|
|
||||||
function getProtocolErrorDetails(error: unknown) {
|
|
||||||
if (typeof error !== "object" || error === null || !("type" in error)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const protocolError = error as {
|
|
||||||
id?: unknown;
|
|
||||||
type?: unknown;
|
|
||||||
data?: unknown;
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
id: protocolError.id,
|
|
||||||
type: protocolError.type,
|
|
||||||
data: protocolError.data,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zod schema validation
|
|
||||||
export function validateResponse<T extends keyof Schemas & string>(
|
|
||||||
type: T,
|
|
||||||
message: { id?: number; type: string; data: unknown },
|
|
||||||
): ProtocolMessage<T> {
|
|
||||||
if (message.type.startsWith("Error")) {
|
|
||||||
return message as ProtocolMessage<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const schema =
|
|
||||||
schemas[message.type as keyof Schemas & string]?.response ??
|
|
||||||
schemas[type]?.response;
|
|
||||||
if (!schema) {
|
|
||||||
return message as ProtocolMessage<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsed = schema.safeParse(message.data);
|
|
||||||
if (!parsed.success) {
|
|
||||||
throw new Error(
|
|
||||||
`Response validation failed for ${type}: ${parsed.error.message}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: message.id,
|
|
||||||
type: message.type,
|
|
||||||
data: parsed.data,
|
|
||||||
} as ProtocolMessage<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function useMessageHandlers() {
|
|
||||||
const interceptorsRef = useRef(new Set<MTPInterceptor>());
|
|
||||||
const pushHandlersRef = useRef(new Set<PushHandler>());
|
|
||||||
const lastInitialStateRef = useRef<ProtocolMessage | null>(null);
|
|
||||||
const subscribePush = useCallback((handler: PushHandler) => {
|
|
||||||
pushHandlersRef.current.add(handler);
|
|
||||||
const initialState = lastInitialStateRef.current;
|
|
||||||
if (initialState?.type === "GetStates") {
|
|
||||||
void Promise.resolve(handler(initialState)).catch(() => undefined);
|
|
||||||
}
|
|
||||||
return () => pushHandlersRef.current.delete(handler);
|
|
||||||
}, []);
|
|
||||||
const addInterceptor = useCallback((interceptor: MTPInterceptor) => {
|
|
||||||
interceptorsRef.current.add(interceptor);
|
|
||||||
return () => interceptorsRef.current.delete(interceptor);
|
|
||||||
}, []);
|
|
||||||
return {
|
|
||||||
addInterceptor,
|
|
||||||
interceptorsRef,
|
|
||||||
lastInitialStateRef,
|
|
||||||
pushHandlersRef,
|
|
||||||
subscribePush,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function BrowserProvider(props: {
|
|
||||||
children: ReactNode;
|
|
||||||
blockConnection?: boolean;
|
|
||||||
}) {
|
|
||||||
const { load } = useStorage();
|
|
||||||
|
|
||||||
const [readyState, setReadyState] = useState<number>(
|
|
||||||
ConnectionState.Disconnected,
|
|
||||||
);
|
|
||||||
const [identified, setIdentified] = useState<boolean>(false);
|
|
||||||
const [identifying, setIdentifying] = useState<boolean>(false);
|
|
||||||
|
|
||||||
const [freshCommunities, setFreshCommunities] = useState<Communities>([]);
|
|
||||||
const [freshContacts, setFreshContacts] = useState<Contacts>([]);
|
|
||||||
const [freshCalls, setFreshCalls] = useState<Calls>([]);
|
|
||||||
|
|
||||||
const clientRef = useRef<Awaited<ReturnType<typeof MTPClient.create>> | null>(
|
|
||||||
null,
|
|
||||||
);
|
|
||||||
const {
|
|
||||||
addInterceptor,
|
|
||||||
interceptorsRef,
|
|
||||||
lastInitialStateRef,
|
|
||||||
pushHandlersRef,
|
|
||||||
subscribePush,
|
|
||||||
} = useMessageHandlers();
|
|
||||||
|
|
||||||
const connected = readyState === ConnectionState.Connected;
|
|
||||||
|
|
||||||
// MTP url
|
|
||||||
const [mtpUrl, setMtpUrl] = useState<string | null>(null);
|
|
||||||
useEffect(() => {
|
|
||||||
load("omega_url").then(setMtpUrl);
|
|
||||||
}, [load]);
|
|
||||||
|
|
||||||
// Validation override functions
|
|
||||||
const send: BoundSendFn = useMemo(
|
|
||||||
() => async (type, data, options) => {
|
|
||||||
const client = clientRef.current;
|
|
||||||
|
|
||||||
if (!client) {
|
|
||||||
throw new Error("mtp is not connected");
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = await client.request(
|
|
||||||
type,
|
|
||||||
(data ?? {}) as Record<string, unknown>,
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
const response = validateResponse(type, message);
|
|
||||||
setFreshContacts((contacts) => removeMissingContacts(contacts, response));
|
|
||||||
if (response.type.startsWith("Error")) {
|
|
||||||
const errorData = response.data as Record<string, unknown>;
|
|
||||||
throw new ProtocolError({
|
|
||||||
type: response.type,
|
|
||||||
requestId: response.id,
|
|
||||||
errorType:
|
|
||||||
typeof errorData.ErrorType === "string"
|
|
||||||
? errorData.ErrorType
|
|
||||||
: undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
const subscribe = useCallback<ContextType["subscribe"]>((type, handler) => {
|
|
||||||
const client = clientRef.current;
|
|
||||||
if (!client) {
|
|
||||||
return () => {};
|
|
||||||
}
|
|
||||||
|
|
||||||
return client.subscribe(type, (message) => {
|
|
||||||
handler(validateResponse(type, message));
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Reconnect stuff
|
|
||||||
const resolveConnectionRef = useRef(() => {});
|
|
||||||
useEffect(() => {
|
|
||||||
if (!mtpUrl) return;
|
|
||||||
|
|
||||||
let attempts = 0;
|
|
||||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
let reconnectResetTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
let reconnectScheduled = false;
|
|
||||||
let disposed = false;
|
|
||||||
let connectionGeneration = 0;
|
|
||||||
|
|
||||||
const clearReconnectTimer = () => {
|
|
||||||
if (!reconnectTimer) return;
|
|
||||||
clearTimeout(reconnectTimer);
|
|
||||||
reconnectTimer = null;
|
|
||||||
reconnectScheduled = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearReconnectResetTimer = () => {
|
|
||||||
if (!reconnectResetTimer) return;
|
|
||||||
clearTimeout(reconnectResetTimer);
|
|
||||||
reconnectResetTimer = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const scheduleReconnect = (error: unknown) => {
|
|
||||||
if (disposed || reconnectScheduled) return;
|
|
||||||
if (attempts >= RECONNECT_TRIES) {
|
|
||||||
log(0, "mtp", "red", "Reconnection attempts exhausted", error);
|
|
||||||
sonnerToast.error("Connection failed", {
|
|
||||||
id: "mtp-connection-toast",
|
|
||||||
description:
|
|
||||||
error instanceof Error
|
|
||||||
? error.message.split(":")[0]
|
|
||||||
: "Connection lost",
|
|
||||||
icon: null,
|
|
||||||
duration: Infinity,
|
|
||||||
closeButton: true,
|
|
||||||
promise: null,
|
|
||||||
} as unknown as Parameters<typeof sonnerToast.error>[1]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
attempts += 1;
|
|
||||||
sonnerToast.loading(
|
|
||||||
`Reconnecting to server... (attempt ${attempts} of ${RECONNECT_TRIES})`,
|
|
||||||
{ id: "mtp-connection-toast" },
|
|
||||||
);
|
|
||||||
reconnectScheduled = true;
|
|
||||||
reconnectTimer = setTimeout(() => {
|
|
||||||
reconnectScheduled = false;
|
|
||||||
reconnectTimer = null;
|
|
||||||
void connect();
|
|
||||||
}, RETRY_INTERVAL);
|
|
||||||
};
|
|
||||||
|
|
||||||
async function connect() {
|
|
||||||
if (disposed || props.blockConnection) return;
|
|
||||||
|
|
||||||
const generation = ++connectionGeneration;
|
|
||||||
let client: Awaited<ReturnType<typeof MTPClient.create>> | null = null;
|
|
||||||
let failed = false;
|
|
||||||
const cleanup = () => {
|
|
||||||
client?.disconnect();
|
|
||||||
if (clientRef.current === client) {
|
|
||||||
clientRef.current = null;
|
|
||||||
}
|
|
||||||
clearReconnectResetTimer();
|
|
||||||
if (generation === connectionGeneration) {
|
|
||||||
setReadyState(ConnectionState.Disconnected);
|
|
||||||
setIdentified(false);
|
|
||||||
setIdentifying(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
setIdentified(false);
|
|
||||||
setIdentifying(false);
|
|
||||||
|
|
||||||
const [userId, keyring] = await Promise.all([
|
|
||||||
load("user_id"),
|
|
||||||
load("mtp_keyring"),
|
|
||||||
]);
|
|
||||||
if (!userId || !keyring) {
|
|
||||||
throw new Error("Missing login credentials");
|
|
||||||
}
|
|
||||||
const forcedOmikronUrl = await load("forced_omikron_url");
|
|
||||||
const forcedOmikronPublicKey = await load("forced_omikron_public_key");
|
|
||||||
|
|
||||||
let url = null;
|
|
||||||
let omikronPublicKey = null;
|
|
||||||
if (forcedOmikronUrl && forcedOmikronPublicKey) {
|
|
||||||
url = forcedOmikronUrl;
|
|
||||||
omikronPublicKey = forcedOmikronPublicKey;
|
|
||||||
} else {
|
|
||||||
log(2, "mtp", "purple", "Fetching Omikron data.");
|
|
||||||
const data = await fetch(`${mtpUrl}api/get/omikron/${userId}`);
|
|
||||||
|
|
||||||
if (data.status === 404) {
|
|
||||||
sonnerToast.error("We couldn't reach your Iota", {
|
|
||||||
description:
|
|
||||||
"Check your network connection and try restarting your Iota",
|
|
||||||
icon: null,
|
|
||||||
duration: Infinity,
|
|
||||||
closeButton: true,
|
|
||||||
});
|
|
||||||
resolveConnectionRef.current?.();
|
|
||||||
cleanup();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const omikronData = (await data.json()) as {
|
|
||||||
id: number;
|
|
||||||
ip_address: string;
|
|
||||||
port: number;
|
|
||||||
public_key: string;
|
|
||||||
status: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (
|
|
||||||
!omikronData.ip_address ||
|
|
||||||
!omikronData.port ||
|
|
||||||
!omikronData.public_key
|
|
||||||
)
|
|
||||||
throw new Error("Invalid Omikron data");
|
|
||||||
|
|
||||||
url = `https://${omikronData.ip_address}:${omikronData.port}`;
|
|
||||||
omikronPublicKey = omikronData.public_key;
|
|
||||||
}
|
|
||||||
//codec.decode(new Uint8Array(await res.arrayBuffer())),
|
|
||||||
|
|
||||||
if (!url || !omikronPublicKey)
|
|
||||||
throw new Error("Missing Omikron URL or Public Key");
|
|
||||||
|
|
||||||
log(2, "mtp", "green", "Connecting to: " + url);
|
|
||||||
|
|
||||||
client = await MTPClient.create({
|
|
||||||
url,
|
|
||||||
credentials: {
|
|
||||||
clientId: userId,
|
|
||||||
keyring: base64ToUint8Array(keyring),
|
|
||||||
},
|
|
||||||
hostPublicKey: omikronPublicKey,
|
|
||||||
descriptor: "client",
|
|
||||||
pings: true,
|
|
||||||
logger: (event) => {
|
|
||||||
if (event.type === "state") {
|
|
||||||
if (generation !== connectionGeneration) return;
|
|
||||||
const state = client?.state ?? ConnectionState.Disconnected;
|
|
||||||
setReadyState(state);
|
|
||||||
if (
|
|
||||||
state === ConnectionState.Disconnected &&
|
|
||||||
clientRef.current === client &&
|
|
||||||
!failed
|
|
||||||
) {
|
|
||||||
failed = true;
|
|
||||||
clientRef.current = null;
|
|
||||||
setIdentified(false);
|
|
||||||
setIdentifying(false);
|
|
||||||
scheduleReconnect(new Error("MTP connection lost"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.type !== "Pong" && event.type !== "Ping") {
|
|
||||||
log(
|
|
||||||
2,
|
|
||||||
"mtp",
|
|
||||||
event.type === "state"
|
|
||||||
? "purple"
|
|
||||||
: event.direction === "recv"
|
|
||||||
? "cyan"
|
|
||||||
: event.direction === "send"
|
|
||||||
? "gray"
|
|
||||||
: "blue",
|
|
||||||
event.type === "state"
|
|
||||||
? event.data
|
|
||||||
: event.direction === "recv"
|
|
||||||
? "< " + event.type
|
|
||||||
: event.direction === "send"
|
|
||||||
? "> " + event.type
|
|
||||||
: event.type,
|
|
||||||
event,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (disposed || generation !== connectionGeneration) {
|
|
||||||
client.disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const activeClient = client;
|
|
||||||
|
|
||||||
clientRef.current = activeClient;
|
|
||||||
for (const type of PUSH_TYPES) {
|
|
||||||
activeClient.subscribe(type, (message) => {
|
|
||||||
let validated: ProtocolMessage;
|
|
||||||
try {
|
|
||||||
validated = validateResponse(type, message);
|
|
||||||
} catch (error) {
|
|
||||||
log(1, "mtp", "red", "Failed to validate push message", error, {
|
|
||||||
type,
|
|
||||||
data: message.data,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setFreshContacts((contacts) =>
|
|
||||||
removeMissingContacts(contacts, validated),
|
|
||||||
);
|
|
||||||
for (const handler of [...pushHandlersRef.current]) {
|
|
||||||
void Promise.resolve()
|
|
||||||
.then(() => handler(validated))
|
|
||||||
.catch((error) => {
|
|
||||||
log(1, "mtp", "red", "Push handler failed", error, { type });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (validated.type === "GetStates") {
|
|
||||||
lastInitialStateRef.current = validated;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
setReadyState(activeClient.state);
|
|
||||||
|
|
||||||
clearReconnectTimer();
|
|
||||||
|
|
||||||
// Schedule reconnect reset
|
|
||||||
clearReconnectResetTimer();
|
|
||||||
reconnectResetTimer = setTimeout(() => {
|
|
||||||
attempts = 0;
|
|
||||||
reconnectResetTimer = null;
|
|
||||||
}, RECONNECT_RESET * 1_000);
|
|
||||||
|
|
||||||
setReadyState(activeClient.state);
|
|
||||||
setIdentifying(true);
|
|
||||||
|
|
||||||
const stateSync = new Promise<ProtocolMessage<"ClientStateSync">>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
let unsubscribeStateSync = () => {};
|
|
||||||
let unsubscribeNoIota = () => {};
|
|
||||||
const cleanupStateSync = () => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
unsubscribeStateSync();
|
|
||||||
unsubscribeNoIota();
|
|
||||||
};
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
cleanupStateSync();
|
|
||||||
reject(new Error("Initial state synchronization timed out"));
|
|
||||||
}, 120_000);
|
|
||||||
unsubscribeStateSync = activeClient.subscribe(
|
|
||||||
"ClientStateSync",
|
|
||||||
(message) => {
|
|
||||||
cleanupStateSync();
|
|
||||||
try {
|
|
||||||
resolve(validateResponse("ClientStateSync", message));
|
|
||||||
} catch (error) {
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
unsubscribeNoIota = activeClient.subscribe("ErrorNoIota", () => {
|
|
||||||
cleanupStateSync();
|
|
||||||
reject(new Error("No Iota is currently connected"));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
const [, finalResponse] = await Promise.all([
|
|
||||||
activeClient.auth(),
|
|
||||||
stateSync,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (finalResponse.type.startsWith("Error")) {
|
|
||||||
throw new Error(
|
|
||||||
`State synchronization failed: ${finalResponse.type}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const acknowledgement = await activeClient.request("ClientStateAck", {
|
|
||||||
SessionId: finalResponse.data.SessionId,
|
|
||||||
VersionNumber: finalResponse.data.VersionNumber,
|
|
||||||
});
|
|
||||||
if (acknowledgement.type.startsWith("Error")) {
|
|
||||||
throw new Error(
|
|
||||||
`State acknowledgement failed: ${acknowledgement.type}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (disposed || clientRef.current !== activeClient) return;
|
|
||||||
|
|
||||||
setFreshContacts(finalResponse.data.Contacts);
|
|
||||||
setFreshCommunities(finalResponse.data.Communities);
|
|
||||||
setFreshCalls(finalResponse.data.Calls);
|
|
||||||
setIdentifying(false);
|
|
||||||
setIdentified(true);
|
|
||||||
resolveConnectionRef.current?.();
|
|
||||||
} catch (connectError) {
|
|
||||||
if (disposed || generation !== connectionGeneration) {
|
|
||||||
client?.disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
failed = true;
|
|
||||||
cleanup();
|
|
||||||
const connectErrorMessage =
|
|
||||||
connectError instanceof Error
|
|
||||||
? connectError.message
|
|
||||||
: String(connectError ?? "Unknown error");
|
|
||||||
log(
|
|
||||||
0,
|
|
||||||
"mtp",
|
|
||||||
"red",
|
|
||||||
`Connection/authentication attempt failed: ${connectErrorMessage}`,
|
|
||||||
getProtocolErrorDetails(connectError) ?? connectError,
|
|
||||||
);
|
|
||||||
|
|
||||||
scheduleReconnect(connectError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void connect();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
disposed = true;
|
|
||||||
clearReconnectTimer();
|
|
||||||
clearReconnectResetTimer();
|
|
||||||
|
|
||||||
clientRef.current?.disconnect();
|
|
||||||
clientRef.current = null;
|
|
||||||
setReadyState(ConnectionState.Disconnected);
|
|
||||||
setIdentified(false);
|
|
||||||
setIdentifying(false);
|
|
||||||
sonnerToast.dismiss("mtp-connection-toast");
|
|
||||||
};
|
|
||||||
}, [
|
|
||||||
lastInitialStateRef,
|
|
||||||
mtpUrl,
|
|
||||||
props.blockConnection,
|
|
||||||
load,
|
|
||||||
pushHandlersRef,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// No Iota check
|
|
||||||
useEffect(() => {
|
|
||||||
if (!connected) return;
|
|
||||||
|
|
||||||
return subscribe("ErrorNoIota", () => {
|
|
||||||
setIdentified(false);
|
|
||||||
setIdentifying(false);
|
|
||||||
sonnerToast.error("We couldn't reach your Iota", {
|
|
||||||
description:
|
|
||||||
"Check your network connection and try restarting your Iota",
|
|
||||||
icon: null,
|
|
||||||
duration: Infinity,
|
|
||||||
closeButton: true,
|
|
||||||
});
|
|
||||||
resolveConnectionRef.current?.();
|
|
||||||
});
|
|
||||||
}, [connected, subscribe]);
|
|
||||||
|
|
||||||
// Async queue
|
|
||||||
const loadingDescription = useMemo(() => {
|
|
||||||
if (!mtpUrl) return "Loading connection details";
|
|
||||||
if (readyState === ConnectionState.Connecting || !connected) {
|
|
||||||
return "Establishing transport channel";
|
|
||||||
}
|
|
||||||
if (identifying || !identified) return "Waiting for authenticated session";
|
|
||||||
return "Loading...";
|
|
||||||
}, [connected, identified, identifying, readyState, mtpUrl]);
|
|
||||||
const contextReady = connected && identified && mtpUrl !== null;
|
|
||||||
const mtpRef = useMemo(
|
|
||||||
() =>
|
|
||||||
createAsyncQueue<{
|
|
||||||
send: typeof send;
|
|
||||||
subscribe: typeof subscribe;
|
|
||||||
subscribePush: typeof subscribePush;
|
|
||||||
}>(),
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
useEffect(() => {
|
|
||||||
if (connected && identified && mtpUrl) {
|
|
||||||
mtpRef.set({
|
|
||||||
send,
|
|
||||||
subscribe,
|
|
||||||
subscribePush,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [connected, identified, mtpUrl, send, subscribe, subscribePush, mtpRef]);
|
|
||||||
|
|
||||||
const sendQueued: BoundSendFn = useMemo(
|
|
||||||
() => async (type, data, options) => {
|
|
||||||
const mtp = await mtpRef.get();
|
|
||||||
const response = await mtp.send(type, data, options);
|
|
||||||
for (const interceptor of interceptorsRef.current) {
|
|
||||||
void Promise.resolve(
|
|
||||||
interceptor({ type, data, response: response as ProtocolMessage }),
|
|
||||||
).catch((error) => {
|
|
||||||
log(1, "mtp", "yellow", "MTP interceptor failed", error, { type });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
},
|
|
||||||
[interceptorsRef, mtpRef],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<MTPContext.Provider
|
|
||||||
value={{
|
|
||||||
send: sendQueued,
|
|
||||||
subscribe,
|
|
||||||
subscribePush,
|
|
||||||
addInterceptor,
|
|
||||||
readyState,
|
|
||||||
identified,
|
|
||||||
freshContacts,
|
|
||||||
freshCommunities,
|
|
||||||
freshCalls,
|
|
||||||
contextReady,
|
|
||||||
loadingDescription,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{props.children}
|
|
||||||
</MTPContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type NativeSnapshot = {
|
|
||||||
generation: number;
|
|
||||||
readyState: number;
|
|
||||||
identified: boolean;
|
|
||||||
state?: unknown;
|
|
||||||
error?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function TauriProvider(props: {
|
|
||||||
children: ReactNode;
|
|
||||||
blockConnection?: boolean;
|
|
||||||
}) {
|
|
||||||
const [snapshot, setSnapshot] = useState<NativeSnapshot>({
|
|
||||||
generation: 0,
|
|
||||||
readyState: ConnectionState.Disconnected,
|
|
||||||
identified: false,
|
|
||||||
});
|
|
||||||
const [freshContacts, setFreshContacts] = useState<Contacts>([]);
|
|
||||||
const [freshCommunities, setFreshCommunities] = useState<Communities>([]);
|
|
||||||
const [freshCalls, setFreshCalls] = useState<Calls>([]);
|
|
||||||
const generationRef = useRef(0);
|
|
||||||
const {
|
|
||||||
addInterceptor,
|
|
||||||
interceptorsRef,
|
|
||||||
lastInitialStateRef,
|
|
||||||
pushHandlersRef,
|
|
||||||
subscribePush,
|
|
||||||
} = useMessageHandlers();
|
|
||||||
const subscriptionsRef = useRef(
|
|
||||||
new Map<string, Set<(message: ProtocolMessage) => void>>(),
|
|
||||||
);
|
|
||||||
|
|
||||||
const applySnapshot = useCallback((next: NativeSnapshot) => {
|
|
||||||
if (next.generation < generationRef.current) return;
|
|
||||||
generationRef.current = next.generation;
|
|
||||||
if (next.error) {
|
|
||||||
log(0, "android", "orange", "MTP connection failed", next.error);
|
|
||||||
}
|
|
||||||
setSnapshot(next);
|
|
||||||
if (!next.identified || next.state === undefined) return;
|
|
||||||
const parsed = schemas.ClientStateSync.response.safeParse(next.state);
|
|
||||||
if (!parsed.success) {
|
|
||||||
log(0, "mtp", "red", "Invalid native MTP state", parsed.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setFreshContacts(parsed.data.Contacts);
|
|
||||||
setFreshCommunities(parsed.data.Communities);
|
|
||||||
setFreshCalls(parsed.data.Calls);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const dispatchMessage = useCallback(
|
|
||||||
(raw: unknown) => {
|
|
||||||
if (!raw || typeof raw !== "object" || !("type" in raw)) return;
|
|
||||||
const message = raw as { id?: number; type: string; data: unknown };
|
|
||||||
let validated: ProtocolMessage;
|
|
||||||
try {
|
|
||||||
validated = validateResponse(
|
|
||||||
message.type as keyof Schemas & string,
|
|
||||||
message,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
log(1, "mtp", "red", "Failed to validate native MTP message", error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (const handler of subscriptionsRef.current.get(validated.type) ??
|
|
||||||
[]) {
|
|
||||||
handler(validated);
|
|
||||||
}
|
|
||||||
if (!isPushType(validated.type)) return;
|
|
||||||
setFreshContacts((contacts) =>
|
|
||||||
removeMissingContacts(contacts, validated),
|
|
||||||
);
|
|
||||||
for (const handler of [...pushHandlersRef.current]) {
|
|
||||||
void Promise.resolve(handler(validated)).catch((error) => {
|
|
||||||
log(1, "mtp", "red", "Native MTP push handler failed", error, {
|
|
||||||
type: validated.type,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (validated.type === "GetStates") {
|
|
||||||
lastInitialStateRef.current = validated;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[lastInitialStateRef, pushHandlersRef],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (props.blockConnection) return;
|
|
||||||
let disposed = false;
|
|
||||||
let unlisten: UnlistenFn | undefined;
|
|
||||||
void (async () => {
|
|
||||||
try {
|
|
||||||
const nextUnlisten = await listen<
|
|
||||||
| { kind: "state"; snapshot: NativeSnapshot }
|
|
||||||
| { kind: "message"; generation: number; message: unknown }
|
|
||||||
| {
|
|
||||||
kind: "log";
|
|
||||||
level: number;
|
|
||||||
message: string;
|
|
||||||
details?: unknown;
|
|
||||||
}
|
|
||||||
>("mtp://event", ({ payload }) => {
|
|
||||||
if (disposed) return;
|
|
||||||
if (payload.kind === "state") {
|
|
||||||
applySnapshot(payload.snapshot);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (payload.kind === "message") {
|
|
||||||
if (payload.generation === generationRef.current) {
|
|
||||||
dispatchMessage(payload.message);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
log(
|
|
||||||
payload.level,
|
|
||||||
"android",
|
|
||||||
"orange",
|
|
||||||
payload.message,
|
|
||||||
payload.details,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
if (disposed) nextUnlisten();
|
|
||||||
else unlisten = nextUnlisten;
|
|
||||||
} catch (error) {
|
|
||||||
log(0, "mtp", "red", "Failed to subscribe to native MTP events", error);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const current = await invoke<NativeSnapshot>("mtp_status");
|
|
||||||
if (!disposed) applySnapshot(current);
|
|
||||||
} catch (error) {
|
|
||||||
log(0, "mtp", "red", "Failed to load native MTP status", error);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
return () => {
|
|
||||||
disposed = true;
|
|
||||||
unlisten?.();
|
|
||||||
};
|
|
||||||
}, [applySnapshot, dispatchMessage, props.blockConnection]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (props.blockConnection) return;
|
|
||||||
const updateVisibility = () => {
|
|
||||||
void invoke("mtp_set_ui_visible", {
|
|
||||||
visible: document.visibilityState === "visible" && document.hasFocus(),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
updateVisibility();
|
|
||||||
document.addEventListener("visibilitychange", updateVisibility);
|
|
||||||
window.addEventListener("focus", updateVisibility);
|
|
||||||
window.addEventListener("blur", updateVisibility);
|
|
||||||
return () => {
|
|
||||||
document.removeEventListener("visibilitychange", updateVisibility);
|
|
||||||
window.removeEventListener("focus", updateVisibility);
|
|
||||||
window.removeEventListener("blur", updateVisibility);
|
|
||||||
void invoke("mtp_set_ui_visible", { visible: false });
|
|
||||||
};
|
|
||||||
}, [props.blockConnection]);
|
|
||||||
|
|
||||||
const send = useCallback<BoundSendFn>(
|
|
||||||
async (type, data, options) => {
|
|
||||||
const response = await invoke<ProtocolMessage>("mtp_request", {
|
|
||||||
typeName: type,
|
|
||||||
data: data ?? {},
|
|
||||||
id: options?.id,
|
|
||||||
});
|
|
||||||
const validated = validateResponse(type, response);
|
|
||||||
setFreshContacts((contacts) =>
|
|
||||||
removeMissingContacts(contacts, validated),
|
|
||||||
);
|
|
||||||
if (validated.type.startsWith("Error")) {
|
|
||||||
const errorData = validated.data as Record<string, unknown>;
|
|
||||||
throw new ProtocolError({
|
|
||||||
type: validated.type,
|
|
||||||
requestId: validated.id,
|
|
||||||
errorType:
|
|
||||||
typeof errorData.ErrorType === "string"
|
|
||||||
? errorData.ErrorType
|
|
||||||
: undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (const interceptor of interceptorsRef.current) {
|
|
||||||
void Promise.resolve(
|
|
||||||
interceptor({ type, data, response: validated as ProtocolMessage }),
|
|
||||||
).catch((error) => {
|
|
||||||
log(1, "mtp", "yellow", "MTP interceptor failed", error, { type });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return validated;
|
|
||||||
},
|
|
||||||
[interceptorsRef],
|
|
||||||
);
|
|
||||||
|
|
||||||
const subscribe = useCallback<ContextType["subscribe"]>((type, handler) => {
|
|
||||||
const handlers =
|
|
||||||
subscriptionsRef.current.get(type) ??
|
|
||||||
new Set<(message: ProtocolMessage) => void>();
|
|
||||||
handlers.add(handler as (message: ProtocolMessage) => void);
|
|
||||||
subscriptionsRef.current.set(type, handlers);
|
|
||||||
return () => {
|
|
||||||
handlers.delete(handler as (message: ProtocolMessage) => void);
|
|
||||||
if (handlers.size === 0) subscriptionsRef.current.delete(type);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
const connected = snapshot.readyState === ConnectionState.Connected;
|
|
||||||
const contextReady = connected && snapshot.identified;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<MTPContext.Provider
|
|
||||||
value={{
|
|
||||||
send,
|
|
||||||
subscribe,
|
|
||||||
subscribePush,
|
|
||||||
addInterceptor,
|
|
||||||
readyState: snapshot.readyState,
|
|
||||||
identified: snapshot.identified,
|
|
||||||
freshContacts,
|
|
||||||
freshCommunities,
|
|
||||||
freshCalls,
|
|
||||||
contextReady,
|
|
||||||
loadingDescription: connected
|
|
||||||
? "Waiting for authenticated session"
|
|
||||||
: "Establishing native transport channel",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{props.children}
|
|
||||||
</MTPContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Provider(props: {
|
export function Provider(props: {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
blockConnection?: boolean;
|
blockConnection?: boolean;
|
||||||
|
}) {
|
||||||
|
if (isTauri()) return <TauriProvider {...props} />;
|
||||||
|
return <BrowserWasmProvider {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function BrowserWasmProvider(props: {
|
||||||
|
children: ReactNode;
|
||||||
|
blockConnection?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const [wasmReady, setWasmReady] = useState(false);
|
const [wasmReady, setWasmReady] = useState(false);
|
||||||
const [wasmError, setWasmError] = useState<unknown>();
|
const [wasmError, setWasmError] = useState<unknown>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let active = true;
|
let active = true;
|
||||||
void MTPClient.init().then(
|
void MTPClient.init().then(
|
||||||
() => {
|
() => active && setWasmReady(true),
|
||||||
if (active) setWasmReady(true);
|
(error: unknown) => active && setWasmError(() => error),
|
||||||
},
|
|
||||||
(error: unknown) => {
|
|
||||||
if (active) setWasmError(() => error);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
return () => {
|
return () => {
|
||||||
active = false;
|
active = false;
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (wasmError) throw wasmError;
|
if (wasmError) throw wasmError;
|
||||||
if (!wasmReady) return null;
|
return wasmReady ? <BrowserProvider {...props} /> : null;
|
||||||
|
|
||||||
return isTauri() ? (
|
|
||||||
<TauriProvider {...props} />
|
|
||||||
) : (
|
|
||||||
<BrowserProvider {...props} />
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useMTP(): ContextType {
|
export function useMTP(): MTPContextType {
|
||||||
const context = useContext(MTPContext);
|
const context = useContext(MTPContext);
|
||||||
if (!context) {
|
if (!context) throw new Error("useMTP must be used within an MTPProvider");
|
||||||
throw new Error("useMTP must be used within an MTPProvider");
|
|
||||||
}
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,5 @@ export type {
|
||||||
BoundSendFn,
|
BoundSendFn,
|
||||||
MTPExchange,
|
MTPExchange,
|
||||||
MTPInterceptor,
|
MTPInterceptor,
|
||||||
PushHandler,
|
|
||||||
ProtocolMessage,
|
ProtocolMessage,
|
||||||
} from "./context";
|
} from "./mtpContext";
|
||||||
|
|
|
||||||
159
packages/mtp/src/mtpContext.tsx
Normal file
159
packages/mtp/src/mtpContext.tsx
Normal file
|
|
@ -0,0 +1,159 @@
|
||||||
|
import { createContext, useCallback, useRef } from "react";
|
||||||
|
import type {
|
||||||
|
MTPRequestFunction,
|
||||||
|
MTPResponseFrame,
|
||||||
|
MTPSubscriptionFunction,
|
||||||
|
} from "mtp";
|
||||||
|
import {
|
||||||
|
mtp as mtpSchemas,
|
||||||
|
type Calls,
|
||||||
|
type Communities,
|
||||||
|
type Contacts,
|
||||||
|
} from "@tensamin/shared/data";
|
||||||
|
import { log } from "@tensamin/shared/log";
|
||||||
|
|
||||||
|
export type ProtocolMessage<
|
||||||
|
Type extends keyof typeof mtpSchemas & string = keyof typeof mtpSchemas &
|
||||||
|
string,
|
||||||
|
> = MTPResponseFrame<typeof mtpSchemas, Type>;
|
||||||
|
|
||||||
|
export type BoundSendFn = MTPRequestFunction<typeof mtpSchemas>;
|
||||||
|
|
||||||
|
export type MTPExchange = {
|
||||||
|
type: keyof typeof mtpSchemas & string;
|
||||||
|
data: unknown;
|
||||||
|
response: ProtocolMessage;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type MTPInterceptor = (exchange: MTPExchange) => void | Promise<void>;
|
||||||
|
|
||||||
|
export type MTPContextType = {
|
||||||
|
send: BoundSendFn;
|
||||||
|
subscribe: MTPSubscriptionFunction<typeof mtpSchemas>;
|
||||||
|
addInterceptor: (interceptor: MTPInterceptor) => () => void;
|
||||||
|
readyState: number;
|
||||||
|
identified: boolean;
|
||||||
|
freshContacts: Contacts;
|
||||||
|
freshCommunities: Communities;
|
||||||
|
freshCalls: Calls;
|
||||||
|
contextReady: boolean;
|
||||||
|
loadingDescription: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const MTPContext = createContext<MTPContextType | undefined>(undefined);
|
||||||
|
|
||||||
|
export function removeMissingContacts(
|
||||||
|
contacts: Contacts,
|
||||||
|
message: ProtocolMessage<"GetStates">,
|
||||||
|
): Contacts {
|
||||||
|
const missing = new Set(message.data.MissingUserIds ?? []);
|
||||||
|
return contacts.filter((contact) => !missing.has(contact.UserId));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useMessageHandlers() {
|
||||||
|
const interceptorsRef = useRef(new Set<MTPInterceptor>());
|
||||||
|
const subscriptionHandlersRef = useRef(
|
||||||
|
new Map<string, Set<(message: ProtocolMessage) => void | Promise<void>>>(),
|
||||||
|
);
|
||||||
|
const transportRef = useRef<{
|
||||||
|
subscribe: MTPSubscriptionFunction<typeof mtpSchemas>;
|
||||||
|
} | null>(null);
|
||||||
|
const transportGenerationRef = useRef(0);
|
||||||
|
const transportUnsubscribersRef = useRef(new Map<string, () => void>());
|
||||||
|
const lastInitialStateRef = useRef<ProtocolMessage<"GetStates"> | null>(null);
|
||||||
|
|
||||||
|
const attachType = useCallback(
|
||||||
|
<Type extends keyof typeof mtpSchemas & string>(type: Type) => {
|
||||||
|
const transport = transportRef.current;
|
||||||
|
if (!transport || transportUnsubscribersRef.current.has(type)) return;
|
||||||
|
const generation = transportGenerationRef.current;
|
||||||
|
const unsubscribe = transport.subscribe(type, (message) => {
|
||||||
|
if (
|
||||||
|
transportRef.current !== transport ||
|
||||||
|
transportGenerationRef.current !== generation
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
if (type === "GetStates") {
|
||||||
|
lastInitialStateRef.current = message as ProtocolMessage<"GetStates">;
|
||||||
|
}
|
||||||
|
for (const handler of [
|
||||||
|
...(subscriptionHandlersRef.current.get(type) ?? []),
|
||||||
|
]) {
|
||||||
|
void Promise.resolve(handler(message as ProtocolMessage)).catch(
|
||||||
|
(error) => {
|
||||||
|
log(1, "mtp", "red", "Subscription handler failed", error, {
|
||||||
|
type,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
transportUnsubscribersRef.current.set(type, unsubscribe);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const attachSubscriptions = useCallback(
|
||||||
|
(transport: { subscribe: MTPSubscriptionFunction<typeof mtpSchemas> }) => {
|
||||||
|
for (const unsubscribe of transportUnsubscribersRef.current.values()) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
transportUnsubscribersRef.current.clear();
|
||||||
|
transportRef.current = transport;
|
||||||
|
const generation = ++transportGenerationRef.current;
|
||||||
|
for (const type of subscriptionHandlersRef.current.keys()) {
|
||||||
|
attachType(type as keyof typeof mtpSchemas & string);
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
if (
|
||||||
|
transportRef.current !== transport ||
|
||||||
|
transportGenerationRef.current !== generation
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
transportRef.current = null;
|
||||||
|
transportGenerationRef.current += 1;
|
||||||
|
for (const unsubscribe of transportUnsubscribersRef.current.values()) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
transportUnsubscribersRef.current.clear();
|
||||||
|
};
|
||||||
|
},
|
||||||
|
[attachType],
|
||||||
|
);
|
||||||
|
|
||||||
|
const subscribe = useCallback<MTPSubscriptionFunction<typeof mtpSchemas>>(
|
||||||
|
(type, handler) => {
|
||||||
|
const handlers = subscriptionHandlersRef.current.get(type) ?? new Set();
|
||||||
|
const untypedHandler = handler as (
|
||||||
|
message: ProtocolMessage,
|
||||||
|
) => void | Promise<void>;
|
||||||
|
handlers.add(untypedHandler);
|
||||||
|
subscriptionHandlersRef.current.set(type, handlers);
|
||||||
|
attachType(type);
|
||||||
|
const initialState = lastInitialStateRef.current;
|
||||||
|
if (type === "GetStates" && initialState) {
|
||||||
|
void Promise.resolve(untypedHandler(initialState)).catch(
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
handlers.delete(untypedHandler);
|
||||||
|
if (handlers.size !== 0) return;
|
||||||
|
subscriptionHandlersRef.current.delete(type);
|
||||||
|
transportUnsubscribersRef.current.get(type)?.();
|
||||||
|
transportUnsubscribersRef.current.delete(type);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
[attachType],
|
||||||
|
);
|
||||||
|
const addInterceptor = useCallback((interceptor: MTPInterceptor) => {
|
||||||
|
interceptorsRef.current.add(interceptor);
|
||||||
|
return () => interceptorsRef.current.delete(interceptor);
|
||||||
|
}, []);
|
||||||
|
return {
|
||||||
|
addInterceptor,
|
||||||
|
attachSubscriptions,
|
||||||
|
interceptorsRef,
|
||||||
|
subscribe,
|
||||||
|
};
|
||||||
|
}
|
||||||
258
packages/mtp/src/tauri.tsx
Normal file
258
packages/mtp/src/tauri.tsx
Normal file
|
|
@ -0,0 +1,258 @@
|
||||||
|
import {
|
||||||
|
type ReactNode,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
||||||
|
import {
|
||||||
|
ConnectionState,
|
||||||
|
MTPProxyConnection,
|
||||||
|
type MTPFrame,
|
||||||
|
type MTPProxyAdapter,
|
||||||
|
} from "mtp";
|
||||||
|
import {
|
||||||
|
mtp as mtpSchemas,
|
||||||
|
type Calls,
|
||||||
|
type Communities,
|
||||||
|
type Contacts,
|
||||||
|
} from "@tensamin/shared/data";
|
||||||
|
import { log } from "@tensamin/shared/log";
|
||||||
|
|
||||||
|
import {
|
||||||
|
type BoundSendFn,
|
||||||
|
MTPContext,
|
||||||
|
type ProtocolMessage,
|
||||||
|
removeMissingContacts,
|
||||||
|
useMessageHandlers,
|
||||||
|
} from "./mtpContext";
|
||||||
|
|
||||||
|
type NativeSnapshot = {
|
||||||
|
generation: number;
|
||||||
|
readyState: number;
|
||||||
|
identified: boolean;
|
||||||
|
state?: unknown;
|
||||||
|
error?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function createTauriAdapter() {
|
||||||
|
const subscriptions = new Map<string, Set<(message: MTPFrame) => void>>();
|
||||||
|
const adapter: MTPProxyAdapter = {
|
||||||
|
request: (type, data) =>
|
||||||
|
invoke<MTPFrame>("mtp_request", { typeName: type, data }),
|
||||||
|
subscribe(type, handler) {
|
||||||
|
const handlers = subscriptions.get(type) ?? new Set();
|
||||||
|
handlers.add(handler);
|
||||||
|
subscriptions.set(type, handlers);
|
||||||
|
return () => {
|
||||||
|
handlers.delete(handler);
|
||||||
|
if (handlers.size === 0) subscriptions.delete(type);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
adapter,
|
||||||
|
dispatch(message: MTPFrame) {
|
||||||
|
for (const handler of subscriptions.get(message.type) ?? [])
|
||||||
|
handler(message);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TauriProvider(props: {
|
||||||
|
children: ReactNode;
|
||||||
|
blockConnection?: boolean;
|
||||||
|
}) {
|
||||||
|
const [snapshot, setSnapshot] = useState<NativeSnapshot>({
|
||||||
|
generation: 0,
|
||||||
|
readyState: ConnectionState.Disconnected,
|
||||||
|
identified: false,
|
||||||
|
});
|
||||||
|
const [freshContacts, setFreshContacts] = useState<Contacts>([]);
|
||||||
|
const [freshCommunities, setFreshCommunities] = useState<Communities>([]);
|
||||||
|
const [freshCalls, setFreshCalls] = useState<Calls>([]);
|
||||||
|
const generationRef = useRef(0);
|
||||||
|
const { addInterceptor, attachSubscriptions, interceptorsRef, subscribe } =
|
||||||
|
useMessageHandlers();
|
||||||
|
const [{ bridge, connection }] = useState(() => {
|
||||||
|
const bridge = createTauriAdapter();
|
||||||
|
return {
|
||||||
|
bridge,
|
||||||
|
connection: new MTPProxyConnection(bridge.adapter, {
|
||||||
|
schemas: mtpSchemas,
|
||||||
|
throwProtocolErrors: true,
|
||||||
|
onValidationError: (error) => {
|
||||||
|
log(1, "mtp", "red", "Failed to validate native MTP message", error);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const applySnapshot = useCallback(async (next: NativeSnapshot) => {
|
||||||
|
if (next.generation < generationRef.current) return;
|
||||||
|
generationRef.current = next.generation;
|
||||||
|
if (next.error)
|
||||||
|
log(0, "android", "orange", "MTP connection failed", next.error);
|
||||||
|
if (!next.identified) {
|
||||||
|
setSnapshot(next);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (next.state === undefined) {
|
||||||
|
setSnapshot({
|
||||||
|
...next,
|
||||||
|
identified: false,
|
||||||
|
error: "Native MTP connection omitted initial state",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const state = await mtpSchemas.ClientStateSync.response.parseAsync(
|
||||||
|
next.state,
|
||||||
|
);
|
||||||
|
setFreshContacts(state.Contacts);
|
||||||
|
setFreshCommunities(state.Communities);
|
||||||
|
setFreshCalls(state.Calls);
|
||||||
|
setSnapshot(next);
|
||||||
|
} catch (error) {
|
||||||
|
log(0, "mtp", "red", "Invalid native MTP state", error);
|
||||||
|
setSnapshot({
|
||||||
|
...next,
|
||||||
|
identified: false,
|
||||||
|
error: "Invalid ClientStateSync payload",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const dispatchMessage = useCallback(
|
||||||
|
(message: MTPFrame) => {
|
||||||
|
bridge.dispatch(message);
|
||||||
|
},
|
||||||
|
[bridge],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return attachSubscriptions(connection);
|
||||||
|
}, [attachSubscriptions, connection]);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() =>
|
||||||
|
subscribe("GetStates", (message) => {
|
||||||
|
setFreshContacts((contacts) =>
|
||||||
|
removeMissingContacts(contacts, message),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
[subscribe],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (props.blockConnection) return;
|
||||||
|
let disposed = false;
|
||||||
|
let unlisten: UnlistenFn | undefined;
|
||||||
|
void (async () => {
|
||||||
|
try {
|
||||||
|
const nextUnlisten = await listen<
|
||||||
|
| { kind: "state"; snapshot: NativeSnapshot }
|
||||||
|
| { kind: "message"; generation: number; message: MTPFrame }
|
||||||
|
| { kind: "log"; level: number; message: string; details?: unknown }
|
||||||
|
>("mtp://event", ({ payload }) => {
|
||||||
|
if (disposed) return;
|
||||||
|
if (payload.kind === "state") {
|
||||||
|
void applySnapshot(payload.snapshot);
|
||||||
|
} else if (payload.kind === "message") {
|
||||||
|
if (payload.generation === generationRef.current) {
|
||||||
|
dispatchMessage(payload.message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log(
|
||||||
|
payload.level,
|
||||||
|
"android",
|
||||||
|
"orange",
|
||||||
|
payload.message,
|
||||||
|
payload.details,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (disposed) nextUnlisten();
|
||||||
|
else unlisten = nextUnlisten;
|
||||||
|
} catch (error) {
|
||||||
|
log(0, "mtp", "red", "Failed to subscribe to native MTP events", error);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const current = await invoke<NativeSnapshot>("mtp_status");
|
||||||
|
if (!disposed) await applySnapshot(current);
|
||||||
|
} catch (error) {
|
||||||
|
log(0, "mtp", "red", "Failed to load native MTP status", error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
return () => {
|
||||||
|
disposed = true;
|
||||||
|
unlisten?.();
|
||||||
|
};
|
||||||
|
}, [applySnapshot, dispatchMessage, props.blockConnection]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (props.blockConnection) return;
|
||||||
|
const updateVisibility = () => {
|
||||||
|
void invoke("mtp_set_ui_visible", {
|
||||||
|
visible: document.visibilityState === "visible" && document.hasFocus(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
updateVisibility();
|
||||||
|
document.addEventListener("visibilitychange", updateVisibility);
|
||||||
|
window.addEventListener("focus", updateVisibility);
|
||||||
|
window.addEventListener("blur", updateVisibility);
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("visibilitychange", updateVisibility);
|
||||||
|
window.removeEventListener("focus", updateVisibility);
|
||||||
|
window.removeEventListener("blur", updateVisibility);
|
||||||
|
void invoke("mtp_set_ui_visible", { visible: false });
|
||||||
|
};
|
||||||
|
}, [props.blockConnection]);
|
||||||
|
|
||||||
|
const send = useCallback<BoundSendFn>(
|
||||||
|
async (type, data, options) => {
|
||||||
|
const response = await connection.request(type, data, options);
|
||||||
|
if (response.type === "GetStates") {
|
||||||
|
setFreshContacts((contacts) =>
|
||||||
|
removeMissingContacts(
|
||||||
|
contacts,
|
||||||
|
response as ProtocolMessage<"GetStates">,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (const interceptor of interceptorsRef.current) {
|
||||||
|
void Promise.resolve(interceptor({ type, data, response })).catch(
|
||||||
|
(error) => {
|
||||||
|
log(1, "mtp", "yellow", "MTP interceptor failed", error, { type });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
[connection, interceptorsRef],
|
||||||
|
);
|
||||||
|
const connected = snapshot.readyState === ConnectionState.Connected;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<MTPContext.Provider
|
||||||
|
value={{
|
||||||
|
send,
|
||||||
|
subscribe,
|
||||||
|
addInterceptor,
|
||||||
|
readyState: snapshot.readyState,
|
||||||
|
identified: snapshot.identified,
|
||||||
|
freshContacts,
|
||||||
|
freshCommunities,
|
||||||
|
freshCalls,
|
||||||
|
contextReady: connected && snapshot.identified,
|
||||||
|
loadingDescription: connected
|
||||||
|
? "Waiting for authenticated session"
|
||||||
|
: "Establishing native transport channel",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</MTPContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
export const RETRY_INTERVAL = 3_000;
|
export const RETRY_INTERVAL = 3_000;
|
||||||
export const RECONNECT_TRIES = 3;
|
export const RECONNECT_TRIES = 3;
|
||||||
export const RECONNECT_RESET = 6;
|
export const RECONNECT_RESET = 6;
|
||||||
|
export const RECONNECT_LONG_INTERVAL = 60_000;
|
||||||
|
export const RECONNECT_JITTER = 0.2;
|
||||||
|
export const DISCOVERY_TIMEOUT = 20_000;
|
||||||
|
export const INITIAL_SYNC_TIMEOUT = 120_000;
|
||||||
|
export const STATE_ACK_TIMEOUT = 30_000;
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ import { useLocation, useNavigate } from "@tanstack/react-router";
|
||||||
import { decryptChatText } from "@tensamin/crypto/chatSecret";
|
import { decryptChatText } from "@tensamin/crypto/chatSecret";
|
||||||
import { log } from "@tensamin/shared/log";
|
import { log } from "@tensamin/shared/log";
|
||||||
import { playSound } from "@tensamin/shared/sounds";
|
import { playSound } from "@tensamin/shared/sounds";
|
||||||
import { type RawMessage } from "@tensamin/chat/values";
|
|
||||||
|
|
||||||
export const context = createContext<contextType | undefined>(undefined);
|
export const context = createContext<contextType | undefined>(undefined);
|
||||||
|
|
||||||
|
|
@ -30,7 +29,7 @@ async function requestNotificationPermission() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Provider(props: { children: React.ReactNode }) {
|
export default function Provider(props: { children: React.ReactNode }) {
|
||||||
const { subscribePush, send } = useMTP();
|
const { subscribe, send } = useMTP();
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
const { get } = useUser();
|
const { get } = useUser();
|
||||||
const { addLiveMessage, chatSecret, getChatSecret, userId } = useChat();
|
const { addLiveMessage, chatSecret, getChatSecret, userId } = useChat();
|
||||||
|
|
@ -39,154 +38,140 @@ export default function Provider(props: { children: React.ReactNode }) {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return subscribePush(async (message) => {
|
return subscribe("MessageLive", async ({ data }) => {
|
||||||
if (message.type === "MessageLive") {
|
if (!data.SenderId) return;
|
||||||
const data = message.data as {
|
|
||||||
Message?: RawMessage;
|
|
||||||
SenderId?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!data.SenderId) return;
|
const isCurrentChat =
|
||||||
|
location.pathname === "/chat" && userId === data.SenderId;
|
||||||
|
const appFocused =
|
||||||
|
document.hasFocus() && document.visibilityState === "visible";
|
||||||
|
const shouldAlert = !isCurrentChat || !appFocused;
|
||||||
|
|
||||||
const isCurrentChat =
|
const messageSecret =
|
||||||
location.pathname === "/chat" && userId === data.SenderId;
|
isCurrentChat && chatSecret
|
||||||
const appFocused =
|
? chatSecret
|
||||||
document.hasFocus() && document.visibilityState === "visible";
|
: await getChatSecret(data.SenderId);
|
||||||
const shouldAlert = !isCurrentChat || !appFocused;
|
|
||||||
|
|
||||||
const messageSecret =
|
if (!data.Message || !messageSecret) return;
|
||||||
isCurrentChat && chatSecret
|
if (shouldAlert) playSound("message");
|
||||||
? chatSecret
|
|
||||||
: await getChatSecret(data.SenderId);
|
|
||||||
|
|
||||||
if (!data.Message || !messageSecret) return;
|
void decryptChatText(messageSecret, data.Message.Content)
|
||||||
if (shouldAlert) playSound("message");
|
.catch((err) => {
|
||||||
|
log(1, "chat", "red", "Failed to decrypt live message", err, {
|
||||||
|
SendTime: data.Message?.SendTime,
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.then(async (content) => {
|
||||||
|
if (!data.Message || !content || !data.SenderId) return;
|
||||||
|
|
||||||
void decryptChatText(messageSecret, data.Message.Content)
|
if (isCurrentChat) {
|
||||||
.catch((err) => {
|
addLiveMessage({
|
||||||
log(1, "chat", "red", "Failed to decrypt live message", err, {
|
...data.Message,
|
||||||
SendTime: data.Message?.SendTime,
|
Content: content ?? "Failed to decrypt message",
|
||||||
|
decryptionFailed: content === null,
|
||||||
});
|
});
|
||||||
return null;
|
}
|
||||||
})
|
|
||||||
.then(async (content) => {
|
|
||||||
if (!data.Message || !content || !data.SenderId) return;
|
|
||||||
|
|
||||||
if (isCurrentChat) {
|
if (!shouldAlert) return;
|
||||||
addLiveMessage({
|
|
||||||
...data.Message,
|
if (!isCurrentChat) {
|
||||||
Content: content ?? "Failed to decrypt message",
|
// todo: add notification symbol to conversation cards (incl. message start)
|
||||||
decryptionFailed: content === null,
|
moveUserIdToTop(data.SenderId);
|
||||||
|
|
||||||
|
if (await load("settings.receive_confirmations")) {
|
||||||
|
void send("MessageState", {
|
||||||
|
MessageState: "received",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!shouldAlert) return;
|
const user = await get(data.SenderId, [
|
||||||
|
"UserId",
|
||||||
|
"Display",
|
||||||
|
"Avatar",
|
||||||
|
]);
|
||||||
|
|
||||||
if (!isCurrentChat) {
|
if (isTauri()) {
|
||||||
// todo: add notification symbol to conversation cards (incl. message start)
|
if (!appFocused) return;
|
||||||
moveUserIdToTop(data.SenderId);
|
const permissionGranted =
|
||||||
|
(await isTauriNotificationPermissionGranted()) ||
|
||||||
|
(await requestTauriNotificationPermission()) === "granted";
|
||||||
|
|
||||||
if (await load("settings.receive_confirmations")) {
|
if (permissionGranted) {
|
||||||
void send(
|
let handledNatively = false;
|
||||||
"MessageState",
|
try {
|
||||||
|
handledNatively = await invoke<boolean>(
|
||||||
|
"mtp_post_message_notification",
|
||||||
{
|
{
|
||||||
MessageState: "received",
|
senderId: user.UserId,
|
||||||
},
|
sender: user.Display,
|
||||||
{
|
body: content,
|
||||||
id: data.Message.SendTime,
|
avatar: user.Avatar,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
} catch (error) {
|
||||||
|
log(
|
||||||
|
1,
|
||||||
|
"notifications",
|
||||||
|
"red",
|
||||||
|
"Failed to create native message notification",
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!handledNatively) {
|
||||||
|
sendTauriNotification({ title: user.Display, body: content });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const hasPermissions = await requestNotificationPermission();
|
||||||
|
|
||||||
const user = await get(data.SenderId, [
|
if (hasPermissions) {
|
||||||
"UserId",
|
const options: NotificationOptions = {
|
||||||
"Display",
|
body: content,
|
||||||
"Avatar",
|
icon: user.Avatar || "/icons/icon-192.png",
|
||||||
]);
|
badge: "/icons/notification-badge.png",
|
||||||
|
tag: `message-${user.UserId}`,
|
||||||
if (isTauri()) {
|
silent: true,
|
||||||
if (!appFocused) return;
|
};
|
||||||
const permissionGranted =
|
if ("serviceWorker" in navigator) {
|
||||||
(await isTauriNotificationPermissionGranted()) ||
|
const registration =
|
||||||
(await requestTauriNotificationPermission()) === "granted";
|
await navigator.serviceWorker.getRegistration();
|
||||||
|
if (registration) {
|
||||||
if (permissionGranted) {
|
await registration.showNotification(user.Display, {
|
||||||
let handledNatively = false;
|
...options,
|
||||||
try {
|
data: { url: `/chat?id=${user.UserId}` },
|
||||||
handledNatively = await invoke<boolean>(
|
|
||||||
"mtp_post_message_notification",
|
|
||||||
{
|
|
||||||
senderId: user.UserId,
|
|
||||||
sender: user.Display,
|
|
||||||
body: content,
|
|
||||||
avatar: user.Avatar,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
log(
|
|
||||||
1,
|
|
||||||
"notifications",
|
|
||||||
"red",
|
|
||||||
"Failed to create native message notification",
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!handledNatively) {
|
|
||||||
sendTauriNotification({ title: user.Display, body: content });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const hasPermissions = await requestNotificationPermission();
|
|
||||||
|
|
||||||
if (hasPermissions) {
|
|
||||||
const options: NotificationOptions = {
|
|
||||||
body: content,
|
|
||||||
icon: user.Avatar || "/icons/icon-192.png",
|
|
||||||
badge: "/icons/notification-badge.png",
|
|
||||||
tag: `message-${user.UserId}`,
|
|
||||||
silent: true,
|
|
||||||
};
|
|
||||||
if ("serviceWorker" in navigator) {
|
|
||||||
const registration =
|
|
||||||
await navigator.serviceWorker.getRegistration();
|
|
||||||
if (registration) {
|
|
||||||
await registration.showNotification(user.Display, {
|
|
||||||
...options,
|
|
||||||
data: { url: `/chat?id=${user.UserId}` },
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const notification = new Notification(user.Display, options);
|
|
||||||
notification.onclick = () => {
|
|
||||||
window.focus();
|
|
||||||
navigate({
|
|
||||||
to: `/chat?id=${user.UserId}`,
|
|
||||||
});
|
});
|
||||||
notification.close();
|
return;
|
||||||
};
|
}
|
||||||
} else {
|
|
||||||
sonnerToast(user.Display, {
|
|
||||||
classNames: {
|
|
||||||
content: "pl-4",
|
|
||||||
},
|
|
||||||
description: content,
|
|
||||||
icon: (
|
|
||||||
<Avatar>
|
|
||||||
<AvatarImage src={user.Avatar} />
|
|
||||||
<AvatarFallback>
|
|
||||||
{user.Display.slice(0, 2).toUpperCase()}
|
|
||||||
</AvatarFallback>
|
|
||||||
</Avatar>
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
const notification = new Notification(user.Display, options);
|
||||||
|
notification.onclick = () => {
|
||||||
|
window.focus();
|
||||||
|
navigate({
|
||||||
|
to: `/chat?id=${user.UserId}`,
|
||||||
|
});
|
||||||
|
notification.close();
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
sonnerToast(user.Display, {
|
||||||
|
classNames: {
|
||||||
|
content: "pl-4",
|
||||||
|
},
|
||||||
|
description: content,
|
||||||
|
icon: (
|
||||||
|
<Avatar>
|
||||||
|
<AvatarImage src={user.Avatar} />
|
||||||
|
<AvatarFallback>
|
||||||
|
{user.Display.slice(0, 2).toUpperCase()}
|
||||||
|
</AvatarFallback>
|
||||||
|
</Avatar>
|
||||||
|
),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, [
|
}, [
|
||||||
addLiveMessage,
|
addLiveMessage,
|
||||||
|
|
@ -197,7 +182,7 @@ export default function Provider(props: { children: React.ReactNode }) {
|
||||||
navigate,
|
navigate,
|
||||||
send,
|
send,
|
||||||
moveUserIdToTop,
|
moveUserIdToTop,
|
||||||
subscribePush,
|
subscribe,
|
||||||
getChatSecret,
|
getChatSecret,
|
||||||
userId,
|
userId,
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { Outlet, useLocation, useNavigate } from "@tanstack/react-router";
|
import { Outlet, useLocation } from "@tanstack/react-router";
|
||||||
import { Button, ClearStorageButton, cn, useIsMobile } from "@methanium/ui";
|
import { Button, useIsMobile } from "@methanium/ui";
|
||||||
import { ArrowLeft } from "lucide-react";
|
import { ArrowLeft } from "lucide-react";
|
||||||
|
|
||||||
import { settingsNavigation } from "./manifest";
|
import { SettingsSidebar } from "./sidebar";
|
||||||
|
|
||||||
export default function SettingsLayout() {
|
export default function SettingsLayout() {
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
|
|
@ -36,47 +36,3 @@ export default function SettingsLayout() {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SettingsSidebar({
|
|
||||||
mobile = false,
|
|
||||||
className,
|
|
||||||
}: {
|
|
||||||
mobile?: boolean;
|
|
||||||
className?: string;
|
|
||||||
}) {
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const categories = [
|
|
||||||
...new Set(settingsNavigation.map((page) => page.category)),
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
mobile ? "w-full p-1" : "p-3 rounded-tl-2xl border-r bg-input/15 w-50",
|
|
||||||
"flex flex-col gap-6",
|
|
||||||
className,
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{categories.map((category) => (
|
|
||||||
<div key={category} className="flex flex-col gap-2">
|
|
||||||
<h2 className="font-bold text-xs uppercase">{category}</h2>
|
|
||||||
{settingsNavigation
|
|
||||||
.filter((page) => page.category === category)
|
|
||||||
.map((page) => (
|
|
||||||
<Button
|
|
||||||
key={page.path}
|
|
||||||
className="w-full"
|
|
||||||
variant="outline"
|
|
||||||
onClick={() => navigate({ to: `/settings/${page.path}` })}
|
|
||||||
>
|
|
||||||
{page.label}
|
|
||||||
</Button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
<div className="mt-auto">
|
|
||||||
<ClearStorageButton className="w-full" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -8,46 +8,24 @@ import Profile from "./pages/profile";
|
||||||
import Security from "./pages/security";
|
import Security from "./pages/security";
|
||||||
import Theme from "./pages/theme";
|
import Theme from "./pages/theme";
|
||||||
import Hotkeys from "./pages/hotkeys";
|
import Hotkeys from "./pages/hotkeys";
|
||||||
|
import { settingsNavigation } from "./navigation";
|
||||||
|
|
||||||
|
const pageComponents = {
|
||||||
|
profile: Profile,
|
||||||
|
security: Security,
|
||||||
|
chat: Chat,
|
||||||
|
call: Call,
|
||||||
|
cache: Cache,
|
||||||
|
theme: Theme,
|
||||||
|
accessibility: Accessibility,
|
||||||
|
hotkeys: Hotkeys,
|
||||||
|
licenses: Licenses,
|
||||||
|
} as const;
|
||||||
|
|
||||||
export const settingsPages = [
|
export const settingsPages = [
|
||||||
{ path: "/", component: Index },
|
{ path: "/", component: Index },
|
||||||
{
|
...settingsNavigation.map((page) => ({
|
||||||
category: "account",
|
...page,
|
||||||
path: "profile",
|
component: pageComponents[page.path],
|
||||||
label: "Profile",
|
})),
|
||||||
component: Profile,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "account",
|
|
||||||
path: "security",
|
|
||||||
label: "Security",
|
|
||||||
component: Security,
|
|
||||||
},
|
|
||||||
{ category: "general", path: "chat", label: "Chat", component: Chat },
|
|
||||||
{ category: "general", path: "call", label: "Call", component: Call },
|
|
||||||
{ category: "application", path: "cache", label: "Cache", component: Cache },
|
|
||||||
{ category: "application", path: "theme", label: "Theme", component: Theme },
|
|
||||||
{
|
|
||||||
category: "application",
|
|
||||||
path: "accessibility",
|
|
||||||
label: "Accessibility",
|
|
||||||
component: Accessibility,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "application",
|
|
||||||
path: "hotkeys",
|
|
||||||
label: "Hotkeys",
|
|
||||||
component: Hotkeys,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "application",
|
|
||||||
path: "licenses",
|
|
||||||
label: "Licenses",
|
|
||||||
component: Licenses,
|
|
||||||
},
|
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export const settingsNavigation = settingsPages.filter(
|
|
||||||
(page): page is Exclude<(typeof settingsPages)[number], { path: "/" }> =>
|
|
||||||
page.path !== "/",
|
|
||||||
);
|
|
||||||
|
|
|
||||||
31
packages/settings/src/navigation.ts
Normal file
31
packages/settings/src/navigation.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
export const settingsNavigation = [
|
||||||
|
{
|
||||||
|
category: "account",
|
||||||
|
path: "profile",
|
||||||
|
label: "Profile",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
category: "account",
|
||||||
|
path: "security",
|
||||||
|
label: "Security",
|
||||||
|
},
|
||||||
|
{ category: "general", path: "chat", label: "Chat" },
|
||||||
|
{ category: "general", path: "call", label: "Call" },
|
||||||
|
{ category: "application", path: "cache", label: "Cache" },
|
||||||
|
{ category: "application", path: "theme", label: "Theme" },
|
||||||
|
{
|
||||||
|
category: "application",
|
||||||
|
path: "accessibility",
|
||||||
|
label: "Accessibility",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
category: "application",
|
||||||
|
path: "hotkeys",
|
||||||
|
label: "Hotkeys",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
category: "application",
|
||||||
|
path: "licenses",
|
||||||
|
label: "Licenses",
|
||||||
|
},
|
||||||
|
] as const;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { SettingsSidebar } from "../layout";
|
import { SettingsSidebar } from "../sidebar";
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
48
packages/settings/src/sidebar.tsx
Normal file
48
packages/settings/src/sidebar.tsx
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { Button, ClearStorageButton, cn } from "@methanium/ui";
|
||||||
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
|
|
||||||
|
import { settingsNavigation } from "./navigation";
|
||||||
|
|
||||||
|
export function SettingsSidebar({
|
||||||
|
mobile = false,
|
||||||
|
className,
|
||||||
|
}: {
|
||||||
|
mobile?: boolean;
|
||||||
|
className?: string;
|
||||||
|
}) {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const categories = [
|
||||||
|
...new Set(settingsNavigation.map((page) => page.category)),
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
mobile ? "w-full p-1" : "p-3 rounded-tl-2xl border-r bg-input/15 w-50",
|
||||||
|
"flex flex-col gap-6",
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{categories.map((category) => (
|
||||||
|
<div key={category} className="flex flex-col gap-2">
|
||||||
|
<h2 className="font-bold text-xs uppercase">{category}</h2>
|
||||||
|
{settingsNavigation
|
||||||
|
.filter((page) => page.category === category)
|
||||||
|
.map((page) => (
|
||||||
|
<Button
|
||||||
|
key={page.path}
|
||||||
|
className="w-full"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => navigate({ to: `/settings/${page.path}` })}
|
||||||
|
>
|
||||||
|
{page.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<div className="mt-auto">
|
||||||
|
<ClearStorageButton className="w-full" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -9,13 +9,12 @@ import {
|
||||||
useState,
|
useState,
|
||||||
useSyncExternalStore,
|
useSyncExternalStore,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { useMTP, type ProtocolMessage } from "@tensamin/mtp";
|
import { useMTP } from "@tensamin/mtp";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
clientUserStateSchema,
|
clientUserStateSchema,
|
||||||
mtp as schemas,
|
mtp as schemas,
|
||||||
publicUserStateSchema,
|
publicUserStateSchema,
|
||||||
userStateEntrySchema,
|
|
||||||
} from "@tensamin/shared/data";
|
} from "@tensamin/shared/data";
|
||||||
import type z from "zod";
|
import type z from "zod";
|
||||||
import { createCache } from "@tensamin/cache";
|
import { createCache } from "@tensamin/cache";
|
||||||
|
|
@ -89,7 +88,7 @@ export default function UserProvider(props: { children: ReactNode }) {
|
||||||
);
|
);
|
||||||
const revisionsRef = useRef(new Map<number, Map<UserField, number>>());
|
const revisionsRef = useRef(new Map<number, Map<UserField, number>>());
|
||||||
|
|
||||||
const { send, subscribePush } = useMTP();
|
const { send, subscribe: subscribeMTP } = useMTP();
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
const { contacts } = useSession();
|
const { contacts } = useSession();
|
||||||
const [accountId, setAccountId] = useState<number | null>(null);
|
const [accountId, setAccountId] = useState<number | null>(null);
|
||||||
|
|
@ -174,39 +173,6 @@ export default function UserProvider(props: { children: ReactNode }) {
|
||||||
[publishUser],
|
[publishUser],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleStatePush = useCallback(
|
|
||||||
async (message: ProtocolMessage) => {
|
|
||||||
if (!accountId) return;
|
|
||||||
const data = message.data as Record<string, unknown>;
|
|
||||||
if (message.type === "GetStates") {
|
|
||||||
if (Array.isArray(data.MissingUserIds)) {
|
|
||||||
for (const userId of data.MissingUserIds) {
|
|
||||||
if (typeof userId === "number") removePresence(userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Array.isArray(data.UserStates)) return;
|
|
||||||
for (const entry of data.UserStates) {
|
|
||||||
const parsed = userStateEntrySchema.safeParse(entry);
|
|
||||||
if (!parsed.success) continue;
|
|
||||||
if (parsed.data.UserId === accountId) continue;
|
|
||||||
initialStatesRef.current.set(
|
|
||||||
parsed.data.UserId,
|
|
||||||
parsed.data.UserState,
|
|
||||||
);
|
|
||||||
if (applyUserState(parsed.data.UserId, parsed.data.UserState)) {
|
|
||||||
initialStatesRef.current.delete(parsed.data.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (message.type !== "ClientChanged") return;
|
|
||||||
const parsed = schemas.ClientChanged.response.safeParse(data);
|
|
||||||
if (!parsed.success) return;
|
|
||||||
applyUserState(parsed.data.UserId, parsed.data.UserState, true);
|
|
||||||
},
|
|
||||||
[accountId, applyUserState, removePresence],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void load("user_id").then((accountId) => {
|
void load("user_id").then((accountId) => {
|
||||||
accountIdRef.current = accountId;
|
accountIdRef.current = accountId;
|
||||||
|
|
@ -223,8 +189,24 @@ export default function UserProvider(props: { children: ReactNode }) {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!accountId) return;
|
if (!accountId) return;
|
||||||
return subscribePush(handleStatePush);
|
const unsubscribeStates = subscribeMTP("GetStates", ({ data }) => {
|
||||||
}, [accountId, handleStatePush, subscribePush]);
|
for (const userId of data.MissingUserIds ?? []) removePresence(userId);
|
||||||
|
for (const entry of data.UserStates) {
|
||||||
|
if (!entry || entry.UserId === accountId) continue;
|
||||||
|
initialStatesRef.current.set(entry.UserId, entry.UserState);
|
||||||
|
if (applyUserState(entry.UserId, entry.UserState)) {
|
||||||
|
initialStatesRef.current.delete(entry.UserId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const unsubscribeChanged = subscribeMTP("ClientChanged", ({ data }) => {
|
||||||
|
applyUserState(data.UserId, data.UserState, true);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
unsubscribeStates();
|
||||||
|
unsubscribeChanged();
|
||||||
|
};
|
||||||
|
}, [accountId, applyUserState, removePresence, subscribeMTP]);
|
||||||
|
|
||||||
const loadUser = useCallback(
|
const loadUser = useCallback(
|
||||||
async (userId: number): Promise<User> => {
|
async (userId: number): Promise<User> => {
|
||||||
|
|
|
||||||
774
pnpm-lock.yaml
generated
774
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
|
@ -7,4 +7,4 @@ allowBuilds:
|
||||||
esbuild: true
|
esbuild: true
|
||||||
overrides:
|
overrides:
|
||||||
"@methanium/ui": "https://git.methanium.net/methanium/ui/releases/download/0.0.29/methanium-ui.tgz"
|
"@methanium/ui": "https://git.methanium.net/methanium/ui/releases/download/0.0.29/methanium-ui.tgz"
|
||||||
mtp: "https://git.methanium.net/methanium/mtp/releases/download/0.2.0-dev-a692bed/mtp-0.2.0.tgz"
|
mtp: "https://git.methanium.net/methanium/mtp/releases/download/0.3.0-dev-c7c7afe/mtp-0.3.0.tgz"
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,14 @@ function getPackageDirs(dir: string): string[] {
|
||||||
|
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const fullPath = join(dir, entry);
|
const fullPath = join(dir, entry);
|
||||||
if (!statSync(fullPath).isDirectory()) continue;
|
if (entry === "node_modules" || entry.startsWith(".")) continue;
|
||||||
|
let stats;
|
||||||
|
try {
|
||||||
|
stats = statSync(fullPath);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!stats.isDirectory()) continue;
|
||||||
|
|
||||||
if (existsSync(join(fullPath, "package.json"))) {
|
if (existsSync(join(fullPath, "package.json"))) {
|
||||||
dirs.push(fullPath);
|
dirs.push(fullPath);
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,14 @@ function getPackageDirs(dir: string): string[] {
|
||||||
|
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const fullPath = join(dir, entry);
|
const fullPath = join(dir, entry);
|
||||||
if (!statSync(fullPath).isDirectory()) continue;
|
if (entry === "node_modules" || entry.startsWith(".")) continue;
|
||||||
|
let stats;
|
||||||
|
try {
|
||||||
|
stats = statSync(fullPath);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!stats.isDirectory()) continue;
|
||||||
|
|
||||||
if (existsSync(join(fullPath, "package.json"))) {
|
if (existsSync(join(fullPath, "package.json"))) {
|
||||||
dirs.push(fullPath);
|
dirs.push(fullPath);
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,14 @@ function getPackageDirs(dir: string): string[] {
|
||||||
|
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const fullPath = join(dir, entry);
|
const fullPath = join(dir, entry);
|
||||||
if (!statSync(fullPath).isDirectory()) continue;
|
if (entry === "node_modules" || entry.startsWith(".")) continue;
|
||||||
|
let stats;
|
||||||
|
try {
|
||||||
|
stats = statSync(fullPath);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!stats.isDirectory()) continue;
|
||||||
|
|
||||||
if (existsSync(join(fullPath, "package.json"))) {
|
if (existsSync(join(fullPath, "package.json"))) {
|
||||||
dirs.push(fullPath);
|
dirs.push(fullPath);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue