From 468843c43068877a84d898410ab1385dcc27b3d5 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Sun, 24 Oct 2021 23:48:10 +0100 Subject: [PATCH] Initial commit --- .editorconfig | 12 + .gitignore | 1 + Cargo.lock | 1151 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 15 + frontend/index.html | 36 ++ frontend/main.js | 175 +++++++ frontend/styles.css | 101 ++++ src/events.rs | 69 +++ src/main.rs | 140 +++++ src/watch_session.rs | 66 +++ 10 files changed, 1766 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 frontend/index.html create mode 100644 frontend/main.js create mode 100644 frontend/styles.css create mode 100644 src/events.rs create mode 100644 src/main.rs create mode 100644 src/watch_session.rs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dc35434 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = crlf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = true + +[*.rs] +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0dde8c7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1151 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "buf_redux" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f" +dependencies = [ + "memchr", + "safemem", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" +dependencies = [ + "libc", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12aa0eb539080d55c3f2d45a67c3b58b6b0773c1a3ca2dfec66d58c97fd66ca" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5da6ba8c3bb3c165d3c7319fc1cc8304facf1fb8db99c5de877183c08a273888" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d" + +[[package]] +name = "futures-executor" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45025be030969d763025784f7f355043dc6bc74093e4ecc5000ca4dc50d8745c" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "522de2a0fe3e380f1bc577ba0474108faf3f6b18321dbf60b3b9c39a75073377" + +[[package]] +name = "futures-macro" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18e4a4b95cea4b4ccbcf1c5675ca7c4ee4e9e75eb79944d07defde18068f79bb" +dependencies = [ + "autocfg", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36ea153c13024fe480590b3e3d4cad89a0cfacecc24577b68f86c6ced9c2bc11" + +[[package]] +name = "futures-task" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99" + +[[package]] +name = "futures-util" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" +dependencies = [ + "autocfg", + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.10.2+wasi-snapshot-preview1", +] + +[[package]] +name = "h2" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fd819562fcebdac5afc5c113c3ec36f902840b70fd4fc458799c8ce4607ae55" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "headers" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c4eb0471fcb85846d8b0690695ef354f9afb11cb03cac2e1d7c9253351afb0" +dependencies = [ + "base64", + "bitflags", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha-1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" + +[[package]] +name = "httpdate" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" + +[[package]] +name = "hyper" +version = "0.14.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b91bb1f221b6ea1f1e4371216b70f40748774c2fb5971b450c07773fb92d26b" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "idna" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "input_buffer" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413" +dependencies = [ + "bytes", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013" + +[[package]] +name = "lock_api" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "matches" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + +[[package]] +name = "mime_guess" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "mio" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + +[[package]] +name = "multipart" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050aeedc89243f5347c3e237e3e13dc76fbe4ae3742a57b94dc14f69acf76d4" +dependencies = [ + "buf_redux", + "httparse", + "log", + "mime", + "mime_guess", + "quick-error", + "rand 0.7.3", + "safemem", + "tempfile", + "twoway", +] + +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "576bc800220cc65dac09e99e97b08b358cfab6e17078de8dc5fee223bd2d0c08" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e8fe8163d14ce7f0cdac2e040116f22eac817edabff0be91e8aff7e9accf389" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro-nested" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" + +[[package]] +name = "proc-macro2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.3", + "rand_hc 0.3.1", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.3", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom 0.2.3", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core 0.6.3", +] + +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha-1" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" + +[[package]] +name = "smallvec" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" + +[[package]] +name = "socket2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "syn" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if", + "libc", + "rand 0.8.4", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "tinyvec" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83b2a3d4d9091d0abd7eba4dc2710b1718583bd4d8992e2190720ea38f391f7" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "tokio" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2c2416fdedca8443ae44b4527de1ea633af61d8f7169ffa6e72c5b53d24efcc" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2dd85aeaba7b68df939bd357c6afb36c87951be9e80bf9c859f2fc3e9fca0fd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2f3f698253f03119ac0102beaa64f67a67e08074d03a22d18784104543727f" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1a5f475f1b9d077ea1017ecbc60890fda8e54942d680ca0b1d2b47cfa2d861b" +dependencies = [ + "futures-util", + "log", + "pin-project", + "tokio", + "tungstenite", +] + +[[package]] +name = "tokio-util" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d3725d3efa29485e87311c5b699de63cde14b00ed4d256b8318aa30ca452cd" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + +[[package]] +name = "tracing" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" +dependencies = [ + "cfg-if", + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + +[[package]] +name = "tungstenite" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ada8297e8d70872fa9a551d93250a9f407beb9f37ef86494eb20012a2ff7c24" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "input_buffer", + "log", + "rand 0.8.4", + "sha-1", + "url", + "utf-8", +] + +[[package]] +name = "twoway" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1" +dependencies = [ + "memchr", +] + +[[package]] +name = "typenum" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec" + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" + +[[package]] +name = "unicode-normalization" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "url" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom 0.2.3", +] + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "warp" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "332d47745e9a0c38636dbd454729b147d16bd1ed08ae67b3ab281c4506771054" +dependencies = [ + "bytes", + "futures", + "headers", + "http", + "hyper", + "log", + "mime", + "mime_guess", + "multipart", + "percent-encoding", + "pin-project", + "scoped-tls", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-stream", + "tokio-tungstenite", + "tokio-util", + "tower-service", + "tracing", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "watch-party" +version = "0.1.0" +dependencies = [ + "futures", + "futures-util", + "once_cell", + "serde", + "serde_json", + "tokio", + "tokio-stream", + "uuid", + "warp", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a1f13ad --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "watch-party" +version = "0.1.0" +edition = "2021" + +[dependencies] +futures = "0.3.17" +futures-util = "0.3.17" +once_cell = "1.8.0" +serde = { version = "1.0.130", features = ["derive"] } +serde_json = "1.0.68" +tokio = { version = "1.12.0", features = ["full"] } +tokio-stream = "0.1.7" +uuid = { version = "0.8.2", features = ["v4"] } +warp = "0.3.1" diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..11ba102 --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,36 @@ + + + + + watch party :D + + + + + + +
+
+

Join a session

+ + + + +
+ +

+ No session to join? Create a session instead. +

+
+ + + + diff --git a/frontend/main.js b/frontend/main.js new file mode 100644 index 0000000..6e3acfe --- /dev/null +++ b/frontend/main.js @@ -0,0 +1,175 @@ +/** + * @param {string} videoUrl + * @param {{name: string, url: string}[]} subtitles + */ +const createVideoElement = (videoUrl, subtitles) => { + document.querySelector("#pre-join-controls").style["display"] = "none"; + + const video = document.createElement("video"); + video.controls = true; + video.autoplay = false; + + const source = document.createElement("source"); + source.src = videoUrl; + + video.appendChild(source); + + let first = true; + for (const { name, url } of subtitles) { + const track = document.createElement("track"); + track.label = name; + track.src = url; + track.kind = "captions"; + + if (first) { + track.default = true; + first = false; + } + + video.appendChild(track); + } + + return video; +} + +/** + * @param {WebSocket} socket + * @param {HTMLVideoElement} video + */ +const setupSocketEvents = (socket, video) => { + const setVideoTime = time => { + const timeSecs = time / 1000.0; + + if (Math.abs(video.currentTime - timeSecs) > 0.5) { + video.currentTime = timeSecs; + } + } + + socket.addEventListener("message", async messageEvent => { + try { + const event = JSON.parse(messageEvent.data); + console.log(event); + + switch (event.op) { + case "SetPlaying": + if (event.data.playing) { + await video.play(); + } else { + video.pause(); + } + + setVideoTime(event.data.time); + + break; + case "SetTime": + setVideoTime(event.data); + + break; + } + } catch (_err) { + } + }); +} + +/** + * @param {string} videoUrl + * @param {{name: string, url: string}[]} subtitles + * @param {number} currentTime + * @param {boolean} playing + * @param {WebSocket} socket + */ +const setupVideo = async (sessionId, videoUrl, subtitles, currentTime, playing, socket) => { + const video = createVideoElement(videoUrl, subtitles); + document.body.appendChild(video); + + video.currentTime = (currentTime / 1000.0); + + try { + if (playing) { + await video.play() + } else { + video.pause() + } + } catch (err) { + // Auto-play is probably disabled, we should uhhhhhhh do something about it + } + + video.addEventListener("pause", async event => { + await fetch(`/sess/${sessionId}/playing`, { + method: "PUT", + body: JSON.stringify(false), + headers: { + "Content-Type": "application/json" + }, + }); + }); + + video.addEventListener("play", async event => { + await fetch(`/sess/${sessionId}/playing`, { + method: "PUT", + body: JSON.stringify(true), + headers: { + "Content-Type": "application/json" + }, + }); + }); + + let firstSeekComplete = false; + video.addEventListener("seeked", async event => { + if (!firstSeekComplete) { + // The first seeked event is performed by the browser when the video is loading + firstSeekComplete = true; + return; + } + + await fetch(`/sess/${sessionId}/current_time`, { + method: "PUT", + body: JSON.stringify((video.currentTime * 1000) | 0), + headers: { + "Content-Type": "application/json" + }, + }); + }); + + setupSocketEvents(socket, video); +} + +/** @param {string} sessionId */ +const joinSession = async (sessionId) => { + try { + window.location.hash = sessionId; + + const { + video_url, subtitle_tracks, + current_time_ms, is_playing + } = await fetch(`/sess/${sessionId}`).then(r => r.json()); + + const wsUrl = new URL(`/sess/${sessionId}/subscribe`, window.location.href); + wsUrl.protocol = { "http:": "ws:", "https:": "wss:" }[wsUrl.protocol]; + const socket = new WebSocket(wsUrl.toString()); + + setupVideo(sessionId, video_url, subtitle_tracks, current_time_ms, is_playing, socket); + } catch (err) { + // TODO: Show an error on the screen + console.error(err); + } +} + +const main = () => { + document.querySelector("#join-session-form").addEventListener("submit", event => { + event.preventDefault(); + + const sessionId = document.querySelector("#join-session-id").value; + joinSession(sessionId); + }); + + if (window.location.hash.match(/#[0-9a-f\-]+/)) { + document.querySelector("#join-session-id").value = window.location.hash.substring(1); + } +}; + +if (document.readyState === "complete") { + main(); +} else { + document.addEventListener("DOMContentLoaded", main); +} diff --git a/frontend/styles.css b/frontend/styles.css new file mode 100644 index 0000000..9a50d74 --- /dev/null +++ b/frontend/styles.css @@ -0,0 +1,101 @@ +:root { + --bg: rgb(28, 23, 36); + --fg: rgb(234, 234, 248); + --accent: hsl(275, 57%, 68%); +} + +html { + background-color: var(--bg); + color: var(--fg); + font-size: 1.125rem; + font-family: sans-serif; +} + +html, +body { + margin: 0; +} + +video { + width: 100vw; + height: auto; +} + +a { + color: var(--accent); +} + +label { + display: block; +} + +input[type="url"], +input[type="text"] { + box-sizing: border-box; + + background: #fff; + background-clip: padding-box; + border: 1px solid rgba(0, 0, 0, 0.12); + border-radius: 6px; + color: rgba(0, 0, 0, 0.8); + display: block; + + margin: 0.5em 0; + padding: 0.5em 1em; + line-height: 1.5; + + font-family: sans-serif; + font-size: 1em; + width: 500px; + + resize: none; + overflow-x: wrap; + overflow-y: scroll; +} + +button { + background-color: var(--accent); + border: var(--accent); + border-radius: 6px; + color: #fff; + padding: 0.5em 1em; + display: inline-block; + font-weight: 400; + text-align: center; + white-space: nowrap; + vertical-align: middle; + + font-family: sans-serif; + font-size: 1em; + width: 500px; + + user-select: none; + border: 1px solid rgba(0, 0, 0, 0); + line-height: 1.5; +} + +button.small-button { + font-size: 0.75em; + padding-top: 0; + padding-bottom: 0; +} + +.subtitle-track-group { + display: flex; +} + +.subtitle-track-group > * { + margin-top: 0 !important; + margin-bottom: 0 !important; + margin-right: 1ch !important; +} + +#pre-join-controls { + width: 60%; + margin: 0 auto; + margin-top: 4em; +} + +#join-session-form { + margin-bottom: 4em; +} diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..ad3de53 --- /dev/null +++ b/src/events.rs @@ -0,0 +1,69 @@ +use once_cell::sync::Lazy; +use std::{ + collections::HashMap, + sync::atomic::{AtomicUsize, Ordering}, +}; + +use futures::{SinkExt, StreamExt, TryFutureExt}; +use tokio::sync::{ + mpsc::{self, UnboundedSender}, + RwLock, +}; +use tokio_stream::wrappers::UnboundedReceiverStream; + +use serde::{Deserialize, Serialize}; + +use uuid::Uuid; +use warp::ws::{Message, WebSocket}; + +static CONNECTED_VIEWERS: Lazy>> = + Lazy::new(|| RwLock::new(HashMap::new())); +static NEXT_VIEWER_ID: AtomicUsize = AtomicUsize::new(1); + +#[derive(Clone, Serialize, Deserialize)] +#[serde(tag = "op", content = "data")] +pub enum WatchEvent { + SetPlaying { playing: bool, time: u64 }, + SetTime(u64), +} + +pub struct ConnectedViewer { + pub session_uuid: Uuid, + pub tx: UnboundedSender, +} + +pub async fn ws_subscribe(session_uuid: Uuid, ws: WebSocket) { + let viewer_id = NEXT_VIEWER_ID.fetch_add(1, Ordering::Relaxed); + let (mut viewer_ws_tx, mut viewer_ws_rx) = ws.split(); + + let (tx, rx) = mpsc::unbounded_channel::(); + let mut rx = UnboundedReceiverStream::new(rx); + + tokio::task::spawn(async move { + while let Some(event) = rx.next().await { + viewer_ws_tx + .send(Message::text( + serde_json::to_string(&event).expect("couldn't convert WatchEvent into JSON"), + )) + .unwrap_or_else(|e| eprintln!("ws send error: {}", e)) + .await; + } + }); + + CONNECTED_VIEWERS + .write() + .await + .insert(viewer_id, ConnectedViewer { session_uuid, tx }); + while let Some(Ok(_)) = viewer_ws_rx.next().await {} + CONNECTED_VIEWERS.write().await.remove(&viewer_id); +} + +pub async fn ws_publish(session_uuid: Uuid, event: WatchEvent) { + for viewer in CONNECTED_VIEWERS.read().await.values() { + if viewer.session_uuid != session_uuid { + continue; + } + + let _ = viewer.tx.send(event.clone()); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2a47301 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,140 @@ +use std::{collections::HashMap, sync::Mutex}; + +use once_cell::sync::Lazy; +use serde_json::json; +use uuid::Uuid; + +use warb::{hyper::StatusCode, Filter, Reply}; +use warp as warb; // i think it's funny + +mod events; +mod watch_session; + +use serde::Deserialize; + +use crate::{ + events::{ws_publish, ws_subscribe, WatchEvent}, + watch_session::{SubtitleTrack, WatchSession}, +}; + +static SESSIONS: Lazy>> = + Lazy::new(|| Mutex::new(HashMap::new())); + +#[derive(Deserialize)] +struct StartSessionBody { + pub video_url: String, + #[serde(default = "Vec::new")] + pub subtitle_tracks: Vec, +} + +#[tokio::main] +async fn main() { + let start_session_route = warb::path!("start_session") + .and(warb::path::end()) + .and(warb::post()) + .and(warb::body::json()) + .map(|body: StartSessionBody| { + let mut sessions = SESSIONS.lock().unwrap(); + let session_uuid = Uuid::new_v4(); + let session = WatchSession::new(body.video_url, body.subtitle_tracks); + let session_view = session.view(); + sessions.insert(session_uuid, session); + + warb::reply::json(&json!({ "id": session_uuid.to_string(), "session": session_view })) + }); + + enum RequestedSession { + Session(Uuid, WatchSession), + Error(warb::reply::WithStatus), + } + + let get_running_session = warb::path::path("sess") + .and(warb::path::param::()) + .map(|session_id: String| { + if let Ok(uuid) = Uuid::parse_str(&session_id) { + if let Some(session) = SESSIONS.lock().unwrap().get(&uuid) { + RequestedSession::Session(uuid, session.clone()) + } else { + RequestedSession::Error(warb::reply::with_status( + warb::reply::json(&json!({ "error": "session does not exist" })), + StatusCode::NOT_FOUND, + )) + } + } else { + RequestedSession::Error(warb::reply::with_status( + warb::reply::json(&json!({ "error": "invalid session UUID" })), + StatusCode::BAD_REQUEST, + )) + } + }); + + let get_status_route = get_running_session + .and(warb::path::end()) + .map(|requested_session| match requested_session { + RequestedSession::Session(_, sess) => { + warb::reply::with_status(warb::reply::json(&sess.view()), StatusCode::OK) + } + RequestedSession::Error(e) => e, + }); + + let set_playing_route = get_running_session + .and(warb::path!("playing")) + .and(warb::put()) + .and(warb::body::json()) + .map(|requested_session, playing: bool| match requested_session { + RequestedSession::Session(uuid, mut sess) => { + sess.set_playing(playing); + let time = sess.get_time_ms(); + SESSIONS.lock().unwrap().insert(uuid, sess.clone()); + + tokio::spawn(async move { + ws_publish(uuid, WatchEvent::SetPlaying { playing, time }).await + }); + + warb::reply::with_status(warb::reply::json(&sess.view()), StatusCode::OK) + } + RequestedSession::Error(e) => e, + }); + + let set_timestamp_route = get_running_session + .and(warb::path!("current_time")) + .and(warb::put()) + .and(warb::body::json()) + .map( + |requested_session, current_time_ms: u64| match requested_session { + RequestedSession::Session(uuid, mut sess) => { + sess.set_time_ms(current_time_ms); + SESSIONS.lock().unwrap().insert(uuid, sess.clone()); + + tokio::spawn(async move { + ws_publish(uuid, WatchEvent::SetTime(current_time_ms)).await + }); + + warb::reply::with_status(warb::reply::json(&sess.view()), StatusCode::OK) + } + RequestedSession::Error(e) => e, + }, + ); + + let ws_subscribe_route = get_running_session + .and(warp::path!("subscribe")) + .and(warp::ws()) + .map( + |requested_session, ws: warb::ws::Ws| match requested_session { + RequestedSession::Session(uuid, _) => ws + .on_upgrade(move |ws| ws_subscribe(uuid, ws)) + .into_response(), + RequestedSession::Error(error_response) => error_response.into_response(), + }, + ); + + let routes = start_session_route + .or(get_status_route) + .or(set_playing_route) + .or(set_timestamp_route) + .or(ws_subscribe_route) + .or(warb::path::end().and(warb::fs::file("frontend/index.html"))) + .or(warb::fs::dir("frontend")); + + warb::serve(routes).run(([127, 0, 0, 1], 3000)).await; +} diff --git a/src/watch_session.rs b/src/watch_session.rs new file mode 100644 index 0000000..7c228ba --- /dev/null +++ b/src/watch_session.rs @@ -0,0 +1,66 @@ +use serde::{Deserialize, Serialize}; +use std::time::Instant; + +#[derive(Serialize, Deserialize, Clone)] +pub struct SubtitleTrack { + pub url: String, + pub name: String, +} + +#[derive(Clone)] +pub struct WatchSession { + pub video_url: String, + pub subtitle_tracks: Vec, + + is_playing: bool, + playing_from_timestamp: u64, + playing_from_instant: Instant, + // TODO: How do we keep track of the current playing time ? +} + +#[derive(Serialize)] +pub struct WatchSessionView { + pub video_url: String, + pub subtitle_tracks: Vec, + pub current_time_ms: u64, + pub is_playing: bool, +} + +impl WatchSession { + pub fn new(video_url: String, subtitle_tracks: Vec) -> Self { + WatchSession { + video_url, + subtitle_tracks, + is_playing: false, + playing_from_timestamp: 0, + playing_from_instant: Instant::now(), + } + } + + pub fn view(&self) -> WatchSessionView { + WatchSessionView { + video_url: self.video_url.clone(), + subtitle_tracks: self.subtitle_tracks.clone(), + current_time_ms: self.get_time_ms() as u64, + is_playing: self.is_playing, + } + } + + pub fn get_time_ms(&self) -> u64 { + if !self.is_playing { + return self.playing_from_timestamp; + } + + self.playing_from_timestamp + self.playing_from_instant.elapsed().as_millis() as u64 + } + + pub fn set_time_ms(&mut self, time_ms: u64) { + self.playing_from_timestamp = time_ms; + self.playing_from_instant = Instant::now(); + } + + pub fn set_playing(&mut self, playing: bool) { + self.set_time_ms(self.get_time_ms()); + self.is_playing = playing; + } +}