• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 11293827300

11 Oct 2024 02:06PM UTC coverage: 55.932% (+0.02%) from 55.917%
11293827300

Pull #1339

github

web-flow
Merge 4c9413d7f into 7cf458010
Pull Request #1339: Bump primereact from 10.8.3 to 10.8.4 in /app

3932 of 7030 relevant lines covered (55.93%)

103.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/handler/session_store.rs
1
use crate::{entity::session, Db};
2
use async_session::{
3
    async_trait,
4
    chrono::{DateTime, Utc},
5
    Session,
6
};
7
use sea_orm::{
8
    sea_query::{any, OnConflict},
9
    ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter,
10
};
11
use serde_json::json;
12
use time::OffsetDateTime;
13

14
#[derive(Debug, Clone)]
15
pub struct SessionStore {
16
    db: Db,
17
}
18

19
impl SessionStore {
20
    pub fn new(db: Db) -> Self {
283✔
21
        Self { db }
283✔
22
    }
283✔
23
}
24

25
impl TryFrom<&Session> for session::Model {
26
    type Error = serde_json::Error;
27

28
    fn try_from(session: &Session) -> Result<Self, Self::Error> {
275✔
29
        Ok(Self {
275✔
30
            id: session.id().to_string(),
275✔
31
            expiry: session
275✔
32
                .expiry()
275✔
33
                .map(|e| OffsetDateTime::from_unix_timestamp(e.timestamp()).unwrap()),
275✔
34
            data: serde_json::from_value(
275✔
35
                serde_json::to_value(session)?.get("data").unwrap().clone(),
275✔
36
            )?,
×
37
        })
38
    }
275✔
39
}
40

41
impl TryFrom<session::Model> for Session {
42
    type Error = serde_json::Error;
43
    fn try_from(db_session: session::Model) -> Result<Session, serde_json::Error> {
×
44
        let mut session: Session = serde_json::from_value(json!({
×
45
            "id": db_session.id,
×
46
            "data": db_session.data,
×
47
        }))?;
×
48
        if let Some(x) = db_session.expiry {
×
49
            session.set_expiry(
×
50
                DateTime::<Utc>::from_timestamp(x.unix_timestamp(), x.nanosecond()).unwrap(),
×
51
            );
×
52
        }
×
53
        Ok(session)
×
54
    }
×
55
}
56

57
#[async_trait]
58
impl async_session::SessionStore for SessionStore {
59
    async fn load_session(&self, cookie_value: String) -> async_session::Result<Option<Session>> {
×
60
        let id = Session::id_from_cookie_value(&cookie_value)?;
×
61
        Ok(session::Entity::find_by_id(id)
×
62
            .filter(any![
×
63
                session::Column::Expiry.is_null(),
×
64
                session::Column::Expiry.gt(OffsetDateTime::now_utc())
×
65
            ])
×
66
            .one(&self.db)
×
67
            .await?
×
68
            .map(Session::try_from)
×
69
            .transpose()?)
×
70
    }
×
71

72
    async fn store_session(&self, session: Session) -> async_session::Result<Option<String>> {
275✔
73
        let session_model = session::Model::try_from(&session)?.into_active_model();
275✔
74

275✔
75
        session::Entity::insert(session_model)
275✔
76
            .on_conflict(
275✔
77
                OnConflict::column(session::Column::Id)
275✔
78
                    .update_columns([session::Column::Data, session::Column::Expiry])
275✔
79
                    .clone(),
275✔
80
            )
275✔
81
            .exec(&self.db)
275✔
82
            .await?;
694✔
83

84
        Ok(session.into_cookie_value())
275✔
85
    }
550✔
86

87
    async fn destroy_session(&self, session: Session) -> async_session::Result {
1✔
88
        session::Entity::delete_by_id(session.id())
1✔
89
            .exec(&self.db)
1✔
90
            .await?;
×
91
        Ok(())
1✔
92
    }
2✔
93

94
    async fn clear_store(&self) -> async_session::Result {
×
95
        session::Entity::delete_many().exec(&self.db).await?;
×
96
        Ok(())
×
97
    }
×
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc