1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use chrono::{
    serde::{ts_milliseconds, ts_milliseconds_option},
    DateTime, Utc,
};
use serde::{Deserialize, Serialize};

use crate::{ValidatorId, IPFS};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AdUnit {
    /// valid ipfs hash of spec props below
    pub ipfs: IPFS,
    /// the type of the ad unit
    /// currently, possible values are:
    /// legacy_300x250, legacy_250x250, legacy_240x400, legacy_336x280,
    /// legacy_180x150, legacy_300x100, legacy_720x300, legacy_468x60,
    /// legacy_234x60, legacy_88x31, legacy_120x90, legacy_120x60,
    /// legacy_120x240, legacy_125x125, legacy_728x90, legacy_160x600,
    /// legacy_120x600, legacy_300x600
    /// see IAB ad unit guidelines and iab_flex_{adUnitName} (see IAB's new ad portfolio and PDF)
    #[serde(rename = "type")]
    pub ad_type: String,
    /// a URL to the resource (usually PNG); must use the ipfs:// protocol, to guarantee data immutability
    pub media_url: String,
    /// MIME type of the media, possible values at the moment are: image/jpeg, image/png
    pub media_mime: String,
    /// Advertised URL
    pub target_url: String,
    /// Number; minimum targeting score (optional)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_targeting_score: Option<f64>,
    /// user address from the session
    pub owner: ValidatorId,
    /// number, UTC timestamp in milliseconds, used as nonce for escaping duplicated spec ipfs hashes
    #[serde(with = "ts_milliseconds")]
    pub created: DateTime<Utc>,
    /// the name of the unit used in platform UI
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// arbitrary text used in platform UI
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// user can change it - used for filtering in platform UI
    #[serde(default)]
    pub archived: bool,
    /// UTC timestamp in milliseconds, changed every time modifiable property is changed
    #[serde(
        default,
        with = "ts_milliseconds_option",
        skip_serializing_if = "Option::is_none"
    )]
    pub modified: Option<DateTime<Utc>>,
}

#[cfg(feature = "postgres")]
mod postgres {
    use super::AdUnit;

    use bytes::BytesMut;
    use std::error::Error;
    use tokio_postgres::types::{accepts, to_sql_checked, FromSql, IsNull, Json, ToSql, Type};
    impl<'a> FromSql<'a> for AdUnit {
        fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
            let json = <Json<Self> as FromSql>::from_sql(ty, raw)?;

            Ok(json.0)
        }

        accepts!(JSONB);
    }

    impl ToSql for AdUnit {
        fn to_sql(
            &self,
            ty: &Type,
            w: &mut BytesMut,
        ) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
            Json(self).to_sql(ty, w)
        }

        accepts!(JSONB);
        to_sql_checked!();
    }
}