2023-01-06 16:56:28 -08:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
use std::path::PathBuf;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
use matrix_sdk::ruma::{
|
|
|
|
event_id,
|
2023-01-12 21:20:32 -08:00
|
|
|
events::room::message::{OriginalRoomMessageEvent, RoomMessageEventContent},
|
2022-12-29 18:00:59 -08:00
|
|
|
server_name,
|
|
|
|
user_id,
|
|
|
|
EventId,
|
2023-01-12 21:20:32 -08:00
|
|
|
OwnedEventId,
|
2022-12-29 18:00:59 -08:00
|
|
|
OwnedRoomId,
|
|
|
|
OwnedUserId,
|
|
|
|
RoomId,
|
|
|
|
UInt,
|
|
|
|
};
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2024-02-27 21:21:05 -08:00
|
|
|
use ratatui::style::{Color, Style};
|
2023-01-10 19:59:30 -08:00
|
|
|
use tokio::sync::mpsc::unbounded_channel;
|
2023-03-13 16:10:28 -07:00
|
|
|
use tracing::Level;
|
2023-01-06 16:56:28 -08:00
|
|
|
use url::Url;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
use crate::{
|
2023-02-09 17:53:33 -08:00
|
|
|
base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo},
|
2023-01-06 16:56:28 -08:00
|
|
|
config::{
|
2023-01-26 15:40:16 -08:00
|
|
|
user_color,
|
|
|
|
user_style_from_color,
|
2023-01-06 16:56:28 -08:00
|
|
|
ApplicationSettings,
|
|
|
|
DirectoryValues,
|
|
|
|
ProfileConfig,
|
2023-10-20 19:32:33 -07:00
|
|
|
SortOverrides,
|
2023-01-06 16:56:28 -08:00
|
|
|
TunableValues,
|
|
|
|
UserColor,
|
2023-07-06 23:15:58 -07:00
|
|
|
UserDisplayStyle,
|
2023-01-06 16:56:28 -08:00
|
|
|
UserDisplayTunables,
|
|
|
|
},
|
2022-12-29 18:00:59 -08:00
|
|
|
message::{
|
|
|
|
Message,
|
2023-01-12 21:20:32 -08:00
|
|
|
MessageEvent,
|
2022-12-29 18:00:59 -08:00
|
|
|
MessageKey,
|
|
|
|
MessageTimeStamp::{LocalEcho, OriginServer},
|
|
|
|
Messages,
|
|
|
|
},
|
|
|
|
worker::Requester,
|
|
|
|
};
|
|
|
|
|
2023-05-24 21:14:13 -07:00
|
|
|
const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned();
|
|
|
|
pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
|
|
|
|
pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned();
|
2023-01-06 16:56:28 -08:00
|
|
|
pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
|
|
|
|
pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
|
|
|
|
pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
|
2023-01-12 21:20:32 -08:00
|
|
|
pub static ref MSG1_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
|
|
|
pub static ref MSG2_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
|
|
|
pub static ref MSG3_EVID: OwnedEventId =
|
|
|
|
event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
|
|
|
|
pub static ref MSG4_EVID: OwnedEventId =
|
|
|
|
event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
|
|
|
|
pub static ref MSG5_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
|
|
|
pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
|
|
|
|
pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
|
|
|
|
pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
|
|
|
|
pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
|
|
|
|
pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
pub fn user_style(user: &str) -> Style {
|
|
|
|
user_style_from_color(user_color(user))
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
pub fn mock_room1_message(
|
|
|
|
content: RoomMessageEventContent,
|
|
|
|
sender: OwnedUserId,
|
|
|
|
key: MessageKey,
|
|
|
|
) -> Message {
|
|
|
|
let origin_server_ts = key.0.as_millis().unwrap();
|
|
|
|
let event_id = key.1;
|
|
|
|
|
|
|
|
let event = OriginalRoomMessageEvent {
|
|
|
|
content,
|
|
|
|
event_id,
|
|
|
|
sender,
|
|
|
|
origin_server_ts,
|
|
|
|
room_id: TEST_ROOM1_ID.clone(),
|
|
|
|
unsigned: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
event.into()
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_message1() -> Message {
|
|
|
|
let content = RoomMessageEventContent::text_plain("writhe");
|
2023-01-26 15:40:16 -08:00
|
|
|
let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_message2() -> Message {
|
|
|
|
let content = RoomMessageEventContent::text_plain("helium");
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_message3() -> Message {
|
|
|
|
let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_message4() -> Message {
|
|
|
|
let content = RoomMessageEventContent::text_plain("help");
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_message5() -> Message {
|
|
|
|
let content = RoomMessageEventContent::text_plain("character");
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-02-09 17:53:33 -08:00
|
|
|
pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
|
2023-01-19 16:05:02 -08:00
|
|
|
let mut keys = HashMap::new();
|
|
|
|
|
2023-02-09 17:53:33 -08:00
|
|
|
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()));
|
2023-01-19 16:05:02 -08:00
|
|
|
|
|
|
|
keys
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
pub fn mock_messages() -> Messages {
|
|
|
|
let mut messages = BTreeMap::new();
|
|
|
|
|
|
|
|
messages.insert(MSG1_KEY.clone(), mock_message1());
|
|
|
|
messages.insert(MSG2_KEY.clone(), mock_message2());
|
|
|
|
messages.insert(MSG3_KEY.clone(), mock_message3());
|
|
|
|
messages.insert(MSG4_KEY.clone(), mock_message4());
|
|
|
|
messages.insert(MSG5_KEY.clone(), mock_message5());
|
|
|
|
|
|
|
|
messages
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mock_room() -> RoomInfo {
|
|
|
|
RoomInfo {
|
|
|
|
name: Some("Watercooler Discussion".into()),
|
2023-01-26 15:40:16 -08:00
|
|
|
tags: None,
|
2023-01-19 16:05:02 -08:00
|
|
|
|
|
|
|
keys: mock_keys(),
|
2022-12-29 18:00:59 -08:00
|
|
|
messages: mock_messages(),
|
2023-01-26 15:40:16 -08:00
|
|
|
|
2023-10-15 18:12:39 -07:00
|
|
|
event_receipts: HashMap::new(),
|
|
|
|
user_receipts: HashMap::new(),
|
2023-02-09 17:53:33 -08:00
|
|
|
reactions: HashMap::new(),
|
2023-01-19 16:05:02 -08:00
|
|
|
|
2023-03-13 10:46:26 -07:00
|
|
|
fetching: false,
|
2022-12-29 18:00:59 -08:00
|
|
|
fetch_id: RoomFetchStatus::NotStarted,
|
|
|
|
fetch_last: None,
|
2023-01-03 13:57:28 -08:00
|
|
|
users_typing: None,
|
2023-07-06 23:15:58 -07:00
|
|
|
display_names: HashMap::new(),
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 18:12:25 -08:00
|
|
|
pub fn mock_dirs() -> DirectoryValues {
|
|
|
|
DirectoryValues {
|
|
|
|
cache: PathBuf::new(),
|
2024-03-02 15:00:29 -08:00
|
|
|
data: PathBuf::new(),
|
2023-01-05 18:12:25 -08:00
|
|
|
logs: PathBuf::new(),
|
2023-07-07 20:35:01 -07:00
|
|
|
downloads: None,
|
2023-11-16 08:36:22 -08:00
|
|
|
image_previews: PathBuf::new(),
|
2023-01-05 18:12:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:56:28 -08:00
|
|
|
pub fn mock_tunables() -> TunableValues {
|
|
|
|
TunableValues {
|
2023-01-28 14:09:40 -08:00
|
|
|
default_room: None,
|
2023-03-13 16:10:28 -07:00
|
|
|
log_level: Level::INFO,
|
2023-02-09 17:53:33 -08:00
|
|
|
reaction_display: true,
|
|
|
|
reaction_shortcode_display: false,
|
2023-01-26 15:40:16 -08:00
|
|
|
read_receipt_send: true,
|
|
|
|
read_receipt_display: true,
|
2023-03-12 15:43:13 -07:00
|
|
|
request_timeout: 120,
|
2023-10-20 19:32:33 -07:00
|
|
|
sort: SortOverrides::default().values(),
|
2023-01-26 15:40:16 -08:00
|
|
|
typing_notice_send: true,
|
2023-01-06 16:56:28 -08:00
|
|
|
typing_notice_display: true,
|
|
|
|
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
|
|
|
color: Some(UserColor(Color::Black)),
|
|
|
|
name: Some("USER 5".into()),
|
|
|
|
})]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<HashMap<_, _>>(),
|
2023-05-01 12:07:54 +01:00
|
|
|
open_command: None,
|
2023-07-06 23:15:58 -07:00
|
|
|
username_display: UserDisplayStyle::Username,
|
2024-02-28 06:52:24 +00:00
|
|
|
message_user_color: false,
|
2023-11-16 08:36:22 -08:00
|
|
|
image_preview: None,
|
2023-01-06 16:56:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
pub fn mock_settings() -> ApplicationSettings {
|
|
|
|
ApplicationSettings {
|
2023-06-28 23:42:31 -07:00
|
|
|
layout_json: PathBuf::new(),
|
2022-12-29 18:00:59 -08:00
|
|
|
session_json: PathBuf::new(),
|
2024-03-02 15:00:29 -08:00
|
|
|
session_json_old: PathBuf::new(),
|
|
|
|
sled_dir: PathBuf::new(),
|
|
|
|
sqlite_dir: PathBuf::new(),
|
2023-06-28 23:42:31 -07:00
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
profile_name: "test".into(),
|
|
|
|
profile: ProfileConfig {
|
|
|
|
user_id: user_id!("@user:example.com").to_owned(),
|
2024-02-28 23:21:31 -08:00
|
|
|
url: None,
|
2023-01-03 13:57:28 -08:00
|
|
|
settings: None,
|
|
|
|
dirs: None,
|
2023-06-28 23:42:31 -07:00
|
|
|
layout: None,
|
2022-12-29 18:00:59 -08:00
|
|
|
},
|
2023-01-06 16:56:28 -08:00
|
|
|
tunables: mock_tunables(),
|
2023-01-05 18:12:25 -08:00
|
|
|
dirs: mock_dirs(),
|
2023-06-28 23:42:31 -07:00
|
|
|
layout: Default::default(),
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:59:30 -08:00
|
|
|
pub async fn mock_store() -> ProgramStore {
|
|
|
|
let (tx, _) = unbounded_channel();
|
|
|
|
let homeserver = Url::parse("https://localhost").unwrap();
|
|
|
|
let client = matrix_sdk::Client::new(homeserver).await.unwrap();
|
|
|
|
let worker = Requester { tx, client };
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
let mut store = ChatStore::new(worker, mock_settings());
|
2023-03-01 18:46:33 -08:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
let room_id = TEST_ROOM1_ID.clone();
|
|
|
|
let info = mock_room();
|
|
|
|
|
2023-05-24 21:14:13 -07:00
|
|
|
store.rooms.insert(room_id.clone(), info);
|
|
|
|
store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
ProgramStore::new(store)
|
|
|
|
}
|