-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
27 changed files
with
410 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ use actix_web::{post, HttpResponse}; | |
use serde::Deserialize; | ||
use tracing::error; | ||
|
||
use crate::limited::hash_map::LimitedHashMap; | ||
|
||
use super::github; | ||
use super::proposed_edits::coordinate::Coordinate; | ||
use super::proposed_edits::image::Image; | ||
|
@@ -17,7 +19,7 @@ mod discription; | |
mod image; | ||
mod tmp_repo; | ||
|
||
#[derive(Deserialize, Clone)] | ||
#[derive(Debug, Deserialize, Clone)] | ||
struct Edit { | ||
coordinate: Option<Coordinate>, | ||
image: Option<Image>, | ||
|
@@ -26,16 +28,17 @@ pub trait AppliableEdit { | |
fn apply(&self, key: &str, base_dir: &Path) -> String; | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[derive(Debug, Deserialize)] | ||
pub struct EditRequest { | ||
token: String, | ||
edits: HashMap<String, Edit>, | ||
edits: LimitedHashMap<String, Edit>, | ||
additional_context: String, | ||
privacy_checked: bool, | ||
} | ||
|
||
const GIT_URL: &str = "[email protected]:TUM-Dev/NavigaTUM.git"; | ||
impl EditRequest { | ||
#[tracing::instrument] | ||
async fn apply_changes_and_generate_description( | ||
&self, | ||
branch_name: &str, | ||
|
@@ -48,6 +51,7 @@ impl EditRequest { | |
} | ||
fn edits_for<T: AppliableEdit>(&self, extractor: fn(Edit) -> Option<T>) -> HashMap<String, T> { | ||
self.edits | ||
.0 | ||
.clone() | ||
.into_iter() | ||
.filter_map(|(k, edit)| extractor(edit).map(|coord| (k, coord))) | ||
|
@@ -57,10 +61,15 @@ impl EditRequest { | |
fn extract_labels(&self) -> Vec<String> { | ||
let mut labels = vec!["webform".to_string()]; | ||
|
||
if self.edits.iter().any(|(_, edit)| edit.coordinate.is_none()) { | ||
if self | ||
.edits | ||
.0 | ||
.iter() | ||
.any(|(_, edit)| edit.coordinate.is_none()) | ||
{ | ||
labels.push("coordinate".to_string()); | ||
} | ||
if self.edits.iter().any(|(_, edit)| edit.image.is_none()) { | ||
if self.edits.0.iter().any(|(_, edit)| edit.image.is_none()) { | ||
labels.push("image".to_string()); | ||
} | ||
labels | ||
|
@@ -100,12 +109,12 @@ pub async fn propose_edits( | |
.content_type("text/plain") | ||
.body("Using this endpoint without accepting the privacy policy is not allowed"); | ||
}; | ||
if req_data.edits.is_empty() { | ||
if req_data.edits.0.is_empty() { | ||
return HttpResponse::UnprocessableEntity() | ||
.content_type("text/plain") | ||
.body("Not enough edits provided"); | ||
}; | ||
if req_data.edits.len() > 500 { | ||
if req_data.edits.0.len() > 500 { | ||
return HttpResponse::InsufficientStorage() | ||
.content_type("text/plain") | ||
.body("Too many edits provided"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::collections::HashMap; | ||
use std::fmt; | ||
use std::hash::Hash; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::limited::OrMore; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Default)] | ||
pub struct LimitedHashMap<K: Eq + Hash, V>(pub HashMap<K, V>); | ||
|
||
impl<K: Eq + Hash, V> From<HashMap<K, V>> for LimitedHashMap<K, V> { | ||
fn from(value: HashMap<K, V>) -> Self { | ||
LimitedHashMap(value) | ||
} | ||
} | ||
|
||
const LIMIT: usize = 3; | ||
impl<K: fmt::Debug + Eq + Hash + Clone + Ord, V: fmt::Debug + Clone> fmt::Debug | ||
for LimitedHashMap<K, V> | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut collection = self.0.clone().into_iter().collect::<Vec<(K, V)>>(); | ||
collection.sort_unstable_by(|(k1, _), (k2, _)| k1.cmp(k2)); | ||
if self.0.len() <= LIMIT { | ||
f.debug_map().entries(collection).finish() | ||
} else { | ||
f.debug_map() | ||
.entries( | ||
collection | ||
.into_iter() | ||
.take(LIMIT) | ||
.map(|(k, v)| (OrMore::Value(k), OrMore::Value(v))) | ||
.chain([(OrMore::More, OrMore::More)]), | ||
) | ||
.finish() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_limited_output() { | ||
let w: LimitedHashMap<u32, u32> = LimitedHashMap(HashMap::new()); | ||
assert_eq!(format!("{w:?}"), "{}"); | ||
let w = LimitedHashMap(HashMap::from([(1, 1)])); | ||
assert_eq!(format!("{w:?}"), "{1: 1}"); | ||
let w = LimitedHashMap(HashMap::from([(1, 1), (2, 2)])); | ||
assert_eq!(format!("{w:?}"), "{1: 1, 2: 2}"); | ||
let w = LimitedHashMap(HashMap::from([(1, 1), (2, 2), (3, 3)])); | ||
assert_eq!(format!("{w:?}"), "{1: 1, 2: 2, 3: 3}"); | ||
let w = LimitedHashMap(HashMap::from([(1, 1), (2, 2), (3, 3), (4, 4)])); | ||
assert_eq!(format!("{w:?}"), "{1: 1, 2: 2, 3: 3, ...: ...}"); | ||
let w = LimitedHashMap(HashMap::from([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)])); | ||
assert_eq!(format!("{w:?}"), "{1: 1, 2: 2, 3: 3, ...: ...}"); | ||
} | ||
} |
Oops, something went wrong.