15 Commits

Author SHA1 Message Date
Ulyssa
8c010d7e7e Release v0.0.4 (#26) 2023-01-28 14:24:08 -08:00
Benjamin Große
4337be108b Add "default_room" to profile settings (#25) 2023-01-28 14:12:30 -08:00
Ulyssa
b968d8c4a2 Focus should switch to message bar after :edit (#22) 2023-01-26 16:07:18 -08:00
Ulyssa
5683a2e7a8 Blank lines in table cells of selected message should be highlighted (#23) 2023-01-26 16:07:13 -08:00
Ulyssa
afe892c7fe Support sending and displaying read markers (#11) 2023-01-26 15:40:16 -08:00
Ulyssa
d8713141f2 Display room tags in list of direct messages (#21) 2023-01-26 15:23:15 -08:00
Ulyssa
a6888bbc93 Support displaying and editing room tags (#15) 2023-01-25 17:54:16 -08:00
Ulyssa
4f2261e66f Support sending and displaying formatted messages (#10) 2023-01-23 17:08:11 -08:00
Ulyssa
8966644f6e Support editing messages (#4) 2023-01-19 16:05:02 -08:00
Ulyssa
69125e3fc4 Release v0.0.3 (#20) 2023-01-13 18:02:58 -08:00
Ulyssa
56ec90523c Support redacting messages (#5) 2023-01-13 17:53:54 -08:00
Ulyssa
d13d4b9f7f Support replying to messages (#3) 2023-01-12 21:20:32 -08:00
Ulyssa
54ce042384 Support sending and accepting room invitations (#7) 2023-01-11 17:54:49 -08:00
Ulyssa
b6f4b03c12 Support uploading and downloading message attachments (#13) 2023-01-10 19:59:30 -08:00
Ulyssa
504b520fe1 Support configuring a user's color and name (#19) 2023-01-06 16:56:28 -08:00
19 changed files with 4730 additions and 1236 deletions

464
Cargo.lock generated
View File

@@ -54,9 +54,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.67" version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7724808837b77f4b4de9d283820f9d98bcf496d5692934b857a2399d31ff22e6" checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
[[package]] [[package]]
name = "anymap2" name = "anymap2"
@@ -124,9 +124,9 @@ dependencies = [
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.60" version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d1d8ab452a3936018a687b20e6f7cf5363d713b732b8884001317b0e48aa3" checksum = "eff18d764974428cf3a9328e23fc5c986f5fbed46e6cd4cdf42544df5d297ec1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -168,6 +168,12 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"
[[package]] [[package]]
name = "base64ct" name = "base64ct"
version = "1.5.3" version = "1.5.3"
@@ -223,9 +229,9 @@ dependencies = [
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.11.1" version = "3.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@@ -327,9 +333,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.0.29" version = "4.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d" checksum = "0e638668a62aced2c9fb72b5135a33b4a500485ccf2a0e402e09aa04ab2fc115"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"clap_derive", "clap_derive",
@@ -342,9 +348,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.0.21" version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" checksum = "684a277d672e91966334af371f1a7b5833f9aa00b07c84e92fbce95e00208ce8"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error", "proc-macro-error",
@@ -355,9 +361,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_lex" name = "clap_lex"
version = "0.3.0" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" checksum = "783fe232adfca04f90f56201b26d79682d4cd2625e0bc7290b95123afe558ade"
dependencies = [ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
@@ -481,6 +487,15 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "css-color-parser"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ccb6ce7ef97e6dc6e575e51b596c9889a5cc88a307b5ef177d215c61fd7581d"
dependencies = [
"lazy_static 0.1.16",
]
[[package]] [[package]]
name = "ctr" name = "ctr"
version = "0.9.2" version = "0.9.2"
@@ -506,9 +521,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.84" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27874566aca772cb515af4c6e997b5fe2119820bca447689145e39bb734d19a0" checksum = "b61a7545f753a88bcbe0a70de1fcc0221e10bfc752f576754fa91e663db1622e"
dependencies = [ dependencies = [
"cc", "cc",
"cxxbridge-flags", "cxxbridge-flags",
@@ -518,9 +533,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx-build" name = "cxx-build"
version = "1.0.84" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7bb951f2523a49533003656a72121306b225ec16a49a09dc6b0ba0d6f3ec3c0" checksum = "f464457d494b5ed6905c63b0c4704842aba319084a0a3561cdc1359536b53200"
dependencies = [ dependencies = [
"cc", "cc",
"codespan-reporting", "codespan-reporting",
@@ -533,15 +548,15 @@ dependencies = [
[[package]] [[package]]
name = "cxxbridge-flags" name = "cxxbridge-flags"
version = "1.0.84" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be778b6327031c1c7b61dd2e48124eee5361e6aa76b8de93692f011b08870ab4" checksum = "43c7119ce3a3701ed81aca8410b9acf6fc399d2629d057b87e2efa4e63a3aaea"
[[package]] [[package]]
name = "cxxbridge-macro" name = "cxxbridge-macro"
version = "1.0.84" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b8a2b87662fe5a0a0b38507756ab66aff32638876a0866e5a5fc82ceb07ee49" checksum = "65e07508b90551e610910fa648a1878991d367064997a596135b86df30daf07e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -593,7 +608,7 @@ dependencies = [
"hashbrown", "hashbrown",
"lock_api", "lock_api",
"once_cell", "once_cell",
"parking_lot_core 0.9.5", "parking_lot_core 0.9.6",
] ]
[[package]] [[package]]
@@ -708,9 +723,9 @@ dependencies = [
[[package]] [[package]]
name = "ed25519" name = "ed25519"
version = "1.5.2" version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9c280362032ea4203659fc489832d0204ef09f247a0506f170dafcac08c369" checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7"
dependencies = [ dependencies = [
"serde", "serde",
"signature", "signature",
@@ -813,6 +828,16 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]]
name = "futf"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df420e2e84819663797d1ec6544b13c5be84629e7bb00dc960d6917db2987843"
dependencies = [
"mac",
"new_debug_unreachable",
]
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.25" version = "0.3.25"
@@ -1019,15 +1044,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@@ -1055,6 +1071,20 @@ dependencies = [
"digest 0.10.6", "digest 0.10.6",
] ]
[[package]]
name = "html5ever"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bea68cab48b8459f17cf1c944c67ddc572d272d9f2b274140f223ecb1da4a3b7"
dependencies = [
"log",
"mac",
"markup5ever",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.8" version = "0.2.8"
@@ -1128,21 +1158,24 @@ dependencies = [
[[package]] [[package]]
name = "iamb" name = "iamb"
version = "0.0.1" version = "0.0.4"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",
"css-color-parser",
"dirs", "dirs",
"futures",
"gethostname", "gethostname",
"lazy_static", "html5ever",
"lazy_static 1.4.0",
"markup5ever_rcdom",
"matrix-sdk", "matrix-sdk",
"mime",
"mime_guess",
"modalkit", "modalkit",
"regex", "regex",
"rpassword", "rpassword",
"serde", "serde",
"serde_json", "serde_json",
"sled",
"thiserror", "thiserror",
"tokio", "tokio",
"tracing", "tracing",
@@ -1251,9 +1284,9 @@ dependencies = [
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.3" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" checksum = "e7d6c6f8c91b4b9ed43484ad1a938e393caf35960fce7f82a040497207bd8e9e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys",
@@ -1261,17 +1294,17 @@ dependencies = [
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.7.0" version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e" checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.1" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189"
dependencies = [ dependencies = [
"hermit-abi 0.2.6", "hermit-abi",
"io-lifetimes", "io-lifetimes",
"rustix", "rustix",
"windows-sys", "windows-sys",
@@ -1319,6 +1352,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -1327,9 +1366,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.138" version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]] [[package]]
name = "link-cplusplus" name = "link-cplusplus"
@@ -1374,12 +1413,44 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "mac"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]] [[package]]
name = "maplit" name = "maplit"
version = "1.0.2" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "markup5ever"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2629bb1404f3d34c2e921f21fd34ba00b206124c81f65c50b43b6aaefeb016"
dependencies = [
"log",
"phf",
"phf_codegen",
"string_cache",
"string_cache_codegen",
"tendril",
]
[[package]]
name = "markup5ever_rcdom"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9521dd6750f8e80ee6c53d65e2e4656d7de37064f3a7a5d2d11d05df93839c2"
dependencies = [
"html5ever",
"markup5ever",
"tendril",
"xml5ever",
]
[[package]] [[package]]
name = "matrix-sdk" name = "matrix-sdk"
version = "0.6.2" version = "0.6.2"
@@ -1467,7 +1538,7 @@ dependencies = [
"aes", "aes",
"async-trait", "async-trait",
"atomic", "atomic",
"base64", "base64 0.13.1",
"byteorder", "byteorder",
"ctr", "ctr",
"dashmap", "dashmap",
@@ -1496,7 +1567,7 @@ checksum = "7847d36bba832bc787214323bc042b71dca7fdf2aee9f0e3eb573b64f2f7eb7f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
"base64", "base64 0.13.1",
"dashmap", "dashmap",
"derive_builder", "derive_builder",
"getrandom 0.2.8", "getrandom 0.2.8",
@@ -1580,6 +1651,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
version = "0.2.1" version = "0.2.1"
@@ -1600,9 +1681,9 @@ dependencies = [
[[package]] [[package]]
name = "modalkit" name = "modalkit"
version = "0.0.9" version = "0.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28a676fc7ab6a9fd329ff82d9d291370aafcf904ac3ff9f72397f64529cb1b2d" checksum = "4f57d0d53c9f3d8cad2508351f88656e4185cbb8b95d0c738b314fc8167bc90f"
dependencies = [ dependencies = [
"anymap2", "anymap2",
"bitflags", "bitflags",
@@ -1618,10 +1699,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "nom" name = "new_debug_unreachable"
version = "7.1.1" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [ dependencies = [
"memchr", "memchr",
"minimal-lexical", "minimal-lexical",
@@ -1658,19 +1745,19 @@ dependencies = [
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.14.0" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
dependencies = [ dependencies = [
"hermit-abi 0.1.19", "hermit-abi",
"libc", "libc",
] ]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.16.0" version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
@@ -1714,7 +1801,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [ dependencies = [
"lock_api", "lock_api",
"parking_lot_core 0.9.5", "parking_lot_core 0.9.6",
] ]
[[package]] [[package]]
@@ -1733,9 +1820,9 @@ dependencies = [
[[package]] [[package]]
name = "parking_lot_core" name = "parking_lot_core"
version = "0.9.5" version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff9f3fef3968a3ec5945535ed654cb38ff72d7495a25619e2247fb15a2ed9ba" checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
@@ -1773,6 +1860,44 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "phf"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand 0.8.5",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.0.12" version = "1.0.12"
@@ -1832,6 +1957,12 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "precomputed-hash"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "1.2.1" version = "1.2.1"
@@ -1869,18 +2000,18 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.48" version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9d89e5dba24725ae5678020bf8f1357a9aa7ff10736b551adbcd3f8d17d766f" checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "prost" name = "prost"
version = "0.11.3" version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0b18e655c21ff5ac2084a5ad0611e827b3f92badf79f4910b5a5c58f4d87ff0" checksum = "21dc42e00223fc37204bd4aa177e69420c604ca4a183209a8f9de30c6d934698"
dependencies = [ dependencies = [
"bytes", "bytes",
"prost-derive", "prost-derive",
@@ -1888,9 +2019,9 @@ dependencies = [
[[package]] [[package]]
name = "prost-derive" name = "prost-derive"
version = "0.11.2" version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164ae68b6587001ca506d3bf7f1000bfa248d0e1217b618108fba4ec1d0cc306" checksum = "8bda8c0881ea9f722eb9629376db3d0b903b462477c1aafcb0566610ac28ac5d"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"itertools", "itertools",
@@ -1900,10 +2031,21 @@ dependencies = [
] ]
[[package]] [[package]]
name = "quote" name = "pulldown-cmark"
version = "1.0.22" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "556d0f47a940e895261e77dc200d5eadfc6ef644c179c6f5edfc105e3a2292c8" checksum = "2d9cc634bc78768157b5cbfe988ffcd1dcba95cd2b2f03a88316c08c6d00ed63"
dependencies = [
"bitflags",
"memchr",
"unicase",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@@ -2001,9 +2143,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.7.0" version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@@ -2018,11 +2160,11 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.13" version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9"
dependencies = [ dependencies = [
"base64", "base64 0.21.0",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
@@ -2072,9 +2214,9 @@ dependencies = [
[[package]] [[package]]
name = "ropey" name = "ropey"
version = "1.5.0" version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd22239fafefc42138ca5da064f3c17726a80d2379d817a3521240e78dd0064" checksum = "a4f832915525613e83f275694cb8c184f5df13ca26a9ef0ea6ce736921964c8e"
dependencies = [ dependencies = [
"smallvec", "smallvec",
"str_indices", "str_indices",
@@ -2138,7 +2280,7 @@ version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "716889595f4edc3cfeb94d9f122e413f73e37d7d80ea1c14196e1004241a3889" checksum = "716889595f4edc3cfeb94d9f122e413f73e37d7d80ea1c14196e1004241a3889"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
"bytes", "bytes",
"form_urlencoded", "form_urlencoded",
"getrandom 0.2.8", "getrandom 0.2.8",
@@ -2149,6 +2291,7 @@ dependencies = [
"js_int", "js_int",
"js_option", "js_option",
"percent-encoding", "percent-encoding",
"pulldown-cmark",
"rand 0.8.5", "rand 0.8.5",
"regex", "regex",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@@ -2211,9 +2354,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.36.5" version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588" checksum = "d4fdebc4b395b7fbb9ab11e462e20ed9051e7b16e42d24042c776eca0ac81b03"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@@ -2225,9 +2368,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.7" version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "539a2bfe908f471bfa933876bd1eb6a19cf2176d375f82ef7f99530a40e48c2c" checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f"
dependencies = [ dependencies = [
"log", "log",
"ring", "ring",
@@ -2237,11 +2380,11 @@ dependencies = [
[[package]] [[package]]
name = "rustls-pemfile" name = "rustls-pemfile"
version = "1.0.1" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55" checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b"
dependencies = [ dependencies = [
"base64", "base64 0.21.0",
] ]
[[package]] [[package]]
@@ -2274,15 +2417,15 @@ dependencies = [
[[package]] [[package]]
name = "semver" name = "semver"
version = "1.0.15" version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bfa246f936730408c0abee392cc1a50b118ece708c7f630516defd64480c7d8" checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.151" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@@ -2298,9 +2441,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.151" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2309,9 +2452,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.90" version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8778cc0b528968fe72abec38b5db5a20a70d148116cd9325d2bc5f5180ca3faf" checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@@ -2360,7 +2503,7 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [ dependencies = [
"lazy_static", "lazy_static 1.4.0",
] ]
[[package]] [[package]]
@@ -2399,6 +2542,12 @@ version = "1.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c"
[[package]]
name = "siphasher"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.7" version = "0.4.7"
@@ -2457,9 +2606,35 @@ dependencies = [
[[package]] [[package]]
name = "str_indices" name = "str_indices"
version = "0.4.0" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d9199fa80c817e074620be84374a520062ebac833f358d74b37060ce4a0f2c0" checksum = "5f026164926842ec52deb1938fae44f83dfdb82d0a5b0270c5bd5935ab74d6dd"
[[package]]
name = "string_cache"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213494b7a2b503146286049378ce02b482200519accc31872ee8be91fa820a08"
dependencies = [
"new_debug_unreachable",
"once_cell",
"parking_lot 0.12.1",
"phf_shared",
"precomputed-hash",
"serde",
]
[[package]]
name = "string_cache_codegen"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bb30289b722be4ff74a408c3cc27edeaad656e06cb1fe8fa9231fa59c728988"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
@@ -2475,9 +2650,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.106" version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ee3a69cd2c7e06684677e5629b3878b253af05e4714964204279c6bc02cf0b" checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2497,10 +2672,21 @@ dependencies = [
] ]
[[package]] [[package]]
name = "termcolor" name = "tendril"
version = "1.1.3" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" checksum = "d24a120c5fc464a3458240ee02c299ebcb9d67b5249c8848b09d639dca8d7bb0"
dependencies = [
"futf",
"mac",
"utf-8",
]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [ dependencies = [
"winapi-util", "winapi-util",
] ]
@@ -2589,9 +2775,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.23.0" version = "1.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eab6d665857cc6ca78d6e80303a02cea7a7851e85dfbd77cbdc09bd129f1ef46" checksum = "597a12a59981d9e3c38d216785b0c37399f6e415e8d0712047620f189371b0bb"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"bytes", "bytes",
@@ -2599,9 +2785,7 @@ dependencies = [
"memchr", "memchr",
"mio", "mio",
"num_cpus", "num_cpus",
"parking_lot 0.12.1",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys",
@@ -2645,9 +2829,9 @@ dependencies = [
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.10" version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [ dependencies = [
"serde", "serde",
] ]
@@ -2708,7 +2892,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"
dependencies = [ dependencies = [
"lazy_static", "lazy_static 1.4.0",
"log", "log",
"tracing-core", "tracing-core",
] ]
@@ -2729,9 +2913,9 @@ dependencies = [
[[package]] [[package]]
name = "try-lock" name = "try-lock"
version = "0.2.3" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
[[package]] [[package]]
name = "tui" name = "tui"
@@ -2753,10 +2937,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]] [[package]]
name = "unicode-bidi" name = "unicase"
version = "0.3.8" version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
@@ -2819,6 +3012,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "utf-8"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "0.8.2" version = "0.8.2"
@@ -2858,7 +3057,7 @@ checksum = "f6f20153a1c82ac5f1243b62e80f067ae608facc415c6ef82f88426a61c79886"
dependencies = [ dependencies = [
"aes", "aes",
"arrayvec", "arrayvec",
"base64", "base64 0.13.1",
"cbc", "cbc",
"ed25519-dalek", "ed25519-dalek",
"hkdf", "hkdf",
@@ -3090,45 +3289,45 @@ dependencies = [
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.42.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]] [[package]]
name = "winreg" name = "winreg"
@@ -3151,6 +3350,17 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "xml5ever"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4034e1d05af98b51ad7214527730626f019682d797ba38b51689212118d8e650"
dependencies = [
"log",
"mac",
"markup5ever",
]
[[package]] [[package]]
name = "zeroize" name = "zeroize"
version = "1.3.0" version = "1.3.0"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "iamb" name = "iamb"
version = "0.0.2" version = "0.0.4"
edition = "2018" edition = "2018"
authors = ["Ulyssa <git@ulyssa.dev>"] authors = ["Ulyssa <git@ulyssa.dev>"]
repository = "https://github.com/ulyssa/iamb" repository = "https://github.com/ulyssa/iamb"
@@ -10,23 +10,24 @@ description = "A Matrix chat client that uses Vim keybindings"
license = "Apache-2.0" license = "Apache-2.0"
exclude = [".github", "CONTRIBUTING.md"] exclude = [".github", "CONTRIBUTING.md"]
keywords = ["matrix", "chat", "tui", "vim"] keywords = ["matrix", "chat", "tui", "vim"]
categories = ["command-line-utilities"]
rust-version = "1.66" rust-version = "1.66"
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
clap = {version = "4.0", features = ["derive"]} clap = {version = "4.0", features = ["derive"]}
css-color-parser = "0.1.2"
dirs = "4.0.0" dirs = "4.0.0"
futures = "0.3.21"
gethostname = "0.4.1" gethostname = "0.4.1"
matrix-sdk = {version = "0.6", default-features = false, features = ["e2e-encryption", "sled", "rustls-tls"]} html5ever = "0.26.0"
modalkit = "0.0.9" markup5ever_rcdom = "0.2.0"
mime = "^0.3.16"
mime_guess = "^2.0.4"
regex = "^1.5" regex = "^1.5"
rpassword = "^7.2" rpassword = "^7.2"
serde = "^1.0" serde = "^1.0"
serde_json = "^1.0" serde_json = "^1.0"
sled = "0.34"
thiserror = "^1.0.37" thiserror = "^1.0.37"
tokio = {version = "1.17.0", features = ["full"]}
tracing = "~0.1.36" tracing = "~0.1.36"
tracing-appender = "~0.2.2" tracing-appender = "~0.2.2"
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"
@@ -34,5 +35,17 @@ unicode-segmentation = "^1.7"
unicode-width = "0.1.10" unicode-width = "0.1.10"
url = {version = "^2.2.2", features = ["serde"]} url = {version = "^2.2.2", features = ["serde"]}
[dependencies.modalkit]
version = "0.0.10"
[dependencies.matrix-sdk]
version = "0.6"
default-features = false
features = ["e2e-encryption", "markdown", "sled", "rustls-tls"]
[dependencies.tokio]
version = "1.24.1"
features = ["macros", "net", "rt-multi-thread", "sync", "time"]
[dev-dependencies] [dev-dependencies]
lazy_static = "1.4.0" lazy_static = "1.4.0"

View File

@@ -1,5 +1,9 @@
# iamb # iamb
[![Build Status](https://github.com/ulyssa/iamb/workflows/CI/badge.svg)](https://github.com/ulyssa/iamb/actions?query=workflow%3ACI+)
[![License: Apache 2.0](https://img.shields.io/crates/l/iamb.svg?logo=apache)](https://crates.io/crates/iamb)
[![Latest Version](https://img.shields.io/crates/v/iamb.svg?logo=rust)](https://crates.io/crates/iamb)
## About ## About
`iamb` is a Matrix client for the terminal that uses Vim keybindings. `iamb` is a Matrix client for the terminal that uses Vim keybindings.
@@ -17,7 +21,7 @@ website, [iamb.chat].
Install Rust and Cargo, and then run: Install Rust and Cargo, and then run:
``` ```
cargo install iamb cargo install --locked iamb
``` ```
## Configuration ## Configuration
@@ -42,38 +46,38 @@ Matrix website's [features comparison table][client-comparison-matrix], showing
two other TUI clients and Element Web: two other TUI clients and Element Web:
| | iamb | [gomuks] | [weechat-matrix] | Element Web/Desktop | | | iamb | [gomuks] | [weechat-matrix] | Element Web/Desktop |
| --------------------------------------- | :----------------- | :----------------: | :----------------: | :-----------------: | | --------------------------------------- | :---------- | :------: | :--------------: | :-----------------: |
| Room directory | :x: ([#14]) | :x: | :heavy_check_mark: | :heavy_check_mark: | | Room directory | ([#14]) | ❌ | ✔️ | ✔️ |
| Room tag showing | :x: ([#15]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Room tag showing | ✔️ | ✔️ | ❌ | ✔️ |
| Room tag editing | :x: ([#15]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Room tag editing | ✔️ | ✔️ | ❌ | ✔️ |
| Search joined rooms | :x: ([#16]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Search joined rooms | ([#16]) | ✔️ | ❌ | ✔️ |
| Room user list | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Room user list | ✔️ | ✔️ | ✔️ | ✔️ |
| Display Room Description | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Display Room Description | ✔️ | ✔️ | ✔️ | ✔️ |
| Edit Room Description | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: | | Edit Room Description | ✔️ | ❌ | ✔️ | ✔️ |
| Highlights | :x: ([#8]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Highlights | ([#8]) | ✔️ | ✔️ | ✔️ |
| Pushrules | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: | | Pushrules | | ✔️ | ❌ | ✔️ |
| Send read markers | :x: ([#11]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Send read markers | ✔️ | ✔️ | ✔️ | ✔️ |
| Display read markers | :x: ([#11]) | :x: | :x: | :heavy_check_mark: | | Display read markers | ✔️ | ❌ | ❌ | ✔️ |
| Sending Invites | :x: ([#7]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Sending Invites | ✔️ | ✔️ | ✔️ | ✔️ |
| Accepting Invites | :x: ([#7]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Accepting Invites | ✔️ | ✔️ | ✔️ | ✔️ |
| Typing Notification | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Typing Notification | ✔️ | ✔️ | ✔️ | ✔️ |
| E2E | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | E2E | ✔️ | ✔️ | ✔️ | ✔️ |
| Replies | :x: ([#3]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Replies | ✔️ | ✔️ | ❌ | ✔️ |
| Attachment uploading | :x: ([#13]) | :x: | :heavy_check_mark: | :heavy_check_mark: | | Attachment uploading | ✔️ | ❌ | ✔️ | ✔️ |
| Attachment downloading | :x: ([#13]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Attachment downloading | ✔️ | ✔️ | ✔️ | ✔️ |
| Send stickers | :x: | :x: | :x: | :heavy_check_mark: | | Send stickers | | ❌ | ❌ | ✔️ |
| Send formatted messages (markdown) | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Send formatted messages (markdown) | ✔️ | ✔️ | ✔️ | ✔️ |
| Rich Text Editor for formatted messages | :x: | :x: | :x: | :heavy_check_mark: | | Rich Text Editor for formatted messages | | ❌ | ❌ | ✔️ |
| Display formatted messages | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Display formatted messages | ✔️ | ✔️ | ✔️ | ✔️ |
| Redacting | :x: ([#5]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | Redacting | ✔️ | ✔️ | ✔️ | ✔️ |
| Multiple Matrix Accounts | :heavy_check_mark: | :x: | :heavy_check_mark: | :x: | | Multiple Matrix Accounts | ✔️ | ❌ | ✔️ | ❌ |
| New user registration | :x: | :x: | :x: | :heavy_check_mark: | | New user registration | | ❌ | ❌ | ✔️ |
| VOIP | :x: | :x: | :x: | :heavy_check_mark: | | VOIP | | ❌ | ❌ | ✔️ |
| Reactions | :x: ([#2]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Reactions | ([#2]) | ✔️ | ❌ | ✔️ |
| Message editing | :x: ([#4]) | :heavy_check_mark: | :x: | :heavy_check_mark: | | Message editing | ✔️ | ✔️ | ❌ | ✔️ |
| Room upgrades | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: | | Room upgrades | | ✔️ | ❌ | ✔️ |
| Localisations | :x: | 1 | :x: | 44 | | Localisations | | 1 | ❌ | 44 |
| SSO Support | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | SSO Support | | ✔️ | ✔️ | ✔️ |
## License ## License

View File

@@ -8,7 +8,22 @@ use tracing::warn;
use matrix_sdk::{ use matrix_sdk::{
encryption::verification::SasVerification, encryption::verification::SasVerification,
ruma::{OwnedRoomId, OwnedUserId, RoomId}, room::Joined,
ruma::{
events::room::message::{
OriginalRoomMessageEvent,
Relation,
Replacement,
RoomMessageEvent,
RoomMessageEventContent,
},
events::tag::{TagName, Tags},
EventId,
OwnedEventId,
OwnedRoomId,
OwnedUserId,
RoomId,
},
}; };
use modalkit::{ use modalkit::{
@@ -41,12 +56,12 @@ use modalkit::{
}; };
use crate::{ use crate::{
message::{user_style, Message, Messages}, message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages},
worker::Requester, worker::Requester,
ApplicationSettings, ApplicationSettings,
}; };
const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(3); const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(2);
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum IambInfo {} pub enum IambInfo {}
@@ -60,43 +75,83 @@ pub enum VerifyAction {
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum SetRoomField { pub enum MessageAction {
Name(String), /// Cance the current reply or edit.
Topic(String), Cancel,
/// Download an attachment to the given path.
///
/// The [bool] argument controls whether to overwrite any already existing file at the
/// destination path.
Download(Option<String>, bool),
/// Edit a sent message.
Edit,
/// Redact a message.
Redact(Option<String>),
/// Reply to a message.
Reply,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RoomField {
Name,
Tag(TagName),
Topic,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum RoomAction { pub enum RoomAction {
InviteAccept,
InviteReject,
InviteSend(OwnedUserId),
Members(Box<CommandContext<ProgramContext>>), Members(Box<CommandContext<ProgramContext>>),
Set(SetRoomField), Set(RoomField, String),
Unset(RoomField),
} }
impl From<SetRoomField> for RoomAction { #[derive(Clone, Debug, Eq, PartialEq)]
fn from(act: SetRoomField) -> Self { pub enum SendAction {
RoomAction::Set(act) Submit,
} Upload(String),
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum IambAction { pub enum IambAction {
Message(MessageAction),
Room(RoomAction), Room(RoomAction),
Send(SendAction),
Verify(VerifyAction, String), Verify(VerifyAction, String),
VerifyRequest(String), VerifyRequest(String),
SendMessage(OwnedRoomId, String),
ToggleScrollbackFocus, ToggleScrollbackFocus,
} }
impl From<MessageAction> for IambAction {
fn from(act: MessageAction) -> Self {
IambAction::Message(act)
}
}
impl From<RoomAction> for IambAction { impl From<RoomAction> for IambAction {
fn from(act: RoomAction) -> Self { fn from(act: RoomAction) -> Self {
IambAction::Room(act) IambAction::Room(act)
} }
} }
impl From<SendAction> for IambAction {
fn from(act: SendAction) -> Self {
IambAction::Send(act)
}
}
impl ApplicationAction for IambAction { impl ApplicationAction for IambAction {
fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus { fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
match self { match self {
IambAction::Message(..) => SequenceStatus::Break,
IambAction::Room(..) => SequenceStatus::Break, IambAction::Room(..) => SequenceStatus::Break,
IambAction::SendMessage(..) => SequenceStatus::Break, IambAction::Send(..) => SequenceStatus::Break,
IambAction::ToggleScrollbackFocus => SequenceStatus::Break, IambAction::ToggleScrollbackFocus => SequenceStatus::Break,
IambAction::Verify(..) => SequenceStatus::Break, IambAction::Verify(..) => SequenceStatus::Break,
IambAction::VerifyRequest(..) => SequenceStatus::Break, IambAction::VerifyRequest(..) => SequenceStatus::Break,
@@ -105,8 +160,9 @@ impl ApplicationAction for IambAction {
fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus { fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
match self { match self {
IambAction::Message(..) => SequenceStatus::Atom,
IambAction::Room(..) => SequenceStatus::Atom, IambAction::Room(..) => SequenceStatus::Atom,
IambAction::SendMessage(..) => SequenceStatus::Atom, IambAction::Send(..) => SequenceStatus::Atom,
IambAction::ToggleScrollbackFocus => SequenceStatus::Atom, IambAction::ToggleScrollbackFocus => SequenceStatus::Atom,
IambAction::Verify(..) => SequenceStatus::Atom, IambAction::Verify(..) => SequenceStatus::Atom,
IambAction::VerifyRequest(..) => SequenceStatus::Atom, IambAction::VerifyRequest(..) => SequenceStatus::Atom,
@@ -115,8 +171,9 @@ impl ApplicationAction for IambAction {
fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus { fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
match self { match self {
IambAction::Message(..) => SequenceStatus::Ignore,
IambAction::Room(..) => SequenceStatus::Ignore, IambAction::Room(..) => SequenceStatus::Ignore,
IambAction::SendMessage(..) => SequenceStatus::Ignore, IambAction::Send(..) => SequenceStatus::Ignore,
IambAction::ToggleScrollbackFocus => SequenceStatus::Ignore, IambAction::ToggleScrollbackFocus => SequenceStatus::Ignore,
IambAction::Verify(..) => SequenceStatus::Ignore, IambAction::Verify(..) => SequenceStatus::Ignore,
IambAction::VerifyRequest(..) => SequenceStatus::Ignore, IambAction::VerifyRequest(..) => SequenceStatus::Ignore,
@@ -125,8 +182,9 @@ impl ApplicationAction for IambAction {
fn is_switchable<C: EditContext>(&self, _: &C) -> bool { fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
match self { match self {
IambAction::Message(..) => false,
IambAction::Room(..) => false, IambAction::Room(..) => false,
IambAction::SendMessage(..) => false, IambAction::Send(..) => false,
IambAction::ToggleScrollbackFocus => false, IambAction::ToggleScrollbackFocus => false,
IambAction::Verify(..) => false, IambAction::Verify(..) => false,
IambAction::VerifyRequest(..) => false, IambAction::VerifyRequest(..) => false,
@@ -134,6 +192,12 @@ impl ApplicationAction for IambAction {
} }
} }
impl From<RoomAction> for ProgramAction {
fn from(act: RoomAction) -> Self {
IambAction::from(act).into()
}
}
impl From<IambAction> for ProgramAction { impl From<IambAction> for ProgramAction {
fn from(act: IambAction) -> Self { fn from(act: IambAction) -> Self {
Action::Application(act) Action::Application(act)
@@ -150,9 +214,11 @@ pub type AsyncProgramStore = Arc<AsyncMutex<ProgramStore>>;
pub type IambResult<T> = UIResult<T, IambInfo>; pub type IambResult<T> = UIResult<T, IambInfo>;
pub type Receipts = HashMap<OwnedEventId, Vec<OwnedUserId>>;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum IambError { pub enum IambError {
#[error("Unknown room identifier: {0}")] #[error("Invalid user identifier: {0}")]
InvalidUserId(String), InvalidUserId(String),
#[error("Invalid verification user/device pair: {0}")] #[error("Invalid verification user/device pair: {0}")]
@@ -173,6 +239,24 @@ pub enum IambError {
#[error("Serialization/deserialization error: {0}")] #[error("Serialization/deserialization error: {0}")]
Serde(#[from] serde_json::Error), Serde(#[from] serde_json::Error),
#[error("Selected message does not have any attachments")]
NoAttachment,
#[error("No message currently selected")]
NoSelectedMessage,
#[error("Current window is not a room or space")]
NoSelectedRoomOrSpace,
#[error("Current window is not a room")]
NoSelectedRoom,
#[error("You do not have a current invitation to this room")]
NotInvited,
#[error("You need to join the room before you can do that")]
NotJoined,
#[error("Unknown room identifier: {0}")] #[error("Unknown room identifier: {0}")]
UnknownRoom(OwnedRoomId), UnknownRoom(OwnedRoomId),
@@ -199,13 +283,76 @@ pub enum RoomFetchStatus {
#[derive(Default)] #[derive(Default)]
pub struct RoomInfo { pub struct RoomInfo {
pub name: Option<String>, pub name: Option<String>,
pub tags: Option<Tags>,
pub keys: HashMap<OwnedEventId, MessageKey>,
pub messages: Messages, pub messages: Messages,
pub receipts: HashMap<OwnedEventId, Vec<OwnedUserId>>,
pub read_till: Option<OwnedEventId>,
pub fetch_id: RoomFetchStatus, pub fetch_id: RoomFetchStatus,
pub fetch_last: Option<Instant>, pub fetch_last: Option<Instant>,
pub users_typing: Option<(Instant, Vec<OwnedUserId>)>, pub users_typing: Option<(Instant, Vec<OwnedUserId>)>,
} }
impl RoomInfo { impl RoomInfo {
pub fn get_event(&self, event_id: &EventId) -> Option<&Message> {
self.messages.get(self.keys.get(event_id)?)
}
pub fn insert_edit(&mut self, msg: Replacement) {
let event_id = msg.event_id;
let new_content = msg.new_content;
let key = if let Some(k) = self.keys.get(&event_id) {
k
} else {
return;
};
let msg = if let Some(msg) = self.messages.get_mut(key) {
msg
} else {
return;
};
match &mut msg.event {
MessageEvent::Original(orig) => {
orig.content = *new_content;
},
MessageEvent::Local(_, content) => {
*content = new_content;
},
MessageEvent::Redacted(_) => {
return;
},
}
}
pub fn insert_message(&mut self, msg: RoomMessageEvent) {
let event_id = msg.event_id().to_owned();
let key = (msg.origin_server_ts().into(), event_id.clone());
self.keys.insert(event_id.clone(), key.clone());
self.messages.insert(key, msg.into());
// Remove any echo.
let key = (MessageTimeStamp::LocalEcho, event_id);
let _ = self.messages.remove(&key);
}
pub fn insert(&mut self, msg: RoomMessageEvent) {
match msg {
RoomMessageEvent::Original(OriginalRoomMessageEvent {
content:
RoomMessageEventContent { relates_to: Some(Relation::Replacement(repl)), .. },
..
}) => self.insert_edit(repl),
_ => self.insert_message(msg),
}
}
fn recently_fetched(&self) -> bool { fn recently_fetched(&self) -> bool {
self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE) self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE)
} }
@@ -222,24 +369,20 @@ impl RoomInfo {
} }
} }
fn get_typing_spans(&self) -> Spans { fn get_typing_spans<'a>(&'a self, settings: &'a ApplicationSettings) -> Spans<'a> {
let typers = self.get_typers(); let typers = self.get_typers();
let n = typers.len(); let n = typers.len();
match n { match n {
0 => Spans(vec![]), 0 => Spans(vec![]),
1 => { 1 => {
let user = typers[0].as_str(); let user = settings.get_user_span(typers[0].as_ref());
let user = Span::styled(user, user_style(user));
Spans(vec![user, Span::from(" is typing...")]) Spans(vec![user, Span::from(" is typing...")])
}, },
2 => { 2 => {
let user1 = typers[0].as_str(); let user1 = settings.get_user_span(typers[0].as_ref());
let user1 = Span::styled(user1, user_style(user1)); let user2 = settings.get_user_span(typers[1].as_ref());
let user2 = typers[1].as_str();
let user2 = Span::styled(user2, user_style(user2));
Spans(vec![ Spans(vec![
user1, user1,
@@ -274,7 +417,7 @@ impl RoomInfo {
let top = Rect::new(area.x, area.y, area.width, area.height - 1); let top = Rect::new(area.x, area.y, area.width, area.height - 1);
let bar = Rect::new(area.x, area.y + top.height, area.width, 1); let bar = Rect::new(area.x, area.y + top.height, area.width, 1);
Paragraph::new(self.get_typing_spans()) Paragraph::new(self.get_typing_spans(settings))
.alignment(Alignment::Center) .alignment(Alignment::Center)
.render(bar, buf); .render(bar, buf);
@@ -304,6 +447,10 @@ impl ChatStore {
} }
} }
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<Joined> {
self.worker.client.get_joined_room(room_id)
}
pub fn get_room_title(&self, room_id: &RoomId) -> String { pub fn get_room_title(&self, room_id: &RoomId) -> String {
self.rooms self.rooms
.get(room_id) .get(room_id)
@@ -312,6 +459,26 @@ impl ChatStore {
.unwrap_or_else(|| "Untitled Matrix Room".to_string()) .unwrap_or_else(|| "Untitled Matrix Room".to_string())
} }
pub async fn set_receipts(&mut self, receipts: Vec<(OwnedRoomId, Receipts)>) {
let mut updates = vec![];
for (room_id, receipts) in receipts.into_iter() {
if let Some(info) = self.rooms.get_mut(&room_id) {
info.receipts = receipts;
if let Some(read_till) = info.read_till.take() {
updates.push((room_id, read_till));
}
}
}
for (room_id, read_till) in updates.into_iter() {
if let Some(room) = self.worker.client.get_joined_room(&room_id) {
let _ = room.read_receipt(read_till.as_ref()).await;
}
}
}
pub fn mark_for_load(&mut self, room_id: OwnedRoomId) { pub fn mark_for_load(&mut self, room_id: OwnedRoomId) {
self.need_load.insert(room_id); self.need_load.insert(room_id);
} }
@@ -340,9 +507,7 @@ impl ChatStore {
match res { match res {
Ok((fetch_id, msgs)) => { Ok((fetch_id, msgs)) => {
for msg in msgs.into_iter() { for msg in msgs.into_iter() {
let key = (msg.origin_server_ts().into(), msg.event_id().to_owned()); info.insert(msg);
info.messages.insert(key, Message::from(msg));
} }
info.fetch_id = info.fetch_id =
@@ -448,11 +613,14 @@ impl ApplicationInfo for IambInfo {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use crate::config::user_style_from_color;
use crate::tests::*; use crate::tests::*;
use modalkit::tui::style::Color;
#[test] #[test]
fn test_typing_spans() { fn test_typing_spans() {
let mut info = RoomInfo::default(); let mut info = RoomInfo::default();
let settings = mock_settings();
let users0 = vec![]; let users0 = vec![];
let users1 = vec![TEST_USER1.clone()]; let users1 = vec![TEST_USER1.clone()];
@@ -473,18 +641,18 @@ pub mod tests {
// Nothing set. // Nothing set.
assert_eq!(info.users_typing, None); assert_eq!(info.users_typing, None);
assert_eq!(info.get_typing_spans(), Spans(vec![])); assert_eq!(info.get_typing_spans(&settings), Spans(vec![]));
// Empty typing list. // Empty typing list.
info.set_typing(users0); info.set_typing(users0);
assert!(info.users_typing.is_some()); assert!(info.users_typing.is_some());
assert_eq!(info.get_typing_spans(), Spans(vec![])); assert_eq!(info.get_typing_spans(&settings), Spans(vec![]));
// Single user typing. // Single user typing.
info.set_typing(users1); info.set_typing(users1);
assert!(info.users_typing.is_some()); assert!(info.users_typing.is_some());
assert_eq!( assert_eq!(
info.get_typing_spans(), info.get_typing_spans(&settings),
Spans(vec![ Spans(vec![
Span::styled("@user1:example.com", user_style("@user1:example.com")), Span::styled("@user1:example.com", user_style("@user1:example.com")),
Span::from(" is typing...") Span::from(" is typing...")
@@ -495,7 +663,7 @@ pub mod tests {
info.set_typing(users2); info.set_typing(users2);
assert!(info.users_typing.is_some()); assert!(info.users_typing.is_some());
assert_eq!( assert_eq!(
info.get_typing_spans(), info.get_typing_spans(&settings),
Spans(vec![ Spans(vec![
Span::styled("@user1:example.com", user_style("@user1:example.com")), Span::styled("@user1:example.com", user_style("@user1:example.com")),
Span::raw(" and "), Span::raw(" and "),
@@ -507,11 +675,22 @@ pub mod tests {
// Four users typing. // Four users typing.
info.set_typing(users4); info.set_typing(users4);
assert!(info.users_typing.is_some()); assert!(info.users_typing.is_some());
assert_eq!(info.get_typing_spans(), Spans::from("Several people are typing...")); assert_eq!(info.get_typing_spans(&settings), Spans::from("Several people are typing..."));
// Five users typing. // Five users typing.
info.set_typing(users5); info.set_typing(users5);
assert!(info.users_typing.is_some()); assert!(info.users_typing.is_some());
assert_eq!(info.get_typing_spans(), Spans::from("Many people are typing...")); assert_eq!(info.get_typing_spans(&settings), Spans::from("Many people are typing..."));
// Test that USER5 gets rendered using the configured color and name.
info.set_typing(vec![TEST_USER5.clone()]);
assert!(info.users_typing.is_some());
assert_eq!(
info.get_typing_spans(&settings),
Spans(vec![
Span::styled("USER 5", user_style_from_color(Color::Black)),
Span::from(" is typing...")
])
);
} }
} }

View File

@@ -1,3 +1,7 @@
use std::convert::TryFrom;
use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
use modalkit::{ use modalkit::{
editing::base::OpenTarget, editing::base::OpenTarget,
env::vim::command::{CommandContext, CommandDescription}, env::vim::command::{CommandContext, CommandDescription},
@@ -8,17 +12,90 @@ use modalkit::{
use crate::base::{ use crate::base::{
IambAction, IambAction,
IambId, IambId,
MessageAction,
ProgramCommand, ProgramCommand,
ProgramCommands, ProgramCommands,
ProgramContext, ProgramContext,
RoomAction, RoomAction,
SetRoomField, RoomField,
SendAction,
VerifyAction, VerifyAction,
}; };
type ProgContext = CommandContext<ProgramContext>; type ProgContext = CommandContext<ProgramContext>;
type ProgResult = CommandResult<ProgramCommand>; type ProgResult = CommandResult<ProgramCommand>;
/// Convert strings the user types into a tag name.
fn tag_name(name: String) -> Result<TagName, CommandError> {
let tag = match name.as_str() {
"fav" | "favorite" | "favourite" | "m.favourite" => TagName::Favorite,
"low" | "lowpriority" | "low_priority" | "low-priority" | "m.lowpriority" => {
TagName::LowPriority
},
"servernotice" | "server_notice" | "server-notice" | "m.server_notice" => {
TagName::ServerNotice
},
_ => {
if let Ok(tag) = name.parse() {
TagName::User(tag)
} else {
let msg = format!("Invalid user tag name: {}", name);
return Err(CommandError::Error(msg));
}
},
};
Ok(tag)
}
fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let args = desc.arg.strings()?;
if args.is_empty() {
return Err(CommandError::InvalidArgument);
}
let ract = match args[0].as_str() {
"accept" => {
if args.len() != 1 {
return Err(CommandError::InvalidArgument);
}
RoomAction::InviteAccept
},
"reject" => {
if args.len() != 1 {
return Err(CommandError::InvalidArgument);
}
RoomAction::InviteReject
},
"send" => {
if args.len() != 2 {
return Err(CommandError::InvalidArgument);
}
if let Ok(user) = OwnedUserId::try_from(args[1].as_str()) {
RoomAction::InviteSend(user)
} else {
let msg = format!("Invalid user identifier: {}", args[1]);
let err = CommandError::Error(msg);
return Err(err);
}
},
_ => {
return Err(CommandError::InvalidArgument);
},
};
let iact = IambAction::from(ract);
let step = CommandStep::Continue(iact.into(), ctx.context.take());
return Ok(step);
}
fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let mut args = desc.arg.strings()?; let mut args = desc.arg.strings()?;
@@ -80,6 +157,52 @@ fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
return Ok(step); return Ok(step);
} }
fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
if !desc.arg.text.is_empty() {
return Result::Err(CommandError::InvalidArgument);
}
let ract = IambAction::from(MessageAction::Cancel);
let step = CommandStep::Continue(ract.into(), ctx.context.take());
return Ok(step);
}
fn iamb_edit(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
if !desc.arg.text.is_empty() {
return Result::Err(CommandError::InvalidArgument);
}
let ract = IambAction::from(MessageAction::Edit);
let step = CommandStep::Continue(ract.into(), ctx.context.take());
return Ok(step);
}
fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let args = desc.arg.strings()?;
if args.len() > 1 {
return Result::Err(CommandError::InvalidArgument);
}
let ract = IambAction::from(MessageAction::Redact(args.into_iter().next()));
let step = CommandStep::Continue(ract.into(), ctx.context.take());
return Ok(step);
}
fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
if !desc.arg.text.is_empty() {
return Result::Err(CommandError::InvalidArgument);
}
let ract = IambAction::from(MessageAction::Reply);
let step = CommandStep::Continue(ract.into(), ctx.context.take());
return Ok(step);
}
fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
if !desc.arg.text.is_empty() { if !desc.arg.text.is_empty() {
return Result::Err(CommandError::InvalidArgument); return Result::Err(CommandError::InvalidArgument);
@@ -126,22 +249,46 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
return Ok(step); return Ok(step);
} }
fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let mut args = desc.arg.strings()?; let mut args = desc.arg.strings()?;
if args.len() != 2 { if args.len() < 2 {
return Result::Err(CommandError::InvalidArgument); return Result::Err(CommandError::InvalidArgument);
} }
let field = args.remove(0); let field = args.remove(0);
let value = args.remove(0); let action = args.remove(0);
let act: IambAction = match field.as_str() { if args.len() > 1 {
"room.name" => RoomAction::Set(SetRoomField::Name(value)).into(),
"room.topic" => RoomAction::Set(SetRoomField::Topic(value)).into(),
_ => {
return Result::Err(CommandError::InvalidArgument); return Result::Err(CommandError::InvalidArgument);
}, }
let act: IambAction = match (field.as_str(), action.as_str(), args.pop()) {
// :room name set <room-name>
("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
// :room name unset
("name", "unset", None) => RoomAction::Unset(RoomField::Name).into(),
("name", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
// :room topic set <topic>
("topic", "set", Some(s)) => RoomAction::Set(RoomField::Topic, s).into(),
("topic", "set", None) => return Result::Err(CommandError::InvalidArgument),
// :room topic unset
("topic", "unset", None) => RoomAction::Unset(RoomField::Topic).into(),
("topic", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
// :room tag set <tag-name>
("tag", "set", Some(s)) => RoomAction::Set(RoomField::Tag(tag_name(s)?), "".into()).into(),
("tag", "set", None) => return Result::Err(CommandError::InvalidArgument),
// :room tag unset <tag-name>
("tag", "unset", Some(s)) => RoomAction::Unset(RoomField::Tag(tag_name(s)?)).into(),
("tag", "unset", None) => return Result::Err(CommandError::InvalidArgument),
_ => return Result::Err(CommandError::InvalidArgument),
}; };
let step = CommandStep::Continue(act.into(), ctx.context.take()); let step = CommandStep::Continue(act.into(), ctx.context.take());
@@ -149,13 +296,48 @@ fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
return Ok(step); return Ok(step);
} }
fn iamb_upload(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let mut args = desc.arg.strings()?;
if args.len() != 1 {
return Result::Err(CommandError::InvalidArgument);
}
let sact = SendAction::Upload(args.remove(0));
let iact = IambAction::from(sact);
let step = CommandStep::Continue(iact.into(), ctx.context.take());
return Ok(step);
}
fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
let mut args = desc.arg.strings()?;
if args.len() > 1 {
return Result::Err(CommandError::InvalidArgument);
}
let mact = MessageAction::Download(args.pop(), desc.bang);
let iact = IambAction::from(mact);
let step = CommandStep::Continue(iact.into(), ctx.context.take());
return Ok(step);
}
fn add_iamb_commands(cmds: &mut ProgramCommands) { fn add_iamb_commands(cmds: &mut ProgramCommands) {
cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms }); cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit });
cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join }); cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members }); cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members });
cmds.add_command(ProgramCommand { names: vec!["redact".into()], f: iamb_redact });
cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply });
cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms }); cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set }); cmds.add_command(ProgramCommand { names: vec!["room".into()], f: iamb_room });
cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces }); cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
cmds.add_command(ProgramCommand { names: vec!["upload".into()], f: iamb_upload });
cmds.add_command(ProgramCommand { names: vec!["verify".into()], f: iamb_verify }); cmds.add_command(ProgramCommand { names: vec!["verify".into()], f: iamb_verify });
cmds.add_command(ProgramCommand { names: vec!["welcome".into()], f: iamb_welcome }); cmds.add_command(ProgramCommand { names: vec!["welcome".into()], f: iamb_welcome });
} }
@@ -171,6 +353,7 @@ pub fn setup_commands() -> ProgramCommands {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use matrix_sdk::ruma::user_id;
use modalkit::editing::action::WindowAction; use modalkit::editing::action::WindowAction;
#[test] #[test]
@@ -241,46 +424,284 @@ mod tests {
} }
#[test] #[test]
fn test_cmd_set() { fn test_cmd_room_invalid() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room foo", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room set topic", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_room_topic_set() {
let mut cmds = setup_commands(); let mut cmds = setup_commands();
let ctx = ProgramContext::default(); let ctx = ProgramContext::default();
let res = cmds let res = cmds
.input_cmd("set room.topic \"Lots of fun discussion!\"", ctx.clone()) .input_cmd("room topic set \"Lots of fun discussion!\"", ctx.clone())
.unwrap(); .unwrap();
let act = IambAction::Room(SetRoomField::Topic("Lots of fun discussion!".into()).into()); let act = RoomAction::Set(RoomField::Topic, "Lots of fun discussion!".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]); assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds let res = cmds
.input_cmd("set room.topic The\\ Discussion\\ Room", ctx.clone()) .input_cmd("room topic set The\\ Discussion\\ Room", ctx.clone())
.unwrap(); .unwrap();
let act = IambAction::Room(SetRoomField::Topic("The Discussion Room".into()).into()); let act = RoomAction::Set(RoomField::Topic, "The Discussion Room".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]); assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("set room.topic Development", ctx.clone()).unwrap(); let res = cmds.input_cmd("room topic set Development", ctx.clone()).unwrap();
let act = IambAction::Room(SetRoomField::Topic("Development".into()).into()); let act = RoomAction::Set(RoomField::Topic, "Development".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]); assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("set room.name Development", ctx.clone()).unwrap(); let res = cmds.input_cmd("room topic", ctx.clone());
let act = IambAction::Room(SetRoomField::Name("Development".into()).into()); assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room topic set", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room topic set A B C", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_room_name_invalid() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room name", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room name foo", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_room_name_set() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room name set Development", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Name, "Development".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]); assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds let res = cmds
.input_cmd("set room.name \"Application Development\"", ctx.clone()) .input_cmd("room name set \"Application Development\"", ctx.clone())
.unwrap(); .unwrap();
let act = IambAction::Room(SetRoomField::Name("Application Development".into()).into()); let act = RoomAction::Set(RoomField::Name, "Application Development".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]); assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("set", ctx.clone()); let res = cmds.input_cmd("room name set", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_room_name_unset() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room name unset", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Name);
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room name unset foo", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_room_tag_set() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room tag set favourite", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set favorite", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set fav", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set low_priority", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set low-priority", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set low", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set servernotice", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set u.custom-tag", ctx.clone()).unwrap();
let act = RoomAction::Set(
RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())),
"".into(),
);
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag set u.irc", ctx.clone()).unwrap();
let act =
RoomAction::Set(RoomField::Tag(TagName::User("u.irc".parse().unwrap())), "".into());
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument)); assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("set room.name", ctx.clone()); let res = cmds.input_cmd("room tag set", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument)); assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("set room.topic", ctx.clone()); let res = cmds.input_cmd("room tag set unknown", ctx.clone());
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
let res = cmds.input_cmd("room tag set needs-leading-u-dot", ctx.clone());
assert_eq!(
res,
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
);
}
#[test]
fn test_cmd_room_tag_unset() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("room tag unset favourite", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset favorite", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset fav", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset low_priority", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset low-priority", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset low", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset servernotice", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset u.custom-tag", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag unset u.irc", ctx.clone()).unwrap();
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.irc".parse().unwrap())));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("room tag", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument)); assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("set room.topic A B C", ctx.clone()); let res = cmds.input_cmd("room tag set", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("room tag unset unknown", ctx.clone());
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
let res = cmds.input_cmd("room tag unset needs-leading-u-dot", ctx.clone());
assert_eq!(
res,
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
);
}
#[test]
fn test_cmd_invite() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("invite accept", ctx.clone()).unwrap();
let act = IambAction::Room(RoomAction::InviteAccept);
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("invite reject", ctx.clone()).unwrap();
let act = IambAction::Room(RoomAction::InviteReject);
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("invite send @user:example.com", ctx.clone()).unwrap();
let act =
IambAction::Room(RoomAction::InviteSend(user_id!("@user:example.com").to_owned()));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("invite", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("invite foo", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("invite accept @user:example.com", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("invite reject @user:example.com", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("invite send", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
let res = cmds.input_cmd("invite @user:example.com", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument));
}
#[test]
fn test_cmd_redact() {
let mut cmds = setup_commands();
let ctx = ProgramContext::default();
let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
let act = IambAction::Message(MessageAction::Redact(None));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("redact Removed", ctx.clone()).unwrap();
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("redact \"Removed\"", ctx.clone()).unwrap();
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
assert_eq!(res, vec![(act.into(), ctx.clone())]);
let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
assert_eq!(res, Err(CommandError::InvalidArgument)); assert_eq!(res, Err(CommandError::InvalidArgument));
} }
} }

View File

@@ -1,14 +1,23 @@
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
use std::fs::File; use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::BufReader; use std::io::BufReader;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process; use std::process;
use clap::Parser; use clap::Parser;
use matrix_sdk::ruma::OwnedUserId; use matrix_sdk::ruma::{OwnedUserId, UserId};
use serde::Deserialize; use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
use url::Url; use url::Url;
use modalkit::tui::{
style::{Color, Modifier as StyleModifier, Style},
text::Span,
};
macro_rules! usage { macro_rules! usage {
( $($args: tt)* ) => { ( $($args: tt)* ) => {
println!($($args)*); println!($($args)*);
@@ -16,6 +25,34 @@ macro_rules! usage {
} }
} }
const COLORS: [Color; 13] = [
Color::Blue,
Color::Cyan,
Color::Green,
Color::LightBlue,
Color::LightGreen,
Color::LightCyan,
Color::LightMagenta,
Color::LightRed,
Color::LightYellow,
Color::Magenta,
Color::Red,
Color::Reset,
Color::Yellow,
];
pub fn user_color(user: &str) -> Color {
let mut hasher = DefaultHasher::new();
user.hash(&mut hasher);
let color = hasher.finish() as usize % COLORS.len();
COLORS[color]
}
pub fn user_style_from_color(color: Color) -> Style {
Style::default().fg(color).add_modifier(StyleModifier::BOLD)
}
fn is_profile_char(c: char) -> bool { fn is_profile_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '.' || c == '-' c.is_ascii_alphanumeric() || c == '.' || c == '-'
} }
@@ -69,30 +106,116 @@ pub enum ConfigError {
Invalid(#[from] serde_json::Error), Invalid(#[from] serde_json::Error),
} }
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UserColor(pub Color);
pub struct UserColorVisitor;
impl<'de> Visitor<'de> for UserColorVisitor {
type Value = UserColor;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid color")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: SerdeError,
{
match value {
"none" => Ok(UserColor(Color::Reset)),
"red" => Ok(UserColor(Color::Red)),
"black" => Ok(UserColor(Color::Black)),
"green" => Ok(UserColor(Color::Green)),
"yellow" => Ok(UserColor(Color::Yellow)),
"blue" => Ok(UserColor(Color::Blue)),
"magenta" => Ok(UserColor(Color::Magenta)),
"cyan" => Ok(UserColor(Color::Cyan)),
"gray" => Ok(UserColor(Color::Gray)),
"dark-gray" => Ok(UserColor(Color::DarkGray)),
"light-red" => Ok(UserColor(Color::LightRed)),
"light-green" => Ok(UserColor(Color::LightGreen)),
"light-yellow" => Ok(UserColor(Color::LightYellow)),
"light-blue" => Ok(UserColor(Color::LightBlue)),
"light-magenta" => Ok(UserColor(Color::LightMagenta)),
"light-cyan" => Ok(UserColor(Color::LightCyan)),
"white" => Ok(UserColor(Color::White)),
_ => Err(E::custom("Could not parse color")),
}
}
}
impl<'de> Deserialize<'de> for UserColor {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(UserColorVisitor)
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct UserDisplayTunables {
pub color: Option<UserColor>,
pub name: Option<String>,
}
pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>;
fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<UserOverrides> {
match (a, b) {
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(Some(mut a), Some(b)) => {
for (k, v) in b {
a.insert(k, v);
}
Some(a)
},
(None, None) => None,
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct TunableValues { pub struct TunableValues {
pub typing_notice: bool, pub read_receipt_send: bool,
pub read_receipt_display: bool,
pub typing_notice_send: bool,
pub typing_notice_display: bool, pub typing_notice_display: bool,
pub users: UserOverrides,
pub default_room: Option<String>,
} }
#[derive(Clone, Default, Deserialize)] #[derive(Clone, Default, Deserialize)]
pub struct Tunables { pub struct Tunables {
pub typing_notice: Option<bool>, pub read_receipt_send: Option<bool>,
pub read_receipt_display: Option<bool>,
pub typing_notice_send: Option<bool>,
pub typing_notice_display: Option<bool>, pub typing_notice_display: Option<bool>,
pub users: Option<UserOverrides>,
pub default_room: Option<String>,
} }
impl Tunables { impl Tunables {
fn merge(self, other: Self) -> Self { fn merge(self, other: Self) -> Self {
Tunables { Tunables {
typing_notice: self.typing_notice.or(other.typing_notice), read_receipt_send: self.read_receipt_send.or(other.read_receipt_send),
read_receipt_display: self.read_receipt_display.or(other.read_receipt_display),
typing_notice_send: self.typing_notice_send.or(other.typing_notice_send),
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
users: merge_users(self.users, other.users),
default_room: self.default_room.or(other.default_room),
} }
} }
fn values(self) -> TunableValues { fn values(self) -> TunableValues {
TunableValues { TunableValues {
typing_notice: self.typing_notice.unwrap_or(true), read_receipt_send: self.read_receipt_send.unwrap_or(true),
typing_notice_display: self.typing_notice.unwrap_or(true), read_receipt_display: self.read_receipt_display.unwrap_or(true),
typing_notice_send: self.typing_notice_send.unwrap_or(true),
typing_notice_display: self.typing_notice_display.unwrap_or(true),
users: self.users.unwrap_or_default(),
default_room: self.default_room,
} }
} }
} }
@@ -123,7 +246,11 @@ impl Directories {
fn values(self) -> DirectoryValues { fn values(self) -> DirectoryValues {
let cache = self let cache = self
.cache .cache
.or_else(dirs::cache_dir) .or_else(|| {
let mut dir = dirs::cache_dir()?;
dir.push("iamb");
dir.into()
})
.expect("no dirs.cache value configured!"); .expect("no dirs.cache value configured!");
let logs = self.logs.unwrap_or_else(|| { let logs = self.logs.unwrap_or_else(|| {
@@ -255,11 +382,49 @@ impl ApplicationSettings {
Ok(settings) Ok(settings)
} }
pub fn get_user_char_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
let (color, c) = self
.tunables
.users
.get(user_id)
.map(|user| {
(
user.color.as_ref().map(|c| c.0),
user.name.as_ref().and_then(|s| s.chars().next()),
)
})
.unwrap_or_default();
let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
let style = user_style_from_color(color);
let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' '));
Span::styled(String::from(c), style)
}
pub fn get_user_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
let (color, name) = self
.tunables
.users
.get(user_id)
.map(|user| (user.color.as_ref().map(|c| c.0), user.name.clone().map(Cow::Owned)))
.unwrap_or_default();
let user_id = user_id.as_str();
let color = color.unwrap_or_else(|| user_color(user_id));
let style = user_style_from_color(color);
let name = name.unwrap_or(Cow::Borrowed(user_id));
Span::styled(name, style)
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use matrix_sdk::ruma::user_id;
#[test] #[test]
fn test_profile_name_invalid() { fn test_profile_name_invalid() {
@@ -283,4 +448,74 @@ mod tests {
assert_eq!(validate_profile_name("a.b-c"), true); assert_eq!(validate_profile_name("a.b-c"), true);
assert_eq!(validate_profile_name("a.B-c"), true); assert_eq!(validate_profile_name("a.B-c"), true);
} }
#[test]
fn test_merge_users() {
let a = None;
let b = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
color: Some(UserColor(Color::Red)),
name: Some("Hello".into()),
})]
.into_iter()
.collect::<HashMap<_, _>>();
let c = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
color: Some(UserColor(Color::Green)),
name: Some("World".into()),
})]
.into_iter()
.collect::<HashMap<_, _>>();
let res = merge_users(a.clone(), a.clone());
assert_eq!(res, None);
let res = merge_users(a.clone(), Some(b.clone()));
assert_eq!(res, Some(b.clone()));
let res = merge_users(Some(b.clone()), a.clone());
assert_eq!(res, Some(b.clone()));
let res = merge_users(Some(b.clone()), Some(b.clone()));
assert_eq!(res, Some(b.clone()));
let res = merge_users(Some(b.clone()), Some(c.clone()));
assert_eq!(res, Some(c.clone()));
let res = merge_users(Some(c.clone()), Some(b.clone()));
assert_eq!(res, Some(b.clone()));
}
#[test]
fn test_parse_tunables() {
let res: Tunables = serde_json::from_str("{}").unwrap();
assert_eq!(res.typing_notice_send, None);
assert_eq!(res.typing_notice_display, None);
assert_eq!(res.users, None);
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": true}").unwrap();
assert_eq!(res.typing_notice_send, Some(true));
assert_eq!(res.typing_notice_display, None);
assert_eq!(res.users, None);
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": false}").unwrap();
assert_eq!(res.typing_notice_send, Some(false));
assert_eq!(res.typing_notice_display, None);
assert_eq!(res.users, None);
let res: Tunables = serde_json::from_str("{\"users\": {}}").unwrap();
assert_eq!(res.typing_notice_send, None);
assert_eq!(res.typing_notice_display, None);
assert_eq!(res.users, Some(HashMap::new()));
let res: Tunables = serde_json::from_str(
"{\"users\": {\"@a:b.c\": {\"color\": \"black\", \"name\": \"Tim\"}}}",
)
.unwrap();
assert_eq!(res.typing_notice_send, None);
assert_eq!(res.typing_notice_display, None);
let users = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
color: Some(UserColor(Color::Black)),
name: Some("Tim".into()),
})];
assert_eq!(res.users, Some(users.into_iter().collect()));
}
} }

View File

@@ -9,6 +9,7 @@ use std::fs::{create_dir_all, File};
use std::io::{stdout, BufReader, Stdout}; use std::io::{stdout, BufReader, Stdout};
use std::ops::DerefMut; use std::ops::DerefMut;
use std::process; use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@@ -41,6 +42,7 @@ mod commands;
mod config; mod config;
mod keybindings; mod keybindings;
mod message; mod message;
mod util;
mod windows; mod windows;
mod worker; mod worker;
@@ -63,7 +65,6 @@ use crate::{
ProgramStore, ProgramStore,
}, },
config::{ApplicationSettings, Iamb}, config::{ApplicationSettings, Iamb},
message::{Message, MessageContent, MessageTimeStamp},
windows::IambWindow, windows::IambWindow,
worker::{ClientWorker, LoginStyle, Requester}, worker::{ClientWorker, LoginStyle, Requester},
}; };
@@ -99,6 +100,14 @@ use modalkit::{
}, },
}; };
const MIN_MSG_LOAD: u32 = 50;
fn msg_load_req(area: Rect) -> u32 {
let n = area.height as u32;
n.max(MIN_MSG_LOAD)
}
struct Application { struct Application {
store: AsyncProgramStore, store: AsyncProgramStore,
worker: Requester, worker: Requester,
@@ -129,7 +138,14 @@ impl Application {
let cmds = crate::commands::setup_commands(); let cmds = crate::commands::setup_commands();
let mut locked = store.lock().await; let mut locked = store.lock().await;
let win = IambWindow::open(IambId::Welcome, locked.deref_mut()).unwrap();
let win = settings
.tunables
.default_room
.and_then(|room| IambWindow::find(room, locked.deref_mut()).ok())
.or_else(|| IambWindow::open(IambId::Welcome, locked.deref_mut()).ok())
.unwrap();
let cmd = CommandBarState::new(IambBufferId::Command, locked.deref_mut()); let cmd = CommandBarState::new(IambBufferId::Command, locked.deref_mut());
let screen = ScreenState::new(win, cmd); let screen = ScreenState::new(win, cmd);
@@ -176,7 +192,7 @@ impl Application {
f.set_cursor(cx, cy); f.set_cursor(cx, cy);
} }
store.application.load_older(area.height as u32); store.application.load_older(msg_load_req(area));
})?; })?;
Ok(()) Ok(())
@@ -186,7 +202,8 @@ impl Application {
loop { loop {
self.redraw(false, self.store.clone().lock().await.deref_mut())?; self.redraw(false, self.store.clone().lock().await.deref_mut())?;
if !poll(Duration::from_millis(500))? { if !poll(Duration::from_secs(1))? {
// Redraw in case there's new messages to show.
continue; continue;
} }
@@ -226,7 +243,7 @@ impl Application {
} }
} }
fn action_run( async fn action_run(
&mut self, &mut self,
action: ProgramAction, action: ProgramAction,
ctx: ProgramContext, ctx: ProgramContext,
@@ -257,7 +274,7 @@ impl Application {
}, },
// Simple delegations. // Simple delegations.
Action::Application(act) => self.iamb_run(act, ctx, store)?, Action::Application(act) => self.iamb_run(act, ctx, store).await?,
Action::CommandBar(act) => self.screen.command_bar(&act, &ctx)?, Action::CommandBar(act) => self.screen.command_bar(&act, &ctx)?,
Action::Macro(act) => self.bindings.macro_command(&act, &ctx, store)?, Action::Macro(act) => self.bindings.macro_command(&act, &ctx, store)?,
Action::Scroll(style) => self.screen.scroll(&style, &ctx, store)?, Action::Scroll(style) => self.screen.scroll(&style, &ctx, store)?,
@@ -314,7 +331,7 @@ impl Application {
return Ok(info); return Ok(info);
} }
fn iamb_run( async fn iamb_run(
&mut self, &mut self,
action: IambAction, action: IambAction,
ctx: ProgramContext, ctx: ProgramContext,
@@ -327,24 +344,19 @@ impl Application {
None None
}, },
IambAction::Message(act) => {
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
},
IambAction::Room(act) => { IambAction::Room(act) => {
let acts = self.screen.current_window_mut()?.room_command(act, ctx, store)?; let acts = self.screen.current_window_mut()?.room_command(act, ctx, store).await?;
self.action_prepend(acts); self.action_prepend(acts);
None None
}, },
IambAction::Send(act) => {
IambAction::SendMessage(room_id, msg) => { self.screen.current_window_mut()?.send_command(act, ctx, store).await?
let (event_id, msg) = self.worker.send_message(room_id.clone(), msg)?;
let user = store.application.settings.profile.user_id.clone();
let info = store.application.get_room_info(room_id);
let key = (MessageTimeStamp::LocalEcho, event_id);
let msg = MessageContent::Original(msg.into());
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
info.messages.insert(key, msg);
None
}, },
IambAction::Verify(act, user_dev) => { IambAction::Verify(act, user_dev) => {
if let Some(sas) = store.application.verifications.get(&user_dev) { if let Some(sas) = store.application.verifications.get(&user_dev) {
self.worker.verify(act, sas.clone())? self.worker.verify(act, sas.clone())?
@@ -378,7 +390,7 @@ impl Application {
let mut keyskip = false; let mut keyskip = false;
while let Some((action, ctx)) = self.action_pop(keyskip) { while let Some((action, ctx)) = self.action_pop(keyskip) {
match self.action_run(action, ctx, locked.deref_mut()) { match self.action_run(action, ctx, locked.deref_mut()).await {
Ok(None) => { Ok(None) => {
// Continue processing. // Continue processing.
continue; continue;
@@ -408,7 +420,7 @@ impl Application {
} }
} }
fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<()> { async fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<()> {
println!("Logging in for {}...", settings.profile.user_id); println!("Logging in for {}...", settings.profile.user_id);
if settings.session_json.is_file() { if settings.session_json.is_file() {
@@ -447,38 +459,15 @@ fn print_exit<T: Display, N>(v: T) -> N {
process::exit(2); process::exit(2);
} }
#[tokio::main] async fn run(settings: ApplicationSettings) -> IambResult<()> {
async fn main() -> IambResult<()> {
// Parse command-line flags.
let iamb = Iamb::parse();
// Load configuration and set up the Matrix SDK.
let settings = ApplicationSettings::load(iamb).unwrap_or_else(print_exit);
// Set up the tracing subscriber so we can log client messages.
let log_prefix = format!("iamb-log-{}", settings.profile_name);
let log_dir = settings.dirs.logs.as_path();
create_dir_all(settings.matrix_dir.as_path())?;
create_dir_all(log_dir)?;
let appender = tracing_appender::rolling::daily(log_dir, log_prefix);
let (appender, _) = tracing_appender::non_blocking(appender);
let subscriber = FmtSubscriber::builder()
.with_writer(appender)
.with_max_level(Level::WARN)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
// Set up the async worker thread and global store. // Set up the async worker thread and global store.
let worker = ClientWorker::spawn(settings.clone()); let worker = ClientWorker::spawn(settings.clone()).await;
let store = ChatStore::new(worker.clone(), settings.clone()); let store = ChatStore::new(worker.clone(), settings.clone());
let store = Store::new(store); let store = Store::new(store);
let store = Arc::new(AsyncMutex::new(store)); let store = Arc::new(AsyncMutex::new(store));
worker.init(store.clone()); worker.init(store.clone());
login(worker, &settings).unwrap_or_else(print_exit); login(worker, &settings).await.unwrap_or_else(print_exit);
// Make sure panics clean up the terminal properly. // Make sure panics clean up the terminal properly.
let orig_hook = std::panic::take_hook(); let orig_hook = std::panic::take_hook();
@@ -495,5 +484,44 @@ async fn main() -> IambResult<()> {
// We can now run the application. // We can now run the application.
application.run().await?; application.run().await?;
Ok(())
}
fn main() -> IambResult<()> {
// Parse command-line flags.
let iamb = Iamb::parse();
// Load configuration and set up the Matrix SDK.
let settings = ApplicationSettings::load(iamb).unwrap_or_else(print_exit);
// Set up the tracing subscriber so we can log client messages.
let log_prefix = format!("iamb-log-{}", settings.profile_name);
let log_dir = settings.dirs.logs.as_path();
create_dir_all(settings.matrix_dir.as_path())?;
create_dir_all(log_dir)?;
let appender = tracing_appender::rolling::daily(log_dir, log_prefix);
let (appender, guard) = tracing_appender::non_blocking(appender);
let subscriber = FmtSubscriber::builder()
.with_writer(appender)
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
format!("iamb-worker-{}", id)
})
.build()
.unwrap();
rt.block_on(async move { run(settings).await })?;
drop(guard);
process::exit(0); process::exit(0);
} }

View File

@@ -1,650 +0,0 @@
use std::borrow::Cow;
use std::cmp::{Ord, Ordering, PartialOrd};
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
use std::str::Lines;
use chrono::{DateTime, NaiveDateTime, Utc};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use matrix_sdk::ruma::{
events::{
room::message::{MessageType, RoomMessageEventContent},
MessageLikeEvent,
},
MilliSecondsSinceUnixEpoch,
OwnedEventId,
OwnedUserId,
UInt,
};
use modalkit::tui::{
style::{Color, Modifier as StyleModifier, Style},
text::{Span, Spans, Text},
};
use modalkit::editing::{base::ViewportContext, cursor::Cursor};
use crate::base::{IambResult, RoomInfo};
pub type MessageEvent = MessageLikeEvent<RoomMessageEventContent>;
pub type MessageFetchResult = IambResult<(Option<String>, Vec<MessageEvent>)>;
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
pub type Messages = BTreeMap<MessageKey, Message>;
const COLORS: [Color; 13] = [
Color::Blue,
Color::Cyan,
Color::Green,
Color::LightBlue,
Color::LightGreen,
Color::LightCyan,
Color::LightMagenta,
Color::LightRed,
Color::LightYellow,
Color::Magenta,
Color::Red,
Color::Reset,
Color::Yellow,
];
const USER_GUTTER: usize = 30;
const TIME_GUTTER: usize = 12;
const MIN_MSG_LEN: usize = 30;
const USER_GUTTER_EMPTY: &str = " ";
const USER_GUTTER_EMPTY_SPAN: Span<'static> = Span {
content: Cow::Borrowed(USER_GUTTER_EMPTY),
style: Style {
fg: None,
bg: None,
add_modifier: StyleModifier::empty(),
sub_modifier: StyleModifier::empty(),
},
};
pub(crate) fn user_color(user: &str) -> Color {
let mut hasher = DefaultHasher::new();
user.hash(&mut hasher);
let color = hasher.finish() as usize % COLORS.len();
COLORS[color]
}
pub(crate) fn user_style(user: &str) -> Style {
Style::default().fg(user_color(user)).add_modifier(StyleModifier::BOLD)
}
struct WrappedLinesIterator<'a> {
iter: Lines<'a>,
curr: Option<&'a str>,
width: usize,
}
impl<'a> WrappedLinesIterator<'a> {
fn new(input: &'a str, width: usize) -> Self {
WrappedLinesIterator { iter: input.lines(), curr: None, width }
}
}
impl<'a> Iterator for WrappedLinesIterator<'a> {
type Item = (&'a str, usize);
fn next(&mut self) -> Option<Self::Item> {
if self.curr.is_none() {
self.curr = self.iter.next();
}
if let Some(s) = self.curr.take() {
let width = UnicodeWidthStr::width(s);
if width <= self.width {
return Some((s, width));
} else {
// Find where to split the line.
let mut width = 0;
let mut idx = 0;
for (i, g) in UnicodeSegmentation::grapheme_indices(s, true) {
let gw = UnicodeWidthStr::width(g);
idx = i;
if width + gw > self.width {
break;
}
width += gw;
}
self.curr = Some(&s[idx..]);
return Some((&s[..idx], width));
}
} else {
return None;
}
}
}
fn wrap(input: &str, width: usize) -> WrappedLinesIterator<'_> {
WrappedLinesIterator::new(input, width)
}
fn space(width: usize) -> String {
" ".repeat(width)
}
#[derive(thiserror::Error, Debug)]
pub enum TimeStampIntError {
#[error("Integer conversion error: {0}")]
IntError(#[from] std::num::TryFromIntError),
#[error("UInt conversion error: {0}")]
UIntError(<UInt as TryFrom<u64>>::Error),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MessageTimeStamp {
OriginServer(UInt),
LocalEcho,
}
impl MessageTimeStamp {
fn show(&self) -> Option<Span> {
match self {
MessageTimeStamp::OriginServer(ts) => {
let time = i64::from(*ts) / 1000;
let time = NaiveDateTime::from_timestamp_opt(time, 0)?;
let time = DateTime::<Utc>::from_utc(time, Utc);
let time = time.format("%T");
let time = format!(" [{}]", time);
Span::raw(time).into()
},
MessageTimeStamp::LocalEcho => None,
}
}
fn is_local_echo(&self) -> bool {
matches!(self, MessageTimeStamp::LocalEcho)
}
}
impl Ord for MessageTimeStamp {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(MessageTimeStamp::OriginServer(_), MessageTimeStamp::LocalEcho) => Ordering::Less,
(MessageTimeStamp::OriginServer(a), MessageTimeStamp::OriginServer(b)) => a.cmp(b),
(MessageTimeStamp::LocalEcho, MessageTimeStamp::OriginServer(_)) => Ordering::Greater,
(MessageTimeStamp::LocalEcho, MessageTimeStamp::LocalEcho) => Ordering::Equal,
}
}
}
impl PartialOrd for MessageTimeStamp {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
MessageTimeStamp::OriginServer(millis.0)
}
}
impl TryFrom<&MessageTimeStamp> for usize {
type Error = TimeStampIntError;
fn try_from(ts: &MessageTimeStamp) -> Result<Self, Self::Error> {
let n = match ts {
MessageTimeStamp::LocalEcho => 0,
MessageTimeStamp::OriginServer(u) => usize::try_from(u64::from(*u))?,
};
Ok(n)
}
}
impl TryFrom<usize> for MessageTimeStamp {
type Error = TimeStampIntError;
fn try_from(u: usize) -> Result<Self, Self::Error> {
if u == 0 {
Ok(MessageTimeStamp::LocalEcho)
} else {
let n = u64::try_from(u)?;
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
Ok(MessageTimeStamp::OriginServer(n))
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MessageCursor {
/// When timestamp is None, the corner is determined by moving backwards from
/// the most recently received message.
pub timestamp: Option<MessageKey>,
/// A row within the [Text] representation of a [Message].
pub text_row: usize,
}
impl MessageCursor {
pub fn new(timestamp: MessageKey, text_row: usize) -> Self {
MessageCursor { timestamp: Some(timestamp), text_row }
}
/// Get a cursor that refers to the most recent message.
pub fn latest() -> Self {
MessageCursor::default()
}
pub fn to_key<'a>(&'a self, info: &'a RoomInfo) -> Option<&'a MessageKey> {
if let Some(ref key) = self.timestamp {
Some(key)
} else {
Some(info.messages.last_key_value()?.0)
}
}
pub fn from_cursor(cursor: &Cursor, info: &RoomInfo) -> Option<Self> {
let ev_hash = u64::try_from(cursor.get_x()).ok()?;
let ev_term = OwnedEventId::try_from("$").ok()?;
let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
let start = (ts_start, ev_term);
let mut mc = None;
for ((ts, event_id), _) in info.messages.range(start..) {
let mut hasher = DefaultHasher::new();
event_id.hash(&mut hasher);
if hasher.finish() == ev_hash {
mc = Self::from((*ts, event_id.clone())).into();
break;
}
if mc.is_none() {
mc = Self::from((*ts, event_id.clone())).into();
}
if ts > &ts_start {
break;
}
}
return mc;
}
pub fn to_cursor(&self, info: &RoomInfo) -> Option<Cursor> {
let (ts, event_id) = self.to_key(info)?;
let y: usize = usize::try_from(ts).ok()?;
let mut hasher = DefaultHasher::new();
event_id.hash(&mut hasher);
let x = usize::try_from(hasher.finish()).ok()?;
Cursor::new(y, x).into()
}
}
impl From<Option<MessageKey>> for MessageCursor {
fn from(key: Option<MessageKey>) -> Self {
MessageCursor { timestamp: key, text_row: 0 }
}
}
impl From<MessageKey> for MessageCursor {
fn from(key: MessageKey) -> Self {
MessageCursor { timestamp: Some(key), text_row: 0 }
}
}
impl Ord for MessageCursor {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.timestamp, &other.timestamp) {
(None, None) => self.text_row.cmp(&other.text_row),
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(st), Some(ot)) => {
let pcmp = st.cmp(ot);
let tcmp = self.text_row.cmp(&other.text_row);
pcmp.then(tcmp)
},
}
}
}
impl PartialOrd for MessageCursor {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
#[derive(Clone)]
pub enum MessageContent {
Original(Box<RoomMessageEventContent>),
Redacted,
}
impl AsRef<str> for MessageContent {
fn as_ref(&self) -> &str {
match self {
MessageContent::Original(ev) => {
match &ev.msgtype {
MessageType::Text(content) => {
return content.body.as_ref();
},
MessageType::Emote(content) => {
return content.body.as_ref();
},
MessageType::Notice(content) => {
return content.body.as_str();
},
MessageType::ServerNotice(_) => {
// XXX: implement
return "[server notice]";
},
MessageType::VerificationRequest(_) => {
// XXX: implement
return "[verification request]";
},
MessageType::Audio(..) => {
return "[audio]";
},
MessageType::File(..) => {
return "[file]";
},
MessageType::Image(..) => {
return "[image]";
},
MessageType::Video(..) => {
return "[video]";
},
_ => return "[unknown message type]",
}
},
MessageContent::Redacted => "[redacted]",
}
}
}
#[derive(Clone)]
pub struct Message {
pub content: MessageContent,
pub sender: OwnedUserId,
pub timestamp: MessageTimeStamp,
}
impl Message {
pub fn new(content: MessageContent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
Message { content, sender, timestamp }
}
pub fn show(&self, selected: bool, vwctx: &ViewportContext<MessageCursor>) -> Text {
let width = vwctx.get_width();
let msg = self.as_ref();
let mut lines = vec![];
let mut style = Style::default();
if selected {
style = style.add_modifier(StyleModifier::REVERSED)
}
if self.timestamp.is_local_echo() {
style = style.add_modifier(StyleModifier::ITALIC);
}
if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
let lw = width - USER_GUTTER - TIME_GUTTER;
for (i, (line, w)) in wrap(msg, lw).enumerate() {
let line = Span::styled(line, style);
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
if i == 0 {
let user = self.show_sender(true);
if let Some(time) = self.timestamp.show() {
lines.push(Spans(vec![user, line, trailing, time]))
} else {
lines.push(Spans(vec![user, line, trailing]))
}
} else {
let space = USER_GUTTER_EMPTY_SPAN;
lines.push(Spans(vec![space, line, trailing]))
}
}
} else if USER_GUTTER + MIN_MSG_LEN <= width {
let lw = width - USER_GUTTER;
for (i, (line, w)) in wrap(msg, lw).enumerate() {
let line = Span::styled(line, style);
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
let prefix = if i == 0 {
self.show_sender(true)
} else {
USER_GUTTER_EMPTY_SPAN
};
lines.push(Spans(vec![prefix, line, trailing]))
}
} else {
lines.push(Spans::from(self.show_sender(false)));
for (line, _) in wrap(msg, width.saturating_sub(2)) {
let line = format!(" {}", line);
let line = Span::styled(line, style);
lines.push(Spans(vec![line]))
}
}
return Text { lines };
}
fn show_sender(&self, align_right: bool) -> Span {
let sender = self.sender.to_string();
let style = user_style(sender.as_str());
let sender = if align_right {
format!("{: >width$} ", sender, width = 28)
} else {
format!("{: <width$} ", sender, width = 28)
};
Span::styled(sender, style)
}
}
impl From<MessageEvent> for Message {
fn from(event: MessageEvent) -> Self {
match event {
MessageLikeEvent::Original(ev) => {
let content = MessageContent::Original(ev.content.into());
Message::new(content, ev.sender, ev.origin_server_ts.into())
},
MessageLikeEvent::Redacted(ev) => {
Message::new(MessageContent::Redacted, ev.sender, ev.origin_server_ts.into())
},
}
}
}
impl AsRef<str> for Message {
fn as_ref(&self) -> &str {
self.content.as_ref()
}
}
impl ToString for Message {
fn to_string(&self) -> String {
self.as_ref().to_string()
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::tests::*;
#[test]
fn test_wrapped_lines_ascii() {
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
let mut iter = wrap(s, 100);
assert_eq!(iter.next(), Some(("hello world!", 12)));
assert_eq!(iter.next(), Some(("abcdefghijklmnopqrstuvwxyz", 26)));
assert_eq!(iter.next(), Some(("goodbye", 7)));
assert_eq!(iter.next(), None);
let mut iter = wrap(s, 5);
assert_eq!(iter.next(), Some(("hello", 5)));
assert_eq!(iter.next(), Some((" worl", 5)));
assert_eq!(iter.next(), Some(("d!", 2)));
assert_eq!(iter.next(), Some(("abcde", 5)));
assert_eq!(iter.next(), Some(("fghij", 5)));
assert_eq!(iter.next(), Some(("klmno", 5)));
assert_eq!(iter.next(), Some(("pqrst", 5)));
assert_eq!(iter.next(), Some(("uvwxy", 5)));
assert_eq!(iter.next(), Some(("z", 1)));
assert_eq!(iter.next(), Some(("goodb", 5)));
assert_eq!(iter.next(), Some(("ye", 2)));
assert_eq!(iter.next(), None);
}
#[test]
fn test_wrapped_lines_unicode() {
let s = "";
let mut iter = wrap(s, 14);
assert_eq!(iter.next(), Some((s, 14)));
assert_eq!(iter.next(), None);
let mut iter = wrap(s, 5);
assert_eq!(iter.next(), Some(("", 4)));
assert_eq!(iter.next(), Some(("", 4)));
assert_eq!(iter.next(), Some(("", 4)));
assert_eq!(iter.next(), Some(("", 2)));
assert_eq!(iter.next(), None);
}
#[test]
fn test_mc_cmp() {
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
// Everything is equal to itself.
assert_eq!(mc1.cmp(&mc1), Ordering::Equal);
assert_eq!(mc2.cmp(&mc2), Ordering::Equal);
assert_eq!(mc3.cmp(&mc3), Ordering::Equal);
assert_eq!(mc4.cmp(&mc4), Ordering::Equal);
assert_eq!(mc5.cmp(&mc5), Ordering::Equal);
// Local echo is always greater than an origin server timestamp.
assert_eq!(mc1.cmp(&mc2), Ordering::Greater);
assert_eq!(mc1.cmp(&mc3), Ordering::Greater);
assert_eq!(mc1.cmp(&mc4), Ordering::Greater);
assert_eq!(mc1.cmp(&mc5), Ordering::Greater);
// mc2 is the smallest timestamp.
assert_eq!(mc2.cmp(&mc1), Ordering::Less);
assert_eq!(mc2.cmp(&mc3), Ordering::Less);
assert_eq!(mc2.cmp(&mc4), Ordering::Less);
assert_eq!(mc2.cmp(&mc5), Ordering::Less);
// mc3 should be less than mc4 because of its event ID.
assert_eq!(mc3.cmp(&mc1), Ordering::Less);
assert_eq!(mc3.cmp(&mc2), Ordering::Greater);
assert_eq!(mc3.cmp(&mc4), Ordering::Less);
assert_eq!(mc3.cmp(&mc5), Ordering::Less);
// mc4 should be greater than mc3 because of its event ID.
assert_eq!(mc4.cmp(&mc1), Ordering::Less);
assert_eq!(mc4.cmp(&mc2), Ordering::Greater);
assert_eq!(mc4.cmp(&mc3), Ordering::Greater);
assert_eq!(mc4.cmp(&mc5), Ordering::Less);
// mc5 is the greatest OriginServer timestamp.
assert_eq!(mc5.cmp(&mc1), Ordering::Less);
assert_eq!(mc5.cmp(&mc2), Ordering::Greater);
assert_eq!(mc5.cmp(&mc3), Ordering::Greater);
assert_eq!(mc5.cmp(&mc4), Ordering::Greater);
}
#[test]
fn test_mc_to_key() {
let info = mock_room();
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
let mc6 = MessageCursor::latest();
let k1 = mc1.to_key(&info).unwrap();
let k2 = mc2.to_key(&info).unwrap();
let k3 = mc3.to_key(&info).unwrap();
let k4 = mc4.to_key(&info).unwrap();
let k5 = mc5.to_key(&info).unwrap();
let k6 = mc6.to_key(&info).unwrap();
// These should all be equal to their MSGN_KEYs.
assert_eq!(k1, &MSG1_KEY.clone());
assert_eq!(k2, &MSG2_KEY.clone());
assert_eq!(k3, &MSG3_KEY.clone());
assert_eq!(k4, &MSG4_KEY.clone());
assert_eq!(k5, &MSG5_KEY.clone());
// MessageCursor::latest() turns into the largest key (our local echo message).
assert_eq!(k6, &MSG1_KEY.clone());
// MessageCursor::latest() fails to convert for a room w/o messages.
let info_empty = RoomInfo::default();
assert_eq!(mc6.to_key(&info_empty), None);
}
#[test]
fn test_mc_to_from_cursor() {
let info = mock_room();
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
let mc6 = MessageCursor::latest();
let identity = |mc: &MessageCursor| {
let c = mc.to_cursor(&info).unwrap();
MessageCursor::from_cursor(&c, &info).unwrap()
};
// These should all convert to a Cursor and back to the original value.
assert_eq!(identity(&mc1), mc1);
assert_eq!(identity(&mc2), mc2);
assert_eq!(identity(&mc3), mc3);
assert_eq!(identity(&mc4), mc4);
assert_eq!(identity(&mc5), mc5);
// MessageCursor::latest() should point at the most recent message after conversion.
assert_eq!(identity(&mc6), mc1);
}
}

942
src/message/html.rs Normal file
View File

@@ -0,0 +1,942 @@
//! # Rendering for formatted bodies
//!
//! This module contains the code for rendering messages that contained an
//! "org.matrix.custom.html"-formatted body.
//!
//! The Matrix specification recommends limiting rendered tags and attributes to a safe subset of
//! HTML. You can read more in section 11.2.1.1, "m.room.message msgtypes":
//!
//! https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes
//!
//! This isn't as important for iamb, since it isn't a browser environment, but we do still map
//! input onto an enum of the safe list of tags to keep it easy to understand and process.
use std::ops::Deref;
use css_color_parser::Color as CssColor;
use markup5ever_rcdom::{Handle, NodeData, RcDom};
use unicode_segmentation::UnicodeSegmentation;
use html5ever::{
driver::{parse_fragment, ParseOpts},
interface::{Attribute, QualName},
local_name,
namespace_url,
ns,
tendril::{StrTendril, TendrilSink},
};
use modalkit::tui::{
layout::Alignment,
style::{Color, Modifier as StyleModifier, Style},
symbols::line,
text::{Span, Spans, Text},
};
use crate::{
message::printer::TextPrinter,
util::{join_cell_text, space_text},
};
struct BulletIterator {
style: ListStyle,
pos: usize,
len: usize,
}
impl BulletIterator {
fn width(&self) -> usize {
match self.style {
ListStyle::Unordered => 2,
ListStyle::Ordered => self.len.to_string().len() + 2,
}
}
}
impl Iterator for BulletIterator {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
if self.pos == self.len {
return None;
}
self.pos += 1;
let bullet = match self.style {
ListStyle::Unordered => "- ".to_string(),
ListStyle::Ordered => {
let w = self.len.to_string().len();
format!("{: >w$}. ", self.pos, w = w)
},
};
return Some(bullet);
}
}
#[derive(Clone, Copy, Debug)]
pub enum ListStyle {
Ordered,
Unordered,
}
impl ListStyle {
fn bullets(&self, len: usize) -> BulletIterator {
BulletIterator { style: *self, pos: 0, len }
}
}
pub type StyleTreeChildren = Vec<StyleTreeNode>;
pub enum CellType {
Data,
Header,
}
pub struct TableRow {
cells: Vec<(CellType, StyleTreeNode)>,
}
impl TableRow {
fn columns(&self) -> usize {
self.cells.len()
}
}
pub struct TableSection {
rows: Vec<TableRow>,
}
impl TableSection {
fn columns(&self) -> usize {
self.rows.iter().map(TableRow::columns).max().unwrap_or(0)
}
}
pub struct Table {
caption: Option<Box<StyleTreeNode>>,
sections: Vec<TableSection>,
}
impl Table {
fn columns(&self) -> usize {
self.sections.iter().map(TableSection::columns).max().unwrap_or(0)
}
fn to_text(&self, width: usize, style: Style) -> Text {
let mut text = Text::default();
let columns = self.columns();
let cell_total = width.saturating_sub(columns).saturating_sub(1);
let cell_min = cell_total / columns;
let mut cell_slop = cell_total - cell_min * columns;
let cell_widths = (0..columns)
.into_iter()
.map(|_| {
let slopped = cell_slop.min(1);
cell_slop -= slopped;
cell_min + slopped
})
.collect::<Vec<_>>();
let mut nrows = 0;
if let Some(caption) = &self.caption {
let subw = width.saturating_sub(6);
let mut printer = TextPrinter::new(subw, style, true).align(Alignment::Center);
caption.print(&mut printer, style);
for mut line in printer.finish().lines {
line.0.insert(0, Span::styled(" ", style));
line.0.push(Span::styled(" ", style));
text.lines.push(line);
}
}
for section in self.sections.iter() {
for row in section.rows.iter() {
let mut ruler = String::new();
for (i, w) in cell_widths.iter().enumerate() {
let cross = match (nrows, i) {
(0, 0) => line::TOP_LEFT,
(0, _) => line::HORIZONTAL_DOWN,
(_, 0) => line::VERTICAL_RIGHT,
(_, _) => line::CROSS,
};
ruler.push_str(cross);
for _ in 0..*w {
ruler.push_str(line::HORIZONTAL);
}
}
if nrows == 0 {
ruler.push_str(line::TOP_RIGHT);
} else {
ruler.push_str(line::VERTICAL_LEFT);
}
text.lines.push(Spans(vec![Span::styled(ruler, style)]));
let cells = cell_widths
.iter()
.enumerate()
.map(|(i, w)| {
let text = if let Some((kind, cell)) = row.cells.get(i) {
let style = match kind {
CellType::Header => style.add_modifier(StyleModifier::BOLD),
CellType::Data => style,
};
cell.to_text(*w, style)
} else {
space_text(*w, style)
};
(text, *w)
})
.collect();
let joined = join_cell_text(cells, Span::styled(line::VERTICAL, style), style);
text.lines.extend(joined.lines);
nrows += 1;
}
}
if nrows > 0 {
let mut ruler = String::new();
for (i, w) in cell_widths.iter().enumerate() {
let cross = if i == 0 {
line::BOTTOM_LEFT
} else {
line::HORIZONTAL_UP
};
ruler.push_str(cross);
for _ in 0..*w {
ruler.push_str(line::HORIZONTAL);
}
}
ruler.push_str(line::BOTTOM_RIGHT);
text.lines.push(Spans(vec![Span::styled(ruler, style)]));
}
text
}
}
pub enum StyleTreeNode {
Blockquote(Box<StyleTreeNode>),
Break,
Code(Box<StyleTreeNode>, Option<String>),
Header(Box<StyleTreeNode>, usize),
Image(Option<String>),
List(StyleTreeChildren, ListStyle),
Paragraph(Box<StyleTreeNode>),
Reply(Box<StyleTreeNode>),
Ruler,
Style(Box<StyleTreeNode>, Style),
Table(Table),
Text(String),
Sequence(StyleTreeChildren),
}
impl StyleTreeNode {
pub fn to_text(&self, width: usize, style: Style) -> Text {
let mut printer = TextPrinter::new(width, style, true);
self.print(&mut printer, style);
printer.finish()
}
pub fn print<'a>(&'a self, printer: &mut TextPrinter<'a>, style: Style) {
let width = printer.width();
match self {
StyleTreeNode::Blockquote(child) => {
let mut subp = printer.sub(4);
child.print(&mut subp, style);
for mut line in subp.finish() {
line.0.insert(0, Span::styled(" ", style));
printer.push_line(line);
}
},
StyleTreeNode::Code(child, _) => {
child.print(printer, style);
},
StyleTreeNode::Header(child, level) => {
let style = style.add_modifier(StyleModifier::BOLD);
let mut hashes = "#".repeat(*level);
hashes.push(' ');
printer.push_str(hashes, style);
child.print(printer, style);
},
StyleTreeNode::Image(None) => {},
StyleTreeNode::Image(Some(alt)) => {
printer.commit();
printer.push_str("Image Alt: ", Style::default());
printer.push_str(alt, Style::default());
printer.commit();
},
StyleTreeNode::List(children, lt) => {
let mut bullets = lt.bullets(children.len());
let liw = bullets.width();
for child in children {
let mut subp = printer.sub(liw);
let mut bullet = bullets.next();
child.print(&mut subp, style);
for mut line in subp.finish() {
let leading = if let Some(bullet) = bullet.take() {
Span::styled(bullet, style)
} else {
Span::styled(" ".repeat(liw), style)
};
line.0.insert(0, leading);
printer.push_line(line);
}
}
},
StyleTreeNode::Paragraph(child) => {
printer.push_break();
child.print(printer, style);
printer.commit();
},
StyleTreeNode::Reply(child) => {
if printer.hide_reply() {
return;
}
printer.push_break();
child.print(printer, style);
printer.commit();
},
StyleTreeNode::Ruler => {
printer.push_str(line::HORIZONTAL.repeat(width), style);
},
StyleTreeNode::Table(table) => {
let text = table.to_text(width, style);
printer.push_text(text);
},
StyleTreeNode::Break => {
printer.push_break();
},
StyleTreeNode::Text(s) => {
printer.push_str(s.as_str(), style);
},
StyleTreeNode::Style(child, patch) => child.print(printer, style.patch(*patch)),
StyleTreeNode::Sequence(children) => {
for child in children {
child.print(printer, style);
}
},
}
}
}
pub struct StyleTree {
children: StyleTreeChildren,
}
impl StyleTree {
pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> {
let mut printer = TextPrinter::new(width, style, hide_reply);
for child in self.children.iter() {
child.print(&mut printer, style);
}
printer.finish()
}
}
fn c2c(handles: &[Handle]) -> Vec<StyleTreeNode> {
handles.iter().flat_map(h2t).collect()
}
fn c2t(handles: &[Handle]) -> Box<StyleTreeNode> {
let node = StyleTreeNode::Sequence(c2c(handles));
Box::new(node)
}
fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> {
let node = hdl.deref();
if let NodeData::Element { name, .. } = &node.data {
if name.local.as_ref() != want {
return None;
}
let c = c2c(&node.children.borrow());
return Some(StyleTreeNode::Sequence(c));
} else {
return None;
}
}
fn li2t(hdl: &Handle) -> Option<StyleTreeNode> {
get_node(hdl, "li")
}
fn table_cell(hdl: &Handle) -> Option<(CellType, StyleTreeNode)> {
if let Some(node) = get_node(hdl, "th") {
return Some((CellType::Header, node));
}
Some((CellType::Data, get_node(hdl, "td")?))
}
fn table_row(hdl: &Handle) -> Option<TableRow> {
let node = hdl.deref();
if let NodeData::Element { name, .. } = &node.data {
if name.local.as_ref() != "tr" {
return None;
}
let cells = table_cells(&node.children.borrow());
return Some(TableRow { cells });
} else {
return None;
}
}
fn table_section(hdl: &Handle) -> Option<TableSection> {
let node = hdl.deref();
if let NodeData::Element { name, .. } = &node.data {
match name.local.as_ref() {
"thead" | "tbody" => {
let rows = table_rows(&node.children.borrow());
Some(TableSection { rows })
},
_ => None,
}
} else {
return None;
}
}
fn table_cells(handles: &[Handle]) -> Vec<(CellType, StyleTreeNode)> {
handles.iter().filter_map(table_cell).collect()
}
fn table_rows(handles: &[Handle]) -> Vec<TableRow> {
handles.iter().filter_map(table_row).collect()
}
fn table_sections(handles: &[Handle]) -> Vec<TableSection> {
handles.iter().filter_map(table_section).collect()
}
fn lic2t(handles: &[Handle]) -> StyleTreeChildren {
handles.iter().filter_map(li2t).collect()
}
fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> {
for attr in attrs {
if attr.name.local.as_ref() != "alt" {
continue;
}
return Some(attr.value.to_string());
}
return None;
}
fn attrs_to_language(attrs: &[Attribute]) -> Option<String> {
for attr in attrs {
if attr.name.local.as_ref() != "class" {
continue;
}
for class in attr.value.as_ref().unicode_words() {
if class.len() > 9 && class.starts_with("language-") {
return Some(class[9..].to_string());
}
}
}
return None;
}
fn attrs_to_style(attrs: &[Attribute]) -> Style {
let mut style = Style::default();
for attr in attrs {
match attr.name.local.as_ref() {
"data-mx-bg-color" => {
if let Ok(rgb) = attr.value.as_ref().parse::<CssColor>() {
let color = Color::Rgb(rgb.r, rgb.g, rgb.b);
style = style.bg(color);
}
},
"data-mx-color" | "color" => {
if let Ok(rgb) = attr.value.as_ref().parse::<CssColor>() {
let color = Color::Rgb(rgb.r, rgb.g, rgb.b);
style = style.fg(color);
}
},
_ => continue,
}
}
return style;
}
fn h2t(hdl: &Handle) -> StyleTreeChildren {
let node = hdl.deref();
let tree = match &node.data {
NodeData::Document => *c2t(node.children.borrow().as_slice()),
NodeData::Text { contents } => StyleTreeNode::Text(contents.borrow().to_string()),
NodeData::Element { name, attrs, .. } => {
match name.local.as_ref() {
// Message that this one replies to.
"mx-reply" => StyleTreeNode::Reply(c2t(&node.children.borrow())),
// Style change
"b" | "strong" => {
let c = c2t(&node.children.borrow());
let s = Style::default().add_modifier(StyleModifier::BOLD);
StyleTreeNode::Style(c, s)
},
"font" => {
let c = c2t(&node.children.borrow());
let s = attrs_to_style(&attrs.borrow());
StyleTreeNode::Style(c, s)
},
"em" | "i" => {
let c = c2t(&node.children.borrow());
let s = Style::default().add_modifier(StyleModifier::ITALIC);
StyleTreeNode::Style(c, s)
},
"span" => {
let c = c2t(&node.children.borrow());
let s = attrs_to_style(&attrs.borrow());
StyleTreeNode::Style(c, s)
},
"del" | "strike" => {
let c = c2t(&node.children.borrow());
let s = Style::default().add_modifier(StyleModifier::CROSSED_OUT);
StyleTreeNode::Style(c, s)
},
"u" => {
let c = c2t(&node.children.borrow());
let s = Style::default().add_modifier(StyleModifier::UNDERLINED);
StyleTreeNode::Style(c, s)
},
// Lists
"ol" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Ordered),
"ul" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Unordered),
// Headers
"h1" => StyleTreeNode::Header(c2t(&node.children.borrow()), 1),
"h2" => StyleTreeNode::Header(c2t(&node.children.borrow()), 2),
"h3" => StyleTreeNode::Header(c2t(&node.children.borrow()), 3),
"h4" => StyleTreeNode::Header(c2t(&node.children.borrow()), 4),
"h5" => StyleTreeNode::Header(c2t(&node.children.borrow()), 5),
"h6" => StyleTreeNode::Header(c2t(&node.children.borrow()), 6),
// Table
"table" => {
let sections = table_sections(&node.children.borrow());
let caption = node
.children
.borrow()
.iter()
.find_map(|hdl| get_node(hdl, "caption"))
.map(Box::new);
let table = Table { caption, sections };
StyleTreeNode::Table(table)
},
// Code blocks.
"code" => {
let c = c2t(&node.children.borrow());
let l = attrs_to_language(&attrs.borrow());
StyleTreeNode::Code(c, l)
},
// Other text blocks.
"blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())),
"div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())),
// No children.
"hr" => StyleTreeNode::Ruler,
"br" => StyleTreeNode::Break,
"img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())),
// These don't render in any special way.
"a" | "details" | "html" | "pre" | "summary" | "sub" | "sup" => {
*c2t(&node.children.borrow())
},
_ => return vec![],
}
},
// These don't render as anything.
NodeData::Doctype { .. } => return vec![],
NodeData::Comment { .. } => return vec![],
NodeData::ProcessingInstruction { .. } => return vec![],
};
vec![tree]
}
fn dom_to_style_tree(dom: RcDom) -> StyleTree {
StyleTree { children: h2t(&dom.document) }
}
pub fn parse_matrix_html(s: &str) -> StyleTree {
let dom = parse_fragment(
RcDom::default(),
ParseOpts::default(),
QualName::new(None, ns!(), local_name!("div")),
vec![],
)
.one(StrTendril::from(s));
dom_to_style_tree(dom)
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::util::space_span;
#[test]
fn test_header() {
let bold = Style::default().add_modifier(StyleModifier::BOLD);
let s = "<h1>Header 1</h1>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("# ", bold),
Span::styled("Header 1", bold),
space_span(10, Style::default())
])]);
let s = "<h2>Header 2</h2>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("## ", bold),
Span::styled("Header 2", bold),
space_span(9, Style::default())
])]);
let s = "<h3>Header 3</h3>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("### ", bold),
Span::styled("Header 3", bold),
space_span(8, Style::default())
])]);
let s = "<h4>Header 4</h4>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("#### ", bold),
Span::styled("Header 4", bold),
space_span(7, Style::default())
])]);
let s = "<h5>Header 5</h5>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("##### ", bold),
Span::styled("Header 5", bold),
space_span(6, Style::default())
])]);
let s = "<h6>Header 6</h6>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("###### ", bold),
Span::styled("Header 6", bold),
space_span(5, Style::default())
])]);
}
#[test]
fn test_style() {
let def = Style::default();
let bold = def.add_modifier(StyleModifier::BOLD);
let italic = def.add_modifier(StyleModifier::ITALIC);
let strike = def.add_modifier(StyleModifier::CROSSED_OUT);
let underl = def.add_modifier(StyleModifier::UNDERLINED);
let red = def.fg(Color::Rgb(0xff, 0x00, 0x00));
let s = "<b>Bold!</b>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Bold!", bold),
space_span(15, def)
])]);
let s = "<strong>Bold!</strong>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Bold!", bold),
space_span(15, def)
])]);
let s = "<i>Italic!</i>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Italic!", italic),
space_span(13, def)
])]);
let s = "<em>Italic!</em>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Italic!", italic),
space_span(13, def)
])]);
let s = "<del>Strikethrough!</del>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Strikethrough!", strike),
space_span(6, def)
])]);
let s = "<strike>Strikethrough!</strike>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Strikethrough!", strike),
space_span(6, def)
])]);
let s = "<u>Underline!</u>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![
Span::styled("Underline!", underl),
space_span(10, def)
])]);
let s = "<font color=\"#ff0000\">Red!</u>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
let s = "<font color=\"red\">Red!</u>";
let tree = parse_matrix_html(s);
let text = tree.to_text(20, Style::default(), false);
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
}
#[test]
fn test_paragraph() {
let s = "<p>Hello world!</p><p>Content</p><p>Goodbye world!</p>";
let tree = parse_matrix_html(s);
let text = tree.to_text(10, Style::default(), false);
assert_eq!(text.lines.len(), 7);
assert_eq!(text.lines[0], Spans(vec![Span::raw("Hello worl")]));
assert_eq!(text.lines[1], Spans(vec![Span::raw("d!"), Span::raw(" ")]));
assert_eq!(text.lines[2], Spans(vec![Span::raw(" ")]));
assert_eq!(text.lines[3], Spans(vec![Span::raw("Content"), Span::raw(" ")]));
assert_eq!(text.lines[4], Spans(vec![Span::raw(" ")]));
assert_eq!(text.lines[5], Spans(vec![Span::raw("Goodbye wo")]));
assert_eq!(text.lines[6], Spans(vec![Span::raw("rld!"), Span::raw(" ")]));
}
#[test]
fn test_blockquote() {
let s = "<blockquote>Hello world!</blockquote>";
let tree = parse_matrix_html(s);
let text = tree.to_text(10, Style::default(), false);
assert_eq!(text.lines.len(), 2);
assert_eq!(text.lines[0], Spans(vec![Span::raw(" "), Span::raw("Hello ")]));
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("world!")]));
}
#[test]
fn test_list_unordered() {
let s = "<ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul>";
let tree = parse_matrix_html(s);
let text = tree.to_text(8, Style::default(), false);
assert_eq!(text.lines.len(), 6);
assert_eq!(text.lines[0], Spans(vec![Span::raw("- "), Span::raw("List I")]));
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")]));
assert_eq!(text.lines[2], Spans(vec![Span::raw("- "), Span::raw("List I")]));
assert_eq!(text.lines[3], Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")]));
assert_eq!(text.lines[4], Spans(vec![Span::raw("- "), Span::raw("List I")]));
assert_eq!(text.lines[5], Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")]));
}
#[test]
fn test_list_ordered() {
let s = "<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ol>";
let tree = parse_matrix_html(s);
let text = tree.to_text(9, Style::default(), false);
assert_eq!(text.lines.len(), 6);
assert_eq!(text.lines[0], Spans(vec![Span::raw("1. "), Span::raw("List I")]));
assert_eq!(
text.lines[1],
Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")])
);
assert_eq!(text.lines[2], Spans(vec![Span::raw("2. "), Span::raw("List I")]));
assert_eq!(
text.lines[3],
Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")])
);
assert_eq!(text.lines[4], Spans(vec![Span::raw("3. "), Span::raw("List I")]));
assert_eq!(
text.lines[5],
Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")])
);
}
#[test]
fn test_table() {
let s = "<table>\
<thead>\
<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>
</thead>\
<tbody>\
<tr><td>a</td><td>b</td><td>c</td></tr>\
<tr><td>a</td><td>b</td><td>c</td></tr>\
<tr><td>a</td><td>b</td><td>c</td></tr>\
</tbody></table>";
let tree = parse_matrix_html(s);
let text = tree.to_text(15, Style::default(), false);
let bold = Style::default().add_modifier(StyleModifier::BOLD);
assert_eq!(text.lines.len(), 11);
// Table header
assert_eq!(text.lines[0].0, vec![Span::raw("┌────┬────┬───┐")]);
assert_eq!(text.lines[1].0, vec![
Span::raw(""),
Span::styled("Colu", bold),
Span::raw(""),
Span::styled("Colu", bold),
Span::raw(""),
Span::styled("Col", bold),
Span::raw("")
]);
assert_eq!(text.lines[2].0, vec![
Span::raw(""),
Span::styled("mn 1", bold),
Span::raw(""),
Span::styled("mn 2", bold),
Span::raw(""),
Span::styled("umn", bold),
Span::raw("")
]);
assert_eq!(text.lines[3].0, vec![
Span::raw(""),
Span::raw(" "),
Span::raw(""),
Span::raw(" "),
Span::raw(""),
Span::styled(" 3", bold),
Span::styled(" ", bold),
Span::raw("")
]);
// First row
assert_eq!(text.lines[4].0, vec![Span::raw("├────┼────┼───┤")]);
assert_eq!(text.lines[5].0, vec![
Span::raw(""),
Span::raw("a"),
Span::raw(" "),
Span::raw(""),
Span::raw("b"),
Span::raw(" "),
Span::raw(""),
Span::raw("c"),
Span::raw(" "),
Span::raw("")
]);
// Second row
assert_eq!(text.lines[6].0, vec![Span::raw("├────┼────┼───┤")]);
assert_eq!(text.lines[7].0, vec![
Span::raw(""),
Span::raw("a"),
Span::raw(" "),
Span::raw(""),
Span::raw("b"),
Span::raw(" "),
Span::raw(""),
Span::raw("c"),
Span::raw(" "),
Span::raw("")
]);
// Third row
assert_eq!(text.lines[8].0, vec![Span::raw("├────┼────┼───┤")]);
assert_eq!(text.lines[9].0, vec![
Span::raw(""),
Span::raw("a"),
Span::raw(" "),
Span::raw(""),
Span::raw("b"),
Span::raw(" "),
Span::raw(""),
Span::raw("c"),
Span::raw(" "),
Span::raw("")
]);
// Bottom ruler
assert_eq!(text.lines[10].0, vec![Span::raw("└────┴────┴───┘")]);
}
#[test]
fn test_matrix_reply() {
let s = "<mx-reply>This was replied to</mx-reply>This is the reply";
let tree = parse_matrix_html(s);
let text = tree.to_text(10, Style::default(), false);
assert_eq!(text.lines.len(), 4);
assert_eq!(text.lines[0], Spans(vec![Span::raw("This was r")]));
assert_eq!(text.lines[1], Spans(vec![Span::raw("eplied to"), Span::raw(" ")]));
assert_eq!(text.lines[2], Spans(vec![Span::raw("This is th")]));
assert_eq!(text.lines[3], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
let tree = parse_matrix_html(s);
let text = tree.to_text(10, Style::default(), true);
assert_eq!(text.lines.len(), 2);
assert_eq!(text.lines[0], Spans(vec![Span::raw("This is th")]));
assert_eq!(text.lines[1], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
}
}

807
src/message/mod.rs Normal file
View File

@@ -0,0 +1,807 @@
use std::borrow::Cow;
use std::cmp::{Ord, Ordering, PartialOrd};
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
use std::slice::Iter;
use chrono::{DateTime, NaiveDateTime, Utc};
use unicode_width::UnicodeWidthStr;
use matrix_sdk::ruma::{
events::{
room::{
message::{
FormattedBody,
MessageFormat,
MessageType,
OriginalRoomMessageEvent,
RedactedRoomMessageEvent,
Relation,
RoomMessageEvent,
RoomMessageEventContent,
},
redaction::SyncRoomRedactionEvent,
},
Redact,
},
EventId,
MilliSecondsSinceUnixEpoch,
OwnedEventId,
OwnedUserId,
RoomVersionId,
UInt,
};
use modalkit::tui::{
style::{Modifier as StyleModifier, Style},
symbols::line::THICK_VERTICAL,
text::{Span, Spans, Text},
};
use modalkit::editing::{base::ViewportContext, cursor::Cursor};
use crate::{
base::{IambResult, RoomInfo},
config::ApplicationSettings,
message::html::{parse_matrix_html, StyleTree},
util::{space_span, wrapped_text},
};
mod html;
mod printer;
pub type MessageFetchResult = IambResult<(Option<String>, Vec<RoomMessageEvent>)>;
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
pub type Messages = BTreeMap<MessageKey, Message>;
const fn span_static(s: &'static str) -> Span<'static> {
Span {
content: Cow::Borrowed(s),
style: Style {
fg: None,
bg: None,
add_modifier: StyleModifier::empty(),
sub_modifier: StyleModifier::empty(),
},
}
}
const USER_GUTTER: usize = 30;
const TIME_GUTTER: usize = 12;
const READ_GUTTER: usize = 5;
const MIN_MSG_LEN: usize = 30;
const USER_GUTTER_EMPTY: &str = " ";
const USER_GUTTER_EMPTY_SPAN: Span<'static> = span_static(USER_GUTTER_EMPTY);
const TIME_GUTTER_EMPTY: &str = " ";
const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
#[derive(thiserror::Error, Debug)]
pub enum TimeStampIntError {
#[error("Integer conversion error: {0}")]
IntError(#[from] std::num::TryFromIntError),
#[error("UInt conversion error: {0}")]
UIntError(<UInt as TryFrom<u64>>::Error),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MessageTimeStamp {
OriginServer(UInt),
LocalEcho,
}
impl MessageTimeStamp {
fn show(&self) -> Option<Span> {
match self {
MessageTimeStamp::OriginServer(ts) => {
let time = i64::from(*ts) / 1000;
let time = NaiveDateTime::from_timestamp_opt(time, 0)?;
let time = DateTime::<Utc>::from_utc(time, Utc);
let time = time.format("%T");
let time = format!(" [{}]", time);
Span::raw(time).into()
},
MessageTimeStamp::LocalEcho => None,
}
}
fn is_local_echo(&self) -> bool {
matches!(self, MessageTimeStamp::LocalEcho)
}
pub fn as_millis(&self) -> Option<MilliSecondsSinceUnixEpoch> {
match self {
MessageTimeStamp::OriginServer(ms) => MilliSecondsSinceUnixEpoch(*ms).into(),
MessageTimeStamp::LocalEcho => None,
}
}
}
impl Ord for MessageTimeStamp {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(MessageTimeStamp::OriginServer(_), MessageTimeStamp::LocalEcho) => Ordering::Less,
(MessageTimeStamp::OriginServer(a), MessageTimeStamp::OriginServer(b)) => a.cmp(b),
(MessageTimeStamp::LocalEcho, MessageTimeStamp::OriginServer(_)) => Ordering::Greater,
(MessageTimeStamp::LocalEcho, MessageTimeStamp::LocalEcho) => Ordering::Equal,
}
}
}
impl PartialOrd for MessageTimeStamp {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
MessageTimeStamp::OriginServer(millis.0)
}
}
impl TryFrom<&MessageTimeStamp> for usize {
type Error = TimeStampIntError;
fn try_from(ts: &MessageTimeStamp) -> Result<Self, Self::Error> {
let n = match ts {
MessageTimeStamp::LocalEcho => 0,
MessageTimeStamp::OriginServer(u) => usize::try_from(u64::from(*u))?,
};
Ok(n)
}
}
impl TryFrom<usize> for MessageTimeStamp {
type Error = TimeStampIntError;
fn try_from(u: usize) -> Result<Self, Self::Error> {
if u == 0 {
Ok(MessageTimeStamp::LocalEcho)
} else {
let n = u64::try_from(u)?;
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
Ok(MessageTimeStamp::OriginServer(n))
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MessageCursor {
/// When timestamp is None, the corner is determined by moving backwards from
/// the most recently received message.
pub timestamp: Option<MessageKey>,
/// A row within the [Text] representation of a [Message].
pub text_row: usize,
}
impl MessageCursor {
pub fn new(timestamp: MessageKey, text_row: usize) -> Self {
MessageCursor { timestamp: Some(timestamp), text_row }
}
/// Get a cursor that refers to the most recent message.
pub fn latest() -> Self {
MessageCursor::default()
}
pub fn to_key<'a>(&'a self, info: &'a RoomInfo) -> Option<&'a MessageKey> {
if let Some(ref key) = self.timestamp {
Some(key)
} else {
Some(info.messages.last_key_value()?.0)
}
}
pub fn from_cursor(cursor: &Cursor, info: &RoomInfo) -> Option<Self> {
let ev_hash = u64::try_from(cursor.get_x()).ok()?;
let ev_term = OwnedEventId::try_from("$").ok()?;
let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
let start = (ts_start, ev_term);
let mut mc = None;
for ((ts, event_id), _) in info.messages.range(start..) {
let mut hasher = DefaultHasher::new();
event_id.hash(&mut hasher);
if hasher.finish() == ev_hash {
mc = Self::from((*ts, event_id.clone())).into();
break;
}
if mc.is_none() {
mc = Self::from((*ts, event_id.clone())).into();
}
if ts > &ts_start {
break;
}
}
return mc;
}
pub fn to_cursor(&self, info: &RoomInfo) -> Option<Cursor> {
let (ts, event_id) = self.to_key(info)?;
let y: usize = usize::try_from(ts).ok()?;
let mut hasher = DefaultHasher::new();
event_id.hash(&mut hasher);
let x = usize::try_from(hasher.finish()).ok()?;
Cursor::new(y, x).into()
}
}
impl From<Option<MessageKey>> for MessageCursor {
fn from(key: Option<MessageKey>) -> Self {
MessageCursor { timestamp: key, text_row: 0 }
}
}
impl From<MessageKey> for MessageCursor {
fn from(key: MessageKey) -> Self {
MessageCursor { timestamp: Some(key), text_row: 0 }
}
}
impl Ord for MessageCursor {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.timestamp, &other.timestamp) {
(None, None) => self.text_row.cmp(&other.text_row),
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(st), Some(ot)) => {
let pcmp = st.cmp(ot);
let tcmp = self.text_row.cmp(&other.text_row);
pcmp.then(tcmp)
},
}
}
}
impl PartialOrd for MessageCursor {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
#[derive(Clone)]
pub enum MessageEvent {
Original(Box<OriginalRoomMessageEvent>),
Redacted(Box<RedactedRoomMessageEvent>),
Local(OwnedEventId, Box<RoomMessageEventContent>),
}
impl MessageEvent {
pub fn event_id(&self) -> &EventId {
match self {
MessageEvent::Original(ev) => ev.event_id.as_ref(),
MessageEvent::Redacted(ev) => ev.event_id.as_ref(),
MessageEvent::Local(event_id, _) => event_id.as_ref(),
}
}
pub fn body(&self) -> Cow<'_, str> {
match self {
MessageEvent::Original(ev) => body_cow_content(&ev.content),
MessageEvent::Redacted(ev) => {
let reason = ev
.unsigned
.redacted_because
.as_ref()
.and_then(|e| e.as_original())
.and_then(|r| r.content.reason.as_ref());
if let Some(r) = reason {
Cow::Owned(format!("[Redacted: {:?}]", r))
} else {
Cow::Borrowed("[Redacted]")
}
},
MessageEvent::Local(_, content) => body_cow_content(content),
}
}
pub fn html(&self) -> Option<StyleTree> {
let content = match self {
MessageEvent::Original(ev) => &ev.content,
MessageEvent::Redacted(_) => return None,
MessageEvent::Local(_, content) => content,
};
if let MessageType::Text(content) = &content.msgtype {
if let Some(FormattedBody { format: MessageFormat::Html, body }) = &content.formatted {
Some(parse_matrix_html(body.as_str()))
} else {
None
}
} else {
None
}
}
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
match self {
MessageEvent::Redacted(_) => return,
MessageEvent::Local(_, _) => return,
MessageEvent::Original(ev) => {
let redacted = ev.clone().redact(redaction, version);
*self = MessageEvent::Redacted(Box::new(redacted));
},
}
}
}
fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
let s = match &content.msgtype {
MessageType::Text(content) => content.body.as_str(),
MessageType::VerificationRequest(_) => "[Verification Request]",
MessageType::Emote(content) => content.body.as_ref(),
MessageType::Notice(content) => content.body.as_str(),
MessageType::ServerNotice(content) => content.body.as_str(),
MessageType::Audio(content) => {
return Cow::Owned(format!("[Attached Audio: {}]", content.body));
},
MessageType::File(content) => {
return Cow::Owned(format!("[Attached File: {}]", content.body));
},
MessageType::Image(content) => {
return Cow::Owned(format!("[Attached Image: {}]", content.body));
},
MessageType::Video(content) => {
return Cow::Owned(format!("[Attached Video: {}]", content.body));
},
_ => {
return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype()));
},
};
Cow::Borrowed(s)
}
enum MessageColumns {
/// Four columns: sender, message, timestamp, read receipts.
Four,
/// Three columns: sender, message, timestamp.
Three,
/// Two columns: sender, message.
Two,
/// One column: message with sender on line before the message.
One,
}
struct MessageFormatter<'a> {
settings: &'a ApplicationSettings,
cols: MessageColumns,
fill: usize,
user: Option<Span<'a>>,
time: Option<Span<'a>>,
read: Iter<'a, OwnedUserId>,
}
impl<'a> MessageFormatter<'a> {
fn width(&self) -> usize {
self.fill
}
#[inline]
fn push_spans(&mut self, spans: Spans<'a>, style: Style, text: &mut Text<'a>) {
match self.cols {
MessageColumns::Four => {
let settings = self.settings;
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN);
let mut line = vec![user];
line.extend(spans.0);
line.push(time);
// Show read receipts.
let user_char =
|user: &'a OwnedUserId| -> Span<'a> { settings.get_user_char_span(user) };
let a = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
let b = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
let c = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
line.push(Span::raw(" "));
line.push(c);
line.push(b);
line.push(a);
line.push(Span::raw(" "));
text.lines.push(Spans(line))
},
MessageColumns::Three => {
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let time = self.time.take().unwrap_or_else(|| Span::from(""));
let mut line = vec![user];
line.extend(spans.0);
line.push(time);
text.lines.push(Spans(line))
},
MessageColumns::Two => {
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let mut line = vec![user];
line.extend(spans.0);
text.lines.push(Spans(line));
},
MessageColumns::One => {
if let Some(user) = self.user.take() {
text.lines.push(Spans(vec![user]));
}
let leading = space_span(2, style);
let mut line = vec![leading];
line.extend(spans.0);
text.lines.push(Spans(line));
},
}
}
fn push_text(&mut self, append: Text<'a>, style: Style, text: &mut Text<'a>) {
for line in append.lines.into_iter() {
self.push_spans(line, style, text);
}
}
}
pub struct Message {
pub event: MessageEvent,
pub sender: OwnedUserId,
pub timestamp: MessageTimeStamp,
pub downloaded: bool,
pub html: Option<StyleTree>,
}
impl Message {
pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
let html = event.html();
let downloaded = false;
Message { event, sender, timestamp, downloaded, html }
}
pub fn reply_to(&self) -> Option<OwnedEventId> {
let content = match &self.event {
MessageEvent::Local(_, content) => content,
MessageEvent::Original(ev) => &ev.content,
MessageEvent::Redacted(_) => return None,
};
if let Some(Relation::Reply { in_reply_to }) = &content.relates_to {
Some(in_reply_to.event_id.clone())
} else {
None
}
}
fn get_render_style(&self, selected: bool) -> Style {
let mut style = Style::default();
if selected {
style = style.add_modifier(StyleModifier::REVERSED)
}
if self.timestamp.is_local_echo() {
style = style.add_modifier(StyleModifier::ITALIC);
}
return style;
}
fn get_render_format<'a>(
&'a self,
prev: Option<&Message>,
width: usize,
info: &'a RoomInfo,
settings: &'a ApplicationSettings,
) -> MessageFormatter<'a> {
if USER_GUTTER + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
settings.tunables.read_receipt_display
{
let cols = MessageColumns::Four;
let fill = width - USER_GUTTER - TIME_GUTTER - READ_GUTTER;
let user = self.show_sender(prev, true, settings);
let time = self.timestamp.show();
let read = match info.receipts.get(self.event.event_id()) {
Some(read) => read.iter(),
None => [].iter(),
};
MessageFormatter { settings, cols, fill, user, time, read }
} else if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
let cols = MessageColumns::Three;
let fill = width - USER_GUTTER - TIME_GUTTER;
let user = self.show_sender(prev, true, settings);
let time = self.timestamp.show();
let read = [].iter();
MessageFormatter { settings, cols, fill, user, time, read }
} else if USER_GUTTER + MIN_MSG_LEN <= width {
let cols = MessageColumns::Two;
let fill = width - USER_GUTTER;
let user = self.show_sender(prev, true, settings);
let time = None;
let read = [].iter();
MessageFormatter { settings, cols, fill, user, time, read }
} else {
let cols = MessageColumns::One;
let fill = width.saturating_sub(2);
let user = self.show_sender(prev, false, settings);
let time = None;
let read = [].iter();
MessageFormatter { settings, cols, fill, user, time, read }
}
}
pub fn show<'a>(
&'a self,
prev: Option<&Message>,
selected: bool,
vwctx: &ViewportContext<MessageCursor>,
info: &'a RoomInfo,
settings: &'a ApplicationSettings,
) -> Text<'a> {
let width = vwctx.get_width();
let style = self.get_render_style(selected);
let mut fmt = self.get_render_format(prev, width, info, settings);
let mut text = Text { lines: vec![] };
let width = fmt.width();
// Show the message that this one replied to, if any.
let reply = self.reply_to().and_then(|e| info.get_event(&e));
if let Some(r) = &reply {
let w = width.saturating_sub(2);
let mut replied = r.show_msg(w, style, true);
let mut sender = r.sender_span(settings);
let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
let trailing = w.saturating_sub(sender_width + 1);
sender.style = sender.style.patch(style);
fmt.push_spans(
Spans(vec![
Span::styled(" ", style),
Span::styled(THICK_VERTICAL, style),
sender,
Span::styled(":", style),
space_span(trailing, style),
]),
style,
&mut text,
);
for line in replied.lines.iter_mut() {
line.0.insert(0, Span::styled(THICK_VERTICAL, style));
line.0.insert(0, Span::styled(" ", style));
}
fmt.push_text(replied, style, &mut text);
}
// Now show the message contents, and the inlined reply if we couldn't find it above.
let msg = self.show_msg(width, style, reply.is_some());
fmt.push_text(msg, style, &mut text);
if text.lines.is_empty() {
// If there was nothing in the body, just show an empty message.
fmt.push_spans(space_span(width, style).into(), style, &mut text);
}
return text;
}
pub fn show_msg(&self, width: usize, style: Style, hide_reply: bool) -> Text {
if let Some(html) = &self.html {
html.to_text(width, style, hide_reply)
} else {
let mut msg = self.event.body();
if self.downloaded {
msg.to_mut().push_str(" \u{2705}");
}
wrapped_text(msg, width, style)
}
}
fn sender_span(&self, settings: &ApplicationSettings) -> Span {
settings.get_user_span(self.sender.as_ref())
}
fn show_sender(
&self,
prev: Option<&Message>,
align_right: bool,
settings: &ApplicationSettings,
) -> Option<Span> {
let user = if matches!(prev, Some(prev) if self.sender == prev.sender) {
return None;
} else {
self.sender_span(settings)
};
let Span { content, style } = user;
let stop = content.len().min(28);
let s = &content[..stop];
let sender = if align_right {
format!("{: >width$} ", s, width = 28)
} else {
format!("{: <width$} ", s, width = 28)
};
Span::styled(sender, style).into()
}
}
impl From<OriginalRoomMessageEvent> for Message {
fn from(event: OriginalRoomMessageEvent) -> Self {
let timestamp = event.origin_server_ts.into();
let user_id = event.sender.clone();
let content = MessageEvent::Original(event.into());
Message::new(content, user_id, timestamp)
}
}
impl From<RedactedRoomMessageEvent> for Message {
fn from(event: RedactedRoomMessageEvent) -> Self {
let timestamp = event.origin_server_ts.into();
let user_id = event.sender.clone();
let content = MessageEvent::Redacted(event.into());
Message::new(content, user_id, timestamp)
}
}
impl From<RoomMessageEvent> for Message {
fn from(event: RoomMessageEvent) -> Self {
match event {
RoomMessageEvent::Original(ev) => ev.into(),
RoomMessageEvent::Redacted(ev) => ev.into(),
}
}
}
impl ToString for Message {
fn to_string(&self) -> String {
self.event.body().into_owned()
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::tests::*;
#[test]
fn test_mc_cmp() {
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
// Everything is equal to itself.
assert_eq!(mc1.cmp(&mc1), Ordering::Equal);
assert_eq!(mc2.cmp(&mc2), Ordering::Equal);
assert_eq!(mc3.cmp(&mc3), Ordering::Equal);
assert_eq!(mc4.cmp(&mc4), Ordering::Equal);
assert_eq!(mc5.cmp(&mc5), Ordering::Equal);
// Local echo is always greater than an origin server timestamp.
assert_eq!(mc1.cmp(&mc2), Ordering::Greater);
assert_eq!(mc1.cmp(&mc3), Ordering::Greater);
assert_eq!(mc1.cmp(&mc4), Ordering::Greater);
assert_eq!(mc1.cmp(&mc5), Ordering::Greater);
// mc2 is the smallest timestamp.
assert_eq!(mc2.cmp(&mc1), Ordering::Less);
assert_eq!(mc2.cmp(&mc3), Ordering::Less);
assert_eq!(mc2.cmp(&mc4), Ordering::Less);
assert_eq!(mc2.cmp(&mc5), Ordering::Less);
// mc3 should be less than mc4 because of its event ID.
assert_eq!(mc3.cmp(&mc1), Ordering::Less);
assert_eq!(mc3.cmp(&mc2), Ordering::Greater);
assert_eq!(mc3.cmp(&mc4), Ordering::Less);
assert_eq!(mc3.cmp(&mc5), Ordering::Less);
// mc4 should be greater than mc3 because of its event ID.
assert_eq!(mc4.cmp(&mc1), Ordering::Less);
assert_eq!(mc4.cmp(&mc2), Ordering::Greater);
assert_eq!(mc4.cmp(&mc3), Ordering::Greater);
assert_eq!(mc4.cmp(&mc5), Ordering::Less);
// mc5 is the greatest OriginServer timestamp.
assert_eq!(mc5.cmp(&mc1), Ordering::Less);
assert_eq!(mc5.cmp(&mc2), Ordering::Greater);
assert_eq!(mc5.cmp(&mc3), Ordering::Greater);
assert_eq!(mc5.cmp(&mc4), Ordering::Greater);
}
#[test]
fn test_mc_to_key() {
let info = mock_room();
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
let mc6 = MessageCursor::latest();
let k1 = mc1.to_key(&info).unwrap();
let k2 = mc2.to_key(&info).unwrap();
let k3 = mc3.to_key(&info).unwrap();
let k4 = mc4.to_key(&info).unwrap();
let k5 = mc5.to_key(&info).unwrap();
let k6 = mc6.to_key(&info).unwrap();
// These should all be equal to their MSGN_KEYs.
assert_eq!(k1, &MSG1_KEY.clone());
assert_eq!(k2, &MSG2_KEY.clone());
assert_eq!(k3, &MSG3_KEY.clone());
assert_eq!(k4, &MSG4_KEY.clone());
assert_eq!(k5, &MSG5_KEY.clone());
// MessageCursor::latest() turns into the largest key (our local echo message).
assert_eq!(k6, &MSG1_KEY.clone());
// MessageCursor::latest() fails to convert for a room w/o messages.
let info_empty = RoomInfo::default();
assert_eq!(mc6.to_key(&info_empty), None);
}
#[test]
fn test_mc_to_from_cursor() {
let info = mock_room();
let mc1 = MessageCursor::from(MSG1_KEY.clone());
let mc2 = MessageCursor::from(MSG2_KEY.clone());
let mc3 = MessageCursor::from(MSG3_KEY.clone());
let mc4 = MessageCursor::from(MSG4_KEY.clone());
let mc5 = MessageCursor::from(MSG5_KEY.clone());
let mc6 = MessageCursor::latest();
let identity = |mc: &MessageCursor| {
let c = mc.to_cursor(&info).unwrap();
MessageCursor::from_cursor(&c, &info).unwrap()
};
// These should all convert to a Cursor and back to the original value.
assert_eq!(identity(&mc1), mc1);
assert_eq!(identity(&mc2), mc2);
assert_eq!(identity(&mc3), mc3);
assert_eq!(identity(&mc4), mc4);
assert_eq!(identity(&mc5), mc5);
// MessageCursor::latest() should point at the most recent message after conversion.
assert_eq!(identity(&mc6), mc1);
}
}

157
src/message/printer.rs Normal file
View File

@@ -0,0 +1,157 @@
use std::borrow::Cow;
use modalkit::tui::layout::Alignment;
use modalkit::tui::style::Style;
use modalkit::tui::text::{Span, Spans, Text};
use unicode_width::UnicodeWidthStr;
use crate::util::{space_span, take_width};
pub struct TextPrinter<'a> {
text: Text<'a>,
width: usize,
base_style: Style,
hide_reply: bool,
alignment: Alignment,
curr_spans: Vec<Span<'a>>,
curr_width: usize,
}
impl<'a> TextPrinter<'a> {
pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
TextPrinter {
text: Text::default(),
width,
base_style,
hide_reply,
alignment: Alignment::Left,
curr_spans: vec![],
curr_width: 0,
}
}
pub fn align(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
pub fn hide_reply(&self) -> bool {
self.hide_reply
}
pub fn width(&self) -> usize {
self.width
}
pub fn sub(&self, indent: usize) -> Self {
TextPrinter {
text: Text::default(),
width: self.width.saturating_sub(indent),
base_style: self.base_style,
hide_reply: self.hide_reply,
alignment: self.alignment,
curr_spans: vec![],
curr_width: 0,
}
}
fn remaining(&self) -> usize {
self.width - self.curr_width
}
pub fn commit(&mut self) {
if self.curr_width > 0 {
self.push_break();
}
}
fn push(&mut self) {
self.curr_width = 0;
self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
}
pub fn push_break(&mut self) {
if self.curr_width == 0 && self.text.lines.is_empty() {
// Disallow leading breaks.
return;
}
let remaining = self.remaining();
if remaining > 0 {
match self.alignment {
Alignment::Left => {
let tspan = space_span(remaining, self.base_style);
self.curr_spans.push(tspan);
},
Alignment::Center => {
let trailing = remaining / 2;
let leading = remaining - trailing;
let tspan = space_span(trailing, self.base_style);
let lspan = space_span(leading, self.base_style);
self.curr_spans.push(tspan);
self.curr_spans.insert(0, lspan);
},
Alignment::Right => {
let lspan = space_span(remaining, self.base_style);
self.curr_spans.insert(0, lspan);
},
}
}
self.push();
}
pub fn push_str<T>(&mut self, s: T, style: Style)
where
T: Into<Cow<'a, str>>,
{
let style = self.base_style.patch(style);
let mut cow = s.into();
loop {
let sw = UnicodeWidthStr::width(cow.as_ref());
if self.curr_width + sw <= self.width {
// The text fits within the current line.
self.curr_spans.push(Span::styled(cow, style));
self.curr_width += sw;
break;
}
// Take a leading portion of the text that fits in the line.
let ((s0, w), s1) = take_width(cow, self.remaining());
cow = s1;
self.curr_spans.push(Span::styled(s0, style));
self.curr_width += w;
self.commit();
}
if self.curr_width == self.width {
// If the last bit fills the full line, start a new one.
self.push();
}
}
pub fn push_line(&mut self, spans: Spans<'a>) {
self.commit();
self.text.lines.push(spans);
}
pub fn push_text(&mut self, text: Text<'a>) {
self.commit();
self.text.lines.extend(text.lines);
}
pub fn finish(mut self) -> Text<'a> {
self.commit();
self.text
}
}

View File

@@ -1,29 +1,39 @@
use std::collections::BTreeMap; use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
use matrix_sdk::ruma::{ use matrix_sdk::ruma::{
event_id, event_id,
events::room::message::RoomMessageEventContent, events::room::message::{OriginalRoomMessageEvent, RoomMessageEventContent},
server_name, server_name,
user_id, user_id,
EventId, EventId,
OwnedEventId,
OwnedRoomId, OwnedRoomId,
OwnedUserId, OwnedUserId,
RoomId, RoomId,
UInt, UInt,
}; };
use std::path::PathBuf;
use std::sync::mpsc::sync_channel;
use url::Url;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use modalkit::tui::style::{Color, Style};
use tokio::sync::mpsc::unbounded_channel;
use url::Url;
use crate::{ use crate::{
base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo}, base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo},
config::{ApplicationSettings, DirectoryValues, ProfileConfig, TunableValues}, config::{
user_color,
user_style_from_color,
ApplicationSettings,
DirectoryValues,
ProfileConfig,
TunableValues,
UserColor,
UserDisplayTunables,
},
message::{ message::{
Message, Message,
MessageContent, MessageEvent,
MessageKey, MessageKey,
MessageTimeStamp::{LocalEcho, OriginServer}, MessageTimeStamp::{LocalEcho, OriginServer},
Messages, Messages,
@@ -35,57 +45,88 @@ lazy_static! {
pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned(); pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned();
pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned(); pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned(); pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned();
pub static ref TEST_USER3: OwnedUserId = user_id!("@user2:example.com").to_owned(); pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
pub static ref TEST_USER4: OwnedUserId = user_id!("@user2:example.com").to_owned(); pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
pub static ref TEST_USER5: OwnedUserId = user_id!("@user2:example.com").to_owned(); pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
pub static ref MSG1_KEY: MessageKey = (LocalEcho, EventId::new(server_name!("example.com"))); pub static ref MSG1_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
pub static ref MSG2_KEY: MessageKey = pub static ref MSG2_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
(OriginServer(UInt::new(1).unwrap()), EventId::new(server_name!("example.com"))); pub static ref MSG3_EVID: OwnedEventId =
pub static ref MSG3_KEY: MessageKey = ( event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
OriginServer(UInt::new(2).unwrap()), pub static ref MSG4_EVID: OwnedEventId =
event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned() event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
); pub static ref MSG5_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
pub static ref MSG4_KEY: MessageKey = ( pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
OriginServer(UInt::new(2).unwrap()), pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned() pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
); pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
pub static ref MSG5_KEY: MessageKey = pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
(OriginServer(UInt::new(8).unwrap()), EventId::new(server_name!("example.com"))); }
pub fn user_style(user: &str) -> Style {
user_style_from_color(user_color(user))
}
pub fn mock_room1_message(
content: RoomMessageEventContent,
sender: OwnedUserId,
key: MessageKey,
) -> Message {
let origin_server_ts = key.0.as_millis().unwrap();
let event_id = key.1;
let event = OriginalRoomMessageEvent {
content,
event_id,
sender,
origin_server_ts,
room_id: TEST_ROOM1_ID.clone(),
unsigned: Default::default(),
};
event.into()
} }
pub fn mock_message1() -> Message { pub fn mock_message1() -> Message {
let content = RoomMessageEventContent::text_plain("writhe"); let content = RoomMessageEventContent::text_plain("writhe");
let content = MessageContent::Original(content.into()); let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
Message::new(content, TEST_USER1.clone(), MSG1_KEY.0) Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
} }
pub fn mock_message2() -> Message { pub fn mock_message2() -> Message {
let content = RoomMessageEventContent::text_plain("helium"); let content = RoomMessageEventContent::text_plain("helium");
let content = MessageContent::Original(content.into());
Message::new(content, TEST_USER2.clone(), MSG2_KEY.0) mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
} }
pub fn mock_message3() -> Message { pub fn mock_message3() -> Message {
let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage"); let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
let content = MessageContent::Original(content.into());
Message::new(content, TEST_USER2.clone(), MSG3_KEY.0) mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
} }
pub fn mock_message4() -> Message { pub fn mock_message4() -> Message {
let content = RoomMessageEventContent::text_plain("help"); let content = RoomMessageEventContent::text_plain("help");
let content = MessageContent::Original(content.into());
Message::new(content, TEST_USER1.clone(), MSG4_KEY.0) mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
} }
pub fn mock_message5() -> Message { pub fn mock_message5() -> Message {
let content = RoomMessageEventContent::text_plain("character"); let content = RoomMessageEventContent::text_plain("character");
let content = MessageContent::Original(content.into());
Message::new(content, TEST_USER2.clone(), MSG5_KEY.0) mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
}
pub fn mock_keys() -> HashMap<OwnedEventId, MessageKey> {
let mut keys = HashMap::new();
keys.insert(MSG1_EVID.clone(), MSG1_KEY.clone());
keys.insert(MSG2_EVID.clone(), MSG2_KEY.clone());
keys.insert(MSG3_EVID.clone(), MSG3_KEY.clone());
keys.insert(MSG4_EVID.clone(), MSG4_KEY.clone());
keys.insert(MSG5_EVID.clone(), MSG5_KEY.clone());
keys
} }
pub fn mock_messages() -> Messages { pub fn mock_messages() -> Messages {
@@ -103,7 +144,14 @@ pub fn mock_messages() -> Messages {
pub fn mock_room() -> RoomInfo { pub fn mock_room() -> RoomInfo {
RoomInfo { RoomInfo {
name: Some("Watercooler Discussion".into()), name: Some("Watercooler Discussion".into()),
tags: None,
keys: mock_keys(),
messages: mock_messages(), messages: mock_messages(),
receipts: HashMap::new(),
read_till: None,
fetch_id: RoomFetchStatus::NotStarted, fetch_id: RoomFetchStatus::NotStarted,
fetch_last: None, fetch_last: None,
users_typing: None, users_typing: None,
@@ -118,6 +166,22 @@ pub fn mock_dirs() -> DirectoryValues {
} }
} }
pub fn mock_tunables() -> TunableValues {
TunableValues {
default_room: None,
read_receipt_send: true,
read_receipt_display: true,
typing_notice_send: true,
typing_notice_display: true,
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
color: Some(UserColor(Color::Black)),
name: Some("USER 5".into()),
})]
.into_iter()
.collect::<HashMap<_, _>>(),
}
}
pub fn mock_settings() -> ApplicationSettings { pub fn mock_settings() -> ApplicationSettings {
ApplicationSettings { ApplicationSettings {
matrix_dir: PathBuf::new(), matrix_dir: PathBuf::new(),
@@ -129,14 +193,16 @@ pub fn mock_settings() -> ApplicationSettings {
settings: None, settings: None,
dirs: None, dirs: None,
}, },
tunables: TunableValues { typing_notice: true, typing_notice_display: true }, tunables: mock_tunables(),
dirs: mock_dirs(), dirs: mock_dirs(),
} }
} }
pub fn mock_store() -> ProgramStore { pub async fn mock_store() -> ProgramStore {
let (tx, _) = sync_channel(5); let (tx, _) = unbounded_channel();
let worker = Requester { tx }; let homeserver = Url::parse("https://localhost").unwrap();
let client = matrix_sdk::Client::new(homeserver).await.unwrap();
let worker = Requester { tx, client };
let mut store = ChatStore::new(worker, mock_settings()); let mut store = ChatStore::new(worker, mock_settings());
let room_id = TEST_ROOM1_ID.clone(); let room_id = TEST_ROOM1_ID.clone();

191
src/util.rs Normal file
View File

@@ -0,0 +1,191 @@
use std::borrow::Cow;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use modalkit::tui::style::Style;
use modalkit::tui::text::{Span, Spans, Text};
pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>) {
match cow {
Cow::Borrowed(s) => {
let s1 = Cow::Borrowed(&s[idx..]);
let s0 = Cow::Borrowed(&s[..idx]);
(s0, s1)
},
Cow::Owned(mut s) => {
let s1 = Cow::Owned(s.split_off(idx));
let s0 = Cow::Owned(s);
(s0, s1)
},
}
}
pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
// Find where to split the line.
let mut idx = 0;
let mut w = 0;
for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) {
let gw = UnicodeWidthStr::width(g);
idx = i;
if w + gw > width {
break;
}
w += gw;
}
let (s0, s1) = split_cow(s, idx);
((s0, w), s1)
}
pub struct WrappedLinesIterator<'a> {
iter: std::vec::IntoIter<Cow<'a, str>>,
curr: Option<Cow<'a, str>>,
width: usize,
}
impl<'a> WrappedLinesIterator<'a> {
fn new<T>(input: T, width: usize) -> Self
where
T: Into<Cow<'a, str>>,
{
let width = width.max(2);
let cows: Vec<Cow<'a, str>> = match input.into() {
Cow::Borrowed(s) => s.lines().map(Cow::Borrowed).collect(),
Cow::Owned(s) => s.lines().map(ToOwned::to_owned).map(Cow::Owned).collect(),
};
WrappedLinesIterator { iter: cows.into_iter(), curr: None, width }
}
}
impl<'a> Iterator for WrappedLinesIterator<'a> {
type Item = (Cow<'a, str>, usize);
fn next(&mut self) -> Option<Self::Item> {
if self.curr.is_none() {
self.curr = self.iter.next();
}
if let Some(s) = self.curr.take() {
let width = UnicodeWidthStr::width(s.as_ref());
if width <= self.width {
return Some((s, width));
} else {
let (prefix, s1) = take_width(s, self.width);
self.curr = Some(s1);
return Some(prefix);
}
} else {
return None;
}
}
}
pub fn wrap<'a, T>(input: T, width: usize) -> WrappedLinesIterator<'a>
where
T: Into<Cow<'a, str>>,
{
WrappedLinesIterator::new(input, width)
}
pub fn wrapped_text<'a, T>(s: T, width: usize, style: Style) -> Text<'a>
where
T: Into<Cow<'a, str>>,
{
let mut text = Text::default();
for (line, w) in wrap(s, width) {
let space = space_span(width.saturating_sub(w), style);
let spans = Spans(vec![Span::styled(line, style), space]);
text.lines.push(spans);
}
return text;
}
pub fn space(width: usize) -> String {
" ".repeat(width)
}
pub fn space_span(width: usize, style: Style) -> Span<'static> {
Span::styled(space(width), style)
}
pub fn space_text(width: usize, style: Style) -> Text<'static> {
space_span(width, style).into()
}
pub fn join_cell_text<'a>(texts: Vec<(Text<'a>, usize)>, join: Span<'a>, style: Style) -> Text<'a> {
let height = texts.iter().map(|t| t.0.height()).max().unwrap_or(0);
let mut text = Text { lines: vec![Spans(vec![join.clone()]); height] };
for (mut t, w) in texts.into_iter() {
for i in 0..height {
if let Some(spans) = t.lines.get_mut(i) {
text.lines[i].0.append(&mut spans.0);
} else {
text.lines[i].0.push(space_span(w, style));
}
text.lines[i].0.push(join.clone());
}
}
text
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn test_wrapped_lines_ascii() {
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
let mut iter = wrap(s, 100);
assert_eq!(iter.next(), Some((Cow::Borrowed("hello world!"), 12)));
assert_eq!(iter.next(), Some((Cow::Borrowed("abcdefghijklmnopqrstuvwxyz"), 26)));
assert_eq!(iter.next(), Some((Cow::Borrowed("goodbye"), 7)));
assert_eq!(iter.next(), None);
let mut iter = wrap(s, 5);
assert_eq!(iter.next(), Some((Cow::Borrowed("hello"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed(" worl"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("d!"), 2)));
assert_eq!(iter.next(), Some((Cow::Borrowed("abcde"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("fghij"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("klmno"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("pqrst"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("uvwxy"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("z"), 1)));
assert_eq!(iter.next(), Some((Cow::Borrowed("goodb"), 5)));
assert_eq!(iter.next(), Some((Cow::Borrowed("ye"), 2)));
assert_eq!(iter.next(), None);
}
#[test]
fn test_wrapped_lines_unicode() {
let s = "";
let mut iter = wrap(s, 14);
assert_eq!(iter.next(), Some((Cow::Borrowed(s), 14)));
assert_eq!(iter.next(), None);
let mut iter = wrap(s, 5);
assert_eq!(iter.next(), Some((Cow::Borrowed(""), 4)));
assert_eq!(iter.next(), Some((Cow::Borrowed(""), 4)));
assert_eq!(iter.next(), Some((Cow::Borrowed(""), 4)));
assert_eq!(iter.next(), Some((Cow::Borrowed(""), 2)));
assert_eq!(iter.next(), None);
}
}

View File

@@ -4,7 +4,12 @@ use std::collections::hash_map::Entry;
use matrix_sdk::{ use matrix_sdk::{
encryption::verification::{format_emojis, SasVerification}, encryption::verification::{format_emojis, SasVerification},
room::{Room as MatrixRoom, RoomMember}, room::{Room as MatrixRoom, RoomMember},
ruma::{events::room::member::MembershipState, OwnedRoomId, RoomId}, ruma::{
events::room::member::MembershipState,
events::tag::{TagName, Tags},
OwnedRoomId,
RoomId,
},
DisplayName, DisplayName,
}; };
@@ -51,19 +56,19 @@ use modalkit::{
}, },
}; };
use crate::{ use crate::base::{
base::{
ChatStore, ChatStore,
IambBufferId, IambBufferId,
IambError,
IambId, IambId,
IambInfo, IambInfo,
IambResult, IambResult,
MessageAction,
ProgramAction, ProgramAction,
ProgramContext, ProgramContext,
ProgramStore, ProgramStore,
RoomAction, RoomAction,
}, SendAction,
message::user_style,
}; };
use self::{room::RoomState, welcome::WelcomeState}; use self::{room::RoomState, welcome::WelcomeState};
@@ -105,6 +110,71 @@ fn selected_text(s: &str, selected: bool) -> Text {
Text::from(selected_span(s, selected)) Text::from(selected_span(s, selected))
} }
fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering {
let ca1 = a.canonical_alias();
let ca2 = b.canonical_alias();
let ord = match (ca1, ca2) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(ca1), Some(ca2)) => ca1.cmp(&ca2),
};
ord.then_with(|| a.room_id().cmp(b.room_id()))
}
fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering {
let (fava, lowa) = a
.as_ref()
.map(|tags| {
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
})
.unwrap_or((false, false));
let (favb, lowb) = b
.as_ref()
.map(|tags| {
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
})
.unwrap_or((false, false));
// If a has Favorite and b doesn't, it should sort earlier in room list.
let cmpf = favb.cmp(&fava);
// If a has LowPriority and b doesn't, it should sort later in room list.
let cmpl = lowa.cmp(&lowb);
cmpl.then(cmpf)
}
fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) {
if tags.is_empty() {
return;
}
spans.push(Span::styled(" (", style));
for (i, tag) in tags.keys().enumerate() {
if i > 0 {
spans.push(Span::styled(", ", style));
}
match tag {
TagName::Favorite => spans.push(Span::styled("Favorite", style)),
TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
TagName::User(tag) => {
spans.push(Span::styled("User Tag: ", style));
spans.push(Span::styled(tag.as_ref(), style));
},
tag => spans.push(Span::styled(format!("{:?}", tag), style)),
}
}
spans.push(Span::styled(")", style));
}
#[inline] #[inline]
fn room_prompt( fn room_prompt(
room_id: &RoomId, room_id: &RoomId,
@@ -168,19 +238,42 @@ impl IambWindow {
} }
} }
pub fn room_command( pub async fn message_command(
&mut self,
act: MessageAction,
ctx: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
if let IambWindow::Room(w) = self {
w.message_command(act, ctx, store).await
} else {
return Err(IambError::NoSelectedRoom.into());
}
}
pub async fn room_command(
&mut self, &mut self,
act: RoomAction, act: RoomAction,
ctx: ProgramContext, ctx: ProgramContext,
store: &mut ProgramStore, store: &mut ProgramStore,
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
if let IambWindow::Room(w) = self { if let IambWindow::Room(w) = self {
w.room_command(act, ctx, store) w.room_command(act, ctx, store).await
} else { } else {
let msg = "No room currently focused!"; return Err(IambError::NoSelectedRoomOrSpace.into());
let err = UIError::Failure(msg.into()); }
}
return Err(err); pub async fn send_command(
&mut self,
act: SendAction,
ctx: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
if let IambWindow::Room(w) = self {
w.send_command(act, ctx, store).await
} else {
return Err(IambError::NoSelectedRoom.into());
} }
} }
} }
@@ -284,8 +377,13 @@ impl WindowOps<IambInfo> for IambWindow {
IambWindow::Room(state) => state.draw(area, buf, focused, store), IambWindow::Room(state) => state.draw(area, buf, focused, store),
IambWindow::DirectList(state) => { IambWindow::DirectList(state) => {
let dms = store.application.worker.direct_messages(); let dms = store.application.worker.direct_messages();
let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store)); let mut items = dms
state.set(items.collect()); .into_iter()
.map(|(id, name, tags)| DirectItem::new(id, name, tags, store))
.collect::<Vec<_>>();
items.sort();
state.set(items);
List::new(store) List::new(store)
.empty_message("No direct messages yet!") .empty_message("No direct messages yet!")
@@ -306,9 +404,14 @@ impl WindowOps<IambInfo> for IambWindow {
.render(area, buf, state); .render(area, buf, state);
}, },
IambWindow::RoomList(state) => { IambWindow::RoomList(state) => {
let joined = store.application.worker.joined_rooms(); let joined = store.application.worker.active_rooms();
let items = joined.into_iter().map(|(id, name)| RoomItem::new(id, name, store)); let mut items = joined
state.set(items.collect()); .into_iter()
.map(|(room, name, tags)| RoomItem::new(room, name, tags, store))
.collect::<Vec<_>>();
items.sort();
state.set(items);
List::new(store) List::new(store)
.empty_message("You haven't joined any rooms yet") .empty_message("You haven't joined any rooms yet")
@@ -429,8 +532,8 @@ impl Window<IambInfo> for IambWindow {
fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> { fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
match id { match id {
IambId::Room(room_id) => { IambId::Room(room_id) => {
let (room, name) = store.application.worker.get_room(room_id)?; let (room, name, tags) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, store); let room = RoomState::new(room, name, tags, store);
return Ok(room.into()); return Ok(room.into());
}, },
@@ -477,8 +580,8 @@ impl Window<IambInfo> for IambWindow {
let room_id = worker.join_room(v.key().to_string())?; let room_id = worker.join_room(v.key().to_string())?;
v.insert(room_id.clone()); v.insert(room_id.clone());
let (room, name) = store.application.worker.get_room(room_id)?; let (room, name, tags) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, store); let room = RoomState::new(room, name, tags, store);
Ok(room.into()) Ok(room.into())
}, },
@@ -505,16 +608,44 @@ impl Window<IambInfo> for IambWindow {
#[derive(Clone)] #[derive(Clone)]
pub struct RoomItem { pub struct RoomItem {
room: MatrixRoom, room: MatrixRoom,
tags: Option<Tags>,
name: String, name: String,
} }
impl RoomItem { impl RoomItem {
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self { fn new(
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
let name = name.to_string(); let name = name.to_string();
store.application.set_room_name(room.room_id(), name.as_str()); let info = store.application.get_room_info(room.room_id().to_owned());
info.name = name.clone().into();
info.tags = tags.clone();
RoomItem { room, name } RoomItem { room, tags, name }
}
}
impl PartialEq for RoomItem {
fn eq(&self, other: &Self) -> bool {
self.room.room_id() == other.room.room_id()
}
}
impl Eq for RoomItem {}
impl Ord for RoomItem {
fn cmp(&self, other: &Self) -> Ordering {
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
}
}
impl PartialOrd for RoomItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
} }
} }
@@ -526,8 +657,17 @@ impl ToString for RoomItem {
impl ListItem<IambInfo> for RoomItem { impl ListItem<IambInfo> for RoomItem {
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
if let Some(tags) = &self.tags {
let style = selected_style(selected);
let mut spans = vec![Span::styled(self.name.as_str(), style)];
append_tags(tags, &mut spans, style);
Text::from(Spans(spans))
} else {
selected_text(self.name.as_str(), selected) selected_text(self.name.as_str(), selected)
} }
}
fn get_word(&self) -> Option<String> { fn get_word(&self) -> Option<String> {
self.room.room_id().to_string().into() self.room.room_id().to_string().into()
@@ -548,16 +688,22 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
#[derive(Clone)] #[derive(Clone)]
pub struct DirectItem { pub struct DirectItem {
room: MatrixRoom, room: MatrixRoom,
tags: Option<Tags>,
name: String, name: String,
} }
impl DirectItem { impl DirectItem {
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self { fn new(
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
let name = name.to_string(); let name = name.to_string();
store.application.set_room_name(room.room_id(), name.as_str()); store.application.set_room_name(room.room_id(), name.as_str());
DirectItem { room, name } DirectItem { room, tags, name }
} }
} }
@@ -569,14 +715,43 @@ impl ToString for DirectItem {
impl ListItem<IambInfo> for DirectItem { impl ListItem<IambInfo> for DirectItem {
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
if let Some(tags) = &self.tags {
let style = selected_style(selected);
let mut spans = vec![Span::styled(self.name.as_str(), style)];
append_tags(tags, &mut spans, style);
Text::from(Spans(spans))
} else {
selected_text(self.name.as_str(), selected) selected_text(self.name.as_str(), selected)
} }
}
fn get_word(&self) -> Option<String> { fn get_word(&self) -> Option<String> {
self.room.room_id().to_string().into() self.room.room_id().to_string().into()
} }
} }
impl PartialEq for DirectItem {
fn eq(&self, other: &Self) -> bool {
self.room.room_id() == other.room.room_id()
}
}
impl Eq for DirectItem {}
impl Ord for DirectItem {
fn cmp(&self, other: &Self) -> Ordering {
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
}
}
impl PartialOrd for DirectItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
fn prompt( fn prompt(
&mut self, &mut self,
@@ -604,6 +779,26 @@ impl SpaceItem {
} }
} }
impl PartialEq for SpaceItem {
fn eq(&self, other: &Self) -> bool {
self.room.room_id() == other.room.room_id()
}
}
impl Eq for SpaceItem {}
impl Ord for SpaceItem {
fn cmp(&self, other: &Self) -> Ordering {
room_cmp(&self.room, &other.room)
}
}
impl PartialOrd for SpaceItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp(other).into()
}
}
impl ToString for SpaceItem { impl ToString for SpaceItem {
fn to_string(&self) -> String { fn to_string(&self) -> String {
return self.room.room_id().to_string(); return self.room.room_id().to_string();
@@ -845,15 +1040,18 @@ impl ToString for MemberItem {
} }
impl ListItem<IambInfo> for MemberItem { impl ListItem<IambInfo> for MemberItem {
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { fn show(
let mut style = user_style(self.member.user_id().as_str()); &self,
selected: bool,
_: &ViewportContext<ListCursor>,
store: &mut ProgramStore,
) -> Text {
let mut user = store.application.settings.get_user_span(self.member.user_id());
if selected { if selected {
style = style.add_modifier(StyleModifier::REVERSED); user.style = user.style.add_modifier(StyleModifier::REVERSED);
} }
let user = Span::styled(self.to_string(), style);
let state = match self.member.membership() { let state = match self.member.membership() {
MembershipState::Ban => Span::raw(" (banned)").into(), MembershipState::Ban => Span::raw(" (banned)").into(),
MembershipState::Invite => Span::raw(" (invited)").into(), MembershipState::Invite => Span::raw(" (invited)").into(),

View File

@@ -1,11 +1,34 @@
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use matrix_sdk::{ use matrix_sdk::{
attachment::AttachmentConfig,
media::{MediaFormat, MediaRequest},
room::Room as MatrixRoom, room::Room as MatrixRoom,
ruma::{OwnedRoomId, RoomId}, ruma::{
events::room::message::{
MessageType,
OriginalRoomMessageEvent,
Relation,
Replacement,
RoomMessageEventContent,
TextMessageEventContent,
},
OwnedRoomId,
RoomId,
},
}; };
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
use modalkit::{ use modalkit::{
tui::{
buffer::Buffer,
layout::Rect,
text::{Span, Spans},
widgets::{Paragraph, StatefulWidget, Widget},
},
widgets::textbox::{TextBox, TextBoxState}, widgets::textbox::{TextBox, TextBoxState},
widgets::TerminalCursor, widgets::TerminalCursor,
widgets::{PromptActions, WindowOps}, widgets::{PromptActions, WindowOps},
@@ -18,10 +41,12 @@ use modalkit::editing::{
EditResult, EditResult,
Editable, Editable,
EditorAction, EditorAction,
InfoMessage,
Jumpable, Jumpable,
PromptAction, PromptAction,
Promptable, Promptable,
Scrollable, Scrollable,
UIError,
}, },
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle}, base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle},
context::Resolve, context::Resolve,
@@ -32,14 +57,20 @@ use modalkit::editing::{
use crate::base::{ use crate::base::{
IambAction, IambAction,
IambBufferId, IambBufferId,
IambError,
IambInfo, IambInfo,
IambResult, IambResult,
MessageAction,
ProgramAction, ProgramAction,
ProgramContext, ProgramContext,
ProgramStore, ProgramStore,
RoomFocus, RoomFocus,
RoomInfo,
SendAction,
}; };
use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
use super::scrollback::{Scrollback, ScrollbackState}; use super::scrollback::{Scrollback, ScrollbackState};
pub struct ChatState { pub struct ChatState {
@@ -52,6 +83,9 @@ pub struct ChatState {
scrollback: ScrollbackState, scrollback: ScrollbackState,
focus: RoomFocus, focus: RoomFocus,
reply_to: Option<MessageKey>,
editing: Option<MessageKey>,
} }
impl ChatState { impl ChatState {
@@ -72,9 +106,289 @@ impl ChatState {
scrollback, scrollback,
focus: RoomFocus::MessageBar, focus: RoomFocus::MessageBar,
reply_to: None,
editing: None,
} }
} }
fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
let key = self.reply_to.as_ref()?;
let msg = info.messages.get(key)?;
if let MessageEvent::Original(ev) = &msg.event {
Some(ev)
} else {
None
}
}
fn reset(&mut self) -> EditRope {
self.reply_to = None;
self.editing = None;
self.tbox.reset()
}
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
if let Some(room) = store.application.worker.client.get_room(self.id()) {
self.room = room;
}
}
pub async fn message_command(
&mut self,
act: MessageAction,
_: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
let client = &store.application.worker.client;
let settings = &store.application.settings;
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
let msg = self.scrollback.get_mut(info).ok_or(IambError::NoSelectedMessage)?;
match act {
MessageAction::Cancel => {
self.reply_to = None;
self.editing = None;
Ok(None)
},
MessageAction::Download(filename, force) => {
if let MessageEvent::Original(ev) = &msg.event {
let media = client.media();
let mut filename = match filename {
Some(f) => PathBuf::from(f),
None => settings.dirs.downloads.clone(),
};
let source = match &ev.content.msgtype {
MessageType::Audio(c) => {
if filename.is_dir() {
filename.push(c.body.as_str());
}
c.source.clone()
},
MessageType::File(c) => {
if filename.is_dir() {
if let Some(name) = &c.filename {
filename.push(name);
} else {
filename.push(c.body.as_str());
}
}
c.source.clone()
},
MessageType::Image(c) => {
if filename.is_dir() {
filename.push(c.body.as_str());
}
c.source.clone()
},
MessageType::Video(c) => {
if filename.is_dir() {
filename.push(c.body.as_str());
}
c.source.clone()
},
_ => {
return Err(IambError::NoAttachment.into());
},
};
if !force && filename.exists() {
let msg = format!(
"The file {} already exists; use :download! to overwrite it.",
filename.display()
);
let err = UIError::Failure(msg);
return Err(err);
}
let req = MediaRequest { source, format: MediaFormat::File };
let bytes =
media.get_media_content(&req, true).await.map_err(IambError::from)?;
fs::write(filename.as_path(), bytes.as_slice())?;
msg.downloaded = true;
let info = InfoMessage::from(format!(
"Attachment downloaded to {}",
filename.display()
));
return Ok(info.into());
}
Err(IambError::NoAttachment.into())
},
MessageAction::Edit => {
if msg.sender != settings.profile.user_id {
let msg = "Cannot edit messages sent by someone else";
let err = UIError::Failure(msg.into());
return Err(err);
}
let ev = match &msg.event {
MessageEvent::Original(ev) => &ev.content,
MessageEvent::Local(_, ev) => ev.deref(),
_ => {
let msg = "Cannot edit a redacted message";
let err = UIError::Failure(msg.into());
return Err(err);
},
};
let text = match &ev.msgtype {
MessageType::Text(msg) => msg.body.as_str(),
_ => {
let msg = "Cannot edit a non-text message";
let err = UIError::Failure(msg.into());
return Err(err);
},
};
self.tbox.set_text(text);
self.editing = self.scrollback.get_key(info);
self.focus = RoomFocus::MessageBar;
Ok(None)
},
MessageAction::Redact(reason) => {
let room = store
.application
.worker
.client
.get_joined_room(self.id())
.ok_or(IambError::NotJoined)?;
let event_id = match &msg.event {
MessageEvent::Original(ev) => ev.event_id.clone(),
MessageEvent::Local(event_id, _) => event_id.clone(),
MessageEvent::Redacted(_) => {
let msg = "";
let err = UIError::Failure(msg.into());
return Err(err);
},
};
let event_id = event_id.as_ref();
let reason = reason.as_deref();
let _ = room.redact(event_id, reason, None).await.map_err(IambError::from)?;
Ok(None)
},
MessageAction::Reply => {
self.reply_to = self.scrollback.get_key(info);
self.focus = RoomFocus::MessageBar;
Ok(None)
},
}
}
pub async fn send_command(
&mut self,
act: SendAction,
_: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
let room = store
.application
.worker
.client
.get_joined_room(self.id())
.ok_or(IambError::NotJoined)?;
let info = store.application.rooms.entry(self.id().to_owned()).or_default();
let mut show_echo = true;
let (event_id, msg) = match act {
SendAction::Submit => {
let msg = self.tbox.get_text();
if msg.is_empty() {
return Ok(None);
}
let msg = TextMessageEventContent::markdown(msg);
let msg = MessageType::Text(msg);
let mut msg = RoomMessageEventContent::new(msg);
if let Some((_, event_id)) = &self.editing {
msg.relates_to = Some(Relation::Replacement(Replacement::new(
event_id.clone(),
Box::new(msg.clone()),
)));
show_echo = false;
} else if let Some(m) = self.get_reply_to(info) {
// XXX: Switch to RoomMessageEventContent::reply() once it's stable?
msg = msg.make_reply_to(m);
}
// XXX: second parameter can be a locally unique transaction id.
// Useful for doing retries.
let resp = room.send(msg.clone(), None).await.map_err(IambError::from)?;
let event_id = resp.event_id;
// Reset message bar state now that it's been sent.
self.reset();
(event_id, msg)
},
SendAction::Upload(file) => {
let path = Path::new(file.as_str());
let mime = mime_guess::from_path(path).first_or(mime::APPLICATION_OCTET_STREAM);
let bytes = fs::read(path)?;
let name = path
.file_name()
.map(OsStr::to_string_lossy)
.unwrap_or_else(|| Cow::from("Attachment"));
let config = AttachmentConfig::new();
let resp = room
.send_attachment(name.as_ref(), &mime, bytes.as_ref(), config)
.await
.map_err(IambError::from)?;
// Mock up the local echo message for the scrollback.
let msg = TextMessageEventContent::plain(format!("[Attached File: {}]", name));
let msg = MessageType::Text(msg);
let msg = RoomMessageEventContent::new(msg);
(resp.event_id, msg)
},
};
if show_echo {
let user = store.application.settings.profile.user_id.clone();
let key = (MessageTimeStamp::LocalEcho, event_id.clone());
let msg = MessageEvent::Local(event_id, msg.into());
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
info.messages.insert(key, msg);
}
// Jump to the end of the scrollback to show the message.
self.scrollback.goto_latest();
Ok(None)
}
pub fn focus_toggle(&mut self) { pub fn focus_toggle(&mut self) {
self.focus = match self.focus { self.focus = match self.focus {
RoomFocus::Scrollback => RoomFocus::MessageBar, RoomFocus::Scrollback => RoomFocus::MessageBar,
@@ -100,7 +414,7 @@ impl ChatState {
return; return;
} }
if !store.application.settings.tunables.typing_notice { if !store.application.settings.tunables.typing_notice_send {
return; return;
} }
@@ -147,6 +461,9 @@ impl WindowOps<IambInfo> for ChatState {
scrollback: self.scrollback.dup(store), scrollback: self.scrollback.dup(store),
focus: self.focus, focus: self.focus,
reply_to: None,
editing: None,
} }
} }
@@ -229,17 +546,9 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
ctx: &ProgramContext, ctx: &ProgramContext,
_: &mut ProgramStore, _: &mut ProgramStore,
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
let txt = self.tbox.reset_text(); let act = SendAction::Submit;
let act = if txt.is_empty() { Ok(vec![(IambAction::from(act).into(), ctx.clone())])
vec![]
} else {
let act = IambAction::SendMessage(self.room_id.clone(), txt).into();
vec![(act, ctx.clone())]
};
Ok(act)
} }
fn abort( fn abort(
@@ -254,7 +563,7 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
return Ok(vec![]); return Ok(vec![]);
} }
let text = self.tbox.reset().trim(); let text = self.reset().trim();
if text.is_empty() { if text.is_empty() {
let _ = self.sent.end(); let _ = self.sent.end();
@@ -328,15 +637,40 @@ impl<'a> StatefulWidget for Chat<'a> {
let lines = state.tbox.has_lines(5).max(1) as u16; let lines = state.tbox.has_lines(5).max(1) as u16;
let drawh = area.height; let drawh = area.height;
let texth = lines.min(drawh).clamp(1, 5); let texth = lines.min(drawh).clamp(1, 5);
let scrollh = drawh.saturating_sub(texth); let desch = if state.reply_to.is_some() {
drawh.saturating_sub(texth).min(1)
} else {
0
};
let scrollh = drawh.saturating_sub(texth).saturating_sub(desch);
let scrollarea = Rect::new(area.x, area.y, area.width, scrollh); let scrollarea = Rect::new(area.x, area.y, area.width, scrollh);
let textarea = Rect::new(scrollarea.x, scrollarea.y + scrollh, scrollarea.width, texth); let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
let scrollback_focused = state.focus.is_scrollback() && self.focused; let scrollback_focused = state.focus.is_scrollback() && self.focused;
let scrollback = Scrollback::new(self.store).focus(scrollback_focused); let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
scrollback.render(scrollarea, buf, &mut state.scrollback); scrollback.render(scrollarea, buf, &mut state.scrollback);
let desc_spans = match (&state.editing, &state.reply_to) {
(None, None) => None,
(Some(_), _) => Some(Spans::from("Editing message")),
(_, Some(_)) => {
state.reply_to.as_ref().and_then(|k| {
let room = self.store.application.rooms.get(state.id())?;
let msg = room.messages.get(k)?;
let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
let spans = Spans(vec![Span::from("Replying to "), user]);
spans.into()
})
},
};
if let Some(desc_spans) = desc_spans {
Paragraph::new(desc_spans).render(descarea, buf);
}
let prompt = if self.focused { "> " } else { " " }; let prompt = if self.focused { "> " } else { " " };
let tbox = TextBox::new().prompt(prompt); let tbox = TextBox::new().prompt(prompt);

View File

@@ -1,13 +1,21 @@
use matrix_sdk::room::Room as MatrixRoom; use matrix_sdk::{
use matrix_sdk::ruma::RoomId; room::{Invited, Room as MatrixRoom},
use matrix_sdk::DisplayName; ruma::{
events::{
room::{name::RoomNameEventContent, topic::RoomTopicEventContent},
tag::{TagInfo, Tags},
},
RoomId,
},
DisplayName,
};
use modalkit::tui::{ use modalkit::tui::{
buffer::Buffer, buffer::Buffer,
layout::Rect, layout::{Alignment, Rect},
style::{Modifier as StyleModifier, Style}, style::{Modifier as StyleModifier, Style},
text::{Span, Spans}, text::{Span, Spans, Text},
widgets::StatefulWidget, widgets::{Paragraph, StatefulWidget, Widget},
}; };
use modalkit::{ use modalkit::{
@@ -21,6 +29,7 @@ use modalkit::{
PromptAction, PromptAction,
Promptable, Promptable,
Scrollable, Scrollable,
UIError,
}, },
editing::base::{ editing::base::{
Axis, Axis,
@@ -37,13 +46,17 @@ use modalkit::{
}; };
use crate::base::{ use crate::base::{
IambError,
IambId, IambId,
IambInfo, IambInfo,
IambResult, IambResult,
MessageAction,
ProgramAction, ProgramAction,
ProgramContext, ProgramContext,
ProgramStore, ProgramStore,
RoomAction, RoomAction,
RoomField,
SendAction,
}; };
use self::chat::ChatState; use self::chat::ChatState;
@@ -80,10 +93,16 @@ impl From<SpaceState> for RoomState {
} }
impl RoomState { impl RoomState {
pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self { pub fn new(
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
let room_id = room.room_id().to_owned(); let room_id = room.room_id().to_owned();
let info = store.application.get_room_info(room_id); let info = store.application.get_room_info(room_id);
info.name = name.to_string().into(); info.name = name.to_string().into();
info.tags = tags;
if room.is_space() { if room.is_space() {
SpaceState::new(room).into() SpaceState::new(room).into()
@@ -92,13 +111,106 @@ impl RoomState {
} }
} }
pub fn room_command( pub fn refresh_room(&mut self, store: &mut ProgramStore) {
match self {
RoomState::Chat(chat) => chat.refresh_room(store),
RoomState::Space(space) => space.refresh_room(store),
}
}
fn draw_invite(
&self,
invited: Invited,
area: Rect,
buf: &mut Buffer,
store: &mut ProgramStore,
) {
let inviter = store.application.worker.get_inviter(invited.clone());
let name = match invited.canonical_alias() {
Some(alias) => alias.to_string(),
None => format!("{:?}", store.application.get_room_title(self.id())),
};
let mut invited = vec![Span::from(format!(
"You have been invited to join {}",
name
))];
if let Ok(Some(inviter)) = &inviter {
invited.push(Span::from(" by "));
invited.push(store.application.settings.get_user_span(inviter.user_id()));
}
let l1 = Spans(invited);
let l2 = Spans::from(
"You can run `:invite accept` or `:invite reject` to accept or reject this invitation.",
);
let text = Text { lines: vec![l1, l2] };
Paragraph::new(text).alignment(Alignment::Center).render(area, buf);
return;
}
pub async fn message_command(
&mut self,
act: MessageAction,
ctx: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
match self {
RoomState::Chat(chat) => chat.message_command(act, ctx, store).await,
RoomState::Space(_) => Err(IambError::NoSelectedMessage.into()),
}
}
pub async fn send_command(
&mut self,
act: SendAction,
ctx: ProgramContext,
store: &mut ProgramStore,
) -> IambResult<EditInfo> {
match self {
RoomState::Chat(chat) => chat.send_command(act, ctx, store).await,
RoomState::Space(_) => Err(IambError::NoSelectedRoom.into()),
}
}
pub async fn room_command(
&mut self, &mut self,
act: RoomAction, act: RoomAction,
_: ProgramContext, _: ProgramContext,
store: &mut ProgramStore, store: &mut ProgramStore,
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
match act { match act {
RoomAction::InviteAccept => {
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
room.accept_invitation().await.map_err(IambError::from)?;
Ok(vec![])
} else {
Err(IambError::NotInvited.into())
}
},
RoomAction::InviteReject => {
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
room.reject_invitation().await.map_err(IambError::from)?;
Ok(vec![])
} else {
Err(IambError::NotInvited.into())
}
},
RoomAction::InviteSend(user) => {
if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?;
Ok(vec![])
} else {
Err(IambError::NotJoined.into())
}
},
RoomAction::Members(mut cmd) => { RoomAction::Members(mut cmd) => {
let width = Count::Exact(30); let width = Count::Exact(30);
let act = let act =
@@ -109,8 +221,50 @@ impl RoomState {
Ok(vec![(act, cmd.context.take())]) Ok(vec![(act, cmd.context.take())])
}, },
RoomAction::Set(field) => { RoomAction::Set(field, value) => {
store.application.worker.set_room(self.id().to_owned(), field)?; let room = store
.application
.get_joined_room(self.id())
.ok_or(UIError::Application(IambError::NotJoined))?;
match field {
RoomField::Name => {
let ev = RoomNameEventContent::new(value.into());
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
RoomField::Tag(tag) => {
let mut info = TagInfo::new();
info.order = Some(1.0);
let _ = room.set_tag(tag, info).await.map_err(IambError::from)?;
},
RoomField::Topic => {
let ev = RoomTopicEventContent::new(value);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
}
Ok(vec![])
},
RoomAction::Unset(field) => {
let room = store
.application
.get_joined_room(self.id())
.ok_or(UIError::Application(IambError::NotJoined))?;
match field {
RoomField::Name => {
let ev = RoomNameEventContent::new(None);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
RoomField::Tag(tag) => {
let _ = room.remove_tag(tag).await.map_err(IambError::from)?;
},
RoomField::Topic => {
let ev = RoomTopicEventContent::new("".into());
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
}
Ok(vec![]) Ok(vec![])
}, },
@@ -209,6 +363,14 @@ impl TerminalCursor for RoomState {
impl WindowOps<IambInfo> for RoomState { impl WindowOps<IambInfo> for RoomState {
fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
if let MatrixRoom::Invited(_) = self.room() {
self.refresh_room(store);
}
if let MatrixRoom::Invited(invited) = self.room() {
self.draw_invite(invited.clone(), area, buf, store);
}
match self { match self {
RoomState::Chat(chat) => chat.draw(area, buf, focused, store), RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
RoomState::Space(space) => { RoomState::Space(space) => {

View File

@@ -61,6 +61,7 @@ use modalkit::editing::{
use crate::{ use crate::{
base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo}, base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
config::ApplicationSettings,
message::{Message, MessageCursor, MessageKey}, message::{Message, MessageCursor, MessageKey},
}; };
@@ -103,11 +104,26 @@ fn nth_after(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageCursor {
} }
pub struct ScrollbackState { pub struct ScrollbackState {
/// The room identifier.
room_id: OwnedRoomId, room_id: OwnedRoomId,
/// The buffer identifier used for saving marks, etc.
id: IambBufferId, id: IambBufferId,
/// The currently selected message in the scrollback.
cursor: MessageCursor, cursor: MessageCursor,
/// Contextual info about the viewport used during rendering.
viewctx: ViewportContext<MessageCursor>, viewctx: ViewportContext<MessageCursor>,
/// The jumplist of visited messages.
jumped: HistoryList<MessageCursor>, jumped: HistoryList<MessageCursor>,
/// Whether the full message should be drawn during the next render() call.
///
/// This is used to ensure that ^E/^Y work nicely when the cursor is currently
/// on a multiline message.
show_full_on_redraw: bool,
} }
impl ScrollbackState { impl ScrollbackState {
@@ -116,8 +132,20 @@ impl ScrollbackState {
let cursor = MessageCursor::default(); let cursor = MessageCursor::default();
let viewctx = ViewportContext::default(); let viewctx = ViewportContext::default();
let jumped = HistoryList::default(); let jumped = HistoryList::default();
let show_full_on_redraw = false;
ScrollbackState { room_id, id, cursor, viewctx, jumped } ScrollbackState {
room_id,
id,
cursor,
viewctx,
jumped,
show_full_on_redraw,
}
}
pub fn goto_latest(&mut self) {
self.cursor = MessageCursor::latest();
} }
/// Set the dimensions and placement within the terminal window for this list. /// Set the dimensions and placement within the terminal window for this list.
@@ -125,6 +153,21 @@ impl ScrollbackState {
self.viewctx.dimensions = (area.width as usize, area.height as usize); self.viewctx.dimensions = (area.width as usize, area.height as usize);
} }
pub fn get_key(&self, info: &mut RoomInfo) -> Option<MessageKey> {
self.cursor
.timestamp
.clone()
.or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
}
pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> {
if let Some(k) = &self.cursor.timestamp {
info.messages.get_mut(k)
} else {
info.messages.last_entry().map(|o| o.into_mut())
}
}
pub fn messages<'a>( pub fn messages<'a>(
&self, &self,
range: EditRange<MessageCursor>, range: EditRange<MessageCursor>,
@@ -148,7 +191,13 @@ impl ScrollbackState {
} }
} }
fn scrollview(&mut self, idx: MessageKey, pos: MovePosition, info: &RoomInfo) { fn scrollview(
&mut self,
idx: MessageKey,
pos: MovePosition,
info: &RoomInfo,
settings: &ApplicationSettings,
) {
let selidx = if let Some(key) = self.cursor.to_key(info) { let selidx = if let Some(key) = self.cursor.to_key(info) {
key key
} else { } else {
@@ -165,7 +214,7 @@ impl ScrollbackState {
for (key, item) in info.messages.range(..=&idx).rev() { for (key, item) in info.messages.range(..=&idx).rev() {
let sel = selidx == key; let sel = selidx == key;
let len = item.show(sel, &self.viewctx).lines.len(); let len = item.show(None, sel, &self.viewctx, info, settings).lines.len();
if key == &idx { if key == &idx {
lines += len / 2; lines += len / 2;
@@ -187,7 +236,7 @@ impl ScrollbackState {
for (key, item) in info.messages.range(..=&idx).rev() { for (key, item) in info.messages.range(..=&idx).rev() {
let sel = key == selidx; let sel = key == selidx;
let len = item.show(sel, &self.viewctx).lines.len(); let len = item.show(None, sel, &self.viewctx, info, settings).lines.len();
lines += len; lines += len;
@@ -202,7 +251,7 @@ impl ScrollbackState {
} }
} }
fn shift_cursor(&mut self, info: &RoomInfo) { fn shift_cursor(&mut self, info: &RoomInfo, settings: &ApplicationSettings) {
let last_key = if let Some(k) = info.messages.last_key_value() { let last_key = if let Some(k) = info.messages.last_key_value() {
k.0 k.0
} else { } else {
@@ -227,7 +276,7 @@ impl ScrollbackState {
break; break;
} }
lines += item.show(false, &self.viewctx).height().max(1); lines += item.show(None, false, &self.viewctx, info, settings).height().max(1);
if lines >= self.viewctx.get_height() { if lines >= self.viewctx.get_height() {
// We've reached the end of the viewport; move cursor into it. // We've reached the end of the viewport; move cursor into it.
@@ -382,7 +431,7 @@ impl ScrollbackState {
continue; continue;
} }
if needle.is_match(msg.as_ref()) { if needle.is_match(msg.event.body().as_ref()) {
mc = MessageCursor::from(key.clone()).into(); mc = MessageCursor::from(key.clone()).into();
count -= 1; count -= 1;
} }
@@ -406,7 +455,7 @@ impl ScrollbackState {
break; break;
} }
if needle.is_match(msg.as_ref()) { if needle.is_match(msg.event.body().as_ref()) {
mc = MessageCursor::from(key.clone()).into(); mc = MessageCursor::from(key.clone()).into();
count -= 1; count -= 1;
} }
@@ -447,6 +496,7 @@ impl WindowOps<IambInfo> for ScrollbackState {
cursor: self.cursor.clone(), cursor: self.cursor.clone(),
viewctx: self.viewctx.clone(), viewctx: self.viewctx.clone(),
jumped: self.jumped.clone(), jumped: self.jumped.clone(),
show_full_on_redraw: false,
} }
} }
@@ -567,6 +617,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
self.cursor = pos; self.cursor = pos;
} }
self.show_full_on_redraw = true;
return Ok(None); return Ok(None);
}, },
EditAction::Yank => { EditAction::Yank => {
@@ -652,7 +704,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
let mut yanked = EditRope::from(""); let mut yanked = EditRope::from("");
for (_, msg) in self.messages(range, info) { for (_, msg) in self.messages(range, info) {
yanked += EditRope::from(msg.as_ref()); yanked += EditRope::from(msg.event.body());
yanked += EditRope::from('\n'); yanked += EditRope::from('\n');
} }
@@ -930,7 +982,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
ctx: &ProgramContext, ctx: &ProgramContext,
store: &mut ProgramStore, store: &mut ProgramStore,
) -> EditResult<EditInfo, IambInfo> { ) -> EditResult<EditInfo, IambInfo> {
let info = store.application.get_room_info(self.room_id.clone()); let info = store.application.rooms.entry(self.room_id.clone()).or_default();
let settings = &store.application.settings;
let mut corner = self.viewctx.corner.clone(); let mut corner = self.viewctx.corner.clone();
let last_key = if let Some(k) = info.messages.last_key_value() { let last_key = if let Some(k) = info.messages.last_key_value() {
@@ -956,7 +1009,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
for (key, item) in info.messages.range(..=&corner_key).rev() { for (key, item) in info.messages.range(..=&corner_key).rev() {
let sel = key == cursor_key; let sel = key == cursor_key;
let txt = item.show(sel, &self.viewctx); let txt = item.show(None, sel, &self.viewctx, info, settings);
let len = txt.height().max(1); let len = txt.height().max(1);
let max = len.saturating_sub(1); let max = len.saturating_sub(1);
@@ -982,7 +1035,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
MoveDir2D::Down => { MoveDir2D::Down => {
for (key, item) in info.messages.range(&corner_key..) { for (key, item) in info.messages.range(&corner_key..) {
let sel = key == cursor_key; let sel = key == cursor_key;
let txt = item.show(sel, &self.viewctx); let txt = item.show(None, sel, &self.viewctx, info, settings);
let len = txt.height().max(1); let len = txt.height().max(1);
let max = len.saturating_sub(1); let max = len.saturating_sub(1);
@@ -1018,7 +1071,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
} }
self.viewctx.corner = corner; self.viewctx.corner = corner;
self.shift_cursor(info); self.shift_cursor(info, settings);
Ok(None) Ok(None)
} }
@@ -1038,10 +1091,11 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
Err(err) Err(err)
}, },
Axis::Vertical => { Axis::Vertical => {
let info = store.application.get_room_info(self.room_id.clone()); let info = store.application.rooms.entry(self.room_id.clone()).or_default();
let settings = &store.application.settings;
if let Some(key) = self.cursor.to_key(info).cloned() { if let Some(key) = self.cursor.to_key(info).cloned() {
self.scrollview(key, pos, info); self.scrollview(key, pos, info, settings);
} }
Ok(None) Ok(None)
@@ -1126,6 +1180,7 @@ impl<'a> StatefulWidget for Scrollback<'a> {
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let info = self.store.application.rooms.entry(state.room_id.clone()).or_default(); let info = self.store.application.rooms.entry(state.room_id.clone()).or_default();
let settings = &self.store.application.settings;
let area = info.render_typing(area, buf, &self.store.application.settings); let area = info.render_typing(area, buf, &self.store.application.settings);
state.set_term_info(area); state.set_term_info(area);
@@ -1149,21 +1204,28 @@ impl<'a> StatefulWidget for Scrollback<'a> {
}; };
let corner = &state.viewctx.corner; let corner = &state.viewctx.corner;
let corner_key = match (&corner.timestamp, &cursor.timestamp) { let corner_key = if let Some(k) = &corner.timestamp {
(_, None) => nth_key_before(cursor_key.clone(), height, info), k.clone()
(None, _) => nth_key_before(cursor_key.clone(), height, info), } else {
(Some(k), _) => k.clone(), nth_key_before(cursor_key.clone(), height, info)
}; };
let foc = self.focused || cursor.timestamp.is_some();
let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
let mut lines = vec![]; let mut lines = vec![];
let mut sawit = false; let mut sawit = false;
let mut prev = None;
for (key, item) in info.messages.range(&corner_key..) { for (key, item) in info.messages.range(&corner_key..) {
let sel = key == cursor_key; let sel = key == cursor_key;
let txt = item.show(self.focused && sel, &state.viewctx); let txt = item.show(prev, foc && sel, &state.viewctx, info, settings);
prev = Some(item);
let incomplete_ok = !full || !sel;
for (row, line) in txt.lines.into_iter().enumerate() { for (row, line) in txt.lines.into_iter().enumerate() {
if sawit && lines.len() >= height { if sawit && lines.len() >= height && incomplete_ok {
// Check whether we've seen the first line of the // Check whether we've seen the first line of the
// selected message and can fill the screen. // selected message and can fill the screen.
break; break;
@@ -1198,7 +1260,13 @@ impl<'a> StatefulWidget for Scrollback<'a> {
y += 1; y += 1;
} }
let first_key = info.messages.first_key_value().map(|f| f.0.clone()); if settings.tunables.read_receipt_send && state.cursor.timestamp.is_none() {
// If the cursor is at the last message, then update the read marker.
info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone());
}
// Check whether we should load older messages for this room.
let first_key = info.messages.first_key_value().map(|(k, _)| k.clone());
if first_key == state.viewctx.corner.timestamp { if first_key == state.viewctx.corner.timestamp {
// If the top of the screen is the older message, load more. // If the top of the screen is the older message, load more.
self.store.application.mark_for_load(state.room_id.clone()); self.store.application.mark_for_load(state.room_id.clone());
@@ -1211,10 +1279,10 @@ mod tests {
use super::*; use super::*;
use crate::tests::*; use crate::tests::*;
#[test] #[tokio::test]
fn test_search_messages() { async fn test_search_messages() {
let room_id = TEST_ROOM1_ID.clone(); let room_id = TEST_ROOM1_ID.clone();
let mut store = mock_store(); let mut store = mock_store().await;
let mut scrollback = ScrollbackState::new(room_id.clone()); let mut scrollback = ScrollbackState::new(room_id.clone());
let ctx = ProgramContext::default(); let ctx = ProgramContext::default();
@@ -1255,9 +1323,9 @@ mod tests {
assert_eq!(scrollback.cursor, MSG1_KEY.clone().into()); assert_eq!(scrollback.cursor, MSG1_KEY.clone().into());
} }
#[test] #[tokio::test]
fn test_movement() { async fn test_movement() {
let mut store = mock_store(); let mut store = mock_store().await;
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone()); let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
let ctx = ProgramContext::default(); let ctx = ProgramContext::default();
@@ -1289,9 +1357,9 @@ mod tests {
assert_eq!(scrollback.cursor, MSG1_KEY.clone().into()); assert_eq!(scrollback.cursor, MSG1_KEY.clone().into());
} }
#[test] #[tokio::test]
fn test_dirscroll() { async fn test_dirscroll() {
let mut store = mock_store(); let mut store = mock_store().await;
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone()); let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
let ctx = ProgramContext::default(); let ctx = ProgramContext::default();
@@ -1423,9 +1491,9 @@ mod tests {
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4)); assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4));
} }
#[test] #[tokio::test]
fn test_cursorpos() { async fn test_cursorpos() {
let mut store = mock_store(); let mut store = mock_store().await;
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone()); let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
let ctx = ProgramContext::default(); let ctx = ProgramContext::default();

View File

@@ -31,6 +31,12 @@ impl SpaceState {
SpaceState { room_id, room, list } SpaceState { room_id, room, list }
} }
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
if let Some(room) = store.application.worker.client.get_room(self.id()) {
self.room = room;
}
}
pub fn room(&self) -> &MatrixRoom { pub fn room(&self) -> &MatrixRoom {
&self.room &self.room
} }
@@ -88,14 +94,20 @@ impl<'a> StatefulWidget for Space<'a> {
type State = SpaceState; type State = SpaceState;
fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
let members = self.store.application.worker.space_members(state.room_id.clone()).unwrap(); let members =
if let Ok(m) = self.store.application.worker.space_members(state.room_id.clone()) {
m
} else {
return;
};
let items = members let items = members
.into_iter() .into_iter()
.filter_map(|id| { .filter_map(|id| {
let (room, name) = self.store.application.worker.get_room(id.clone()).ok()?; let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?;
if id != state.room_id { if id != state.room_id {
Some(RoomItem::new(room, name, self.store)) Some(RoomItem::new(room, name, tags, self.store))
} else { } else {
None None
} }

View File

@@ -1,12 +1,14 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::{Debug, Formatter};
use std::fs::File; use std::fs::File;
use std::io::BufWriter; use std::io::BufWriter;
use std::str::FromStr; use std::str::FromStr;
use std::sync::mpsc::{sync_channel, Receiver, RecvTimeoutError, SyncSender}; use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use gethostname::gethostname; use gethostname::gethostname;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing::error; use tracing::error;
@@ -15,7 +17,7 @@ use matrix_sdk::{
encryption::verification::{SasVerification, Verification}, encryption::verification::{SasVerification, Verification},
event_handler::Ctx, event_handler::Ctx,
reqwest, reqwest,
room::{Messages, MessagesOptions, Room as MatrixRoom, RoomMember}, room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
ruma::{ ruma::{
api::client::{ api::client::{
room::create_room::v3::{Request as CreateRoomRequest, RoomPreset}, room::create_room::v3::{Request as CreateRoomRequest, RoomPreset},
@@ -31,20 +33,21 @@ use matrix_sdk::{
VerificationMethod, VerificationMethod,
}, },
room::{ room::{
message::{MessageType, RoomMessageEventContent, TextMessageEventContent}, message::{MessageType, RoomMessageEventContent},
name::RoomNameEventContent, name::RoomNameEventContent,
topic::RoomTopicEventContent, redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
}, },
tag::Tags,
typing::SyncTypingEvent, typing::SyncTypingEvent,
AnyMessageLikeEvent, AnyMessageLikeEvent,
AnyTimelineEvent, AnyTimelineEvent,
SyncMessageLikeEvent, SyncMessageLikeEvent,
SyncStateEvent, SyncStateEvent,
}, },
OwnedEventId,
OwnedRoomId, OwnedRoomId,
OwnedRoomOrAliasId, OwnedRoomOrAliasId,
OwnedUserId, OwnedUserId,
RoomVersionId,
}, },
Client, Client,
DisplayName, DisplayName,
@@ -54,8 +57,8 @@ use matrix_sdk::{
use modalkit::editing::action::{EditInfo, InfoMessage, UIError}; use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
use crate::{ use crate::{
base::{AsyncProgramStore, IambError, IambResult, SetRoomField, VerifyAction}, base::{AsyncProgramStore, IambError, IambResult, Receipts, VerifyAction},
message::{Message, MessageFetchResult, MessageTimeStamp}, message::MessageFetchResult,
ApplicationSettings, ApplicationSettings,
}; };
@@ -67,6 +70,7 @@ fn initial_devname() -> String {
format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
} }
#[derive(Debug)]
pub enum LoginStyle { pub enum LoginStyle {
SessionRestore(Session), SessionRestore(Session),
Password(String), Password(String),
@@ -95,29 +99,133 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) {
return (reply, response); return (reply, response);
} }
type EchoPair = (OwnedEventId, RoomMessageEventContent); async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> {
let mut rooms = vec![];
for room in client.joined_rooms() {
if let Ok(users) = room.active_members_no_sync().await {
let mut receipts = Receipts::new();
for member in users {
let res = room.user_read_receipt(member.user_id()).await;
if let Ok(Some((event_id, _))) = res {
let user_id = member.user_id().to_owned();
receipts.entry(event_id).or_default().push(user_id);
}
}
rooms.push((room.room_id().to_owned(), receipts));
}
}
return rooms;
}
pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
pub enum WorkerTask { pub enum WorkerTask {
DirectMessages(ClientReply<Vec<(MatrixRoom, DisplayName)>>), ActiveRooms(ClientReply<Vec<FetchedRoom>>),
DirectMessages(ClientReply<Vec<FetchedRoom>>),
Init(AsyncProgramStore, ClientReply<()>), Init(AsyncProgramStore, ClientReply<()>),
LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>), LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>),
Login(LoginStyle, ClientReply<IambResult<EditInfo>>), Login(LoginStyle, ClientReply<IambResult<EditInfo>>),
GetRoom(OwnedRoomId, ClientReply<IambResult<(MatrixRoom, DisplayName)>>), GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>),
GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>),
JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>),
JoinedRooms(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>), Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
SendMessage(OwnedRoomId, String, ClientReply<IambResult<EchoPair>>),
SetRoom(OwnedRoomId, SetRoomField, ClientReply<IambResult<()>>),
TypingNotice(OwnedRoomId), TypingNotice(OwnedRoomId),
Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>), Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>),
VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>), VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>),
} }
impl Debug for WorkerTask {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
WorkerTask::ActiveRooms(_) => {
f.debug_tuple("WorkerTask::ActiveRooms").field(&format_args!("_")).finish()
},
WorkerTask::DirectMessages(_) => {
f.debug_tuple("WorkerTask::DirectMessages")
.field(&format_args!("_"))
.finish()
},
WorkerTask::Init(_, _) => {
f.debug_tuple("WorkerTask::Init")
.field(&format_args!("_"))
.field(&format_args!("_"))
.finish()
},
WorkerTask::LoadOlder(room_id, from, n, _) => {
f.debug_tuple("WorkerTask::LoadOlder")
.field(room_id)
.field(from)
.field(n)
.field(&format_args!("_"))
.finish()
},
WorkerTask::Login(style, _) => {
f.debug_tuple("WorkerTask::Login")
.field(style)
.field(&format_args!("_"))
.finish()
},
WorkerTask::GetInviter(invite, _) => {
f.debug_tuple("WorkerTask::GetInviter").field(invite).finish()
},
WorkerTask::GetRoom(room_id, _) => {
f.debug_tuple("WorkerTask::GetRoom")
.field(room_id)
.field(&format_args!("_"))
.finish()
},
WorkerTask::JoinRoom(s, _) => {
f.debug_tuple("WorkerTask::JoinRoom")
.field(s)
.field(&format_args!("_"))
.finish()
},
WorkerTask::Members(room_id, _) => {
f.debug_tuple("WorkerTask::Members")
.field(room_id)
.field(&format_args!("_"))
.finish()
},
WorkerTask::SpaceMembers(room_id, _) => {
f.debug_tuple("WorkerTask::SpaceMembers")
.field(room_id)
.field(&format_args!("_"))
.finish()
},
WorkerTask::Spaces(_) => {
f.debug_tuple("WorkerTask::Spaces").field(&format_args!("_")).finish()
},
WorkerTask::TypingNotice(room_id) => {
f.debug_tuple("WorkerTask::TypingNotice").field(room_id).finish()
},
WorkerTask::Verify(act, sasv1, _) => {
f.debug_tuple("WorkerTask::Verify")
.field(act)
.field(sasv1)
.field(&format_args!("_"))
.finish()
},
WorkerTask::VerifyRequest(user_id, _) => {
f.debug_tuple("WorkerTask::VerifyRequest")
.field(user_id)
.field(&format_args!("_"))
.finish()
},
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Requester { pub struct Requester {
pub tx: SyncSender<WorkerTask>, pub client: Client,
pub tx: UnboundedSender<WorkerTask>,
} }
impl Requester { impl Requester {
@@ -152,15 +260,7 @@ impl Requester {
return response.recv(); return response.recv();
} }
pub fn send_message(&self, room_id: OwnedRoomId, msg: String) -> IambResult<EchoPair> { pub fn direct_messages(&self) -> Vec<FetchedRoom> {
let (reply, response) = oneshot();
self.tx.send(WorkerTask::SendMessage(room_id, msg, reply)).unwrap();
return response.recv();
}
pub fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> {
let (reply, response) = oneshot(); let (reply, response) = oneshot();
self.tx.send(WorkerTask::DirectMessages(reply)).unwrap(); self.tx.send(WorkerTask::DirectMessages(reply)).unwrap();
@@ -168,7 +268,15 @@ impl Requester {
return response.recv(); return response.recv();
} }
pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<(MatrixRoom, DisplayName)> { pub fn get_inviter(&self, invite: Invited) -> IambResult<Option<RoomMember>> {
let (reply, response) = oneshot();
self.tx.send(WorkerTask::GetInviter(invite, reply)).unwrap();
return response.recv();
}
pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
let (reply, response) = oneshot(); let (reply, response) = oneshot();
self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap(); self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap();
@@ -184,10 +292,10 @@ impl Requester {
return response.recv(); return response.recv();
} }
pub fn joined_rooms(&self) -> Vec<(MatrixRoom, DisplayName)> { pub fn active_rooms(&self) -> Vec<FetchedRoom> {
let (reply, response) = oneshot(); let (reply, response) = oneshot();
self.tx.send(WorkerTask::JoinedRooms(reply)).unwrap(); self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap();
return response.recv(); return response.recv();
} }
@@ -208,14 +316,6 @@ impl Requester {
return response.recv(); return response.recv();
} }
pub fn set_room(&self, room_id: OwnedRoomId, ev: SetRoomField) -> IambResult<()> {
let (reply, response) = oneshot();
self.tx.send(WorkerTask::SetRoom(room_id, ev, reply)).unwrap();
return response.recv();
}
pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> { pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
let (reply, response) = oneshot(); let (reply, response) = oneshot();
@@ -253,10 +353,8 @@ pub struct ClientWorker {
} }
impl ClientWorker { impl ClientWorker {
pub fn spawn(settings: ApplicationSettings) -> Requester { pub async fn spawn(settings: ApplicationSettings) -> Requester {
let (tx, rx) = sync_channel(5); let (tx, rx) = unbounded_channel();
let _ = tokio::spawn(async move {
let account = &settings.profile; let account = &settings.profile;
// Set up a custom client that only uses HTTP/1. // Set up a custom client that only uses HTTP/1.
@@ -265,9 +363,10 @@ impl ClientWorker {
// will need to be revisited in the future. // will need to be revisited in the future.
let http = reqwest::Client::builder() let http = reqwest::Client::builder()
.user_agent(IAMB_USER_AGENT) .user_agent(IAMB_USER_AGENT)
.timeout(Duration::from_secs(60)) .timeout(Duration::from_secs(30))
.pool_idle_timeout(Duration::from_secs(120)) .pool_idle_timeout(Duration::from_secs(60))
.pool_max_idle_per_host(5) .pool_max_idle_per_host(10)
.tcp_keepalive(Duration::from_secs(10))
.http1_only() .http1_only()
.build() .build()
.unwrap(); .unwrap();
@@ -279,9 +378,7 @@ impl ClientWorker {
.store_config(StoreConfig::default()) .store_config(StoreConfig::default())
.sled_store(settings.matrix_dir.as_path(), None) .sled_store(settings.matrix_dir.as_path(), None)
.expect("Failed to setup up sled store for Matrix SDK") .expect("Failed to setup up sled store for Matrix SDK")
.request_config( .request_config(RequestConfig::new().timeout(REQ_TIMEOUT).retry_timeout(REQ_TIMEOUT))
RequestConfig::new().timeout(REQ_TIMEOUT).retry_timeout(REQ_TIMEOUT),
)
.build() .build()
.await .await
.expect("Failed to instantiate Matrix client"); .expect("Failed to instantiate Matrix client");
@@ -289,24 +386,24 @@ impl ClientWorker {
let mut worker = ClientWorker { let mut worker = ClientWorker {
initialized: false, initialized: false,
settings, settings,
client, client: client.clone(),
sync_handle: None, sync_handle: None,
}; };
let _ = tokio::spawn(async move {
worker.work(rx).await; worker.work(rx).await;
}); });
return Requester { tx }; return Requester { client, tx };
} }
async fn work(&mut self, rx: Receiver<WorkerTask>) { async fn work(&mut self, mut rx: UnboundedReceiver<WorkerTask>) {
loop { loop {
let t = rx.recv_timeout(Duration::from_secs(1)); let t = rx.recv().await;
match t { match t {
Ok(task) => self.run(task).await, Some(task) => self.run(task).await,
Err(RecvTimeoutError::Timeout) => {}, None => {
Err(RecvTimeoutError::Disconnected) => {
break; break;
}, },
} }
@@ -332,13 +429,17 @@ impl ClientWorker {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.join_room(room_id).await); reply.send(self.join_room(room_id).await);
}, },
WorkerTask::GetInviter(invited, reply) => {
assert!(self.initialized);
reply.send(self.get_inviter(invited).await);
},
WorkerTask::GetRoom(room_id, reply) => { WorkerTask::GetRoom(room_id, reply) => {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.get_room(room_id).await); reply.send(self.get_room(room_id).await);
}, },
WorkerTask::JoinedRooms(reply) => { WorkerTask::ActiveRooms(reply) => {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.joined_rooms().await); reply.send(self.active_rooms().await);
}, },
WorkerTask::LoadOlder(room_id, fetch_id, limit, reply) => { WorkerTask::LoadOlder(room_id, fetch_id, limit, reply) => {
assert!(self.initialized); assert!(self.initialized);
@@ -352,10 +453,6 @@ impl ClientWorker {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.members(room_id).await); reply.send(self.members(room_id).await);
}, },
WorkerTask::SetRoom(room_id, field, reply) => {
assert!(self.initialized);
reply.send(self.set_room(room_id, field).await);
},
WorkerTask::SpaceMembers(space, reply) => { WorkerTask::SpaceMembers(space, reply) => {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.space_members(space).await); reply.send(self.space_members(space).await);
@@ -364,10 +461,6 @@ impl ClientWorker {
assert!(self.initialized); assert!(self.initialized);
reply.send(self.spaces().await); reply.send(self.spaces().await);
}, },
WorkerTask::SendMessage(room_id, msg, reply) => {
assert!(self.initialized);
reply.send(self.send_message(room_id, msg).await);
},
WorkerTask::TypingNotice(room_id) => { WorkerTask::TypingNotice(room_id) => {
assert!(self.initialized); assert!(self.initialized);
self.typing_notice(room_id).await; self.typing_notice(room_id).await;
@@ -384,7 +477,7 @@ impl ClientWorker {
} }
async fn init(&mut self, store: AsyncProgramStore) { async fn init(&mut self, store: AsyncProgramStore) {
self.client.add_event_handler_context(store); self.client.add_event_handler_context(store.clone());
let _ = self.client.add_event_handler( let _ = self.client.add_event_handler(
|ev: SyncTypingEvent, room: MatrixRoom, store: Ctx<AsyncProgramStore>| { |ev: SyncTypingEvent, room: MatrixRoom, store: Ctx<AsyncProgramStore>| {
@@ -448,15 +541,33 @@ impl ClientWorker {
let mut locked = store.lock().await; let mut locked = store.lock().await;
let mut info = locked.application.get_room_info(room_id.to_owned()); let mut info = locked.application.get_room_info(room_id.to_owned());
info.name = room_name; info.name = room_name;
info.insert(ev.into_full_event(room_id.to_owned()));
}
},
);
let event_id = ev.event_id().to_owned(); let _ = self.client.add_event_handler(
let key = (ev.origin_server_ts().into(), event_id.clone()); |ev: OriginalSyncRoomRedactionEvent,
let msg = Message::from(ev.into_full_event(room_id.to_owned())); room: MatrixRoom,
info.messages.insert(key, msg); store: Ctx<AsyncProgramStore>| {
async move {
let room_id = room.room_id();
let room_info = room.clone_info();
let room_version = room_info.room_version().unwrap_or(&RoomVersionId::V1);
// Remove the echo. let mut locked = store.lock().await;
let key = (MessageTimeStamp::LocalEcho, event_id); let info = locked.application.get_room_info(room_id.to_owned());
let _ = info.messages.remove(&key);
let key = if let Some(k) = info.keys.get(&ev.redacts) {
k
} else {
return;
};
if let Some(msg) = info.messages.get_mut(key) {
let ev = SyncRoomRedactionEvent::Original(ev);
msg.event.redact(ev, room_version);
}
} }
}, },
); );
@@ -573,6 +684,19 @@ impl ClientWorker {
}, },
); );
let client = self.client.clone();
let _ = tokio::spawn(async move {
// Update the displayed read receipts ever 5 seconds.
let mut interval = tokio::time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
let receipts = update_receipts(&client).await;
store.lock().await.application.set_receipts(receipts).await;
}
});
self.initialized = true; self.initialized = true;
} }
@@ -615,37 +739,10 @@ impl ClientWorker {
Ok(Some(InfoMessage::from("Successfully logged in!"))) Ok(Some(InfoMessage::from("Successfully logged in!")))
} }
async fn send_message(&mut self, room_id: OwnedRoomId, msg: String) -> IambResult<EchoPair> { async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> {
let room = if let r @ Some(_) = self.client.get_joined_room(&room_id) { for (room, name, tags) in self.direct_messages().await {
r
} else if self.client.join_room_by_id(&room_id).await.is_ok() {
self.client.get_joined_room(&room_id)
} else {
None
};
if let Some(room) = room {
let msg = TextMessageEventContent::plain(msg);
let msg = MessageType::Text(msg);
let msg = RoomMessageEventContent::new(msg);
// XXX: second parameter can be a locally unique transaction id.
// Useful for doing retries.
let resp = room.send(msg.clone(), None).await.map_err(IambError::from)?;
let event_id = resp.event_id;
// XXX: need to either give error messages and retry when needed!
return Ok((event_id, msg));
} else {
Err(IambError::UnknownRoom(room_id).into())
}
}
async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<(MatrixRoom, DisplayName)> {
for (room, name) in self.direct_messages().await {
if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() { if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() {
return Ok((room, name)); return Ok((room, name, tags));
} }
} }
@@ -673,11 +770,18 @@ impl ClientWorker {
} }
} }
async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<(MatrixRoom, DisplayName)> { async fn get_inviter(&mut self, invited: Invited) -> IambResult<Option<RoomMember>> {
let details = invited.invite_details().await.map_err(IambError::from)?;
Ok(details.inviter)
}
async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
if let Some(room) = self.client.get_room(&room_id) { if let Some(room) = self.client.get_room(&room_id) {
let name = room.display_name().await.map_err(IambError::from)?; let name = room.display_name().await.map_err(IambError::from)?;
let tags = room.tags().await.map_err(IambError::from)?;
Ok((room, name)) Ok((room, name, tags))
} else { } else {
Err(IambError::UnknownRoom(room_id).into()) Err(IambError::UnknownRoom(room_id).into())
} }
@@ -706,33 +810,57 @@ impl ClientWorker {
} }
} }
async fn direct_messages(&mut self) -> Vec<(MatrixRoom, DisplayName)> { async fn direct_messages(&self) -> Vec<FetchedRoom> {
let mut rooms = vec![]; let mut rooms = vec![];
for room in self.client.joined_rooms().into_iter() { for room in self.client.invited_rooms().into_iter() {
if room.is_space() || !room.is_direct() { if !room.is_direct() {
continue; continue;
} }
if let Ok(name) = room.display_name().await { let name = room.display_name().await.unwrap_or(DisplayName::Empty);
rooms.push((MatrixRoom::from(room), name)) let tags = room.tags().await.unwrap_or_default();
rooms.push((room.into(), name, tags));
} }
for room in self.client.joined_rooms().into_iter() {
if !room.is_direct() {
continue;
}
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
let tags = room.tags().await.unwrap_or_default();
rooms.push((room.into(), name, tags));
} }
return rooms; return rooms;
} }
async fn joined_rooms(&mut self) -> Vec<(MatrixRoom, DisplayName)> { async fn active_rooms(&self) -> Vec<FetchedRoom> {
let mut rooms = vec![]; let mut rooms = vec![];
for room in self.client.invited_rooms().into_iter() {
if room.is_space() || room.is_direct() {
continue;
}
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
let tags = room.tags().await.unwrap_or_default();
rooms.push((room.into(), name, tags));
}
for room in self.client.joined_rooms().into_iter() { for room in self.client.joined_rooms().into_iter() {
if room.is_space() || room.is_direct() { if room.is_space() || room.is_direct() {
continue; continue;
} }
if let Ok(name) = room.display_name().await { let name = room.display_name().await.unwrap_or(DisplayName::Empty);
rooms.push((MatrixRoom::from(room), name)) let tags = room.tags().await.unwrap_or_default();
}
rooms.push((room.into(), name, tags));
} }
return rooms; return rooms;
@@ -781,27 +909,6 @@ impl ClientWorker {
} }
} }
async fn set_room(&mut self, room_id: OwnedRoomId, field: SetRoomField) -> IambResult<()> {
let room = if let Some(r) = self.client.get_joined_room(&room_id) {
r
} else {
return Err(IambError::UnknownRoom(room_id).into());
};
match field {
SetRoomField::Name(name) => {
let ev = RoomNameEventContent::new(name.into());
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
SetRoomField::Topic(topic) => {
let ev = RoomTopicEventContent::new(topic);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
}
Ok(())
}
async fn space_members(&mut self, space: OwnedRoomId) -> IambResult<Vec<OwnedRoomId>> { async fn space_members(&mut self, space: OwnedRoomId) -> IambResult<Vec<OwnedRoomId>> {
let mut req = SpaceHierarchyRequest::new(&space); let mut req = SpaceHierarchyRequest::new(&space);
req.limit = Some(1000u32.into()); req.limit = Some(1000u32.into());
@@ -814,17 +921,27 @@ impl ClientWorker {
Ok(rooms) Ok(rooms)
} }
async fn spaces(&mut self) -> Vec<(MatrixRoom, DisplayName)> { async fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
let mut spaces = vec![]; let mut spaces = vec![];
for room in self.client.invited_rooms().into_iter() {
if !room.is_space() {
continue;
}
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
spaces.push((room.into(), name));
}
for room in self.client.joined_rooms().into_iter() { for room in self.client.joined_rooms().into_iter() {
if !room.is_space() { if !room.is_space() {
continue; continue;
} }
if let Ok(name) = room.display_name().await { let name = room.display_name().await.unwrap_or(DisplayName::Empty);
spaces.push((MatrixRoom::from(room), name));
} spaces.push((room.into(), name));
} }
return spaces; return spaces;