add: user ads

This commit is contained in:
trisua 2025-08-11 20:21:05 -04:00
parent 46b3e66cd4
commit 2cb7d08ddc
41 changed files with 1095 additions and 29 deletions

View file

@ -104,6 +104,10 @@ pub enum CoinTransferSource {
Purchase,
/// A refund of coins.
Refund,
/// The charge for keeping an ad running.
AdCharge,
/// Gained coins from a click on an ad on your site.
AdClick,
}
#[derive(Serialize, Deserialize)]
@ -149,3 +153,69 @@ impl CoinTransfer {
(sender.coins < 0, receiver.coins < 0)
}
}
/// <https://en.wikipedia.org/wiki/Web_banner#Standard_sizes>
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum UserAdSize {
/// 970x250
Billboard,
/// 720x90
Leaderboard,
/// 160x600
Skyscraper,
/// 300x250
MediumRectangle,
/// 320x50 - mobile only
MobileLeaderboard,
}
impl Default for UserAdSize {
fn default() -> Self {
Self::MediumRectangle
}
}
impl UserAdSize {
/// Get the dimensions of the size in CSS pixels.
pub fn dimensions(&self) -> (u16, u16) {
match self {
Self::Billboard => (970, 250),
Self::Leaderboard => (720, 90),
Self::Skyscraper => (160, 600),
Self::MediumRectangle => (300, 250),
Self::MobileLeaderboard => (320, 50),
}
}
}
#[derive(Serialize, Deserialize)]
pub struct UserAd {
pub id: usize,
pub created: usize,
pub owner: usize,
pub upload_id: usize,
pub target: String,
/// The time that the owner was last charged for keeping this ad up.
///
/// Ads cost 50 coins per day of running.
pub last_charge_time: usize,
pub is_running: bool,
pub size: UserAdSize,
}
impl UserAd {
/// Create a new [`UserAd`].
pub fn new(owner: usize, upload_id: usize, target: String, size: UserAdSize) -> Self {
let created = unix_epoch_timestamp();
Self {
id: 0, // will be overwritten by postgres
created,
owner,
upload_id,
target,
last_charge_time: 0,
is_running: false,
size,
}
}
}