Link shorting
This commit is contained in:
parent
ae67a8dad6
commit
bd49f9499f
3 changed files with 30 additions and 20 deletions
|
|
@ -68,7 +68,7 @@ impl OmikronConnection {
|
|||
message_text
|
||||
);
|
||||
}
|
||||
sender.send(message_text).await.unwrap();
|
||||
let _ = sender.send(message_text).await;
|
||||
}
|
||||
pub async fn get_omikron_id(&self) -> i64 {
|
||||
*self.omikron_id.read().await
|
||||
|
|
@ -208,6 +208,20 @@ impl OmikronConnection {
|
|||
}
|
||||
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
|
||||
if cv.is_type(CommunicationType::user_connected) {
|
||||
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 =
|
||||
CommunicationValue::new(CommunicationType::error_not_found).with_id(cv.get_id());
|
||||
self.send_message(&response).await;
|
||||
|
|
|
|||
|
|
@ -128,8 +128,8 @@ impl Service<HttpRequest<Incoming>> for HttpService {
|
|||
|
||||
Ok(api::handle(&path, headers.clone(), body_string).await)
|
||||
} else if path.starts_with("/direct") {
|
||||
let short = path.split('/').nth(2).unwrap_or_default();
|
||||
if let Ok(long) = get_short_link(short).await {
|
||||
let short = path.replace("/direct/", "");
|
||||
if let Ok(long) = get_short_link(&short).await {
|
||||
let response = HttpResponse::builder()
|
||||
.status(StatusCode::FOUND)
|
||||
.header("Location", long)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ pub async fn add_short_link(long: &str) -> Result<String, ()> {
|
|||
LINKS.insert(raw.clone(), long.to_string());
|
||||
|
||||
Ok(format!(
|
||||
"omega.tensamin.net/direct/{}",
|
||||
"https://omega.tensamin.net/direct/{}",
|
||||
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, ()> {
|
||||
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 ---------------- */
|
||||
|
|
|
|||
Loading…
Reference in a new issue