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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Interface for the slap system
//!
//! Botanist includes a moderation system called `Slapping`. It serves the purpose of giving warnings
//! to members and keeping a record of all of these warnings in a simple way.
//!
//! ## Errors
//! All methods of this module which return a `Result` do so because sql querries through to the database may
//! fail. As such you should handle [`AdapterError::SqlxError`]. Because it is part of the signature of most methods
//! errors are undocumented if they only return a database error. Otherwise an *Error* section is provided.

use crate::{from_i64, stringify_option, to_i64, AdapterError};
#[cfg(feature = "net")]
use serde::{Deserialize, Serialize};

use serenity::{
    futures::TryStreamExt,
    model::id::{GuildId, MessageId, UserId},
};
use sqlx::{query, query_scalar, Executor, Postgres};
use tokio_stream::{Stream, StreamExt};

/// Method through which the slap was issued
///
/// Botanist allows slaps to be given either by a member with the
/// `manager` privilege or by a public vote.
//internally uses None as Community
#[cfg_attr(feature = "net", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Enforcer {
    /// The verdict was issued by popular vote
    Community,
    /// A manager issued a slap. Their [`UserId`] is encapsulated.
    Manager(UserId),
}

impl From<Option<u64>> for Enforcer {
    fn from(option: Option<u64>) -> Self {
        match option {
            Some(id) => Enforcer::Manager(id.into()),
            None => Enforcer::Community,
        }
    }
}

fn option_to_enforcer(option: Option<i64>) -> Enforcer {
    match option {
        Some(int) => Enforcer::Manager(from_i64(int)),
        None => Enforcer::Community,
    }
}

pub(crate) fn enforcer_to_option(enforcer: Enforcer) -> Option<UserId> {
    match enforcer {
        Enforcer::Manager(user) => Some(user),
        Enforcer::Community => None,
    }
}

type Result<R> = std::result::Result<R, AdapterError>;
/// A single slap object
#[cfg_attr(feature = "net", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct SlapReport {
    /// Message from which the slap originates
    ///
    /// Depending on `enforcer`, `sentence` has a different meaning. If it is [`Enforcer::Community`], then `sentence`
    /// points to the message attributed to the reason of the slap. That is to say the one users collectively reacted
    /// with the slap emoji.
    /// Otherwise it points to the message that issued the slap (so a command message).
    pub sentence: MessageId,
    /// The slapped user.
    pub offender: UserId,
    /// Who delivered the slap.
    ///
    /// See [`Enforcer`] for more information.
    pub enforcer: Enforcer,
    /// The reason for the slap.
    ///
    /// This is [`None`] if `enforcer` is  [`Enforcer::Community`] or if the default reason was used.
    /// The default reason is used when the enforcer doesn't provide a `reason` argument when issueing the slap.
    pub reason: Option<String>,
}

impl SlapReport {
    /// Retrieves a SlapReport
    ///
    /// Returns [`None`] if no such slap exists.
    pub async fn get<'a, PgExec: Executor<'a, Database = Postgres>>(
        conn: PgExec,
        sentence: MessageId,
    ) -> Result<Option<SlapReport>> {
        Ok(query!(
            "SELECT offender, enforcer, reason FROM slaps WHERE sentence=$1",
            to_i64(sentence)
        )
        .fetch_optional(conn)
        .await?
        .map(|record| SlapReport {
            sentence,
            offender: UserId(from_i64(record.offender)),
            enforcer: option_to_enforcer(record.enforcer),
            reason: record.reason,
        }))
    }
}

async fn insert_raw_slap<'a, PgExec: Executor<'a, Database = Postgres>, S: std::fmt::Display>(
    conn: PgExec,
    sentence: i64,
    guild: i64,
    offender: i64,
    enforcer: Enforcer,
    reason: Option<S>,
) -> Result<()> {
    sqlx::query(&format!("INSERT INTO slaps(sentence, guild, offender, enforcer, reason) VALUES ({}, {}, {}, {}, {})",sentence, guild, offender, stringify_option(enforcer_to_option(enforcer)), stringify_option(reason))).execute(conn).await?;
    Ok(())
}

/// Record of slaps of a guild member
#[derive(Debug, PartialEq, Eq)]
pub struct MemberSlapRecord(pub GuildId, pub UserId);

impl MemberSlapRecord {
    ///Adds a slap entry for this member
    pub async fn new_slap<'a, PgExec: Executor<'a, Database = Postgres> + Copy>(
        &self,
        conn: PgExec,
        sentence: MessageId,
        enforcer: Enforcer,
        reason: Option<String>,
    ) -> Result<SlapReport> {
        insert_raw_slap(
            conn,
            to_i64(sentence),
            to_i64(self.0),
            to_i64(self.1),
            enforcer.clone(),
            //try and remove this clone. Consider making stringify_option more generic for that
            reason.clone(),
        )
        .await?;
        Ok(SlapReport {
            sentence,
            offender: self.1,
            enforcer,
            reason,
        })
    }

    ///A stream over all of the member's slaps
    pub fn slaps<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
        &'a self,
        conn: PgExec,
    ) -> impl Stream<Item = Result<SlapReport>> + 'a {
        let offender = to_i64(self.1);
        query!(
            "SELECT sentence, enforcer, reason FROM slaps WHERE guild=$1 AND offender=$2",
            to_i64(self.0),
            offender
        )
        .fetch(conn)
        .map_err(|e| AdapterError::from(e))
        .map(move |res| {
            res.map(|record| SlapReport {
                sentence: MessageId(from_i64(record.sentence)),
                offender: self.1,
                enforcer: match record.enforcer {
                    Some(user) => Enforcer::Manager(UserId(from_i64(user))),
                    None => Enforcer::Community,
                },
                reason: record.reason,
            })
        })
    }

    ///The number of slaps of the member
    pub async fn len<'a, PgExec: Executor<'a, Database = Postgres>>(
        &self,
        conn: PgExec,
    ) -> Result<usize> {
        Ok(query_scalar!(
            r#"SELECT COUNT(sentence) as "count!" FROM slaps WHERE guild=$1 AND offender=$2"#,
            to_i64(self.0),
            to_i64(self.1),
        )
        .fetch_one(conn)
        .await? as usize)
    }
}

impl From<(GuildId, UserId)> for MemberSlapRecord {
    fn from(src: (GuildId, UserId)) -> Self {
        MemberSlapRecord(src.0, src.1)
    }
}

impl From<(GuildSlapRecord, UserId)> for MemberSlapRecord {
    fn from(src: (GuildSlapRecord, UserId)) -> Self {
        MemberSlapRecord(src.0 .0, src.1)
    }
}

/// Record of slaps of a guild
#[derive(Debug, PartialEq, Eq)]
pub struct GuildSlapRecord(pub GuildId);

impl GuildSlapRecord {
    ///Adds a slap to the guild
    pub async fn new_slap<
        'a,
        PgExec: Executor<'a, Database = Postgres> + Copy,
        S: std::fmt::Display,
    >(
        &self,
        conn: PgExec,
        sentence: MessageId,
        offender: UserId,
        enforcer: Enforcer,
        reason: Option<S>,
    ) -> Result<SlapReport> {
        let reason = reason.map(|s| s.to_string());
        insert_raw_slap(
            conn,
            to_i64(sentence),
            to_i64(self.0),
            to_i64(offender),
            enforcer.clone(),
            reason.clone(),
        )
        .await?;
        Ok(SlapReport {
            sentence,
            offender,
            enforcer,
            reason,
        })
    }

    ///Number of slaps in the guild
    pub async fn len<'a, PgExec: Executor<'a, Database = Postgres>>(
        &self,
        conn: PgExec,
    ) -> Result<usize> {
        Ok(query_scalar!(
            // "count!" is to force non-null -> see sqlx::query! docs
            r#"SELECT COUNT(sentence) as "count!" FROM slaps WHERE guild=$1"#,
            to_i64(self.0),
        )
        .fetch_one(conn)
        .await? as usize)
    }

    ///A stream over all slaps of the guild
    pub fn slaps<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
        &'a self,
        conn: PgExec,
    ) -> impl Stream<Item = Result<SlapReport>> + 'a {
        query!(
            "SELECT sentence, offender, enforcer, reason FROM slaps WHERE guild=$1",
            to_i64(self.0),
        )
        .fetch(conn)
        .map_err(|e| AdapterError::from(e))
        .map(move |res| {
            res.map(|record| SlapReport {
                sentence: MessageId(from_i64(record.sentence)),
                offender: UserId(from_i64(record.offender)),
                enforcer: match record.enforcer {
                    Some(user) => Enforcer::Manager(UserId(from_i64(user))),
                    None => Enforcer::Community,
                },
                reason: record.reason,
            })
        })
    }

    ///A stream over all members with a slap record
    pub fn offenders<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
        &'a self,
        conn: PgExec,
    ) -> impl Stream<Item = Result<MemberSlapRecord>> + 'a {
        query!(
            "SELECT DISTINCT offender FROM slaps WHERE guild=$1",
            to_i64(self.0)
        )
        .fetch(conn)
        .map_err(|e| AdapterError::from(e))
        .map(move |res| {
            res.map(|record| MemberSlapRecord(self.0, UserId(from_i64(record.offender))))
        })
    }

    ///Number of offending members in the guild
    pub async fn offender_count<'a, PgExec: Executor<'a, Database = Postgres>>(
        &self,
        conn: PgExec,
    ) -> Result<usize> {
        Ok(query_scalar!(
            // "count!" is to force non-null -> see sqlx::query! docs
            r#"SELECT COUNT(DISTINCT offender) as "count!" FROM slaps WHERE guild=$1"#,
            to_i64(self.0),
        )
        .fetch_one(conn)
        .await? as usize)
    }
}

impl From<GuildId> for GuildSlapRecord {
    fn from(src: GuildId) -> Self {
        GuildSlapRecord(src)
    }
}