Permalink
Browse files

fix(*): removed all problems leading to warnings

  • Loading branch information...
ziedavid committed Jul 26, 2017
1 parent 2a5dd68 commit af04df8efe2b9d3039f22351c646d1071731bb3b
@@ -0,0 +1,2 @@
[notification]
send-to = ["david-ziegler@posteo.de"]
@@ -0,0 +1,146 @@

pub enum Combination {
And,
Or
}

pub fn entries_by_tags<'a>(tags: &'a [String], triples: &'a [Triple], combination: &Combination) -> Box<Fn(&&Entry) -> bool + 'a> {
let triples: Vec<(&String, &String)> = triples
.into_iter()
.filter_map(|x| match *x {
Triple {
subject : ObjectId::Entry(ref e_id),
predicate : Relation::IsTaggedWith,
object : ObjectId::Tag(ref t_id)
} => Some((e_id,t_id)),
_ => None
})
.collect();

match *combination {
Combination::Or => {
Box::new(move |entry|
tags.iter()
.any(|tag| triples.iter().any(|t| *t.0 == entry.id && t.1 == tag))
)
},
Combination::And => {
Box::new(move |entry| {
let e_tags : Vec<&String> = triples
.iter()
.filter(|t| *t.0 == entry.id)
.map(|t|t.1)
.collect();
tags.iter().all(|tag|e_tags.iter().any(|t| *t == tag))
}
)
}
}
}

pub fn entries_by_search_text<'a>(text: &'a str) -> Box<Fn(&&Entry) -> bool + 'a> {
let words = to_words(text);
Box::new(move |e| {
words.iter().any(|word| {
e.title.to_lowercase().contains(word) || e.description.to_lowercase().contains(word)
})
})
}

#[cfg(test)]
mod tests {

use super::*;
use business::builder::*;

#[test]
fn filter_by_tags() {
let entries = vec![
Entry::build().id("a").finish(),
Entry::build().id("b").finish(),
Entry::build().id("c").finish(),
];
let tags = vec!["csa".into()];
let no_triples = vec![];
let x: Vec<&Entry> = entries.iter().filter(&*entries_by_tags(&tags,&no_triples, &Combination::Or)).collect();
assert_eq!(x.len(), 0);
let triples = vec![
Triple{ subject: ObjectId::Entry("b".into()), predicate: Relation::IsTaggedWith, object: ObjectId::Tag("csa".into())},
Triple{ subject: ObjectId::Entry("c".into()), predicate: Relation::IsTaggedWith, object: ObjectId::Tag("foo".into())}
];
let x: Vec<&Entry> = entries.iter().filter(&*entries_by_tags(&tags,&triples, &Combination::Or)).collect();
assert_eq!(x.len(), 1);
assert_eq!(x[0].id,"b");
let tags = vec!["csa".into(),"foo".into()];
let x: Vec<&Entry> = entries.iter().filter(&*entries_by_tags(&tags,&triples, &Combination::Or)).collect();
assert_eq!(x.len(), 2);
let x: Vec<&Entry> = entries.iter().filter(&*entries_by_tags(&tags,&triples, &Combination::And)).collect();
assert_eq!(x.len(), 0);
}

#[test]
fn search_entries_by_title() {
let entries = vec![
Entry::build().title("a title").description("x").finish(),
Entry::build().title("not so interesting").description("y").finish(),
];
let filter = entries_by_search_text("a");
let x: Vec<&Entry> = entries.iter().filter(&*filter).collect();
assert_eq!(x.len(), 1);
assert_eq!(x[0].title, "a title");
let filter = entries_by_search_text("ti");
let x: Vec<&Entry> = entries.iter().filter(&*filter).collect();
assert_eq!(x.len(), 2);
}

#[test]
fn search_entries_by_description() {
let entries = vec![
Entry::build().title("a").description("x").finish(),
Entry::build().title("b").description("y").finish(),
Entry::build().title("c").description("x").finish(),
];
let filter = entries_by_search_text("x");
let x: Vec<&Entry> = entries.iter().filter(&*filter).collect();
assert_eq!(x.len(), 2);
}

#[test]
fn search_with_multiple_words() {
let entries = vec![
Entry::build().title("SoLaWi").description("mit gemüse").finish(),
Entry::build().title("csa").description("This is a great csa").finish(),
Entry::build().title("solawi").description("Das ist eine tolle solawi").finish(),
];
let filter = entries_by_search_text("csa,toll");
let x: Vec<&Entry> = entries.iter().filter(&*filter).collect();
assert_eq!(x.len(), 2);
let filter = entries_by_search_text("great,to,gemü");
let x: Vec<&Entry> = entries.iter().filter(&*filter).collect();
assert_eq!(x.len(), 3);
}

#[test]
fn search_and_ignore_capitalisation() {
let e0 = Entry::build().title("Eintrag").description("Hallo! Ein EinTrag").finish();
let e1 = Entry::build().title("Ein trag").description("foo").finish();
let e2 = Entry::build().title("CSA").description("cool vegetables").finish();
let entries = vec![&e0, &e1, &e2];

let filter = entries_by_search_text("Foo");
let x: Vec<&Entry> = entries.iter().cloned().filter(&*filter).collect();
assert_eq!(x.len(), 1);
assert_eq!(x[0].id, e1.id);

let filter = entries_by_search_text("trag");
let x: Vec<&Entry> = entries.iter().cloned().filter(&*filter).collect();
assert_eq!(x.len(), 2);
assert_eq!(x[0].id, e0.id);
assert_eq!(x[1].id, e1.id);

let filter = entries_by_search_text("csa");
let x: Vec<&Entry> = entries.iter().cloned().filter(&*filter).collect();
assert_eq!(x.len(), 1);
assert_eq!(x[0].id, e2.id);
}
}
@@ -0,0 +1,81 @@
use business::usecase::{NewEntry, UpdateEntry};
use entities::Entry;

pub fn new_entry_email(e: &NewEntry, id: &str, categories: Vec<String>) -> String{
let intro_sentence = "ein neuer Eintrag auf der Karte von Morgen wurde erstellt";
let entry = Entry {
id : id.into(),
title:e.title.clone(),
description:e.description.clone(),
street:e.street.clone(),
zip:e.zip.clone(),
city:e.city.clone(),
country :e.country.clone(),
email:e.email.clone(),
telephone:e.telephone.clone(),
homepage:e.homepage.clone(),
categories:e.categories.clone(),
lat:0.0,
lng:0.0,
created:0,
version:0,
license:None
};
entry_email(&entry, categories, &e.tags, intro_sentence)
}

pub fn changed_entry_email(e: &UpdateEntry, categories: Vec<String>) -> String{
let intro_sentence = "folgender Eintrag der Karte von Morgen wurde verändert";
let entry = Entry {
id : e.id.clone(),
title:e.title.clone(),
description:e.description.clone(),
street:e.street.clone(),
zip:e.zip.clone(),
city:e.city.clone(),
country :e.country.clone(),
email:e.email.clone(),
telephone:e.telephone.clone(),
homepage:e.homepage.clone(),
categories:e.categories.clone(),
lat:0.0,
lng:0.0,
created:0,
version:0,
license:None
};
entry_email(&entry, categories, &e.tags, intro_sentence)
}

fn entry_email(e: &Entry, categories: Vec<String>, tags: &Vec<String>, intro_sentence: &str) -> String{
let category = if categories.len() > 0 { categories[0].clone() } else { "".to_string() };

format!(
"Hallo,
{introSentence}:\n
{title} ({category})
{description}\n
Tags: {tags}
Adresse: {street}, {zip} {city}, {country}
Webseite: {homepage}
Email-Adresse: {email}
Telefon: {telephone}\n
Eintrag anschauen oder bearbeiten:
https://prototyp.kartevonmorgen.org/#/?entry={id}\n
Du kannst dein Abonnement des Kartenbereichs abbestellen indem du dich auf https://prototyp.kartevonmorgen.org einloggst.\n
euphorische Grüße ;)
das Karte von Morgen-Team",
introSentence = intro_sentence,
title = &e.title,
id = &e.id,
description= &e.description,
street = e.street.clone().unwrap_or("".into()),
zip = e.zip.clone().unwrap_or("".into()),
city = e.city.clone().unwrap_or("".into()),
country = e.country.clone().unwrap_or("".into()),
email = e.email.clone().unwrap_or("".into()),
telephone = e.telephone.clone().unwrap_or("".into()),
homepage = e.homepage.clone().unwrap_or("".into()),
category = category,
tags = tags.join(", "))
}
@@ -18,7 +18,6 @@ pub fn distance(a: &Coordinate, b: &Coordinate) -> f64 {
EARTH_RADIUS * c
}


pub fn center(south_west: &Coordinate, north_east: &Coordinate) -> Coordinate {
Coordinate {
lat: (south_west.lat + north_east.lat) / 2.0,
@@ -564,7 +564,7 @@ pub fn create_or_modify_subscription(bbox: &Bbox, username: String, db: &mut Db)
.collect();

if user_subscriptions.len() > 0 {
db.delete_bbox_subscription(&user_subscriptions[0].clone());
db.delete_bbox_subscription(&user_subscriptions[0].clone())?;
}

let s_id = Uuid::new_v4().simple().to_string();
@@ -585,11 +585,6 @@ pub fn create_or_modify_subscription(bbox: &Bbox, username: String, db: &mut Db)
}

pub fn unsubscribe_all_bboxes(username: &str, db: &mut Db) -> Result<()>{
let users : Vec<User> = db.all_users()?
.into_iter()
.filter(|u| *u.id == *username)
.collect();
let u_id = &users[0].id;
let user_subscriptions : Vec<String> = db.all_triples()?
.into_iter()
.filter_map(|triple| match triple {
@@ -646,16 +641,10 @@ pub fn email_addresses_to_notify(lat: &f64, lng: &f64, db: &mut Db) -> Vec<Strin
.nth(0).unwrap()))
.collect();

let user_triples : Vec<Triple> = db.all_triples()
.unwrap()
.into_iter()
.filter(|triple| triple.subject == ObjectId::User("123".into()))
.collect();

let emails_to_notify : Vec<String> = users_and_bboxes.clone()
.into_iter()
.filter(|&(ref email, ref bbox)| geo::is_in_bbox(lat, lng, &bbox))
.map(|(email, bbox)| email)
.filter(|&(_, ref bbox)| geo::is_in_bbox(lat, lng, &bbox))
.map(|(email, _)| email)
.collect();

emails_to_notify
Oops, something went wrong.

0 comments on commit af04df8

Please sign in to comment.