Permalink
Browse files

update item if it already exists. and mongoose's 'unique' attribute d…

…oesn't seem to be working..
  • Loading branch information...
1 parent 9ecf647 commit 7c167c81ae6d6514d92ece305a7028d02414dabd @brianshaler brianshaler committed Jun 1, 2012
Showing with 296 additions and 240 deletions.
  1. +106 −84 controllers/FoursquareController.js
  2. +92 −75 controllers/InstagramController.js
  3. +98 −81 controllers/TwitterController.js
@@ -243,106 +243,128 @@ exports.controller = function(req, res, next) {
cb();
return;
}
-
+
var message = "";
if (checkin.shout && checkin.shout.length > 0) {
message = checkin.shout + " @ " + checkin.venue.name;
} else {
message = "I'm at " + checkin.venue.name;
}
-
+
var identity;
- var activity_item = new ActivityItem();
- Identity.findOne({platform: self.platform, platform_id: checkin.user.id}, function (err, identity) {
- if (err || !identity) {
- identity = new Identity();
- identity.photo = [];
- }
- identity.platform = self.platform;
- identity.platform_id = checkin.user.id;
- identity.user_name = checkin.user.firstName + " " + checkin.user.lastName;
- identity.display_name = identity.user_name;
- identity.guid = identity.platform + "-" + identity.platform_id;
- var photo_found = false;
- identity.photo.forEach(function (photo) {
- if (photo.url == checkin.user.photo) {
- photo_found = true;
- }
- });
- if (!photo_found) {
- identity.photo.push({url: checkin.user.photo});
+ var new_item = false;
+ var activity_item;// = new ActivityItem();
+
+ ActivityItem.findOne({guid: self.platform+"-"+checkin.id}, function (err, item) {
+ if (err) throw err;
+
+ if (item) {
+ // it exists, let's just update it
+ activity_item = item;
+ } else {
+ // doesn't exist, create a blank one
+ activity_item = new ActivityItem();
+ new_item = true;
}
- identity.updated_at = new Date();
- identity.save(function (err) {
- activity_item.platform = self.platform;
- activity_item.guid = activity_item.platform + "-" + checkin.id;
- activity_item.user = identity.id;
- activity_item.message = message;
- if (checkin.photos.count > 0) {
- var image = {};
- image.type = "photo";
- image.sizes = [];
- checkin.photos.items.forEach (function (photo) {
- photo.sizes.items.forEach(function (size) {
- image.sizes.push({url: size.url, width: size.width, height: size.height});
- });
- });
- activity_item.media = [image];
- }
- activity_item.posted_at = new Date(Date.parse(checkin.createdAt));
- activity_item.analyzed_at = new Date(0);
- activity_item.topics = [];
- activity_item.characteristics = [];
- activity_item.attributes = {};
- activity_item.data = checkin;
-
- var chars = [];
- chars.push("Foursquare venue: "+checkin.venue.name);
- if (checkin.shout && checkin.shout.length > 0) {
- chars.push("Foursquare shout");
- } else {
- chars.push("Foursquare no shout");
+
+ Identity.findOne({platform: self.platform, platform_id: checkin.user.id}, function (err, identity) {
+ if (err || !identity) {
+ identity = new Identity();
+ identity.photo = [];
}
-
- function add_characteristic () {
- if (chars.length == 0) {
- return save_activity_item();
+ identity.platform = self.platform;
+ identity.platform_id = checkin.user.id;
+ identity.guid = identity.platform + "-" + identity.platform_id;
+ identity.user_name = checkin.user.firstName + " " + checkin.user.lastName;
+ identity.display_name = identity.user_name;
+ var photo_found = false;
+ identity.photo.forEach(function (photo) {
+ if (photo.url == checkin.user.photo) {
+ photo_found = true;
}
- ch = chars.shift();
- Characteristic.findOne({text: ch}, function (err, c) {
- if (err || !c) {
- c = new Characteristic({text: ch, ratings: {overall: 0}});
- c.save(function (err) {
- activity_item.characteristics.push(c.id);
- add_characteristic();
+ });
+ if (!photo_found) {
+ identity.photo.push({url: checkin.user.photo});
+ }
+ identity.updated_at = new Date();
+ identity.save(function (err) {
+ activity_item.platform = self.platform;
+ activity_item.guid = activity_item.platform + "-" + checkin.id;
+ activity_item.user = identity.id;
+ activity_item.message = message;
+ if (checkin.photos.count > 0) {
+ var image = {};
+ image.type = "photo";
+ image.sizes = [];
+ checkin.photos.items.forEach (function (photo) {
+ photo.sizes.items.forEach(function (size) {
+ image.sizes.push({url: size.url, width: size.width, height: size.height});
});
+ });
+ activity_item.media = [image];
+ }
+ activity_item.posted_at = new Date(Date.parse(checkin.createdAt));
+ activity_item.data = checkin;
+
+ var chars = [];
+
+ if (new_item) {
+ activity_item.analyzed_at = new Date(0);
+ activity_item.topics = [];
+ activity_item.characteristics = [];
+ activity_item.attributes = {};
+
+ chars.push("Foursquare venue: "+checkin.venue.name);
+ if (checkin.shout && checkin.shout.length > 0) {
+ chars.push("Foursquare shout");
} else {
- activity_item.characteristics.push(c.id);
- add_characteristic();
+ chars.push("Foursquare no shout");
}
- });
- }
- add_characteristic();
-
- function save_activity_item () {
- activity_item.save(function (err) {
- // ERROR?
- activity_item.analyze(function (err, _item) {
- if (!err && _item) {
- _item.save(function (err) {
- //console.log("ActivytItem saved / "+err);
- // ERROR?
+ checkin.venue.categories.forEach(function (category) {
+ chars.push("Venue category: "+category.name);
+ });
+ }
+
+ function add_characteristic () {
+ if (chars.length == 0) {
+ return save_activity_item();
+ }
+ ch = chars.shift();
+ Characteristic.findOne({text: ch}, function (err, c) {
+ if (err || !c) {
+ c = new Characteristic({text: ch, ratings: {overall: 0}});
+ c.save(function (err) {
+ activity_item.characteristics.push(c.id);
+ add_characteristic();
});
- }
- if (cb) {
- //console.log("Finished: "+checkin.text.substring(0, 50));
- cb();
+ } else {
+ activity_item.characteristics.push(c.id);
+ add_characteristic();
}
});
- });
- }
- });
- });
+ }
+ add_characteristic();
+
+ function save_activity_item () {
+ activity_item.save(function (err) {
+ // ERROR?
+ activity_item.analyze(function (err, _item) {
+ if (!err && _item) {
+ _item.save(function (err) {
+ //console.log("ActivytItem saved / "+err);
+ // ERROR?
+ });
+ }
+ if (cb) {
+ //console.log("Finished: "+checkin.text.substring(0, 50));
+ cb();
+ }
+ });
+ });
+ }
+ }); // id.save
+ }); // Identity.findOne
+ }); // ActivityItem.findOne
}
self._get_settings = function (cb) {
@@ -280,89 +280,106 @@ exports.controller = function(req, res, next) {
}
var identity;
- var activity_item = new ActivityItem();
- Identity.findOne({platform: self.platform, platform_id: post.user.id}, function (err, identity) {
- if (err || !identity) {
- identity = new Identity();
- identity.photo = [];
+ var new_item = false;
+ var activity_item;// = new ActivityItem();
+
+ ActivityItem.findOne({guid: self.platform+"-"+post.id}, function (err, item) {
+ if (err) throw err;
+
+ if (item) {
+ // it exists, let's just update it
+ activity_item = item;
+ } else {
+ // doesn't exist, create a blank one
+ activity_item = new ActivityItem();
+ new_item = true;
}
- identity.platform = self.platform;
- identity.platform_id = post.user.id;
- identity.user_name = post.user.username;
- identity.display_name = post.user.full_name + " (" + post.user.username + ")";
- identity.guid = identity.platform + "-" + identity.platform_id;
- var photo_found = false;
- identity.photo.forEach(function (photo) {
- if (photo.url == post.user.profile_picture) {
- photo_found = true;
+
+ Identity.findOne({platform: self.platform, platform_id: post.user.id}, function (err, identity) {
+ if (err || !identity) {
+ identity = new Identity();
+ identity.photo = [];
}
- });
- if (!photo_found) {
- identity.photo.push({url: post.user.profile_picture});
- }
- identity.updated_at = new Date();
- identity.save(function (err) {
- activity_item.platform = self.platform;
- activity_item.guid = activity_item.platform + "-" + post.id;
- activity_item.user = identity.id;
- activity_item.message = message;
- var image = {};
- var keys = ["standard_resolution", "thumbnail", "low_resolution"];
- image.type = "photo";
- image.sizes = [];
- keys.forEach (function (size) {
- image.sizes.push({url: post.images[size].url, width: post.images[size].width, height: post.images[size].height});
+ identity.platform = self.platform;
+ identity.platform_id = post.user.id;
+ identity.guid = identity.platform + "-" + identity.platform_id;
+ identity.user_name = post.user.username;
+ identity.display_name = post.user.full_name + " (" + post.user.username + ")";
+ var photo_found = false;
+ identity.photo.forEach(function (photo) {
+ if (photo.url == post.user.profile_picture) {
+ photo_found = true;
+ }
});
- activity_item.media = [image];
- activity_item.posted_at = new Date(parseInt(post.created_time)*1000);
- activity_item.analyzed_at = new Date(0);
- activity_item.topics = [];
- activity_item.characteristics = [];
- activity_item.attributes = {};
- activity_item.data = post;
-
- var chars = [];
- //chars.push("Instagram venue: "+post.venue.name);
-
- function add_characteristic () {
- if (chars.length == 0) {
- return save_activity_item();
+ if (!photo_found) {
+ identity.photo.push({url: post.user.profile_picture});
+ }
+ identity.updated_at = new Date();
+ identity.save(function (err) {
+ activity_item.platform = self.platform;
+ activity_item.guid = activity_item.platform + "-" + post.id;
+ activity_item.user = identity.id;
+ activity_item.message = message;
+ var image = {};
+ var keys = ["standard_resolution", "thumbnail", "low_resolution"];
+ image.type = "photo";
+ image.sizes = [];
+ keys.forEach (function (size) {
+ image.sizes.push({url: post.images[size].url, width: post.images[size].width, height: post.images[size].height});
+ });
+ activity_item.media = [image];
+ activity_item.posted_at = new Date(parseInt(post.created_time)*1000);
+ activity_item.data = post;
+
+ var chars = [];
+
+ if (new_item) {
+ activity_item.analyzed_at = new Date(0);
+ activity_item.topics = [];
+ activity_item.characteristics = [];
+ activity_item.attributes = {};
}
- ch = chars.shift();
- Characteristic.findOne({text: ch}, function (err, c) {
- if (err || !c) {
- c = new Characteristic({text: ch, ratings: {overall: 0}});
- c.save(function (err) {
- activity_item.characteristics.push(c.id);
- add_characteristic();
- });
- } else {
- activity_item.characteristics.push(c.id);
- add_characteristic();
+
+ function add_characteristic () {
+ if (chars.length == 0) {
+ return save_activity_item();
}
- });
- }
- add_characteristic();
-
- function save_activity_item () {
- activity_item.save(function (err) {
- // ERROR?
- activity_item.analyze(function (err, _item) {
- if (!err && _item) {
- _item.save(function (err) {
- //console.log("ActivytItem saved / "+err);
- // ERROR?
+ ch = chars.shift();
+ Characteristic.findOne({text: ch}, function (err, c) {
+ if (err || !c) {
+ c = new Characteristic({text: ch, ratings: {overall: 0}});
+ c.save(function (err) {
+ activity_item.characteristics.push(c.id);
+ add_characteristic();
});
- }
- if (cb) {
- //console.log("Finished: "+post.text.substring(0, 50));
- cb();
+ } else {
+ activity_item.characteristics.push(c.id);
+ add_characteristic();
}
});
- });
- }
- });
- });
+ }
+ add_characteristic();
+
+ function save_activity_item () {
+ activity_item.save(function (err) {
+ // ERROR?
+ activity_item.analyze(function (err, _item) {
+ if (!err && _item) {
+ _item.save(function (err) {
+ //console.log("ActivytItem saved / "+err);
+ // ERROR?
+ });
+ }
+ if (cb) {
+ //console.log("Finished: "+post.text.substring(0, 50));
+ cb();
+ }
+ });
+ });
+ }
+ }); // id.save
+ }); // Identity.findOne
+ }); // ActivityItem.findOne
}
self._get_settings = function (cb) {
Oops, something went wrong.

0 comments on commit 7c167c8

Please sign in to comment.