Skip to content

Commit 3ad6fbe

Browse files
committed
debug: send 202 for app commands
1 parent 47cf4ca commit 3ad6fbe

File tree

20 files changed

+153
-117
lines changed

20 files changed

+153
-117
lines changed

api/src/dynamo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ pub fn as_map_vec(val: Option<&AttributeValue>) -> Vec<&HashMap<String, Attribut
6161
}
6262
}
6363
vec![]
64+
}
65+
66+
67+
pub fn as_bool(val: Option<&AttributeValue>, default: bool) -> bool {
68+
if let Some(v) = val {
69+
if let Ok(b) = v.as_bool() {
70+
return *b;
71+
}
72+
}
73+
default
6474
}

api/src/interactions/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use axum::routing::post;
66
use axum::{Json, middleware};
77
use ed25519_dalek::{PUBLIC_KEY_LENGTH, Verifier, VerifyingKey};
88
use hex::FromHex;
9-
use http::{HeaderMap, HeaderValue, StatusCode};
9+
use http::{HeaderMap, StatusCode};
1010
use http_body_util::BodyExt;
1111
use lambda_http::tracing::error;
1212
use once_cell::sync::Lazy;
@@ -82,12 +82,6 @@ pub async fn user_agent_response_middleware(
8282
) -> Result<Response, StatusCode> {
8383
let mut response = next.run(request).await;
8484

85-
let user_agent = response
86-
.headers()
87-
.get("User-Agent")
88-
.and_then(|v| v.to_str().ok())
89-
.unwrap_or("Unknown");
90-
9185
if response.headers().get("User-Agent").is_none() {
9286
let full_version = env!("CARGO_PKG_VERSION");
9387
response.headers_mut().insert("User-Agent", format!("KoalaBot/{} (+{})",full_version, get_url()).parse().unwrap());
@@ -104,7 +98,8 @@ async fn post_interactions(
10498
kind: InteractionResponseType::Pong,
10599
data: None
106100
}))),
107-
InteractionType::ApplicationCommand => handle_command_interaction(_app_state, interaction).await,
101+
// InteractionType::ApplicationCommand => handle_command_interaction(_app_state, interaction).await,
102+
InteractionType::ApplicationCommand => Err(StatusCode::ACCEPTED),
108103
InteractionType::MessageComponent => Err(StatusCode::NOT_IMPLEMENTED),
109104
InteractionType::ApplicationCommandAutocomplete => Err(StatusCode::NOT_IMPLEMENTED),
110105
InteractionType::ModalSubmit => Err(StatusCode::NOT_IMPLEMENTED),

api/src/users/links.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::AppState;
22
use axum::extract::{Path, State};
3-
use axum::routing::post;
3+
use axum::routing::{delete, post};
44
use axum::{Extension, Json};
55
use serde::{Deserialize, Serialize};
66
use std::sync::Arc;
7+
use lambda_http::tracing::info;
78
use serde_json::json;
89
use twilight_model::id::marker::UserMarker;
910
use twilight_model::id::Id;
@@ -12,14 +13,16 @@ use crate::users::models::{Link, User};
1213
pub fn router() -> axum::Router<AppState> {
1314
axum::Router::new()
1415
.route("/", post(post_link))
16+
.route("/{link_address}", delete(delete_link))
1517
}
1618

1719
#[derive(Clone, Serialize, Deserialize)]
20+
#[serde(rename_all = "lowercase")]
1821
enum LinkOrigin {
19-
MICROSOFT,
20-
GOOGLE,
21-
EMAIL,
22-
DISCORD,
22+
Microsoft,
23+
Google,
24+
Email,
25+
Discord,
2326
}
2427

2528
#[derive(Clone, Serialize, Deserialize)]
@@ -39,25 +42,25 @@ async fn post_link(
3942
}
4043
let email;
4144
match link_req.origin {
42-
LinkOrigin::DISCORD => {
45+
LinkOrigin::Discord => {
4346
// Handle Discord linking logic here
4447
email = discord_user.current_user().await.map_err(|_| http::StatusCode::UNAUTHORIZED)?
4548
.model().await.map_err(|_| http::StatusCode::UNAUTHORIZED)?.email.unwrap();
4649
},
47-
LinkOrigin::MICROSOFT => {
50+
LinkOrigin::Microsoft => {
4851
// Handle Microsoft linking logic here
4952
email = oidc_email("https://graph.microsoft.com/oidc/userinfo", link_req.token, &app_state).await?;
5053
},
51-
LinkOrigin::GOOGLE => {
54+
LinkOrigin::Google => {
5255
// Handle Google linking logic here
5356
email = oidc_email("https://openidconnect.googleapis.com/v1/userinfo", link_req.token, &app_state).await?;
5457
},
55-
LinkOrigin::EMAIL => {
58+
LinkOrigin::Email => {
5659
todo!();
5760
},
5861
}
5962

60-
let new_link = Link{link_address: email, linked_at: chrono::Utc::now().timestamp_millis() as u64};
63+
let new_link = Link{link_address: email, linked_at: chrono::Utc::now().timestamp_millis() as u64, active: true};
6164
let json_resp = Json(json!(new_link));
6265
let mut user_model = User::from_db(&user_id.to_string(), &app_state.dynamo).await.unwrap();
6366
user_model.links.retain(|l| l.link_address != new_link.link_address);
@@ -89,3 +92,26 @@ async fn oidc_email(
8992
Err(_) => Err(http::StatusCode::INTERNAL_SERVER_ERROR),
9093
}
9194
}
95+
96+
async fn delete_link(
97+
Path((user_id, link_address)): Path<(Id<UserMarker>, String)>,
98+
Extension(discord_user): Extension<Arc<twilight_http::Client>>,
99+
State(app_state): State<AppState>
100+
) -> Result<Json<serde_json::Value>, http::StatusCode> {
101+
if user_id != discord_user.current_user().await.unwrap().model().await.unwrap().id {
102+
return Err(http::StatusCode::NOT_FOUND);
103+
}
104+
105+
let mut user_model = User::from_db(&user_id.to_string(), &app_state.dynamo).await.unwrap();
106+
let existing_link = user_model.links.pop_if(|l| l.link_address == link_address);
107+
info!("Deleting link for user {}: {}", user_id, link_address);
108+
if existing_link.is_none() {
109+
return Err(http::StatusCode::NOT_FOUND);
110+
}
111+
let mut existing_link = existing_link.unwrap();
112+
existing_link.active = false;
113+
user_model.links.push(existing_link);
114+
user_model.save(&app_state.dynamo).await;
115+
116+
Ok(Json(json!({"status": "success", "message": "Link deleted"})))
117+
}

api/src/users/models.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use aws_sdk_dynamodb::types::AttributeValue;
33
use http::StatusCode;
44
use lambda_http::tracing::error;
55
use serde::{Deserialize, Serialize};
6-
use crate::dynamo::{as_u64, as_string, as_string_vec, as_map_vec};
6+
use crate::dynamo::{as_u64, as_string, as_string_vec, as_map_vec, as_bool};
77

88
#[derive(Clone, Serialize, Deserialize)]
99
pub struct Link {
1010
pub link_address: String,
1111
pub linked_at: u64,
12+
pub active: bool,
1213
}
1314

1415
#[derive(Clone, Serialize, Deserialize)]
@@ -27,13 +28,46 @@ impl From<&HashMap<String, AttributeValue>> for User {
2728
.map(|m| Link {
2829
link_address: as_string(m.get("link_address"), &"".to_string()),
2930
linked_at: as_u64(m.get("linked_at"), 0),
31+
active: as_bool(m.get("active"), false),
3032
})
3133
.collect(),
3234
linked_guild_ids: as_string_vec(item.get("linked_guild_ids")),
3335
}
3436
}
3537
}
3638

39+
impl Into<HashMap<String, AttributeValue>> for User {
40+
fn into(self) -> HashMap<String, AttributeValue> {
41+
let mut item = HashMap::new();
42+
item.insert("user_id".to_string(), AttributeValue::S(self.user_id));
43+
item.insert(
44+
"links".to_string(),
45+
AttributeValue::L(
46+
self.links
47+
.into_iter()
48+
.map(|l| {
49+
let mut map = HashMap::new();
50+
map.insert("link_address".to_string(), AttributeValue::S(l.link_address));
51+
map.insert("linked_at".to_string(), AttributeValue::N(l.linked_at.to_string()));
52+
map.insert("active".to_string(), AttributeValue::Bool(l.active));
53+
AttributeValue::M(map)
54+
})
55+
.collect(),
56+
),
57+
);
58+
item.insert(
59+
"linked_guild_ids".to_string(),
60+
AttributeValue::L(
61+
self.linked_guild_ids
62+
.into_iter()
63+
.map(|id| AttributeValue::S(id))
64+
.collect(),
65+
),
66+
);
67+
item
68+
}
69+
}
70+
3771
impl User {
3872
pub async fn from_db(user_id: &str, dynamo: &aws_sdk_dynamodb::Client) -> Option<User> {
3973
match dynamo.query().table_name(format!("kb2_users_{}",std::env::var("DEPLOYMENT_ENV").expect("DEPLOYMENT_ENV must be set"),))
@@ -61,36 +95,11 @@ impl User {
6195
}
6296

6397
pub async fn save(&self, dynamo: &aws_sdk_dynamodb::Client) {
64-
let mut item = HashMap::new();
65-
item.insert("user_id".to_string(), AttributeValue::S(self.user_id.clone()));
66-
item.insert(
67-
"links".to_string(),
68-
AttributeValue::L(
69-
self.links
70-
.iter()
71-
.map(|l| {
72-
let mut map = HashMap::new();
73-
map.insert("link_address".to_string(), AttributeValue::S(l.link_address.clone()));
74-
map.insert("linked_at".to_string(), AttributeValue::N(l.linked_at.to_string()));
75-
AttributeValue::M(map)
76-
})
77-
.collect(),
78-
),
79-
);
80-
item.insert(
81-
"linked_guild_ids".to_string(),
82-
AttributeValue::L(
83-
self.linked_guild_ids
84-
.iter()
85-
.map(|id| AttributeValue::S(id.clone()))
86-
.collect(),
87-
),
88-
);
8998

9099
match dynamo
91100
.put_item()
92101
.table_name(format!("kb2_users_{}", std::env::var("DEPLOYMENT_ENV").expect("DEPLOYMENT_ENV must be set")))
93-
.set_item(Some(item))
102+
.set_item(Some(self.clone().into()))
94103
.send()
95104
.await
96105
{

ui/src/components/auth/DiscordAuthButton.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {onClickOutside} from "@vueuse/core";
88
import {AuthorizationFlowPKCE, ImplicitFlow} from "../../helpers/auth";
99
1010
let props = defineProps({
11-
longText: Boolean
11+
longText: Boolean,
1212
})
1313
1414
const emit = defineEmits(['logout'])

ui/src/components/dashboard/AnnounceComponent.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
2+
let skeleton = true
33
</script>
44

55
<template>
@@ -10,10 +10,10 @@
1010
<fa :icon="['fas', 'bullhorn']"/>
1111
Announcements
1212
</h1>
13-
<button class="btn btn-primary btn-sm justify-end">Create</button>
13+
<button class="btn btn-primary btn-sm justify-end" v-if="!skeleton">Create</button>
1414
</div>
1515
<div class="divider my-0"></div>
16-
<table class="table">
16+
<table class="table" v-if="!skeleton">
1717
<thead>
1818
<tr>
1919
<th>Title</th>
@@ -67,6 +67,7 @@
6767
</tr>
6868
</tbody>
6969
</table>
70+
<div class="skeleton h-48 text-center font-bold p-20" v-if="skeleton"> Coming Soon </div>
7071
</div>
7172
</div>
7273
</template>

ui/src/components/dashboard/ColourRoleComponent.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</h1>
1313
</div>
1414
<div class="divider my-0"></div>
15-
<div class="skeleton h-48"></div>
15+
<div class="skeleton h-48 text-center font-bold p-20"> Coming Soon </div>
1616
</div>
1717
</div>
1818
</template>

ui/src/components/dashboard/InsightsStatsComponent.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
<fa :icon="['far', 'building']" size="xl"/>
1818
</div>
1919
<div class="stat-title">Total Guilds</div>
20-
<div class="stat-value text-primary">126</div>
21-
<div class="stat-desc">2% more than last month</div>
20+
<div class="stat-value text-primary">-</div>
21+
<div class="stat-desc">-% more than last month</div>
2222
</div>
2323

2424
<div class="stat">
2525
<div class="stat-figure text-secondary">
2626
<fa :icon="['far', 'user']" size="xl"/>
2727
</div>
2828
<div class="stat-title">Combined Members</div>
29-
<div class="stat-value text-secondary">62.4k</div>
30-
<div class="stat-desc">21% more than last month</div>
29+
<div class="stat-value text-secondary">-</div>
30+
<div class="stat-desc">-% more than last month</div>
3131
</div>
3232
</div>
3333
</div>

ui/src/components/dashboard/StreamAlertComponent.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
2+
let skeleton = true
33
</script>
44

55
<template>
@@ -10,10 +10,10 @@
1010
<fa :icon="['fas', 'video']"/>
1111
Stream Alert
1212
</h1>
13-
<button class="btn btn-primary btn-sm justify-end">Add</button>
13+
<button class="btn btn-primary btn-sm justify-end" v-if="!skeleton">Add</button>
1414
</div>
1515
<div class="divider my-0"></div>
16-
<table class="table">
16+
<table class="table" v-if="!skeleton">
1717
<thead>
1818
<tr>
1919
<th>Channel</th>
@@ -76,6 +76,7 @@
7676
</tr>
7777
</tbody>
7878
</table>
79+
<div class="skeleton h-48 text-center font-bold p-20" v-if="skeleton"> Coming Soon </div>
7980
</div>
8081
</div>
8182
</template>

0 commit comments

Comments
 (0)