Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89bb107c87 | ||
|
|
ca4c0034d9 | ||
|
|
bb30cecc63 | ||
|
|
7b050f82aa | ||
|
|
b1ccec6732 | ||
|
|
6e8e12b579 | ||
|
|
3da9835a17 | ||
|
|
64891ec68f | ||
|
|
61aba80be1 | ||
|
|
8d4539831f | ||
|
|
7c39e88ba2 | ||
|
|
758397eb5a | ||
|
|
1a0af6df37 | ||
|
|
885b56038f | ||
|
|
430c601ff2 | ||
|
|
0ddefcd7b3 | ||
|
|
2a573b6056 | ||
|
|
a020b860dd | ||
|
|
6c031f589e | ||
|
|
b0256d7120 | ||
|
|
0f870367b3 | ||
|
|
8d22b83d85 | ||
|
|
529073f4d4 | ||
|
|
17c87a617e | ||
|
|
2899d4f45a | ||
|
|
ad8b4a60d2 | ||
|
|
4935899aed | ||
|
|
cc1d2f3bf8 | ||
|
|
5df9fe7960 | ||
|
|
a5c25f2487 | ||
|
|
50023bad40 | ||
|
|
b6a318dfe3 | ||
|
|
ad3b40d538 | ||
|
|
953be6a195 | ||
|
|
463d46b8ab | ||
|
|
274234ce4c | ||
|
|
a2590b6bbb | ||
|
|
725ebb9fd6 | ||
|
|
ca395097e1 | ||
|
|
e98d58a8cc | ||
|
|
e6cdd02f22 | ||
|
|
0bc4ff07b0 | ||
|
|
14fe916d94 | ||
|
|
db35581d07 | ||
|
|
7c1c62897a | ||
|
|
61897ea6f2 | ||
|
|
6a0722795a | ||
|
|
f3bbc6ad9f | ||
|
|
2dd8c0fddf | ||
|
|
a786369b14 | ||
|
|
066f60ad32 | ||
|
|
10b142c071 | ||
|
|
ac6ff63d25 | ||
|
|
54a0e76823 | ||
|
|
93eff79f79 | ||
|
|
11625262f1 | ||
|
|
0ed1d53946 |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
ko_fi: ulyssam
|
||||
54
.github/workflows/ci.yml
vendored
54
.github/workflows/ci.yml
vendored
@@ -9,25 +9,6 @@ on:
|
||||
name: CI
|
||||
|
||||
jobs:
|
||||
clippy_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
override: true
|
||||
- name: Check Clippy
|
||||
uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
toolchain: stable
|
||||
args:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -38,23 +19,38 @@ jobs:
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
- name: Install Rust (1.66 w/ clippy)
|
||||
uses: dtolnay/rust-toolchain@1.66
|
||||
with:
|
||||
toolchain: nightly
|
||||
override: true
|
||||
components: rustfmt, clippy
|
||||
components: clippy
|
||||
- name: Install Rust (nightly w/ rustfmt)
|
||||
run: rustup toolchain install nightly --component rustfmt
|
||||
- name: Cache cargo registry
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cargo/registry
|
||||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||
- name: Cache cargo build
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: target
|
||||
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
|
||||
- name: Check formatting
|
||||
uses: actions-rs/cargo@v1
|
||||
run: cargo +nightly fmt --all -- --check
|
||||
- name: Check Clippy
|
||||
if: matrix.platform == 'ubuntu-latest'
|
||||
uses: giraffate/clippy-action@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
reporter: 'github-check'
|
||||
- name: Run tests
|
||||
uses: actions-rs/cargo@v1
|
||||
run: cargo test
|
||||
- name: Build artifacts
|
||||
run: cargo build --release
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@master
|
||||
with:
|
||||
command: test
|
||||
name: iamb-${{ matrix.platform }}
|
||||
path: |
|
||||
./target/release/iamb
|
||||
./target/release/iamb.exe
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/target
|
||||
/result
|
||||
/TODO
|
||||
/docs/iamb.[15]
|
||||
|
||||
1702
Cargo.lock
generated
1702
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
25
Cargo.toml
25
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "iamb"
|
||||
version = "0.0.5"
|
||||
version = "0.0.8"
|
||||
edition = "2018"
|
||||
authors = ["Ulyssa <git@ulyssa.dev>"]
|
||||
repository = "https://github.com/ulyssa/iamb"
|
||||
@@ -12,16 +12,30 @@ exclude = [".github", "CONTRIBUTING.md"]
|
||||
keywords = ["matrix", "chat", "tui", "vim"]
|
||||
categories = ["command-line-utilities"]
|
||||
rust-version = "1.66"
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
mandown = "0.1.3"
|
||||
|
||||
[build-dependencies.vergen]
|
||||
version = "8"
|
||||
default-features = false
|
||||
features = ["build", "git", "gitcl",]
|
||||
|
||||
[dependencies]
|
||||
arboard = "3.2.0"
|
||||
bitflags = "1.3.2"
|
||||
chrono = "0.4"
|
||||
clap = {version = "4.0", features = ["derive"]}
|
||||
comrak = {version = "0.18.0", features = ["shortcodes"]}
|
||||
css-color-parser = "0.1.2"
|
||||
dirs = "4.0.0"
|
||||
emojis = "~0.5.2"
|
||||
futures = "0.3"
|
||||
gethostname = "0.4.1"
|
||||
html5ever = "0.26.0"
|
||||
image = "0.24.5"
|
||||
libc = "0.2"
|
||||
markup5ever_rcdom = "0.2.0"
|
||||
mime = "^0.3.16"
|
||||
mime_guess = "^2.0.4"
|
||||
@@ -39,12 +53,12 @@ unicode-width = "0.1.10"
|
||||
url = {version = "^2.2.2", features = ["serde"]}
|
||||
|
||||
[dependencies.modalkit]
|
||||
version = "0.0.11"
|
||||
version = "0.0.16"
|
||||
|
||||
[dependencies.matrix-sdk]
|
||||
version = "0.6"
|
||||
default-features = false
|
||||
features = ["e2e-encryption", "markdown", "sled", "rustls-tls"]
|
||||
features = ["e2e-encryption", "sled", "rustls-tls"]
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1.24.1"
|
||||
@@ -52,3 +66,8 @@ features = ["macros", "net", "rt-multi-thread", "sync", "time"]
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "1.4.0"
|
||||
pretty_assertions = "1.4.0"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
incremental = false
|
||||
|
||||
43
README.md
43
README.md
@@ -2,6 +2,7 @@
|
||||
|
||||
[](https://github.com/ulyssa/iamb/actions?query=workflow%3ACI+)
|
||||
[](https://crates.io/crates/iamb)
|
||||
[](https://matrix.to/#/#iamb:0x.badd.cafe)
|
||||
[](https://crates.io/crates/iamb)
|
||||
|
||||
## About
|
||||
@@ -11,6 +12,8 @@
|
||||
This project is a work-in-progress, and there's still a lot to be implemented,
|
||||
but much of the basic client functionality is already present.
|
||||
|
||||

|
||||
|
||||
## Documentation
|
||||
|
||||
You can find documentation for installing, configuring, and using iamb on its
|
||||
@@ -18,12 +21,35 @@ website, [iamb.chat].
|
||||
|
||||
## Installation
|
||||
|
||||
Install Rust and Cargo, and then run:
|
||||
Install Rust (1.66.0 or above) and Cargo, and then run:
|
||||
|
||||
```
|
||||
cargo install --locked iamb
|
||||
```
|
||||
|
||||
### NetBSD
|
||||
|
||||
On NetBSD a package is available from the official repositories. To install it simply run:
|
||||
|
||||
```
|
||||
pkgin install iamb
|
||||
```
|
||||
|
||||
### Arch Linux
|
||||
|
||||
On Arch Linux a [package](https://aur.archlinux.org/packages/iamb-git) is available in the
|
||||
Arch User Repositories (AUR). To install it simply run with your favorite AUR helper:
|
||||
|
||||
```
|
||||
paru iamb-git
|
||||
```
|
||||
|
||||
### Nix / NixOS (flake)
|
||||
|
||||
```
|
||||
nix profile install "github:ulyssa/iamb"
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
You can create a basic configuration in `$CONFIG_DIR/iamb/config.json` that looks like:
|
||||
@@ -75,7 +101,7 @@ two other TUI clients and Element Web:
|
||||
| VOIP | ❌ | ❌ | ❌ | ✔️ |
|
||||
| Reactions | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Message editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Room upgrades | ❌ | ✔️ | ❌ | ✔️ |
|
||||
| Room upgrades | ❌ ([#41]) | ✔️ | ❌ | ✔️ |
|
||||
| Localisations | ❌ | 1 | ❌ | 44 |
|
||||
| SSO Support | ❌ | ✔️ | ✔️ | ✔️ |
|
||||
|
||||
@@ -88,18 +114,7 @@ iamb is released under the [Apache License, Version 2.0].
|
||||
[iamb.chat]: https://iamb.chat
|
||||
[gomuks]: https://github.com/tulir/gomuks
|
||||
[weechat-matrix]: https://github.com/poljar/weechat-matrix
|
||||
[#2]: https://github.com/ulyssa/iamb/issues/2
|
||||
[#3]: https://github.com/ulyssa/iamb/issues/3
|
||||
[#4]: https://github.com/ulyssa/iamb/issues/4
|
||||
[#5]: https://github.com/ulyssa/iamb/issues/5
|
||||
[#6]: https://github.com/ulyssa/iamb/issues/6
|
||||
[#7]: https://github.com/ulyssa/iamb/issues/7
|
||||
[#8]: https://github.com/ulyssa/iamb/issues/8
|
||||
[#9]: https://github.com/ulyssa/iamb/issues/9
|
||||
[#10]: https://github.com/ulyssa/iamb/issues/10
|
||||
[#11]: https://github.com/ulyssa/iamb/issues/11
|
||||
[#12]: https://github.com/ulyssa/iamb/issues/12
|
||||
[#13]: https://github.com/ulyssa/iamb/issues/13
|
||||
[#14]: https://github.com/ulyssa/iamb/issues/14
|
||||
[#15]: https://github.com/ulyssa/iamb/issues/15
|
||||
[#16]: https://github.com/ulyssa/iamb/issues/16
|
||||
[#41]: https://github.com/ulyssa/iamb/issues/41
|
||||
|
||||
29
build.rs
Normal file
29
build.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::iter::FromIterator;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mandown::convert;
|
||||
use vergen::EmitBuilder;
|
||||
|
||||
const IAMB_1_MD: &str = include_str!("docs/iamb.1.md");
|
||||
const IAMB_5_MD: &str = include_str!("docs/iamb.5.md");
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
EmitBuilder::builder().git_sha(true).emit()?;
|
||||
|
||||
// Build the manual pages.
|
||||
println!("cargo:rerun-if-changed=docs/iamb.1.md");
|
||||
println!("cargo:rerun-if-changed=docs/iamb.5.md");
|
||||
|
||||
let iamb_1 = convert(IAMB_1_MD, "IAMB", 1);
|
||||
let iamb_5 = convert(IAMB_5_MD, "IAMB", 5);
|
||||
|
||||
let out_dir = std::env::var("OUT_DIR");
|
||||
let out_dir = out_dir.as_deref().unwrap_or("docs");
|
||||
|
||||
fs::write(PathBuf::from_iter([out_dir, "iamb.1"]), iamb_1.as_bytes())?;
|
||||
fs::write(PathBuf::from_iter([out_dir, "iamb.5"]), iamb_5.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
32
docs/example_config.json
Normal file
32
docs/example_config.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"default_profile": "default",
|
||||
"profiles": {
|
||||
"default": {
|
||||
"user_id": "",
|
||||
"url": "https://matrix.org",
|
||||
"settings": {},
|
||||
"dirs": {}
|
||||
}
|
||||
},
|
||||
"settings": {
|
||||
"log_level": "warn",
|
||||
"reaction_display": true,
|
||||
"reaction_shortcode_display": false,
|
||||
"read_receipt_send": true,
|
||||
"read_receipt_display": true,
|
||||
"request_timeout": 10000,
|
||||
"typing_notice_send": true,
|
||||
"typing_notice_display": true,
|
||||
"users": {
|
||||
"@user:matrix.org": {
|
||||
"name": "John Doe",
|
||||
"color": "magenta"
|
||||
}
|
||||
},
|
||||
"default_room": "#iamb-users:0x.badd.cafe"
|
||||
},
|
||||
"dirs": {
|
||||
"cache": "~/.cache/iamb/",
|
||||
"logs": "~/.local/share/iamb/logs/",
|
||||
"downloads": "~/Downloads/"
|
||||
}
|
||||
29
docs/iamb.1.md
Normal file
29
docs/iamb.1.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# NAME
|
||||
|
||||
iamb – a terminal-based client for Matrix for the Vim addict
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
**iamb** [**--profile** _profile_] [**--config-directory** _directory_] [**--help** | **--version**]
|
||||
|
||||
# OPTIONS
|
||||
|
||||
These options are primitives at the top-level of the file.
|
||||
|
||||
**--profile**, **-P**
|
||||
> The profile to start with. Overrides **default_profile** from **iamb(5)**.
|
||||
|
||||
**--config-directory**, **-C**
|
||||
> Path to the directory the configuration file is located in.
|
||||
|
||||
**--help**, **-h**
|
||||
> Show a short help text and quit.
|
||||
|
||||
**--version**, **-V**
|
||||
> Show the iamb version and quit.
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
**iamb(5)**
|
||||
|
||||
Full documentation is available online at \<https://iamb.chat\>
|
||||
134
docs/iamb.5.md
Normal file
134
docs/iamb.5.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# NAME
|
||||
|
||||
config.json – configuration file for iamb
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
Configuration must be placed under _~/.config/iamb/_ and is named config.json.
|
||||
|
||||
Example configuration usually comes bundled with your installation and can
|
||||
typically be found in _/usr/share/iamb_.
|
||||
|
||||
As implied by the filename, the configuration is formatted in JSON. It's
|
||||
structure and fields are described below.
|
||||
|
||||
# BASIC SETTINGS
|
||||
|
||||
These options are primitives at the top-level of the file.
|
||||
|
||||
**default_profile** (type: string)
|
||||
> The default profile to connect to, unless overwritten by a commandline
|
||||
> switch. It has to be defined in the *PROFILES* section.
|
||||
|
||||
# PROFILES
|
||||
|
||||
These options are configured as a map under the profiles name.
|
||||
|
||||
**user_id** (type: string)
|
||||
> The user ID to use when connecting to the server. For example "user" for
|
||||
> "@user:matrix.org".
|
||||
|
||||
**url** (type: string)
|
||||
> The URL of the users server. For example "https://matrix.org" for
|
||||
> "@user:matrix.org".
|
||||
|
||||
**settings** (type: settings object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *TUNABLES*.
|
||||
|
||||
**layout** (type: startup layout object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *STARTUP LAYOUT*.
|
||||
|
||||
**dirs** (type: XDG overrides object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *DIRECTORIES*.
|
||||
|
||||
# TUNABLES
|
||||
|
||||
These options are configured as a map under the *settings* key and can be
|
||||
overridden as described in *PROFILES*.
|
||||
|
||||
**log_level** (type: string)
|
||||
> Specifies the lowest log level that should be shown. Possible values
|
||||
> are: _trace_, _debug_, _info_, _warn_, and _error_.
|
||||
|
||||
**reaction_display** (type: boolean)
|
||||
> Defines whether or not reactions should be shown.
|
||||
|
||||
**reaction_shortcode_display** (type: boolean)
|
||||
> Defines whether or not reactions should be shown as their respective
|
||||
> shortcode.
|
||||
|
||||
**read_receipt_send** (type: boolean)
|
||||
> Defines whether or not read confirmations are sent.
|
||||
|
||||
**read_receipt_display** (type: boolean)
|
||||
> Defines whether or not read confirmations are displayed.
|
||||
|
||||
**request_timeout** (type: uint64)
|
||||
> Defines the maximum time per request in seconds.
|
||||
|
||||
**typing_notice_send** (type: boolean)
|
||||
> Defines whether or not the typing state is sent.
|
||||
|
||||
**typing_notice_display** (type: boolean)
|
||||
> Defines whether or not the typing state is displayed.
|
||||
|
||||
**user** (type: map)
|
||||
> Overrides values for the specified user. See *USER OVERRIDES* for
|
||||
> details on the format.
|
||||
|
||||
**default_room** (type: string)
|
||||
> The room to show by default instead of a welcome-screen.
|
||||
|
||||
## USER OVERRIDES
|
||||
|
||||
Overrides are mapped onto matrix User IDs such as _@user:matrix.org_ and are
|
||||
maps containing the following key value pairs.
|
||||
|
||||
**name** (type: string)
|
||||
> Change the display name of the user.
|
||||
|
||||
**color** (type: string)
|
||||
> Change the color the user is shown as. Possible values are: _black_,
|
||||
> _blue_, _cyan_, _dark-gray_, _gray_, _green_, _light-blue_,
|
||||
> _light-cyan_, _light-green_, _light-magenta_, _light-red_,
|
||||
> _light-yellow_, _magenta_, _none_, _red_, _white_, _yellow_
|
||||
|
||||
# STARTUP LAYOUT
|
||||
|
||||
Specifies what initial set of tabs and windows to show when starting the
|
||||
client. Configured as an object under the key *layout*.
|
||||
|
||||
**style** (type: string)
|
||||
> Specifies what window layout to load when starting. Valid values are
|
||||
> _restore_ to restore the layout from the last time the client was exited,
|
||||
> _new_ to open a single window (uses the value of _default\_room_ if set), or
|
||||
> _config_ to open the layout described under _tabs_.
|
||||
|
||||
**tabs** (type: array of window objects)
|
||||
> If **style** is set to _config_, then this value will be used to open a set
|
||||
> of tabs and windows at startup. Each object can contain either a **window**
|
||||
> key specifying a username, room identifier or room alias to show, or a
|
||||
> **split** key specifying an array of window objects.
|
||||
|
||||
# DIRECTORIES
|
||||
|
||||
Specifies the directories to save data in. Configured as a map under the key
|
||||
*dirs*.
|
||||
|
||||
**cache** (type: string)
|
||||
> Specifies where to store assets and temporary data in.
|
||||
|
||||
**logs** (type: string)
|
||||
> Specifies where to store log files.
|
||||
|
||||
**downloads** (type: string)
|
||||
> Specifies where to store downloaded files.
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
*iamb(1)*
|
||||
|
||||
Full documentation is available online at \<https://iamb.chat\>
|
||||
94
flake.lock
generated
Normal file
94
flake.lock
generated
Normal file
@@ -0,0 +1,94 @@
|
||||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1678901627,
|
||||
"narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils_2": {
|
||||
"locked": {
|
||||
"lastModified": 1659877975,
|
||||
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1679437018,
|
||||
"narHash": "sha256-vOuiDPLHSEo/7NkiWtxpHpHgoXoNmrm+wkXZ6a072Fc=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "19cf008bb18e47b6e3b4e16e32a9a4bdd4b45f7e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1665296151,
|
||||
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"rust-overlay": "rust-overlay"
|
||||
}
|
||||
},
|
||||
"rust-overlay": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils_2",
|
||||
"nixpkgs": "nixpkgs_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1679624450,
|
||||
"narHash": "sha256-wiDqUaklmc31E1+wz5sv52sMcWvZKsL1FBeGJCxz628=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "afbdcf305fd6f05f708fe76d52f24d37d066c251",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
40
flake.nix
Normal file
40
flake.nix
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
description = "iamb";
|
||||
nixConfig.bash-prompt = "\[nix-develop\]$ ";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, rust-overlay, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
# We only need the nightly overlay in the devShell because .rs files are formatted with nightly.
|
||||
overlays = [ (import rust-overlay) ];
|
||||
pkgs = import nixpkgs { inherit system overlays; };
|
||||
rustNightly = pkgs.rust-bin.nightly."2023-03-17".default;
|
||||
in
|
||||
with pkgs;
|
||||
{
|
||||
packages.default = pkgs.rustPlatform.buildRustPackage {
|
||||
pname = "iamb";
|
||||
version = "0.0.7";
|
||||
src = ./.;
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
nativeBuildInputs = [ pkgs.pkgconfig ];
|
||||
buildInputs = [ pkgs.openssl ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin
|
||||
(with pkgs.darwin.apple_sdk.frameworks; [ AppKit Security ]);
|
||||
};
|
||||
devShell = mkShell {
|
||||
buildInputs = [
|
||||
(rustNightly.override { extensions = [ "rust-src" ]; })
|
||||
pkg-config
|
||||
cargo-tarpaulin
|
||||
rust-analyzer
|
||||
rustfmt
|
||||
];
|
||||
};
|
||||
});
|
||||
}
|
||||
649
src/base.rs
649
src/base.rs
@@ -1,17 +1,31 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::{self, Display};
|
||||
use std::hash::Hash;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use emojis::Emoji;
|
||||
use serde::{
|
||||
de::Error as SerdeError,
|
||||
de::Visitor,
|
||||
Deserialize,
|
||||
Deserializer,
|
||||
Serialize,
|
||||
Serializer,
|
||||
};
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tracing::warn;
|
||||
use url::Url;
|
||||
|
||||
use matrix_sdk::{
|
||||
encryption::verification::SasVerification,
|
||||
room::Joined,
|
||||
room::{Joined, Room as MatrixRoom},
|
||||
ruma::{
|
||||
events::{
|
||||
reaction::ReactionEvent,
|
||||
room::encrypted::RoomEncryptedEvent,
|
||||
room::message::{
|
||||
OriginalRoomMessageEvent,
|
||||
Relation,
|
||||
@@ -20,9 +34,9 @@ use matrix_sdk::{
|
||||
RoomMessageEventContent,
|
||||
},
|
||||
tag::{TagName, Tags},
|
||||
AnyMessageLikeEvent,
|
||||
MessageLikeEvent,
|
||||
},
|
||||
presence::PresenceState,
|
||||
EventId,
|
||||
OwnedEventId,
|
||||
OwnedRoomId,
|
||||
@@ -42,11 +56,15 @@ use modalkit::{
|
||||
ApplicationStore,
|
||||
ApplicationWindowId,
|
||||
},
|
||||
base::{CommandType, WordStyle},
|
||||
completion::{complete_path, CompletionMap},
|
||||
context::EditContext,
|
||||
cursor::Cursor,
|
||||
rope::EditRope,
|
||||
store::Store,
|
||||
},
|
||||
env::vim::{
|
||||
command::{CommandContext, VimCommand, VimCommandMachine},
|
||||
command::{CommandContext, CommandDescription, VimCommand, VimCommandMachine},
|
||||
keybindings::VimMachine,
|
||||
VimContext,
|
||||
},
|
||||
@@ -66,6 +84,20 @@ use crate::{
|
||||
ApplicationSettings,
|
||||
};
|
||||
|
||||
pub const MATRIX_ID_WORD: WordStyle = WordStyle::CharSet(is_mxid_char);
|
||||
|
||||
/// Find the boundaries for a Matrix username, room alias, or room ID.
|
||||
///
|
||||
/// Technically "[" and "]" should be here since IPv6 addresses are allowed
|
||||
/// in the server name, but in practice that should be uncommon, and people
|
||||
/// can just use `gf` and friends in Visual mode instead.
|
||||
fn is_mxid_char(c: char) -> bool {
|
||||
return c >= 'a' && c <= 'z' ||
|
||||
c >= 'A' && c <= 'Z' ||
|
||||
c >= '0' && c <= '9' ||
|
||||
":-./@_#!".contains(c);
|
||||
}
|
||||
|
||||
const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(2);
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -82,7 +114,9 @@ pub enum VerifyAction {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum MessageAction {
|
||||
/// Cance the current reply or edit.
|
||||
Cancel,
|
||||
///
|
||||
/// The [bool] argument indicates whether to skip confirmation for clearing the message bar.
|
||||
Cancel(bool),
|
||||
|
||||
/// Download an attachment to the given path.
|
||||
///
|
||||
@@ -97,7 +131,9 @@ pub enum MessageAction {
|
||||
React(String),
|
||||
|
||||
/// Redact a message, with an optional reason.
|
||||
Redact(Option<String>),
|
||||
///
|
||||
/// The [bool] argument indicates whether to skip confirmation.
|
||||
Redact(Option<String>, bool),
|
||||
|
||||
/// Reply to a message.
|
||||
Reply,
|
||||
@@ -109,6 +145,30 @@ pub enum MessageAction {
|
||||
Unreact(Option<String>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum CreateRoomType {
|
||||
/// A direct message room.
|
||||
Direct(OwnedUserId),
|
||||
|
||||
/// A standard chat room.
|
||||
Room,
|
||||
|
||||
/// A Matrix space.
|
||||
Space,
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
pub struct CreateRoomFlags: u32 {
|
||||
const NONE = 0b00000000;
|
||||
|
||||
/// Make the room public.
|
||||
const PUBLIC = 0b00000001;
|
||||
|
||||
/// Encrypt this room.
|
||||
const ENCRYPTED = 0b00000010;
|
||||
}
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
pub struct DownloadFlags: u32 {
|
||||
const NONE = 0b00000000;
|
||||
@@ -133,6 +193,7 @@ pub enum RoomAction {
|
||||
InviteAccept,
|
||||
InviteReject,
|
||||
InviteSend(OwnedUserId),
|
||||
Leave(bool),
|
||||
Members(Box<CommandContext<ProgramContext>>),
|
||||
Set(RoomField, String),
|
||||
Unset(RoomField),
|
||||
@@ -142,10 +203,17 @@ pub enum RoomAction {
|
||||
pub enum SendAction {
|
||||
Submit,
|
||||
Upload(String),
|
||||
UploadImage(usize, usize, Cow<'static, [u8]>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum HomeserverAction {
|
||||
CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum IambAction {
|
||||
Homeserver(HomeserverAction),
|
||||
Message(MessageAction),
|
||||
Room(RoomAction),
|
||||
Send(SendAction),
|
||||
@@ -154,6 +222,12 @@ pub enum IambAction {
|
||||
ToggleScrollbackFocus,
|
||||
}
|
||||
|
||||
impl From<HomeserverAction> for IambAction {
|
||||
fn from(act: HomeserverAction) -> Self {
|
||||
IambAction::Homeserver(act)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MessageAction> for IambAction {
|
||||
fn from(act: MessageAction) -> Self {
|
||||
IambAction::Message(act)
|
||||
@@ -175,6 +249,7 @@ impl From<SendAction> for IambAction {
|
||||
impl ApplicationAction for IambAction {
|
||||
fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Break,
|
||||
IambAction::Message(..) => SequenceStatus::Break,
|
||||
IambAction::Room(..) => SequenceStatus::Break,
|
||||
IambAction::Send(..) => SequenceStatus::Break,
|
||||
@@ -186,6 +261,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Atom,
|
||||
IambAction::Message(..) => SequenceStatus::Atom,
|
||||
IambAction::Room(..) => SequenceStatus::Atom,
|
||||
IambAction::Send(..) => SequenceStatus::Atom,
|
||||
@@ -197,6 +273,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Ignore,
|
||||
IambAction::Message(..) => SequenceStatus::Ignore,
|
||||
IambAction::Room(..) => SequenceStatus::Ignore,
|
||||
IambAction::Send(..) => SequenceStatus::Ignore,
|
||||
@@ -208,6 +285,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => false,
|
||||
IambAction::Message(..) => false,
|
||||
IambAction::Room(..) => false,
|
||||
IambAction::Send(..) => false,
|
||||
@@ -271,6 +349,9 @@ pub enum IambError {
|
||||
#[error("Serialization/deserialization error: {0}")]
|
||||
Serde(#[from] serde_json::Error),
|
||||
|
||||
#[error("No download directory configured")]
|
||||
NoDownloadDir,
|
||||
|
||||
#[error("Selected message does not have any attachments")]
|
||||
NoAttachment,
|
||||
|
||||
@@ -294,6 +375,12 @@ pub enum IambError {
|
||||
|
||||
#[error("Verification request error: {0}")]
|
||||
VerificationRequestError(#[from] matrix_sdk::encryption::identities::RequestVerificationError),
|
||||
|
||||
#[error("Image error: {0}")]
|
||||
Image(#[from] image::ImageError),
|
||||
|
||||
#[error("Could not use system clipboard data")]
|
||||
Clipboard,
|
||||
}
|
||||
|
||||
impl From<IambError> for UIError<IambInfo> {
|
||||
@@ -350,6 +437,9 @@ pub struct RoomInfo {
|
||||
/// A map of message identifiers to a map of reaction events.
|
||||
pub reactions: HashMap<OwnedEventId, MessageReactions>,
|
||||
|
||||
/// Whether the scrollback for this room is currently being fetched.
|
||||
pub fetching: bool,
|
||||
|
||||
/// Where to continue fetching from when we continue loading scrollback history.
|
||||
pub fetch_id: RoomFetchStatus,
|
||||
|
||||
@@ -358,6 +448,9 @@ pub struct RoomInfo {
|
||||
|
||||
/// Users currently typing in this room, and when we received notification of them doing so.
|
||||
pub users_typing: Option<(Instant, Vec<OwnedUserId>)>,
|
||||
|
||||
/// The display names for users in this room.
|
||||
pub display_names: HashMap<OwnedUserId, String>,
|
||||
}
|
||||
|
||||
impl RoomInfo {
|
||||
@@ -379,8 +472,12 @@ impl RoomInfo {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_message_key(&self, event_id: &EventId) -> Option<&MessageKey> {
|
||||
self.keys.get(event_id)?.to_message_key()
|
||||
}
|
||||
|
||||
pub fn get_event(&self, event_id: &EventId) -> Option<&Message> {
|
||||
self.messages.get(self.keys.get(event_id)?.to_message_key()?)
|
||||
self.messages.get(self.get_message_key(event_id)?)
|
||||
}
|
||||
|
||||
pub fn insert_reaction(&mut self, react: ReactionEvent) {
|
||||
@@ -422,15 +519,28 @@ impl RoomInfo {
|
||||
|
||||
match &mut msg.event {
|
||||
MessageEvent::Original(orig) => {
|
||||
orig.content = *new_content;
|
||||
orig.content.msgtype = new_content.msgtype;
|
||||
},
|
||||
MessageEvent::Local(_, content) => {
|
||||
*content = new_content;
|
||||
content.msgtype = new_content.msgtype;
|
||||
},
|
||||
MessageEvent::Redacted(_) => {
|
||||
MessageEvent::Redacted(_) |
|
||||
MessageEvent::EncryptedOriginal(_) |
|
||||
MessageEvent::EncryptedRedacted(_) => {
|
||||
return;
|
||||
},
|
||||
}
|
||||
|
||||
msg.html = msg.event.html();
|
||||
}
|
||||
|
||||
/// Inserts events that couldn't be decrypted into the scrollback.
|
||||
pub fn insert_encrypted(&mut self, msg: RoomEncryptedEvent) {
|
||||
let event_id = msg.event_id().to_owned();
|
||||
let key = (msg.origin_server_ts().into(), event_id.clone());
|
||||
|
||||
self.keys.insert(event_id, EventLocation::Message(key.clone()));
|
||||
self.messages.insert(key, msg.into());
|
||||
}
|
||||
|
||||
pub fn insert_message(&mut self, msg: RoomMessageEvent) {
|
||||
@@ -456,7 +566,7 @@ impl RoomInfo {
|
||||
}
|
||||
}
|
||||
|
||||
fn recently_fetched(&self) -> bool {
|
||||
pub fn recently_fetched(&self) -> bool {
|
||||
self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE)
|
||||
}
|
||||
|
||||
@@ -479,13 +589,13 @@ impl RoomInfo {
|
||||
match n {
|
||||
0 => Spans(vec![]),
|
||||
1 => {
|
||||
let user = settings.get_user_span(typers[0].as_ref());
|
||||
let user = settings.get_user_span(typers[0].as_ref(), self);
|
||||
|
||||
Spans(vec![user, Span::from(" is typing...")])
|
||||
},
|
||||
2 => {
|
||||
let user1 = settings.get_user_span(typers[0].as_ref());
|
||||
let user2 = settings.get_user_span(typers[1].as_ref());
|
||||
let user1 = settings.get_user_span(typers[0].as_ref(), self);
|
||||
let user2 = settings.get_user_span(typers[1].as_ref(), self);
|
||||
|
||||
Spans(vec![
|
||||
user1,
|
||||
@@ -528,13 +638,36 @@ impl RoomInfo {
|
||||
}
|
||||
}
|
||||
|
||||
fn emoji_map() -> CompletionMap<String, &'static Emoji> {
|
||||
let mut emojis = CompletionMap::default();
|
||||
|
||||
for emoji in emojis::iter() {
|
||||
for shortcode in emoji.shortcodes() {
|
||||
emojis.insert(shortcode.to_string(), emoji);
|
||||
}
|
||||
}
|
||||
|
||||
return emojis;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SyncInfo {
|
||||
pub spaces: Vec<MatrixRoom>,
|
||||
pub rooms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
|
||||
pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
|
||||
}
|
||||
|
||||
pub struct ChatStore {
|
||||
pub cmds: ProgramCommands,
|
||||
pub worker: Requester,
|
||||
pub rooms: HashMap<OwnedRoomId, RoomInfo>,
|
||||
pub names: HashMap<String, OwnedRoomId>,
|
||||
pub rooms: CompletionMap<OwnedRoomId, RoomInfo>,
|
||||
pub names: CompletionMap<String, OwnedRoomId>,
|
||||
pub presences: CompletionMap<OwnedUserId, PresenceState>,
|
||||
pub verifications: HashMap<String, SasVerification>,
|
||||
pub settings: ApplicationSettings,
|
||||
pub need_load: HashSet<OwnedRoomId>,
|
||||
pub emojis: CompletionMap<String, &'static Emoji>,
|
||||
pub sync_info: SyncInfo,
|
||||
}
|
||||
|
||||
impl ChatStore {
|
||||
@@ -543,10 +676,15 @@ impl ChatStore {
|
||||
worker,
|
||||
settings,
|
||||
|
||||
cmds: crate::commands::setup_commands(),
|
||||
emojis: emoji_map(),
|
||||
|
||||
names: Default::default(),
|
||||
rooms: Default::default(),
|
||||
presences: Default::default(),
|
||||
verifications: Default::default(),
|
||||
need_load: Default::default(),
|
||||
sync_info: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,7 +700,10 @@ impl ChatStore {
|
||||
.unwrap_or_else(|| "Untitled Matrix Room".to_string())
|
||||
}
|
||||
|
||||
pub async fn set_receipts(&mut self, receipts: Vec<(OwnedRoomId, Receipts)>) {
|
||||
pub async fn set_receipts(
|
||||
&mut self,
|
||||
receipts: Vec<(OwnedRoomId, Receipts)>,
|
||||
) -> Vec<(OwnedRoomId, OwnedEventId)> {
|
||||
let mut updates = vec![];
|
||||
|
||||
for (room_id, receipts) in receipts.into_iter() {
|
||||
@@ -575,75 +716,19 @@ impl ChatStore {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
pub fn mark_for_load(&mut self, room_id: OwnedRoomId) {
|
||||
self.need_load.insert(room_id);
|
||||
}
|
||||
|
||||
pub fn load_older(&mut self, limit: u32) {
|
||||
let ChatStore { need_load, rooms, worker, .. } = self;
|
||||
|
||||
for room_id in std::mem::take(need_load).into_iter() {
|
||||
let info = rooms.entry(room_id.clone()).or_default();
|
||||
|
||||
if info.recently_fetched() {
|
||||
need_load.insert(room_id);
|
||||
continue;
|
||||
} else {
|
||||
info.fetch_last = Instant::now().into();
|
||||
}
|
||||
|
||||
let fetch_id = match &info.fetch_id {
|
||||
RoomFetchStatus::Done => continue,
|
||||
RoomFetchStatus::HaveMore(fetch_id) => Some(fetch_id.clone()),
|
||||
RoomFetchStatus::NotStarted => None,
|
||||
};
|
||||
|
||||
let res = worker.load_older(room_id.clone(), fetch_id, limit);
|
||||
|
||||
match res {
|
||||
Ok((fetch_id, msgs)) => {
|
||||
for msg in msgs.into_iter() {
|
||||
match msg {
|
||||
AnyMessageLikeEvent::RoomMessage(msg) => {
|
||||
info.insert(msg);
|
||||
},
|
||||
AnyMessageLikeEvent::Reaction(ev) => {
|
||||
info.insert_reaction(ev);
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
info.fetch_id =
|
||||
fetch_id.map_or(RoomFetchStatus::Done, RoomFetchStatus::HaveMore);
|
||||
},
|
||||
Err(e) => {
|
||||
warn!(
|
||||
room_id = room_id.as_str(),
|
||||
err = e.to_string(),
|
||||
"Failed to load older messages"
|
||||
);
|
||||
|
||||
// Wait and try again.
|
||||
need_load.insert(room_id);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_room_info(&mut self, room_id: OwnedRoomId) -> &mut RoomInfo {
|
||||
self.rooms.entry(room_id).or_default()
|
||||
self.rooms.get_or_default(room_id)
|
||||
}
|
||||
|
||||
pub fn set_room_name(&mut self, room_id: &RoomId, name: &str) {
|
||||
self.rooms.entry(room_id.to_owned()).or_default().name = name.to_string().into();
|
||||
self.rooms.get_or_default(room_id.to_owned()).name = name.to_string().into();
|
||||
}
|
||||
|
||||
pub fn insert_sas(&mut self, sas: SasVerification) {
|
||||
@@ -657,17 +742,159 @@ impl ApplicationStore for ChatStore {}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum IambId {
|
||||
/// A Matrix room.
|
||||
Room(OwnedRoomId),
|
||||
|
||||
/// The `:rooms` window.
|
||||
DirectList,
|
||||
|
||||
/// The `:members` window for a given Matrix room.
|
||||
MemberList(OwnedRoomId),
|
||||
|
||||
/// The `:rooms` window.
|
||||
RoomList,
|
||||
|
||||
/// The `:spaces` window.
|
||||
SpaceList,
|
||||
|
||||
/// The `:verify` window.
|
||||
VerifyList,
|
||||
|
||||
/// The `:welcome` window.
|
||||
Welcome,
|
||||
}
|
||||
|
||||
impl Display for IambId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
IambId::Room(room_id) => {
|
||||
write!(f, "iamb://room/{room_id}")
|
||||
},
|
||||
IambId::MemberList(room_id) => {
|
||||
write!(f, "iamb://members/{room_id}")
|
||||
},
|
||||
IambId::DirectList => f.write_str("iamb://dms"),
|
||||
IambId::RoomList => f.write_str("iamb://rooms"),
|
||||
IambId::SpaceList => f.write_str("iamb://spaces"),
|
||||
IambId::VerifyList => f.write_str("iamb://verify"),
|
||||
IambId::Welcome => f.write_str("iamb://welcome"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationWindowId for IambId {}
|
||||
|
||||
impl Serialize for IambId {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for IambId {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(IambIdVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct IambIdVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for IambIdVisitor {
|
||||
type Value = IambId;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a valid window URL")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: SerdeError,
|
||||
{
|
||||
let Ok(url) = Url::parse(value) else {
|
||||
return Err(E::custom("Invalid iamb window URL"));
|
||||
};
|
||||
|
||||
if url.scheme() != "iamb" {
|
||||
return Err(E::custom("Invalid iamb window URL"));
|
||||
}
|
||||
|
||||
match url.domain() {
|
||||
Some("room") => {
|
||||
let Some(path) = url.path_segments() else {
|
||||
return Err(E::custom("Invalid members window URL"));
|
||||
};
|
||||
|
||||
let &[room_id] = path.collect::<Vec<_>>().as_slice() else {
|
||||
return Err(E::custom("Invalid members window URL"));
|
||||
};
|
||||
|
||||
let Ok(room_id) = OwnedRoomId::try_from(room_id) else {
|
||||
return Err(E::custom("Invalid room identifier"));
|
||||
};
|
||||
|
||||
Ok(IambId::Room(room_id))
|
||||
},
|
||||
Some("members") => {
|
||||
let Some(path) = url.path_segments() else {
|
||||
return Err(E::custom("Invalid members window URL"));
|
||||
};
|
||||
|
||||
let &[room_id] = path.collect::<Vec<_>>().as_slice() else {
|
||||
return Err(E::custom("Invalid members window URL"));
|
||||
};
|
||||
|
||||
let Ok(room_id) = OwnedRoomId::try_from(room_id) else {
|
||||
return Err(E::custom("Invalid room identifier"));
|
||||
};
|
||||
|
||||
Ok(IambId::MemberList(room_id))
|
||||
},
|
||||
Some("dms") => {
|
||||
if url.path() != "" {
|
||||
return Err(E::custom("iamb://dms takes no path"));
|
||||
}
|
||||
|
||||
Ok(IambId::DirectList)
|
||||
},
|
||||
Some("rooms") => {
|
||||
if url.path() != "" {
|
||||
return Err(E::custom("iamb://rooms takes no path"));
|
||||
}
|
||||
|
||||
Ok(IambId::RoomList)
|
||||
},
|
||||
Some("spaces") => {
|
||||
if url.path() != "" {
|
||||
return Err(E::custom("iamb://spaces takes no path"));
|
||||
}
|
||||
|
||||
Ok(IambId::SpaceList)
|
||||
},
|
||||
Some("verify") => {
|
||||
if url.path() != "" {
|
||||
return Err(E::custom("iamb://verify takes no path"));
|
||||
}
|
||||
|
||||
Ok(IambId::VerifyList)
|
||||
},
|
||||
Some("welcome") => {
|
||||
if url.path() != "" {
|
||||
return Err(E::custom("iamb://welcome takes no path"));
|
||||
}
|
||||
|
||||
Ok(IambId::Welcome)
|
||||
},
|
||||
Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))),
|
||||
None => Err(E::custom("Invalid iamb window URL")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum RoomFocus {
|
||||
Scrollback,
|
||||
@@ -686,7 +913,7 @@ impl RoomFocus {
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum IambBufferId {
|
||||
Command,
|
||||
Command(CommandType),
|
||||
Room(OwnedRoomId, RoomFocus),
|
||||
DirectList,
|
||||
MemberList(OwnedRoomId),
|
||||
@@ -699,7 +926,7 @@ pub enum IambBufferId {
|
||||
impl IambBufferId {
|
||||
pub fn to_window(&self) -> Option<IambId> {
|
||||
match self {
|
||||
IambBufferId::Command => None,
|
||||
IambBufferId::Command(_) => None,
|
||||
IambBufferId::Room(room, _) => Some(IambId::Room(room.clone())),
|
||||
IambBufferId::DirectList => Some(IambId::DirectList),
|
||||
IambBufferId::MemberList(room) => Some(IambId::MemberList(room.clone())),
|
||||
@@ -719,6 +946,199 @@ impl ApplicationInfo for IambInfo {
|
||||
type Action = IambAction;
|
||||
type WindowId = IambId;
|
||||
type ContentId = IambBufferId;
|
||||
|
||||
fn complete(
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
content: &IambBufferId,
|
||||
store: &mut ProgramStore,
|
||||
) -> Vec<String> {
|
||||
match content {
|
||||
IambBufferId::Command(CommandType::Command) => complete_cmdbar(text, cursor, store),
|
||||
IambBufferId::Command(CommandType::Search) => vec![],
|
||||
|
||||
IambBufferId::Room(_, RoomFocus::MessageBar) => complete_msgbar(text, cursor, store),
|
||||
IambBufferId::Room(_, RoomFocus::Scrollback) => vec![],
|
||||
|
||||
IambBufferId::DirectList => vec![],
|
||||
IambBufferId::MemberList(_) => vec![],
|
||||
IambBufferId::RoomList => vec![],
|
||||
IambBufferId::SpaceList => vec![],
|
||||
IambBufferId::VerifyList => vec![],
|
||||
IambBufferId::Welcome => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn content_of_command(ct: CommandType) -> IambBufferId {
|
||||
IambBufferId::Command(ct)
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let id = text
|
||||
.get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
let id = Cow::from(&id);
|
||||
|
||||
store
|
||||
.application
|
||||
.presences
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let id = text
|
||||
.get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
let id = Cow::from(&id);
|
||||
|
||||
match id.chars().next() {
|
||||
// Complete room aliases.
|
||||
Some('#') => {
|
||||
return store.application.names.complete(id.as_ref());
|
||||
},
|
||||
|
||||
// Complete room identifiers.
|
||||
Some('!') => {
|
||||
return store
|
||||
.application
|
||||
.rooms
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect();
|
||||
},
|
||||
|
||||
// Complete Emoji shortcodes.
|
||||
Some(':') => {
|
||||
let list = store.application.emojis.complete(&id[1..]);
|
||||
let iter = list.into_iter().take(200).map(|s| format!(":{}:", s));
|
||||
|
||||
return iter.collect();
|
||||
},
|
||||
|
||||
// Complete usernames for @ and empty strings.
|
||||
Some('@') | None => {
|
||||
return store
|
||||
.application
|
||||
.presences
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect();
|
||||
},
|
||||
|
||||
// Unknown sigil.
|
||||
Some(_) => return vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_matrix_names(
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
let id = text
|
||||
.get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
let id = Cow::from(&id);
|
||||
|
||||
let list = store.application.names.complete(id.as_ref());
|
||||
if !list.is_empty() {
|
||||
return list;
|
||||
}
|
||||
|
||||
let list = store.application.presences.complete(id.as_ref());
|
||||
if !list.is_empty() {
|
||||
return list.into_iter().map(|i| i.to_string()).collect();
|
||||
}
|
||||
|
||||
store
|
||||
.application
|
||||
.rooms
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let sc = text.get_prefix_word_mut(cursor, &WordStyle::Little);
|
||||
let sc = sc.unwrap_or_else(EditRope::empty);
|
||||
let sc = Cow::from(&sc);
|
||||
|
||||
store.application.emojis.complete(sc.as_ref())
|
||||
}
|
||||
|
||||
fn complete_cmdname(
|
||||
desc: CommandDescription,
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
// Complete command name and set cursor position.
|
||||
let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little);
|
||||
store.application.cmds.complete_name(desc.command.as_str())
|
||||
}
|
||||
|
||||
fn complete_cmdarg(
|
||||
desc: CommandDescription,
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
let cmd = match store.application.cmds.get(desc.command.as_str()) {
|
||||
Ok(cmd) => cmd,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
match cmd.name.as_str() {
|
||||
"cancel" | "dms" | "edit" | "redact" | "reply" => vec![],
|
||||
"members" | "rooms" | "spaces" | "welcome" => vec![],
|
||||
"download" | "open" | "upload" => complete_path(text, cursor),
|
||||
"react" | "unreact" => complete_emoji(text, cursor, store),
|
||||
|
||||
"invite" => complete_users(text, cursor, store),
|
||||
"join" | "split" | "vsplit" | "tabedit" => complete_matrix_names(text, cursor, store),
|
||||
"room" => vec![],
|
||||
"verify" => vec![],
|
||||
"vertical" | "horizontal" | "aboveleft" | "belowright" | "tab" => {
|
||||
complete_cmd(desc.arg.text.as_str(), text, cursor, store)
|
||||
},
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_cmd(
|
||||
cmd: &str,
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
match CommandDescription::from_str(cmd) {
|
||||
Ok(desc) => {
|
||||
if desc.arg.untrimmed.is_empty() {
|
||||
complete_cmdname(desc, text, cursor, store)
|
||||
} else {
|
||||
// Complete command argument.
|
||||
complete_cmdarg(desc, text, cursor, store)
|
||||
}
|
||||
},
|
||||
|
||||
// Can't parse command text, so return zero completions.
|
||||
Err(_) => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let eo = text.cursor_to_offset(cursor);
|
||||
let slice = text.slice(0.into(), eo, false);
|
||||
let cow = Cow::from(&slice);
|
||||
|
||||
complete_cmd(cow.as_ref(), text, cursor, store)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -804,4 +1224,77 @@ pub mod tests {
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_complete_msgbar() {
|
||||
let store = mock_store().await;
|
||||
|
||||
let text = EditRope::from("going for a walk :walk ");
|
||||
let mut cursor = Cursor::new(0, 22);
|
||||
let res = complete_msgbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec![":walking:", ":walking_man:", ":walking_woman:"]);
|
||||
assert_eq!(cursor, Cursor::new(0, 17));
|
||||
|
||||
let text = EditRope::from("hello @user1 ");
|
||||
let mut cursor = Cursor::new(0, 12);
|
||||
let res = complete_msgbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["@user1:example.com"]);
|
||||
assert_eq!(cursor, Cursor::new(0, 6));
|
||||
|
||||
let text = EditRope::from("see #room ");
|
||||
let mut cursor = Cursor::new(0, 9);
|
||||
let res = complete_msgbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["#room1:example.com"]);
|
||||
assert_eq!(cursor, Cursor::new(0, 4));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_complete_cmdbar() {
|
||||
let store = mock_store().await;
|
||||
let users = vec![
|
||||
"@user1:example.com",
|
||||
"@user2:example.com",
|
||||
"@user3:example.com",
|
||||
"@user4:example.com",
|
||||
"@user5:example.com",
|
||||
];
|
||||
|
||||
let text = EditRope::from("invite ");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let id = text
|
||||
.get_prefix_word_mut(&mut cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
assert_eq!(id.to_string(), "");
|
||||
assert_eq!(cursor, Cursor::new(0, 7));
|
||||
|
||||
let text = EditRope::from("invite ");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, users);
|
||||
|
||||
let text = EditRope::from("invite ignored");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, users);
|
||||
|
||||
let text = EditRope::from("invite @user1ignored");
|
||||
let mut cursor = Cursor::new(0, 13);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["@user1:example.com"]);
|
||||
|
||||
let text = EditRope::from("abo hor");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["horizontal"]);
|
||||
|
||||
let text = EditRope::from("abo hor inv");
|
||||
let mut cursor = Cursor::new(0, 11);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["invite"]);
|
||||
|
||||
let text = EditRope::from("abo hor invite \n");
|
||||
let mut cursor = Cursor::new(0, 15);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, users);
|
||||
}
|
||||
}
|
||||
|
||||
176
src/commands.rs
176
src/commands.rs
@@ -4,13 +4,16 @@ use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
|
||||
|
||||
use modalkit::{
|
||||
editing::base::OpenTarget,
|
||||
env::vim::command::{CommandContext, CommandDescription},
|
||||
env::vim::command::{CommandContext, CommandDescription, OptionType},
|
||||
input::commands::{CommandError, CommandResult, CommandStep},
|
||||
input::InputContext,
|
||||
};
|
||||
|
||||
use crate::base::{
|
||||
CreateRoomFlags,
|
||||
CreateRoomType,
|
||||
DownloadFlags,
|
||||
HomeserverAction,
|
||||
IambAction,
|
||||
IambId,
|
||||
MessageAction,
|
||||
@@ -158,12 +161,23 @@ fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_leave(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let leave = IambAction::Room(RoomAction::Leave(desc.bang));
|
||||
let step = CommandStep::Continue(leave.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mact = IambAction::from(MessageAction::Cancel);
|
||||
let mact = IambAction::from(MessageAction::Cancel(desc.bang));
|
||||
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
@@ -234,7 +248,8 @@ fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let ract = IambAction::from(MessageAction::Redact(args.into_iter().next()));
|
||||
let reason = args.into_iter().next();
|
||||
let ract = IambAction::from(MessageAction::Redact(reason, desc.bang));
|
||||
let step = CommandStep::Continue(ract.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
@@ -297,6 +312,53 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_create(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let args = desc.arg.options()?;
|
||||
let mut flags = CreateRoomFlags::NONE;
|
||||
let mut alias = None;
|
||||
let mut ct = CreateRoomType::Room;
|
||||
|
||||
for arg in args {
|
||||
match arg {
|
||||
OptionType::Flag(name, Some(arg)) => {
|
||||
match name.as_str() {
|
||||
"alias" => {
|
||||
if alias.is_some() {
|
||||
let msg = "Multiple ++alias arguments are not allowed";
|
||||
let err = CommandError::Error(msg.into());
|
||||
|
||||
return Err(err);
|
||||
} else {
|
||||
alias = Some(arg);
|
||||
}
|
||||
},
|
||||
_ => return Err(CommandError::InvalidArgument),
|
||||
}
|
||||
},
|
||||
OptionType::Flag(name, None) => {
|
||||
match name.as_str() {
|
||||
"public" => flags |= CreateRoomFlags::PUBLIC,
|
||||
"space" => ct = CreateRoomType::Space,
|
||||
"enc" | "encrypted" => flags |= CreateRoomFlags::ENCRYPTED,
|
||||
_ => return Err(CommandError::InvalidArgument),
|
||||
}
|
||||
},
|
||||
OptionType::Positional(_) => {
|
||||
let msg = ":create doesn't take any positional arguments";
|
||||
let err = CommandError::Error(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let hact = HomeserverAction::CreateRoom(alias, ct, flags);
|
||||
let iact = IambAction::from(hact);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
@@ -395,24 +457,86 @@ fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
}
|
||||
|
||||
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!["download".into()], f: iamb_download });
|
||||
cmds.add_command(ProgramCommand { names: vec!["open".into()], f: iamb_open });
|
||||
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!["members".into()], f: iamb_members });
|
||||
cmds.add_command(ProgramCommand { names: vec!["react".into()], f: iamb_react });
|
||||
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!["room".into()], f: iamb_room });
|
||||
cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
|
||||
cmds.add_command(ProgramCommand { names: vec!["unreact".into()], f: iamb_unreact });
|
||||
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!["welcome".into()], f: iamb_welcome });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "cancel".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_cancel,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "create".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_create,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "download".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_download,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "open".into(), aliases: vec![], f: iamb_open });
|
||||
cmds.add_command(ProgramCommand { name: "edit".into(), aliases: vec![], f: iamb_edit });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "invite".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_invite,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "leave".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_leave,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "members".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_members,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "react".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_react,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "redact".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_redact,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "reply".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_reply,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "rooms".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_rooms,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "room".into(), aliases: vec![], f: iamb_room });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "spaces".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_spaces,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "unreact".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_unreact,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "upload".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_upload,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "verify".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_verify,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "welcome".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_welcome,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn setup_commands() -> ProgramCommands {
|
||||
@@ -763,15 +887,19 @@ mod tests {
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(None));
|
||||
let act = IambAction::Message(MessageAction::Redact(None, false));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact!", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(None, true));
|
||||
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())));
|
||||
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
||||
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())));
|
||||
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
|
||||
|
||||
263
src/config.rs
263
src/config.rs
@@ -9,8 +9,9 @@ use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
||||
use clap::Parser;
|
||||
use matrix_sdk::ruma::{OwnedUserId, UserId};
|
||||
use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
|
||||
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
|
||||
use tracing::Level;
|
||||
use url::Url;
|
||||
|
||||
use modalkit::tui::{
|
||||
@@ -18,6 +19,8 @@ use modalkit::tui::{
|
||||
text::Span,
|
||||
};
|
||||
|
||||
use super::base::{IambId, RoomInfo};
|
||||
|
||||
macro_rules! usage {
|
||||
( $($args: tt)* ) => {
|
||||
println!($($args)*);
|
||||
@@ -25,6 +28,8 @@ macro_rules! usage {
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_REQ_TIMEOUT: u64 = 120;
|
||||
|
||||
const COLORS: [Color; 13] = [
|
||||
Color::Blue,
|
||||
Color::Cyan,
|
||||
@@ -86,8 +91,13 @@ fn validate_profile_names(names: &HashMap<String, ProfileConfig>) {
|
||||
}
|
||||
}
|
||||
|
||||
const VERSION: &str = match option_env!("VERGEN_GIT_SHA") {
|
||||
None => env!("CARGO_PKG_VERSION"),
|
||||
Some(_) => concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")"),
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(version, about, long_about = None)]
|
||||
#[clap(version = VERSION, about, long_about = None)]
|
||||
#[clap(propagate_version = true)]
|
||||
pub struct Iamb {
|
||||
#[clap(short = 'P', long, value_parser)]
|
||||
@@ -106,6 +116,47 @@ pub enum ConfigError {
|
||||
Invalid(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct LogLevel(pub Level);
|
||||
pub struct LogLevelVisitor;
|
||||
|
||||
impl From<LogLevel> for Level {
|
||||
fn from(level: LogLevel) -> Level {
|
||||
level.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for LogLevelVisitor {
|
||||
type Value = LogLevel;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: SerdeError,
|
||||
{
|
||||
match value {
|
||||
"info" => Ok(LogLevel(Level::INFO)),
|
||||
"debug" => Ok(LogLevel(Level::DEBUG)),
|
||||
"warn" => Ok(LogLevel(Level::WARN)),
|
||||
"error" => Ok(LogLevel(Level::ERROR)),
|
||||
"trace" => Ok(LogLevel(Level::TRACE)),
|
||||
_ => Err(E::custom("Could not parse log level")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for LogLevel {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(LogLevelVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct UserColor(pub Color);
|
||||
pub struct UserColorVisitor;
|
||||
@@ -176,56 +227,90 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum UserDisplayStyle {
|
||||
// The Matrix username for the sender (e.g., "@user:example.com").
|
||||
#[default]
|
||||
Username,
|
||||
|
||||
// The localpart of the Matrix username (e.g., "@user").
|
||||
LocalPart,
|
||||
|
||||
// The display name for the Matrix user, calculated according to the rules from the spec.
|
||||
//
|
||||
// This is usually something like "Ada Lovelace" if the user has configured a display name, but
|
||||
// it can wind up being the Matrix username if there are display name collisions in the room,
|
||||
// in order to avoid any confusion.
|
||||
DisplayName,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TunableValues {
|
||||
pub log_level: Level,
|
||||
pub reaction_display: bool,
|
||||
pub reaction_shortcode_display: bool,
|
||||
pub read_receipt_send: bool,
|
||||
pub read_receipt_display: bool,
|
||||
pub request_timeout: u64,
|
||||
pub typing_notice_send: bool,
|
||||
pub typing_notice_display: bool,
|
||||
pub users: UserOverrides,
|
||||
pub username_display: UserDisplayStyle,
|
||||
pub default_room: Option<String>,
|
||||
pub open_command: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
pub struct Tunables {
|
||||
pub log_level: Option<LogLevel>,
|
||||
pub reaction_display: Option<bool>,
|
||||
pub reaction_shortcode_display: Option<bool>,
|
||||
pub read_receipt_send: Option<bool>,
|
||||
pub read_receipt_display: Option<bool>,
|
||||
pub request_timeout: Option<u64>,
|
||||
pub typing_notice_send: Option<bool>,
|
||||
pub typing_notice_display: Option<bool>,
|
||||
pub users: Option<UserOverrides>,
|
||||
pub username_display: Option<UserDisplayStyle>,
|
||||
pub default_room: Option<String>,
|
||||
pub open_command: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl Tunables {
|
||||
fn merge(self, other: Self) -> Self {
|
||||
Tunables {
|
||||
log_level: self.log_level.or(other.log_level),
|
||||
reaction_display: self.reaction_display.or(other.reaction_display),
|
||||
reaction_shortcode_display: self
|
||||
.reaction_shortcode_display
|
||||
.or(other.reaction_shortcode_display),
|
||||
read_receipt_send: self.read_receipt_send.or(other.read_receipt_send),
|
||||
read_receipt_display: self.read_receipt_display.or(other.read_receipt_display),
|
||||
request_timeout: self.request_timeout.or(other.request_timeout),
|
||||
typing_notice_send: self.typing_notice_send.or(other.typing_notice_send),
|
||||
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
|
||||
users: merge_users(self.users, other.users),
|
||||
username_display: self.username_display.or(other.username_display),
|
||||
default_room: self.default_room.or(other.default_room),
|
||||
open_command: self.open_command.or(other.open_command),
|
||||
}
|
||||
}
|
||||
|
||||
fn values(self) -> TunableValues {
|
||||
TunableValues {
|
||||
log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
|
||||
reaction_display: self.reaction_display.unwrap_or(true),
|
||||
reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
|
||||
read_receipt_send: self.read_receipt_send.unwrap_or(true),
|
||||
read_receipt_display: self.read_receipt_display.unwrap_or(true),
|
||||
request_timeout: self.request_timeout.unwrap_or(DEFAULT_REQ_TIMEOUT),
|
||||
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(),
|
||||
username_display: self.username_display.unwrap_or_default(),
|
||||
default_room: self.default_room,
|
||||
open_command: self.open_command,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -234,7 +319,7 @@ impl Tunables {
|
||||
pub struct DirectoryValues {
|
||||
pub cache: PathBuf,
|
||||
pub logs: PathBuf,
|
||||
pub downloads: PathBuf,
|
||||
pub downloads: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
@@ -269,21 +354,49 @@ impl Directories {
|
||||
dir
|
||||
});
|
||||
|
||||
let downloads = self
|
||||
.downloads
|
||||
.or_else(dirs::download_dir)
|
||||
.expect("no dirs.download value configured!");
|
||||
let downloads = self.downloads.or_else(dirs::download_dir);
|
||||
|
||||
DirectoryValues { cache, logs, downloads }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum WindowPath {
|
||||
AliasId(OwnedRoomAliasId),
|
||||
RoomId(OwnedRoomId),
|
||||
UserId(OwnedUserId),
|
||||
Window(IambId),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
|
||||
#[serde(untagged, deny_unknown_fields)]
|
||||
pub enum WindowLayout {
|
||||
Window { window: WindowPath },
|
||||
Split { split: Vec<WindowLayout> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase", tag = "style")]
|
||||
pub enum Layout {
|
||||
/// Restore the layout from the previous session.
|
||||
#[default]
|
||||
Restore,
|
||||
|
||||
/// Open a single window using the `default_room` value.
|
||||
New,
|
||||
|
||||
/// Open the window layouts described under `tabs`.
|
||||
Config { tabs: Vec<WindowLayout> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct ProfileConfig {
|
||||
pub user_id: OwnedUserId,
|
||||
pub url: Url,
|
||||
pub settings: Option<Tunables>,
|
||||
pub dirs: Option<Directories>,
|
||||
pub layout: Option<Layout>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
@@ -292,6 +405,7 @@ pub struct IambConfig {
|
||||
pub default_profile: Option<String>,
|
||||
pub settings: Option<Tunables>,
|
||||
pub dirs: Option<Directories>,
|
||||
pub layout: Option<Layout>,
|
||||
}
|
||||
|
||||
impl IambConfig {
|
||||
@@ -315,11 +429,13 @@ impl IambConfig {
|
||||
#[derive(Clone)]
|
||||
pub struct ApplicationSettings {
|
||||
pub matrix_dir: PathBuf,
|
||||
pub layout_json: PathBuf,
|
||||
pub session_json: PathBuf,
|
||||
pub profile_name: String,
|
||||
pub profile: ProfileConfig,
|
||||
pub tunables: TunableValues,
|
||||
pub dirs: DirectoryValues,
|
||||
pub layout: Layout,
|
||||
}
|
||||
|
||||
impl ApplicationSettings {
|
||||
@@ -340,6 +456,7 @@ impl ApplicationSettings {
|
||||
default_profile,
|
||||
dirs,
|
||||
settings: global,
|
||||
layout,
|
||||
} = IambConfig::load(config_json.as_path())?;
|
||||
|
||||
validate_profile_names(&profiles);
|
||||
@@ -363,10 +480,17 @@ impl ApplicationSettings {
|
||||
);
|
||||
};
|
||||
|
||||
let layout = profile.layout.take().or(layout).unwrap_or_default();
|
||||
|
||||
let tunables = global.unwrap_or_default();
|
||||
let tunables = profile.settings.take().unwrap_or_default().merge(tunables);
|
||||
let tunables = tunables.values();
|
||||
|
||||
let dirs = dirs.unwrap_or_default();
|
||||
let dirs = profile.dirs.take().unwrap_or_default().merge(dirs);
|
||||
let dirs = dirs.values();
|
||||
|
||||
// Set up paths that live inside the profile's data directory.
|
||||
let mut profile_dir = config_dir.clone();
|
||||
profile_dir.push("profiles");
|
||||
profile_dir.push(profile_name.as_str());
|
||||
@@ -377,17 +501,23 @@ impl ApplicationSettings {
|
||||
let mut session_json = profile_dir;
|
||||
session_json.push("session.json");
|
||||
|
||||
let dirs = dirs.unwrap_or_default();
|
||||
let dirs = profile.dirs.take().unwrap_or_default().merge(dirs);
|
||||
let dirs = dirs.values();
|
||||
// Set up paths that live inside the profile's cache directory.
|
||||
let mut cache_dir = dirs.cache.clone();
|
||||
cache_dir.push("profiles");
|
||||
cache_dir.push(profile_name.as_str());
|
||||
|
||||
let mut layout_json = cache_dir.clone();
|
||||
layout_json.push("layout.json");
|
||||
|
||||
let settings = ApplicationSettings {
|
||||
matrix_dir,
|
||||
layout_json,
|
||||
session_json,
|
||||
profile_name,
|
||||
profile,
|
||||
tunables,
|
||||
dirs,
|
||||
layout,
|
||||
};
|
||||
|
||||
Ok(settings)
|
||||
@@ -414,18 +544,45 @@ impl ApplicationSettings {
|
||||
Span::styled(String::from(c), style)
|
||||
}
|
||||
|
||||
pub fn get_user_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
||||
let (color, name) = self
|
||||
.tunables
|
||||
pub fn get_user_overrides(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> (Option<Color>, Option<Cow<'static, str>>) {
|
||||
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();
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
let user_id = user_id.as_str();
|
||||
let color = color.unwrap_or_else(|| user_color(user_id));
|
||||
pub fn get_user_style(&self, user_id: &UserId) -> Style {
|
||||
let color = self
|
||||
.tunables
|
||||
.users
|
||||
.get(user_id)
|
||||
.and_then(|user| user.color.as_ref().map(|c| c.0))
|
||||
.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||
|
||||
user_style_from_color(color)
|
||||
}
|
||||
|
||||
pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> {
|
||||
let (color, name) = self.get_user_overrides(user_id);
|
||||
|
||||
let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||
let style = user_style_from_color(color);
|
||||
let name = name.unwrap_or(Cow::Borrowed(user_id));
|
||||
let name = match (name, &self.tunables.username_display) {
|
||||
(Some(name), _) => name,
|
||||
(None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()),
|
||||
(None, UserDisplayStyle::LocalPart) => Cow::Borrowed(user_id.localpart()),
|
||||
(None, UserDisplayStyle::DisplayName) => {
|
||||
if let Some(display) = info.display_names.get(user_id) {
|
||||
Cow::Borrowed(display.as_str())
|
||||
} else {
|
||||
Cow::Borrowed(user_id.as_str())
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Span::styled(name, style)
|
||||
}
|
||||
@@ -435,6 +592,7 @@ impl ApplicationSettings {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use matrix_sdk::ruma::user_id;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
#[test]
|
||||
fn test_profile_name_invalid() {
|
||||
@@ -528,4 +686,75 @@ mod tests {
|
||||
})];
|
||||
assert_eq!(res.users, Some(users.into_iter().collect()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_tunables_username_display() {
|
||||
let res: Tunables = serde_json::from_str("{\"username_display\": \"username\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::Username));
|
||||
|
||||
let res: Tunables = serde_json::from_str("{\"username_display\": \"localpart\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::LocalPart));
|
||||
|
||||
let res: Tunables =
|
||||
serde_json::from_str("{\"username_display\": \"displayname\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::DisplayName));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_layout() {
|
||||
let user = WindowPath::UserId(user_id!("@user:example.com").to_owned());
|
||||
let alias = WindowPath::AliasId(OwnedRoomAliasId::try_from("#room:example.com").unwrap());
|
||||
let room = WindowPath::RoomId(OwnedRoomId::try_from("!room:example.com").unwrap());
|
||||
let dms = WindowPath::Window(IambId::DirectList);
|
||||
let welcome = WindowPath::Window(IambId::Welcome);
|
||||
|
||||
let res: Layout = serde_json::from_str("{\"style\": \"restore\"}").unwrap();
|
||||
assert_eq!(res, Layout::Restore);
|
||||
|
||||
let res: Layout = serde_json::from_str("{\"style\": \"new\"}").unwrap();
|
||||
assert_eq!(res, Layout::New);
|
||||
|
||||
let res: Layout = serde_json::from_str(
|
||||
"{\"style\": \"config\", \"tabs\": [{\"window\":\"@user:example.com\"}]}",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(res, Layout::Config {
|
||||
tabs: vec![WindowLayout::Window { window: user.clone() }]
|
||||
});
|
||||
|
||||
let res: Layout = serde_json::from_str(
|
||||
"{\
|
||||
\"style\": \"config\",\
|
||||
\"tabs\": [\
|
||||
{\"split\":[\
|
||||
{\"window\":\"@user:example.com\"},\
|
||||
{\"window\":\"#room:example.com\"}\
|
||||
]},\
|
||||
{\"split\":[\
|
||||
{\"window\":\"!room:example.com\"},\
|
||||
{\"split\":[\
|
||||
{\"window\":\"iamb://dms\"},\
|
||||
{\"window\":\"iamb://welcome\"}\
|
||||
]}\
|
||||
]}\
|
||||
]}",
|
||||
)
|
||||
.unwrap();
|
||||
let split1 = WindowLayout::Split {
|
||||
split: vec![
|
||||
WindowLayout::Window { window: user.clone() },
|
||||
WindowLayout::Window { window: alias },
|
||||
],
|
||||
};
|
||||
let split2 = WindowLayout::Split {
|
||||
split: vec![WindowLayout::Window { window: dms }, WindowLayout::Window {
|
||||
window: welcome,
|
||||
}],
|
||||
};
|
||||
let split3 = WindowLayout::Split {
|
||||
split: vec![WindowLayout::Window { window: room }, split2],
|
||||
};
|
||||
let tabs = vec![split1, split3];
|
||||
assert_eq!(res, Layout::Config { tabs });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,21 @@
|
||||
use modalkit::{
|
||||
editing::action::WindowAction,
|
||||
editing::base::WordStyle,
|
||||
env::vim::keybindings::{InputStep, VimBindings},
|
||||
env::vim::VimMode,
|
||||
input::bindings::{EdgeEvent, EdgeRepeat, InputBindings},
|
||||
input::key::TerminalKey,
|
||||
};
|
||||
|
||||
use crate::base::{IambAction, IambInfo, Keybindings};
|
||||
use crate::base::{IambAction, IambInfo, Keybindings, MATRIX_ID_WORD};
|
||||
|
||||
type IambStep = InputStep<IambInfo>;
|
||||
|
||||
/// Find the boundaries for a Matrix username, room alias, or room ID.
|
||||
///
|
||||
/// Technically "[" and "]" should be here since IPv6 addresses are allowed
|
||||
/// in the server name, but in practice that should be uncommon, and people
|
||||
/// can just use `gf` and friends in Visual mode instead.
|
||||
fn is_mxid_char(c: char) -> bool {
|
||||
return c >= 'a' && c <= 'z' ||
|
||||
c >= 'A' && c <= 'Z' ||
|
||||
c >= '0' && c <= '9' ||
|
||||
":-./@_#!".contains(c);
|
||||
}
|
||||
|
||||
pub fn setup_keybindings() -> Keybindings {
|
||||
let mut ism = Keybindings::empty();
|
||||
|
||||
let vim = VimBindings::default()
|
||||
.submit_on_enter()
|
||||
.cursor_open(WordStyle::CharSet(is_mxid_char));
|
||||
.cursor_open(MATRIX_ID_WORD.clone());
|
||||
|
||||
vim.setup(&mut ism);
|
||||
|
||||
|
||||
304
src/main.rs
304
src/main.rs
@@ -6,7 +6,7 @@ use std::collections::VecDeque;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::Display;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::io::{stdout, BufReader, Stdout};
|
||||
use std::io::{stdout, BufReader, BufWriter, Stdout};
|
||||
use std::ops::DerefMut;
|
||||
use std::process;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
@@ -15,15 +15,28 @@ use std::time::Duration;
|
||||
|
||||
use clap::Parser;
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tracing::{self, Level};
|
||||
use tracing_subscriber::FmtSubscriber;
|
||||
|
||||
use matrix_sdk::ruma::OwnedUserId;
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
ruma::{
|
||||
api::client::filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
|
||||
OwnedUserId,
|
||||
},
|
||||
};
|
||||
|
||||
use modalkit::crossterm::{
|
||||
self,
|
||||
cursor::Show as CursorShow,
|
||||
event::{poll, read, DisableBracketedPaste, EnableBracketedPaste, Event},
|
||||
event::{
|
||||
poll,
|
||||
read,
|
||||
DisableBracketedPaste,
|
||||
DisableFocusChange,
|
||||
EnableBracketedPaste,
|
||||
EnableFocusChange,
|
||||
Event,
|
||||
},
|
||||
execute,
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle},
|
||||
};
|
||||
@@ -53,20 +66,19 @@ use crate::{
|
||||
base::{
|
||||
AsyncProgramStore,
|
||||
ChatStore,
|
||||
HomeserverAction,
|
||||
IambAction,
|
||||
IambBufferId,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
ProgramAction,
|
||||
ProgramCommands,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
},
|
||||
config::{ApplicationSettings, Iamb},
|
||||
windows::IambWindow,
|
||||
worker::{ClientWorker, LoginStyle, Requester},
|
||||
worker::{create_room, ClientWorker, LoginStyle, Requester},
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
@@ -78,12 +90,15 @@ use modalkit::{
|
||||
EditInfo,
|
||||
Editable,
|
||||
EditorAction,
|
||||
InfoMessage,
|
||||
InsertTextAction,
|
||||
Jumpable,
|
||||
Promptable,
|
||||
Scrollable,
|
||||
TabAction,
|
||||
TabContainer,
|
||||
TabCount,
|
||||
UIError,
|
||||
WindowAction,
|
||||
WindowContainer,
|
||||
},
|
||||
@@ -92,32 +107,123 @@ use modalkit::{
|
||||
key::KeyManager,
|
||||
store::Store,
|
||||
},
|
||||
input::{bindings::BindingMachine, key::TerminalKey},
|
||||
input::{bindings::BindingMachine, dialog::Pager, key::TerminalKey},
|
||||
widgets::{
|
||||
cmdbar::CommandBarState,
|
||||
screen::{Screen, ScreenState},
|
||||
screen::{FocusList, Screen, ScreenState, TabLayoutDescription},
|
||||
windows::WindowLayoutDescription,
|
||||
TerminalCursor,
|
||||
TerminalExtOps,
|
||||
Window,
|
||||
},
|
||||
};
|
||||
|
||||
const MIN_MSG_LOAD: u32 = 50;
|
||||
fn config_tab_to_desc(
|
||||
layout: config::WindowLayout,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<WindowLayoutDescription<IambInfo>> {
|
||||
let desc = match layout {
|
||||
config::WindowLayout::Window { window } => {
|
||||
let ChatStore { names, worker, .. } = &mut store.application;
|
||||
|
||||
fn msg_load_req(area: Rect) -> u32 {
|
||||
let n = area.height as u32;
|
||||
let window = match window {
|
||||
config::WindowPath::UserId(user_id) => {
|
||||
let name = user_id.to_string();
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
IambId::Room(room_id)
|
||||
},
|
||||
config::WindowPath::RoomId(room_id) => IambId::Room(room_id),
|
||||
config::WindowPath::AliasId(alias) => {
|
||||
let name = alias.to_string();
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
IambId::Room(room_id)
|
||||
},
|
||||
config::WindowPath::Window(id) => id,
|
||||
};
|
||||
|
||||
n.max(MIN_MSG_LOAD)
|
||||
WindowLayoutDescription::Window { window, length: None }
|
||||
},
|
||||
config::WindowLayout::Split { split } => {
|
||||
let children = split
|
||||
.into_iter()
|
||||
.map(|child| config_tab_to_desc(child, store))
|
||||
.collect::<IambResult<Vec<_>>>()?;
|
||||
|
||||
WindowLayoutDescription::Split { children, length: None }
|
||||
},
|
||||
};
|
||||
|
||||
Ok(desc)
|
||||
}
|
||||
|
||||
fn setup_screen(
|
||||
settings: ApplicationSettings,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<ScreenState<IambWindow, IambInfo>> {
|
||||
let cmd = CommandBarState::new(store);
|
||||
let dims = crossterm::terminal::size()?;
|
||||
let area = Rect::new(0, 0, dims.0, dims.1);
|
||||
|
||||
match settings.layout {
|
||||
config::Layout::Restore => {
|
||||
if let Ok(layout) = std::fs::read(&settings.layout_json) {
|
||||
let tabs: TabLayoutDescription<IambInfo> =
|
||||
serde_json::from_slice(&layout).map_err(IambError::from)?;
|
||||
let tabs = tabs.to_layout(area.into(), store)?;
|
||||
|
||||
return Ok(ScreenState::from_list(tabs, cmd));
|
||||
}
|
||||
},
|
||||
config::Layout::New => {},
|
||||
config::Layout::Config { tabs } => {
|
||||
let mut list = FocusList::default();
|
||||
|
||||
for tab in tabs.into_iter() {
|
||||
let tab = config_tab_to_desc(tab, store)?;
|
||||
let tab = tab.to_layout(area.into(), store)?;
|
||||
list.push(tab);
|
||||
}
|
||||
|
||||
return Ok(ScreenState::from_list(list, cmd));
|
||||
},
|
||||
}
|
||||
|
||||
let win = settings
|
||||
.tunables
|
||||
.default_room
|
||||
.and_then(|room| IambWindow::find(room, store).ok())
|
||||
.or_else(|| IambWindow::open(IambId::Welcome, store).ok())
|
||||
.unwrap();
|
||||
|
||||
return Ok(ScreenState::new(win, cmd));
|
||||
}
|
||||
|
||||
struct Application {
|
||||
store: AsyncProgramStore,
|
||||
worker: Requester,
|
||||
/// Terminal backend.
|
||||
terminal: Terminal<CrosstermBackend<Stdout>>,
|
||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||
cmds: ProgramCommands,
|
||||
|
||||
/// State for the Matrix client, editing, etc.
|
||||
store: AsyncProgramStore,
|
||||
|
||||
/// UI state (open tabs, command bar, etc.) to use when rendering.
|
||||
screen: ScreenState<IambWindow, IambInfo>,
|
||||
|
||||
/// Handle to communicate synchronously with the Matrix worker task.
|
||||
worker: Requester,
|
||||
|
||||
/// Mapped keybindings.
|
||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||
|
||||
/// Pending actions to run.
|
||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||
|
||||
/// Whether or not the terminal is currently focused.
|
||||
focused: bool,
|
||||
|
||||
/// The tab layout before the last executed [TabAction].
|
||||
last_layout: Option<TabLayoutDescription<IambInfo>>,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
@@ -129,6 +235,7 @@ impl Application {
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
crossterm::execute!(stdout, EnterAlternateScreen)?;
|
||||
crossterm::execute!(stdout, EnableBracketedPaste)?;
|
||||
crossterm::execute!(stdout, EnableFocusChange)?;
|
||||
|
||||
let title = format!("iamb ({})", settings.profile.user_id);
|
||||
crossterm::execute!(stdout, SetTitle(title))?;
|
||||
@@ -138,19 +245,9 @@ impl Application {
|
||||
|
||||
let bindings = crate::keybindings::setup_keybindings();
|
||||
let bindings = KeyManager::new(bindings);
|
||||
let cmds = crate::commands::setup_commands();
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
|
||||
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 screen = ScreenState::new(win, cmd);
|
||||
let screen = setup_screen(settings, locked.deref_mut())?;
|
||||
|
||||
let worker = locked.application.worker.clone();
|
||||
drop(locked);
|
||||
@@ -163,14 +260,15 @@ impl Application {
|
||||
terminal,
|
||||
bindings,
|
||||
actstack,
|
||||
cmds,
|
||||
screen,
|
||||
focused: true,
|
||||
last_layout: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> {
|
||||
let modestr = self.bindings.showmode();
|
||||
let cursor = self.bindings.get_cursor_indicator();
|
||||
let bindings = &mut self.bindings;
|
||||
let focused = self.focused;
|
||||
let sstate = &mut self.screen;
|
||||
let term = &mut self.terminal;
|
||||
|
||||
@@ -181,9 +279,24 @@ impl Application {
|
||||
term.draw(|f| {
|
||||
let area = f.size();
|
||||
|
||||
let screen = Screen::new(store).showmode(modestr).borders(true);
|
||||
let modestr = bindings.show_mode();
|
||||
let cursor = bindings.get_cursor_indicator();
|
||||
let dialogstr = bindings.show_dialog(area.height as usize, area.width as usize);
|
||||
|
||||
// Don't show terminal cursor when we show a dialog.
|
||||
let hide_cursor = !dialogstr.is_empty();
|
||||
|
||||
let screen = Screen::new(store)
|
||||
.show_dialog(dialogstr)
|
||||
.show_mode(modestr)
|
||||
.borders(true)
|
||||
.focus(focused);
|
||||
f.render_stateful_widget(screen, area, sstate);
|
||||
|
||||
if hide_cursor {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some((cx, cy)) = sstate.get_term_cursor() {
|
||||
if let Some(c) = cursor {
|
||||
let style = Style::default().fg(Color::Green);
|
||||
@@ -194,8 +307,6 @@ impl Application {
|
||||
}
|
||||
f.set_cursor(cx, cy);
|
||||
}
|
||||
|
||||
store.application.load_older(msg_load_req(area));
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
@@ -215,8 +326,11 @@ impl Application {
|
||||
Event::Mouse(_) => {
|
||||
// Do nothing for now.
|
||||
},
|
||||
Event::FocusGained | Event::FocusLost => {
|
||||
// Do nothing for now.
|
||||
Event::FocusGained => {
|
||||
self.focused = true;
|
||||
},
|
||||
Event::FocusLost => {
|
||||
self.focused = false;
|
||||
},
|
||||
Event::Resize(_, _) => {
|
||||
// We'll redraw for the new size next time step() is called.
|
||||
@@ -230,7 +344,8 @@ impl Application {
|
||||
match self.screen.editor_command(&act, &ctx, store.deref_mut()) {
|
||||
Ok(None) => {},
|
||||
Ok(Some(info)) => {
|
||||
self.screen.push_info(info);
|
||||
drop(store);
|
||||
self.handle_info(info);
|
||||
},
|
||||
Err(e) => {
|
||||
self.screen.push_error(e);
|
||||
@@ -294,8 +409,7 @@ impl Application {
|
||||
Action::CommandBar(act) => self.screen.command_bar(&act, &ctx)?,
|
||||
Action::Macro(act) => self.bindings.macro_command(&act, &ctx, store)?,
|
||||
Action::Scroll(style) => self.screen.scroll(&style, &ctx, store)?,
|
||||
Action::Suspend => self.terminal.program_suspend()?,
|
||||
Action::Tab(cmd) => self.screen.tab_command(&cmd, &ctx, store)?,
|
||||
Action::ShowInfoMessage(info) => Some(info),
|
||||
Action::Window(cmd) => self.screen.window_command(&cmd, &ctx, store)?,
|
||||
|
||||
Action::Jump(l, dir, count) => {
|
||||
@@ -304,8 +418,20 @@ impl Application {
|
||||
|
||||
None
|
||||
},
|
||||
Action::Suspend => {
|
||||
self.terminal.program_suspend()?;
|
||||
|
||||
None
|
||||
},
|
||||
|
||||
// UI actions.
|
||||
Action::Tab(cmd) => {
|
||||
if let TabAction::Close(_, _) = &cmd {
|
||||
self.last_layout = self.screen.as_description().into();
|
||||
}
|
||||
|
||||
self.screen.tab_command(&cmd, &ctx, store)?
|
||||
},
|
||||
Action::RedrawScreen => {
|
||||
self.screen.clear_message();
|
||||
self.redraw(true, store)?;
|
||||
@@ -321,7 +447,7 @@ impl Application {
|
||||
None
|
||||
},
|
||||
Action::Command(act) => {
|
||||
let acts = self.cmds.command(&act, &ctx)?;
|
||||
let acts = store.application.cmds.command(&act, &ctx)?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
@@ -360,6 +486,12 @@ impl Application {
|
||||
None
|
||||
},
|
||||
|
||||
IambAction::Homeserver(act) => {
|
||||
let acts = self.homeserver_command(act, ctx, store).await?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
},
|
||||
IambAction::Message(act) => {
|
||||
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
|
||||
},
|
||||
@@ -392,6 +524,37 @@ impl Application {
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
async fn homeserver_command(
|
||||
&mut self,
|
||||
action: HomeserverAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
|
||||
match action {
|
||||
HomeserverAction::CreateRoom(alias, vis, flags) => {
|
||||
let client = &store.application.worker.client;
|
||||
let room_id = create_room(client, alias.as_deref(), vis, flags).await?;
|
||||
let room = IambId::Room(room_id);
|
||||
let target = OpenTarget::Application(room);
|
||||
let action = WindowAction::Switch(target);
|
||||
|
||||
Ok(vec![(action.into(), ctx)])
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_info(&mut self, info: InfoMessage) {
|
||||
match info {
|
||||
InfoMessage::Message(info) => {
|
||||
self.screen.push_info(info);
|
||||
},
|
||||
InfoMessage::Pager(text) => {
|
||||
let pager = Box::new(Pager::new(text, vec![]));
|
||||
self.bindings.run_dialog(pager);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<(), std::io::Error> {
|
||||
self.terminal.clear()?;
|
||||
|
||||
@@ -412,11 +575,18 @@ impl Application {
|
||||
continue;
|
||||
},
|
||||
Ok(Some(info)) => {
|
||||
self.screen.push_info(info);
|
||||
self.handle_info(info);
|
||||
|
||||
// Continue processing; we'll redraw later.
|
||||
continue;
|
||||
},
|
||||
Err(
|
||||
UIError::NeedConfirm(dialog) |
|
||||
UIError::EditingFailure(EditError::NeedConfirm(dialog)),
|
||||
) => {
|
||||
self.bindings.run_dialog(dialog);
|
||||
continue;
|
||||
},
|
||||
Err(e) => {
|
||||
self.screen.push_error(e);
|
||||
|
||||
@@ -428,6 +598,19 @@ impl Application {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref layout) = self.last_layout {
|
||||
let locked = self.store.lock().await;
|
||||
let path = locked.application.settings.layout_json.as_path();
|
||||
path.parent().map(create_dir_all).transpose()?;
|
||||
|
||||
let file = File::create(path)?;
|
||||
let writer = BufWriter::new(file);
|
||||
|
||||
if let Err(e) = serde_json::to_writer(writer, layout) {
|
||||
tracing::error!("Failed to save window layout while exiting: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||
self.terminal.show_cursor()?;
|
||||
@@ -467,6 +650,20 @@ async fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<
|
||||
}
|
||||
}
|
||||
|
||||
// Perform an initial, lazily-loaded sync.
|
||||
let mut room = RoomEventFilter::default();
|
||||
room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
|
||||
|
||||
let mut room_ev = RoomFilter::default();
|
||||
room_ev.state = room;
|
||||
|
||||
let mut filter = FilterDefinition::default();
|
||||
filter.room = room_ev;
|
||||
|
||||
let settings = SyncSettings::new().filter(filter.into());
|
||||
|
||||
worker.client.sync_once(settings).await.map_err(IambError::from)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -485,13 +682,18 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
|
||||
|
||||
login(worker, &settings).await.unwrap_or_else(print_exit);
|
||||
|
||||
fn restore_tty() {
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
let _ = crossterm::execute!(stdout(), DisableBracketedPaste);
|
||||
let _ = crossterm::execute!(stdout(), DisableFocusChange);
|
||||
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
||||
let _ = crossterm::execute!(stdout(), CursorShow);
|
||||
}
|
||||
|
||||
// Make sure panics clean up the terminal properly.
|
||||
let orig_hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |panic_info| {
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
let _ = crossterm::execute!(stdout(), DisableBracketedPaste);
|
||||
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
||||
let _ = crossterm::execute!(stdout(), CursorShow);
|
||||
restore_tty();
|
||||
orig_hook(panic_info);
|
||||
process::exit(1);
|
||||
}));
|
||||
@@ -500,6 +702,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
|
||||
|
||||
// We can now run the application.
|
||||
application.run().await?;
|
||||
restore_tty();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -511,6 +714,12 @@ fn main() -> IambResult<()> {
|
||||
// Load configuration and set up the Matrix SDK.
|
||||
let settings = ApplicationSettings::load(iamb).unwrap_or_else(print_exit);
|
||||
|
||||
// Set umask on Unix platforms so that tokens, keys, etc. are only readable by the user.
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
libc::umask(0o077);
|
||||
};
|
||||
|
||||
// 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();
|
||||
@@ -523,12 +732,13 @@ fn main() -> IambResult<()> {
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.with_writer(appender)
|
||||
.with_max_level(Level::TRACE)
|
||||
.with_max_level(settings.tunables.log_level)
|
||||
.finish();
|
||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.worker_threads(2)
|
||||
.thread_name_fn(|| {
|
||||
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//! 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
|
||||
//! <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.
|
||||
@@ -130,7 +130,6 @@ impl Table {
|
||||
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;
|
||||
@@ -238,6 +237,7 @@ pub enum StyleTreeNode {
|
||||
Image(Option<String>),
|
||||
List(StyleTreeChildren, ListStyle),
|
||||
Paragraph(Box<StyleTreeNode>),
|
||||
Pre(Box<StyleTreeNode>),
|
||||
Reply(Box<StyleTreeNode>),
|
||||
Ruler,
|
||||
Style(Box<StyleTreeNode>, Style),
|
||||
@@ -271,10 +271,12 @@ impl StyleTreeNode {
|
||||
},
|
||||
StyleTreeNode::Header(child, level) => {
|
||||
let style = style.add_modifier(StyleModifier::BOLD);
|
||||
let mut hashes = "#".repeat(*level);
|
||||
hashes.push(' ');
|
||||
|
||||
printer.push_str(hashes, style);
|
||||
for _ in 0..*level {
|
||||
printer.push_str("#", style);
|
||||
}
|
||||
|
||||
printer.push_str(" ", style);
|
||||
child.print(printer, style);
|
||||
},
|
||||
StyleTreeNode::Image(None) => {},
|
||||
@@ -310,6 +312,39 @@ impl StyleTreeNode {
|
||||
child.print(printer, style);
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Pre(child) => {
|
||||
let mut subp = printer.sub(2).literal(true);
|
||||
let subw = subp.width();
|
||||
|
||||
child.print(&mut subp, style);
|
||||
|
||||
printer.commit();
|
||||
printer.push_line(
|
||||
vec![
|
||||
Span::styled(line::TOP_LEFT, style),
|
||||
Span::styled(line::HORIZONTAL.repeat(subw), style),
|
||||
Span::styled(line::TOP_RIGHT, style),
|
||||
]
|
||||
.into(),
|
||||
);
|
||||
|
||||
for mut line in subp.finish() {
|
||||
line.0.insert(0, Span::styled(line::VERTICAL, style));
|
||||
line.0.push(Span::styled(line::VERTICAL, style));
|
||||
printer.push_line(line);
|
||||
}
|
||||
|
||||
printer.push_line(
|
||||
vec![
|
||||
Span::styled(line::BOTTOM_LEFT, style),
|
||||
Span::styled(line::HORIZONTAL.repeat(subw), style),
|
||||
Span::styled(line::BOTTOM_RIGHT, style),
|
||||
]
|
||||
.into(),
|
||||
);
|
||||
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Reply(child) => {
|
||||
if printer.hide_reply() {
|
||||
return;
|
||||
@@ -320,7 +355,9 @@ impl StyleTreeNode {
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Ruler => {
|
||||
printer.push_str(line::HORIZONTAL.repeat(width), style);
|
||||
for _ in 0..width {
|
||||
printer.push_str(line::HORIZONTAL, style);
|
||||
}
|
||||
},
|
||||
StyleTreeNode::Table(table) => {
|
||||
let text = table.to_text(width, style);
|
||||
@@ -582,6 +619,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
|
||||
// Other text blocks.
|
||||
"blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())),
|
||||
"div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())),
|
||||
"pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())),
|
||||
|
||||
// No children.
|
||||
"hr" => StyleTreeNode::Ruler,
|
||||
@@ -590,7 +628,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
|
||||
"img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())),
|
||||
|
||||
// These don't render in any special way.
|
||||
"a" | "details" | "html" | "pre" | "summary" | "sub" | "sup" => {
|
||||
"a" | "details" | "html" | "summary" | "sub" | "sup" => {
|
||||
*c2t(&node.children.borrow())
|
||||
},
|
||||
|
||||
@@ -615,7 +653,7 @@ pub fn parse_matrix_html(s: &str) -> StyleTree {
|
||||
let dom = parse_fragment(
|
||||
RcDom::default(),
|
||||
ParseOpts::default(),
|
||||
QualName::new(None, ns!(), local_name!("div")),
|
||||
QualName::new(None, ns!(html), local_name!("body")),
|
||||
vec![],
|
||||
)
|
||||
.one(StrTendril::from(s));
|
||||
@@ -627,6 +665,7 @@ pub fn parse_matrix_html(s: &str) -> StyleTree {
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::util::space_span;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_header() {
|
||||
@@ -636,8 +675,11 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("1", bold),
|
||||
space_span(10, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -645,8 +687,12 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("2", bold),
|
||||
space_span(9, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -654,8 +700,13 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("3", bold),
|
||||
space_span(8, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -663,8 +714,14 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("4", bold),
|
||||
space_span(7, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -672,8 +729,15 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("5", bold),
|
||||
space_span(6, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -681,8 +745,16 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("6", bold),
|
||||
space_span(5, Style::default())
|
||||
])]);
|
||||
}
|
||||
@@ -700,7 +772,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Bold", bold),
|
||||
Span::styled("!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
@@ -708,7 +781,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Bold", bold),
|
||||
Span::styled("!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
@@ -716,7 +790,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Italic", italic),
|
||||
Span::styled("!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
@@ -724,7 +799,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Italic", italic),
|
||||
Span::styled("!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
@@ -732,7 +808,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Strikethrough", strike),
|
||||
Span::styled("!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
@@ -740,7 +817,8 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Strikethrough", strike),
|
||||
Span::styled("!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
@@ -748,19 +826,28 @@ pub mod tests {
|
||||
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),
|
||||
Span::styled("Underline", underl),
|
||||
Span::styled("!", 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)])]);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Red", red),
|
||||
Span::styled("!", 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)])]);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Red", red),
|
||||
Span::styled("!", red),
|
||||
space_span(16, def)
|
||||
])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -769,13 +856,25 @@ pub mod tests {
|
||||
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[0],
|
||||
Spans(vec![Span::raw("Hello"), Span::raw(" "), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw("world"), Span::raw("!"), 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(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![Span::raw("Goodbye"), Span::raw(" "), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[6],
|
||||
Spans(vec![Span::raw("world"), Span::raw("!"), Span::raw(" ")])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -784,8 +883,14 @@ pub mod tests {
|
||||
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!")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![Span::raw(" "), Span::raw("Hello"), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw(" "), Span::raw("world"), Span::raw("!")])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -794,12 +899,60 @@ pub mod tests {
|
||||
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(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("1")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("2")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[4],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("3")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -808,20 +961,59 @@ pub mod tests {
|
||||
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[0],
|
||||
Spans(vec![
|
||||
Span::raw("1. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("1")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("2. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
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(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("2")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[4],
|
||||
Spans(vec![
|
||||
Span::raw("3. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
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(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("3")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -854,9 +1046,13 @@ pub mod tests {
|
||||
]);
|
||||
assert_eq!(text.lines[2].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 1", bold),
|
||||
Span::styled("mn", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("1", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 2", bold),
|
||||
Span::styled("mn", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("2", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("umn", bold),
|
||||
Span::raw("│")
|
||||
@@ -867,8 +1063,8 @@ pub mod tests {
|
||||
Span::raw("│"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::styled(" 3", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("3", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::raw("│")
|
||||
]);
|
||||
|
||||
@@ -928,15 +1124,161 @@ pub mod tests {
|
||||
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(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("was"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw("replied"), Span::raw(" "), Span::raw("to")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("is"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![
|
||||
Span::raw("the"),
|
||||
Span::raw(" "),
|
||||
Span::raw("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(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("is"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![
|
||||
Span::raw("the"),
|
||||
Span::raw(" "),
|
||||
Span::raw("reply"),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_self_closing() {
|
||||
let s = "Hello<br>World<br>Goodbye";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(7, Style::default(), true);
|
||||
assert_eq!(text.lines.len(), 3);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("Hello"), Span::raw(" "),]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("World"), Span::raw(" "),]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("Goodbye")]),);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_embedded_newline() {
|
||||
let s = "<p>Hello\nWorld</p>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(15, Style::default(), true);
|
||||
assert_eq!(text.lines.len(), 1);
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("Hello"),
|
||||
Span::raw(" "),
|
||||
Span::raw("World"),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pre_tag() {
|
||||
let s = concat!(
|
||||
"<pre><code class=\"language-rust\">",
|
||||
"fn hello() -> usize {\n",
|
||||
" return 5;\n",
|
||||
"}\n",
|
||||
"</code></pre>\n"
|
||||
);
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(25, Style::default(), true);
|
||||
assert_eq!(text.lines.len(), 5);
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw(line::TOP_LEFT),
|
||||
Span::raw(line::HORIZONTAL.repeat(23)),
|
||||
Span::raw(line::TOP_RIGHT)
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![
|
||||
Span::raw(line::VERTICAL),
|
||||
Span::raw("fn"),
|
||||
Span::raw(" "),
|
||||
Span::raw("hello"),
|
||||
Span::raw("("),
|
||||
Span::raw(")"),
|
||||
Span::raw(" "),
|
||||
Span::raw("-"),
|
||||
Span::raw(">"),
|
||||
Span::raw(" "),
|
||||
Span::raw("usize"),
|
||||
Span::raw(" "),
|
||||
Span::raw("{"),
|
||||
Span::raw(" "),
|
||||
Span::raw(line::VERTICAL)
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw(line::VERTICAL),
|
||||
Span::raw(" "),
|
||||
Span::raw("return"),
|
||||
Span::raw(" "),
|
||||
Span::raw("5"),
|
||||
Span::raw(";"),
|
||||
Span::raw(" "),
|
||||
Span::raw(line::VERTICAL)
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![
|
||||
Span::raw(line::VERTICAL),
|
||||
Span::raw("}"),
|
||||
Span::raw(" ".repeat(22)),
|
||||
Span::raw(line::VERTICAL)
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[4],
|
||||
Spans(vec![
|
||||
Span::raw(line::BOTTOM_LEFT),
|
||||
Span::raw(line::HORIZONTAL.repeat(23)),
|
||||
Span::raw(line::BOTTOM_RIGHT)
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,17 @@ use std::hash::{Hash, Hasher};
|
||||
use std::slice::Iter;
|
||||
|
||||
use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone};
|
||||
use comrak::{markdown_to_html, ComrakOptions};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use matrix_sdk::ruma::{
|
||||
events::{
|
||||
room::{
|
||||
encrypted::{
|
||||
OriginalRoomEncryptedEvent,
|
||||
RedactedRoomEncryptedEvent,
|
||||
RoomEncryptedEvent,
|
||||
},
|
||||
message::{
|
||||
FormattedBody,
|
||||
MessageFormat,
|
||||
@@ -21,11 +27,13 @@ use matrix_sdk::ruma::{
|
||||
Relation,
|
||||
RoomMessageEvent,
|
||||
RoomMessageEventContent,
|
||||
TextMessageEventContent,
|
||||
},
|
||||
redaction::SyncRoomRedactionEvent,
|
||||
},
|
||||
AnyMessageLikeEvent,
|
||||
Redact,
|
||||
RedactedUnsigned,
|
||||
},
|
||||
EventId,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
@@ -87,6 +95,20 @@ 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);
|
||||
|
||||
fn text_to_message_content(input: String) -> TextMessageEventContent {
|
||||
let mut options = ComrakOptions::default();
|
||||
options.extension.shortcodes = true;
|
||||
options.render.hardbreaks = true;
|
||||
let html = markdown_to_html(input.as_str(), &options);
|
||||
|
||||
TextMessageEventContent::html(input, html)
|
||||
}
|
||||
|
||||
pub fn text_to_message(input: String) -> RoomMessageEventContent {
|
||||
let msg = MessageType::Text(text_to_message_content(input));
|
||||
RoomMessageEventContent::new(msg)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> {
|
||||
let time = i64::from(ms) / 1000;
|
||||
@@ -318,6 +340,8 @@ impl PartialOrd for MessageCursor {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum MessageEvent {
|
||||
EncryptedOriginal(Box<OriginalRoomEncryptedEvent>),
|
||||
EncryptedRedacted(Box<RedactedRoomEncryptedEvent>),
|
||||
Original(Box<OriginalRoomMessageEvent>),
|
||||
Redacted(Box<RedactedRoomMessageEvent>),
|
||||
Local(OwnedEventId, Box<RoomMessageEventContent>),
|
||||
@@ -326,35 +350,45 @@ pub enum MessageEvent {
|
||||
impl MessageEvent {
|
||||
pub fn event_id(&self) -> &EventId {
|
||||
match self {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.as_ref(),
|
||||
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 content(&self) -> Option<&RoomMessageEventContent> {
|
||||
match self {
|
||||
MessageEvent::EncryptedOriginal(_) => None,
|
||||
MessageEvent::Original(ev) => Some(&ev.content),
|
||||
MessageEvent::EncryptedRedacted(_) => None,
|
||||
MessageEvent::Redacted(_) => None,
|
||||
MessageEvent::Local(_, content) => Some(content),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_emote(&self) -> bool {
|
||||
matches!(
|
||||
self.content(),
|
||||
Some(RoomMessageEventContent { msgtype: MessageType::Emote(_), .. })
|
||||
)
|
||||
}
|
||||
|
||||
pub fn body(&self) -> Cow<'_, str> {
|
||||
match self {
|
||||
MessageEvent::EncryptedOriginal(_) => "[Unable to decrypt message]".into(),
|
||||
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::EncryptedRedacted(ev) => body_cow_reason(&ev.unsigned),
|
||||
MessageEvent::Redacted(ev) => body_cow_reason(&ev.unsigned),
|
||||
MessageEvent::Local(_, content) => body_cow_content(content),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn html(&self) -> Option<StyleTree> {
|
||||
let content = match self {
|
||||
MessageEvent::EncryptedOriginal(_) => return None,
|
||||
MessageEvent::EncryptedRedacted(_) => return None,
|
||||
MessageEvent::Original(ev) => &ev.content,
|
||||
MessageEvent::Redacted(_) => return None,
|
||||
MessageEvent::Local(_, content) => content,
|
||||
@@ -371,8 +405,10 @@ impl MessageEvent {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
||||
fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
||||
match self {
|
||||
MessageEvent::EncryptedOriginal(_) => return,
|
||||
MessageEvent::EncryptedRedacted(_) => return,
|
||||
MessageEvent::Redacted(_) => return,
|
||||
MessageEvent::Local(_, _) => return,
|
||||
MessageEvent::Original(ev) => {
|
||||
@@ -411,6 +447,20 @@ fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
||||
Cow::Borrowed(s)
|
||||
}
|
||||
|
||||
fn body_cow_reason(unsigned: &RedactedUnsigned) -> Cow<'_, str> {
|
||||
let reason = 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]")
|
||||
}
|
||||
}
|
||||
|
||||
enum MessageColumns {
|
||||
/// Four columns: sender, message, timestamp, read receipts.
|
||||
Four,
|
||||
@@ -548,6 +598,8 @@ impl Message {
|
||||
|
||||
pub fn reply_to(&self) -> Option<OwnedEventId> {
|
||||
let content = match &self.event {
|
||||
MessageEvent::EncryptedOriginal(_) => return None,
|
||||
MessageEvent::EncryptedRedacted(_) => return None,
|
||||
MessageEvent::Local(_, content) => content,
|
||||
MessageEvent::Original(ev) => &ev.content,
|
||||
MessageEvent::Redacted(_) => return None,
|
||||
@@ -592,7 +644,7 @@ impl Message {
|
||||
{
|
||||
let cols = MessageColumns::Four;
|
||||
let fill = width - USER_GUTTER - TIME_GUTTER - READ_GUTTER;
|
||||
let user = self.show_sender(prev, true, settings);
|
||||
let user = self.show_sender(prev, true, info, settings);
|
||||
let time = self.timestamp.show_time();
|
||||
let read = match info.receipts.get(self.event.event_id()) {
|
||||
Some(read) => read.iter(),
|
||||
@@ -603,7 +655,7 @@ impl Message {
|
||||
} 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 user = self.show_sender(prev, true, info, settings);
|
||||
let time = self.timestamp.show_time();
|
||||
let read = [].iter();
|
||||
|
||||
@@ -611,7 +663,7 @@ impl Message {
|
||||
} 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 user = self.show_sender(prev, true, info, settings);
|
||||
let time = None;
|
||||
let read = [].iter();
|
||||
|
||||
@@ -619,7 +671,7 @@ impl Message {
|
||||
} else {
|
||||
let cols = MessageColumns::One;
|
||||
let fill = width.saturating_sub(2);
|
||||
let user = self.show_sender(prev, false, settings);
|
||||
let user = self.show_sender(prev, false, info, settings);
|
||||
let time = None;
|
||||
let read = [].iter();
|
||||
|
||||
@@ -648,7 +700,7 @@ impl Message {
|
||||
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 mut sender = r.sender_span(info, settings);
|
||||
let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
|
||||
let trailing = w.saturating_sub(sender_width + 1);
|
||||
|
||||
@@ -710,7 +762,11 @@ impl Message {
|
||||
key
|
||||
};
|
||||
|
||||
emojis.push_str(format!("[{name} {count}]"), style);
|
||||
emojis.push_str("[", style);
|
||||
emojis.push_str(name, style);
|
||||
emojis.push_str(" ", style);
|
||||
emojis.push_span_nobreak(Span::styled(count.to_string(), style));
|
||||
emojis.push_str("]", style);
|
||||
|
||||
reactions += 1;
|
||||
}
|
||||
@@ -737,23 +793,31 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
fn sender_span(&self, settings: &ApplicationSettings) -> Span {
|
||||
settings.get_user_span(self.sender.as_ref())
|
||||
fn sender_span<'a>(
|
||||
&'a self,
|
||||
info: &'a RoomInfo,
|
||||
settings: &'a ApplicationSettings,
|
||||
) -> Span<'a> {
|
||||
settings.get_user_span(self.sender.as_ref(), info)
|
||||
}
|
||||
|
||||
fn show_sender(
|
||||
&self,
|
||||
fn show_sender<'a>(
|
||||
&'a self,
|
||||
prev: Option<&Message>,
|
||||
align_right: bool,
|
||||
settings: &ApplicationSettings,
|
||||
) -> Option<Span> {
|
||||
info: &'a RoomInfo,
|
||||
settings: &'a ApplicationSettings,
|
||||
) -> Option<Span<'a>> {
|
||||
if let Some(prev) = prev {
|
||||
if self.sender == prev.sender && self.timestamp.same_day(&prev.timestamp) {
|
||||
if self.sender == prev.sender &&
|
||||
self.timestamp.same_day(&prev.timestamp) &&
|
||||
!self.event.is_emote()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let Span { content, style } = self.sender_span(settings);
|
||||
let Span { content, style } = self.sender_span(info, settings);
|
||||
let stop = content.len().min(28);
|
||||
let s = &content[..stop];
|
||||
|
||||
@@ -765,6 +829,24 @@ impl Message {
|
||||
|
||||
Span::styled(sender, style).into()
|
||||
}
|
||||
|
||||
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
||||
self.event.redact(redaction, version);
|
||||
self.html = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RoomEncryptedEvent> for Message {
|
||||
fn from(event: RoomEncryptedEvent) -> Self {
|
||||
let timestamp = event.origin_server_ts().into();
|
||||
let user_id = event.sender().to_owned();
|
||||
let content = match event {
|
||||
RoomEncryptedEvent::Original(ev) => MessageEvent::EncryptedOriginal(ev.into()),
|
||||
RoomEncryptedEvent::Redacted(ev) => MessageEvent::EncryptedRedacted(ev.into()),
|
||||
};
|
||||
|
||||
Message::new(content, user_id, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OriginalRoomMessageEvent> for Message {
|
||||
@@ -911,4 +993,53 @@ pub mod tests {
|
||||
// MessageCursor::latest() should point at the most recent message after conversion.
|
||||
assert_eq!(identity(&mc6), mc1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_markdown_message() {
|
||||
let input = "**bold**\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p><strong>bold</strong></p>\n");
|
||||
|
||||
let input = "*emphasis*\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p><em>emphasis</em></p>\n");
|
||||
|
||||
let input = "`code`\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p><code>code</code></p>\n");
|
||||
|
||||
let input = "```rust\nconst A: usize = 1;\n```\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(
|
||||
content.formatted.unwrap().body,
|
||||
"<pre><code class=\"language-rust\">const A: usize = 1;\n</code></pre>\n"
|
||||
);
|
||||
|
||||
let input = ":heart:\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p>\u{2764}\u{FE0F}</p>\n");
|
||||
|
||||
let input = "para 1\n\npara 2\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p>para 1</p>\n<p>para 2</p>\n");
|
||||
|
||||
let input = "line 1\nline 2\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(content.formatted.unwrap().body, "<p>line 1<br />\nline 2</p>\n");
|
||||
|
||||
let input = "# Heading\n## Subheading\n\ntext\n";
|
||||
let content = text_to_message_content(input.into());
|
||||
assert_eq!(content.body, input);
|
||||
assert_eq!(
|
||||
content.formatted.unwrap().body,
|
||||
"<h1>Heading</h1>\n<h2>Subheading</h2>\n<p>text</p>\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::borrow::Cow;
|
||||
use modalkit::tui::layout::Alignment;
|
||||
use modalkit::tui::style::Style;
|
||||
use modalkit::tui::text::{Span, Spans, Text};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::util::{space_span, take_width};
|
||||
@@ -16,6 +17,7 @@ pub struct TextPrinter<'a> {
|
||||
alignment: Alignment,
|
||||
curr_spans: Vec<Span<'a>>,
|
||||
curr_width: usize,
|
||||
literal: bool,
|
||||
}
|
||||
|
||||
impl<'a> TextPrinter<'a> {
|
||||
@@ -29,6 +31,7 @@ impl<'a> TextPrinter<'a> {
|
||||
alignment: Alignment::Left,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
literal: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +40,11 @@ impl<'a> TextPrinter<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn literal(mut self, literal: bool) -> Self {
|
||||
self.literal = literal;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn hide_reply(&self) -> bool {
|
||||
self.hide_reply
|
||||
}
|
||||
@@ -55,6 +63,7 @@ impl<'a> TextPrinter<'a> {
|
||||
alignment: self.alignment,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
literal: self.literal,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +116,7 @@ impl<'a> TextPrinter<'a> {
|
||||
self.push();
|
||||
}
|
||||
|
||||
pub fn push_str<T>(&mut self, s: T, style: Style)
|
||||
fn push_str_wrapped<T>(&mut self, s: T, style: Style)
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
@@ -140,6 +149,69 @@ impl<'a> TextPrinter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_span_nobreak(&mut self, span: Span<'a>) {
|
||||
let sw = UnicodeWidthStr::width(span.content.as_ref());
|
||||
|
||||
if self.curr_width + sw > self.width {
|
||||
// Span doesn't fit on this line, so start a new one.
|
||||
self.commit();
|
||||
}
|
||||
|
||||
self.curr_spans.push(span);
|
||||
self.curr_width += sw;
|
||||
}
|
||||
|
||||
pub fn push_str(&mut self, s: &'a str, style: Style) {
|
||||
let style = self.base_style.patch(style);
|
||||
|
||||
if self.width == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
for mut word in UnicodeSegmentation::split_word_bounds(s) {
|
||||
if let "\n" | "\r\n" = word {
|
||||
if self.literal {
|
||||
self.commit();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Render embedded newlines as spaces.
|
||||
word = " ";
|
||||
}
|
||||
|
||||
if !self.literal && self.curr_width == 0 && word.chars().all(char::is_whitespace) {
|
||||
// Drop leading whitespace.
|
||||
continue;
|
||||
}
|
||||
|
||||
let sw = UnicodeWidthStr::width(word);
|
||||
|
||||
if sw > self.width {
|
||||
self.push_str_wrapped(word, style);
|
||||
continue;
|
||||
}
|
||||
|
||||
if self.curr_width + sw > self.width {
|
||||
// Word doesn't fit on this line, so start a new one.
|
||||
self.commit();
|
||||
|
||||
if !self.literal && word.chars().all(char::is_whitespace) {
|
||||
// Drop leading whitespace.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let span = Span::styled(word, style);
|
||||
self.curr_spans.push(span);
|
||||
self.curr_width += sw;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
27
src/tests.rs
27
src/tests.rs
@@ -17,6 +17,7 @@ use matrix_sdk::ruma::{
|
||||
use lazy_static::lazy_static;
|
||||
use modalkit::tui::style::{Color, Style};
|
||||
use tokio::sync::mpsc::unbounded_channel;
|
||||
use tracing::Level;
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
@@ -29,6 +30,7 @@ use crate::{
|
||||
ProfileConfig,
|
||||
TunableValues,
|
||||
UserColor,
|
||||
UserDisplayStyle,
|
||||
UserDisplayTunables,
|
||||
},
|
||||
message::{
|
||||
@@ -41,6 +43,8 @@ use crate::{
|
||||
worker::Requester,
|
||||
};
|
||||
|
||||
const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
|
||||
|
||||
lazy_static! {
|
||||
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();
|
||||
@@ -153,9 +157,11 @@ pub fn mock_room() -> RoomInfo {
|
||||
read_till: None,
|
||||
reactions: HashMap::new(),
|
||||
|
||||
fetching: false,
|
||||
fetch_id: RoomFetchStatus::NotStarted,
|
||||
fetch_last: None,
|
||||
users_typing: None,
|
||||
display_names: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,17 +169,19 @@ pub fn mock_dirs() -> DirectoryValues {
|
||||
DirectoryValues {
|
||||
cache: PathBuf::new(),
|
||||
logs: PathBuf::new(),
|
||||
downloads: PathBuf::new(),
|
||||
downloads: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mock_tunables() -> TunableValues {
|
||||
TunableValues {
|
||||
default_room: None,
|
||||
log_level: Level::INFO,
|
||||
reaction_display: true,
|
||||
reaction_shortcode_display: false,
|
||||
read_receipt_send: true,
|
||||
read_receipt_display: true,
|
||||
request_timeout: 120,
|
||||
typing_notice_send: true,
|
||||
typing_notice_display: true,
|
||||
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
||||
@@ -182,22 +190,28 @@ pub fn mock_tunables() -> TunableValues {
|
||||
})]
|
||||
.into_iter()
|
||||
.collect::<HashMap<_, _>>(),
|
||||
open_command: None,
|
||||
username_display: UserDisplayStyle::Username,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mock_settings() -> ApplicationSettings {
|
||||
ApplicationSettings {
|
||||
matrix_dir: PathBuf::new(),
|
||||
layout_json: PathBuf::new(),
|
||||
session_json: PathBuf::new(),
|
||||
|
||||
profile_name: "test".into(),
|
||||
profile: ProfileConfig {
|
||||
user_id: user_id!("@user:example.com").to_owned(),
|
||||
url: Url::parse("https://example.com").unwrap(),
|
||||
settings: None,
|
||||
dirs: None,
|
||||
layout: None,
|
||||
},
|
||||
tunables: mock_tunables(),
|
||||
dirs: mock_dirs(),
|
||||
layout: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,10 +222,19 @@ pub async fn mock_store() -> ProgramStore {
|
||||
let worker = Requester { tx, client };
|
||||
|
||||
let mut store = ChatStore::new(worker, mock_settings());
|
||||
|
||||
// Add presence information.
|
||||
store.presences.get_or_default(TEST_USER1.clone());
|
||||
store.presences.get_or_default(TEST_USER2.clone());
|
||||
store.presences.get_or_default(TEST_USER3.clone());
|
||||
store.presences.get_or_default(TEST_USER4.clone());
|
||||
store.presences.get_or_default(TEST_USER5.clone());
|
||||
|
||||
let room_id = TEST_ROOM1_ID.clone();
|
||||
let info = mock_room();
|
||||
|
||||
store.rooms.insert(room_id, info);
|
||||
store.rooms.insert(room_id.clone(), info);
|
||||
store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
|
||||
|
||||
ProgramStore::new(store)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use matrix_sdk::{
|
||||
encryption::verification::{format_emojis, SasVerification},
|
||||
@@ -10,7 +12,6 @@ use matrix_sdk::{
|
||||
OwnedRoomId,
|
||||
RoomId,
|
||||
},
|
||||
DisplayName,
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
@@ -45,7 +46,9 @@ use modalkit::{
|
||||
ScrollStyle,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
},
|
||||
widgets::{
|
||||
list::{List, ListCursor, ListItem, ListState},
|
||||
@@ -76,6 +79,10 @@ use self::{room::RoomState, welcome::WelcomeState};
|
||||
pub mod room;
|
||||
pub mod welcome;
|
||||
|
||||
type MatrixRoomInfo = Arc<(MatrixRoom, Option<Tags>)>;
|
||||
|
||||
const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
|
||||
|
||||
#[inline]
|
||||
fn bold_style() -> Style {
|
||||
Style::default().add_modifier(StyleModifier::BOLD)
|
||||
@@ -195,7 +202,7 @@ fn room_prompt(
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
@@ -210,7 +217,7 @@ macro_rules! delegate {
|
||||
match $s {
|
||||
IambWindow::Room($id) => $e,
|
||||
IambWindow::DirectList($id) => $e,
|
||||
IambWindow::MemberList($id, _) => $e,
|
||||
IambWindow::MemberList($id, _, _) => $e,
|
||||
IambWindow::RoomList($id) => $e,
|
||||
IambWindow::SpaceList($id) => $e,
|
||||
IambWindow::VerifyList($id) => $e,
|
||||
@@ -221,7 +228,7 @@ macro_rules! delegate {
|
||||
|
||||
pub enum IambWindow {
|
||||
DirectList(DirectListState),
|
||||
MemberList(MemberListState, OwnedRoomId),
|
||||
MemberList(MemberListState, OwnedRoomId, Option<Instant>),
|
||||
Room(RoomState),
|
||||
VerifyList(VerifyListState),
|
||||
RoomList(RoomListState),
|
||||
@@ -376,10 +383,13 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(state) => state.draw(area, buf, focused, store),
|
||||
IambWindow::DirectList(state) => {
|
||||
let dms = store.application.worker.direct_messages();
|
||||
let mut items = dms
|
||||
let mut items = store
|
||||
.application
|
||||
.sync_info
|
||||
.dms
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(id, name, tags)| DirectItem::new(id, name, tags, store))
|
||||
.map(|room_info| DirectItem::new(room_info, store))
|
||||
.collect::<Vec<_>>();
|
||||
items.sort();
|
||||
|
||||
@@ -391,10 +401,18 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.focus(focused)
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::MemberList(state, room_id) => {
|
||||
if let Ok(mems) = store.application.worker.members(room_id.clone()) {
|
||||
let items = mems.into_iter().map(MemberItem::new);
|
||||
state.set(items.collect());
|
||||
IambWindow::MemberList(state, room_id, last_fetch) => {
|
||||
let need_fetch = match last_fetch {
|
||||
Some(i) => i.elapsed() >= MEMBER_FETCH_DEBOUNCE,
|
||||
None => true,
|
||||
};
|
||||
|
||||
if need_fetch {
|
||||
if let Ok(mems) = store.application.worker.members(room_id.clone()) {
|
||||
let items = mems.into_iter().map(|m| MemberItem::new(m, room_id.clone()));
|
||||
state.set(items.collect());
|
||||
*last_fetch = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
List::new(store)
|
||||
@@ -404,10 +422,13 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::RoomList(state) => {
|
||||
let joined = store.application.worker.active_rooms();
|
||||
let mut items = joined
|
||||
let mut items = store
|
||||
.application
|
||||
.sync_info
|
||||
.rooms
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(room, name, tags)| RoomItem::new(room, name, tags, store))
|
||||
.map(|room_info| RoomItem::new(room_info, store))
|
||||
.collect::<Vec<_>>();
|
||||
items.sort();
|
||||
|
||||
@@ -420,9 +441,13 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::SpaceList(state) => {
|
||||
let spaces = store.application.worker.spaces();
|
||||
let items =
|
||||
spaces.into_iter().map(|(room, name)| SpaceItem::new(room, name, store));
|
||||
let items = store
|
||||
.application
|
||||
.sync_info
|
||||
.spaces
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|room| SpaceItem::new(room, store));
|
||||
state.set(items.collect());
|
||||
state.draw(area, buf, focused, store);
|
||||
|
||||
@@ -455,8 +480,8 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(w) => w.dup(store).into(),
|
||||
IambWindow::DirectList(w) => w.dup(store).into(),
|
||||
IambWindow::MemberList(w, room_id) => {
|
||||
IambWindow::MemberList(w.dup(store), room_id.clone())
|
||||
IambWindow::MemberList(w, room_id, last_fetch) => {
|
||||
IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
|
||||
},
|
||||
IambWindow::RoomList(w) => w.dup(store).into(),
|
||||
IambWindow::SpaceList(w) => w.dup(store).into(),
|
||||
@@ -469,6 +494,19 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
delegate!(self, w => w.close(flags, store))
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
delegate!(self, w => w.write(path, flags, store))
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
delegate!(self, w => w.get_completions())
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
delegate!(self, w => w.get_cursor_word(style))
|
||||
}
|
||||
@@ -483,7 +521,7 @@ impl Window<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
|
||||
IambWindow::DirectList(_) => IambId::DirectList,
|
||||
IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()),
|
||||
IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()),
|
||||
IambWindow::RoomList(_) => IambId::RoomList,
|
||||
IambWindow::SpaceList(_) => IambId::SpaceList,
|
||||
IambWindow::VerifyList(_) => IambId::VerifyList,
|
||||
@@ -504,10 +542,15 @@ impl Window<IambInfo> for IambWindow {
|
||||
|
||||
Spans::from(title)
|
||||
},
|
||||
IambWindow::MemberList(_, room_id) => {
|
||||
IambWindow::MemberList(state, room_id, _) => {
|
||||
let title = store.application.get_room_title(room_id.as_ref());
|
||||
|
||||
Spans(vec![bold_span("Room Members: "), title.into()])
|
||||
let n = state.len();
|
||||
let v = vec![
|
||||
bold_span("Room Members "),
|
||||
Span::styled(format!("({n}): "), bold_style()),
|
||||
title.into(),
|
||||
];
|
||||
Spans(v)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -521,10 +564,15 @@ impl Window<IambInfo> for IambWindow {
|
||||
IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
|
||||
|
||||
IambWindow::Room(w) => w.get_title(store),
|
||||
IambWindow::MemberList(_, room_id) => {
|
||||
IambWindow::MemberList(state, room_id, _) => {
|
||||
let title = store.application.get_room_title(room_id.as_ref());
|
||||
|
||||
Spans(vec![bold_span("Room Members: "), title.into()])
|
||||
let n = state.len();
|
||||
let v = vec![
|
||||
bold_span("Room Members "),
|
||||
Span::styled(format!("({n}): "), bold_style()),
|
||||
title.into(),
|
||||
];
|
||||
Spans(v)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -545,7 +593,7 @@ impl Window<IambInfo> for IambWindow {
|
||||
IambId::MemberList(room_id) => {
|
||||
let id = IambBufferId::MemberList(room_id.clone());
|
||||
let list = MemberListState::new(id, vec![]);
|
||||
let win = IambWindow::MemberList(list, room_id);
|
||||
let win = IambWindow::MemberList(list, room_id, None);
|
||||
|
||||
return Ok(win);
|
||||
},
|
||||
@@ -575,21 +623,18 @@ impl Window<IambInfo> for IambWindow {
|
||||
fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> {
|
||||
let ChatStore { names, worker, .. } = &mut store.application;
|
||||
|
||||
match names.entry(name) {
|
||||
Entry::Vacant(v) => {
|
||||
let room_id = worker.join_room(v.key().to_string())?;
|
||||
v.insert(room_id.clone());
|
||||
if let Some(room) = names.get_mut(&name) {
|
||||
let id = IambId::Room(room.clone());
|
||||
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
IambWindow::open(id, store)
|
||||
} else {
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
|
||||
Ok(room.into())
|
||||
},
|
||||
Entry::Occupied(o) => {
|
||||
let id = IambId::Room(o.get().clone());
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
|
||||
IambWindow::open(id, store)
|
||||
},
|
||||
Ok(room.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,31 +652,45 @@ impl Window<IambInfo> for IambWindow {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RoomItem {
|
||||
room: MatrixRoom,
|
||||
tags: Option<Tags>,
|
||||
room_info: MatrixRoomInfo,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl RoomItem {
|
||||
fn new(
|
||||
room: MatrixRoom,
|
||||
name: DisplayName,
|
||||
tags: Option<Tags>,
|
||||
store: &mut ProgramStore,
|
||||
) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
|
||||
let room = &room_info.deref().0;
|
||||
let room_id = room.room_id();
|
||||
|
||||
let info = store.application.get_room_info(room.room_id().to_owned());
|
||||
info.name = name.clone().into();
|
||||
info.tags = tags.clone();
|
||||
let info = store.application.get_room_info(room_id.to_owned());
|
||||
let name = info.name.clone().unwrap_or_default();
|
||||
info.tags = room_info.deref().1.clone();
|
||||
|
||||
RoomItem { room, tags, name }
|
||||
if let Some(alias) = room.canonical_alias() {
|
||||
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||
}
|
||||
|
||||
RoomItem { room_info, name }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room(&self) -> &MatrixRoom {
|
||||
&self.room_info.deref().0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room_id(&self) -> &RoomId {
|
||||
self.room().room_id()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn tags(&self) -> &Option<Tags> {
|
||||
&self.room_info.deref().1
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for RoomItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.room.room_id() == other.room.room_id()
|
||||
self.room_id() == other.room_id()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,7 +698,7 @@ 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))
|
||||
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -657,7 +716,7 @@ impl ToString for RoomItem {
|
||||
|
||||
impl ListItem<IambInfo> for RoomItem {
|
||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||
if let Some(tags) = &self.tags {
|
||||
if let Some(tags) = &self.tags() {
|
||||
let style = selected_style(selected);
|
||||
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||
|
||||
@@ -670,7 +729,7 @@ impl ListItem<IambInfo> for RoomItem {
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
self.room.room_id().to_string().into()
|
||||
self.room_id().to_string().into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -681,29 +740,37 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
room_prompt(self.room.room_id(), act, ctx)
|
||||
room_prompt(self.room_id(), act, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DirectItem {
|
||||
room: MatrixRoom,
|
||||
tags: Option<Tags>,
|
||||
room_info: MatrixRoomInfo,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl DirectItem {
|
||||
fn new(
|
||||
room: MatrixRoom,
|
||||
name: DisplayName,
|
||||
tags: Option<Tags>,
|
||||
store: &mut ProgramStore,
|
||||
) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
|
||||
let room_id = room_info.deref().0.room_id().to_owned();
|
||||
let name = store.application.get_room_info(room_id).name.clone().unwrap_or_default();
|
||||
|
||||
store.application.set_room_name(room.room_id(), name.as_str());
|
||||
DirectItem { room_info, name }
|
||||
}
|
||||
|
||||
DirectItem { room, tags, name }
|
||||
#[inline]
|
||||
fn room(&self) -> &MatrixRoom {
|
||||
&self.room_info.deref().0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room_id(&self) -> &RoomId {
|
||||
self.room().room_id()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn tags(&self) -> &Option<Tags> {
|
||||
&self.room_info.deref().1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -715,7 +782,7 @@ impl ToString for DirectItem {
|
||||
|
||||
impl ListItem<IambInfo> for DirectItem {
|
||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||
if let Some(tags) = &self.tags {
|
||||
if let Some(tags) = &self.tags() {
|
||||
let style = selected_style(selected);
|
||||
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||
|
||||
@@ -728,13 +795,13 @@ impl ListItem<IambInfo> for DirectItem {
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
self.room.room_id().to_string().into()
|
||||
self.room_id().to_string().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for DirectItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.room.room_id() == other.room.room_id()
|
||||
self.room_id() == other.room_id()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -742,7 +809,7 @@ 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))
|
||||
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -759,7 +826,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
room_prompt(self.room.room_id(), act, ctx)
|
||||
room_prompt(self.room_id(), act, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,10 +837,18 @@ pub struct SpaceItem {
|
||||
}
|
||||
|
||||
impl SpaceItem {
|
||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room: MatrixRoom, store: &mut ProgramStore) -> Self {
|
||||
let room_id = room.room_id();
|
||||
let name = store
|
||||
.application
|
||||
.get_room_info(room_id.to_owned())
|
||||
.name
|
||||
.clone()
|
||||
.unwrap_or_default();
|
||||
|
||||
store.application.set_room_name(room.room_id(), name.as_str());
|
||||
if let Some(alias) = room.canonical_alias() {
|
||||
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||
}
|
||||
|
||||
SpaceItem { room, name }
|
||||
}
|
||||
@@ -1011,7 +1086,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
@@ -1025,11 +1100,12 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
|
||||
#[derive(Clone)]
|
||||
pub struct MemberItem {
|
||||
member: RoomMember,
|
||||
room_id: OwnedRoomId,
|
||||
}
|
||||
|
||||
impl MemberItem {
|
||||
fn new(member: RoomMember) -> Self {
|
||||
Self { member }
|
||||
fn new(member: RoomMember, room_id: OwnedRoomId) -> Self {
|
||||
Self { member, room_id }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1046,12 +1122,32 @@ impl ListItem<IambInfo> for MemberItem {
|
||||
_: &ViewportContext<ListCursor>,
|
||||
store: &mut ProgramStore,
|
||||
) -> Text {
|
||||
let mut user = store.application.settings.get_user_span(self.member.user_id());
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let user_id = self.member.user_id();
|
||||
|
||||
let (color, name) = store.application.settings.get_user_overrides(self.member.user_id());
|
||||
let color = color.unwrap_or_else(|| super::config::user_color(user_id.as_str()));
|
||||
let mut style = super::config::user_style_from_color(color);
|
||||
|
||||
if selected {
|
||||
user.style = user.style.add_modifier(StyleModifier::REVERSED);
|
||||
style = style.add_modifier(StyleModifier::REVERSED);
|
||||
}
|
||||
|
||||
let mut spans = vec![];
|
||||
let mut parens = false;
|
||||
|
||||
if let Some(name) = name {
|
||||
spans.push(Span::styled(name, style));
|
||||
parens = true;
|
||||
} else if let Some(display) = info.display_names.get(user_id) {
|
||||
spans.push(Span::styled(display.clone(), style));
|
||||
parens = true;
|
||||
}
|
||||
|
||||
spans.extend(parens.then_some(Span::styled(" (", style)));
|
||||
spans.push(Span::styled(user_id.as_str(), style));
|
||||
spans.extend(parens.then_some(Span::styled(")", style)));
|
||||
|
||||
let state = match self.member.membership() {
|
||||
MembershipState::Ban => Span::raw(" (banned)").into(),
|
||||
MembershipState::Invite => Span::raw(" (invited)").into(),
|
||||
@@ -1061,11 +1157,9 @@ impl ListItem<IambInfo> for MemberItem {
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(state) = state {
|
||||
Spans(vec![user, state]).into()
|
||||
} else {
|
||||
user.into()
|
||||
}
|
||||
spans.extend(state);
|
||||
|
||||
return Spans(spans).into();
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
@@ -1088,7 +1182,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MemberItem {
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::OsStr;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs;
|
||||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use modalkit::editing::store::RegisterError;
|
||||
use std::process::Command;
|
||||
use tokio;
|
||||
|
||||
use matrix_sdk::{
|
||||
@@ -27,6 +29,7 @@ use matrix_sdk::{
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
input::dialog::PromptYesNo,
|
||||
tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
@@ -40,6 +43,7 @@ use modalkit::{
|
||||
|
||||
use modalkit::editing::{
|
||||
action::{
|
||||
Action,
|
||||
EditError,
|
||||
EditInfo,
|
||||
EditResult,
|
||||
@@ -52,7 +56,8 @@ use modalkit::editing::{
|
||||
Scrollable,
|
||||
UIError,
|
||||
},
|
||||
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle},
|
||||
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle, WriteFlags},
|
||||
completion::CompletionList,
|
||||
context::Resolve,
|
||||
history::{self, HistoryList},
|
||||
rope::EditRope,
|
||||
@@ -74,7 +79,7 @@ use crate::base::{
|
||||
SendAction,
|
||||
};
|
||||
|
||||
use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
|
||||
use crate::message::{text_to_message, Message, MessageEvent, MessageKey, MessageTimeStamp};
|
||||
use crate::worker::Requester;
|
||||
|
||||
use super::scrollback::{Scrollback, ScrollbackState};
|
||||
@@ -154,7 +159,7 @@ impl ChatState {
|
||||
let client = &store.application.worker.client;
|
||||
|
||||
let settings = &store.application.settings;
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
|
||||
let msg = self
|
||||
.scrollback
|
||||
@@ -162,59 +167,67 @@ impl ChatState {
|
||||
.ok_or(IambError::NoSelectedMessage)?;
|
||||
|
||||
match act {
|
||||
MessageAction::Cancel => {
|
||||
MessageAction::Cancel(skip_confirm) => {
|
||||
self.reply_to = None;
|
||||
self.editing = None;
|
||||
|
||||
Ok(None)
|
||||
if skip_confirm {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let msg = "Would you like to clear the message bar?";
|
||||
let act = PromptAction::Abort(false);
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(UIError::NeedConfirm(prompt))
|
||||
},
|
||||
MessageAction::Download(filename, flags) => {
|
||||
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 mut filename = match (filename, &settings.dirs.downloads) {
|
||||
(Some(f), _) => PathBuf::from(f),
|
||||
(None, Some(downloads)) => downloads.clone(),
|
||||
(None, None) => return Err(IambError::NoDownloadDir.into()),
|
||||
};
|
||||
|
||||
let source = match &ev.content.msgtype {
|
||||
MessageType::Audio(c) => {
|
||||
if filename.is_dir() {
|
||||
filename.push(c.body.as_str());
|
||||
}
|
||||
|
||||
c.source.clone()
|
||||
},
|
||||
let (source, msg_filename) = match &ev.content.msgtype {
|
||||
MessageType::Audio(c) => (c.source.clone(), c.body.as_str()),
|
||||
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()
|
||||
(c.source.clone(), c.filename.as_deref().unwrap_or(c.body.as_str()))
|
||||
},
|
||||
MessageType::Image(c) => (c.source.clone(), c.body.as_str()),
|
||||
MessageType::Video(c) => (c.source.clone(), c.body.as_str()),
|
||||
_ => {
|
||||
return Err(IambError::NoAttachment.into());
|
||||
},
|
||||
};
|
||||
|
||||
if filename.is_dir() {
|
||||
filename.push(msg_filename);
|
||||
}
|
||||
|
||||
if filename.exists() && !flags.contains(DownloadFlags::FORCE) {
|
||||
// Find an incrementally suffixed filename, e.g. image-2.jpg -> image-3.jpg
|
||||
if let Some(stem) = filename.file_stem().and_then(OsStr::to_str) {
|
||||
let ext = filename.extension();
|
||||
let mut filename_incr = filename.clone();
|
||||
for n in 1..=1000 {
|
||||
if let Some(ext) = ext.and_then(OsStr::to_str) {
|
||||
filename_incr.set_file_name(format!("{}-{}.{}", stem, n, ext));
|
||||
} else {
|
||||
filename_incr.set_file_name(format!("{}-{}", stem, n));
|
||||
}
|
||||
|
||||
if !filename_incr.exists() {
|
||||
filename = filename_incr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !filename.exists() || flags.contains(DownloadFlags::FORCE) {
|
||||
let req = MediaRequest { source, format: MediaFormat::File };
|
||||
|
||||
@@ -235,14 +248,21 @@ impl ChatState {
|
||||
}
|
||||
|
||||
let info = if flags.contains(DownloadFlags::OPEN) {
|
||||
// open::that may not return until the spawned program closes.
|
||||
let target = filename.clone().into_os_string();
|
||||
tokio::task::spawn_blocking(move || open::that(target));
|
||||
|
||||
InfoMessage::from(format!(
|
||||
"Attachment downloaded to {} and opened",
|
||||
filename.display()
|
||||
))
|
||||
match open_command(
|
||||
store.application.settings.tunables.open_command.as_ref(),
|
||||
target,
|
||||
) {
|
||||
Ok(_) => {
|
||||
InfoMessage::from(format!(
|
||||
"Attachment downloaded to {} and opened",
|
||||
filename.display()
|
||||
))
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
InfoMessage::from(format!(
|
||||
"Attachment downloaded to {}",
|
||||
@@ -285,6 +305,7 @@ impl ChatState {
|
||||
};
|
||||
|
||||
self.tbox.set_text(text);
|
||||
self.reply_to = msg.reply_to().and_then(|id| info.get_message_key(&id)).cloned();
|
||||
self.editing = self.scrollback.get_key(info);
|
||||
self.focus = RoomFocus::MessageBar;
|
||||
|
||||
@@ -293,6 +314,8 @@ impl ChatState {
|
||||
MessageAction::React(emoji) => {
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
@@ -309,9 +332,20 @@ impl ChatState {
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::Redact(reason) => {
|
||||
MessageAction::Redact(reason, skip_confirm) => {
|
||||
if !skip_confirm {
|
||||
let msg = "Are you sure you want to redact this message?";
|
||||
let act = IambAction::Message(MessageAction::Redact(reason, true));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
return Err(UIError::NeedConfirm(prompt));
|
||||
}
|
||||
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
@@ -337,6 +371,8 @@ impl ChatState {
|
||||
MessageAction::Unreact(emoji) => {
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id: &EventId = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::Original(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::Local(event_id, _) => event_id.as_ref(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
@@ -389,21 +425,18 @@ impl ChatState {
|
||||
.client
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(IambError::NotJoined)?;
|
||||
let info = store.application.rooms.entry(self.id().to_owned()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.id().to_owned());
|
||||
let mut show_echo = true;
|
||||
|
||||
let (event_id, msg) = match act {
|
||||
SendAction::Submit => {
|
||||
let msg = self.tbox.get_text();
|
||||
let msg = self.tbox.get();
|
||||
|
||||
if msg.is_empty() {
|
||||
if msg.is_blank() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let msg = TextMessageEventContent::markdown(msg);
|
||||
let msg = MessageType::Text(msg);
|
||||
|
||||
let mut msg = RoomMessageEventContent::new(msg);
|
||||
let mut msg = text_to_message(msg.trim_end().to_string());
|
||||
|
||||
if let Some((_, event_id)) = &self.editing {
|
||||
msg.relates_to = Some(Relation::Replacement(Replacement::new(
|
||||
@@ -448,6 +481,36 @@ impl ChatState {
|
||||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
(resp.event_id, msg)
|
||||
},
|
||||
SendAction::UploadImage(width, height, bytes) => {
|
||||
// Convert to png because arboard does not give us the mime type.
|
||||
let bytes =
|
||||
image::ImageBuffer::from_raw(width as _, height as _, bytes.into_owned())
|
||||
.ok_or(IambError::Clipboard)
|
||||
.and_then(|imagebuf| {
|
||||
let dynimage = image::DynamicImage::ImageRgba8(imagebuf);
|
||||
let bytes = Vec::<u8>::new();
|
||||
let mut buff = std::io::Cursor::new(bytes);
|
||||
dynimage.write_to(&mut buff, image::ImageOutputFormat::Png)?;
|
||||
Ok(buff.into_inner())
|
||||
})
|
||||
.map_err(IambError::from)?;
|
||||
let mime = mime::IMAGE_PNG;
|
||||
|
||||
let name = "Clipboard.png";
|
||||
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)
|
||||
},
|
||||
};
|
||||
@@ -550,6 +613,21 @@ impl WindowOps<IambInfo> for ChatState {
|
||||
true
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
_: Option<&str>,
|
||||
_: WriteFlags,
|
||||
_: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
// XXX: what's the right writing behaviour for a room?
|
||||
// Should write send a message?
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
delegate!(self, w => w.get_completions())
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
delegate!(self, w => w.get_cursor_word(style))
|
||||
}
|
||||
@@ -579,6 +657,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
// Run command again.
|
||||
delegate!(self, w => w.editor_command(act, ctx, store))
|
||||
},
|
||||
Err(EditError::Register(RegisterError::ClipboardImage(data))) => {
|
||||
let msg = "Do you really want to upload the image from your system clipboard?";
|
||||
let send =
|
||||
IambAction::Send(SendAction::UploadImage(data.width, data.height, data.bytes));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(send)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(EditError::NeedConfirm(prompt))
|
||||
},
|
||||
res @ Err(_) => res,
|
||||
}
|
||||
}
|
||||
@@ -655,13 +742,14 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
&mut self,
|
||||
dir: &MoveDir1D,
|
||||
count: &Count,
|
||||
prefixed: bool,
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
let count = ctx.resolve(count);
|
||||
let rope = self.tbox.get();
|
||||
|
||||
let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, count);
|
||||
let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, prefixed, count);
|
||||
|
||||
if let Some(text) = text {
|
||||
self.tbox.set_text(text);
|
||||
@@ -685,7 +773,9 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
match act {
|
||||
PromptAction::Submit => self.submit(ctx, store),
|
||||
PromptAction::Abort(empty) => self.abort(*empty, ctx, store),
|
||||
PromptAction::Recall(dir, count) => self.recall(dir, count, ctx, store),
|
||||
PromptAction::Recall(dir, count, prefixed) => {
|
||||
self.recall(dir, count, *prefixed, ctx, store)
|
||||
},
|
||||
_ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
|
||||
}
|
||||
}
|
||||
@@ -711,10 +801,33 @@ impl<'a> StatefulWidget for Chat<'a> {
|
||||
type State = ChatState;
|
||||
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||
// Determine whether we have a description to show for the message bar.
|
||||
let desc_spans = match (&state.editing, &state.reply_to) {
|
||||
(None, None) => None,
|
||||
(Some(_), None) => Some(Spans::from("Editing message")),
|
||||
(editing, 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(), room);
|
||||
let prefix = if editing.is_some() {
|
||||
Span::from("Editing reply to ")
|
||||
} else {
|
||||
Span::from("Replying to ")
|
||||
};
|
||||
let spans = Spans(vec![prefix, user]);
|
||||
|
||||
spans.into()
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
// Determine the region to show each UI element.
|
||||
let lines = state.tbox.has_lines(5).max(1) as u16;
|
||||
let drawh = area.height;
|
||||
let texth = lines.min(drawh).clamp(1, 5);
|
||||
let desch = if state.reply_to.is_some() {
|
||||
let desch = if desc_spans.is_some() {
|
||||
drawh.saturating_sub(texth).min(1)
|
||||
} else {
|
||||
0
|
||||
@@ -725,25 +838,7 @@ impl<'a> StatefulWidget for Chat<'a> {
|
||||
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 = Scrollback::new(self.store).focus(scrollback_focused);
|
||||
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()
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
// Render the message bar and any description for it.
|
||||
if let Some(desc_spans) = desc_spans {
|
||||
Paragraph::new(desc_spans).render(descarea, buf);
|
||||
}
|
||||
@@ -752,5 +847,35 @@ impl<'a> StatefulWidget for Chat<'a> {
|
||||
|
||||
let tbox = TextBox::new().prompt(prompt);
|
||||
tbox.render(textarea, buf, &mut state.tbox);
|
||||
|
||||
// Render the message scrollback.
|
||||
let scrollback_focused = state.focus.is_scrollback() && self.focused;
|
||||
let scrollback = Scrollback::new(self.store)
|
||||
.focus(scrollback_focused)
|
||||
.room_focus(self.focused);
|
||||
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
||||
}
|
||||
}
|
||||
|
||||
fn open_command(open_command: Option<&Vec<String>>, target: OsString) -> IambResult<()> {
|
||||
if let Some(mut cmd) = open_command.and_then(cmd) {
|
||||
cmd.arg(target);
|
||||
cmd.spawn()?;
|
||||
return Ok(());
|
||||
} else {
|
||||
// open::that may not return until the spawned program closes.
|
||||
tokio::task::spawn_blocking(move || {
|
||||
return open::that(target);
|
||||
});
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd(open_command: &Vec<String>) -> Option<Command> {
|
||||
if let [program, args @ ..] = open_command.as_slice() {
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.args(args);
|
||||
return Some(cmd);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
@@ -40,12 +40,16 @@ use modalkit::{
|
||||
PositionList,
|
||||
ScrollStyle,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
editing::completion::CompletionList,
|
||||
input::dialog::PromptYesNo,
|
||||
input::InputContext,
|
||||
widgets::{TermOffset, TerminalCursor, WindowOps},
|
||||
};
|
||||
|
||||
use crate::base::{
|
||||
IambAction,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
@@ -135,8 +139,9 @@ impl RoomState {
|
||||
let mut invited = vec![Span::from(format!("You have been invited to join {name}"))];
|
||||
|
||||
if let Ok(Some(inviter)) = &inviter {
|
||||
let info = store.application.rooms.get_or_default(self.id().to_owned());
|
||||
invited.push(Span::from(" by "));
|
||||
invited.push(store.application.settings.get_user_span(inviter.user_id()));
|
||||
invited.push(store.application.settings.get_user_span(inviter.user_id(), info));
|
||||
}
|
||||
|
||||
let l1 = Spans(invited);
|
||||
@@ -183,8 +188,16 @@ impl RoomState {
|
||||
match act {
|
||||
RoomAction::InviteAccept => {
|
||||
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
|
||||
let details = room.invite_details().await.map_err(IambError::from)?;
|
||||
let details = details.invitee.event().original_content();
|
||||
let is_direct = details.and_then(|ev| ev.is_direct).unwrap_or_default();
|
||||
|
||||
room.accept_invitation().await.map_err(IambError::from)?;
|
||||
|
||||
if is_direct {
|
||||
room.set_is_direct(true).await.map_err(IambError::from)?;
|
||||
}
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
Err(IambError::NotInvited.into())
|
||||
@@ -208,6 +221,24 @@ impl RoomState {
|
||||
Err(IambError::NotJoined.into())
|
||||
}
|
||||
},
|
||||
RoomAction::Leave(skip_confirm) => {
|
||||
if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
|
||||
if skip_confirm {
|
||||
room.leave().await.map_err(IambError::from)?;
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
let msg = "Do you really want to leave this room?";
|
||||
let leave = IambAction::Room(RoomAction::Leave(true));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(leave)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(UIError::NeedConfirm(prompt))
|
||||
}
|
||||
} else {
|
||||
Err(IambError::NotJoined.into())
|
||||
}
|
||||
},
|
||||
RoomAction::Members(mut cmd) => {
|
||||
let width = Count::Exact(30);
|
||||
let act =
|
||||
@@ -383,10 +414,30 @@ impl WindowOps<IambInfo> for RoomState {
|
||||
}
|
||||
}
|
||||
|
||||
fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
|
||||
// XXX: what's the right closing behaviour for a room?
|
||||
// Should write send a message?
|
||||
true
|
||||
fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.close(flags, store),
|
||||
RoomState::Space(space) => space.close(flags, store),
|
||||
}
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.write(path, flags, store),
|
||||
RoomState::Space(space) => space.write(path, flags, store),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.get_completions(),
|
||||
RoomState::Space(space) => space.get_completions(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
|
||||
@@ -4,7 +4,13 @@ use regex::Regex;
|
||||
|
||||
use matrix_sdk::ruma::OwnedRoomId;
|
||||
|
||||
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
|
||||
use modalkit::tui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Rect},
|
||||
style::{Modifier as StyleModifier, Style},
|
||||
text::{Span, Spans},
|
||||
widgets::{Paragraph, StatefulWidget, Widget},
|
||||
};
|
||||
use modalkit::widgets::{ScrollActions, TerminalCursor, WindowOps};
|
||||
|
||||
use modalkit::editing::{
|
||||
@@ -32,6 +38,9 @@ use modalkit::editing::{
|
||||
base::{
|
||||
Axis,
|
||||
CloseFlags,
|
||||
CompletionDisplay,
|
||||
CompletionSelection,
|
||||
CompletionType,
|
||||
Count,
|
||||
EditRange,
|
||||
EditTarget,
|
||||
@@ -51,7 +60,9 @@ use modalkit::editing::{
|
||||
TargetShape,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
context::{EditContext, Resolve},
|
||||
cursor::{CursorGroup, CursorState},
|
||||
history::HistoryList,
|
||||
@@ -60,7 +71,7 @@ use modalkit::editing::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
base::{IambBufferId, IambInfo, IambResult, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
config::ApplicationSettings,
|
||||
message::{Message, MessageCursor, MessageKey, Messages},
|
||||
};
|
||||
@@ -515,6 +526,23 @@ impl WindowOps<IambInfo> for ScrollbackState {
|
||||
true
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
_: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
_: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
if flags.contains(WriteFlags::FORCE) {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(EditError::ReadOnly.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
None
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, _: &WordStyle) -> Option<String> {
|
||||
None
|
||||
}
|
||||
@@ -532,7 +560,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
ctx: &ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let key = if let Some(k) = self.cursor.to_key(info) {
|
||||
k.clone()
|
||||
} else {
|
||||
@@ -762,6 +790,17 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn complete(
|
||||
&mut self,
|
||||
_: &CompletionType,
|
||||
_: &CompletionSelection,
|
||||
_: &CompletionDisplay,
|
||||
_: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
Err(EditError::ReadOnly)
|
||||
}
|
||||
|
||||
fn insert_text(
|
||||
&mut self,
|
||||
_: &InsertTextAction,
|
||||
@@ -867,9 +906,9 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
EditorAction::Mark(name) => self.mark(ctx.resolve(name), ctx, store),
|
||||
EditorAction::Selection(act) => self.selection_command(act, ctx, store),
|
||||
|
||||
EditorAction::Complete(_, _) => {
|
||||
let msg = "";
|
||||
let err = EditError::Unimplemented(msg.into());
|
||||
EditorAction::Complete(_, _, _) => {
|
||||
let msg = "Nothing to complete in message scrollback";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
Err(err)
|
||||
},
|
||||
@@ -991,7 +1030,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
ctx: &ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let settings = &store.application.settings;
|
||||
let mut corner = self.viewctx.corner.clone();
|
||||
|
||||
@@ -1105,7 +1144,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
Err(err)
|
||||
},
|
||||
Axis::Vertical => {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let settings = &store.application.settings;
|
||||
|
||||
if let Some(key) = self.cursor.to_key(info).cloned() {
|
||||
@@ -1172,14 +1211,43 @@ impl TerminalCursor for ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn render_jump_to_recent(area: Rect, buf: &mut Buffer, focused: bool) -> Rect {
|
||||
if area.height <= 5 || area.width <= 20 {
|
||||
return area;
|
||||
}
|
||||
|
||||
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 msg = vec![
|
||||
Span::raw("Use "),
|
||||
Span::styled("G", Style::default().add_modifier(StyleModifier::BOLD)),
|
||||
Span::raw(if focused { "" } else { " in scrollback" }),
|
||||
Span::raw(" to jump to latest message"),
|
||||
];
|
||||
|
||||
Paragraph::new(Spans::from(msg))
|
||||
.alignment(Alignment::Center)
|
||||
.render(bar, buf);
|
||||
|
||||
return top;
|
||||
}
|
||||
|
||||
pub struct Scrollback<'a> {
|
||||
room_focused: bool,
|
||||
focused: bool,
|
||||
store: &'a mut ProgramStore,
|
||||
}
|
||||
|
||||
impl<'a> Scrollback<'a> {
|
||||
pub fn new(store: &'a mut ProgramStore) -> Self {
|
||||
Scrollback { focused: false, store }
|
||||
Scrollback { room_focused: false, focused: false, store }
|
||||
}
|
||||
|
||||
/// Indicate whether the room window is currently focused, regardless of whether the scrollback
|
||||
/// also is.
|
||||
pub fn room_focus(mut self, focused: bool) -> Self {
|
||||
self.room_focused = focused;
|
||||
self
|
||||
}
|
||||
|
||||
/// Indicate whether the scrollback is currently focused.
|
||||
@@ -1193,9 +1261,13 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
||||
type State = ScrollbackState;
|
||||
|
||||
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.get_or_default(state.room_id.clone());
|
||||
let settings = &self.store.application.settings;
|
||||
let area = info.render_typing(area, buf, &self.store.application.settings);
|
||||
let area = if state.cursor.timestamp.is_some() {
|
||||
render_jump_to_recent(area, buf, self.focused)
|
||||
} else {
|
||||
info.render_typing(area, buf, &self.store.application.settings)
|
||||
};
|
||||
|
||||
state.set_term_info(area);
|
||||
|
||||
@@ -1274,7 +1346,10 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
||||
y += 1;
|
||||
}
|
||||
|
||||
if settings.tunables.read_receipt_send && state.cursor.timestamp.is_none() {
|
||||
if self.room_focused &&
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use matrix_sdk::{
|
||||
room::Room as MatrixRoom,
|
||||
ruma::{OwnedRoomId, RoomId},
|
||||
};
|
||||
|
||||
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
|
||||
use modalkit::tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
style::{Color, Style},
|
||||
text::{Span, Spans, Text},
|
||||
widgets::StatefulWidget,
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
widgets::list::{List, ListState},
|
||||
@@ -16,10 +23,13 @@ use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus};
|
||||
|
||||
use crate::windows::RoomItem;
|
||||
|
||||
const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
|
||||
|
||||
pub struct SpaceState {
|
||||
room_id: OwnedRoomId,
|
||||
room: MatrixRoom,
|
||||
list: ListState<RoomItem, IambInfo>,
|
||||
last_fetch: Option<Instant>,
|
||||
}
|
||||
|
||||
impl SpaceState {
|
||||
@@ -27,8 +37,9 @@ impl SpaceState {
|
||||
let room_id = room.room_id().to_owned();
|
||||
let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback);
|
||||
let list = ListState::new(content, vec![]);
|
||||
let last_fetch = None;
|
||||
|
||||
SpaceState { room_id, room, list }
|
||||
SpaceState { room_id, room, list, last_fetch }
|
||||
}
|
||||
|
||||
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
|
||||
@@ -50,6 +61,7 @@ impl SpaceState {
|
||||
room_id: self.room_id.clone(),
|
||||
room: self.room.clone(),
|
||||
list: self.list.dup(store),
|
||||
last_fetch: self.last_fetch,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,30 +106,52 @@ impl<'a> StatefulWidget for Space<'a> {
|
||||
type State = SpaceState;
|
||||
|
||||
fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
|
||||
let members =
|
||||
if let Ok(m) = self.store.application.worker.space_members(state.room_id.clone()) {
|
||||
m
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let mut empty_message = None;
|
||||
let need_fetch = match state.last_fetch {
|
||||
Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE,
|
||||
None => true,
|
||||
};
|
||||
|
||||
let items = members
|
||||
.into_iter()
|
||||
.filter_map(|id| {
|
||||
let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?;
|
||||
if need_fetch {
|
||||
let res = self.store.application.worker.space_members(state.room_id.clone());
|
||||
|
||||
if id != state.room_id {
|
||||
Some(RoomItem::new(room, name, tags, self.store))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
match res {
|
||||
Ok(members) => {
|
||||
let items = members
|
||||
.into_iter()
|
||||
.filter_map(|id| {
|
||||
let (room, _, tags) =
|
||||
self.store.application.worker.get_room(id.clone()).ok()?;
|
||||
let room_info = std::sync::Arc::new((room, tags));
|
||||
|
||||
state.list.set(items);
|
||||
if id != state.room_id {
|
||||
Some(RoomItem::new(room_info, self.store))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
List::new(self.store)
|
||||
.focus(self.focused)
|
||||
.render(area, buffer, &mut state.list)
|
||||
state.list.set(items);
|
||||
state.last_fetch = Some(Instant::now());
|
||||
},
|
||||
Err(e) => {
|
||||
let lines = vec![
|
||||
Spans::from("Unable to fetch space room hierarchy:"),
|
||||
Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(),
|
||||
];
|
||||
|
||||
empty_message = Text { lines }.into();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let mut list = List::new(self.store).focus(self.focused);
|
||||
|
||||
if let Some(text) = empty_message {
|
||||
list = list.empty_message(text);
|
||||
}
|
||||
|
||||
list.render(area, buffer, &mut state.list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ use modalkit::{
|
||||
widgets::{TermOffset, TerminalCursor},
|
||||
};
|
||||
|
||||
use modalkit::editing::base::{CloseFlags, WordStyle};
|
||||
use modalkit::editing::action::EditInfo;
|
||||
use modalkit::editing::base::{CloseFlags, WordStyle, WriteFlags};
|
||||
use modalkit::editing::completion::CompletionList;
|
||||
|
||||
use crate::base::{IambBufferId, IambInfo, ProgramStore};
|
||||
use crate::base::{IambBufferId, IambInfo, IambResult, ProgramStore};
|
||||
|
||||
const WELCOME_TEXT: &str = include_str!("welcome.md");
|
||||
|
||||
@@ -63,6 +65,19 @@ impl WindowOps<IambInfo> for WelcomeState {
|
||||
self.tbox.close(flags, store)
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
self.tbox.write(path, flags, store)
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
self.tbox.get_completions()
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
self.tbox.get_cursor_word(style)
|
||||
}
|
||||
|
||||
676
src/worker.rs
676
src/worker.rs
@@ -1,3 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::fs::File;
|
||||
@@ -5,25 +6,27 @@ use std::io::BufWriter;
|
||||
use std::str::FromStr;
|
||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use futures::{stream::FuturesUnordered, StreamExt};
|
||||
use gethostname::gethostname;
|
||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::error;
|
||||
use tracing::{error, warn};
|
||||
|
||||
use matrix_sdk::{
|
||||
config::{RequestConfig, StoreConfig, SyncSettings},
|
||||
config::{RequestConfig, SyncSettings},
|
||||
encryption::verification::{SasVerification, Verification},
|
||||
event_handler::Ctx,
|
||||
reqwest,
|
||||
room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
|
||||
ruma::{
|
||||
api::client::{
|
||||
room::create_room::v3::{Request as CreateRoomRequest, RoomPreset},
|
||||
room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
|
||||
room::Visibility,
|
||||
space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
|
||||
},
|
||||
assign,
|
||||
events::{
|
||||
key::verification::{
|
||||
done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
|
||||
@@ -32,21 +35,33 @@ use matrix_sdk::{
|
||||
start::{OriginalSyncKeyVerificationStartEvent, ToDeviceKeyVerificationStartEvent},
|
||||
VerificationMethod,
|
||||
},
|
||||
presence::PresenceEvent,
|
||||
reaction::ReactionEventContent,
|
||||
room::{
|
||||
encryption::RoomEncryptionEventContent,
|
||||
member::OriginalSyncRoomMemberEvent,
|
||||
message::{MessageType, RoomMessageEventContent},
|
||||
name::RoomNameEventContent,
|
||||
redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
|
||||
},
|
||||
tag::Tags,
|
||||
typing::SyncTypingEvent,
|
||||
AnyInitialStateEvent,
|
||||
AnyMessageLikeEvent,
|
||||
AnyTimelineEvent,
|
||||
EmptyStateKey,
|
||||
InitialStateEvent,
|
||||
SyncMessageLikeEvent,
|
||||
SyncStateEvent,
|
||||
},
|
||||
room::RoomType,
|
||||
serde::Raw,
|
||||
EventEncryptionAlgorithm,
|
||||
OwnedEventId,
|
||||
OwnedRoomId,
|
||||
OwnedRoomOrAliasId,
|
||||
OwnedUserId,
|
||||
RoomId,
|
||||
RoomVersionId,
|
||||
},
|
||||
Client,
|
||||
@@ -57,19 +72,314 @@ use matrix_sdk::{
|
||||
use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
|
||||
|
||||
use crate::{
|
||||
base::{AsyncProgramStore, EventLocation, IambError, IambResult, Receipts, VerifyAction},
|
||||
base::{
|
||||
AsyncProgramStore,
|
||||
ChatStore,
|
||||
CreateRoomFlags,
|
||||
CreateRoomType,
|
||||
EventLocation,
|
||||
IambError,
|
||||
IambResult,
|
||||
Receipts,
|
||||
RoomFetchStatus,
|
||||
VerifyAction,
|
||||
},
|
||||
message::MessageFetchResult,
|
||||
ApplicationSettings,
|
||||
};
|
||||
|
||||
const IAMB_DEVICE_NAME: &str = "iamb";
|
||||
const IAMB_USER_AGENT: &str = "iamb";
|
||||
const REQ_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
const MIN_MSG_LOAD: u32 = 50;
|
||||
|
||||
fn initial_devname() -> String {
|
||||
format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
|
||||
}
|
||||
|
||||
pub async fn create_room(
|
||||
client: &Client,
|
||||
room_alias_name: Option<&str>,
|
||||
rt: CreateRoomType,
|
||||
flags: CreateRoomFlags,
|
||||
) -> IambResult<OwnedRoomId> {
|
||||
let mut creation_content = None;
|
||||
let mut initial_state = vec![];
|
||||
let mut is_direct = false;
|
||||
let mut preset = None;
|
||||
let mut invite = vec![];
|
||||
|
||||
let visibility = if flags.contains(CreateRoomFlags::PUBLIC) {
|
||||
Visibility::Public
|
||||
} else {
|
||||
Visibility::Private
|
||||
};
|
||||
|
||||
match rt {
|
||||
CreateRoomType::Direct(user) => {
|
||||
invite.push(user);
|
||||
is_direct = true;
|
||||
preset = Some(RoomPreset::TrustedPrivateChat);
|
||||
},
|
||||
CreateRoomType::Space => {
|
||||
let mut cc = CreationContent::new();
|
||||
cc.room_type = Some(RoomType::Space);
|
||||
|
||||
let raw_cc = Raw::new(&cc).map_err(IambError::from)?;
|
||||
creation_content = Some(raw_cc);
|
||||
},
|
||||
CreateRoomType::Room => {},
|
||||
}
|
||||
|
||||
// Set up encryption.
|
||||
if flags.contains(CreateRoomFlags::ENCRYPTED) {
|
||||
// XXX: Once matrix-sdk uses ruma 0.8, then this can skip the cast.
|
||||
let algo = EventEncryptionAlgorithm::MegolmV1AesSha2;
|
||||
let content = RoomEncryptionEventContent::new(algo);
|
||||
let encr = InitialStateEvent { content, state_key: EmptyStateKey };
|
||||
let encr_raw = Raw::new(&encr).map_err(IambError::from)?;
|
||||
let encr_raw = encr_raw.cast::<AnyInitialStateEvent>();
|
||||
initial_state.push(encr_raw);
|
||||
}
|
||||
|
||||
let request = assign!(CreateRoomRequest::new(), {
|
||||
room_alias_name,
|
||||
creation_content,
|
||||
initial_state: initial_state.as_slice(),
|
||||
invite: invite.as_slice(),
|
||||
is_direct,
|
||||
visibility,
|
||||
preset,
|
||||
});
|
||||
|
||||
let resp = client.create_room(request).await.map_err(IambError::from)?;
|
||||
|
||||
if is_direct {
|
||||
if let Some(room) = client.get_room(&resp.room_id) {
|
||||
room.set_is_direct(true).await.map_err(IambError::from)?;
|
||||
} else {
|
||||
error!(
|
||||
room_id = resp.room_id.as_str(),
|
||||
"Couldn't set is_direct for new direct message room"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(resp.room_id);
|
||||
}
|
||||
|
||||
async fn load_plan(store: &AsyncProgramStore) -> HashMap<OwnedRoomId, Option<String>> {
|
||||
let mut locked = store.lock().await;
|
||||
let ChatStore { need_load, rooms, .. } = &mut locked.application;
|
||||
let mut plan = HashMap::new();
|
||||
|
||||
for room_id in std::mem::take(need_load).into_iter() {
|
||||
let info = rooms.get_or_default(room_id.clone());
|
||||
|
||||
if info.recently_fetched() || info.fetching {
|
||||
need_load.insert(room_id);
|
||||
continue;
|
||||
} else {
|
||||
info.fetch_last = Instant::now().into();
|
||||
info.fetching = true;
|
||||
}
|
||||
|
||||
let fetch_id = match &info.fetch_id {
|
||||
RoomFetchStatus::Done => continue,
|
||||
RoomFetchStatus::HaveMore(fetch_id) => Some(fetch_id.clone()),
|
||||
RoomFetchStatus::NotStarted => None,
|
||||
};
|
||||
|
||||
plan.insert(room_id, fetch_id);
|
||||
}
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
async fn load_older_one(
|
||||
client: Client,
|
||||
room_id: &RoomId,
|
||||
fetch_id: Option<String>,
|
||||
limit: u32,
|
||||
) -> MessageFetchResult {
|
||||
if let Some(room) = client.get_room(room_id) {
|
||||
let mut opts = match &fetch_id {
|
||||
Some(id) => MessagesOptions::backward().from(id.as_str()),
|
||||
None => MessagesOptions::backward(),
|
||||
};
|
||||
opts.limit = limit.into();
|
||||
|
||||
let Messages { end, chunk, .. } = room.messages(opts).await.map_err(IambError::from)?;
|
||||
|
||||
let msgs = chunk.into_iter().filter_map(|ev| {
|
||||
match ev.event.deserialize() {
|
||||
Ok(AnyTimelineEvent::MessageLike(msg)) => Some(msg),
|
||||
Ok(AnyTimelineEvent::State(_)) => None,
|
||||
Err(_) => None,
|
||||
}
|
||||
});
|
||||
|
||||
Ok((end, msgs.collect()))
|
||||
} else {
|
||||
Err(IambError::UnknownRoom(room_id.to_owned()).into())
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_insert(room_id: OwnedRoomId, res: MessageFetchResult, store: AsyncProgramStore) {
|
||||
let mut locked = store.lock().await;
|
||||
let ChatStore { need_load, presences, rooms, .. } = &mut locked.application;
|
||||
let info = rooms.get_or_default(room_id.clone());
|
||||
info.fetching = false;
|
||||
|
||||
match res {
|
||||
Ok((fetch_id, msgs)) => {
|
||||
for msg in msgs.into_iter() {
|
||||
let sender = msg.sender().to_owned();
|
||||
let _ = presences.get_or_default(sender);
|
||||
|
||||
match msg {
|
||||
AnyMessageLikeEvent::RoomEncrypted(msg) => {
|
||||
info.insert_encrypted(msg);
|
||||
},
|
||||
AnyMessageLikeEvent::RoomMessage(msg) => {
|
||||
info.insert(msg);
|
||||
},
|
||||
AnyMessageLikeEvent::Reaction(ev) => {
|
||||
info.insert_reaction(ev);
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
info.fetch_id = fetch_id.map_or(RoomFetchStatus::Done, RoomFetchStatus::HaveMore);
|
||||
},
|
||||
Err(e) => {
|
||||
warn!(room_id = room_id.as_str(), err = e.to_string(), "Failed to load older messages");
|
||||
|
||||
// Wait and try again.
|
||||
need_load.insert(room_id);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_older(client: &Client, store: &AsyncProgramStore) -> usize {
|
||||
let limit = MIN_MSG_LOAD;
|
||||
|
||||
// Fetch each room separately, so they don't block each other.
|
||||
load_plan(store)
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|(room_id, fetch_id)| {
|
||||
let client = client.clone();
|
||||
let store = store.clone();
|
||||
|
||||
async move {
|
||||
let res = load_older_one(client, room_id.as_ref(), fetch_id, limit).await;
|
||||
load_insert(room_id, res, store).await;
|
||||
}
|
||||
})
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.count()
|
||||
.await
|
||||
}
|
||||
|
||||
async fn load_older_forever(client: &Client, store: &AsyncProgramStore) {
|
||||
// Load older messages every 2 seconds.
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(2));
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
load_older(client, store).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn refresh_rooms(client: &Client, store: &AsyncProgramStore) {
|
||||
let mut names = vec![];
|
||||
|
||||
let mut spaces = vec![];
|
||||
let mut rooms = vec![];
|
||||
let mut dms = vec![];
|
||||
|
||||
for room in client.invited_rooms().into_iter() {
|
||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string();
|
||||
names.push((room.room_id().to_owned(), name));
|
||||
|
||||
if room.is_direct() {
|
||||
let tags = room.tags().await.unwrap_or_default();
|
||||
|
||||
dms.push(Arc::new((room.into(), tags)));
|
||||
} else if room.is_space() {
|
||||
spaces.push(room.into());
|
||||
} else {
|
||||
let tags = room.tags().await.unwrap_or_default();
|
||||
|
||||
rooms.push(Arc::new((room.into(), tags)));
|
||||
}
|
||||
}
|
||||
|
||||
for room in client.joined_rooms().into_iter() {
|
||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string();
|
||||
names.push((room.room_id().to_owned(), name));
|
||||
|
||||
if room.is_direct() {
|
||||
let tags = room.tags().await.unwrap_or_default();
|
||||
|
||||
dms.push(Arc::new((room.into(), tags)));
|
||||
} else if room.is_space() {
|
||||
spaces.push(room.into());
|
||||
} else {
|
||||
let tags = room.tags().await.unwrap_or_default();
|
||||
|
||||
rooms.push(Arc::new((room.into(), tags)));
|
||||
}
|
||||
}
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
locked.application.sync_info.spaces = spaces;
|
||||
locked.application.sync_info.rooms = rooms;
|
||||
locked.application.sync_info.dms = dms;
|
||||
|
||||
for (room_id, name) in names {
|
||||
locked.application.set_room_name(&room_id, &name);
|
||||
}
|
||||
}
|
||||
|
||||
async fn refresh_rooms_forever(client: &Client, store: &AsyncProgramStore) {
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(5));
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
refresh_rooms(client, store).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn refresh_receipts_forever(client: &Client, store: &AsyncProgramStore) {
|
||||
// Update the displayed read receipts every 5 seconds.
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(5));
|
||||
let mut sent = HashMap::<OwnedRoomId, OwnedEventId>::default();
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let receipts = update_receipts(client).await;
|
||||
let read = store.lock().await.application.set_receipts(receipts).await;
|
||||
|
||||
for (room_id, read_till) in read.into_iter() {
|
||||
if let Some(read_sent) = sent.get(&room_id) {
|
||||
if read_sent == &read_till {
|
||||
// Skip unchanged receipts.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(room) = client.get_joined_room(&room_id) {
|
||||
if room.read_receipt(&read_till).await.is_ok() {
|
||||
sent.insert(room_id, read_till);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LoginStyle {
|
||||
SessionRestore(Session),
|
||||
@@ -125,17 +435,13 @@ async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> {
|
||||
pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
|
||||
|
||||
pub enum WorkerTask {
|
||||
ActiveRooms(ClientReply<Vec<FetchedRoom>>),
|
||||
DirectMessages(ClientReply<Vec<FetchedRoom>>),
|
||||
Init(AsyncProgramStore, ClientReply<()>),
|
||||
LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>),
|
||||
Login(LoginStyle, ClientReply<IambResult<EditInfo>>),
|
||||
GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>),
|
||||
GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>),
|
||||
JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>),
|
||||
Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
|
||||
SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
|
||||
Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
|
||||
TypingNotice(OwnedRoomId),
|
||||
Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>),
|
||||
VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>),
|
||||
@@ -144,28 +450,12 @@ pub enum WorkerTask {
|
||||
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)
|
||||
@@ -199,9 +489,6 @@ impl Debug for WorkerTask {
|
||||
.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()
|
||||
},
|
||||
@@ -237,21 +524,6 @@ impl Requester {
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn load_older(
|
||||
&self,
|
||||
room_id: OwnedRoomId,
|
||||
fetch_id: Option<String>,
|
||||
limit: u32,
|
||||
) -> MessageFetchResult {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
self.tx
|
||||
.send(WorkerTask::LoadOlder(room_id, fetch_id, limit, reply))
|
||||
.unwrap();
|
||||
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
@@ -260,14 +532,6 @@ impl Requester {
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn direct_messages(&self) -> Vec<FetchedRoom> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
self.tx.send(WorkerTask::DirectMessages(reply)).unwrap();
|
||||
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn get_inviter(&self, invite: Invited) -> IambResult<Option<RoomMember>> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
@@ -292,14 +556,6 @@ impl Requester {
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn active_rooms(&self) -> Vec<FetchedRoom> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap();
|
||||
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
@@ -316,14 +572,6 @@ impl Requester {
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
||||
let (reply, response) = oneshot();
|
||||
|
||||
self.tx.send(WorkerTask::Spaces(reply)).unwrap();
|
||||
|
||||
return response.recv();
|
||||
}
|
||||
|
||||
pub fn typing_notice(&self, room_id: OwnedRoomId) {
|
||||
self.tx.send(WorkerTask::TypingNotice(room_id)).unwrap();
|
||||
}
|
||||
@@ -349,8 +597,8 @@ pub struct ClientWorker {
|
||||
initialized: bool,
|
||||
settings: ApplicationSettings,
|
||||
client: Client,
|
||||
load_handle: Option<JoinHandle<()>>,
|
||||
sync_handle: Option<JoinHandle<()>>,
|
||||
rcpt_handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl ClientWorker {
|
||||
@@ -358,28 +606,27 @@ impl ClientWorker {
|
||||
let (tx, rx) = unbounded_channel();
|
||||
let account = &settings.profile;
|
||||
|
||||
// Set up a custom client that only uses HTTP/1.
|
||||
//
|
||||
// During my testing, I kept stumbling across something weird with sync and HTTP/2 that
|
||||
// will need to be revisited in the future.
|
||||
let req_timeout = Duration::from_secs(settings.tunables.request_timeout);
|
||||
|
||||
// Set up the HTTP client.
|
||||
let http = reqwest::Client::builder()
|
||||
.user_agent(IAMB_USER_AGENT)
|
||||
.timeout(Duration::from_secs(30))
|
||||
.timeout(req_timeout)
|
||||
.pool_idle_timeout(Duration::from_secs(60))
|
||||
.pool_max_idle_per_host(10)
|
||||
.tcp_keepalive(Duration::from_secs(10))
|
||||
.http1_only()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let req_config = RequestConfig::new().timeout(req_timeout).retry_timeout(req_timeout);
|
||||
|
||||
// Set up the Matrix client for the selected profile.
|
||||
let client = Client::builder()
|
||||
.http_client(Arc::new(http))
|
||||
.homeserver_url(account.url.clone())
|
||||
.store_config(StoreConfig::default())
|
||||
.sled_store(settings.matrix_dir.as_path(), None)
|
||||
.expect("Failed to setup up sled store for Matrix SDK")
|
||||
.request_config(RequestConfig::new().timeout(REQ_TIMEOUT).retry_timeout(REQ_TIMEOUT))
|
||||
.request_config(req_config)
|
||||
.build()
|
||||
.await
|
||||
.expect("Failed to instantiate Matrix client");
|
||||
@@ -388,8 +635,8 @@ impl ClientWorker {
|
||||
initialized: false,
|
||||
settings,
|
||||
client: client.clone(),
|
||||
load_handle: None,
|
||||
sync_handle: None,
|
||||
rcpt_handle: None,
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
@@ -414,18 +661,10 @@ impl ClientWorker {
|
||||
if let Some(handle) = self.sync_handle.take() {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
if let Some(handle) = self.rcpt_handle.take() {
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&mut self, task: WorkerTask) {
|
||||
match task {
|
||||
WorkerTask::DirectMessages(reply) => {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.direct_messages().await);
|
||||
},
|
||||
WorkerTask::Init(store, reply) => {
|
||||
assert_eq!(self.initialized, false);
|
||||
self.init(store).await;
|
||||
@@ -443,14 +682,6 @@ impl ClientWorker {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.get_room(room_id).await);
|
||||
},
|
||||
WorkerTask::ActiveRooms(reply) => {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.active_rooms().await);
|
||||
},
|
||||
WorkerTask::LoadOlder(room_id, fetch_id, limit, reply) => {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.load_older(room_id, fetch_id, limit).await);
|
||||
},
|
||||
WorkerTask::Login(style, reply) => {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.login_and_sync(style).await);
|
||||
@@ -463,10 +694,6 @@ impl ClientWorker {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.space_members(space).await);
|
||||
},
|
||||
WorkerTask::Spaces(reply) => {
|
||||
assert!(self.initialized);
|
||||
reply.send(self.spaces().await);
|
||||
},
|
||||
WorkerTask::TypingNotice(room_id) => {
|
||||
assert!(self.initialized);
|
||||
self.typing_notice(room_id).await;
|
||||
@@ -503,6 +730,15 @@ impl ClientWorker {
|
||||
},
|
||||
);
|
||||
|
||||
let _ =
|
||||
self.client
|
||||
.add_event_handler(|ev: PresenceEvent, store: Ctx<AsyncProgramStore>| {
|
||||
async move {
|
||||
let mut locked = store.lock().await;
|
||||
locked.application.presences.insert(ev.sender, ev.content.presence);
|
||||
}
|
||||
});
|
||||
|
||||
let _ = self.client.add_event_handler(
|
||||
|ev: SyncStateEvent<RoomNameEventContent>,
|
||||
room: MatrixRoom,
|
||||
@@ -513,8 +749,7 @@ impl ClientWorker {
|
||||
let room_id = room.room_id().to_owned();
|
||||
let room_name = Some(room_name.to_string());
|
||||
let mut locked = store.lock().await;
|
||||
let mut info =
|
||||
locked.application.rooms.entry(room_id.to_owned()).or_default();
|
||||
let mut info = locked.application.rooms.get_or_default(room_id.clone());
|
||||
info.name = room_name;
|
||||
}
|
||||
}
|
||||
@@ -529,8 +764,6 @@ impl ClientWorker {
|
||||
store: Ctx<AsyncProgramStore>| {
|
||||
async move {
|
||||
let room_id = room.room_id();
|
||||
let room_name = room.display_name().await.ok();
|
||||
let room_name = room_name.as_ref().map(ToString::to_string);
|
||||
|
||||
if let Some(msg) = ev.as_original() {
|
||||
if let MessageType::VerificationRequest(_) = msg.content.msgtype {
|
||||
@@ -545,8 +778,11 @@ impl ClientWorker {
|
||||
}
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
let mut info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.name = room_name;
|
||||
|
||||
let sender = ev.sender().to_owned();
|
||||
let _ = locked.application.presences.get_or_default(sender);
|
||||
|
||||
let info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.insert(ev.into_full_event(room_id.to_owned()));
|
||||
}
|
||||
},
|
||||
@@ -560,6 +796,10 @@ impl ClientWorker {
|
||||
let room_id = room.room_id();
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
|
||||
let sender = ev.sender().to_owned();
|
||||
let _ = locked.application.presences.get_or_default(sender);
|
||||
|
||||
let info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.insert_reaction(ev.into_full_event(room_id.to_owned()));
|
||||
}
|
||||
@@ -583,7 +823,7 @@ impl ClientWorker {
|
||||
Some(EventLocation::Message(key)) => {
|
||||
if let Some(msg) = info.messages.get_mut(key) {
|
||||
let ev = SyncRoomRedactionEvent::Original(ev);
|
||||
msg.event.redact(ev, room_version);
|
||||
msg.redact(ev, room_version);
|
||||
}
|
||||
},
|
||||
Some(EventLocation::Reaction(event_id)) => {
|
||||
@@ -598,6 +838,38 @@ impl ClientWorker {
|
||||
},
|
||||
);
|
||||
|
||||
let _ = self.client.add_event_handler(
|
||||
|ev: OriginalSyncRoomMemberEvent,
|
||||
room: MatrixRoom,
|
||||
client: Client,
|
||||
store: Ctx<AsyncProgramStore>| {
|
||||
async move {
|
||||
let room_id = room.room_id();
|
||||
let user_id = ev.state_key;
|
||||
|
||||
let ambiguous_name =
|
||||
ev.content.displayname.as_deref().unwrap_or_else(|| user_id.localpart());
|
||||
let ambiguous = client
|
||||
.store()
|
||||
.get_users_with_display_name(room_id, ambiguous_name)
|
||||
.await
|
||||
.map(|users| users.len() > 1)
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
let info = locked.application.get_room_info(room_id.to_owned());
|
||||
|
||||
if ambiguous {
|
||||
info.display_names.remove(&user_id);
|
||||
} else if let Some(display) = ev.content.displayname {
|
||||
info.display_names.insert(user_id, display);
|
||||
} else {
|
||||
info.display_names.remove(&user_id);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let _ = self.client.add_event_handler(
|
||||
|ev: OriginalSyncKeyVerificationStartEvent,
|
||||
client: Client,
|
||||
@@ -654,10 +926,11 @@ impl ClientWorker {
|
||||
let request = client
|
||||
.encryption()
|
||||
.get_verification_request(&ev.sender, &ev.content.transaction_id)
|
||||
.await
|
||||
.unwrap();
|
||||
.await;
|
||||
|
||||
request.accept().await.unwrap();
|
||||
if let Some(request) = request {
|
||||
request.accept().await.unwrap();
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -710,17 +983,14 @@ impl ClientWorker {
|
||||
},
|
||||
);
|
||||
|
||||
let client = self.client.clone();
|
||||
self.load_handle = tokio::spawn({
|
||||
let client = self.client.clone();
|
||||
|
||||
self.rcpt_handle = 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;
|
||||
async move {
|
||||
let load = load_older_forever(&client, &store);
|
||||
let rcpt = refresh_receipts_forever(&client, &store);
|
||||
let room = refresh_rooms_forever(&client, &store);
|
||||
let ((), (), ()) = tokio::join!(load, rcpt, room);
|
||||
}
|
||||
})
|
||||
.into();
|
||||
@@ -749,53 +1019,42 @@ impl ClientWorker {
|
||||
},
|
||||
}
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
self.sync_handle = tokio::spawn(async move {
|
||||
loop {
|
||||
let settings = SyncSettings::default();
|
||||
|
||||
let _ = client.sync(settings).await;
|
||||
}
|
||||
});
|
||||
|
||||
self.sync_handle = Some(handle);
|
||||
|
||||
self.client
|
||||
.sync_once(SyncSettings::default())
|
||||
.await
|
||||
.map_err(IambError::from)?;
|
||||
})
|
||||
.into();
|
||||
|
||||
Ok(Some(InfoMessage::from("Successfully logged in!")))
|
||||
}
|
||||
|
||||
async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> {
|
||||
for (room, name, tags) in self.direct_messages().await {
|
||||
async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<OwnedRoomId> {
|
||||
for room in self.client.rooms() {
|
||||
if !room.is_direct() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() {
|
||||
return Ok((room, name, tags));
|
||||
return Ok(room.room_id().to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
let mut request = CreateRoomRequest::new();
|
||||
let invite = [user.clone()];
|
||||
request.is_direct = true;
|
||||
request.invite = &invite;
|
||||
request.visibility = Visibility::Private;
|
||||
request.preset = Some(RoomPreset::PrivateChat);
|
||||
let rt = CreateRoomType::Direct(user.clone());
|
||||
let flags = CreateRoomFlags::ENCRYPTED;
|
||||
|
||||
match self.client.create_room(request).await {
|
||||
Ok(resp) => self.get_room(resp.room_id).await,
|
||||
Err(e) => {
|
||||
error!(
|
||||
user_id = user.as_str(),
|
||||
err = e.to_string(),
|
||||
"Failed to create direct message room"
|
||||
);
|
||||
create_room(&self.client, None, rt, flags).await.map_err(|e| {
|
||||
error!(
|
||||
user_id = user.as_str(),
|
||||
err = e.to_string(),
|
||||
"Failed to create direct message room"
|
||||
);
|
||||
|
||||
let msg = format!("Could not open a room with {user}");
|
||||
let err = UIError::Failure(msg);
|
||||
|
||||
Err(err)
|
||||
},
|
||||
}
|
||||
let msg = format!("Could not open a room with {user}");
|
||||
UIError::Failure(msg)
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_inviter(&mut self, invited: Invited) -> IambResult<Option<RoomMember>> {
|
||||
@@ -827,9 +1086,7 @@ impl ClientWorker {
|
||||
},
|
||||
}
|
||||
} else if let Ok(user) = OwnedUserId::try_from(name.as_str()) {
|
||||
let room = self.direct_message(user).await?.0;
|
||||
|
||||
return Ok(room.room_id().to_owned());
|
||||
self.direct_message(user).await
|
||||
} else {
|
||||
let msg = format!("{:?} is not a valid room or user name", name.as_str());
|
||||
let err = UIError::Failure(msg);
|
||||
@@ -838,91 +1095,6 @@ impl ClientWorker {
|
||||
}
|
||||
}
|
||||
|
||||
async fn direct_messages(&self) -> Vec<FetchedRoom> {
|
||||
let mut rooms = vec![];
|
||||
|
||||
for room in self.client.invited_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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
async fn active_rooms(&self) -> Vec<FetchedRoom> {
|
||||
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() {
|
||||
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));
|
||||
}
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
async fn load_older(
|
||||
&mut self,
|
||||
room_id: OwnedRoomId,
|
||||
fetch_id: Option<String>,
|
||||
limit: u32,
|
||||
) -> MessageFetchResult {
|
||||
if let Some(room) = self.client.get_room(room_id.as_ref()) {
|
||||
let mut opts = match &fetch_id {
|
||||
Some(id) => MessagesOptions::backward().from(id.as_str()),
|
||||
None => MessagesOptions::backward(),
|
||||
};
|
||||
opts.limit = limit.into();
|
||||
|
||||
let Messages { end, chunk, .. } = room.messages(opts).await.map_err(IambError::from)?;
|
||||
|
||||
let msgs = chunk.into_iter().filter_map(|ev| {
|
||||
match ev.event.deserialize() {
|
||||
Ok(AnyTimelineEvent::MessageLike(msg)) => Some(msg),
|
||||
Ok(AnyTimelineEvent::State(_)) => None,
|
||||
Err(_) => None,
|
||||
}
|
||||
});
|
||||
|
||||
Ok((end, msgs.collect()))
|
||||
} else {
|
||||
Err(IambError::UnknownRoom(room_id).into())
|
||||
}
|
||||
}
|
||||
|
||||
async fn members(&mut self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> {
|
||||
if let Some(room) = self.client.get_room(room_id.as_ref()) {
|
||||
Ok(room.active_members().await.map_err(IambError::from)?)
|
||||
@@ -943,32 +1115,6 @@ impl ClientWorker {
|
||||
Ok(rooms)
|
||||
}
|
||||
|
||||
async fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
||||
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() {
|
||||
if !room.is_space() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
||||
|
||||
spaces.push((room.into(), name));
|
||||
}
|
||||
|
||||
return spaces;
|
||||
}
|
||||
|
||||
async fn typing_notice(&mut self, room_id: OwnedRoomId) {
|
||||
if let Some(room) = self.client.get_joined_room(room_id.as_ref()) {
|
||||
let _ = room.typing_notice(true).await;
|
||||
@@ -1031,7 +1177,7 @@ impl ClientWorker {
|
||||
let _req = request.await.map_err(IambError::from)?;
|
||||
let info = format!("Sent verification request to {user_id}");
|
||||
|
||||
Ok(InfoMessage::from(info).into())
|
||||
Ok(Some(InfoMessage::from(info)))
|
||||
},
|
||||
None => {
|
||||
let msg = format!("Could not find identity information for {user_id}");
|
||||
|
||||
Reference in New Issue
Block a user