Allow anonymous jump-hop recommend play

This commit is contained in:
kdletters
2026-05-24 16:02:49 +08:00
parent 2ba4691bc0
commit 2c6fb2e81a
12 changed files with 209 additions and 67 deletions

View File

@@ -13,7 +13,7 @@ pub(crate) const WORK_PLAY_START_EVENT_KEY: &str = "work_play_start";
pub(crate) struct WorkPlayTrackingDraft {
pub play_type: &'static str,
pub work_id: String,
pub user_id: String,
pub user_id: Option<String>,
pub owner_user_id: Option<String>,
pub profile_id: Option<String>,
pub run_id: Option<String>,
@@ -28,7 +28,28 @@ impl WorkPlayTrackingDraft {
authenticated: &AuthenticatedAccessToken,
source_route: &'static str,
) -> Self {
let user_id = authenticated.claims().user_id().to_string();
Self::with_user_id(
play_type,
work_id,
Some(authenticated.claims().user_id().to_string()),
source_route,
)
}
pub(crate) fn anonymous(
play_type: &'static str,
work_id: impl Into<String>,
source_route: &'static str,
) -> Self {
Self::with_user_id(play_type, work_id, None, source_route)
}
fn with_user_id(
play_type: &'static str,
work_id: impl Into<String>,
user_id: Option<String>,
source_route: &'static str,
) -> Self {
Self {
play_type,
work_id: work_id.into(),
@@ -91,7 +112,11 @@ async fn record_work_play_start_input_after_success(
"workId": draft.work_id,
"sourceRoute": draft.source_route,
});
metadata["userId"] = json!(draft.user_id);
if let Some(user_id) = draft.user_id.as_deref() {
metadata["userId"] = json!(user_id);
} else {
metadata["userKind"] = json!("anonymous");
}
if let Some(owner_user_id) = draft.owner_user_id.as_deref() {
metadata["ownerUserId"] = json!(owner_user_id);
}
@@ -108,7 +133,7 @@ async fn record_work_play_start_input_after_success(
let mut tracking = TrackingEventDraft::new(WORK_PLAY_START_EVENT_KEY, draft.play_type);
tracking.scope_kind = RuntimeTrackingScopeKind::Work;
tracking.scope_id = draft.work_id;
tracking.user_id = Some(draft.user_id);
tracking.user_id = draft.user_id;
tracking.owner_user_id = draft.owner_user_id;
tracking.profile_id = draft.profile_id;
tracking.metadata = metadata;