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;
|
2023-01-06 16:56:28 -08:00
|
|
|
use modalkit::tui::style::Color;
|
2023-01-10 19:59:30 -08:00
|
|
|
use tokio::sync::mpsc::unbounded_channel;
|
2023-01-06 16:56:28 -08:00
|
|
|
use url::Url;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo},
|
2023-01-06 16:56:28 -08:00
|
|
|
config::{
|
|
|
|
ApplicationSettings,
|
|
|
|
DirectoryValues,
|
|
|
|
ProfileConfig,
|
|
|
|
TunableValues,
|
|
|
|
UserColor,
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 21:20:32 -08:00
|
|
|
let content = MessageEvent::Local(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-01-19 16:05:02 -08:00
|
|
|
pub fn mock_keys() -> HashMap<OwnedEventId, MessageKey> {
|
|
|
|
let mut keys = HashMap::new();
|
|
|
|
|
|
|
|
keys.insert(MSG1_EVID.clone(), MSG1_KEY.clone());
|
|
|
|
keys.insert(MSG2_EVID.clone(), MSG2_KEY.clone());
|
|
|
|
keys.insert(MSG3_EVID.clone(), MSG3_KEY.clone());
|
|
|
|
keys.insert(MSG4_EVID.clone(), MSG4_KEY.clone());
|
|
|
|
keys.insert(MSG5_EVID.clone(), MSG5_KEY.clone());
|
|
|
|
|
|
|
|
keys
|
|
|
|
}
|
|
|
|
|
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-19 16:05:02 -08:00
|
|
|
|
|
|
|
keys: mock_keys(),
|
2022-12-29 18:00:59 -08:00
|
|
|
messages: mock_messages(),
|
2023-01-19 16:05:02 -08:00
|
|
|
|
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,
|
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(),
|
|
|
|
logs: PathBuf::new(),
|
|
|
|
downloads: PathBuf::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:56:28 -08:00
|
|
|
pub fn mock_tunables() -> TunableValues {
|
|
|
|
TunableValues {
|
|
|
|
typing_notice: true,
|
|
|
|
typing_notice_display: true,
|
|
|
|
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
|
|
|
color: Some(UserColor(Color::Black)),
|
|
|
|
name: Some("USER 5".into()),
|
|
|
|
})]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<HashMap<_, _>>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
pub fn mock_settings() -> ApplicationSettings {
|
|
|
|
ApplicationSettings {
|
|
|
|
matrix_dir: PathBuf::new(),
|
|
|
|
session_json: PathBuf::new(),
|
|
|
|
profile_name: "test".into(),
|
|
|
|
profile: ProfileConfig {
|
|
|
|
user_id: user_id!("@user:example.com").to_owned(),
|
|
|
|
url: Url::parse("https://example.com").unwrap(),
|
2023-01-03 13:57:28 -08:00
|
|
|
settings: None,
|
|
|
|
dirs: 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(),
|
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());
|
|
|
|
let room_id = TEST_ROOM1_ID.clone();
|
|
|
|
let info = mock_room();
|
|
|
|
|
|
|
|
store.rooms.insert(room_id, info);
|
|
|
|
|
|
|
|
ProgramStore::new(store)
|
|
|
|
}
|