Skip to main content

telegram_raf/persistence/
types.rs

1// Copyright 2021 Paolo Galeone <nessuno@nerdz.eu>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use chrono::DateTime;
16use chrono::Utc;
17use r2d2_sqlite::SqliteConnectionManager;
18use typemap::Key;
19
20/// A User is a human using the bot
21#[derive(Debug, Clone)]
22pub struct User {
23    /// User unique ID, Telegram generated
24    pub id: i64,
25    /// User first name, mandatory from telegram API
26    pub first_name: String,
27    /// User last name, optional
28    pub last_name: Option<String>,
29    /// User username, optional
30    pub username: Option<String>,
31}
32
33/// The Channel structure is used for identifying both
34/// channels and (super)groups, since they have the very same attributes.
35#[derive(Debug)]
36pub struct Channel {
37    /// Channel unique ID, Telegram generated.
38    pub id: i64,
39    /// User unique ID, the user that registered the channel to the bot.
40    /// Almost always the channel creator.
41    pub registered_by: i64,
42    /// The invitation link for the channel. Can be the user-decided one
43    /// or the private-link Telegram generated.
44    pub link: String,
45    /// Channel name
46    pub name: String,
47}
48
49/// A reference to the user that's currently managing a channel.
50#[derive(Debug)]
51pub struct BeingManagedChannel {
52    /// User unique ID
53    pub chan: i64,
54}
55
56/// An invitation sent from source, to dest, for the chan.
57#[derive(Debug)]
58pub struct Invite {
59    /// Invitation unique ID, locally generated
60    pub id: i64,
61    /// Whenever the invitation has been created
62    pub date: DateTime<Utc>,
63    /// The user who's inviting
64    pub source: i64,
65    /// The user who's being invited
66    pub dest: i64,
67    /// The channel dest user is being invited into
68    pub chan: i64,
69}
70
71/// A referral based strategy contest
72#[derive(Debug)]
73pub struct Contest {
74    /// Contest unique ID, locally generated
75    pub id: i64,
76    /// Contest name, unique and locally generated
77    pub name: String,
78    /// The prize the owner of the `chan` wants to give to the contest's winner
79    pub prize: String,
80    /// Contest end date and time. Invitations received after the end date
81    /// won't generate an increase in the ranking.
82    pub end: DateTime<Utc>,
83    /// Whenever the contest's owner decided to start the Contest
84    pub started_at: Option<DateTime<Utc>>,
85    /// True if the user decided to stop this contest.
86    pub stopped: bool,
87    /// The channel ID for this contest
88    pub chan: i64,
89}
90
91/// Helper struct containing a rank ID and a Contest
92#[derive(Debug)]
93pub struct RankContest {
94    /// A user rank (position)
95    pub rank: i64,
96    /// The contest associated
97    pub c: Contest,
98}
99
100/// Rank is like a row in a ranking table.
101#[derive(Debug, Clone)]
102pub struct Rank {
103    /// The position in the chart
104    pub rank: i64,
105    /// Number of invitations sent by this user
106    pub invites: i64,
107    /// The user that is in `rank` position because it sent `invites` invitations
108    pub user: User,
109}
110
111/// Unique type for a `typemap::Key` used to fetch from the Telexide context
112/// the `r2d2::Pool<SqliteConnectionManager>`
113pub struct DBKey;
114impl Key for DBKey {
115    type Value = r2d2::Pool<SqliteConnectionManager>;
116}
117
118/// Unique type for a `typemap::Key` used to fetch from the Telexide context
119/// the bot name, without accessing in this way to the `env`.
120pub struct NameKey;
121impl Key for NameKey {
122    type Value = String;
123}