Link shorting (no #)
This commit is contained in:
parent
ae67a8dad6
commit
a16d7b8a79
3 changed files with 31 additions and 21 deletions
|
|
@ -68,7 +68,7 @@ impl OmikronConnection {
|
||||||
message_text
|
message_text
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
sender.send(message_text).await.unwrap();
|
let _ = sender.send(message_text).await;
|
||||||
}
|
}
|
||||||
pub async fn get_omikron_id(&self) -> i64 {
|
pub async fn get_omikron_id(&self) -> i64 {
|
||||||
*self.omikron_id.read().await
|
*self.omikron_id.read().await
|
||||||
|
|
@ -208,6 +208,20 @@ impl OmikronConnection {
|
||||||
}
|
}
|
||||||
let omikron_id = self.get_omikron_id().await;
|
let omikron_id = self.get_omikron_id().await;
|
||||||
|
|
||||||
|
if cv.is_type(CommunicationType::shorten_link) {
|
||||||
|
if let Some(link) = cv.get_data(DataTypes::link) {
|
||||||
|
if let Some(link) = link.as_str() {
|
||||||
|
if let Ok(short_link) = add_short_link(link).await {
|
||||||
|
let response = CommunicationValue::new(CommunicationType::shorten_link)
|
||||||
|
.with_id(cv.get_id())
|
||||||
|
.add_data(DataTypes::link, JsonValue::String(short_link));
|
||||||
|
self.send_message(&response).await;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ONLINE STATUS TRACKING
|
// ONLINE STATUS TRACKING
|
||||||
if cv.is_type(CommunicationType::user_connected) {
|
if cv.is_type(CommunicationType::user_connected) {
|
||||||
if let Some(user_id) = cv.get_data(DataTypes::user_id).and_then(|v| v.as_i64()) {
|
if let Some(user_id) = cv.get_data(DataTypes::user_id).and_then(|v| v.as_i64()) {
|
||||||
|
|
@ -474,20 +488,6 @@ impl OmikronConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cv.is_type(CommunicationType::shorten_link) {
|
|
||||||
if let Some(link) = cv.get_data(DataTypes::link) {
|
|
||||||
if let Some(link) = link.as_str() {
|
|
||||||
if let Ok(short_link) = add_short_link(link).await {
|
|
||||||
let response = CommunicationValue::new(CommunicationType::shorten_link)
|
|
||||||
.with_id(cv.get_id())
|
|
||||||
.add_data(DataTypes::link, JsonValue::String(short_link));
|
|
||||||
self.send_message(&response).await;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let response =
|
let response =
|
||||||
CommunicationValue::new(CommunicationType::error_not_found).with_id(cv.get_id());
|
CommunicationValue::new(CommunicationType::error_not_found).with_id(cv.get_id());
|
||||||
self.send_message(&response).await;
|
self.send_message(&response).await;
|
||||||
|
|
|
||||||
|
|
@ -128,8 +128,8 @@ impl Service<HttpRequest<Incoming>> for HttpService {
|
||||||
|
|
||||||
Ok(api::handle(&path, headers.clone(), body_string).await)
|
Ok(api::handle(&path, headers.clone(), body_string).await)
|
||||||
} else if path.starts_with("/direct") {
|
} else if path.starts_with("/direct") {
|
||||||
let short = path.split('/').nth(2).unwrap_or_default();
|
let short = path.replace("/direct/", "");
|
||||||
if let Ok(long) = get_short_link(short).await {
|
if let Ok(long) = get_short_link(&short).await {
|
||||||
let response = HttpResponse::builder()
|
let response = HttpResponse::builder()
|
||||||
.status(StatusCode::FOUND)
|
.status(StatusCode::FOUND)
|
||||||
.header("Location", long)
|
.header("Location", long)
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,14 @@ use rand::{Rng, thread_rng};
|
||||||
|
|
||||||
static LINKS: Lazy<DashMap<String, String>> = Lazy::new(DashMap::new);
|
static LINKS: Lazy<DashMap<String, String>> = Lazy::new(DashMap::new);
|
||||||
|
|
||||||
const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ#1234567890";
|
const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ1234567890";
|
||||||
|
|
||||||
pub async fn add_short_link(long: &str) -> Result<String, ()> {
|
pub async fn add_short_link(long: &str) -> Result<String, ()> {
|
||||||
let raw = generate_unique_short_link().await;
|
let raw = generate_unique_short_link().await;
|
||||||
LINKS.insert(raw.clone(), long.to_string());
|
LINKS.insert(raw.clone(), long.to_string());
|
||||||
|
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"omega.tensamin.net/direct/{}",
|
"https://omega.tensamin.net/direct/{}",
|
||||||
format_with_dashes(&raw)
|
format_with_dashes(&raw)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
@ -38,9 +38,19 @@ pub async fn generate_short_link() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_short_link(short: &str) -> Result<String, ()> {
|
pub async fn get_short_link(short: &str) -> Result<String, ()> {
|
||||||
let normalized = normalize_short(short);
|
let key = if short.contains("/") {
|
||||||
|
short.split("/").nth(1).unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
short
|
||||||
|
};
|
||||||
|
let frag = short.replace(key, "");
|
||||||
|
let normalized = normalize_short(&key);
|
||||||
|
|
||||||
LINKS.get(&normalized).map(|v| v.value().clone()).ok_or(())
|
if let Ok(t) = LINKS.get(&normalized).map(|v| v.value().clone()).ok_or(()) {
|
||||||
|
Ok(format!("{}{}", t, frag))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------- helpers ---------------- */
|
/* ---------------- helpers ---------------- */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue