Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions etc/app/config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ user:
# Leave empty to use the default (baibot).
name: baibot

# An optional path to a 768x768 PNG to be used as a custom avatar image.
# Leave empty to use the default.
avatar:
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things:

  • I suppose other image resolutions are also OK. It doesn't need to be 768x768. It's just what the default avatar image is like, but anything else is OK. Still, having a somewhat high-res image is probably sensible

  • I suppose PNG is a requirement right now, because of the LOGO_MIME_TYPE constant. With some extra work, we may be able to get rid of it

  • avatar_path may be a better name for this setting, if this will deal with filesystem paths only

  • if we'll be adding other avatar-related options, it may be better to expand this section and have sub-keys.

    Example other options:

    • avatar.manage = true|false (allows people to the bot to not touch the currently-configured avatar)
    • avatar.path = ... (file path)
    • avatar.source = ... (a more generic name than path, so it would also allow handling of both file paths and MXC URIs). One could potentially specify the avatar using something like mxc://example.com/... in which case we won't be doing upload_avatar(), but rather set_avatar_url()
    • avatar.mime_type = ... (potentially allowing for non-PNG images to be used). This may be unnecessary if we can do some-detection

I suppose this complicates things a lot, but also covers a bunch of additional use-cases and makes this feature complete.


encryption:
# An optional passphrase to use for backing up and recovering the bot's encryption keys.
# You can use any string here.
Expand Down
25 changes: 20 additions & 5 deletions src/bot/implementation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fs;
use std::{future::Future, pin::Pin};

use mxlink::matrix_sdk::Room;
Expand Down Expand Up @@ -316,6 +317,18 @@ impl Bot {
}
}

let logo_bytes: Option<Vec<u8>> = if self.inner.config.user.avatar.is_none() {
Some(LOGO_BYTES.to_vec())
} else {
let avatar_path = self.inner.config.user.avatar.as_ref().unwrap();
match fs::read(avatar_path) {
Ok(bytes) => Some(bytes),
Err(_e) => {
Some(LOGO_BYTES.to_vec())
}
}
};
Comment on lines +320 to +330
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is right now, all if code paths return Some stuff, so there's no need to have logo_bytes be a Option.

If it's just Vec<u8> code below can remain as it was before.

That said, if we introduce additional avatar options (as discussed in my other comment in this review), then this may change.


let should_update_avatar = match &current_avatar_url {
Some(avatar_url) => {
let request = MediaRequestParameters {
Expand All @@ -328,7 +341,7 @@ impl Bot {
.await
.map_err(|e| anyhow::anyhow!("Failed fetching existing avatar: {:?}", e))?;

content.as_slice() != LOGO_BYTES
Some(content.as_slice()) != logo_bytes.as_deref()
}
None => true,
};
Expand All @@ -340,10 +353,12 @@ impl Bot {
.parse()
.expect("Failed parsing mime type for logo");

account
.upload_avatar(&mime_type, LOGO_BYTES.to_vec())
.await
.map_err(|e| anyhow::anyhow!("Failed uploading avatar: {:?}", e))?;
if let Some(bytes) = logo_bytes {
account
.upload_avatar(&mime_type, bytes)
.await
.map_err(|e| anyhow::anyhow!("Failed uploading avatar: {:?}", e))?;
}
}

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/bot/load_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub fn load() -> anyhow::Result<Config> {
config.user.encryption.recovery_reset_allowed = value.parse::<bool>()?;
}
cfg_env::BAIBOT_USER_NAME => config.user.name = value,
cfg_env::BAIBOT_USER_AVATAR => {
config.user.avatar = Some(value);
}
cfg_env::BAIBOT_COMMAND_PREFIX => config.command_prefix = value,
cfg_env::BAIBOT_ROOM_POST_JOIN_SELF_INTRODUCTION_ENABLED => {
config.room.post_join_self_introduction_enabled = value.parse::<bool>()?;
Expand Down
3 changes: 3 additions & 0 deletions src/entity/cfg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub struct ConfigUser {

#[serde(default)]
pub encryption: ConfigUserEncryption,

#[serde(default)]
pub avatar: Option<String>,
}

impl ConfigUser {
Expand Down
1 change: 1 addition & 0 deletions src/entity/cfg/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub const BAIBOT_HOMESERVER_URL: &str = "BAIBOT_HOMESERVER_URL";
pub const BAIBOT_USER_MXID_LOCALPART: &str = "BAIBOT_USER_MXID_LOCALPART";
pub const BAIBOT_USER_PASSWORD: &str = "BAIBOT_USER_PASSWORD";
pub const BAIBOT_USER_NAME: &str = "BAIBOT_USER_NAME";
pub const BAIBOT_USER_AVATAR: &str = "BAIBOT_USER_AVATAR";
pub const BAIBOT_USER_ENCRYPTION_RECOVERY_PASSPHRASE: &str =
"BAIBOT_USER_ENCRYPTION_RECOVERY_PASSPHRASE";
pub const BAIBOT_USER_ENCRYPTION_RECOVERY_RESET_ALLOWED: &str =
Expand Down