Skip to content

Commit

Permalink
fix: 🐛 user and bot avatar lost
Browse files Browse the repository at this point in the history
  • Loading branch information
sologeek authored and wjian23 committed Jan 29, 2024
1 parent 9242851 commit b0c6883
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
3 changes: 1 addition & 2 deletions config/bot.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"name": "Polestar Assistant",
"desc": "AI Assistant powered by OpenAI.",
"avatar": {
"name": "🌉",
"color": "#EDF7FBFF"
"url": "outlined_logo.png"
},
"cat": "Assistant",
"tags": [
Expand Down
43 changes: 38 additions & 5 deletions core/src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use home::home_dir;

Expand All @@ -15,6 +15,7 @@ static CURRENT_USER: &str = "current_user";
static NONCE_FILE: &str = "nonce";
static TOKEN_FILE: &str = "token";
static LOCAL_STATE: &str = "local_state";
static POLESTAR_STATIC: &str = "static";

pub fn project_home_path() -> PathBuf {
home_dir()
Expand Down Expand Up @@ -103,6 +104,14 @@ pub fn create_if_not_exist_dir(path: PathBuf) {
}
}

pub fn get_static_file(file: &str) -> PolestarResult<Vec<u8>> {
let mut path = project_home_path();
path.push(POLESTAR_STATIC);
path.push(file);
let content = std::fs::read(&path)?;
Ok(content)
}

pub mod encrypt {
use std::{fs::File, io::Write};

Expand Down Expand Up @@ -156,18 +165,18 @@ pub mod encrypt {
}

pub mod launch {
use std::{fs, io::Write};
use std::{fs, io::Write, path::PathBuf};

use super::{
create_if_not_exist_dir, project_bot_config_path, project_config_path, project_home_path,
project_user_path,
copy_dir_all, create_if_not_exist_dir, project_bot_config_path, project_config_path,
project_home_path, project_user_path, POLESTAR_STATIC,
};

pub fn setup_project() {
create_if_not_exist_dir(project_home_path());
create_if_not_exist_dir(project_user_path());
create_if_not_exist_dir(project_config_path());
write_default_bot_config();
copy_static_files_to_user_data();
}

pub fn write_default_bot_config() {
Expand All @@ -183,4 +192,28 @@ pub mod launch {
.write_all(content.as_bytes())
.expect("can't write bot.json");
}

pub fn copy_static_files_to_user_data() {
let mut src = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
src.push("../gui/static");
let mut dst = project_home_path();
dst.push(POLESTAR_STATIC);
copy_dir_all(src, dst).expect("Failed to copy static files");
}
}

use std::{fs, io};

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
16 changes: 10 additions & 6 deletions gui/src/widgets/common/avatar.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use polestar_core::model::BotAvatar;
use polestar_core::{get_static_file, model::BotAvatar};
use ribir::prelude::*;

pub fn w_avatar(avatar: BotAvatar) -> impl WidgetBuilder {
fn_widget! {
@ {
match avatar {
BotAvatar::Image { url: _ } => {
let data = vec![0];
@Avatar {
@ { ShareResource::new(PixelImage::from_png(&data)) }
}.widget_build(ctx!())
BotAvatar::Image { url } => {
let data = get_static_file(&url);
if let Ok(data) = data {
@Avatar {
@ { ShareResource::new(PixelImage::from_png(&data)) }
}.widget_build(ctx!())
} else {
@Void {}.widget_build(ctx!())
}
}
BotAvatar::Text { name, color } => {
@Avatar {
Expand Down
26 changes: 22 additions & 4 deletions gui/src/widgets/home/chat/msg_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use polestar_core::model::{ChannelId, MsgCont, MsgId, MsgRole};
use polestar_core::model::{BotAvatar, ChannelId, MsgCont, MsgId, MsgRole};
use ribir::prelude::*;
use uuid::Uuid;

use crate::style::decorator::channel::message_style;
use crate::style::{GAINSBORO, WHITE};
use crate::theme::polestar_svg;
use crate::widgets::app::Chat;
use crate::widgets::common::IconButton;
use crate::widgets::common::{w_avatar, IconButton};
use crate::widgets::helper::send_msg;

use super::onboarding::w_msg_onboarding;
Expand Down Expand Up @@ -254,15 +254,33 @@ fn w_msg(
}
}
};
let avatar = match role {
MsgRole::User => {
BotAvatar::Image {
url: "user_avatar.png".to_owned(),
}
}
MsgRole::Bot(bot_id) => {
let chat = $chat;
chat.info().bot(&bot_id).map(|bot| bot.avatar().clone()).unwrap_or(BotAvatar::Image {
url: "outlined_logo.png".to_owned(),
})
}
MsgRole::System(_) => {
BotAvatar::Image {
url: "outlined_logo.png".to_owned(),
}
}
};
@$stack {
@Row {
h_align: match msg.role() {
MsgRole::User => HAlign::Right,
_ => HAlign::Left,
},
@$row {
@Avatar {
@ { Label::new("A") }
@ {
w_avatar(avatar.clone())
}
@Column {
@ {
Expand Down
6 changes: 1 addition & 5 deletions gui/src/widgets/home/sidebar/channel_thumbnail_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub fn w_channel_thumbnail_list(
@InteractiveList {
active: pipe! {
let channels = $channel_mgr.channel_ids();
let last_idx = if !channels.is_empty() {
channels.len() - 1
} else {
0
};
let last_idx = (channels.len() - 1).min(0);
$channel_mgr.cur_channel_id().and_then(|id| {
channels.iter().position(|ch| ch == id).map(|idx| last_idx - idx)
}).unwrap_or(last_idx)
Expand Down
Binary file added gui/static/user_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b0c6883

Please sign in to comment.