Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: different profiles per specific stream #8

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions src/http/v1/settings_controller.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use actix_web::{get, put, web, HttpResponse, Responder};
use charybdis::operations::{Find, Insert};
use charybdis::options::Consistency;
use log::info;
use serde::Deserialize;
use serde_json::json;
use web::Json;

use crate::config::app::AppState;
use crate::http::SomeError;
use crate::models::v1::settings::Settings;
use crate::models::v1::settings_by_username::SettingsByUsername;
use crate::models::v1::settings::{Settings, SettingsByUsername};

#[put("/api/v1/settings")]
pub async fn put_settings(
Expand Down Expand Up @@ -39,29 +40,76 @@
Ok(HttpResponse::Ok().json(json!(settings)))
}

#[derive(Deserialize)]
struct SettingsQuery {
channel_id: Option<String>,
}

#[get("/api/v1/settings/{username}")]
pub async fn get_settings(
data: web::Data<AppState>,
username: web::Path<String>,
channel_id: web::Query<SettingsQuery>,
) -> Result<impl Responder, SomeError> {
let username = username.into_inner();
let channel_id = channel_id
.into_inner()
.channel_id
.unwrap_or("global".to_string());

info!(
"[GET Settings] -> Channel/User -> {} / {}",
channel_id, username
);

let settings = SettingsByUsername {
username,
username: username.clone(),
channel_id: channel_id.clone(),
..Default::default()
};

let settings = settings
// Query the user settings with the given username and channel_id
let mut settings_model = settings
.find_by_partition_key()
.consistency(Consistency::LocalOne)
.execute(&data.database)
.await?;
.await?
.try_collect()
.await
.unwrap();

if channel_id == "global" {
return Ok(HttpResponse::Ok().json(json!(settings_model.first())));
}

let settings = settings.try_collect().await?;
let response = match settings.is_empty() {
true => HttpResponse::NotFound().json(json!({})),
false => HttpResponse::Ok().json(json!(settings[0].clone())),
let settings_model = settings_model.pop();

let should_query_global = settings_model.clone().is_some_and(|s| !s.enabled);

let result = if should_query_global {
let settings = SettingsByUsername {
username,
channel_id: "global".to_string(),
..Default::default()
};

let mut settings_model = settings
.find_by_partition_key()
.consistency(Consistency::LocalOne)
.execute(&data.database)
.await?
.try_collect()
.await
.unwrap();
settings_model.pop()
} else {
settings_model

Check warning on line 106 in src/http/v1/settings_controller.rs

View workflow job for this annotation

GitHub Actions / Run lint and format

Diff in /home/runner/work/tbp-consumer-api/tbp-consumer-api/src/http/v1/settings_controller.rs

Check warning on line 106 in src/http/v1/settings_controller.rs

View workflow job for this annotation

GitHub Actions / Run lint and format

Diff in /home/runner/work/tbp-consumer-api/tbp-consumer-api/src/http/v1/settings_controller.rs
};

let response = match result {
Some(data) => HttpResponse::Ok().json(json!(data)),
None => HttpResponse::NotFound().json(json!({})),
};

Ok(response)
}
1 change: 0 additions & 1 deletion src/models/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod metrics;
pub mod settings;
pub mod settings_by_username;
pub mod throttle;
pub mod users;
30 changes: 27 additions & 3 deletions src/models/v1/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use charybdis::macros::{charybdis_model, charybdis_udt_model};
use charybdis::macros::{charybdis_model, charybdis_udt_model, charybdis_view_model};
use charybdis::types::{Boolean, Frozen, Int, Text, Timestamp};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct EffectOption {

#[charybdis_model(
table_name = settings_v1,
partition_keys = [user_id],
partition_keys = [user_id, channel_id],
clustering_keys = [],
global_secondary_indexes = [],
local_secondary_indexes = [],
Expand All @@ -42,14 +42,38 @@ pub struct EffectOption {
pub struct Settings {
pub user_id: Int,
pub username: Text,
pub channel_id: Text,
pub enabled: Option<Boolean>,
pub locale: Option<Text>,
pub timezone: Option<Text>,
pub occupation: Option<Frozen<SettingOptions>>,
pub pronouns: Option<Frozen<SettingOptions>>,
pub color: Option<Frozen<ColorOption>>,
pub effect: Option<Frozen<EffectOption>>,
pub is_developer: Option<Boolean>,
#[serde(default = "default_updated_at")]
pub updated_at: Timestamp,
}

#[charybdis_view_model(
table_name = settings_by_username_v1,
base_table = settings_v1,
partition_keys = [username, channel_id],
clustering_keys = [user_id],
)]
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct SettingsByUsername {
pub user_id: Int,
pub username: Text,
pub channel_id: Text,
pub enabled: Boolean,
pub locale: Option<Text>,
pub timezone: Option<Text>,
pub occupation: Frozen<SettingOptions>,
pub pronouns: Frozen<SettingOptions>,
pub color: Frozen<ColorOption>,
pub effect: Frozen<EffectOption>,
pub is_developer: Option<Boolean>,
#[serde(default = "default_updated_at")]
pub updated_at: Timestamp,
}

Expand Down
23 changes: 0 additions & 23 deletions src/models/v1/settings_by_username.rs

This file was deleted.

Loading