Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10b142c071 | ||
|
|
ac6ff63d25 | ||
|
|
54a0e76823 | ||
|
|
93eff79f79 | ||
|
|
11625262f1 | ||
|
|
0ed1d53946 | ||
|
|
e3be8c16cb | ||
|
|
4c5c57e26c | ||
|
|
8eef8787cc | ||
|
|
c9c547acc1 | ||
|
|
3629f15e0d | ||
|
|
fd72cf5c4e | ||
|
|
1d93461183 | ||
|
|
a1574c6b8d | ||
|
|
e8205df21d | ||
|
|
8c010d7e7e | ||
|
|
4337be108b | ||
|
|
b968d8c4a2 | ||
|
|
5683a2e7a8 | ||
|
|
afe892c7fe | ||
|
|
d8713141f2 | ||
|
|
a6888bbc93 | ||
|
|
4f2261e66f | ||
|
|
8966644f6e |
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
@@ -45,7 +45,7 @@ jobs:
|
|||||||
override: true
|
override: true
|
||||||
components: rustfmt, clippy
|
components: rustfmt, clippy
|
||||||
- name: Cache cargo registry
|
- name: Cache cargo registry
|
||||||
uses: actions/cache@v1
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/registry
|
path: ~/.cargo/registry
|
||||||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|||||||
657
Cargo.lock
generated
657
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
26
Cargo.toml
26
Cargo.toml
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "iamb"
|
name = "iamb"
|
||||||
version = "0.0.3"
|
version = "0.0.6"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
authors = ["Ulyssa <git@ulyssa.dev>"]
|
authors = ["Ulyssa <git@ulyssa.dev>"]
|
||||||
repository = "https://github.com/ulyssa/iamb"
|
repository = "https://github.com/ulyssa/iamb"
|
||||||
@@ -10,25 +10,27 @@ description = "A Matrix chat client that uses Vim keybindings"
|
|||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
exclude = [".github", "CONTRIBUTING.md"]
|
exclude = [".github", "CONTRIBUTING.md"]
|
||||||
keywords = ["matrix", "chat", "tui", "vim"]
|
keywords = ["matrix", "chat", "tui", "vim"]
|
||||||
|
categories = ["command-line-utilities"]
|
||||||
rust-version = "1.66"
|
rust-version = "1.66"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bitflags = "1.3.2"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
clap = {version = "4.0", features = ["derive"]}
|
clap = {version = "4.0", features = ["derive"]}
|
||||||
|
css-color-parser = "0.1.2"
|
||||||
dirs = "4.0.0"
|
dirs = "4.0.0"
|
||||||
futures = "0.3.21"
|
emojis = "~0.5.2"
|
||||||
gethostname = "0.4.1"
|
gethostname = "0.4.1"
|
||||||
matrix-sdk = {version = "0.6", default-features = false, features = ["e2e-encryption", "sled", "rustls-tls"]}
|
html5ever = "0.26.0"
|
||||||
|
markup5ever_rcdom = "0.2.0"
|
||||||
mime = "^0.3.16"
|
mime = "^0.3.16"
|
||||||
mime_guess = "^2.0.4"
|
mime_guess = "^2.0.4"
|
||||||
modalkit = "0.0.9"
|
open = "3.2.0"
|
||||||
regex = "^1.5"
|
regex = "^1.5"
|
||||||
rpassword = "^7.2"
|
rpassword = "^7.2"
|
||||||
serde = "^1.0"
|
serde = "^1.0"
|
||||||
serde_json = "^1.0"
|
serde_json = "^1.0"
|
||||||
sled = "0.34"
|
|
||||||
thiserror = "^1.0.37"
|
thiserror = "^1.0.37"
|
||||||
tokio = {version = "1.24.1", features = ["macros", "net", "rt-multi-thread", "sync", "time"]}
|
|
||||||
tracing = "~0.1.36"
|
tracing = "~0.1.36"
|
||||||
tracing-appender = "~0.2.2"
|
tracing-appender = "~0.2.2"
|
||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = "0.3.16"
|
||||||
@@ -36,5 +38,17 @@ unicode-segmentation = "^1.7"
|
|||||||
unicode-width = "0.1.10"
|
unicode-width = "0.1.10"
|
||||||
url = {version = "^2.2.2", features = ["serde"]}
|
url = {version = "^2.2.2", features = ["serde"]}
|
||||||
|
|
||||||
|
[dependencies.modalkit]
|
||||||
|
version = "0.0.13"
|
||||||
|
|
||||||
|
[dependencies.matrix-sdk]
|
||||||
|
version = "0.6"
|
||||||
|
default-features = false
|
||||||
|
features = ["e2e-encryption", "markdown", "sled", "rustls-tls"]
|
||||||
|
|
||||||
|
[dependencies.tokio]
|
||||||
|
version = "1.24.1"
|
||||||
|
features = ["macros", "net", "rt-multi-thread", "sync", "time"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
|||||||
35
README.md
35
README.md
@@ -11,6 +11,8 @@
|
|||||||
This project is a work-in-progress, and there's still a lot to be implemented,
|
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.
|
but much of the basic client functionality is already present.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
You can find documentation for installing, configuring, and using iamb on its
|
You can find documentation for installing, configuring, and using iamb on its
|
||||||
@@ -21,7 +23,7 @@ website, [iamb.chat].
|
|||||||
Install Rust and Cargo, and then run:
|
Install Rust and Cargo, and then run:
|
||||||
|
|
||||||
```
|
```
|
||||||
cargo install iamb
|
cargo install --locked iamb
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
@@ -48,16 +50,16 @@ two other TUI clients and Element Web:
|
|||||||
| | iamb | [gomuks] | [weechat-matrix] | Element Web/Desktop |
|
| | iamb | [gomuks] | [weechat-matrix] | Element Web/Desktop |
|
||||||
| --------------------------------------- | :---------- | :------: | :--------------: | :-----------------: |
|
| --------------------------------------- | :---------- | :------: | :--------------: | :-----------------: |
|
||||||
| Room directory | ❌ ([#14]) | ❌ | ✔️ | ✔️ |
|
| Room directory | ❌ ([#14]) | ❌ | ✔️ | ✔️ |
|
||||||
| Room tag showing | ❌ ([#15]) | ✔️ | ❌ | ✔️ |
|
| Room tag showing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||||
| Room tag editing | ❌ ([#15]) | ✔️ | ❌ | ✔️ |
|
| Room tag editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||||
| Search joined rooms | ❌ ([#16]) | ✔️ | ❌ | ✔️ |
|
| Search joined rooms | ❌ ([#16]) | ✔️ | ❌ | ✔️ |
|
||||||
| Room user list | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Room user list | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Display Room Description | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Display Room Description | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Edit Room Description | ✔️ | ❌ | ✔️ | ✔️ |
|
| Edit Room Description | ✔️ | ❌ | ✔️ | ✔️ |
|
||||||
| Highlights | ❌ ([#8]) | ✔️ | ✔️ | ✔️ |
|
| Highlights | ❌ ([#8]) | ✔️ | ✔️ | ✔️ |
|
||||||
| Pushrules | ❌ | ✔️ | ❌ | ✔️ |
|
| Pushrules | ❌ | ✔️ | ❌ | ✔️ |
|
||||||
| Send read markers | ❌ ([#11]) | ✔️ | ✔️ | ✔️ |
|
| Send read markers | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Display read markers | ❌ ([#11]) | ❌ | ❌ | ✔️ |
|
| Display read markers | ✔️ | ❌ | ❌ | ✔️ |
|
||||||
| Sending Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Sending Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Accepting Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Accepting Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Typing Notification | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Typing Notification | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
@@ -66,16 +68,16 @@ two other TUI clients and Element Web:
|
|||||||
| Attachment uploading | ✔️ | ❌ | ✔️ | ✔️ |
|
| Attachment uploading | ✔️ | ❌ | ✔️ | ✔️ |
|
||||||
| Attachment downloading | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Attachment downloading | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Send stickers | ❌ | ❌ | ❌ | ✔️ |
|
| Send stickers | ❌ | ❌ | ❌ | ✔️ |
|
||||||
| Send formatted messages (markdown) | ❌ ([#10]) | ✔️ | ✔️ | ✔️ |
|
| Send formatted messages (markdown) | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Rich Text Editor for formatted messages | ❌ | ❌ | ❌ | ✔️ |
|
| Rich Text Editor for formatted messages | ❌ | ❌ | ❌ | ✔️ |
|
||||||
| Display formatted messages | ❌ ([#10]) | ✔️ | ✔️ | ✔️ |
|
| Display formatted messages | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Redacting | ✔️ | ✔️ | ✔️ | ✔️ |
|
| Redacting | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||||
| Multiple Matrix Accounts | ✔️ | ❌ | ✔️ | ❌ |
|
| Multiple Matrix Accounts | ✔️ | ❌ | ✔️ | ❌ |
|
||||||
| New user registration | ❌ | ❌ | ❌ | ✔️ |
|
| New user registration | ❌ | ❌ | ❌ | ✔️ |
|
||||||
| VOIP | ❌ | ❌ | ❌ | ✔️ |
|
| VOIP | ❌ | ❌ | ❌ | ✔️ |
|
||||||
| Reactions | ❌ ([#2]) | ✔️ | ❌ | ✔️ |
|
| Reactions | ✔️ | ✔️ | ❌ | ✔️ |
|
||||||
| Message editing | ❌ ([#4]) | ✔️ | ❌ | ✔️ |
|
| Message editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||||
| Room upgrades | ❌ | ✔️ | ❌ | ✔️ |
|
| Room upgrades | ❌ ([#41]) | ✔️ | ❌ | ✔️ |
|
||||||
| Localisations | ❌ | 1 | ❌ | 44 |
|
| Localisations | ❌ | 1 | ❌ | 44 |
|
||||||
| SSO Support | ❌ | ✔️ | ✔️ | ✔️ |
|
| SSO Support | ❌ | ✔️ | ✔️ | ✔️ |
|
||||||
|
|
||||||
@@ -88,18 +90,7 @@ iamb is released under the [Apache License, Version 2.0].
|
|||||||
[iamb.chat]: https://iamb.chat
|
[iamb.chat]: https://iamb.chat
|
||||||
[gomuks]: https://github.com/tulir/gomuks
|
[gomuks]: https://github.com/tulir/gomuks
|
||||||
[weechat-matrix]: https://github.com/poljar/weechat-matrix
|
[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
|
[#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
|
[#14]: https://github.com/ulyssa/iamb/issues/14
|
||||||
[#15]: https://github.com/ulyssa/iamb/issues/15
|
|
||||||
[#16]: https://github.com/ulyssa/iamb/issues/16
|
[#16]: https://github.com/ulyssa/iamb/issues/16
|
||||||
|
[#41]: https://github.com/ulyssa/iamb/issues/41
|
||||||
|
|||||||
534
src/base.rs
534
src/base.rs
@@ -1,14 +1,38 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use emojis::Emoji;
|
||||||
use tokio::sync::Mutex as AsyncMutex;
|
use tokio::sync::Mutex as AsyncMutex;
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
encryption::verification::SasVerification,
|
encryption::verification::SasVerification,
|
||||||
ruma::{OwnedRoomId, OwnedUserId, RoomId},
|
room::Joined,
|
||||||
|
ruma::{
|
||||||
|
events::{
|
||||||
|
reaction::ReactionEvent,
|
||||||
|
room::message::{
|
||||||
|
OriginalRoomMessageEvent,
|
||||||
|
Relation,
|
||||||
|
Replacement,
|
||||||
|
RoomMessageEvent,
|
||||||
|
RoomMessageEventContent,
|
||||||
|
},
|
||||||
|
tag::{TagName, Tags},
|
||||||
|
AnyMessageLikeEvent,
|
||||||
|
MessageLikeEvent,
|
||||||
|
},
|
||||||
|
presence::PresenceState,
|
||||||
|
EventId,
|
||||||
|
OwnedEventId,
|
||||||
|
OwnedRoomId,
|
||||||
|
OwnedUserId,
|
||||||
|
RoomId,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use modalkit::{
|
use modalkit::{
|
||||||
@@ -22,11 +46,15 @@ use modalkit::{
|
|||||||
ApplicationStore,
|
ApplicationStore,
|
||||||
ApplicationWindowId,
|
ApplicationWindowId,
|
||||||
},
|
},
|
||||||
|
base::{CommandType, WordStyle},
|
||||||
|
completion::{complete_path, CompletionMap},
|
||||||
context::EditContext,
|
context::EditContext,
|
||||||
|
cursor::Cursor,
|
||||||
|
rope::EditRope,
|
||||||
store::Store,
|
store::Store,
|
||||||
},
|
},
|
||||||
env::vim::{
|
env::vim::{
|
||||||
command::{CommandContext, VimCommand, VimCommandMachine},
|
command::{CommandContext, CommandDescription, VimCommand, VimCommandMachine},
|
||||||
keybindings::VimMachine,
|
keybindings::VimMachine,
|
||||||
VimContext,
|
VimContext,
|
||||||
},
|
},
|
||||||
@@ -41,12 +69,26 @@ use modalkit::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
message::{Message, Messages},
|
message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages},
|
||||||
worker::Requester,
|
worker::Requester,
|
||||||
ApplicationSettings,
|
ApplicationSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(3);
|
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)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum IambInfo {}
|
pub enum IambInfo {}
|
||||||
@@ -61,16 +103,75 @@ pub enum VerifyAction {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum MessageAction {
|
pub enum MessageAction {
|
||||||
|
/// Cance the current reply or edit.
|
||||||
Cancel,
|
Cancel,
|
||||||
Download(Option<String>, bool),
|
|
||||||
|
/// Download an attachment to the given path.
|
||||||
|
///
|
||||||
|
/// The second argument controls whether to overwrite any already existing file at the
|
||||||
|
/// destination path, or to open the attachment after downloading.
|
||||||
|
Download(Option<String>, DownloadFlags),
|
||||||
|
|
||||||
|
/// Edit a sent message.
|
||||||
|
Edit,
|
||||||
|
|
||||||
|
/// React to a message with an Emoji.
|
||||||
|
React(String),
|
||||||
|
|
||||||
|
/// Redact a message, with an optional reason.
|
||||||
Redact(Option<String>),
|
Redact(Option<String>),
|
||||||
|
|
||||||
|
/// Reply to a message.
|
||||||
Reply,
|
Reply,
|
||||||
|
|
||||||
|
/// Unreact to a message.
|
||||||
|
///
|
||||||
|
/// If no specific Emoji to remove to is specified, then all reactions from the user on the
|
||||||
|
/// message are removed.
|
||||||
|
Unreact(Option<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum SetRoomField {
|
pub enum CreateRoomType {
|
||||||
Name(String),
|
/// A direct message room.
|
||||||
Topic(String),
|
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;
|
||||||
|
|
||||||
|
/// Overwrite file if it already exists.
|
||||||
|
const FORCE = 0b00000001;
|
||||||
|
|
||||||
|
/// Open file after downloading.
|
||||||
|
const OPEN = 0b00000010;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub enum RoomField {
|
||||||
|
Name,
|
||||||
|
Tag(TagName),
|
||||||
|
Topic,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
@@ -79,13 +180,8 @@ pub enum RoomAction {
|
|||||||
InviteReject,
|
InviteReject,
|
||||||
InviteSend(OwnedUserId),
|
InviteSend(OwnedUserId),
|
||||||
Members(Box<CommandContext<ProgramContext>>),
|
Members(Box<CommandContext<ProgramContext>>),
|
||||||
Set(SetRoomField),
|
Set(RoomField, String),
|
||||||
}
|
Unset(RoomField),
|
||||||
|
|
||||||
impl From<SetRoomField> for RoomAction {
|
|
||||||
fn from(act: SetRoomField) -> Self {
|
|
||||||
RoomAction::Set(act)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
@@ -94,8 +190,14 @@ pub enum SendAction {
|
|||||||
Upload(String),
|
Upload(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub enum HomeserverAction {
|
||||||
|
CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum IambAction {
|
pub enum IambAction {
|
||||||
|
Homeserver(HomeserverAction),
|
||||||
Message(MessageAction),
|
Message(MessageAction),
|
||||||
Room(RoomAction),
|
Room(RoomAction),
|
||||||
Send(SendAction),
|
Send(SendAction),
|
||||||
@@ -104,6 +206,12 @@ pub enum IambAction {
|
|||||||
ToggleScrollbackFocus,
|
ToggleScrollbackFocus,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<HomeserverAction> for IambAction {
|
||||||
|
fn from(act: HomeserverAction) -> Self {
|
||||||
|
IambAction::Homeserver(act)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<MessageAction> for IambAction {
|
impl From<MessageAction> for IambAction {
|
||||||
fn from(act: MessageAction) -> Self {
|
fn from(act: MessageAction) -> Self {
|
||||||
IambAction::Message(act)
|
IambAction::Message(act)
|
||||||
@@ -125,6 +233,7 @@ impl From<SendAction> for IambAction {
|
|||||||
impl ApplicationAction for IambAction {
|
impl ApplicationAction for IambAction {
|
||||||
fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||||
match self {
|
match self {
|
||||||
|
IambAction::Homeserver(..) => SequenceStatus::Break,
|
||||||
IambAction::Message(..) => SequenceStatus::Break,
|
IambAction::Message(..) => SequenceStatus::Break,
|
||||||
IambAction::Room(..) => SequenceStatus::Break,
|
IambAction::Room(..) => SequenceStatus::Break,
|
||||||
IambAction::Send(..) => SequenceStatus::Break,
|
IambAction::Send(..) => SequenceStatus::Break,
|
||||||
@@ -136,6 +245,7 @@ impl ApplicationAction for IambAction {
|
|||||||
|
|
||||||
fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||||
match self {
|
match self {
|
||||||
|
IambAction::Homeserver(..) => SequenceStatus::Atom,
|
||||||
IambAction::Message(..) => SequenceStatus::Atom,
|
IambAction::Message(..) => SequenceStatus::Atom,
|
||||||
IambAction::Room(..) => SequenceStatus::Atom,
|
IambAction::Room(..) => SequenceStatus::Atom,
|
||||||
IambAction::Send(..) => SequenceStatus::Atom,
|
IambAction::Send(..) => SequenceStatus::Atom,
|
||||||
@@ -147,6 +257,7 @@ impl ApplicationAction for IambAction {
|
|||||||
|
|
||||||
fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||||
match self {
|
match self {
|
||||||
|
IambAction::Homeserver(..) => SequenceStatus::Ignore,
|
||||||
IambAction::Message(..) => SequenceStatus::Ignore,
|
IambAction::Message(..) => SequenceStatus::Ignore,
|
||||||
IambAction::Room(..) => SequenceStatus::Ignore,
|
IambAction::Room(..) => SequenceStatus::Ignore,
|
||||||
IambAction::Send(..) => SequenceStatus::Ignore,
|
IambAction::Send(..) => SequenceStatus::Ignore,
|
||||||
@@ -158,6 +269,7 @@ impl ApplicationAction for IambAction {
|
|||||||
|
|
||||||
fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
|
fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
|
||||||
match self {
|
match self {
|
||||||
|
IambAction::Homeserver(..) => false,
|
||||||
IambAction::Message(..) => false,
|
IambAction::Message(..) => false,
|
||||||
IambAction::Room(..) => false,
|
IambAction::Room(..) => false,
|
||||||
IambAction::Send(..) => false,
|
IambAction::Send(..) => false,
|
||||||
@@ -168,6 +280,12 @@ impl ApplicationAction for IambAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<RoomAction> for ProgramAction {
|
||||||
|
fn from(act: RoomAction) -> Self {
|
||||||
|
IambAction::from(act).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<IambAction> for ProgramAction {
|
impl From<IambAction> for ProgramAction {
|
||||||
fn from(act: IambAction) -> Self {
|
fn from(act: IambAction) -> Self {
|
||||||
Action::Application(act)
|
Action::Application(act)
|
||||||
@@ -184,6 +302,14 @@ pub type AsyncProgramStore = Arc<AsyncMutex<ProgramStore>>;
|
|||||||
|
|
||||||
pub type IambResult<T> = UIResult<T, IambInfo>;
|
pub type IambResult<T> = UIResult<T, IambInfo>;
|
||||||
|
|
||||||
|
/// Reaction events for some message.
|
||||||
|
///
|
||||||
|
/// The event identifier used as a key here is the ID for the reaction, and not for the message
|
||||||
|
/// it's reacting to.
|
||||||
|
pub type MessageReactions = HashMap<OwnedEventId, (String, OwnedUserId)>;
|
||||||
|
|
||||||
|
pub type Receipts = HashMap<OwnedEventId, Vec<OwnedUserId>>;
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum IambError {
|
pub enum IambError {
|
||||||
#[error("Invalid user identifier: {0}")]
|
#[error("Invalid user identifier: {0}")]
|
||||||
@@ -248,16 +374,152 @@ pub enum RoomFetchStatus {
|
|||||||
NotStarted,
|
NotStarted,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum EventLocation {
|
||||||
|
Message(MessageKey),
|
||||||
|
Reaction(OwnedEventId),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLocation {
|
||||||
|
fn to_message_key(&self) -> Option<&MessageKey> {
|
||||||
|
if let EventLocation::Message(key) = self {
|
||||||
|
Some(key)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct RoomInfo {
|
pub struct RoomInfo {
|
||||||
|
/// The display name for this room.
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
|
||||||
|
/// The tags placed on this room.
|
||||||
|
pub tags: Option<Tags>,
|
||||||
|
|
||||||
|
/// A map of event IDs to where they are stored in this struct.
|
||||||
|
pub keys: HashMap<OwnedEventId, EventLocation>,
|
||||||
|
|
||||||
|
/// The messages loaded for this room.
|
||||||
pub messages: Messages,
|
pub messages: Messages,
|
||||||
|
|
||||||
|
/// A map of read markers to display on different events.
|
||||||
|
pub receipts: HashMap<OwnedEventId, Vec<OwnedUserId>>,
|
||||||
|
|
||||||
|
/// An event ID for where we should indicate we've read up to.
|
||||||
|
pub read_till: Option<OwnedEventId>,
|
||||||
|
|
||||||
|
/// A map of message identifiers to a map of reaction events.
|
||||||
|
pub reactions: HashMap<OwnedEventId, MessageReactions>,
|
||||||
|
|
||||||
|
/// Where to continue fetching from when we continue loading scrollback history.
|
||||||
pub fetch_id: RoomFetchStatus,
|
pub fetch_id: RoomFetchStatus,
|
||||||
|
|
||||||
|
/// The time that we last fetched scrollback for this room.
|
||||||
pub fetch_last: Option<Instant>,
|
pub fetch_last: Option<Instant>,
|
||||||
|
|
||||||
|
/// Users currently typing in this room, and when we received notification of them doing so.
|
||||||
pub users_typing: Option<(Instant, Vec<OwnedUserId>)>,
|
pub users_typing: Option<(Instant, Vec<OwnedUserId>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RoomInfo {
|
impl RoomInfo {
|
||||||
|
pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize)> {
|
||||||
|
if let Some(reacts) = self.reactions.get(event_id) {
|
||||||
|
let mut counts = HashMap::new();
|
||||||
|
|
||||||
|
for (key, _) in reacts.values() {
|
||||||
|
let count = counts.entry(key.as_str()).or_default();
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut reactions = counts.into_iter().collect::<Vec<_>>();
|
||||||
|
reactions.sort();
|
||||||
|
|
||||||
|
reactions
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_event(&self, event_id: &EventId) -> Option<&Message> {
|
||||||
|
self.messages.get(self.keys.get(event_id)?.to_message_key()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_reaction(&mut self, react: ReactionEvent) {
|
||||||
|
match react {
|
||||||
|
MessageLikeEvent::Original(react) => {
|
||||||
|
let rel_id = react.content.relates_to.event_id;
|
||||||
|
let key = react.content.relates_to.key;
|
||||||
|
|
||||||
|
let message = self.reactions.entry(rel_id.clone()).or_default();
|
||||||
|
let event_id = react.event_id;
|
||||||
|
let user_id = react.sender;
|
||||||
|
|
||||||
|
message.insert(event_id.clone(), (key, user_id));
|
||||||
|
|
||||||
|
let loc = EventLocation::Reaction(rel_id);
|
||||||
|
self.keys.insert(event_id, loc);
|
||||||
|
},
|
||||||
|
MessageLikeEvent::Redacted(_) => {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_edit(&mut self, msg: Replacement) {
|
||||||
|
let event_id = msg.event_id;
|
||||||
|
let new_content = msg.new_content;
|
||||||
|
|
||||||
|
let key = if let Some(EventLocation::Message(k)) = self.keys.get(&event_id) {
|
||||||
|
k
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let msg = if let Some(msg) = self.messages.get_mut(key) {
|
||||||
|
msg
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
match &mut msg.event {
|
||||||
|
MessageEvent::Original(orig) => {
|
||||||
|
orig.content = *new_content;
|
||||||
|
},
|
||||||
|
MessageEvent::Local(_, content) => {
|
||||||
|
*content = new_content;
|
||||||
|
},
|
||||||
|
MessageEvent::Redacted(_) => {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.html = msg.event.html();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_message(&mut self, msg: RoomMessageEvent) {
|
||||||
|
let event_id = msg.event_id().to_owned();
|
||||||
|
let key = (msg.origin_server_ts().into(), event_id.clone());
|
||||||
|
|
||||||
|
self.keys.insert(event_id.clone(), EventLocation::Message(key.clone()));
|
||||||
|
self.messages.insert(key, msg.into());
|
||||||
|
|
||||||
|
// Remove any echo.
|
||||||
|
let key = (MessageTimeStamp::LocalEcho, event_id);
|
||||||
|
let _ = self.messages.remove(&key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, msg: RoomMessageEvent) {
|
||||||
|
match msg {
|
||||||
|
RoomMessageEvent::Original(OriginalRoomMessageEvent {
|
||||||
|
content:
|
||||||
|
RoomMessageEventContent { relates_to: Some(Relation::Replacement(repl)), .. },
|
||||||
|
..
|
||||||
|
}) => self.insert_edit(repl),
|
||||||
|
_ => self.insert_message(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn recently_fetched(&self) -> bool {
|
fn recently_fetched(&self) -> bool {
|
||||||
self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE)
|
self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE)
|
||||||
}
|
}
|
||||||
@@ -274,7 +536,7 @@ impl RoomInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_typing_spans(&self, settings: &ApplicationSettings) -> Spans {
|
fn get_typing_spans<'a>(&'a self, settings: &'a ApplicationSettings) -> Spans<'a> {
|
||||||
let typers = self.get_typers();
|
let typers = self.get_typers();
|
||||||
let n = typers.len();
|
let n = typers.len();
|
||||||
|
|
||||||
@@ -330,13 +592,28 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ChatStore {
|
pub struct ChatStore {
|
||||||
|
pub cmds: ProgramCommands,
|
||||||
pub worker: Requester,
|
pub worker: Requester,
|
||||||
pub rooms: HashMap<OwnedRoomId, RoomInfo>,
|
pub rooms: CompletionMap<OwnedRoomId, RoomInfo>,
|
||||||
pub names: HashMap<String, OwnedRoomId>,
|
pub names: CompletionMap<String, OwnedRoomId>,
|
||||||
|
pub presences: CompletionMap<OwnedUserId, PresenceState>,
|
||||||
pub verifications: HashMap<String, SasVerification>,
|
pub verifications: HashMap<String, SasVerification>,
|
||||||
pub settings: ApplicationSettings,
|
pub settings: ApplicationSettings,
|
||||||
pub need_load: HashSet<OwnedRoomId>,
|
pub need_load: HashSet<OwnedRoomId>,
|
||||||
|
pub emojis: CompletionMap<String, &'static Emoji>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatStore {
|
impl ChatStore {
|
||||||
@@ -345,13 +622,20 @@ impl ChatStore {
|
|||||||
worker,
|
worker,
|
||||||
settings,
|
settings,
|
||||||
|
|
||||||
|
cmds: crate::commands::setup_commands(),
|
||||||
names: Default::default(),
|
names: Default::default(),
|
||||||
rooms: Default::default(),
|
rooms: Default::default(),
|
||||||
|
presences: Default::default(),
|
||||||
verifications: Default::default(),
|
verifications: Default::default(),
|
||||||
need_load: Default::default(),
|
need_load: Default::default(),
|
||||||
|
emojis: emoji_map(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<Joined> {
|
||||||
|
self.worker.client.get_joined_room(room_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_room_title(&self, room_id: &RoomId) -> String {
|
pub fn get_room_title(&self, room_id: &RoomId) -> String {
|
||||||
self.rooms
|
self.rooms
|
||||||
.get(room_id)
|
.get(room_id)
|
||||||
@@ -360,15 +644,35 @@ impl ChatStore {
|
|||||||
.unwrap_or_else(|| "Untitled Matrix Room".to_string())
|
.unwrap_or_else(|| "Untitled Matrix Room".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn set_receipts(&mut self, receipts: Vec<(OwnedRoomId, Receipts)>) {
|
||||||
|
let mut updates = vec![];
|
||||||
|
|
||||||
|
for (room_id, receipts) in receipts.into_iter() {
|
||||||
|
if let Some(info) = self.rooms.get_mut(&room_id) {
|
||||||
|
info.receipts = receipts;
|
||||||
|
|
||||||
|
if let Some(read_till) = info.read_till.take() {
|
||||||
|
updates.push((room_id, read_till));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (room_id, read_till) in updates.into_iter() {
|
||||||
|
if let Some(room) = self.worker.client.get_joined_room(&room_id) {
|
||||||
|
let _ = room.read_receipt(read_till.as_ref()).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn mark_for_load(&mut self, room_id: OwnedRoomId) {
|
pub fn mark_for_load(&mut self, room_id: OwnedRoomId) {
|
||||||
self.need_load.insert(room_id);
|
self.need_load.insert(room_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_older(&mut self, limit: u32) {
|
pub fn load_older(&mut self, limit: u32) {
|
||||||
let ChatStore { need_load, rooms, worker, .. } = self;
|
let ChatStore { need_load, presences, rooms, worker, .. } = self;
|
||||||
|
|
||||||
for room_id in std::mem::take(need_load).into_iter() {
|
for room_id in std::mem::take(need_load).into_iter() {
|
||||||
let info = rooms.entry(room_id.clone()).or_default();
|
let info = rooms.get_or_default(room_id.clone());
|
||||||
|
|
||||||
if info.recently_fetched() {
|
if info.recently_fetched() {
|
||||||
need_load.insert(room_id);
|
need_load.insert(room_id);
|
||||||
@@ -388,9 +692,18 @@ impl ChatStore {
|
|||||||
match res {
|
match res {
|
||||||
Ok((fetch_id, msgs)) => {
|
Ok((fetch_id, msgs)) => {
|
||||||
for msg in msgs.into_iter() {
|
for msg in msgs.into_iter() {
|
||||||
let key = (msg.origin_server_ts().into(), msg.event_id().to_owned());
|
let sender = msg.sender().to_owned();
|
||||||
|
let _ = presences.get_or_default(sender);
|
||||||
|
|
||||||
info.messages.insert(key, Message::from(msg));
|
match msg {
|
||||||
|
AnyMessageLikeEvent::RoomMessage(msg) => {
|
||||||
|
info.insert(msg);
|
||||||
|
},
|
||||||
|
AnyMessageLikeEvent::Reaction(ev) => {
|
||||||
|
info.insert_reaction(ev);
|
||||||
|
},
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info.fetch_id =
|
info.fetch_id =
|
||||||
@@ -411,11 +724,11 @@ impl ChatStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_room_info(&mut self, room_id: OwnedRoomId) -> &mut RoomInfo {
|
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) {
|
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) {
|
pub fn insert_sas(&mut self, sas: SasVerification) {
|
||||||
@@ -458,7 +771,7 @@ impl RoomFocus {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub enum IambBufferId {
|
pub enum IambBufferId {
|
||||||
Command,
|
Command(CommandType),
|
||||||
Room(OwnedRoomId, RoomFocus),
|
Room(OwnedRoomId, RoomFocus),
|
||||||
DirectList,
|
DirectList,
|
||||||
MemberList(OwnedRoomId),
|
MemberList(OwnedRoomId),
|
||||||
@@ -471,7 +784,7 @@ pub enum IambBufferId {
|
|||||||
impl IambBufferId {
|
impl IambBufferId {
|
||||||
pub fn to_window(&self) -> Option<IambId> {
|
pub fn to_window(&self) -> Option<IambId> {
|
||||||
match self {
|
match self {
|
||||||
IambBufferId::Command => None,
|
IambBufferId::Command(_) => None,
|
||||||
IambBufferId::Room(room, _) => Some(IambId::Room(room.clone())),
|
IambBufferId::Room(room, _) => Some(IambId::Room(room.clone())),
|
||||||
IambBufferId::DirectList => Some(IambId::DirectList),
|
IambBufferId::DirectList => Some(IambId::DirectList),
|
||||||
IambBufferId::MemberList(room) => Some(IambId::MemberList(room.clone())),
|
IambBufferId::MemberList(room) => Some(IambId::MemberList(room.clone())),
|
||||||
@@ -491,12 +804,139 @@ impl ApplicationInfo for IambInfo {
|
|||||||
type Action = IambAction;
|
type Action = IambAction;
|
||||||
type WindowId = IambId;
|
type WindowId = IambId;
|
||||||
type ContentId = IambBufferId;
|
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_matrix_names(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_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_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" => complete_matrix_names(text, cursor, store),
|
||||||
|
"room" => vec![],
|
||||||
|
"verify" => vec![],
|
||||||
|
_ => panic!("unknown command {}", cmd.name.as_str()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
match CommandDescription::from_str(cow.as_ref()) {
|
||||||
|
Ok(desc) => {
|
||||||
|
if desc.arg.untrimmed.is_empty() {
|
||||||
|
// 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())
|
||||||
|
} else {
|
||||||
|
// Complete command argument.
|
||||||
|
complete_cmdarg(desc, text, cursor, store)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Can't parse command text, so return zero completions.
|
||||||
|
Err(_) => vec![],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::config::{user_style, user_style_from_color};
|
use crate::config::user_style_from_color;
|
||||||
use crate::tests::*;
|
use crate::tests::*;
|
||||||
use modalkit::tui::style::Color;
|
use modalkit::tui::style::Color;
|
||||||
|
|
||||||
@@ -576,4 +1016,44 @@ pub mod tests {
|
|||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_complete_cmdbar() {
|
||||||
|
let store = mock_store().await;
|
||||||
|
|
||||||
|
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, vec![
|
||||||
|
"@user1:example.com",
|
||||||
|
"@user2:example.com",
|
||||||
|
"@user3:example.com",
|
||||||
|
"@user4:example.com",
|
||||||
|
"@user5:example.com"
|
||||||
|
]);
|
||||||
|
|
||||||
|
let text = EditRope::from("invite ignored");
|
||||||
|
let mut cursor = Cursor::new(0, 7);
|
||||||
|
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||||
|
assert_eq!(res, vec![
|
||||||
|
"@user1:example.com",
|
||||||
|
"@user2:example.com",
|
||||||
|
"@user3:example.com",
|
||||||
|
"@user4:example.com",
|
||||||
|
"@user5:example.com"
|
||||||
|
]);
|
||||||
|
|
||||||
|
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"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
506
src/commands.rs
506
src/commands.rs
@@ -1,15 +1,19 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use matrix_sdk::ruma::OwnedUserId;
|
use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
|
||||||
|
|
||||||
use modalkit::{
|
use modalkit::{
|
||||||
editing::base::OpenTarget,
|
editing::base::OpenTarget,
|
||||||
env::vim::command::{CommandContext, CommandDescription},
|
env::vim::command::{CommandContext, CommandDescription, OptionType},
|
||||||
input::commands::{CommandError, CommandResult, CommandStep},
|
input::commands::{CommandError, CommandResult, CommandStep},
|
||||||
input::InputContext,
|
input::InputContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::base::{
|
use crate::base::{
|
||||||
|
CreateRoomFlags,
|
||||||
|
CreateRoomType,
|
||||||
|
DownloadFlags,
|
||||||
|
HomeserverAction,
|
||||||
IambAction,
|
IambAction,
|
||||||
IambId,
|
IambId,
|
||||||
MessageAction,
|
MessageAction,
|
||||||
@@ -17,14 +21,38 @@ use crate::base::{
|
|||||||
ProgramCommands,
|
ProgramCommands,
|
||||||
ProgramContext,
|
ProgramContext,
|
||||||
RoomAction,
|
RoomAction,
|
||||||
|
RoomField,
|
||||||
SendAction,
|
SendAction,
|
||||||
SetRoomField,
|
|
||||||
VerifyAction,
|
VerifyAction,
|
||||||
};
|
};
|
||||||
|
|
||||||
type ProgContext = CommandContext<ProgramContext>;
|
type ProgContext = CommandContext<ProgramContext>;
|
||||||
type ProgResult = CommandResult<ProgramCommand>;
|
type ProgResult = CommandResult<ProgramCommand>;
|
||||||
|
|
||||||
|
/// Convert strings the user types into a tag name.
|
||||||
|
fn tag_name(name: String) -> Result<TagName, CommandError> {
|
||||||
|
let tag = match name.as_str() {
|
||||||
|
"fav" | "favorite" | "favourite" | "m.favourite" => TagName::Favorite,
|
||||||
|
"low" | "lowpriority" | "low_priority" | "low-priority" | "m.lowpriority" => {
|
||||||
|
TagName::LowPriority
|
||||||
|
},
|
||||||
|
"servernotice" | "server_notice" | "server-notice" | "m.server_notice" => {
|
||||||
|
TagName::ServerNotice
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
if let Ok(tag) = name.parse() {
|
||||||
|
TagName::User(tag)
|
||||||
|
} else {
|
||||||
|
let msg = format!("Invalid user tag name: {name}");
|
||||||
|
|
||||||
|
return Err(CommandError::Error(msg));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(tag)
|
||||||
|
}
|
||||||
|
|
||||||
fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||||
let args = desc.arg.strings()?;
|
let args = desc.arg.strings()?;
|
||||||
|
|
||||||
@@ -138,8 +166,66 @@ fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|||||||
return Result::Err(CommandError::InvalidArgument);
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ract = IambAction::from(MessageAction::Cancel);
|
let mact = IambAction::from(MessageAction::Cancel);
|
||||||
let step = CommandStep::Continue(ract.into(), ctx.context.take());
|
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||||
|
|
||||||
|
return Ok(step);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iamb_edit(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||||
|
if !desc.arg.text.is_empty() {
|
||||||
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mact = IambAction::from(MessageAction::Edit);
|
||||||
|
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||||
|
|
||||||
|
return Ok(step);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iamb_react(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||||
|
let args = desc.arg.strings()?;
|
||||||
|
|
||||||
|
if args.len() != 1 {
|
||||||
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
|
}
|
||||||
|
|
||||||
|
let k = args[0].as_str();
|
||||||
|
|
||||||
|
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
||||||
|
let mact = IambAction::from(MessageAction::React(emoji.to_string()));
|
||||||
|
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||||
|
|
||||||
|
return Ok(step);
|
||||||
|
} else {
|
||||||
|
let msg = format!("Invalid Emoji or shortcode: {k}");
|
||||||
|
|
||||||
|
return Result::Err(CommandError::Error(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||||
|
let mut args = desc.arg.strings()?;
|
||||||
|
|
||||||
|
if args.len() > 1 {
|
||||||
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mact = if let Some(k) = args.pop() {
|
||||||
|
let k = k.as_str();
|
||||||
|
|
||||||
|
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
||||||
|
IambAction::from(MessageAction::Unreact(Some(emoji.to_string())))
|
||||||
|
} else {
|
||||||
|
let msg = format!("Invalid Emoji or shortcode: {k}");
|
||||||
|
|
||||||
|
return Result::Err(CommandError::Error(msg));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
IambAction::from(MessageAction::Unreact(None))
|
||||||
|
};
|
||||||
|
|
||||||
|
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||||
|
|
||||||
return Ok(step);
|
return Ok(step);
|
||||||
}
|
}
|
||||||
@@ -214,22 +300,93 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|||||||
return Ok(step);
|
return Ok(step);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
fn iamb_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()?;
|
let mut args = desc.arg.strings()?;
|
||||||
|
|
||||||
if args.len() != 2 {
|
if args.len() < 2 {
|
||||||
return Result::Err(CommandError::InvalidArgument);
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
}
|
}
|
||||||
|
|
||||||
let field = args.remove(0);
|
let field = args.remove(0);
|
||||||
let value = args.remove(0);
|
let action = args.remove(0);
|
||||||
|
|
||||||
let act: IambAction = match field.as_str() {
|
if args.len() > 1 {
|
||||||
"room.name" => RoomAction::Set(SetRoomField::Name(value)).into(),
|
|
||||||
"room.topic" => RoomAction::Set(SetRoomField::Topic(value)).into(),
|
|
||||||
_ => {
|
|
||||||
return Result::Err(CommandError::InvalidArgument);
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
},
|
}
|
||||||
|
|
||||||
|
let act: IambAction = match (field.as_str(), action.as_str(), args.pop()) {
|
||||||
|
// :room name set <room-name>
|
||||||
|
("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
|
||||||
|
("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
// :room name unset
|
||||||
|
("name", "unset", None) => RoomAction::Unset(RoomField::Name).into(),
|
||||||
|
("name", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
// :room topic set <topic>
|
||||||
|
("topic", "set", Some(s)) => RoomAction::Set(RoomField::Topic, s).into(),
|
||||||
|
("topic", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
// :room topic unset
|
||||||
|
("topic", "unset", None) => RoomAction::Unset(RoomField::Topic).into(),
|
||||||
|
("topic", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
// :room tag set <tag-name>
|
||||||
|
("tag", "set", Some(s)) => RoomAction::Set(RoomField::Tag(tag_name(s)?), "".into()).into(),
|
||||||
|
("tag", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
// :room tag unset <tag-name>
|
||||||
|
("tag", "unset", Some(s)) => RoomAction::Unset(RoomField::Tag(tag_name(s)?)).into(),
|
||||||
|
("tag", "unset", None) => return Result::Err(CommandError::InvalidArgument),
|
||||||
|
|
||||||
|
_ => return Result::Err(CommandError::InvalidArgument),
|
||||||
};
|
};
|
||||||
|
|
||||||
let step = CommandStep::Continue(act.into(), ctx.context.take());
|
let step = CommandStep::Continue(act.into(), ctx.context.take());
|
||||||
@@ -258,7 +415,29 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult
|
|||||||
return Result::Err(CommandError::InvalidArgument);
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mact = MessageAction::Download(args.pop(), desc.bang);
|
let mut flags = DownloadFlags::NONE;
|
||||||
|
if desc.bang {
|
||||||
|
flags |= DownloadFlags::FORCE;
|
||||||
|
};
|
||||||
|
let mact = MessageAction::Download(args.pop(), flags);
|
||||||
|
let iact = IambAction::from(mact);
|
||||||
|
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||||
|
|
||||||
|
return Ok(step);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||||
|
let mut args = desc.arg.strings()?;
|
||||||
|
|
||||||
|
if args.len() > 1 {
|
||||||
|
return Result::Err(CommandError::InvalidArgument);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut flags = DownloadFlags::OPEN;
|
||||||
|
if desc.bang {
|
||||||
|
flags |= DownloadFlags::FORCE;
|
||||||
|
};
|
||||||
|
let mact = MessageAction::Download(args.pop(), flags);
|
||||||
let iact = IambAction::from(mact);
|
let iact = IambAction::from(mact);
|
||||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||||
|
|
||||||
@@ -266,20 +445,81 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_iamb_commands(cmds: &mut ProgramCommands) {
|
fn add_iamb_commands(cmds: &mut ProgramCommands) {
|
||||||
cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
|
cmds.add_command(ProgramCommand {
|
||||||
cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
|
name: "cancel".into(),
|
||||||
cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
|
aliases: vec![],
|
||||||
cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
|
f: iamb_cancel,
|
||||||
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 {
|
||||||
cmds.add_command(ProgramCommand { names: vec!["redact".into()], f: iamb_redact });
|
name: "create".into(),
|
||||||
cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply });
|
aliases: vec![],
|
||||||
cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
|
f: iamb_create,
|
||||||
cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set });
|
});
|
||||||
cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
|
cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
|
||||||
cmds.add_command(ProgramCommand { names: vec!["upload".into()], f: iamb_upload });
|
cmds.add_command(ProgramCommand {
|
||||||
cmds.add_command(ProgramCommand { names: vec!["verify".into()], f: iamb_verify });
|
name: "download".into(),
|
||||||
cmds.add_command(ProgramCommand { names: vec!["welcome".into()], f: iamb_welcome });
|
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: "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 {
|
pub fn setup_commands() -> ProgramCommands {
|
||||||
@@ -364,47 +604,227 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cmd_set() {
|
fn test_cmd_room_invalid() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room foo", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room set topic", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_topic_set() {
|
||||||
let mut cmds = setup_commands();
|
let mut cmds = setup_commands();
|
||||||
let ctx = ProgramContext::default();
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
let res = cmds
|
let res = cmds
|
||||||
.input_cmd("set room.topic \"Lots of fun discussion!\"", ctx.clone())
|
.input_cmd("room topic set \"Lots of fun discussion!\"", ctx.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let act = IambAction::Room(SetRoomField::Topic("Lots of fun discussion!".into()).into());
|
let act = RoomAction::Set(RoomField::Topic, "Lots of fun discussion!".into());
|
||||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
let res = cmds
|
let res = cmds
|
||||||
.input_cmd("set room.topic The\\ Discussion\\ Room", ctx.clone())
|
.input_cmd("room topic set The\\ Discussion\\ Room", ctx.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let act = IambAction::Room(SetRoomField::Topic("The Discussion Room".into()).into());
|
let act = RoomAction::Set(RoomField::Topic, "The Discussion Room".into());
|
||||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
let res = cmds.input_cmd("set room.topic Development", ctx.clone()).unwrap();
|
let res = cmds.input_cmd("room topic set Development", ctx.clone()).unwrap();
|
||||||
let act = IambAction::Room(SetRoomField::Topic("Development".into()).into());
|
let act = RoomAction::Set(RoomField::Topic, "Development".into());
|
||||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
let res = cmds.input_cmd("set room.name Development", ctx.clone()).unwrap();
|
let res = cmds.input_cmd("room topic", ctx.clone());
|
||||||
let act = IambAction::Room(SetRoomField::Name("Development".into()).into());
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room topic set", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room topic set A B C", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_name_invalid() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room name", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room name foo", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_name_set() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room name set Development", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Name, "Development".into());
|
||||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
let res = cmds
|
let res = cmds
|
||||||
.input_cmd("set room.name \"Application Development\"", ctx.clone())
|
.input_cmd("room name set \"Application Development\"", ctx.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let act = IambAction::Room(SetRoomField::Name("Application Development".into()).into());
|
let act = RoomAction::Set(RoomField::Name, "Application Development".into());
|
||||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
let res = cmds.input_cmd("set", ctx.clone());
|
let res = cmds.input_cmd("room name set", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_name_unset() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room name unset", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Name);
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room name unset foo", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_tag_set() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set favourite", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set favorite", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set fav", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set low_priority", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set low-priority", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set low", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set servernotice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set u.custom-tag", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Set(
|
||||||
|
RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())),
|
||||||
|
"".into(),
|
||||||
|
);
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set u.irc", ctx.clone()).unwrap();
|
||||||
|
let act =
|
||||||
|
RoomAction::Set(RoomField::Tag(TagName::User("u.irc".parse().unwrap())), "".into());
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag", ctx.clone());
|
||||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
let res = cmds.input_cmd("set room.name", ctx.clone());
|
let res = cmds.input_cmd("room tag set", ctx.clone());
|
||||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
let res = cmds.input_cmd("set room.topic", ctx.clone());
|
let res = cmds.input_cmd("room tag set unknown", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag set needs-leading-u-dot", ctx.clone());
|
||||||
|
assert_eq!(
|
||||||
|
res,
|
||||||
|
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_room_tag_unset() {
|
||||||
|
let mut cmds = setup_commands();
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset favourite", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset favorite", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset fav", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset low_priority", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset low-priority", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset low", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset servernotice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset u.custom-tag", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset u.irc", ctx.clone()).unwrap();
|
||||||
|
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.irc".parse().unwrap())));
|
||||||
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag", ctx.clone());
|
||||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
let res = cmds.input_cmd("set room.topic A B C", ctx.clone());
|
let res = cmds.input_cmd("room tag set", ctx.clone());
|
||||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset unknown", ctx.clone());
|
||||||
|
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
||||||
|
|
||||||
|
let res = cmds.input_cmd("room tag unset needs-leading-u-dot", ctx.clone());
|
||||||
|
assert_eq!(
|
||||||
|
res,
|
||||||
|
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
use std::collections::hash_map::DefaultHasher;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@@ -52,10 +53,6 @@ pub fn user_style_from_color(color: Color) -> Style {
|
|||||||
Style::default().fg(color).add_modifier(StyleModifier::BOLD)
|
Style::default().fg(color).add_modifier(StyleModifier::BOLD)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn user_style(user: &str) -> Style {
|
|
||||||
user_style_from_color(user_color(user))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_profile_char(c: char) -> bool {
|
fn is_profile_char(c: char) -> bool {
|
||||||
c.is_ascii_alphanumeric() || c == '.' || c == '-'
|
c.is_ascii_alphanumeric() || c == '.' || c == '-'
|
||||||
}
|
}
|
||||||
@@ -181,32 +178,54 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TunableValues {
|
pub struct TunableValues {
|
||||||
pub typing_notice: bool,
|
pub reaction_display: bool,
|
||||||
|
pub reaction_shortcode_display: bool,
|
||||||
|
pub read_receipt_send: bool,
|
||||||
|
pub read_receipt_display: bool,
|
||||||
|
pub typing_notice_send: bool,
|
||||||
pub typing_notice_display: bool,
|
pub typing_notice_display: bool,
|
||||||
pub users: UserOverrides,
|
pub users: UserOverrides,
|
||||||
|
pub default_room: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub struct Tunables {
|
pub struct Tunables {
|
||||||
pub typing_notice: Option<bool>,
|
pub reaction_display: Option<bool>,
|
||||||
|
pub reaction_shortcode_display: Option<bool>,
|
||||||
|
pub read_receipt_send: Option<bool>,
|
||||||
|
pub read_receipt_display: Option<bool>,
|
||||||
|
pub typing_notice_send: Option<bool>,
|
||||||
pub typing_notice_display: Option<bool>,
|
pub typing_notice_display: Option<bool>,
|
||||||
pub users: Option<UserOverrides>,
|
pub users: Option<UserOverrides>,
|
||||||
|
pub default_room: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tunables {
|
impl Tunables {
|
||||||
fn merge(self, other: Self) -> Self {
|
fn merge(self, other: Self) -> Self {
|
||||||
Tunables {
|
Tunables {
|
||||||
typing_notice: self.typing_notice.or(other.typing_notice),
|
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),
|
||||||
|
typing_notice_send: self.typing_notice_send.or(other.typing_notice_send),
|
||||||
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
|
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
|
||||||
users: merge_users(self.users, other.users),
|
users: merge_users(self.users, other.users),
|
||||||
|
default_room: self.default_room.or(other.default_room),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn values(self) -> TunableValues {
|
fn values(self) -> TunableValues {
|
||||||
TunableValues {
|
TunableValues {
|
||||||
typing_notice: self.typing_notice.unwrap_or(true),
|
reaction_display: self.reaction_display.unwrap_or(true),
|
||||||
typing_notice_display: self.typing_notice.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),
|
||||||
|
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(),
|
users: self.users.unwrap_or_default(),
|
||||||
|
default_room: self.default_room,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -374,24 +393,41 @@ impl ApplicationSettings {
|
|||||||
Ok(settings)
|
Ok(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
pub fn get_user_char_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
||||||
if let Some(user) = self.tunables.users.get(user_id) {
|
let (color, c) = self
|
||||||
let color = if let Some(UserColor(c)) = user.color {
|
.tunables
|
||||||
c
|
.users
|
||||||
} else {
|
.get(user_id)
|
||||||
user_color(user_id.as_str())
|
.map(|user| {
|
||||||
};
|
(
|
||||||
|
user.color.as_ref().map(|c| c.0),
|
||||||
|
user.name.as_ref().and_then(|s| s.chars().next()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||||
let style = user_style_from_color(color);
|
let style = user_style_from_color(color);
|
||||||
|
|
||||||
if let Some(name) = &user.name {
|
let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' '));
|
||||||
Span::styled(name.clone(), style)
|
|
||||||
} else {
|
Span::styled(String::from(c), style)
|
||||||
Span::styled(user_id.as_str(), style)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Span::styled(user_id.as_str(), user_style(user_id.as_str()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_user_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
||||||
|
let (color, name) = self
|
||||||
|
.tunables
|
||||||
|
.users
|
||||||
|
.get(user_id)
|
||||||
|
.map(|user| (user.color.as_ref().map(|c| c.0), user.name.clone().map(Cow::Owned)))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let user_id = user_id.as_str();
|
||||||
|
let color = color.unwrap_or_else(|| user_color(user_id));
|
||||||
|
let style = user_style_from_color(color);
|
||||||
|
let name = name.unwrap_or(Cow::Borrowed(user_id));
|
||||||
|
|
||||||
|
Span::styled(name, style)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,22 +497,22 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_parse_tunables() {
|
fn test_parse_tunables() {
|
||||||
let res: Tunables = serde_json::from_str("{}").unwrap();
|
let res: Tunables = serde_json::from_str("{}").unwrap();
|
||||||
assert_eq!(res.typing_notice, None);
|
assert_eq!(res.typing_notice_send, None);
|
||||||
assert_eq!(res.typing_notice_display, None);
|
assert_eq!(res.typing_notice_display, None);
|
||||||
assert_eq!(res.users, None);
|
assert_eq!(res.users, None);
|
||||||
|
|
||||||
let res: Tunables = serde_json::from_str("{\"typing_notice\": true}").unwrap();
|
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": true}").unwrap();
|
||||||
assert_eq!(res.typing_notice, Some(true));
|
assert_eq!(res.typing_notice_send, Some(true));
|
||||||
assert_eq!(res.typing_notice_display, None);
|
assert_eq!(res.typing_notice_display, None);
|
||||||
assert_eq!(res.users, None);
|
assert_eq!(res.users, None);
|
||||||
|
|
||||||
let res: Tunables = serde_json::from_str("{\"typing_notice\": false}").unwrap();
|
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": false}").unwrap();
|
||||||
assert_eq!(res.typing_notice, Some(false));
|
assert_eq!(res.typing_notice_send, Some(false));
|
||||||
assert_eq!(res.typing_notice_display, None);
|
assert_eq!(res.typing_notice_display, None);
|
||||||
assert_eq!(res.users, None);
|
assert_eq!(res.users, None);
|
||||||
|
|
||||||
let res: Tunables = serde_json::from_str("{\"users\": {}}").unwrap();
|
let res: Tunables = serde_json::from_str("{\"users\": {}}").unwrap();
|
||||||
assert_eq!(res.typing_notice, None);
|
assert_eq!(res.typing_notice_send, None);
|
||||||
assert_eq!(res.typing_notice_display, None);
|
assert_eq!(res.typing_notice_display, None);
|
||||||
assert_eq!(res.users, Some(HashMap::new()));
|
assert_eq!(res.users, Some(HashMap::new()));
|
||||||
|
|
||||||
@@ -484,7 +520,7 @@ mod tests {
|
|||||||
"{\"users\": {\"@a:b.c\": {\"color\": \"black\", \"name\": \"Tim\"}}}",
|
"{\"users\": {\"@a:b.c\": {\"color\": \"black\", \"name\": \"Tim\"}}}",
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(res.typing_notice, None);
|
assert_eq!(res.typing_notice_send, None);
|
||||||
assert_eq!(res.typing_notice_display, None);
|
assert_eq!(res.typing_notice_display, None);
|
||||||
let users = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
|
let users = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
|
||||||
color: Some(UserColor(Color::Black)),
|
color: Some(UserColor(Color::Black)),
|
||||||
|
|||||||
@@ -1,32 +1,21 @@
|
|||||||
use modalkit::{
|
use modalkit::{
|
||||||
editing::action::WindowAction,
|
editing::action::WindowAction,
|
||||||
editing::base::WordStyle,
|
|
||||||
env::vim::keybindings::{InputStep, VimBindings},
|
env::vim::keybindings::{InputStep, VimBindings},
|
||||||
env::vim::VimMode,
|
env::vim::VimMode,
|
||||||
input::bindings::{EdgeEvent, EdgeRepeat, InputBindings},
|
input::bindings::{EdgeEvent, EdgeRepeat, InputBindings},
|
||||||
input::key::TerminalKey,
|
input::key::TerminalKey,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::base::{IambAction, Keybindings};
|
use crate::base::{IambAction, IambInfo, Keybindings, MATRIX_ID_WORD};
|
||||||
|
|
||||||
/// Find the boundaries for a Matrix username, room alias, or room ID.
|
type IambStep = InputStep<IambInfo>;
|
||||||
///
|
|
||||||
/// 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 {
|
pub fn setup_keybindings() -> Keybindings {
|
||||||
let mut ism = Keybindings::empty();
|
let mut ism = Keybindings::empty();
|
||||||
|
|
||||||
let vim = VimBindings::default()
|
let vim = VimBindings::default()
|
||||||
.submit_on_enter()
|
.submit_on_enter()
|
||||||
.cursor_open(WordStyle::CharSet(is_mxid_char));
|
.cursor_open(MATRIX_ID_WORD.clone());
|
||||||
|
|
||||||
vim.setup(&mut ism);
|
vim.setup(&mut ism);
|
||||||
|
|
||||||
@@ -44,19 +33,27 @@ pub fn setup_keybindings() -> Keybindings {
|
|||||||
(EdgeRepeat::Once, ctrl_w.clone()),
|
(EdgeRepeat::Once, ctrl_w.clone()),
|
||||||
(EdgeRepeat::Once, ctrl_z),
|
(EdgeRepeat::Once, ctrl_z),
|
||||||
];
|
];
|
||||||
let zoom = InputStep::new().actions(vec![WindowAction::ZoomToggle.into()]);
|
let zoom = IambStep::new()
|
||||||
|
.actions(vec![WindowAction::ZoomToggle.into()])
|
||||||
|
.goto(VimMode::Normal);
|
||||||
|
|
||||||
ism.add_mapping(VimMode::Normal, &cwz, &zoom);
|
ism.add_mapping(VimMode::Normal, &cwz, &zoom);
|
||||||
|
ism.add_mapping(VimMode::Visual, &cwz, &zoom);
|
||||||
ism.add_mapping(VimMode::Normal, &cwcz, &zoom);
|
ism.add_mapping(VimMode::Normal, &cwcz, &zoom);
|
||||||
|
ism.add_mapping(VimMode::Visual, &cwcz, &zoom);
|
||||||
|
|
||||||
let cwm = vec![
|
let cwm = vec![
|
||||||
(EdgeRepeat::Once, ctrl_w.clone()),
|
(EdgeRepeat::Once, ctrl_w.clone()),
|
||||||
(EdgeRepeat::Once, key_m_lc),
|
(EdgeRepeat::Once, key_m_lc),
|
||||||
];
|
];
|
||||||
let cwcm = vec![(EdgeRepeat::Once, ctrl_w), (EdgeRepeat::Once, ctrl_m)];
|
let cwcm = vec![(EdgeRepeat::Once, ctrl_w), (EdgeRepeat::Once, ctrl_m)];
|
||||||
let stoggle = InputStep::new().actions(vec![IambAction::ToggleScrollbackFocus.into()]);
|
let stoggle = IambStep::new()
|
||||||
|
.actions(vec![IambAction::ToggleScrollbackFocus.into()])
|
||||||
|
.goto(VimMode::Normal);
|
||||||
ism.add_mapping(VimMode::Normal, &cwm, &stoggle);
|
ism.add_mapping(VimMode::Normal, &cwm, &stoggle);
|
||||||
|
ism.add_mapping(VimMode::Visual, &cwm, &stoggle);
|
||||||
ism.add_mapping(VimMode::Normal, &cwcm, &stoggle);
|
ism.add_mapping(VimMode::Normal, &cwcm, &stoggle);
|
||||||
|
ism.add_mapping(VimMode::Visual, &cwcm, &stoggle);
|
||||||
|
|
||||||
return ism;
|
return ism;
|
||||||
}
|
}
|
||||||
|
|||||||
95
src/main.rs
95
src/main.rs
@@ -23,7 +23,7 @@ use matrix_sdk::ruma::OwnedUserId;
|
|||||||
use modalkit::crossterm::{
|
use modalkit::crossterm::{
|
||||||
self,
|
self,
|
||||||
cursor::Show as CursorShow,
|
cursor::Show as CursorShow,
|
||||||
event::{poll, read, Event},
|
event::{poll, read, DisableBracketedPaste, EnableBracketedPaste, Event},
|
||||||
execute,
|
execute,
|
||||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle},
|
terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle},
|
||||||
};
|
};
|
||||||
@@ -42,6 +42,7 @@ mod commands;
|
|||||||
mod config;
|
mod config;
|
||||||
mod keybindings;
|
mod keybindings;
|
||||||
mod message;
|
mod message;
|
||||||
|
mod util;
|
||||||
mod windows;
|
mod windows;
|
||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
@@ -52,20 +53,19 @@ use crate::{
|
|||||||
base::{
|
base::{
|
||||||
AsyncProgramStore,
|
AsyncProgramStore,
|
||||||
ChatStore,
|
ChatStore,
|
||||||
|
HomeserverAction,
|
||||||
IambAction,
|
IambAction,
|
||||||
IambBufferId,
|
|
||||||
IambError,
|
IambError,
|
||||||
IambId,
|
IambId,
|
||||||
IambInfo,
|
IambInfo,
|
||||||
IambResult,
|
IambResult,
|
||||||
ProgramAction,
|
ProgramAction,
|
||||||
ProgramCommands,
|
|
||||||
ProgramContext,
|
ProgramContext,
|
||||||
ProgramStore,
|
ProgramStore,
|
||||||
},
|
},
|
||||||
config::{ApplicationSettings, Iamb},
|
config::{ApplicationSettings, Iamb},
|
||||||
windows::IambWindow,
|
windows::IambWindow,
|
||||||
worker::{ClientWorker, LoginStyle, Requester},
|
worker::{create_room, ClientWorker, LoginStyle, Requester},
|
||||||
};
|
};
|
||||||
|
|
||||||
use modalkit::{
|
use modalkit::{
|
||||||
@@ -76,6 +76,8 @@ use modalkit::{
|
|||||||
EditError,
|
EditError,
|
||||||
EditInfo,
|
EditInfo,
|
||||||
Editable,
|
Editable,
|
||||||
|
EditorAction,
|
||||||
|
InsertTextAction,
|
||||||
Jumpable,
|
Jumpable,
|
||||||
Promptable,
|
Promptable,
|
||||||
Scrollable,
|
Scrollable,
|
||||||
@@ -84,7 +86,7 @@ use modalkit::{
|
|||||||
WindowAction,
|
WindowAction,
|
||||||
WindowContainer,
|
WindowContainer,
|
||||||
},
|
},
|
||||||
base::{OpenTarget, RepeatType},
|
base::{MoveDir1D, OpenTarget, RepeatType},
|
||||||
context::Resolve,
|
context::Resolve,
|
||||||
key::KeyManager,
|
key::KeyManager,
|
||||||
store::Store,
|
store::Store,
|
||||||
@@ -99,13 +101,20 @@ use modalkit::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MIN_MSG_LOAD: u32 = 50;
|
||||||
|
|
||||||
|
fn msg_load_req(area: Rect) -> u32 {
|
||||||
|
let n = area.height as u32;
|
||||||
|
|
||||||
|
n.max(MIN_MSG_LOAD)
|
||||||
|
}
|
||||||
|
|
||||||
struct Application {
|
struct Application {
|
||||||
store: AsyncProgramStore,
|
store: AsyncProgramStore,
|
||||||
worker: Requester,
|
worker: Requester,
|
||||||
terminal: Terminal<CrosstermBackend<Stdout>>,
|
terminal: Terminal<CrosstermBackend<Stdout>>,
|
||||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||||
cmds: ProgramCommands,
|
|
||||||
screen: ScreenState<IambWindow, IambInfo>,
|
screen: ScreenState<IambWindow, IambInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +126,7 @@ impl Application {
|
|||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
crossterm::terminal::enable_raw_mode()?;
|
crossterm::terminal::enable_raw_mode()?;
|
||||||
crossterm::execute!(stdout, EnterAlternateScreen)?;
|
crossterm::execute!(stdout, EnterAlternateScreen)?;
|
||||||
|
crossterm::execute!(stdout, EnableBracketedPaste)?;
|
||||||
|
|
||||||
let title = format!("iamb ({})", settings.profile.user_id);
|
let title = format!("iamb ({})", settings.profile.user_id);
|
||||||
crossterm::execute!(stdout, SetTitle(title))?;
|
crossterm::execute!(stdout, SetTitle(title))?;
|
||||||
@@ -126,11 +136,17 @@ impl Application {
|
|||||||
|
|
||||||
let bindings = crate::keybindings::setup_keybindings();
|
let bindings = crate::keybindings::setup_keybindings();
|
||||||
let bindings = KeyManager::new(bindings);
|
let bindings = KeyManager::new(bindings);
|
||||||
let cmds = crate::commands::setup_commands();
|
|
||||||
|
|
||||||
let mut locked = store.lock().await;
|
let mut locked = store.lock().await;
|
||||||
let win = IambWindow::open(IambId::Welcome, locked.deref_mut()).unwrap();
|
|
||||||
let cmd = CommandBarState::new(IambBufferId::Command, locked.deref_mut());
|
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(locked.deref_mut());
|
||||||
let screen = ScreenState::new(win, cmd);
|
let screen = ScreenState::new(win, cmd);
|
||||||
|
|
||||||
let worker = locked.application.worker.clone();
|
let worker = locked.application.worker.clone();
|
||||||
@@ -144,7 +160,6 @@ impl Application {
|
|||||||
terminal,
|
terminal,
|
||||||
bindings,
|
bindings,
|
||||||
actstack,
|
actstack,
|
||||||
cmds,
|
|
||||||
screen,
|
screen,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -176,7 +191,7 @@ impl Application {
|
|||||||
f.set_cursor(cx, cy);
|
f.set_cursor(cx, cy);
|
||||||
}
|
}
|
||||||
|
|
||||||
store.application.load_older(area.height as u32);
|
store.application.load_older(msg_load_req(area));
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -186,7 +201,8 @@ impl Application {
|
|||||||
loop {
|
loop {
|
||||||
self.redraw(false, self.store.clone().lock().await.deref_mut())?;
|
self.redraw(false, self.store.clone().lock().await.deref_mut())?;
|
||||||
|
|
||||||
if !poll(Duration::from_millis(500))? {
|
if !poll(Duration::from_secs(1))? {
|
||||||
|
// Redraw in case there's new messages to show.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +217,21 @@ impl Application {
|
|||||||
Event::Resize(_, _) => {
|
Event::Resize(_, _) => {
|
||||||
// We'll redraw for the new size next time step() is called.
|
// We'll redraw for the new size next time step() is called.
|
||||||
},
|
},
|
||||||
Event::Paste(_) => {
|
Event::Paste(s) => {
|
||||||
// Do nothing for now.
|
let act = InsertTextAction::Transcribe(s, MoveDir1D::Previous, 1.into());
|
||||||
|
let act = EditorAction::from(act);
|
||||||
|
let ctx = ProgramContext::default();
|
||||||
|
let mut store = self.store.lock().await;
|
||||||
|
|
||||||
|
match self.screen.editor_command(&act, &ctx, store.deref_mut()) {
|
||||||
|
Ok(None) => {},
|
||||||
|
Ok(Some(info)) => {
|
||||||
|
self.screen.push_info(info);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
self.screen.push_error(e);
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -288,7 +317,7 @@ impl Application {
|
|||||||
None
|
None
|
||||||
},
|
},
|
||||||
Action::Command(act) => {
|
Action::Command(act) => {
|
||||||
let acts = self.cmds.command(&act, &ctx)?;
|
let acts = store.application.cmds.command(&act, &ctx)?;
|
||||||
self.action_prepend(acts);
|
self.action_prepend(acts);
|
||||||
|
|
||||||
None
|
None
|
||||||
@@ -327,6 +356,12 @@ impl Application {
|
|||||||
None
|
None
|
||||||
},
|
},
|
||||||
|
|
||||||
|
IambAction::Homeserver(act) => {
|
||||||
|
let acts = self.homeserver_command(act, ctx, store).await?;
|
||||||
|
self.action_prepend(acts);
|
||||||
|
|
||||||
|
None
|
||||||
|
},
|
||||||
IambAction::Message(act) => {
|
IambAction::Message(act) => {
|
||||||
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
|
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
|
||||||
},
|
},
|
||||||
@@ -359,6 +394,25 @@ impl Application {
|
|||||||
Ok(info)
|
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)])
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn run(&mut self) -> Result<(), std::io::Error> {
|
pub async fn run(&mut self) -> Result<(), std::io::Error> {
|
||||||
self.terminal.clear()?;
|
self.terminal.clear()?;
|
||||||
|
|
||||||
@@ -422,13 +476,13 @@ async fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<
|
|||||||
match worker.login(LoginStyle::Password(password)) {
|
match worker.login(LoginStyle::Password(password)) {
|
||||||
Ok(info) => {
|
Ok(info) => {
|
||||||
if let Some(msg) = info {
|
if let Some(msg) = info {
|
||||||
println!("{}", msg);
|
println!("{msg}");
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("Failed to login: {}", err);
|
println!("Failed to login: {err}");
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -438,7 +492,7 @@ async fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_exit<T: Display, N>(v: T) -> N {
|
fn print_exit<T: Display, N>(v: T) -> N {
|
||||||
println!("{}", v);
|
println!("{v}");
|
||||||
process::exit(2);
|
process::exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,6 +510,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
|
|||||||
let orig_hook = std::panic::take_hook();
|
let orig_hook = std::panic::take_hook();
|
||||||
std::panic::set_hook(Box::new(move |panic_info| {
|
std::panic::set_hook(Box::new(move |panic_info| {
|
||||||
let _ = crossterm::terminal::disable_raw_mode();
|
let _ = crossterm::terminal::disable_raw_mode();
|
||||||
|
let _ = crossterm::execute!(stdout(), DisableBracketedPaste);
|
||||||
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
||||||
let _ = crossterm::execute!(stdout(), CursorShow);
|
let _ = crossterm::execute!(stdout(), CursorShow);
|
||||||
orig_hook(panic_info);
|
orig_hook(panic_info);
|
||||||
@@ -489,7 +544,7 @@ fn main() -> IambResult<()> {
|
|||||||
|
|
||||||
let subscriber = FmtSubscriber::builder()
|
let subscriber = FmtSubscriber::builder()
|
||||||
.with_writer(appender)
|
.with_writer(appender)
|
||||||
.with_max_level(Level::TRACE)
|
.with_max_level(Level::INFO)
|
||||||
.finish();
|
.finish();
|
||||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||||
|
|
||||||
@@ -498,7 +553,7 @@ fn main() -> IambResult<()> {
|
|||||||
.thread_name_fn(|| {
|
.thread_name_fn(|| {
|
||||||
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
||||||
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
|
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
|
||||||
format!("iamb-worker-{}", id)
|
format!("iamb-worker-{id}")
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|||||||
1150
src/message/html.rs
Normal file
1150
src/message/html.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,26 +4,30 @@ use std::collections::hash_map::DefaultHasher;
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::str::Lines;
|
use std::slice::Iter;
|
||||||
|
|
||||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
use matrix_sdk::ruma::{
|
use matrix_sdk::ruma::{
|
||||||
events::{
|
events::{
|
||||||
room::{
|
room::{
|
||||||
message::{
|
message::{
|
||||||
|
FormattedBody,
|
||||||
|
MessageFormat,
|
||||||
MessageType,
|
MessageType,
|
||||||
OriginalRoomMessageEvent,
|
OriginalRoomMessageEvent,
|
||||||
RedactedRoomMessageEvent,
|
RedactedRoomMessageEvent,
|
||||||
|
Relation,
|
||||||
RoomMessageEvent,
|
RoomMessageEvent,
|
||||||
RoomMessageEventContent,
|
RoomMessageEventContent,
|
||||||
},
|
},
|
||||||
redaction::SyncRoomRedactionEvent,
|
redaction::SyncRoomRedactionEvent,
|
||||||
},
|
},
|
||||||
|
AnyMessageLikeEvent,
|
||||||
Redact,
|
Redact,
|
||||||
},
|
},
|
||||||
|
EventId,
|
||||||
MilliSecondsSinceUnixEpoch,
|
MilliSecondsSinceUnixEpoch,
|
||||||
OwnedEventId,
|
OwnedEventId,
|
||||||
OwnedUserId,
|
OwnedUserId,
|
||||||
@@ -33,6 +37,7 @@ use matrix_sdk::ruma::{
|
|||||||
|
|
||||||
use modalkit::tui::{
|
use modalkit::tui::{
|
||||||
style::{Modifier as StyleModifier, Style},
|
style::{Modifier as StyleModifier, Style},
|
||||||
|
symbols::line::THICK_VERTICAL,
|
||||||
text::{Span, Spans, Text},
|
text::{Span, Spans, Text},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,84 +46,53 @@ use modalkit::editing::{base::ViewportContext, cursor::Cursor};
|
|||||||
use crate::{
|
use crate::{
|
||||||
base::{IambResult, RoomInfo},
|
base::{IambResult, RoomInfo},
|
||||||
config::ApplicationSettings,
|
config::ApplicationSettings,
|
||||||
|
message::html::{parse_matrix_html, StyleTree},
|
||||||
|
util::{space_span, wrapped_text},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type MessageFetchResult = IambResult<(Option<String>, Vec<RoomMessageEvent>)>;
|
mod html;
|
||||||
|
mod printer;
|
||||||
|
|
||||||
|
pub type MessageFetchResult = IambResult<(Option<String>, Vec<AnyMessageLikeEvent>)>;
|
||||||
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
|
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
|
||||||
pub type Messages = BTreeMap<MessageKey, Message>;
|
pub type Messages = BTreeMap<MessageKey, Message>;
|
||||||
|
|
||||||
const USER_GUTTER: usize = 30;
|
const fn span_static(s: &'static str) -> Span<'static> {
|
||||||
const TIME_GUTTER: usize = 12;
|
Span {
|
||||||
const MIN_MSG_LEN: usize = 30;
|
content: Cow::Borrowed(s),
|
||||||
|
|
||||||
const USER_GUTTER_EMPTY: &str = " ";
|
|
||||||
const USER_GUTTER_EMPTY_SPAN: Span<'static> = Span {
|
|
||||||
content: Cow::Borrowed(USER_GUTTER_EMPTY),
|
|
||||||
style: Style {
|
style: Style {
|
||||||
fg: None,
|
fg: None,
|
||||||
bg: None,
|
bg: None,
|
||||||
add_modifier: StyleModifier::empty(),
|
add_modifier: StyleModifier::empty(),
|
||||||
sub_modifier: StyleModifier::empty(),
|
sub_modifier: StyleModifier::empty(),
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const BOLD_STYLE: Style = Style {
|
||||||
|
fg: None,
|
||||||
|
bg: None,
|
||||||
|
add_modifier: StyleModifier::BOLD,
|
||||||
|
sub_modifier: StyleModifier::empty(),
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WrappedLinesIterator<'a> {
|
const USER_GUTTER: usize = 30;
|
||||||
iter: Lines<'a>,
|
const TIME_GUTTER: usize = 12;
|
||||||
curr: Option<&'a str>,
|
const READ_GUTTER: usize = 5;
|
||||||
width: usize,
|
const MIN_MSG_LEN: usize = 30;
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> WrappedLinesIterator<'a> {
|
const USER_GUTTER_EMPTY: &str = " ";
|
||||||
fn new(input: &'a str, width: usize) -> Self {
|
const USER_GUTTER_EMPTY_SPAN: Span<'static> = span_static(USER_GUTTER_EMPTY);
|
||||||
WrappedLinesIterator { iter: input.lines(), curr: None, width }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for WrappedLinesIterator<'a> {
|
const TIME_GUTTER_EMPTY: &str = " ";
|
||||||
type Item = (&'a str, usize);
|
const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
#[inline]
|
||||||
if self.curr.is_none() {
|
fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> {
|
||||||
self.curr = self.iter.next();
|
let time = i64::from(ms) / 1000;
|
||||||
}
|
let time = NaiveDateTime::from_timestamp_opt(time, 0).unwrap_or_default();
|
||||||
|
|
||||||
if let Some(s) = self.curr.take() {
|
LocalTz.from_utc_datetime(&time)
|
||||||
let width = UnicodeWidthStr::width(s);
|
|
||||||
|
|
||||||
if width <= self.width {
|
|
||||||
return Some((s, width));
|
|
||||||
} else {
|
|
||||||
// Find where to split the line.
|
|
||||||
let mut width = 0;
|
|
||||||
let mut idx = 0;
|
|
||||||
|
|
||||||
for (i, g) in UnicodeSegmentation::grapheme_indices(s, true) {
|
|
||||||
let gw = UnicodeWidthStr::width(g);
|
|
||||||
idx = i;
|
|
||||||
|
|
||||||
if width + gw > self.width {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
width += gw;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.curr = Some(&s[idx..]);
|
|
||||||
|
|
||||||
return Some((&s[..idx], width));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wrap(input: &str, width: usize) -> WrappedLinesIterator<'_> {
|
|
||||||
WrappedLinesIterator::new(input, width)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn space(width: usize) -> String {
|
|
||||||
" ".repeat(width)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
@@ -137,14 +111,31 @@ pub enum MessageTimeStamp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MessageTimeStamp {
|
impl MessageTimeStamp {
|
||||||
fn show(&self) -> Option<Span> {
|
fn as_datetime(&self) -> DateTime<LocalTz> {
|
||||||
match self {
|
match self {
|
||||||
MessageTimeStamp::OriginServer(ts) => {
|
MessageTimeStamp::OriginServer(ms) => millis_to_datetime(*ms),
|
||||||
let time = i64::from(*ts) / 1000;
|
MessageTimeStamp::LocalEcho => LocalTz::now(),
|
||||||
let time = NaiveDateTime::from_timestamp_opt(time, 0)?;
|
}
|
||||||
let time = DateTime::<Utc>::from_utc(time, Utc);
|
}
|
||||||
let time = time.format("%T");
|
|
||||||
let time = format!(" [{}]", time);
|
fn same_day(&self, other: &Self) -> bool {
|
||||||
|
let dt1 = self.as_datetime();
|
||||||
|
let dt2 = other.as_datetime();
|
||||||
|
|
||||||
|
dt1.date_naive() == dt2.date_naive()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_date(&self) -> Option<Span> {
|
||||||
|
let time = self.as_datetime().format("%A, %B %d %Y").to_string();
|
||||||
|
|
||||||
|
Span::styled(time, BOLD_STYLE).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_time(&self) -> Option<Span> {
|
||||||
|
match self {
|
||||||
|
MessageTimeStamp::OriginServer(ms) => {
|
||||||
|
let time = millis_to_datetime(*ms).format("%T");
|
||||||
|
let time = format!(" [{time}]");
|
||||||
|
|
||||||
Span::raw(time).into()
|
Span::raw(time).into()
|
||||||
},
|
},
|
||||||
@@ -181,6 +172,12 @@ impl PartialOrd for MessageTimeStamp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<UInt> for MessageTimeStamp {
|
||||||
|
fn from(millis: UInt) -> Self {
|
||||||
|
MessageTimeStamp::OriginServer(millis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
|
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
|
||||||
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
|
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
|
||||||
MessageTimeStamp::OriginServer(millis.0)
|
MessageTimeStamp::OriginServer(millis.0)
|
||||||
@@ -210,7 +207,7 @@ impl TryFrom<usize> for MessageTimeStamp {
|
|||||||
let n = u64::try_from(u)?;
|
let n = u64::try_from(u)?;
|
||||||
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
|
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
|
||||||
|
|
||||||
Ok(MessageTimeStamp::OriginServer(n))
|
Ok(MessageTimeStamp::from(n))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,13 +320,21 @@ impl PartialOrd for MessageCursor {
|
|||||||
pub enum MessageEvent {
|
pub enum MessageEvent {
|
||||||
Original(Box<OriginalRoomMessageEvent>),
|
Original(Box<OriginalRoomMessageEvent>),
|
||||||
Redacted(Box<RedactedRoomMessageEvent>),
|
Redacted(Box<RedactedRoomMessageEvent>),
|
||||||
Local(Box<RoomMessageEventContent>),
|
Local(OwnedEventId, Box<RoomMessageEventContent>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageEvent {
|
impl MessageEvent {
|
||||||
pub fn show(&self) -> Cow<'_, str> {
|
pub fn event_id(&self) -> &EventId {
|
||||||
match self {
|
match self {
|
||||||
MessageEvent::Original(ev) => show_room_content(&ev.content),
|
MessageEvent::Original(ev) => ev.event_id.as_ref(),
|
||||||
|
MessageEvent::Redacted(ev) => ev.event_id.as_ref(),
|
||||||
|
MessageEvent::Local(event_id, _) => event_id.as_ref(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn body(&self) -> Cow<'_, str> {
|
||||||
|
match self {
|
||||||
|
MessageEvent::Original(ev) => body_cow_content(&ev.content),
|
||||||
MessageEvent::Redacted(ev) => {
|
MessageEvent::Redacted(ev) => {
|
||||||
let reason = ev
|
let reason = ev
|
||||||
.unsigned
|
.unsigned
|
||||||
@@ -339,19 +344,37 @@ impl MessageEvent {
|
|||||||
.and_then(|r| r.content.reason.as_ref());
|
.and_then(|r| r.content.reason.as_ref());
|
||||||
|
|
||||||
if let Some(r) = reason {
|
if let Some(r) = reason {
|
||||||
Cow::Owned(format!("[Redacted: {:?}]", r))
|
Cow::Owned(format!("[Redacted: {r:?}]"))
|
||||||
} else {
|
} else {
|
||||||
Cow::Borrowed("[Redacted]")
|
Cow::Borrowed("[Redacted]")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
MessageEvent::Local(content) => show_room_content(content),
|
MessageEvent::Local(_, content) => body_cow_content(content),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn html(&self) -> Option<StyleTree> {
|
||||||
|
let content = match self {
|
||||||
|
MessageEvent::Original(ev) => &ev.content,
|
||||||
|
MessageEvent::Redacted(_) => return None,
|
||||||
|
MessageEvent::Local(_, content) => content,
|
||||||
|
};
|
||||||
|
|
||||||
|
if let MessageType::Text(content) = &content.msgtype {
|
||||||
|
if let Some(FormattedBody { format: MessageFormat::Html, body }) = &content.formatted {
|
||||||
|
Some(parse_matrix_html(body.as_str()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
||||||
match self {
|
match self {
|
||||||
MessageEvent::Redacted(_) => return,
|
MessageEvent::Redacted(_) => return,
|
||||||
MessageEvent::Local(_) => return,
|
MessageEvent::Local(_, _) => return,
|
||||||
MessageEvent::Original(ev) => {
|
MessageEvent::Original(ev) => {
|
||||||
let redacted = ev.clone().redact(redaction, version);
|
let redacted = ev.clone().redact(redaction, version);
|
||||||
*self = MessageEvent::Redacted(Box::new(redacted));
|
*self = MessageEvent::Redacted(Box::new(redacted));
|
||||||
@@ -360,18 +383,14 @@ impl MessageEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_room_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
||||||
let s = match &content.msgtype {
|
let s = match &content.msgtype {
|
||||||
MessageType::Text(content) => content.body.as_ref(),
|
MessageType::Text(content) => content.body.as_str(),
|
||||||
|
MessageType::VerificationRequest(_) => "[Verification Request]",
|
||||||
MessageType::Emote(content) => content.body.as_ref(),
|
MessageType::Emote(content) => content.body.as_ref(),
|
||||||
MessageType::Notice(content) => content.body.as_str(),
|
MessageType::Notice(content) => content.body.as_str(),
|
||||||
MessageType::ServerNotice(content) => content.body.as_str(),
|
MessageType::ServerNotice(content) => content.body.as_str(),
|
||||||
|
|
||||||
MessageType::VerificationRequest(_) => {
|
|
||||||
// XXX: implement
|
|
||||||
|
|
||||||
return Cow::Owned("[verification request]".into());
|
|
||||||
},
|
|
||||||
MessageType::Audio(content) => {
|
MessageType::Audio(content) => {
|
||||||
return Cow::Owned(format!("[Attached Audio: {}]", content.body));
|
return Cow::Owned(format!("[Attached Audio: {}]", content.body));
|
||||||
},
|
},
|
||||||
@@ -392,37 +411,156 @@ fn show_room_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
|||||||
Cow::Borrowed(s)
|
Cow::Borrowed(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
enum MessageColumns {
|
||||||
|
/// Four columns: sender, message, timestamp, read receipts.
|
||||||
|
Four,
|
||||||
|
|
||||||
|
/// Three columns: sender, message, timestamp.
|
||||||
|
Three,
|
||||||
|
|
||||||
|
/// Two columns: sender, message.
|
||||||
|
Two,
|
||||||
|
|
||||||
|
/// One column: message with sender on line before the message.
|
||||||
|
One,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MessageFormatter<'a> {
|
||||||
|
settings: &'a ApplicationSettings,
|
||||||
|
|
||||||
|
/// How many columns to print.
|
||||||
|
cols: MessageColumns,
|
||||||
|
|
||||||
|
/// The full, original width.
|
||||||
|
orig: usize,
|
||||||
|
|
||||||
|
/// The width that the message contents need to fill.
|
||||||
|
fill: usize,
|
||||||
|
|
||||||
|
/// The formatted Span for the message sender.
|
||||||
|
user: Option<Span<'a>>,
|
||||||
|
|
||||||
|
/// The time the message was sent.
|
||||||
|
time: Option<Span<'a>>,
|
||||||
|
|
||||||
|
/// The date the message was sent.
|
||||||
|
date: Option<Span<'a>>,
|
||||||
|
|
||||||
|
/// Iterator over the users who have read up to this message.
|
||||||
|
read: Iter<'a, OwnedUserId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> MessageFormatter<'a> {
|
||||||
|
fn width(&self) -> usize {
|
||||||
|
self.fill
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn push_spans(&mut self, spans: Spans<'a>, style: Style, text: &mut Text<'a>) {
|
||||||
|
if let Some(date) = self.date.take() {
|
||||||
|
let len = date.content.as_ref().len();
|
||||||
|
let padding = self.orig.saturating_sub(len);
|
||||||
|
let leading = space_span(padding / 2, Style::default());
|
||||||
|
let trailing = space_span(padding.saturating_sub(padding / 2), Style::default());
|
||||||
|
|
||||||
|
text.lines.push(Spans(vec![leading, date, trailing]));
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.cols {
|
||||||
|
MessageColumns::Four => {
|
||||||
|
let settings = self.settings;
|
||||||
|
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
|
||||||
|
let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN);
|
||||||
|
|
||||||
|
let mut line = vec![user];
|
||||||
|
line.extend(spans.0);
|
||||||
|
line.push(time);
|
||||||
|
|
||||||
|
// Show read receipts.
|
||||||
|
let user_char =
|
||||||
|
|user: &'a OwnedUserId| -> Span<'a> { settings.get_user_char_span(user) };
|
||||||
|
|
||||||
|
let a = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
||||||
|
let b = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
||||||
|
let c = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
||||||
|
|
||||||
|
line.push(Span::raw(" "));
|
||||||
|
line.push(c);
|
||||||
|
line.push(b);
|
||||||
|
line.push(a);
|
||||||
|
line.push(Span::raw(" "));
|
||||||
|
|
||||||
|
text.lines.push(Spans(line))
|
||||||
|
},
|
||||||
|
MessageColumns::Three => {
|
||||||
|
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
|
||||||
|
let time = self.time.take().unwrap_or_else(|| Span::from(""));
|
||||||
|
|
||||||
|
let mut line = vec![user];
|
||||||
|
line.extend(spans.0);
|
||||||
|
line.push(time);
|
||||||
|
|
||||||
|
text.lines.push(Spans(line))
|
||||||
|
},
|
||||||
|
MessageColumns::Two => {
|
||||||
|
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
|
||||||
|
let mut line = vec![user];
|
||||||
|
line.extend(spans.0);
|
||||||
|
|
||||||
|
text.lines.push(Spans(line));
|
||||||
|
},
|
||||||
|
MessageColumns::One => {
|
||||||
|
if let Some(user) = self.user.take() {
|
||||||
|
text.lines.push(Spans(vec![user]));
|
||||||
|
}
|
||||||
|
|
||||||
|
let leading = space_span(2, style);
|
||||||
|
let mut line = vec![leading];
|
||||||
|
line.extend(spans.0);
|
||||||
|
|
||||||
|
text.lines.push(Spans(line));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_text(&mut self, append: Text<'a>, style: Style, text: &mut Text<'a>) {
|
||||||
|
for line in append.lines.into_iter() {
|
||||||
|
self.push_spans(line, style, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub event: MessageEvent,
|
pub event: MessageEvent,
|
||||||
pub sender: OwnedUserId,
|
pub sender: OwnedUserId,
|
||||||
pub timestamp: MessageTimeStamp,
|
pub timestamp: MessageTimeStamp,
|
||||||
pub downloaded: bool,
|
pub downloaded: bool,
|
||||||
|
pub html: Option<StyleTree>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
|
pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
|
||||||
Message { event, sender, timestamp, downloaded: false }
|
let html = event.html();
|
||||||
|
let downloaded = false;
|
||||||
|
|
||||||
|
Message { event, sender, timestamp, downloaded, html }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show(
|
pub fn reply_to(&self) -> Option<OwnedEventId> {
|
||||||
&self,
|
let content = match &self.event {
|
||||||
prev: Option<&Message>,
|
MessageEvent::Local(_, content) => content,
|
||||||
selected: bool,
|
MessageEvent::Original(ev) => &ev.content,
|
||||||
vwctx: &ViewportContext<MessageCursor>,
|
MessageEvent::Redacted(_) => return None,
|
||||||
settings: &ApplicationSettings,
|
};
|
||||||
) -> Text {
|
|
||||||
let width = vwctx.get_width();
|
|
||||||
let mut msg = self.event.show();
|
|
||||||
|
|
||||||
if self.downloaded {
|
if let Some(Relation::Reply { in_reply_to }) = &content.relates_to {
|
||||||
msg.to_mut().push_str(" \u{2705}");
|
Some(in_reply_to.event_id.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = msg.as_ref();
|
fn get_render_style(&self, selected: bool) -> Style {
|
||||||
|
|
||||||
let mut lines = vec![];
|
|
||||||
|
|
||||||
let mut style = Style::default();
|
let mut style = Style::default();
|
||||||
|
|
||||||
if selected {
|
if selected {
|
||||||
@@ -433,54 +571,178 @@ impl Message {
|
|||||||
style = style.add_modifier(StyleModifier::ITALIC);
|
style = style.add_modifier(StyleModifier::ITALIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
|
return style;
|
||||||
let lw = width - USER_GUTTER - TIME_GUTTER;
|
|
||||||
|
|
||||||
for (i, (line, w)) in wrap(msg, lw).enumerate() {
|
|
||||||
let line = Span::styled(line.to_string(), style);
|
|
||||||
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
|
|
||||||
|
|
||||||
if i == 0 {
|
|
||||||
let user = self.show_sender(prev, true, settings);
|
|
||||||
|
|
||||||
if let Some(time) = self.timestamp.show() {
|
|
||||||
lines.push(Spans(vec![user, line, trailing, time]))
|
|
||||||
} else {
|
|
||||||
lines.push(Spans(vec![user, line, trailing]))
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let space = USER_GUTTER_EMPTY_SPAN;
|
|
||||||
|
|
||||||
lines.push(Spans(vec![space, line, trailing]))
|
fn get_render_format<'a>(
|
||||||
}
|
&'a self,
|
||||||
}
|
prev: Option<&Message>,
|
||||||
} else if USER_GUTTER + MIN_MSG_LEN <= width {
|
width: usize,
|
||||||
let lw = width - USER_GUTTER;
|
info: &'a RoomInfo,
|
||||||
|
settings: &'a ApplicationSettings,
|
||||||
for (i, (line, w)) in wrap(msg, lw).enumerate() {
|
) -> MessageFormatter<'a> {
|
||||||
let line = Span::styled(line.to_string(), style);
|
let orig = width;
|
||||||
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
|
let date = match &prev {
|
||||||
|
Some(prev) if prev.timestamp.same_day(&self.timestamp) => None,
|
||||||
let prefix = if i == 0 {
|
_ => self.timestamp.show_date(),
|
||||||
self.show_sender(prev, true, settings)
|
|
||||||
} else {
|
|
||||||
USER_GUTTER_EMPTY_SPAN
|
|
||||||
};
|
};
|
||||||
|
|
||||||
lines.push(Spans(vec![prefix, line, trailing]))
|
if USER_GUTTER + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
|
||||||
|
settings.tunables.read_receipt_display
|
||||||
|
{
|
||||||
|
let cols = MessageColumns::Four;
|
||||||
|
let fill = width - USER_GUTTER - TIME_GUTTER - READ_GUTTER;
|
||||||
|
let user = self.show_sender(prev, true, settings);
|
||||||
|
let time = self.timestamp.show_time();
|
||||||
|
let read = match info.receipts.get(self.event.event_id()) {
|
||||||
|
Some(read) => read.iter(),
|
||||||
|
None => [].iter(),
|
||||||
|
};
|
||||||
|
|
||||||
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
||||||
|
} else if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
|
||||||
|
let cols = MessageColumns::Three;
|
||||||
|
let fill = width - USER_GUTTER - TIME_GUTTER;
|
||||||
|
let user = self.show_sender(prev, true, settings);
|
||||||
|
let time = self.timestamp.show_time();
|
||||||
|
let read = [].iter();
|
||||||
|
|
||||||
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
||||||
|
} else if USER_GUTTER + MIN_MSG_LEN <= width {
|
||||||
|
let cols = MessageColumns::Two;
|
||||||
|
let fill = width - USER_GUTTER;
|
||||||
|
let user = self.show_sender(prev, true, settings);
|
||||||
|
let time = None;
|
||||||
|
let read = [].iter();
|
||||||
|
|
||||||
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
||||||
|
} else {
|
||||||
|
let cols = MessageColumns::One;
|
||||||
|
let fill = width.saturating_sub(2);
|
||||||
|
let user = self.show_sender(prev, false, settings);
|
||||||
|
let time = None;
|
||||||
|
let read = [].iter();
|
||||||
|
|
||||||
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show<'a>(
|
||||||
|
&'a self,
|
||||||
|
prev: Option<&Message>,
|
||||||
|
selected: bool,
|
||||||
|
vwctx: &ViewportContext<MessageCursor>,
|
||||||
|
info: &'a RoomInfo,
|
||||||
|
settings: &'a ApplicationSettings,
|
||||||
|
) -> Text<'a> {
|
||||||
|
let width = vwctx.get_width();
|
||||||
|
|
||||||
|
let style = self.get_render_style(selected);
|
||||||
|
let mut fmt = self.get_render_format(prev, width, info, settings);
|
||||||
|
let mut text = Text { lines: vec![] };
|
||||||
|
let width = fmt.width();
|
||||||
|
|
||||||
|
// Show the message that this one replied to, if any.
|
||||||
|
let reply = self.reply_to().and_then(|e| info.get_event(&e));
|
||||||
|
|
||||||
|
if let Some(r) = &reply {
|
||||||
|
let w = width.saturating_sub(2);
|
||||||
|
let mut replied = r.show_msg(w, style, true);
|
||||||
|
let mut sender = r.sender_span(settings);
|
||||||
|
let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
|
||||||
|
let trailing = w.saturating_sub(sender_width + 1);
|
||||||
|
|
||||||
|
sender.style = sender.style.patch(style);
|
||||||
|
|
||||||
|
fmt.push_spans(
|
||||||
|
Spans(vec![
|
||||||
|
Span::styled(" ", style),
|
||||||
|
Span::styled(THICK_VERTICAL, style),
|
||||||
|
sender,
|
||||||
|
Span::styled(":", style),
|
||||||
|
space_span(trailing, style),
|
||||||
|
]),
|
||||||
|
style,
|
||||||
|
&mut text,
|
||||||
|
);
|
||||||
|
|
||||||
|
for line in replied.lines.iter_mut() {
|
||||||
|
line.0.insert(0, Span::styled(THICK_VERTICAL, style));
|
||||||
|
line.0.insert(0, Span::styled(" ", style));
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.push_text(replied, style, &mut text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now show the message contents, and the inlined reply if we couldn't find it above.
|
||||||
|
let msg = self.show_msg(width, style, reply.is_some());
|
||||||
|
fmt.push_text(msg, style, &mut text);
|
||||||
|
|
||||||
|
if text.lines.is_empty() {
|
||||||
|
// If there was nothing in the body, just show an empty message.
|
||||||
|
fmt.push_spans(space_span(width, style).into(), style, &mut text);
|
||||||
|
}
|
||||||
|
|
||||||
|
if settings.tunables.reaction_display {
|
||||||
|
let mut emojis = printer::TextPrinter::new(width, style, false);
|
||||||
|
let mut reactions = 0;
|
||||||
|
|
||||||
|
for (key, count) in info.get_reactions(self.event.event_id()).into_iter() {
|
||||||
|
if reactions != 0 {
|
||||||
|
emojis.push_str(" ", style);
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = if settings.tunables.reaction_shortcode_display {
|
||||||
|
if let Some(emoji) = emojis::get(key) {
|
||||||
|
if let Some(short) = emoji.shortcode() {
|
||||||
|
short
|
||||||
|
} else {
|
||||||
|
// No ASCII shortcode name to show.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if key.chars().all(|c| c.is_ascii_alphanumeric()) {
|
||||||
|
key
|
||||||
|
} else {
|
||||||
|
// Not an Emoji or a printable ASCII string.
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lines.push(Spans::from(self.show_sender(prev, false, settings)));
|
key
|
||||||
|
};
|
||||||
|
|
||||||
for (line, _) in wrap(msg, width.saturating_sub(2)) {
|
emojis.push_str("[", style);
|
||||||
let line = format!(" {}", line);
|
emojis.push_str(name, style);
|
||||||
let line = Span::styled(line, style);
|
emojis.push_str(" ", style);
|
||||||
|
emojis.push_span_nobreak(Span::styled(count.to_string(), style));
|
||||||
|
emojis.push_str("]", style);
|
||||||
|
|
||||||
lines.push(Spans(vec![line]))
|
reactions += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if reactions > 0 {
|
||||||
|
fmt.push_text(emojis.finish(), style, &mut text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Text { lines };
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_msg(&self, width: usize, style: Style, hide_reply: bool) -> Text {
|
||||||
|
if let Some(html) = &self.html {
|
||||||
|
html.to_text(width, style, hide_reply)
|
||||||
|
} else {
|
||||||
|
let mut msg = self.event.body();
|
||||||
|
|
||||||
|
if self.downloaded {
|
||||||
|
msg.to_mut().push_str(" \u{2705}");
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapped_text(msg, width, style)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sender_span(&self, settings: &ApplicationSettings) -> Span {
|
||||||
|
settings.get_user_span(self.sender.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_sender(
|
fn show_sender(
|
||||||
@@ -488,14 +750,14 @@ impl Message {
|
|||||||
prev: Option<&Message>,
|
prev: Option<&Message>,
|
||||||
align_right: bool,
|
align_right: bool,
|
||||||
settings: &ApplicationSettings,
|
settings: &ApplicationSettings,
|
||||||
) -> Span {
|
) -> Option<Span> {
|
||||||
let user = if matches!(prev, Some(prev) if self.sender == prev.sender) {
|
if let Some(prev) = prev {
|
||||||
USER_GUTTER_EMPTY_SPAN
|
if self.sender == prev.sender && self.timestamp.same_day(&prev.timestamp) {
|
||||||
} else {
|
return None;
|
||||||
settings.get_user_span(self.sender.as_ref())
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
let Span { content, style } = user;
|
let Span { content, style } = self.sender_span(settings);
|
||||||
let stop = content.len().min(28);
|
let stop = content.len().min(28);
|
||||||
let s = &content[..stop];
|
let s = &content[..stop];
|
||||||
|
|
||||||
@@ -505,7 +767,7 @@ impl Message {
|
|||||||
format!("{: <width$} ", s, width = 28)
|
format!("{: <width$} ", s, width = 28)
|
||||||
};
|
};
|
||||||
|
|
||||||
Span::styled(sender, style)
|
Span::styled(sender, style).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -540,7 +802,7 @@ impl From<RoomMessageEvent> for Message {
|
|||||||
|
|
||||||
impl ToString for Message {
|
impl ToString for Message {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
self.event.show().into_owned()
|
self.event.body().into_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,47 +811,6 @@ pub mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::tests::*;
|
use crate::tests::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_wrapped_lines_ascii() {
|
|
||||||
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
|
|
||||||
|
|
||||||
let mut iter = wrap(s, 100);
|
|
||||||
assert_eq!(iter.next(), Some(("hello world!", 12)));
|
|
||||||
assert_eq!(iter.next(), Some(("abcdefghijklmnopqrstuvwxyz", 26)));
|
|
||||||
assert_eq!(iter.next(), Some(("goodbye", 7)));
|
|
||||||
assert_eq!(iter.next(), None);
|
|
||||||
|
|
||||||
let mut iter = wrap(s, 5);
|
|
||||||
assert_eq!(iter.next(), Some(("hello", 5)));
|
|
||||||
assert_eq!(iter.next(), Some((" worl", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("d!", 2)));
|
|
||||||
assert_eq!(iter.next(), Some(("abcde", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("fghij", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("klmno", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("pqrst", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("uvwxy", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("z", 1)));
|
|
||||||
assert_eq!(iter.next(), Some(("goodb", 5)));
|
|
||||||
assert_eq!(iter.next(), Some(("ye", 2)));
|
|
||||||
assert_eq!(iter.next(), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_wrapped_lines_unicode() {
|
|
||||||
let s = "CHICKEN";
|
|
||||||
|
|
||||||
let mut iter = wrap(s, 14);
|
|
||||||
assert_eq!(iter.next(), Some((s, 14)));
|
|
||||||
assert_eq!(iter.next(), None);
|
|
||||||
|
|
||||||
let mut iter = wrap(s, 5);
|
|
||||||
assert_eq!(iter.next(), Some(("CH", 4)));
|
|
||||||
assert_eq!(iter.next(), Some(("IC", 4)));
|
|
||||||
assert_eq!(iter.next(), Some(("KE", 4)));
|
|
||||||
assert_eq!(iter.next(), Some(("N", 2)));
|
|
||||||
assert_eq!(iter.next(), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mc_cmp() {
|
fn test_mc_cmp() {
|
||||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||||
207
src/message/printer.rs
Normal file
207
src/message/printer.rs
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
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};
|
||||||
|
|
||||||
|
pub struct TextPrinter<'a> {
|
||||||
|
text: Text<'a>,
|
||||||
|
width: usize,
|
||||||
|
base_style: Style,
|
||||||
|
hide_reply: bool,
|
||||||
|
|
||||||
|
alignment: Alignment,
|
||||||
|
curr_spans: Vec<Span<'a>>,
|
||||||
|
curr_width: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextPrinter<'a> {
|
||||||
|
pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
|
||||||
|
TextPrinter {
|
||||||
|
text: Text::default(),
|
||||||
|
width,
|
||||||
|
base_style,
|
||||||
|
hide_reply,
|
||||||
|
|
||||||
|
alignment: Alignment::Left,
|
||||||
|
curr_spans: vec![],
|
||||||
|
curr_width: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn align(mut self, alignment: Alignment) -> Self {
|
||||||
|
self.alignment = alignment;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hide_reply(&self) -> bool {
|
||||||
|
self.hide_reply
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn width(&self) -> usize {
|
||||||
|
self.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sub(&self, indent: usize) -> Self {
|
||||||
|
TextPrinter {
|
||||||
|
text: Text::default(),
|
||||||
|
width: self.width.saturating_sub(indent),
|
||||||
|
base_style: self.base_style,
|
||||||
|
hide_reply: self.hide_reply,
|
||||||
|
|
||||||
|
alignment: self.alignment,
|
||||||
|
curr_spans: vec![],
|
||||||
|
curr_width: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remaining(&self) -> usize {
|
||||||
|
self.width - self.curr_width
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn commit(&mut self) {
|
||||||
|
if self.curr_width > 0 {
|
||||||
|
self.push_break();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push(&mut self) {
|
||||||
|
self.curr_width = 0;
|
||||||
|
self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_break(&mut self) {
|
||||||
|
if self.curr_width == 0 && self.text.lines.is_empty() {
|
||||||
|
// Disallow leading breaks.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let remaining = self.remaining();
|
||||||
|
|
||||||
|
if remaining > 0 {
|
||||||
|
match self.alignment {
|
||||||
|
Alignment::Left => {
|
||||||
|
let tspan = space_span(remaining, self.base_style);
|
||||||
|
self.curr_spans.push(tspan);
|
||||||
|
},
|
||||||
|
Alignment::Center => {
|
||||||
|
let trailing = remaining / 2;
|
||||||
|
let leading = remaining - trailing;
|
||||||
|
|
||||||
|
let tspan = space_span(trailing, self.base_style);
|
||||||
|
let lspan = space_span(leading, self.base_style);
|
||||||
|
|
||||||
|
self.curr_spans.push(tspan);
|
||||||
|
self.curr_spans.insert(0, lspan);
|
||||||
|
},
|
||||||
|
Alignment::Right => {
|
||||||
|
let lspan = space_span(remaining, self.base_style);
|
||||||
|
self.curr_spans.insert(0, lspan);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.push();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_str_wrapped<T>(&mut self, s: T, style: Style)
|
||||||
|
where
|
||||||
|
T: Into<Cow<'a, str>>,
|
||||||
|
{
|
||||||
|
let style = self.base_style.patch(style);
|
||||||
|
let mut cow = s.into();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sw = UnicodeWidthStr::width(cow.as_ref());
|
||||||
|
|
||||||
|
if self.curr_width + sw <= self.width {
|
||||||
|
// The text fits within the current line.
|
||||||
|
self.curr_spans.push(Span::styled(cow, style));
|
||||||
|
self.curr_width += sw;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take a leading portion of the text that fits in the line.
|
||||||
|
let ((s0, w), s1) = take_width(cow, self.remaining());
|
||||||
|
cow = s1;
|
||||||
|
|
||||||
|
self.curr_spans.push(Span::styled(s0, style));
|
||||||
|
self.curr_width += w;
|
||||||
|
|
||||||
|
self.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.curr_width == self.width {
|
||||||
|
// If the last bit fills the full line, start a new one.
|
||||||
|
self.push();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_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);
|
||||||
|
|
||||||
|
for word in UnicodeSegmentation::split_word_bounds(s) {
|
||||||
|
if self.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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_text(&mut self, text: Text<'a>) {
|
||||||
|
self.commit();
|
||||||
|
self.text.lines.extend(text.lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(mut self) -> Text<'a> {
|
||||||
|
self.commit();
|
||||||
|
self.text
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/tests.rs
47
src/tests.rs
@@ -15,13 +15,15 @@ use matrix_sdk::ruma::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use modalkit::tui::style::Color;
|
use modalkit::tui::style::{Color, Style};
|
||||||
use tokio::sync::mpsc::unbounded_channel;
|
use tokio::sync::mpsc::unbounded_channel;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo},
|
base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo},
|
||||||
config::{
|
config::{
|
||||||
|
user_color,
|
||||||
|
user_style_from_color,
|
||||||
ApplicationSettings,
|
ApplicationSettings,
|
||||||
DirectoryValues,
|
DirectoryValues,
|
||||||
ProfileConfig,
|
ProfileConfig,
|
||||||
@@ -60,6 +62,10 @@ lazy_static! {
|
|||||||
pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
|
pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn user_style(user: &str) -> Style {
|
||||||
|
user_style_from_color(user_color(user))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn mock_room1_message(
|
pub fn mock_room1_message(
|
||||||
content: RoomMessageEventContent,
|
content: RoomMessageEventContent,
|
||||||
sender: OwnedUserId,
|
sender: OwnedUserId,
|
||||||
@@ -82,7 +88,7 @@ pub fn mock_room1_message(
|
|||||||
|
|
||||||
pub fn mock_message1() -> Message {
|
pub fn mock_message1() -> Message {
|
||||||
let content = RoomMessageEventContent::text_plain("writhe");
|
let content = RoomMessageEventContent::text_plain("writhe");
|
||||||
let content = MessageEvent::Local(content.into());
|
let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
|
||||||
|
|
||||||
Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
|
Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
|
||||||
}
|
}
|
||||||
@@ -111,6 +117,18 @@ pub fn mock_message5() -> Message {
|
|||||||
mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
|
mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
|
||||||
|
let mut keys = HashMap::new();
|
||||||
|
|
||||||
|
keys.insert(MSG1_EVID.clone(), EventLocation::Message(MSG1_KEY.clone()));
|
||||||
|
keys.insert(MSG2_EVID.clone(), EventLocation::Message(MSG2_KEY.clone()));
|
||||||
|
keys.insert(MSG3_EVID.clone(), EventLocation::Message(MSG3_KEY.clone()));
|
||||||
|
keys.insert(MSG4_EVID.clone(), EventLocation::Message(MSG4_KEY.clone()));
|
||||||
|
keys.insert(MSG5_EVID.clone(), EventLocation::Message(MSG5_KEY.clone()));
|
||||||
|
|
||||||
|
keys
|
||||||
|
}
|
||||||
|
|
||||||
pub fn mock_messages() -> Messages {
|
pub fn mock_messages() -> Messages {
|
||||||
let mut messages = BTreeMap::new();
|
let mut messages = BTreeMap::new();
|
||||||
|
|
||||||
@@ -126,7 +144,15 @@ pub fn mock_messages() -> Messages {
|
|||||||
pub fn mock_room() -> RoomInfo {
|
pub fn mock_room() -> RoomInfo {
|
||||||
RoomInfo {
|
RoomInfo {
|
||||||
name: Some("Watercooler Discussion".into()),
|
name: Some("Watercooler Discussion".into()),
|
||||||
|
tags: None,
|
||||||
|
|
||||||
|
keys: mock_keys(),
|
||||||
messages: mock_messages(),
|
messages: mock_messages(),
|
||||||
|
|
||||||
|
receipts: HashMap::new(),
|
||||||
|
read_till: None,
|
||||||
|
reactions: HashMap::new(),
|
||||||
|
|
||||||
fetch_id: RoomFetchStatus::NotStarted,
|
fetch_id: RoomFetchStatus::NotStarted,
|
||||||
fetch_last: None,
|
fetch_last: None,
|
||||||
users_typing: None,
|
users_typing: None,
|
||||||
@@ -143,7 +169,12 @@ pub fn mock_dirs() -> DirectoryValues {
|
|||||||
|
|
||||||
pub fn mock_tunables() -> TunableValues {
|
pub fn mock_tunables() -> TunableValues {
|
||||||
TunableValues {
|
TunableValues {
|
||||||
typing_notice: true,
|
default_room: None,
|
||||||
|
reaction_display: true,
|
||||||
|
reaction_shortcode_display: false,
|
||||||
|
read_receipt_send: true,
|
||||||
|
read_receipt_display: true,
|
||||||
|
typing_notice_send: true,
|
||||||
typing_notice_display: true,
|
typing_notice_display: true,
|
||||||
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
||||||
color: Some(UserColor(Color::Black)),
|
color: Some(UserColor(Color::Black)),
|
||||||
@@ -177,6 +208,14 @@ pub async fn mock_store() -> ProgramStore {
|
|||||||
let worker = Requester { tx, client };
|
let worker = Requester { tx, client };
|
||||||
|
|
||||||
let mut store = ChatStore::new(worker, mock_settings());
|
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 room_id = TEST_ROOM1_ID.clone();
|
||||||
let info = mock_room();
|
let info = mock_room();
|
||||||
|
|
||||||
|
|||||||
191
src/util.rs
Normal file
191
src/util.rs
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
|
use modalkit::tui::style::Style;
|
||||||
|
use modalkit::tui::text::{Span, Spans, Text};
|
||||||
|
|
||||||
|
pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>) {
|
||||||
|
match cow {
|
||||||
|
Cow::Borrowed(s) => {
|
||||||
|
let s1 = Cow::Borrowed(&s[idx..]);
|
||||||
|
let s0 = Cow::Borrowed(&s[..idx]);
|
||||||
|
|
||||||
|
(s0, s1)
|
||||||
|
},
|
||||||
|
Cow::Owned(mut s) => {
|
||||||
|
let s1 = Cow::Owned(s.split_off(idx));
|
||||||
|
let s0 = Cow::Owned(s);
|
||||||
|
|
||||||
|
(s0, s1)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
|
||||||
|
// Find where to split the line.
|
||||||
|
let mut idx = 0;
|
||||||
|
let mut w = 0;
|
||||||
|
|
||||||
|
for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) {
|
||||||
|
let gw = UnicodeWidthStr::width(g);
|
||||||
|
idx = i;
|
||||||
|
|
||||||
|
if w + gw > width {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
w += gw;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (s0, s1) = split_cow(s, idx);
|
||||||
|
|
||||||
|
((s0, w), s1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WrappedLinesIterator<'a> {
|
||||||
|
iter: std::vec::IntoIter<Cow<'a, str>>,
|
||||||
|
curr: Option<Cow<'a, str>>,
|
||||||
|
width: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> WrappedLinesIterator<'a> {
|
||||||
|
fn new<T>(input: T, width: usize) -> Self
|
||||||
|
where
|
||||||
|
T: Into<Cow<'a, str>>,
|
||||||
|
{
|
||||||
|
let width = width.max(2);
|
||||||
|
|
||||||
|
let cows: Vec<Cow<'a, str>> = match input.into() {
|
||||||
|
Cow::Borrowed(s) => s.lines().map(Cow::Borrowed).collect(),
|
||||||
|
Cow::Owned(s) => s.lines().map(ToOwned::to_owned).map(Cow::Owned).collect(),
|
||||||
|
};
|
||||||
|
|
||||||
|
WrappedLinesIterator { iter: cows.into_iter(), curr: None, width }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for WrappedLinesIterator<'a> {
|
||||||
|
type Item = (Cow<'a, str>, usize);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.curr.is_none() {
|
||||||
|
self.curr = self.iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(s) = self.curr.take() {
|
||||||
|
let width = UnicodeWidthStr::width(s.as_ref());
|
||||||
|
|
||||||
|
if width <= self.width {
|
||||||
|
return Some((s, width));
|
||||||
|
} else {
|
||||||
|
let (prefix, s1) = take_width(s, self.width);
|
||||||
|
self.curr = Some(s1);
|
||||||
|
return Some(prefix);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wrap<'a, T>(input: T, width: usize) -> WrappedLinesIterator<'a>
|
||||||
|
where
|
||||||
|
T: Into<Cow<'a, str>>,
|
||||||
|
{
|
||||||
|
WrappedLinesIterator::new(input, width)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wrapped_text<'a, T>(s: T, width: usize, style: Style) -> Text<'a>
|
||||||
|
where
|
||||||
|
T: Into<Cow<'a, str>>,
|
||||||
|
{
|
||||||
|
let mut text = Text::default();
|
||||||
|
|
||||||
|
for (line, w) in wrap(s, width) {
|
||||||
|
let space = space_span(width.saturating_sub(w), style);
|
||||||
|
let spans = Spans(vec![Span::styled(line, style), space]);
|
||||||
|
|
||||||
|
text.lines.push(spans);
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn space(width: usize) -> String {
|
||||||
|
" ".repeat(width)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn space_span(width: usize, style: Style) -> Span<'static> {
|
||||||
|
Span::styled(space(width), style)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn space_text(width: usize, style: Style) -> Text<'static> {
|
||||||
|
space_span(width, style).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn join_cell_text<'a>(texts: Vec<(Text<'a>, usize)>, join: Span<'a>, style: Style) -> Text<'a> {
|
||||||
|
let height = texts.iter().map(|t| t.0.height()).max().unwrap_or(0);
|
||||||
|
let mut text = Text { lines: vec![Spans(vec![join.clone()]); height] };
|
||||||
|
|
||||||
|
for (mut t, w) in texts.into_iter() {
|
||||||
|
for i in 0..height {
|
||||||
|
if let Some(spans) = t.lines.get_mut(i) {
|
||||||
|
text.lines[i].0.append(&mut spans.0);
|
||||||
|
} else {
|
||||||
|
text.lines[i].0.push(space_span(w, style));
|
||||||
|
}
|
||||||
|
|
||||||
|
text.lines[i].0.push(join.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
text
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_wrapped_lines_ascii() {
|
||||||
|
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
|
||||||
|
|
||||||
|
let mut iter = wrap(s, 100);
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("hello world!"), 12)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("abcdefghijklmnopqrstuvwxyz"), 26)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("goodbye"), 7)));
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
|
||||||
|
let mut iter = wrap(s, 5);
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("hello"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed(" worl"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("d!"), 2)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("abcde"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("fghij"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("klmno"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("pqrst"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("uvwxy"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("z"), 1)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("goodb"), 5)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("ye"), 2)));
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_wrapped_lines_unicode() {
|
||||||
|
let s = "CHICKEN";
|
||||||
|
|
||||||
|
let mut iter = wrap(s, 14);
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed(s), 14)));
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
|
||||||
|
let mut iter = wrap(s, 5);
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("CH"), 4)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("IC"), 4)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("KE"), 4)));
|
||||||
|
assert_eq!(iter.next(), Some((Cow::Borrowed("N"), 2)));
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||||
use std::collections::hash_map::Entry;
|
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
encryption::verification::{format_emojis, SasVerification},
|
encryption::verification::{format_emojis, SasVerification},
|
||||||
room::{Room as MatrixRoom, RoomMember},
|
room::{Room as MatrixRoom, RoomMember},
|
||||||
ruma::{events::room::member::MembershipState, OwnedRoomId, RoomId},
|
ruma::{
|
||||||
|
events::room::member::MembershipState,
|
||||||
|
events::tag::{TagName, Tags},
|
||||||
|
OwnedRoomId,
|
||||||
|
RoomId,
|
||||||
|
},
|
||||||
DisplayName,
|
DisplayName,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,7 +44,9 @@ use modalkit::{
|
|||||||
ScrollStyle,
|
ScrollStyle,
|
||||||
ViewportContext,
|
ViewportContext,
|
||||||
WordStyle,
|
WordStyle,
|
||||||
|
WriteFlags,
|
||||||
},
|
},
|
||||||
|
completion::CompletionList,
|
||||||
},
|
},
|
||||||
widgets::{
|
widgets::{
|
||||||
list::{List, ListCursor, ListItem, ListState},
|
list::{List, ListCursor, ListItem, ListState},
|
||||||
@@ -119,6 +125,57 @@ fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering {
|
|||||||
ord.then_with(|| a.room_id().cmp(b.room_id()))
|
ord.then_with(|| a.room_id().cmp(b.room_id()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering {
|
||||||
|
let (fava, lowa) = a
|
||||||
|
.as_ref()
|
||||||
|
.map(|tags| {
|
||||||
|
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
|
||||||
|
})
|
||||||
|
.unwrap_or((false, false));
|
||||||
|
|
||||||
|
let (favb, lowb) = b
|
||||||
|
.as_ref()
|
||||||
|
.map(|tags| {
|
||||||
|
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
|
||||||
|
})
|
||||||
|
.unwrap_or((false, false));
|
||||||
|
|
||||||
|
// If a has Favorite and b doesn't, it should sort earlier in room list.
|
||||||
|
let cmpf = favb.cmp(&fava);
|
||||||
|
|
||||||
|
// If a has LowPriority and b doesn't, it should sort later in room list.
|
||||||
|
let cmpl = lowa.cmp(&lowb);
|
||||||
|
|
||||||
|
cmpl.then(cmpf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) {
|
||||||
|
if tags.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
spans.push(Span::styled(" (", style));
|
||||||
|
|
||||||
|
for (i, tag) in tags.keys().enumerate() {
|
||||||
|
if i > 0 {
|
||||||
|
spans.push(Span::styled(", ", style));
|
||||||
|
}
|
||||||
|
|
||||||
|
match tag {
|
||||||
|
TagName::Favorite => spans.push(Span::styled("Favorite", style)),
|
||||||
|
TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
|
||||||
|
TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
|
||||||
|
TagName::User(tag) => {
|
||||||
|
spans.push(Span::styled("User Tag: ", style));
|
||||||
|
spans.push(Span::styled(tag.as_ref(), style));
|
||||||
|
},
|
||||||
|
tag => spans.push(Span::styled(format!("{tag:?}"), style)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spans.push(Span::styled(")", style));
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn room_prompt(
|
fn room_prompt(
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
@@ -321,8 +378,13 @@ impl WindowOps<IambInfo> for IambWindow {
|
|||||||
IambWindow::Room(state) => state.draw(area, buf, focused, store),
|
IambWindow::Room(state) => state.draw(area, buf, focused, store),
|
||||||
IambWindow::DirectList(state) => {
|
IambWindow::DirectList(state) => {
|
||||||
let dms = store.application.worker.direct_messages();
|
let dms = store.application.worker.direct_messages();
|
||||||
let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store));
|
let mut items = dms
|
||||||
state.set(items.collect());
|
.into_iter()
|
||||||
|
.map(|(id, name, tags)| DirectItem::new(id, name, tags, store))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
items.sort();
|
||||||
|
|
||||||
|
state.set(items);
|
||||||
|
|
||||||
List::new(store)
|
List::new(store)
|
||||||
.empty_message("No direct messages yet!")
|
.empty_message("No direct messages yet!")
|
||||||
@@ -346,7 +408,7 @@ impl WindowOps<IambInfo> for IambWindow {
|
|||||||
let joined = store.application.worker.active_rooms();
|
let joined = store.application.worker.active_rooms();
|
||||||
let mut items = joined
|
let mut items = joined
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(room, name)| RoomItem::new(room, name, store))
|
.map(|(room, name, tags)| RoomItem::new(room, name, tags, store))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
items.sort();
|
items.sort();
|
||||||
|
|
||||||
@@ -408,6 +470,19 @@ impl WindowOps<IambInfo> for IambWindow {
|
|||||||
delegate!(self, w => w.close(flags, store))
|
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> {
|
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||||
delegate!(self, w => w.get_cursor_word(style))
|
delegate!(self, w => w.get_cursor_word(style))
|
||||||
}
|
}
|
||||||
@@ -471,8 +546,8 @@ impl Window<IambInfo> for IambWindow {
|
|||||||
fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
|
fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
|
||||||
match id {
|
match id {
|
||||||
IambId::Room(room_id) => {
|
IambId::Room(room_id) => {
|
||||||
let (room, name) = store.application.worker.get_room(room_id)?;
|
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||||
let room = RoomState::new(room, name, store);
|
let room = RoomState::new(room, name, tags, store);
|
||||||
|
|
||||||
return Ok(room.into());
|
return Ok(room.into());
|
||||||
},
|
},
|
||||||
@@ -514,26 +589,23 @@ impl Window<IambInfo> for IambWindow {
|
|||||||
fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> {
|
fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> {
|
||||||
let ChatStore { names, worker, .. } = &mut store.application;
|
let ChatStore { names, worker, .. } = &mut store.application;
|
||||||
|
|
||||||
match names.entry(name) {
|
if let Some(room) = names.get_mut(&name) {
|
||||||
Entry::Vacant(v) => {
|
let id = IambId::Room(room.clone());
|
||||||
let room_id = worker.join_room(v.key().to_string())?;
|
|
||||||
v.insert(room_id.clone());
|
|
||||||
|
|
||||||
let (room, name) = store.application.worker.get_room(room_id)?;
|
|
||||||
let room = RoomState::new(room, name, store);
|
|
||||||
|
|
||||||
Ok(room.into())
|
|
||||||
},
|
|
||||||
Entry::Occupied(o) => {
|
|
||||||
let id = IambId::Room(o.get().clone());
|
|
||||||
|
|
||||||
IambWindow::open(id, store)
|
IambWindow::open(id, store)
|
||||||
},
|
} else {
|
||||||
|
let room_id = worker.join_room(name.clone())?;
|
||||||
|
names.insert(name, room_id.clone());
|
||||||
|
|
||||||
|
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||||
|
let room = RoomState::new(room, name, tags, store);
|
||||||
|
|
||||||
|
Ok(room.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> {
|
fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> {
|
||||||
let msg = format!("Cannot find indexed buffer (index = {})", index);
|
let msg = format!("Cannot find indexed buffer (index = {index})");
|
||||||
let err = UIError::Unimplemented(msg);
|
let err = UIError::Unimplemented(msg);
|
||||||
|
|
||||||
Err(err)
|
Err(err)
|
||||||
@@ -547,16 +619,29 @@ impl Window<IambInfo> for IambWindow {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RoomItem {
|
pub struct RoomItem {
|
||||||
room: MatrixRoom,
|
room: MatrixRoom,
|
||||||
|
tags: Option<Tags>,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RoomItem {
|
impl RoomItem {
|
||||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
fn new(
|
||||||
|
room: MatrixRoom,
|
||||||
|
name: DisplayName,
|
||||||
|
tags: Option<Tags>,
|
||||||
|
store: &mut ProgramStore,
|
||||||
|
) -> Self {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
|
let room_id = room.room_id();
|
||||||
|
|
||||||
store.application.set_room_name(room.room_id(), name.as_str());
|
let info = store.application.get_room_info(room_id.to_owned());
|
||||||
|
info.name = name.clone().into();
|
||||||
|
info.tags = tags.clone();
|
||||||
|
|
||||||
RoomItem { room, name }
|
if let Some(alias) = room.canonical_alias() {
|
||||||
|
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
|
RoomItem { room, tags, name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,7 +655,7 @@ impl Eq for RoomItem {}
|
|||||||
|
|
||||||
impl Ord for RoomItem {
|
impl Ord for RoomItem {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
room_cmp(&self.room, &other.room)
|
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,8 +673,17 @@ impl ToString for RoomItem {
|
|||||||
|
|
||||||
impl ListItem<IambInfo> for RoomItem {
|
impl ListItem<IambInfo> for RoomItem {
|
||||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||||
|
if let Some(tags) = &self.tags {
|
||||||
|
let style = selected_style(selected);
|
||||||
|
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||||
|
|
||||||
|
append_tags(tags, &mut spans, style);
|
||||||
|
|
||||||
|
Text::from(Spans(spans))
|
||||||
|
} else {
|
||||||
selected_text(self.name.as_str(), selected)
|
selected_text(self.name.as_str(), selected)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_word(&self) -> Option<String> {
|
fn get_word(&self) -> Option<String> {
|
||||||
self.room.room_id().to_string().into()
|
self.room.room_id().to_string().into()
|
||||||
@@ -610,16 +704,22 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DirectItem {
|
pub struct DirectItem {
|
||||||
room: MatrixRoom,
|
room: MatrixRoom,
|
||||||
|
tags: Option<Tags>,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirectItem {
|
impl DirectItem {
|
||||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
fn new(
|
||||||
|
room: MatrixRoom,
|
||||||
|
name: DisplayName,
|
||||||
|
tags: Option<Tags>,
|
||||||
|
store: &mut ProgramStore,
|
||||||
|
) -> Self {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
|
|
||||||
store.application.set_room_name(room.room_id(), name.as_str());
|
store.application.set_room_name(room.room_id(), name.as_str());
|
||||||
|
|
||||||
DirectItem { room, name }
|
DirectItem { room, tags, name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -631,14 +731,43 @@ impl ToString for DirectItem {
|
|||||||
|
|
||||||
impl ListItem<IambInfo> for DirectItem {
|
impl ListItem<IambInfo> for DirectItem {
|
||||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||||
|
if let Some(tags) = &self.tags {
|
||||||
|
let style = selected_style(selected);
|
||||||
|
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||||
|
|
||||||
|
append_tags(tags, &mut spans, style);
|
||||||
|
|
||||||
|
Text::from(Spans(spans))
|
||||||
|
} else {
|
||||||
selected_text(self.name.as_str(), selected)
|
selected_text(self.name.as_str(), selected)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_word(&self) -> Option<String> {
|
fn get_word(&self) -> Option<String> {
|
||||||
self.room.room_id().to_string().into()
|
self.room.room_id().to_string().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for DirectItem {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.room.room_id() == other.room.room_id()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for DirectItem {}
|
||||||
|
|
||||||
|
impl Ord for DirectItem {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for DirectItem {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
self.cmp(other).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
|
impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
|
||||||
fn prompt(
|
fn prompt(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -659,8 +788,13 @@ pub struct SpaceItem {
|
|||||||
impl SpaceItem {
|
impl SpaceItem {
|
||||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
|
let room_id = room.room_id();
|
||||||
|
|
||||||
store.application.set_room_name(room.room_id(), name.as_str());
|
store.application.set_room_name(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 }
|
SpaceItem { room, name }
|
||||||
}
|
}
|
||||||
@@ -739,7 +873,7 @@ impl VerifyItem {
|
|||||||
let device = self.sasv1.other_device();
|
let device = self.sasv1.other_device();
|
||||||
|
|
||||||
if let Some(display_name) = device.display_name() {
|
if let Some(display_name) = device.display_name() {
|
||||||
format!("Device verification with {} ({})", display_name, state)
|
format!("Device verification with {display_name} ({state})")
|
||||||
} else {
|
} else {
|
||||||
format!("Device verification with device {} ({})", device.device_id(), state)
|
format!("Device verification with device {} ({})", device.device_id(), state)
|
||||||
}
|
}
|
||||||
@@ -845,7 +979,7 @@ impl ListItem<IambInfo> for VerifyItem {
|
|||||||
lines.push(Spans::from(""));
|
lines.push(Spans::from(""));
|
||||||
|
|
||||||
for line in format_emojis(emoji).lines() {
|
for line in format_emojis(emoji).lines() {
|
||||||
lines.push(Spans::from(format!(" {}", line)));
|
lines.push(Spans::from(format!(" {line}")));
|
||||||
}
|
}
|
||||||
|
|
||||||
lines.push(Spans::from(""));
|
lines.push(Spans::from(""));
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use tokio;
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
attachment::AttachmentConfig,
|
attachment::AttachmentConfig,
|
||||||
media::{MediaFormat, MediaRequest},
|
media::{MediaFormat, MediaRequest},
|
||||||
room::Room as MatrixRoom,
|
room::{Joined, Room as MatrixRoom},
|
||||||
ruma::{
|
ruma::{
|
||||||
|
events::reaction::{ReactionEventContent, Relation as Reaction},
|
||||||
events::room::message::{
|
events::room::message::{
|
||||||
MessageType,
|
MessageType,
|
||||||
OriginalRoomMessageEvent,
|
OriginalRoomMessageEvent,
|
||||||
|
Relation,
|
||||||
|
Replacement,
|
||||||
RoomMessageEventContent,
|
RoomMessageEventContent,
|
||||||
TextMessageEventContent,
|
TextMessageEventContent,
|
||||||
},
|
},
|
||||||
|
EventId,
|
||||||
OwnedRoomId,
|
OwnedRoomId,
|
||||||
RoomId,
|
RoomId,
|
||||||
},
|
},
|
||||||
@@ -45,13 +52,15 @@ use modalkit::editing::{
|
|||||||
Scrollable,
|
Scrollable,
|
||||||
UIError,
|
UIError,
|
||||||
},
|
},
|
||||||
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle},
|
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle, WriteFlags},
|
||||||
|
completion::CompletionList,
|
||||||
context::Resolve,
|
context::Resolve,
|
||||||
history::{self, HistoryList},
|
history::{self, HistoryList},
|
||||||
rope::EditRope,
|
rope::EditRope,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::base::{
|
use crate::base::{
|
||||||
|
DownloadFlags,
|
||||||
IambAction,
|
IambAction,
|
||||||
IambBufferId,
|
IambBufferId,
|
||||||
IambError,
|
IambError,
|
||||||
@@ -67,6 +76,7 @@ use crate::base::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
|
use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
|
||||||
|
use crate::worker::Requester;
|
||||||
|
|
||||||
use super::scrollback::{Scrollback, ScrollbackState};
|
use super::scrollback::{Scrollback, ScrollbackState};
|
||||||
|
|
||||||
@@ -82,6 +92,7 @@ pub struct ChatState {
|
|||||||
focus: RoomFocus,
|
focus: RoomFocus,
|
||||||
|
|
||||||
reply_to: Option<MessageKey>,
|
reply_to: Option<MessageKey>,
|
||||||
|
editing: Option<MessageKey>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatState {
|
impl ChatState {
|
||||||
@@ -104,9 +115,14 @@ impl ChatState {
|
|||||||
focus: RoomFocus::MessageBar,
|
focus: RoomFocus::MessageBar,
|
||||||
|
|
||||||
reply_to: None,
|
reply_to: None,
|
||||||
|
editing: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_joined(&self, worker: &Requester) -> Result<Joined, IambError> {
|
||||||
|
worker.client.get_joined_room(self.id()).ok_or(IambError::NotJoined)
|
||||||
|
}
|
||||||
|
|
||||||
fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
|
fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
|
||||||
let key = self.reply_to.as_ref()?;
|
let key = self.reply_to.as_ref()?;
|
||||||
let msg = info.messages.get(key)?;
|
let msg = info.messages.get(key)?;
|
||||||
@@ -120,6 +136,7 @@ impl ChatState {
|
|||||||
|
|
||||||
fn reset(&mut self) -> EditRope {
|
fn reset(&mut self) -> EditRope {
|
||||||
self.reply_to = None;
|
self.reply_to = None;
|
||||||
|
self.editing = None;
|
||||||
self.tbox.reset()
|
self.tbox.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,17 +155,21 @@ impl ChatState {
|
|||||||
let client = &store.application.worker.client;
|
let client = &store.application.worker.client;
|
||||||
|
|
||||||
let settings = &store.application.settings;
|
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.get_mut(info).ok_or(IambError::NoSelectedMessage)?;
|
let msg = self
|
||||||
|
.scrollback
|
||||||
|
.get_mut(&mut info.messages)
|
||||||
|
.ok_or(IambError::NoSelectedMessage)?;
|
||||||
|
|
||||||
match act {
|
match act {
|
||||||
MessageAction::Cancel => {
|
MessageAction::Cancel => {
|
||||||
self.reply_to = None;
|
self.reply_to = None;
|
||||||
|
self.editing = None;
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
},
|
},
|
||||||
MessageAction::Download(filename, force) => {
|
MessageAction::Download(filename, flags) => {
|
||||||
if let MessageEvent::Original(ev) = &msg.event {
|
if let MessageEvent::Original(ev) = &msg.event {
|
||||||
let media = client.media();
|
let media = client.media();
|
||||||
|
|
||||||
@@ -195,16 +216,7 @@ impl ChatState {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if !force && filename.exists() {
|
if !filename.exists() || flags.contains(DownloadFlags::FORCE) {
|
||||||
let msg = format!(
|
|
||||||
"The file {} already exists; use :download! to overwrite it.",
|
|
||||||
filename.display()
|
|
||||||
);
|
|
||||||
let err = UIError::Failure(msg);
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
let req = MediaRequest { source, format: MediaFormat::File };
|
let req = MediaRequest { source, format: MediaFormat::File };
|
||||||
|
|
||||||
let bytes =
|
let bytes =
|
||||||
@@ -213,32 +225,98 @@ impl ChatState {
|
|||||||
fs::write(filename.as_path(), bytes.as_slice())?;
|
fs::write(filename.as_path(), bytes.as_slice())?;
|
||||||
|
|
||||||
msg.downloaded = true;
|
msg.downloaded = true;
|
||||||
|
} else if !flags.contains(DownloadFlags::OPEN) {
|
||||||
|
let msg = format!(
|
||||||
|
"The file {} already exists; add ! to end of command to overwrite it.",
|
||||||
|
filename.display()
|
||||||
|
);
|
||||||
|
let err = UIError::Failure(msg);
|
||||||
|
|
||||||
let info = InfoMessage::from(format!(
|
return Err(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
InfoMessage::from(format!(
|
||||||
"Attachment downloaded to {}",
|
"Attachment downloaded to {}",
|
||||||
filename.display()
|
filename.display()
|
||||||
));
|
))
|
||||||
|
};
|
||||||
|
|
||||||
return Ok(info.into());
|
return Ok(info.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(IambError::NoAttachment.into())
|
Err(IambError::NoAttachment.into())
|
||||||
},
|
},
|
||||||
MessageAction::Redact(reason) => {
|
MessageAction::Edit => {
|
||||||
let room = store
|
if msg.sender != settings.profile.user_id {
|
||||||
.application
|
let msg = "Cannot edit messages sent by someone else";
|
||||||
.worker
|
let err = UIError::Failure(msg.into());
|
||||||
.client
|
|
||||||
.get_joined_room(self.id())
|
|
||||||
.ok_or(IambError::NotJoined)?;
|
|
||||||
|
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ev = match &msg.event {
|
||||||
|
MessageEvent::Original(ev) => &ev.content,
|
||||||
|
MessageEvent::Local(_, ev) => ev.deref(),
|
||||||
|
_ => {
|
||||||
|
let msg = "Cannot edit a redacted message";
|
||||||
|
let err = UIError::Failure(msg.into());
|
||||||
|
|
||||||
|
return Err(err);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let text = match &ev.msgtype {
|
||||||
|
MessageType::Text(msg) => msg.body.as_str(),
|
||||||
|
_ => {
|
||||||
|
let msg = "Cannot edit a non-text message";
|
||||||
|
let err = UIError::Failure(msg.into());
|
||||||
|
|
||||||
|
return Err(err);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
self.tbox.set_text(text);
|
||||||
|
self.editing = self.scrollback.get_key(info);
|
||||||
|
self.focus = RoomFocus::MessageBar;
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
},
|
||||||
|
MessageAction::React(emoji) => {
|
||||||
|
let room = self.get_joined(&store.application.worker)?;
|
||||||
let event_id = match &msg.event {
|
let event_id = match &msg.event {
|
||||||
MessageEvent::Original(ev) => ev.event_id.clone(),
|
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||||
MessageEvent::Local(_) => {
|
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||||
self.scrollback.get_key(info).ok_or(IambError::NoSelectedMessage)?.1
|
|
||||||
},
|
|
||||||
MessageEvent::Redacted(_) => {
|
MessageEvent::Redacted(_) => {
|
||||||
let msg = "";
|
let msg = "Cannot react to a redacted message";
|
||||||
|
let err = UIError::Failure(msg.into());
|
||||||
|
|
||||||
|
return Err(err);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let reaction = Reaction::new(event_id, emoji);
|
||||||
|
let msg = ReactionEventContent::new(reaction);
|
||||||
|
let _ = room.send(msg, None).await.map_err(IambError::from)?;
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
},
|
||||||
|
MessageAction::Redact(reason) => {
|
||||||
|
let room = self.get_joined(&store.application.worker)?;
|
||||||
|
let event_id = match &msg.event {
|
||||||
|
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||||
|
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||||
|
MessageEvent::Redacted(_) => {
|
||||||
|
let msg = "Cannot redact already redacted message";
|
||||||
let err = UIError::Failure(msg.into());
|
let err = UIError::Failure(msg.into());
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -255,6 +333,46 @@ impl ChatState {
|
|||||||
self.reply_to = self.scrollback.get_key(info);
|
self.reply_to = self.scrollback.get_key(info);
|
||||||
self.focus = RoomFocus::MessageBar;
|
self.focus = RoomFocus::MessageBar;
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
},
|
||||||
|
MessageAction::Unreact(emoji) => {
|
||||||
|
let room = self.get_joined(&store.application.worker)?;
|
||||||
|
let event_id: &EventId = match &msg.event {
|
||||||
|
MessageEvent::Original(ev) => ev.event_id.as_ref(),
|
||||||
|
MessageEvent::Local(event_id, _) => event_id.as_ref(),
|
||||||
|
MessageEvent::Redacted(_) => {
|
||||||
|
let msg = "Cannot unreact to a redacted message";
|
||||||
|
let err = UIError::Failure(msg.into());
|
||||||
|
|
||||||
|
return Err(err);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let reactions = match info.reactions.get(event_id) {
|
||||||
|
Some(r) => r,
|
||||||
|
None => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
let reactions = reactions.iter().filter_map(|(event_id, (reaction, user_id))| {
|
||||||
|
if user_id != &settings.profile.user_id {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(emoji) = &emoji {
|
||||||
|
if emoji == reaction {
|
||||||
|
return Some(event_id);
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Some(event_id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for reaction in reactions {
|
||||||
|
let _ = room.redact(reaction, None, None).await.map_err(IambError::from)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -272,7 +390,8 @@ impl ChatState {
|
|||||||
.client
|
.client
|
||||||
.get_joined_room(self.id())
|
.get_joined_room(self.id())
|
||||||
.ok_or(IambError::NotJoined)?;
|
.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 {
|
let (event_id, msg) = match act {
|
||||||
SendAction::Submit => {
|
SendAction::Submit => {
|
||||||
@@ -282,12 +401,19 @@ impl ChatState {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = TextMessageEventContent::plain(msg);
|
let msg = TextMessageEventContent::markdown(msg);
|
||||||
let msg = MessageType::Text(msg);
|
let msg = MessageType::Text(msg);
|
||||||
|
|
||||||
let mut msg = RoomMessageEventContent::new(msg);
|
let mut msg = RoomMessageEventContent::new(msg);
|
||||||
|
|
||||||
if let Some(m) = self.get_reply_to(info) {
|
if let Some((_, event_id)) = &self.editing {
|
||||||
|
msg.relates_to = Some(Relation::Replacement(Replacement::new(
|
||||||
|
event_id.clone(),
|
||||||
|
Box::new(msg.clone()),
|
||||||
|
)));
|
||||||
|
|
||||||
|
show_echo = false;
|
||||||
|
} else if let Some(m) = self.get_reply_to(info) {
|
||||||
// XXX: Switch to RoomMessageEventContent::reply() once it's stable?
|
// XXX: Switch to RoomMessageEventContent::reply() once it's stable?
|
||||||
msg = msg.make_reply_to(m);
|
msg = msg.make_reply_to(m);
|
||||||
}
|
}
|
||||||
@@ -319,7 +445,7 @@ impl ChatState {
|
|||||||
.map_err(IambError::from)?;
|
.map_err(IambError::from)?;
|
||||||
|
|
||||||
// Mock up the local echo message for the scrollback.
|
// Mock up the local echo message for the scrollback.
|
||||||
let msg = TextMessageEventContent::plain(format!("[Attached File: {}]", name));
|
let msg = TextMessageEventContent::plain(format!("[Attached File: {name}]"));
|
||||||
let msg = MessageType::Text(msg);
|
let msg = MessageType::Text(msg);
|
||||||
let msg = RoomMessageEventContent::new(msg);
|
let msg = RoomMessageEventContent::new(msg);
|
||||||
|
|
||||||
@@ -327,11 +453,13 @@ impl ChatState {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if show_echo {
|
||||||
let user = store.application.settings.profile.user_id.clone();
|
let user = store.application.settings.profile.user_id.clone();
|
||||||
let key = (MessageTimeStamp::LocalEcho, event_id);
|
let key = (MessageTimeStamp::LocalEcho, event_id.clone());
|
||||||
let msg = MessageEvent::Local(msg.into());
|
let msg = MessageEvent::Local(event_id, msg.into());
|
||||||
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
|
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
|
||||||
info.messages.insert(key, msg);
|
info.messages.insert(key, msg);
|
||||||
|
}
|
||||||
|
|
||||||
// Jump to the end of the scrollback to show the message.
|
// Jump to the end of the scrollback to show the message.
|
||||||
self.scrollback.goto_latest();
|
self.scrollback.goto_latest();
|
||||||
@@ -364,7 +492,7 @@ impl ChatState {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !store.application.settings.tunables.typing_notice {
|
if !store.application.settings.tunables.typing_notice_send {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,6 +541,7 @@ impl WindowOps<IambInfo> for ChatState {
|
|||||||
focus: self.focus,
|
focus: self.focus,
|
||||||
|
|
||||||
reply_to: None,
|
reply_to: None,
|
||||||
|
editing: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,6 +551,21 @@ impl WindowOps<IambInfo> for ChatState {
|
|||||||
true
|
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> {
|
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||||
delegate!(self, w => w.get_cursor_word(style))
|
delegate!(self, w => w.get_cursor_word(style))
|
||||||
}
|
}
|
||||||
@@ -601,14 +745,20 @@ impl<'a> StatefulWidget for Chat<'a> {
|
|||||||
let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
|
let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
|
||||||
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
||||||
|
|
||||||
let desc_spans = state.reply_to.as_ref().and_then(|k| {
|
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 room = self.store.application.rooms.get(state.id())?;
|
||||||
let msg = room.messages.get(k)?;
|
let msg = room.messages.get(k)?;
|
||||||
let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
|
let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
|
||||||
let spans = Spans(vec![Span::from("Replying to "), user]);
|
let spans = Spans(vec![Span::from("Replying to "), user]);
|
||||||
|
|
||||||
spans.into()
|
spans.into()
|
||||||
});
|
})
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(desc_spans) = desc_spans {
|
if let Some(desc_spans) = desc_spans {
|
||||||
Paragraph::new(desc_spans).render(descarea, buf);
|
Paragraph::new(desc_spans).render(descarea, buf);
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
room::{Invited, Room as MatrixRoom},
|
room::{Invited, Room as MatrixRoom},
|
||||||
ruma::RoomId,
|
ruma::{
|
||||||
|
events::{
|
||||||
|
room::{name::RoomNameEventContent, topic::RoomTopicEventContent},
|
||||||
|
tag::{TagInfo, Tags},
|
||||||
|
},
|
||||||
|
RoomId,
|
||||||
|
},
|
||||||
DisplayName,
|
DisplayName,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -23,6 +29,7 @@ use modalkit::{
|
|||||||
PromptAction,
|
PromptAction,
|
||||||
Promptable,
|
Promptable,
|
||||||
Scrollable,
|
Scrollable,
|
||||||
|
UIError,
|
||||||
},
|
},
|
||||||
editing::base::{
|
editing::base::{
|
||||||
Axis,
|
Axis,
|
||||||
@@ -33,7 +40,9 @@ use modalkit::{
|
|||||||
PositionList,
|
PositionList,
|
||||||
ScrollStyle,
|
ScrollStyle,
|
||||||
WordStyle,
|
WordStyle,
|
||||||
|
WriteFlags,
|
||||||
},
|
},
|
||||||
|
editing::completion::CompletionList,
|
||||||
input::InputContext,
|
input::InputContext,
|
||||||
widgets::{TermOffset, TerminalCursor, WindowOps},
|
widgets::{TermOffset, TerminalCursor, WindowOps},
|
||||||
};
|
};
|
||||||
@@ -48,6 +57,7 @@ use crate::base::{
|
|||||||
ProgramContext,
|
ProgramContext,
|
||||||
ProgramStore,
|
ProgramStore,
|
||||||
RoomAction,
|
RoomAction,
|
||||||
|
RoomField,
|
||||||
SendAction,
|
SendAction,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -85,10 +95,16 @@ impl From<SpaceState> for RoomState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RoomState {
|
impl RoomState {
|
||||||
pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
pub fn new(
|
||||||
|
room: MatrixRoom,
|
||||||
|
name: DisplayName,
|
||||||
|
tags: Option<Tags>,
|
||||||
|
store: &mut ProgramStore,
|
||||||
|
) -> Self {
|
||||||
let room_id = room.room_id().to_owned();
|
let room_id = room.room_id().to_owned();
|
||||||
let info = store.application.get_room_info(room_id);
|
let info = store.application.get_room_info(room_id);
|
||||||
info.name = name.to_string().into();
|
info.name = name.to_string().into();
|
||||||
|
info.tags = tags;
|
||||||
|
|
||||||
if room.is_space() {
|
if room.is_space() {
|
||||||
SpaceState::new(room).into()
|
SpaceState::new(room).into()
|
||||||
@@ -118,10 +134,7 @@ impl RoomState {
|
|||||||
None => format!("{:?}", store.application.get_room_title(self.id())),
|
None => format!("{:?}", store.application.get_room_title(self.id())),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut invited = vec![Span::from(format!(
|
let mut invited = vec![Span::from(format!("You have been invited to join {name}"))];
|
||||||
"You have been invited to join {}",
|
|
||||||
name
|
|
||||||
))];
|
|
||||||
|
|
||||||
if let Ok(Some(inviter)) = &inviter {
|
if let Ok(Some(inviter)) = &inviter {
|
||||||
invited.push(Span::from(" by "));
|
invited.push(Span::from(" by "));
|
||||||
@@ -172,8 +185,16 @@ impl RoomState {
|
|||||||
match act {
|
match act {
|
||||||
RoomAction::InviteAccept => {
|
RoomAction::InviteAccept => {
|
||||||
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
|
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)?;
|
room.accept_invitation().await.map_err(IambError::from)?;
|
||||||
|
|
||||||
|
if is_direct {
|
||||||
|
room.set_is_direct(true).await.map_err(IambError::from)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
} else {
|
} else {
|
||||||
Err(IambError::NotInvited.into())
|
Err(IambError::NotInvited.into())
|
||||||
@@ -207,8 +228,50 @@ impl RoomState {
|
|||||||
|
|
||||||
Ok(vec![(act, cmd.context.take())])
|
Ok(vec![(act, cmd.context.take())])
|
||||||
},
|
},
|
||||||
RoomAction::Set(field) => {
|
RoomAction::Set(field, value) => {
|
||||||
store.application.worker.set_room(self.id().to_owned(), field)?;
|
let room = store
|
||||||
|
.application
|
||||||
|
.get_joined_room(self.id())
|
||||||
|
.ok_or(UIError::Application(IambError::NotJoined))?;
|
||||||
|
|
||||||
|
match field {
|
||||||
|
RoomField::Name => {
|
||||||
|
let ev = RoomNameEventContent::new(value.into());
|
||||||
|
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
RoomField::Tag(tag) => {
|
||||||
|
let mut info = TagInfo::new();
|
||||||
|
info.order = Some(1.0);
|
||||||
|
|
||||||
|
let _ = room.set_tag(tag, info).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
RoomField::Topic => {
|
||||||
|
let ev = RoomTopicEventContent::new(value);
|
||||||
|
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(vec![])
|
||||||
|
},
|
||||||
|
RoomAction::Unset(field) => {
|
||||||
|
let room = store
|
||||||
|
.application
|
||||||
|
.get_joined_room(self.id())
|
||||||
|
.ok_or(UIError::Application(IambError::NotJoined))?;
|
||||||
|
|
||||||
|
match field {
|
||||||
|
RoomField::Name => {
|
||||||
|
let ev = RoomNameEventContent::new(None);
|
||||||
|
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
RoomField::Tag(tag) => {
|
||||||
|
let _ = room.remove_tag(tag).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
RoomField::Topic => {
|
||||||
|
let ev = RoomTopicEventContent::new("".into());
|
||||||
|
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
},
|
},
|
||||||
@@ -330,10 +393,30 @@ impl WindowOps<IambInfo> for RoomState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
|
fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
|
||||||
// XXX: what's the right closing behaviour for a room?
|
match self {
|
||||||
// Should write send a message?
|
RoomState::Chat(chat) => chat.close(flags, store),
|
||||||
true
|
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> {
|
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ use modalkit::editing::{
|
|||||||
base::{
|
base::{
|
||||||
Axis,
|
Axis,
|
||||||
CloseFlags,
|
CloseFlags,
|
||||||
|
CompletionDisplay,
|
||||||
|
CompletionSelection,
|
||||||
|
CompletionType,
|
||||||
Count,
|
Count,
|
||||||
EditRange,
|
EditRange,
|
||||||
EditTarget,
|
EditTarget,
|
||||||
@@ -51,7 +54,9 @@ use modalkit::editing::{
|
|||||||
TargetShape,
|
TargetShape,
|
||||||
ViewportContext,
|
ViewportContext,
|
||||||
WordStyle,
|
WordStyle,
|
||||||
|
WriteFlags,
|
||||||
},
|
},
|
||||||
|
completion::CompletionList,
|
||||||
context::{EditContext, Resolve},
|
context::{EditContext, Resolve},
|
||||||
cursor::{CursorGroup, CursorState},
|
cursor::{CursorGroup, CursorState},
|
||||||
history::HistoryList,
|
history::HistoryList,
|
||||||
@@ -60,9 +65,9 @@ use modalkit::editing::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
base::{IambBufferId, IambInfo, IambResult, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||||
config::ApplicationSettings,
|
config::ApplicationSettings,
|
||||||
message::{Message, MessageCursor, MessageKey},
|
message::{Message, MessageCursor, MessageKey, Messages},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn nth_key_before(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageKey {
|
fn nth_key_before(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageKey {
|
||||||
@@ -103,6 +108,10 @@ fn nth_after(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageCursor {
|
|||||||
nth_key_after(pos, n, info).into()
|
nth_key_after(pos, n, info).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prevmsg<'a>(key: &MessageKey, info: &'a RoomInfo) -> Option<&'a Message> {
|
||||||
|
info.messages.range(..key).next_back().map(|(_, v)| v)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ScrollbackState {
|
pub struct ScrollbackState {
|
||||||
/// The room identifier.
|
/// The room identifier.
|
||||||
room_id: OwnedRoomId,
|
room_id: OwnedRoomId,
|
||||||
@@ -160,11 +169,11 @@ impl ScrollbackState {
|
|||||||
.or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
|
.or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> {
|
pub fn get_mut<'a>(&mut self, messages: &'a mut Messages) -> Option<&'a mut Message> {
|
||||||
if let Some(k) = &self.cursor.timestamp {
|
if let Some(k) = &self.cursor.timestamp {
|
||||||
info.messages.get_mut(k)
|
messages.get_mut(k)
|
||||||
} else {
|
} else {
|
||||||
info.messages.last_entry().map(|o| o.into_mut())
|
messages.last_entry().map(|o| o.into_mut())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +223,8 @@ impl ScrollbackState {
|
|||||||
|
|
||||||
for (key, item) in info.messages.range(..=&idx).rev() {
|
for (key, item) in info.messages.range(..=&idx).rev() {
|
||||||
let sel = selidx == key;
|
let sel = selidx == key;
|
||||||
let len = item.show(None, sel, &self.viewctx, settings).lines.len();
|
let prev = prevmsg(key, info);
|
||||||
|
let len = item.show(prev, sel, &self.viewctx, info, settings).lines.len();
|
||||||
|
|
||||||
if key == &idx {
|
if key == &idx {
|
||||||
lines += len / 2;
|
lines += len / 2;
|
||||||
@@ -236,7 +246,8 @@ impl ScrollbackState {
|
|||||||
|
|
||||||
for (key, item) in info.messages.range(..=&idx).rev() {
|
for (key, item) in info.messages.range(..=&idx).rev() {
|
||||||
let sel = key == selidx;
|
let sel = key == selidx;
|
||||||
let len = item.show(None, sel, &self.viewctx, settings).lines.len();
|
let prev = prevmsg(key, info);
|
||||||
|
let len = item.show(prev, sel, &self.viewctx, info, settings).lines.len();
|
||||||
|
|
||||||
lines += len;
|
lines += len;
|
||||||
|
|
||||||
@@ -269,6 +280,7 @@ impl ScrollbackState {
|
|||||||
let mut lines = 0;
|
let mut lines = 0;
|
||||||
|
|
||||||
let cursor_key = self.cursor.timestamp.as_ref().unwrap_or(last_key);
|
let cursor_key = self.cursor.timestamp.as_ref().unwrap_or(last_key);
|
||||||
|
let mut prev = prevmsg(cursor_key, info);
|
||||||
|
|
||||||
for (idx, item) in info.messages.range(corner_key.clone()..) {
|
for (idx, item) in info.messages.range(corner_key.clone()..) {
|
||||||
if idx == cursor_key {
|
if idx == cursor_key {
|
||||||
@@ -276,13 +288,15 @@ impl ScrollbackState {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
lines += item.show(None, false, &self.viewctx, settings).height().max(1);
|
lines += item.show(prev, false, &self.viewctx, info, settings).height().max(1);
|
||||||
|
|
||||||
if lines >= self.viewctx.get_height() {
|
if lines >= self.viewctx.get_height() {
|
||||||
// We've reached the end of the viewport; move cursor into it.
|
// We've reached the end of the viewport; move cursor into it.
|
||||||
self.cursor = idx.clone().into();
|
self.cursor = idx.clone().into();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prev = Some(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,7 +445,7 @@ impl ScrollbackState {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if needle.is_match(msg.event.show().as_ref()) {
|
if needle.is_match(msg.event.body().as_ref()) {
|
||||||
mc = MessageCursor::from(key.clone()).into();
|
mc = MessageCursor::from(key.clone()).into();
|
||||||
count -= 1;
|
count -= 1;
|
||||||
}
|
}
|
||||||
@@ -455,7 +469,7 @@ impl ScrollbackState {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if needle.is_match(msg.event.show().as_ref()) {
|
if needle.is_match(msg.event.body().as_ref()) {
|
||||||
mc = MessageCursor::from(key.clone()).into();
|
mc = MessageCursor::from(key.clone()).into();
|
||||||
count -= 1;
|
count -= 1;
|
||||||
}
|
}
|
||||||
@@ -506,6 +520,23 @@ impl WindowOps<IambInfo> for ScrollbackState {
|
|||||||
true
|
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> {
|
fn get_cursor_word(&self, _: &WordStyle) -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -523,7 +554,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
ctx: &ProgramContext,
|
ctx: &ProgramContext,
|
||||||
store: &mut ProgramStore,
|
store: &mut ProgramStore,
|
||||||
) -> EditResult<EditInfo, IambInfo> {
|
) -> EditResult<EditInfo, IambInfo> {
|
||||||
let info = store.application.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) {
|
let key = if let Some(k) = self.cursor.to_key(info) {
|
||||||
k.clone()
|
k.clone()
|
||||||
} else {
|
} else {
|
||||||
@@ -582,7 +613,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
let needle = match ctx.get_search_regex() {
|
let needle = match ctx.get_search_regex() {
|
||||||
Some(re) => re,
|
Some(re) => re,
|
||||||
None => {
|
None => {
|
||||||
let lsearch = store.registers.get(&Register::LastSearch);
|
let lsearch = store.registers.get(&Register::LastSearch)?;
|
||||||
let lsearch = lsearch.value.to_string();
|
let lsearch = lsearch.value.to_string();
|
||||||
|
|
||||||
Regex::new(lsearch.as_ref())?
|
Regex::new(lsearch.as_ref())?
|
||||||
@@ -606,7 +637,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
let msg = format!("Unknown editing target: {:?}", motion);
|
let msg = format!("Unknown editing target: {motion:?}");
|
||||||
let err = EditError::Unimplemented(msg);
|
let err = EditError::Unimplemented(msg);
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -668,7 +699,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
let needle = match ctx.get_search_regex() {
|
let needle = match ctx.get_search_regex() {
|
||||||
Some(re) => re,
|
Some(re) => re,
|
||||||
None => {
|
None => {
|
||||||
let lsearch = store.registers.get(&Register::LastSearch);
|
let lsearch = store.registers.get(&Register::LastSearch)?;
|
||||||
let lsearch = lsearch.value.to_string();
|
let lsearch = lsearch.value.to_string();
|
||||||
|
|
||||||
Regex::new(lsearch.as_ref())?
|
Regex::new(lsearch.as_ref())?
|
||||||
@@ -693,7 +724,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
let msg = format!("Unknown motion: {:?}", motion);
|
let msg = format!("Unknown motion: {motion:?}");
|
||||||
let err = EditError::Unimplemented(msg);
|
let err = EditError::Unimplemented(msg);
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -704,7 +735,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
let mut yanked = EditRope::from("");
|
let mut yanked = EditRope::from("");
|
||||||
|
|
||||||
for (_, msg) in self.messages(range, info) {
|
for (_, msg) in self.messages(range, info) {
|
||||||
yanked += EditRope::from(msg.event.show().into_owned());
|
yanked += EditRope::from(msg.event.body());
|
||||||
yanked += EditRope::from('\n');
|
yanked += EditRope::from('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -716,7 +747,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
flags |= RegisterPutFlags::APPEND;
|
flags |= RegisterPutFlags::APPEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
store.registers.put(®ister, cell, flags);
|
store.registers.put(®ister, cell, flags)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
@@ -724,7 +755,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
|
|
||||||
// Everything else is a modifying action.
|
// Everything else is a modifying action.
|
||||||
EditAction::ChangeCase(_) => Err(EditError::ReadOnly),
|
EditAction::ChangeCase(_) => Err(EditError::ReadOnly),
|
||||||
EditAction::ChangeNumber(_) => Err(EditError::ReadOnly),
|
EditAction::ChangeNumber(_, _) => Err(EditError::ReadOnly),
|
||||||
EditAction::Delete => Err(EditError::ReadOnly),
|
EditAction::Delete => Err(EditError::ReadOnly),
|
||||||
EditAction::Format => Err(EditError::ReadOnly),
|
EditAction::Format => Err(EditError::ReadOnly),
|
||||||
EditAction::Indent(_) => Err(EditError::ReadOnly),
|
EditAction::Indent(_) => Err(EditError::ReadOnly),
|
||||||
@@ -753,6 +784,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(
|
fn insert_text(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &InsertTextAction,
|
_: &InsertTextAction,
|
||||||
@@ -781,7 +823,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
HistoryAction::Checkpoint => Ok(None),
|
HistoryAction::Checkpoint => Ok(None),
|
||||||
HistoryAction::Undo(_) => Err(EditError::Failure("Nothing to undo".into())),
|
HistoryAction::Undo(_) => Err(EditError::Failure("Nothing to undo".into())),
|
||||||
HistoryAction::Redo(_) => Err(EditError::Failure("Nothing to redo".into())),
|
HistoryAction::Redo(_) => Err(EditError::Failure("Nothing to redo".into())),
|
||||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -838,7 +880,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
},
|
},
|
||||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -858,14 +900,14 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
EditorAction::Mark(name) => self.mark(ctx.resolve(name), ctx, store),
|
EditorAction::Mark(name) => self.mark(ctx.resolve(name), ctx, store),
|
||||||
EditorAction::Selection(act) => self.selection_command(act, ctx, store),
|
EditorAction::Selection(act) => self.selection_command(act, ctx, store),
|
||||||
|
|
||||||
EditorAction::Complete(_, _) => {
|
EditorAction::Complete(_, _, _) => {
|
||||||
let msg = "";
|
let msg = "Nothing to complete in message scrollback";
|
||||||
let err = EditError::Unimplemented(msg.into());
|
let err = EditError::Failure(msg.into());
|
||||||
|
|
||||||
Err(err)
|
Err(err)
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -964,7 +1006,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
return Err(err);
|
return Err(err);
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
let msg = format!("Messages scrollback doesn't support {:?}", act);
|
let msg = format!("Messages scrollback doesn't support {act:?}");
|
||||||
let err = EditError::Unimplemented(msg);
|
let err = EditError::Unimplemented(msg);
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -982,7 +1024,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
ctx: &ProgramContext,
|
ctx: &ProgramContext,
|
||||||
store: &mut ProgramStore,
|
store: &mut ProgramStore,
|
||||||
) -> EditResult<EditInfo, IambInfo> {
|
) -> EditResult<EditInfo, IambInfo> {
|
||||||
let info = store.application.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 settings = &store.application.settings;
|
||||||
let mut corner = self.viewctx.corner.clone();
|
let mut corner = self.viewctx.corner.clone();
|
||||||
|
|
||||||
@@ -1009,7 +1051,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
|
|
||||||
for (key, item) in info.messages.range(..=&corner_key).rev() {
|
for (key, item) in info.messages.range(..=&corner_key).rev() {
|
||||||
let sel = key == cursor_key;
|
let sel = key == cursor_key;
|
||||||
let txt = item.show(None, sel, &self.viewctx, settings);
|
let prev = prevmsg(key, info);
|
||||||
|
let txt = item.show(prev, sel, &self.viewctx, info, settings);
|
||||||
let len = txt.height().max(1);
|
let len = txt.height().max(1);
|
||||||
let max = len.saturating_sub(1);
|
let max = len.saturating_sub(1);
|
||||||
|
|
||||||
@@ -1033,12 +1076,16 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
MoveDir2D::Down => {
|
MoveDir2D::Down => {
|
||||||
|
let mut prev = prevmsg(&corner_key, info);
|
||||||
|
|
||||||
for (key, item) in info.messages.range(&corner_key..) {
|
for (key, item) in info.messages.range(&corner_key..) {
|
||||||
let sel = key == cursor_key;
|
let sel = key == cursor_key;
|
||||||
let txt = item.show(None, sel, &self.viewctx, settings);
|
let txt = item.show(prev, sel, &self.viewctx, info, settings);
|
||||||
let len = txt.height().max(1);
|
let len = txt.height().max(1);
|
||||||
let max = len.saturating_sub(1);
|
let max = len.saturating_sub(1);
|
||||||
|
|
||||||
|
prev = Some(item);
|
||||||
|
|
||||||
if key != &corner_key {
|
if key != &corner_key {
|
||||||
corner.text_row = 0;
|
corner.text_row = 0;
|
||||||
}
|
}
|
||||||
@@ -1091,7 +1138,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||||||
Err(err)
|
Err(err)
|
||||||
},
|
},
|
||||||
Axis::Vertical => {
|
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;
|
let settings = &store.application.settings;
|
||||||
|
|
||||||
if let Some(key) = self.cursor.to_key(info).cloned() {
|
if let Some(key) = self.cursor.to_key(info).cloned() {
|
||||||
@@ -1179,7 +1226,7 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
|||||||
type State = ScrollbackState;
|
type State = ScrollbackState;
|
||||||
|
|
||||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||||
let info = self.store.application.rooms.entry(state.room_id.clone()).or_default();
|
let info = self.store.application.rooms.get_or_default(state.room_id.clone());
|
||||||
let settings = &self.store.application.settings;
|
let settings = &self.store.application.settings;
|
||||||
let area = info.render_typing(area, buf, &self.store.application.settings);
|
let area = info.render_typing(area, buf, &self.store.application.settings);
|
||||||
|
|
||||||
@@ -1214,11 +1261,11 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
|||||||
let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
|
let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
let mut sawit = false;
|
let mut sawit = false;
|
||||||
let mut prev = None;
|
let mut prev = prevmsg(&corner_key, info);
|
||||||
|
|
||||||
for (key, item) in info.messages.range(&corner_key..) {
|
for (key, item) in info.messages.range(&corner_key..) {
|
||||||
let sel = key == cursor_key;
|
let sel = key == cursor_key;
|
||||||
let txt = item.show(prev, foc && sel, &state.viewctx, settings);
|
let txt = item.show(prev, foc && sel, &state.viewctx, info, settings);
|
||||||
|
|
||||||
prev = Some(item);
|
prev = Some(item);
|
||||||
|
|
||||||
@@ -1260,7 +1307,13 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
|||||||
y += 1;
|
y += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let first_key = info.messages.first_key_value().map(|f| f.0.clone());
|
if settings.tunables.read_receipt_send && state.cursor.timestamp.is_none() {
|
||||||
|
// If the cursor is at the last message, then update the read marker.
|
||||||
|
info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether we should load older messages for this room.
|
||||||
|
let first_key = info.messages.first_key_value().map(|(k, _)| k.clone());
|
||||||
if first_key == state.viewctx.corner.timestamp {
|
if first_key == state.viewctx.corner.timestamp {
|
||||||
// If the top of the screen is the older message, load more.
|
// If the top of the screen is the older message, load more.
|
||||||
self.store.application.mark_for_load(state.room_id.clone());
|
self.store.application.mark_for_load(state.room_id.clone());
|
||||||
@@ -1367,10 +1420,11 @@ mod tests {
|
|||||||
assert_eq!(scrollback.viewctx.dimensions, (0, 0));
|
assert_eq!(scrollback.viewctx.dimensions, (0, 0));
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::latest());
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::latest());
|
||||||
|
|
||||||
// Set a terminal width of 60, and height of 3, rendering in scrollback as:
|
// Set a terminal width of 60, and height of 4, rendering in scrollback as:
|
||||||
//
|
//
|
||||||
// |------------------------------------------------------------|
|
// |------------------------------------------------------------|
|
||||||
// MSG2: | @user2:example.com helium |
|
// MSG2: | Wednesday, December 31 1969 |
|
||||||
|
// | @user2:example.com helium |
|
||||||
// MSG3: | @user2:example.com this |
|
// MSG3: | @user2:example.com this |
|
||||||
// | is |
|
// | is |
|
||||||
// | a |
|
// | a |
|
||||||
@@ -1378,14 +1432,15 @@ mod tests {
|
|||||||
// | message |
|
// | message |
|
||||||
// MSG4: | @user1:example.com help |
|
// MSG4: | @user1:example.com help |
|
||||||
// MSG5: | @user2:example.com character |
|
// MSG5: | @user2:example.com character |
|
||||||
// MSG1: | @user1:example.com writhe |
|
// MSG1: | XXXday, Month NN 20XX |
|
||||||
|
// | @user1:example.com writhe |
|
||||||
// |------------------------------------------------------------|
|
// |------------------------------------------------------------|
|
||||||
let area = Rect::new(0, 0, 60, 3);
|
let area = Rect::new(0, 0, 60, 4);
|
||||||
let mut buffer = Buffer::empty(area);
|
let mut buffer = Buffer::empty(area);
|
||||||
scrollback.draw(area, &mut buffer, true, &mut store);
|
scrollback.draw(area, &mut buffer, true, &mut store);
|
||||||
|
|
||||||
assert_eq!(scrollback.cursor, MessageCursor::latest());
|
assert_eq!(scrollback.cursor, MessageCursor::latest());
|
||||||
assert_eq!(scrollback.viewctx.dimensions, (60, 3));
|
assert_eq!(scrollback.viewctx.dimensions, (60, 4));
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG4_KEY.clone(), 0));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG4_KEY.clone(), 0));
|
||||||
|
|
||||||
// Scroll up a line at a time until we hit the first message.
|
// Scroll up a line at a time until we hit the first message.
|
||||||
@@ -1414,6 +1469,11 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 0));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 0));
|
||||||
|
|
||||||
|
scrollback
|
||||||
|
.dirscroll(prev, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 1));
|
||||||
|
|
||||||
scrollback
|
scrollback
|
||||||
.dirscroll(prev, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
.dirscroll(prev, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -1426,6 +1486,11 @@ mod tests {
|
|||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 0));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 0));
|
||||||
|
|
||||||
// Now scroll back down one line at a time.
|
// Now scroll back down one line at a time.
|
||||||
|
scrollback
|
||||||
|
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 1));
|
||||||
|
|
||||||
scrollback
|
scrollback
|
||||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -1466,19 +1531,24 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 0));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 0));
|
||||||
|
|
||||||
|
scrollback
|
||||||
|
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 1));
|
||||||
|
|
||||||
// Cannot scroll down any further.
|
// Cannot scroll down any further.
|
||||||
scrollback
|
scrollback
|
||||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 0));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 1));
|
||||||
|
|
||||||
// Scroll up two Pages (six lines).
|
// Scroll up two Pages (eight lines).
|
||||||
scrollback
|
scrollback
|
||||||
.dirscroll(prev, ScrollSize::Page, &2.into(), &ctx, &mut store)
|
.dirscroll(prev, ScrollSize::Page, &2.into(), &ctx, &mut store)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 1));
|
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 0));
|
||||||
|
|
||||||
// Scroll down two HalfPages (three lines).
|
// Scroll down two HalfPages (four lines).
|
||||||
scrollback
|
scrollback
|
||||||
.dirscroll(next, ScrollSize::HalfPage, &2.into(), &ctx, &mut store)
|
.dirscroll(next, ScrollSize::HalfPage, &2.into(), &ctx, &mut store)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -1497,7 +1567,8 @@ mod tests {
|
|||||||
// Set a terminal width of 60, and height of 3, rendering in scrollback as:
|
// Set a terminal width of 60, and height of 3, rendering in scrollback as:
|
||||||
//
|
//
|
||||||
// |------------------------------------------------------------|
|
// |------------------------------------------------------------|
|
||||||
// MSG2: | @user2:example.com helium |
|
// MSG2: | Wednesday, December 31 1969 |
|
||||||
|
// | @user2:example.com helium |
|
||||||
// MSG3: | @user2:example.com this |
|
// MSG3: | @user2:example.com this |
|
||||||
// | is |
|
// | is |
|
||||||
// | a |
|
// | a |
|
||||||
@@ -1505,7 +1576,8 @@ mod tests {
|
|||||||
// | message |
|
// | message |
|
||||||
// MSG4: | @user1:example.com help |
|
// MSG4: | @user1:example.com help |
|
||||||
// MSG5: | @user2:example.com character |
|
// MSG5: | @user2:example.com character |
|
||||||
// MSG1: | @user1:example.com writhe |
|
// MSG1: | XXXday, Month NN 20XX |
|
||||||
|
// | @user1:example.com writhe |
|
||||||
// |------------------------------------------------------------|
|
// |------------------------------------------------------------|
|
||||||
let area = Rect::new(0, 0, 60, 3);
|
let area = Rect::new(0, 0, 60, 3);
|
||||||
let mut buffer = Buffer::empty(area);
|
let mut buffer = Buffer::empty(area);
|
||||||
|
|||||||
@@ -104,10 +104,10 @@ impl<'a> StatefulWidget for Space<'a> {
|
|||||||
let items = members
|
let items = members
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|id| {
|
.filter_map(|id| {
|
||||||
let (room, name) = self.store.application.worker.get_room(id.clone()).ok()?;
|
let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?;
|
||||||
|
|
||||||
if id != state.room_id {
|
if id != state.room_id {
|
||||||
Some(RoomItem::new(room, name, self.store))
|
Some(RoomItem::new(room, name, tags, self.store))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ use modalkit::{
|
|||||||
widgets::{TermOffset, TerminalCursor},
|
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");
|
const WELCOME_TEXT: &str = include_str!("welcome.md");
|
||||||
|
|
||||||
@@ -63,6 +65,19 @@ impl WindowOps<IambInfo> for WelcomeState {
|
|||||||
self.tbox.close(flags, store)
|
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> {
|
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||||
self.tbox.get_cursor_word(style)
|
self.tbox.get_cursor_word(style)
|
||||||
}
|
}
|
||||||
|
|||||||
320
src/worker.rs
320
src/worker.rs
@@ -20,10 +20,11 @@ use matrix_sdk::{
|
|||||||
room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
|
room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
|
||||||
ruma::{
|
ruma::{
|
||||||
api::client::{
|
api::client::{
|
||||||
room::create_room::v3::{Request as CreateRoomRequest, RoomPreset},
|
room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
|
||||||
room::Visibility,
|
room::Visibility,
|
||||||
space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
|
space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
|
||||||
},
|
},
|
||||||
|
assign,
|
||||||
events::{
|
events::{
|
||||||
key::verification::{
|
key::verification::{
|
||||||
done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
|
done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
|
||||||
@@ -32,18 +33,26 @@ use matrix_sdk::{
|
|||||||
start::{OriginalSyncKeyVerificationStartEvent, ToDeviceKeyVerificationStartEvent},
|
start::{OriginalSyncKeyVerificationStartEvent, ToDeviceKeyVerificationStartEvent},
|
||||||
VerificationMethod,
|
VerificationMethod,
|
||||||
},
|
},
|
||||||
|
presence::PresenceEvent,
|
||||||
|
reaction::ReactionEventContent,
|
||||||
room::{
|
room::{
|
||||||
|
encryption::RoomEncryptionEventContent,
|
||||||
message::{MessageType, RoomMessageEventContent},
|
message::{MessageType, RoomMessageEventContent},
|
||||||
name::RoomNameEventContent,
|
name::RoomNameEventContent,
|
||||||
redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
|
redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
|
||||||
topic::RoomTopicEventContent,
|
|
||||||
},
|
},
|
||||||
|
tag::Tags,
|
||||||
typing::SyncTypingEvent,
|
typing::SyncTypingEvent,
|
||||||
AnyMessageLikeEvent,
|
AnyInitialStateEvent,
|
||||||
AnyTimelineEvent,
|
AnyTimelineEvent,
|
||||||
|
EmptyStateKey,
|
||||||
|
InitialStateEvent,
|
||||||
SyncMessageLikeEvent,
|
SyncMessageLikeEvent,
|
||||||
SyncStateEvent,
|
SyncStateEvent,
|
||||||
},
|
},
|
||||||
|
room::RoomType,
|
||||||
|
serde::Raw,
|
||||||
|
EventEncryptionAlgorithm,
|
||||||
OwnedRoomId,
|
OwnedRoomId,
|
||||||
OwnedRoomOrAliasId,
|
OwnedRoomOrAliasId,
|
||||||
OwnedUserId,
|
OwnedUserId,
|
||||||
@@ -57,8 +66,17 @@ use matrix_sdk::{
|
|||||||
use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
|
use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{AsyncProgramStore, IambError, IambResult, SetRoomField, VerifyAction},
|
base::{
|
||||||
message::{Message, MessageFetchResult, MessageTimeStamp},
|
AsyncProgramStore,
|
||||||
|
CreateRoomFlags,
|
||||||
|
CreateRoomType,
|
||||||
|
EventLocation,
|
||||||
|
IambError,
|
||||||
|
IambResult,
|
||||||
|
Receipts,
|
||||||
|
VerifyAction,
|
||||||
|
},
|
||||||
|
message::MessageFetchResult,
|
||||||
ApplicationSettings,
|
ApplicationSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,6 +88,77 @@ fn initial_devname() -> String {
|
|||||||
format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LoginStyle {
|
pub enum LoginStyle {
|
||||||
SessionRestore(Session),
|
SessionRestore(Session),
|
||||||
@@ -99,19 +188,43 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) {
|
|||||||
return (reply, response);
|
return (reply, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> {
|
||||||
|
let mut rooms = vec![];
|
||||||
|
|
||||||
|
for room in client.joined_rooms() {
|
||||||
|
if let Ok(users) = room.active_members_no_sync().await {
|
||||||
|
let mut receipts = Receipts::new();
|
||||||
|
|
||||||
|
for member in users {
|
||||||
|
let res = room.user_read_receipt(member.user_id()).await;
|
||||||
|
|
||||||
|
if let Ok(Some((event_id, _))) = res {
|
||||||
|
let user_id = member.user_id().to_owned();
|
||||||
|
receipts.entry(event_id).or_default().push(user_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rooms.push((room.room_id().to_owned(), receipts));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rooms;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
|
||||||
|
|
||||||
pub enum WorkerTask {
|
pub enum WorkerTask {
|
||||||
ActiveRooms(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
|
ActiveRooms(ClientReply<Vec<FetchedRoom>>),
|
||||||
DirectMessages(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
|
DirectMessages(ClientReply<Vec<FetchedRoom>>),
|
||||||
Init(AsyncProgramStore, ClientReply<()>),
|
Init(AsyncProgramStore, ClientReply<()>),
|
||||||
LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>),
|
LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>),
|
||||||
Login(LoginStyle, ClientReply<IambResult<EditInfo>>),
|
Login(LoginStyle, ClientReply<IambResult<EditInfo>>),
|
||||||
GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>),
|
GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>),
|
||||||
GetRoom(OwnedRoomId, ClientReply<IambResult<(MatrixRoom, DisplayName)>>),
|
GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>),
|
||||||
JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>),
|
JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>),
|
||||||
Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
|
Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
|
||||||
SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
|
SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
|
||||||
Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
|
Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
|
||||||
SetRoom(OwnedRoomId, SetRoomField, ClientReply<IambResult<()>>),
|
|
||||||
TypingNotice(OwnedRoomId),
|
TypingNotice(OwnedRoomId),
|
||||||
Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>),
|
Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>),
|
||||||
VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>),
|
VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>),
|
||||||
@@ -178,13 +291,6 @@ impl Debug for WorkerTask {
|
|||||||
WorkerTask::Spaces(_) => {
|
WorkerTask::Spaces(_) => {
|
||||||
f.debug_tuple("WorkerTask::Spaces").field(&format_args!("_")).finish()
|
f.debug_tuple("WorkerTask::Spaces").field(&format_args!("_")).finish()
|
||||||
},
|
},
|
||||||
WorkerTask::SetRoom(room_id, field, _) => {
|
|
||||||
f.debug_tuple("WorkerTask::SetRoom")
|
|
||||||
.field(room_id)
|
|
||||||
.field(field)
|
|
||||||
.field(&format_args!("_"))
|
|
||||||
.finish()
|
|
||||||
},
|
|
||||||
WorkerTask::TypingNotice(room_id) => {
|
WorkerTask::TypingNotice(room_id) => {
|
||||||
f.debug_tuple("WorkerTask::TypingNotice").field(room_id).finish()
|
f.debug_tuple("WorkerTask::TypingNotice").field(room_id).finish()
|
||||||
},
|
},
|
||||||
@@ -243,7 +349,7 @@ impl Requester {
|
|||||||
return response.recv();
|
return response.recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
pub fn direct_messages(&self) -> Vec<FetchedRoom> {
|
||||||
let (reply, response) = oneshot();
|
let (reply, response) = oneshot();
|
||||||
|
|
||||||
self.tx.send(WorkerTask::DirectMessages(reply)).unwrap();
|
self.tx.send(WorkerTask::DirectMessages(reply)).unwrap();
|
||||||
@@ -259,7 +365,7 @@ impl Requester {
|
|||||||
return response.recv();
|
return response.recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<(MatrixRoom, DisplayName)> {
|
pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
|
||||||
let (reply, response) = oneshot();
|
let (reply, response) = oneshot();
|
||||||
|
|
||||||
self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap();
|
self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap();
|
||||||
@@ -275,7 +381,7 @@ impl Requester {
|
|||||||
return response.recv();
|
return response.recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
pub fn active_rooms(&self) -> Vec<FetchedRoom> {
|
||||||
let (reply, response) = oneshot();
|
let (reply, response) = oneshot();
|
||||||
|
|
||||||
self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap();
|
self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap();
|
||||||
@@ -299,14 +405,6 @@ impl Requester {
|
|||||||
return response.recv();
|
return response.recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_room(&self, room_id: OwnedRoomId, ev: SetRoomField) -> IambResult<()> {
|
|
||||||
let (reply, response) = oneshot();
|
|
||||||
|
|
||||||
self.tx.send(WorkerTask::SetRoom(room_id, ev, reply)).unwrap();
|
|
||||||
|
|
||||||
return response.recv();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
||||||
let (reply, response) = oneshot();
|
let (reply, response) = oneshot();
|
||||||
|
|
||||||
@@ -341,6 +439,7 @@ pub struct ClientWorker {
|
|||||||
settings: ApplicationSettings,
|
settings: ApplicationSettings,
|
||||||
client: Client,
|
client: Client,
|
||||||
sync_handle: Option<JoinHandle<()>>,
|
sync_handle: Option<JoinHandle<()>>,
|
||||||
|
rcpt_handle: Option<JoinHandle<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientWorker {
|
impl ClientWorker {
|
||||||
@@ -379,9 +478,10 @@ impl ClientWorker {
|
|||||||
settings,
|
settings,
|
||||||
client: client.clone(),
|
client: client.clone(),
|
||||||
sync_handle: None,
|
sync_handle: None,
|
||||||
|
rcpt_handle: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
worker.work(rx).await;
|
worker.work(rx).await;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -403,6 +503,10 @@ impl ClientWorker {
|
|||||||
if let Some(handle) = self.sync_handle.take() {
|
if let Some(handle) = self.sync_handle.take() {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(handle) = self.rcpt_handle.take() {
|
||||||
|
handle.abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run(&mut self, task: WorkerTask) {
|
async fn run(&mut self, task: WorkerTask) {
|
||||||
@@ -444,10 +548,6 @@ impl ClientWorker {
|
|||||||
assert!(self.initialized);
|
assert!(self.initialized);
|
||||||
reply.send(self.members(room_id).await);
|
reply.send(self.members(room_id).await);
|
||||||
},
|
},
|
||||||
WorkerTask::SetRoom(room_id, field, reply) => {
|
|
||||||
assert!(self.initialized);
|
|
||||||
reply.send(self.set_room(room_id, field).await);
|
|
||||||
},
|
|
||||||
WorkerTask::SpaceMembers(space, reply) => {
|
WorkerTask::SpaceMembers(space, reply) => {
|
||||||
assert!(self.initialized);
|
assert!(self.initialized);
|
||||||
reply.send(self.space_members(space).await);
|
reply.send(self.space_members(space).await);
|
||||||
@@ -472,7 +572,7 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn init(&mut self, store: AsyncProgramStore) {
|
async fn init(&mut self, store: AsyncProgramStore) {
|
||||||
self.client.add_event_handler_context(store);
|
self.client.add_event_handler_context(store.clone());
|
||||||
|
|
||||||
let _ = self.client.add_event_handler(
|
let _ = self.client.add_event_handler(
|
||||||
|ev: SyncTypingEvent, room: MatrixRoom, store: Ctx<AsyncProgramStore>| {
|
|ev: SyncTypingEvent, room: MatrixRoom, store: Ctx<AsyncProgramStore>| {
|
||||||
@@ -492,6 +592,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(
|
let _ = self.client.add_event_handler(
|
||||||
|ev: SyncStateEvent<RoomNameEventContent>,
|
|ev: SyncStateEvent<RoomNameEventContent>,
|
||||||
room: MatrixRoom,
|
room: MatrixRoom,
|
||||||
@@ -502,8 +611,7 @@ impl ClientWorker {
|
|||||||
let room_id = room.room_id().to_owned();
|
let room_id = room.room_id().to_owned();
|
||||||
let room_name = Some(room_name.to_string());
|
let room_name = Some(room_name.to_string());
|
||||||
let mut locked = store.lock().await;
|
let mut locked = store.lock().await;
|
||||||
let mut info =
|
let mut info = locked.application.rooms.get_or_default(room_id.clone());
|
||||||
locked.application.rooms.entry(room_id.to_owned()).or_default();
|
|
||||||
info.name = room_name;
|
info.name = room_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -518,8 +626,6 @@ impl ClientWorker {
|
|||||||
store: Ctx<AsyncProgramStore>| {
|
store: Ctx<AsyncProgramStore>| {
|
||||||
async move {
|
async move {
|
||||||
let room_id = room.room_id();
|
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 Some(msg) = ev.as_original() {
|
||||||
if let MessageType::VerificationRequest(_) = msg.content.msgtype {
|
if let MessageType::VerificationRequest(_) = msg.content.msgtype {
|
||||||
@@ -534,17 +640,30 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut locked = store.lock().await;
|
let mut locked = store.lock().await;
|
||||||
let mut info = locked.application.get_room_info(room_id.to_owned());
|
|
||||||
info.name = room_name;
|
|
||||||
|
|
||||||
let event_id = ev.event_id().to_owned();
|
let sender = ev.sender().to_owned();
|
||||||
let key = (ev.origin_server_ts().into(), event_id.clone());
|
let _ = locked.application.presences.get_or_default(sender);
|
||||||
let msg = Message::from(ev.into_full_event(room_id.to_owned()));
|
|
||||||
info.messages.insert(key, msg);
|
|
||||||
|
|
||||||
// Remove the echo.
|
let info = locked.application.get_room_info(room_id.to_owned());
|
||||||
let key = (MessageTimeStamp::LocalEcho, event_id);
|
info.insert(ev.into_full_event(room_id.to_owned()));
|
||||||
let _ = info.messages.remove(&key);
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = self.client.add_event_handler(
|
||||||
|
|ev: SyncMessageLikeEvent<ReactionEventContent>,
|
||||||
|
room: MatrixRoom,
|
||||||
|
store: Ctx<AsyncProgramStore>| {
|
||||||
|
async move {
|
||||||
|
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()));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -561,17 +680,21 @@ impl ClientWorker {
|
|||||||
let mut locked = store.lock().await;
|
let mut locked = store.lock().await;
|
||||||
let info = locked.application.get_room_info(room_id.to_owned());
|
let info = locked.application.get_room_info(room_id.to_owned());
|
||||||
|
|
||||||
// XXX: need to store a mapping of EventId -> MessageKey somewhere
|
match info.keys.get(&ev.redacts) {
|
||||||
// to avoid having to iterate over the messages here.
|
None => return,
|
||||||
for ((_, id), msg) in info.messages.iter_mut().rev() {
|
Some(EventLocation::Message(key)) => {
|
||||||
if id != &ev.redacts {
|
if let Some(msg) = info.messages.get_mut(key) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let ev = SyncRoomRedactionEvent::Original(ev);
|
let ev = SyncRoomRedactionEvent::Original(ev);
|
||||||
msg.event.redact(ev, room_version);
|
msg.event.redact(ev, room_version);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(EventLocation::Reaction(event_id)) => {
|
||||||
|
if let Some(reactions) = info.reactions.get_mut(event_id) {
|
||||||
|
reactions.remove(&ev.redacts);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
info.keys.remove(&ev.redacts);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -689,6 +812,21 @@ impl ClientWorker {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into();
|
||||||
|
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -731,22 +869,18 @@ impl ClientWorker {
|
|||||||
Ok(Some(InfoMessage::from("Successfully logged in!")))
|
Ok(Some(InfoMessage::from("Successfully logged in!")))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<(MatrixRoom, DisplayName)> {
|
async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> {
|
||||||
for (room, name) in self.direct_messages().await {
|
for (room, name, tags) in self.direct_messages().await {
|
||||||
if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() {
|
if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() {
|
||||||
return Ok((room, name));
|
return Ok((room, name, tags));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut request = CreateRoomRequest::new();
|
let rt = CreateRoomType::Direct(user.clone());
|
||||||
let invite = [user.clone()];
|
let flags = CreateRoomFlags::ENCRYPTED;
|
||||||
request.is_direct = true;
|
|
||||||
request.invite = &invite;
|
|
||||||
request.visibility = Visibility::Private;
|
|
||||||
request.preset = Some(RoomPreset::PrivateChat);
|
|
||||||
|
|
||||||
match self.client.create_room(request).await {
|
match create_room(&self.client, None, rt, flags).await {
|
||||||
Ok(resp) => self.get_room(resp.room_id).await,
|
Ok(room_id) => self.get_room(room_id).await,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
user_id = user.as_str(),
|
user_id = user.as_str(),
|
||||||
@@ -754,7 +888,7 @@ impl ClientWorker {
|
|||||||
"Failed to create direct message room"
|
"Failed to create direct message room"
|
||||||
);
|
);
|
||||||
|
|
||||||
let msg = format!("Could not open a room with {}", user);
|
let msg = format!("Could not open a room with {user}");
|
||||||
let err = UIError::Failure(msg);
|
let err = UIError::Failure(msg);
|
||||||
|
|
||||||
Err(err)
|
Err(err)
|
||||||
@@ -768,11 +902,12 @@ impl ClientWorker {
|
|||||||
Ok(details.inviter)
|
Ok(details.inviter)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<(MatrixRoom, DisplayName)> {
|
async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
|
||||||
if let Some(room) = self.client.get_room(&room_id) {
|
if let Some(room) = self.client.get_room(&room_id) {
|
||||||
let name = room.display_name().await.map_err(IambError::from)?;
|
let name = room.display_name().await.map_err(IambError::from)?;
|
||||||
|
let tags = room.tags().await.map_err(IambError::from)?;
|
||||||
|
|
||||||
Ok((room, name))
|
Ok((room, name, tags))
|
||||||
} else {
|
} else {
|
||||||
Err(IambError::UnknownRoom(room_id).into())
|
Err(IambError::UnknownRoom(room_id).into())
|
||||||
}
|
}
|
||||||
@@ -801,7 +936,7 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
async fn direct_messages(&self) -> Vec<FetchedRoom> {
|
||||||
let mut rooms = vec![];
|
let mut rooms = vec![];
|
||||||
|
|
||||||
for room in self.client.invited_rooms().into_iter() {
|
for room in self.client.invited_rooms().into_iter() {
|
||||||
@@ -810,8 +945,9 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
||||||
|
let tags = room.tags().await.unwrap_or_default();
|
||||||
|
|
||||||
rooms.push((room.into(), name));
|
rooms.push((room.into(), name, tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
for room in self.client.joined_rooms().into_iter() {
|
for room in self.client.joined_rooms().into_iter() {
|
||||||
@@ -820,14 +956,15 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
||||||
|
let tags = room.tags().await.unwrap_or_default();
|
||||||
|
|
||||||
rooms.push((room.into(), name));
|
rooms.push((room.into(), name, tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
return rooms;
|
return rooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName)> {
|
async fn active_rooms(&self) -> Vec<FetchedRoom> {
|
||||||
let mut rooms = vec![];
|
let mut rooms = vec![];
|
||||||
|
|
||||||
for room in self.client.invited_rooms().into_iter() {
|
for room in self.client.invited_rooms().into_iter() {
|
||||||
@@ -836,8 +973,9 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
||||||
|
let tags = room.tags().await.unwrap_or_default();
|
||||||
|
|
||||||
rooms.push((room.into(), name));
|
rooms.push((room.into(), name, tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
for room in self.client.joined_rooms().into_iter() {
|
for room in self.client.joined_rooms().into_iter() {
|
||||||
@@ -846,8 +984,9 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
let name = room.display_name().await.unwrap_or(DisplayName::Empty);
|
||||||
|
let tags = room.tags().await.unwrap_or_default();
|
||||||
|
|
||||||
rooms.push((room.into(), name));
|
rooms.push((room.into(), name, tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
return rooms;
|
return rooms;
|
||||||
@@ -870,13 +1009,7 @@ impl ClientWorker {
|
|||||||
|
|
||||||
let msgs = chunk.into_iter().filter_map(|ev| {
|
let msgs = chunk.into_iter().filter_map(|ev| {
|
||||||
match ev.event.deserialize() {
|
match ev.event.deserialize() {
|
||||||
Ok(AnyTimelineEvent::MessageLike(msg)) => {
|
Ok(AnyTimelineEvent::MessageLike(msg)) => Some(msg),
|
||||||
if let AnyMessageLikeEvent::RoomMessage(msg) = msg {
|
|
||||||
Some(msg)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Ok(AnyTimelineEvent::State(_)) => None,
|
Ok(AnyTimelineEvent::State(_)) => None,
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
}
|
}
|
||||||
@@ -896,27 +1029,6 @@ impl ClientWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_room(&mut self, room_id: OwnedRoomId, field: SetRoomField) -> IambResult<()> {
|
|
||||||
let room = if let Some(r) = self.client.get_joined_room(&room_id) {
|
|
||||||
r
|
|
||||||
} else {
|
|
||||||
return Err(IambError::UnknownRoom(room_id).into());
|
|
||||||
};
|
|
||||||
|
|
||||||
match field {
|
|
||||||
SetRoomField::Name(name) => {
|
|
||||||
let ev = RoomNameEventContent::new(name.into());
|
|
||||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
|
||||||
},
|
|
||||||
SetRoomField::Topic(topic) => {
|
|
||||||
let ev = RoomTopicEventContent::new(topic);
|
|
||||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn space_members(&mut self, space: OwnedRoomId) -> IambResult<Vec<OwnedRoomId>> {
|
async fn space_members(&mut self, space: OwnedRoomId) -> IambResult<Vec<OwnedRoomId>> {
|
||||||
let mut req = SpaceHierarchyRequest::new(&space);
|
let mut req = SpaceHierarchyRequest::new(&space);
|
||||||
req.limit = Some(1000u32.into());
|
req.limit = Some(1000u32.into());
|
||||||
@@ -1015,12 +1127,12 @@ impl ClientWorker {
|
|||||||
let methods = vec![VerificationMethod::SasV1];
|
let methods = vec![VerificationMethod::SasV1];
|
||||||
let request = identity.request_verification_with_methods(methods);
|
let request = identity.request_verification_with_methods(methods);
|
||||||
let _req = request.await.map_err(IambError::from)?;
|
let _req = request.await.map_err(IambError::from)?;
|
||||||
let info = format!("Sent verification request to {}", user_id);
|
let info = format!("Sent verification request to {user_id}");
|
||||||
|
|
||||||
Ok(InfoMessage::from(info).into())
|
Ok(InfoMessage::from(info).into())
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
let msg = format!("Could not find identity information for {}", user_id);
|
let msg = format!("Could not find identity information for {user_id}");
|
||||||
let err = UIError::Failure(msg);
|
let err = UIError::Failure(msg);
|
||||||
|
|
||||||
Err(err)
|
Err(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user