Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-alpha.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
nekofar committed Sep 19, 2023
2 parents 447fcfe + a4d60dd commit ae57d73
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 89 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All notable changes to this project will be documented in this file.

### Refactor

- Update cache set and get operations for efficiency
- Update caching functions to use map and collect methods
- Update notification messages for clarity and tracking

## [1.0.0-alpha.6] - 2023-09-18

### Refactor

- Add new fields to Proposal and Vote in Prop House fetcher
- Update notification handling for new Prop House proposals and votes
- Move handler functions to DiscordHandler structs
Expand Down
64 changes: 63 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lilnouns-bots"
version = "1.0.0-alpha.6"
version = "1.0.0-alpha.7"
edition = "2021"
include = ["*.graphql"]

Expand All @@ -21,3 +21,4 @@ clap = { version = "4.4.3", features = ["derive"] }
serenity = { version = "0.11.6", default-features = false, features = ["rustls_backend", "model"] }
rocksdb = { version = "0.21.0", default-features = false, features = ["lz4"] }
html2md = "0.2.14"
chrono = "0.4.31"
48 changes: 21 additions & 27 deletions src/prop_house/cacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,57 @@ fn vote_cache_key(id: isize) -> String {

pub(crate) fn set_auction_cache(auction: &Auction) -> Result<()> {
let cache = &cache::CACHE;
let cache_key = auction_cache_key(auction.id);
cache.set(&cache_key, auction)
cache.set(auction_cache_key(auction.id), auction)
}

pub(crate) fn set_auctions_cache(auctions: &Vec<Auction>) -> Result<()> {
pub(crate) fn set_auctions_cache(auctions: &[Auction]) -> Result<()> {
let cache = &cache::CACHE;
let mut items = Vec::new();
for auction in auctions {
items.push((auction_cache_key(auction.id), auction))
}
let items: Vec<_> = auctions
.iter()
.map(|auction| (auction_cache_key(auction.id), auction))
.collect();
cache.set_batch(items)
}

pub(crate) fn get_auction_cache(id: isize) -> Result<Option<Auction>> {
let cache = &cache::CACHE;
let cache_key = auction_cache_key(id);
cache.get::<String, Auction>(&cache_key)
cache.get::<String, Auction>(&auction_cache_key(id))
}

pub(crate) fn set_proposal_cache(proposal: &Proposal) -> Result<()> {
let cache = &cache::CACHE;
let cache_key = proposal_cache_key(proposal.id);
cache.set(&cache_key, proposal)
cache.set(proposal_cache_key(proposal.id), proposal)
}

pub(crate) fn set_proposals_cache(proposals: &Vec<Proposal>) -> Result<()> {
pub(crate) fn set_proposals_cache(proposals: &[Proposal]) -> Result<()> {
let cache = &cache::CACHE;
let mut items = Vec::new();
for proposal in proposals {
items.push((proposal_cache_key(proposal.id), proposal))
}
let items: Vec<_> = proposals
.iter()
.map(|proposal| (proposal_cache_key(proposal.id), proposal))
.collect();
cache.set_batch(items)
}

pub(crate) fn get_proposal_cache(id: isize) -> Result<Option<Proposal>> {
let cache = &cache::CACHE;
let cache_key = proposal_cache_key(id);
cache.get::<String, Proposal>(&cache_key)
cache.get::<String, Proposal>(&proposal_cache_key(id))
}

pub(crate) fn set_vote_cache(vote: &Vote) -> Result<()> {
let cache = &cache::CACHE;
let cache_key = vote_cache_key(vote.id);
cache.set(&cache_key, vote)
cache.set(vote_cache_key(vote.id), vote)
}

pub(crate) fn set_votes_cache(votes: &Vec<Vote>) -> Result<()> {
pub(crate) fn set_votes_cache(votes: &[Vote]) -> Result<()> {
let cache = &cache::CACHE;
let mut items = Vec::new();
for vote in votes {
items.push((vote_cache_key(vote.id), vote))
}
let items: Vec<_> = votes
.iter()
.map(|vote| (vote_cache_key(vote.id), vote))
.collect();
cache.set_batch(items)
}

pub(crate) fn get_vote_cache(id: isize) -> Result<Option<Vote>> {
let cache = &cache::CACHE;
let cache_key = vote_cache_key(id);
cache.get::<String, Vote>(&cache_key)
cache.get::<String, Vote>(&vote_cache_key(id))
}
2 changes: 1 addition & 1 deletion src/prop_house/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) async fn fetch_auctions() -> Option<Vec<Auction>> {
.map(|auction| Auction {
id: auction.id.try_into().unwrap(),
title: auction.title.clone(),
description: html2md::parse_html(&*auction.description),
description: html2md::parse_html(&auction.description),
})
.collect();

Expand Down
39 changes: 26 additions & 13 deletions src/prop_house/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;

use anyhow::{Context, Result};
use chrono::Local;
use serenity::http::Http;
use serenity::json::Value;
use serenity::model::channel::Embed;
Expand Down Expand Up @@ -48,13 +49,17 @@ impl DiscordHandler {

pub(crate) async fn handle_new_auction(&self, auction: &Auction) -> Result<()> {
let message = Embed::fake(|e| {
e.title(format!("New Round: {}", auction.title))
e.title("New Prop House Round")
.url(format!(
"{}/{}",
self.base_url,
auction.title.replace(' ', "-").to_lowercase()
))
.description(&auction.description)
.description(format!(
"A new Prop House round has been created: {}",
auction.title
))
.footer(|f| f.text(format!("{}", Local::now().format("%m/%d/%Y %I:%M %p"))))
.colour(0x8A2CE2)
});

Expand All @@ -76,15 +81,20 @@ impl DiscordHandler {
&proposal.address[0..4],
&proposal.address[38..42]
))
.url(format!("https://etherscan.io/address/{}", proposal.address))
})
.title(format!("New Proposal: {}", proposal.title))
.title("New Prop House Proposal")
.url(format!(
"{}/{}/{}",
self.base_url,
auction.title.replace(' ', "-").to_lowercase(),
proposal.id
))
.description(&proposal.tldr)
.description(format!(
"A new Prop House proposal has been created: {}",
proposal.title
))
.footer(|f| f.text(format!("{}", Local::now().format("%m/%d/%Y %I:%M %p"))))
.colour(0x8A2CE2)
});

Expand All @@ -106,22 +116,25 @@ impl DiscordHandler {
&vote.address[0..4],
&vote.address[38..42]
))
.url(format!("https://etherscan.io/address/{}", vote.address))
})
.title(format!(
"New Vote {}: {}",
match vote.direction {
1 => "For",
_ => "Against",
},
proposal.title
))
.title("New Prop House Proposal Vote")
.url(format!(
"{}/{}/{}",
self.base_url,
proposal.title.replace(' ', "-").to_lowercase(),
proposal.id
))
.description(&proposal.tldr)
.description(format!(
"{} has voted {} Proposal ({})",
format!("{}...{}", &vote.address[0..4], &vote.address[38..42]),
match vote.direction {
1 => "for",
_ => "against",
},
proposal.title
))
.footer(|f| f.text(format!("{}", Local::now().format("%m/%d/%Y %I:%M %p"))))
.colour(0x8A2CE2)
});

Expand Down
6 changes: 3 additions & 3 deletions src/prop_house/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub async fn start() {

if let Some(auctions) = fetch_auctions().await {
for auction in auctions {
if let Ok(cached_auction) = get_auction_cache(auction.id.try_into().unwrap()) {
if let Ok(cached_auction) = get_auction_cache(auction.id) {
if cached_auction.is_none() {
info!("Handle a new auction... ({:?})", auction.id);
if let Err(err) = handler.handle_new_auction(&auction).await {
Expand All @@ -47,7 +47,7 @@ pub async fn start() {

if let Some(proposals) = fetch_proposals().await {
for proposal in proposals {
if let Ok(cached_proposal) = get_proposal_cache(proposal.id.try_into().unwrap()) {
if let Ok(cached_proposal) = get_proposal_cache(proposal.id) {
if cached_proposal.is_none() {
info!("Handle a new proposal... ({:?})", proposal.id);
if let Err(err) = handler.handle_new_proposal(&proposal).await {
Expand All @@ -60,7 +60,7 @@ pub async fn start() {

if let Some(votes) = fetch_votes().await {
for vote in votes {
if let Ok(cached_vote) = get_vote_cache(vote.id.try_into().unwrap()) {
if let Ok(cached_vote) = get_vote_cache(vote.id) {
if cached_vote.is_none() {
info!("Handle a new vote... ({:?})", vote.id);
if let Err(err) = handler.handle_new_vote(&vote).await {
Expand Down
Loading

0 comments on commit ae57d73

Please sign in to comment.