Skip to content

Commit

Permalink
fixes #25 and fixes#35
Browse files Browse the repository at this point in the history
  • Loading branch information
daraeman committed Aug 17, 2017
1 parent 7610031 commit 0b22d08
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 74 deletions.
166 changes: 104 additions & 62 deletions server/controller/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const Utils = require( "./utils" );
const innoutLocations = require( "innout_locations" );
const fs = require( "fs" );

const ObjectId = require( "mongoose" ).Schema.Types.ObjectId;

const base = process.env.BASE || process.cwd();

const cached_stores_file = base + "/data/stores/stores.json";

const findStoreNearCoords = function( latitude, longitude ) {
Expand All @@ -32,71 +29,112 @@ function parseStringForStoreHashtag( tweet ) {
return ( matches ) ? matches[1] : false;
}

function saveTwitterPlace( tweet, store ) {
function saveTwitterPlace( raw_twitter_place, store ) {

return new Promise( ( resolve, reject ) => {

if ( ! tweet.data.place || ! tweet.data.place.id )
if ( ! raw_twitter_place )
return resolve( null );

let twitter_place = new TwitterPlace();
twitter_place.last_update = new Date();
twitter_place.data = tweet.data.place;
twitter_place.save( ( error, twitter_place ) => {

if ( error )
return reject( error );
let this_twitter_place;
TwitterPlace.create({
last_update: new Date(),
data: raw_twitter_place,
})
.then( ( twitter_place ) => {
this_twitter_place = twitter_place;
store.location.twitter_place = twitter_place._id;
return store.save();
})
.then( () => {
return resolve( this_twitter_place );
})
.catch( ( error ) => {
return reject( error );
});
});
}

store.location.twitter_place = twitter_place._id;
store.save( ( error ) => {
function boundingBoxCenter( bounding_box ) {

if ( error )
return reject( error );
let lat_min = Infinity,
long_min = Infinity,
lat_max = -Infinity,
long_max = -Infinity;

return resolve( twitter_place );
});
});
bounding_box.forEach( ( val ) => {
lat_min = Math.min( lat_min, val[1] );
long_min = Math.min( long_min, val[0] );
lat_max = Math.max( lat_max, val[1] );
long_max = Math.max( long_max, val[0] );
});

return {
longitude: ( ( long_min + long_max ) / 2 ),
latitude: ( ( lat_min + lat_max ) / 2 )
};
}

function findSavedTwitterPlace( tweet ) {
function findSavedTwitterPlace( raw_twitter_place ) {

return new Promise( ( resolve, reject ) => {

if ( ! tweet.data.place || ! tweet.data.place.id )
if ( ! raw_twitter_place ) {
return resolve( null );
}

TwitterPlace.findOne( { "data.id": tweet.data.place.id }, ( error, twitter_place ) => {

if ( error )
return reject( error );

if ( ! twitter_place )
return resolve( null );
TwitterPlace.findOne( { "data.id": raw_twitter_place.id } )
.then( ( twitter_place ) => {

return resolve( twitter_place );
if ( ! twitter_place )
return resolve( null );

});
return resolve( twitter_place );
})
.catch( ( error ) => {
return reject( error );
});
});
}

function findStoreFromTwitterPlace( tweet ) {

return new Promise( ( resolve, reject ) => {
findSavedTwitterPlace( tweet ).then( ( twitter_place ) => {

if ( ! twitter_place )
return resolve( null );
let raw_twitter_place = tweet.data.place;
let this_twitter_place;
let is_new_twitter_place = false;

if ( ! raw_twitter_place )
return resolve();

findSavedTwitterPlace( raw_twitter_place )
.then( ( twitter_place ) => {
this_twitter_place = twitter_place;
if ( ! twitter_place ) {
is_new_twitter_place = true;
let coords = boundingBoxCenter( raw_twitter_place.bounding_box.coordinates[0] );
return findStoreNearCoords( coords.latitude, coords.longitude );
}
else {
return Store.findOne( { "location.twitter_place": twitter_place._id });
}
})
.then( ( store ) => {
this_store = store;

Store.findOne( { "location.twitter_place": new ObjectId( twitter_place._id ) }, ( error, store ) => {
if ( error )
return reject( error );
return resolve( store );
});
if ( is_new_twitter_place && store ) {
return saveTwitterPlace( raw_twitter_place, store );
}

}).catch( ( error ) => {
reject( error );
});
return null;
})
.then( () => {
return resolve( this_store );
})
.catch( ( error ) => {
reject( error );
});
});
}

Expand All @@ -109,28 +147,32 @@ const parseTweetForStore = function( tweet, ignore_hashtag ) {

let coords = getTweetCoords( tweet );
if ( ! coords ) {
return resolve( null );
}

findStoreNearCoords( coords.latitude, coords.longitude )
.then( ( store ) => {

if ( ! store ) {

findStoreFromTwitterPlace( tweet )
.then( ( store ) => {
return resolve( store );
});

}
else {
findStoreFromTwitterPlace( tweet )
.then( ( store ) => {
return resolve( store );
}

})
.catch( ( error ) => {
return reject( error );
});
});
}
else {
findStoreNearCoords( coords.latitude, coords.longitude )
.then( ( store ) => {

if ( ! store ) {

findStoreFromTwitterPlace( tweet )
.then( ( store ) => {
return resolve( store );
});

}
else {
return resolve( store );
}

})
.catch( ( error ) => {
return reject( error );
});
}
}
else {
Store.findOne( { number: store_number } )
Expand Down
13 changes: 1 addition & 12 deletions server/controller/tweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ const getTweetsFromSearchApp = function( search_string ) {

return new Promise( ( resolve, reject ) => {

const TweetQueue = require( "../model/tweet_queue" );
Tweet.remove({})
.then( () => {
return Receipt.remove({});
})
.then( () => {
return TweetQueue.remove({});
})
.then( () => {
return getLatestSearchTweetFromDb()
})
//getLatestSearchTweetFromDb()
getLatestSearchTweetFromDb()
.then( ( last_tweet ) => {

let search_params = { q: search_string, count: 100 };
Expand Down

0 comments on commit 0b22d08

Please sign in to comment.