mirror of
https://github.com/youwen5/iamb.git
synced 2025-06-19 21:29:52 -07:00
Update to matrix-sdk@0.7.1 (#200)
This commit is contained in:
parent
1948d80ec8
commit
9732971fc2
13 changed files with 1579 additions and 754 deletions
126
src/config.rs
126
src/config.rs
|
@ -5,20 +5,29 @@ use std::collections::HashMap;
|
|||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::BufReader;
|
||||
use std::io::{BufReader, BufWriter};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
||||
use clap::Parser;
|
||||
use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
|
||||
use matrix_sdk::matrix_auth::MatrixSession;
|
||||
use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
|
||||
use ratatui::style::{Color, Modifier as StyleModifier, Style};
|
||||
use ratatui::text::Span;
|
||||
use ratatui_image::picker::ProtocolType;
|
||||
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
|
||||
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer, Serialize};
|
||||
use tracing::Level;
|
||||
use url::Url;
|
||||
|
||||
use super::base::{IambId, RoomInfo, SortColumn, SortFieldRoom, SortFieldUser, SortOrder};
|
||||
use super::base::{
|
||||
IambError,
|
||||
IambId,
|
||||
RoomInfo,
|
||||
SortColumn,
|
||||
SortFieldRoom,
|
||||
SortFieldUser,
|
||||
SortOrder,
|
||||
};
|
||||
|
||||
macro_rules! usage {
|
||||
( $($args: tt)* ) => {
|
||||
|
@ -215,6 +224,40 @@ impl<'de> Deserialize<'de> for UserColor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub struct Session {
|
||||
access_token: String,
|
||||
refresh_token: Option<String>,
|
||||
user_id: OwnedUserId,
|
||||
device_id: OwnedDeviceId,
|
||||
}
|
||||
|
||||
impl From<Session> for MatrixSession {
|
||||
fn from(session: Session) -> Self {
|
||||
MatrixSession {
|
||||
tokens: matrix_sdk::matrix_auth::MatrixSessionTokens {
|
||||
access_token: session.access_token,
|
||||
refresh_token: session.refresh_token,
|
||||
},
|
||||
meta: matrix_sdk::SessionMeta {
|
||||
user_id: session.user_id,
|
||||
device_id: session.device_id,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MatrixSession> for Session {
|
||||
fn from(session: MatrixSession) -> Self {
|
||||
Session {
|
||||
access_token: session.tokens.access_token,
|
||||
refresh_token: session.tokens.refresh_token,
|
||||
user_id: session.meta.user_id,
|
||||
device_id: session.meta.device_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
pub struct UserDisplayTunables {
|
||||
pub color: Option<UserColor>,
|
||||
|
@ -421,14 +464,35 @@ impl Tunables {
|
|||
#[derive(Clone)]
|
||||
pub struct DirectoryValues {
|
||||
pub cache: PathBuf,
|
||||
pub data: PathBuf,
|
||||
pub logs: PathBuf,
|
||||
pub downloads: Option<PathBuf>,
|
||||
pub image_previews: PathBuf,
|
||||
}
|
||||
|
||||
impl DirectoryValues {
|
||||
fn create_dir_all(&self) -> std::io::Result<()> {
|
||||
use std::fs::create_dir_all;
|
||||
|
||||
let Self { cache, data, logs, downloads, image_previews } = self;
|
||||
|
||||
create_dir_all(cache)?;
|
||||
create_dir_all(data)?;
|
||||
create_dir_all(logs)?;
|
||||
create_dir_all(image_previews)?;
|
||||
|
||||
if let Some(downloads) = downloads {
|
||||
create_dir_all(downloads)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
pub struct Directories {
|
||||
pub cache: Option<PathBuf>,
|
||||
pub data: Option<PathBuf>,
|
||||
pub logs: Option<PathBuf>,
|
||||
pub downloads: Option<PathBuf>,
|
||||
pub image_previews: Option<PathBuf>,
|
||||
|
@ -438,6 +502,7 @@ impl Directories {
|
|||
fn merge(self, other: Self) -> Self {
|
||||
Directories {
|
||||
cache: self.cache.or(other.cache),
|
||||
data: self.data.or(other.data),
|
||||
logs: self.logs.or(other.logs),
|
||||
downloads: self.downloads.or(other.downloads),
|
||||
image_previews: self.image_previews.or(other.image_previews),
|
||||
|
@ -454,6 +519,15 @@ impl Directories {
|
|||
})
|
||||
.expect("no dirs.cache value configured!");
|
||||
|
||||
let data = self
|
||||
.data
|
||||
.or_else(|| {
|
||||
let mut dir = dirs::data_dir()?;
|
||||
dir.push("iamb");
|
||||
dir.into()
|
||||
})
|
||||
.expect("no dirs.data value configured!");
|
||||
|
||||
let logs = self.logs.unwrap_or_else(|| {
|
||||
let mut dir = cache.clone();
|
||||
dir.push("logs");
|
||||
|
@ -468,7 +542,7 @@ impl Directories {
|
|||
dir
|
||||
});
|
||||
|
||||
DirectoryValues { cache, logs, downloads, image_previews }
|
||||
DirectoryValues { cache, data, logs, downloads, image_previews }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -540,9 +614,11 @@ impl IambConfig {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct ApplicationSettings {
|
||||
pub matrix_dir: PathBuf,
|
||||
pub layout_json: PathBuf,
|
||||
pub session_json: PathBuf,
|
||||
pub session_json_old: PathBuf,
|
||||
pub sled_dir: PathBuf,
|
||||
pub sqlite_dir: PathBuf,
|
||||
pub profile_name: String,
|
||||
pub profile: ProfileConfig,
|
||||
pub tunables: TunableValues,
|
||||
|
@ -602,17 +678,30 @@ impl ApplicationSettings {
|
|||
let dirs = profile.dirs.take().unwrap_or_default().merge(dirs);
|
||||
let dirs = dirs.values();
|
||||
|
||||
// Create directories
|
||||
dirs.create_dir_all()?;
|
||||
|
||||
// Set up paths that live inside the profile's data directory.
|
||||
let mut profile_dir = config_dir.clone();
|
||||
profile_dir.push("profiles");
|
||||
profile_dir.push(profile_name.as_str());
|
||||
|
||||
let mut matrix_dir = profile_dir.clone();
|
||||
matrix_dir.push("matrix");
|
||||
let mut profile_data_dir = dirs.data.clone();
|
||||
profile_data_dir.push("profiles");
|
||||
profile_data_dir.push(profile_name.as_str());
|
||||
|
||||
let mut session_json = profile_dir;
|
||||
let mut sled_dir = profile_dir.clone();
|
||||
sled_dir.push("matrix");
|
||||
|
||||
let mut sqlite_dir = profile_data_dir.clone();
|
||||
sqlite_dir.push("sqlite");
|
||||
|
||||
let mut session_json = profile_data_dir.clone();
|
||||
session_json.push("session.json");
|
||||
|
||||
let mut session_json_old = profile_dir;
|
||||
session_json_old.push("session.json");
|
||||
|
||||
// Set up paths that live inside the profile's cache directory.
|
||||
let mut cache_dir = dirs.cache.clone();
|
||||
cache_dir.push("profiles");
|
||||
|
@ -622,9 +711,11 @@ impl ApplicationSettings {
|
|||
layout_json.push("layout.json");
|
||||
|
||||
let settings = ApplicationSettings {
|
||||
matrix_dir,
|
||||
sled_dir,
|
||||
layout_json,
|
||||
session_json,
|
||||
session_json_old,
|
||||
sqlite_dir,
|
||||
profile_name,
|
||||
profile,
|
||||
tunables,
|
||||
|
@ -635,6 +726,21 @@ impl ApplicationSettings {
|
|||
Ok(settings)
|
||||
}
|
||||
|
||||
pub fn read_session(&self, path: impl AsRef<Path>) -> Result<Session, IambError> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let session = serde_json::from_reader(reader).map_err(IambError::from)?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
pub fn write_session(&self, session: MatrixSession) -> Result<(), IambError> {
|
||||
let file = File::create(self.session_json.as_path())?;
|
||||
let writer = BufWriter::new(file);
|
||||
let session = Session::from(session);
|
||||
serde_json::to_writer(writer, &session).map_err(IambError::from)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_user_char_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
||||
let (color, c) = self
|
||||
.tunables
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue