Skip to content

Commit

Permalink
Merge pull request #1325 from HTGAzureX1212/nightly
Browse files Browse the repository at this point in the history
Cache: Add `roles` to Member Entity
  • Loading branch information
HTGAzureX1212 authored Aug 12, 2023
2 parents 3d9008e + 86ab48b commit 702eacb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use hartex_discord_core::discord::model::guild::Member;
use hartex_discord_core::discord::model::id::marker::GuildMarker;
use hartex_discord_core::discord::model::id::marker::RoleMarker;
use hartex_discord_core::discord::model::id::marker::UserMarker;
use hartex_discord_core::discord::model::id::Id;
use hartex_discord_entitycache_core::Entity;
Expand All @@ -31,6 +32,7 @@ use hartex_discord_entitycache_core::Entity;
pub struct MemberEntity {
#[entity(id)]
pub guild_id: Id<GuildMarker>,
pub roles: Vec<Id<RoleMarker>>,
#[entity(id)]
pub user_id: Id<UserMarker>,
}
Expand All @@ -39,6 +41,7 @@ impl From<(Member, Id<GuildMarker>)> for MemberEntity {
fn from((member, guild_id): (Member, Id<GuildMarker>)) -> Self {
Self {
guild_id,
roles: member.roles,
user_id: member.user.id,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
*/

use std::env;
use std::str::FromStr;

use hartex_discord_core::discord::model::id::marker::GuildMarker;
use hartex_discord_core::discord::model::id::marker::RoleMarker;
use hartex_discord_core::discord::model::id::marker::UserMarker;
use hartex_discord_core::discord::model::id::Id;
use hartex_discord_entitycache_core::error::CacheResult;
Expand Down Expand Up @@ -72,7 +74,18 @@ impl Repository<MemberEntity> for CachedMemberRepository {
&self,
(guild_id, user_id): <MemberEntity as Entity>::Id,
) -> CacheResult<MemberEntity> {
Ok(MemberEntity { guild_id, user_id })
let pass = env::var("DOCKER_REDIS_REQUIREPASS")?;
let client = Client::open(format!("redis://:{pass}@127.0.0.1/"))?;
let mut connection = client.get_tokio_connection().await?;

let roles = connection
.get::<String, String>(format!("guild:{guild_id}:member:{user_id}:roles"))
.await?
.split(',')
.map(|str| Id::<RoleMarker>::from_str(str).unwrap())
.collect::<Vec<_>>();

Ok(MemberEntity { guild_id, roles, user_id })
}

async fn upsert(&self, entity: MemberEntity) -> CacheResult<()> {
Expand All @@ -88,6 +101,20 @@ impl Repository<MemberEntity> for CachedMemberRepository {
entity.user_id.get(),
)
.await?;
connection
.set(
format!(
"guild:{}:member:{}:roles",
entity.guild_id, entity.user_id
),
entity
.roles
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<String>>()
.join(",")
)
.await?;

Ok(())
}
Expand Down

0 comments on commit 702eacb

Please sign in to comment.