Skip to content

Commit

Permalink
Overrides Update (#32)
Browse files Browse the repository at this point in the history
* restructure channel cache

* check overrides for all parent channels
  • Loading branch information
circuitsacul authored Aug 4, 2022
1 parent 847634e commit 68a78aa
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 118 deletions.
102 changes: 51 additions & 51 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,57 @@
},
"query": "INSERT INTO members (user_id, guild_id) VALUES ($1, $2) RETURNING *"
},
"04b5d7f155912898490d7858fdcb12e19e97f14ed5e8c89c727880fcaecfd376": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "guild_id",
"ordinal": 1,
"type_info": "Int8"
},
{
"name": "name",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "starboard_id",
"ordinal": 3,
"type_info": "Int4"
},
{
"name": "channel_ids",
"ordinal": 4,
"type_info": "Int8Array"
},
{
"name": "overrides",
"ordinal": 5,
"type_info": "Json"
}
],
"nullable": [
false,
false,
false,
false,
false,
false
],
"parameters": {
"Left": [
"Int4",
"Int8Array"
]
}
},
"query": "SELECT * FROM overrides\n WHERE starboard_id=$1 AND\n channel_ids && $2::bigint[]"
},
"0503b7de215f55d8463a73b800bba7a9e1c48f91ba6d67e36c3b105f8b1a1484": {
"describe": {
"columns": [
Expand Down Expand Up @@ -2244,57 +2295,6 @@
},
"query": "SELECT * FROM starboard_messages WHERE\n starboard_message_id=$1"
},
"d1ea348f247afd449131c6450d242e23a52fd553e1ff59e7bdaf456ce6f5c94f": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "guild_id",
"ordinal": 1,
"type_info": "Int8"
},
{
"name": "name",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "starboard_id",
"ordinal": 3,
"type_info": "Int4"
},
{
"name": "channel_ids",
"ordinal": 4,
"type_info": "Int8Array"
},
{
"name": "overrides",
"ordinal": 5,
"type_info": "Json"
}
],
"nullable": [
false,
false,
false,
false,
false,
false
],
"parameters": {
"Left": [
"Int4",
"Int8"
]
}
},
"query": "SELECT * FROM overrides\n WHERE starboard_id=$1 AND\n channel_ids && array[$2]::bigint[]"
},
"db47dd094d91342ad83996e3ebe7cacc85c7f77f1e7ecd0c9d8ba6914fb9ec8a": {
"describe": {
"columns": [
Expand Down
73 changes: 59 additions & 14 deletions src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use twilight_model::{
};

use crate::{
cache::models::channel::CachedChannel,
client::bot::StarboardBot,
constants,
errors::StarboardResult,
Expand Down Expand Up @@ -97,6 +98,54 @@ impl Cache {
})
}

pub async fn qualified_channel_ids(
&self,
bot: &StarboardBot,
guild_id: Id<GuildMarker>,
channel_id: Id<ChannelMarker>,
) -> StarboardResult<Vec<Id<ChannelMarker>>> {
let mut current_channel_id = Some(channel_id);
let mut channel_ids = Vec::new();

while let Some(channel_id) = current_channel_id {
channel_ids.push(channel_id);

let must_fetch = self.guilds.with(&guild_id, |_, guild| {
let guild = guild.as_ref().unwrap();

if let Some(thread_parent_id) = guild.active_thread_parents.get(&channel_id) {
current_channel_id = Some(thread_parent_id.to_owned());
return false;
}

if let Some(channel) = guild.channels.get(&channel_id) {
if let Some(parent_id) = channel.parent_id {
current_channel_id = Some(parent_id);
} else {
current_channel_id = None;
}
return false;
}

true
});

if must_fetch {
let channel = bot
.http
.channel(channel_id)
.exec()
.await?
.model()
.await
.unwrap();
current_channel_id = channel.parent_id;
}
}

Ok(channel_ids)
}

pub async fn fog_message(
&self,
bot: &StarboardBot,
Expand Down Expand Up @@ -186,16 +235,13 @@ impl Cache {
};

// check the cached nsfw/sfw channel list
if guild.nsfw_channels.contains(&channel_id) {
CachedResult::Cached(true)
} else if guild.sfw_channels.contains(&channel_id) {
CachedResult::Cached(false)
} else {
// return the parent_id, in case the channel_id was originally
// a thread. This isn't gauaranteed to be a parent channel - it
// could be a thread if the thread wasn't cached.
CachedResult::NotCached(channel_id)
if let Some(channel) = guild.channels.get(&channel_id) {
if let Some(nsfw) = channel.is_nsfw {
return CachedResult::Cached(nsfw);
}
}

CachedResult::NotCached(channel_id)
});

// handle the result
Expand All @@ -217,11 +263,10 @@ impl Cache {

// cache the value
self.guilds.alter(&guild_id, |_, mut guild| {
if is_nsfw {
guild.nsfw_channels.insert(parent.id);
} else {
guild.sfw_channels.insert(parent.id);
}
guild.channels.insert(
parent.id,
CachedChannel::from_channel(guild.channels.get(&parent.id), &parent),
);
guild
});

Expand Down
37 changes: 11 additions & 26 deletions src/cache/events/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use twilight_model::gateway::payload::incoming::{ChannelCreate, ChannelDelete, ChannelUpdate};

use crate::cache::{cache::Cache, update::UpdateCache};
use crate::cache::{cache::Cache, models::channel::CachedChannel, update::UpdateCache};

#[async_trait]
impl UpdateCache for ChannelCreate {
Expand All @@ -10,20 +10,13 @@ impl UpdateCache for ChannelCreate {
None => return,
Some(guild_id) => guild_id,
};
let is_nsfw = match self.nsfw {
None => return,
Some(nsfw) => nsfw,
};

cache.guilds.alter(&guild_id, |_, mut guild| {
match is_nsfw {
true => {
guild.nsfw_channels.insert(self.id);
}
false => {
guild.sfw_channels.insert(self.id);
}
}
let channel = guild.channels.get(&self.id);
guild
.channels
.insert(self.id, CachedChannel::from_channel(channel, self));

guild
})
}
Expand All @@ -38,8 +31,7 @@ impl UpdateCache for ChannelDelete {
};

cache.guilds.alter(&guild_id, |_, mut guild| {
guild.nsfw_channels.remove(&self.id);
guild.sfw_channels.remove(&self.id);
guild.channels.remove(&self.id);
guild.active_thread_parents = guild
.active_thread_parents
.into_iter()
Expand All @@ -58,19 +50,12 @@ impl UpdateCache for ChannelUpdate {
None => return,
Some(guild_id) => guild_id,
};
let is_nsfw = match self.nsfw {
None => return,
Some(nsfw) => nsfw,
};

cache.guilds.alter(&guild_id, |_, mut guild| {
if is_nsfw {
guild.sfw_channels.remove(&self.id);
guild.nsfw_channels.insert(self.id);
} else {
guild.nsfw_channels.remove(&self.id);
guild.sfw_channels.insert(self.id);
}
let channel = guild.channels.get(&self.id);
guild
.channels
.insert(self.id, CachedChannel::from_channel(channel, self));

guild
})
Expand Down
30 changes: 11 additions & 19 deletions src/cache/events/guild.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
use std::collections::HashSet;

use async_trait::async_trait;
use twilight_model::gateway::payload::incoming::{GuildCreate, GuildDelete, GuildEmojisUpdate};

use crate::cache::{cache::Cache, models::guild::CachedGuild, update::UpdateCache};
use crate::cache::{
cache::Cache,
models::{channel::CachedChannel, guild::CachedGuild},
update::UpdateCache,
};

#[async_trait]
impl UpdateCache for GuildCreate {
async fn update_cache(&self, cache: &Cache) {
let mut nsfw_channels = HashSet::new();
let mut sfw_channels = HashSet::new();

for channel in self.channels.iter() {
match channel.nsfw {
None => {}
Some(true) => {
nsfw_channels.insert(channel.id);
}
Some(false) => {
sfw_channels.insert(channel.id);
}
}
}
let channels = self
.channels
.iter()
.map(|c| (c.id, CachedChannel::from_channel(None, c)))
.collect();

let guild = CachedGuild {
emojis: self.emojis.iter().map(|e| e.id).collect(),
nsfw_channels,
sfw_channels,
channels,
active_thread_parents: self
.threads
.iter()
Expand Down
25 changes: 25 additions & 0 deletions src/cache/models/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use twilight_model::{
channel::Channel,
id::{marker::ChannelMarker, Id},
};

pub struct CachedChannel {
pub is_nsfw: Option<bool>,
pub parent_id: Option<Id<ChannelMarker>>,
}

impl CachedChannel {
pub fn from_channel(original: Option<&CachedChannel>, new: &Channel) -> Self {
if let Some(original) = original {
Self {
is_nsfw: new.nsfw.or(original.is_nsfw),
parent_id: new.parent_id,
}
} else {
Self {
is_nsfw: new.nsfw,
parent_id: new.parent_id,
}
}
}
}
6 changes: 4 additions & 2 deletions src/cache/models/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use twilight_model::id::{
Id,
};

use super::channel::CachedChannel;

pub struct CachedGuild {
pub emojis: HashSet<Id<EmojiMarker>>,
pub nsfw_channels: HashSet<Id<ChannelMarker>>,
pub sfw_channels: HashSet<Id<ChannelMarker>>,
/// all textable channels except for threads
pub channels: HashMap<Id<ChannelMarker>, CachedChannel>,
pub active_thread_parents: HashMap<Id<ChannelMarker>, Id<ChannelMarker>>,
}
1 change: 1 addition & 0 deletions src/cache/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod channel;
pub mod guild;
pub mod message;
pub mod user;
10 changes: 8 additions & 2 deletions src/core/starboard/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ impl StarboardConfig {
let starboards = Starboard::list_by_guild(&bot.pool, unwrap_id!(guild_id)).await?;
let mut configs = Vec::new();

let channel_id = unwrap_id!(channel_id);
let channel_ids: Vec<i64> = bot
.cache
.qualified_channel_ids(bot, guild_id, channel_id).await?
.into_iter()
.map(|cid| unwrap_id!(cid))
.collect();

for sb in starboards.into_iter() {
let overrides =
StarboardOverride::list_by_starboard_and_channel(&bot.pool, sb.id, channel_id)
StarboardOverride::list_by_starboard_and_channels(&bot.pool, sb.id, &channel_ids)
.await?;
configs.push(Self::new(sb, overrides)?);
}
Expand Down
Loading

0 comments on commit 68a78aa

Please sign in to comment.