Skip to content

Commit

Permalink
Merge pull request #767 from Stremio/feat/profile-new-user-field
Browse files Browse the repository at this point in the history
feat(profile): impl new user check
  • Loading branch information
elpiel authored Jan 14, 2025
2 parents 9c9b7eb + c0ec208 commit 5a6b217
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub const NOTIFICATION_ITEMS_COUNT: usize = 100;
/// Maximum calendar items to fetch from `calendarIds` resource
pub const CALENDAR_ITEMS_COUNT: usize = 100;

/// Account age in days to be considered a new user
pub const NEW_USER_DAYS: chrono::Duration = chrono::Duration::days(30);

/// A `LibraryItem` is considered watched once we've watched more than the `duration * threshold`:
///
/// `LibraryItem.state.time_watched` > `LibraryItem.state.duration` * [`WATCHED_THRESHOLD_COEF`]
Expand Down
9 changes: 9 additions & 0 deletions src/types/profile/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use derivative::Derivative;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError, DefaultOnNull, DurationSeconds, NoneAsEmptyString};

use crate::{constants::NEW_USER_DAYS, runtime::Env};

#[serde_as]
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(test, derive(Derivative))]
Expand Down Expand Up @@ -69,3 +71,10 @@ pub struct User {
#[serde(rename = "gdpr_consent")]
pub gdpr_consent: GDPRConsent,
}

impl User {
pub fn is_new_user<E: Env + 'static>(&self) -> bool {
let now = E::now();
now.signed_duration_since(self.date_registered) < NEW_USER_DAYS
}
}
41 changes: 36 additions & 5 deletions stremio-core-web/src/model/serialize_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ mod model {

use stremio_core::deep_links::SearchHistoryItemDeepLinks;
use stremio_core::types::{
events::Events, notifications::NotificationItem, profile::Profile, resource::MetaItemId,
events::Events, notifications::NotificationItem, resource::MetaItemId,
};
use url::Url;

use crate::model::deep_links_ext::DeepLinksExt;
use crate::{env::WebEnv, model::deep_links_ext::DeepLinksExt};

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Ctx<'a> {
/// keep the original Profile model inside.
pub profile: &'a Profile,
pub profile: Profile<'a>,
pub notifications: Notifications<'a>,
pub search_history: Vec<SearchHistoryItem<'a>>,
pub events: &'a Events,
Expand Down Expand Up @@ -57,10 +56,42 @@ mod model {
pub deep_links: SearchHistoryItemDeepLinks,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Profile<'a> {
#[serde(flatten)]
profile: &'a stremio_core::types::profile::Profile,
#[serde(skip_serializing_if = "Option::is_none")]
auth: Option<Auth<'a>>,
}

#[derive(Serialize)]
pub struct Auth<'a> {
pub key: stremio_core::types::profile::AuthKey,
pub user: User<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct User<'a> {
#[serde(flatten)]
pub user: &'a stremio_core::types::profile::User,
pub is_new_user: bool,
}

impl<'a> From<&'a stremio_core::models::ctx::Ctx> for Ctx<'a> {
fn from(ctx: &'a stremio_core::models::ctx::Ctx) -> Self {
Self {
profile: &ctx.profile,
profile: Profile {
profile: &ctx.profile,
auth: ctx.profile.auth.as_ref().map(|auth| Auth {
key: auth.key.clone(),
user: User {
user: &auth.user,
is_new_user: auth.user.is_new_user::<WebEnv>(),
},
}),
},
notifications: Notifications {
items: ctx
.notifications
Expand Down

0 comments on commit 5a6b217

Please sign in to comment.