diff --git a/cloud/Community.js b/cloud/Community.js index 10fb03842c..276d73cd04 100644 --- a/cloud/Community.js +++ b/cloud/Community.js @@ -55,21 +55,21 @@ function incrementCommunityTotalsForKey(community, key, response) { CommunityTotals.getAllTimeTotals(community, function(allTimeTotals) { allTimeTotals.increment(key); - allTimeTotals.save(); + allTimeTotals.save({},{ useMasterKey: true }); if (--queryCount == 0) { response.success(); } }); CommunityTotals.getDailyTotals(community, currDate, function(dailyTotals) { dailyTotals.increment(key); - dailyTotals.save(); + dailyTotals.save({},{ useMasterKey: true }); if (--queryCount == 0) { response.success(); } }); CommunityTotals.getMonthlyTotals(community, currDate, function(monthlyTotals) { monthlyTotals.increment(key); - monthlyTotals.save(); + monthlyTotals.save({},{ useMasterKey: true }); if (--queryCount == 0) { response.success(); } diff --git a/cloud/CommunityTotals.js b/cloud/CommunityTotals.js index 06b8608d4c..d3601f0327 100644 --- a/cloud/CommunityTotals.js +++ b/cloud/CommunityTotals.js @@ -5,8 +5,8 @@ exports.getAllTimeTotals = function(community, callback) { var CommunityAllTimeTotals = Parse.Object.extend("CommunityAllTimeTotals"); var totalQuery = new Parse.Query(CommunityAllTimeTotals); totalQuery.equalTo("community", community); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { // success var allTimeTotals = object; if (!allTimeTotals) { console.log("CommunityAllTimeTotals not found, creating"); @@ -20,14 +20,14 @@ exports.getAllTimeTotals = function(community, callback) { allTimeTotals.set("missedCallCount", 0); allTimeTotals.set("missedOtherCount", 0); allTimeTotals.set("community", community); - allTimeTotals.save(); + allTimeTotals.save({},{ useMasterKey: true }); } callback(allTimeTotals); }, - error: function(error) { + function(error) { // error console.error("Got an error " + error.code + " : " + error.message); } - }); + ); } exports.getDailyTotals = function(community, date, callback, failCallback) { @@ -38,8 +38,8 @@ exports.getDailyTotals = function(community, date, callback, failCallback) { queryDate.setHours(0, 0, 0, 0); //only year, month, day remain totalQuery.equalTo("date", queryDate); totalQuery.equalTo("community", community); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { //success var dailyTotals = object; if (!dailyTotals) { console.log("CommunityDailyTotals not found, creating"); @@ -54,18 +54,18 @@ exports.getDailyTotals = function(community, date, callback, failCallback) { dailyTotals.set("missedCallCount", 0); dailyTotals.set("missedOtherCount", 0); dailyTotals.set("community", community); - dailyTotals.save(); + dailyTotals.save({},{ useMasterKey: true }); } callback(dailyTotals); }, - error: function(error) { + function(error) { // error console.error("Got an error " + error.code + " : " + error.message); if (failCallback) { failCallback(error); } } - }); + ); } exports.getMonthlyTotals = function(community, date, callback, failCallback) { @@ -77,8 +77,8 @@ exports.getMonthlyTotals = function(community, date, callback, failCallback) { queryDate.setDate(1); //only year, month remain totalQuery.equalTo("date", queryDate); totalQuery.equalTo("community", community); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { var monthlyTotals = object; if (!monthlyTotals) { console.log("CommunityMonthlyTotals not found, creating"); @@ -93,18 +93,18 @@ exports.getMonthlyTotals = function(community, date, callback, failCallback) { monthlyTotals.set("missedCallCount", 0); monthlyTotals.set("missedOtherCount", 0); monthlyTotals.set("community", community); - monthlyTotals.save(); + monthlyTotals.save({},{ useMasterKey: true }); } callback(monthlyTotals); }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); if (failCallback) { failCallback(error); } } - }); + ); } exports.updateTotals = function(trip, totals) { @@ -136,6 +136,6 @@ exports.updateTotals = function(trip, totals) { if (missedOtherCount) { totals.increment("missedOtherCount", missedOtherCount); } - totals.save(); + totals.save({},{ useMasterKey: true }); } } diff --git a/cloud/Installation.js b/cloud/Installation.js index becb28e3b5..fd2096e815 100644 --- a/cloud/Installation.js +++ b/cloud/Installation.js @@ -10,17 +10,16 @@ Parse.Cloud.beforeSave(Parse.Installation, function(request, response) { id: installation.get("user").id }); - userPointer.fetch({ - success: function(user) { + userPointer.fetch({ useMasterKey: true }).then( + function(user) { installation.set("channels", user.get("channels")); response.success(); }, - error: function(myObject, error) { + function(myObject, error) { console.error("Unable to find user " + userPointer.id + " " + error.code + " : " + error.message); response.error(); - }, - useMasterKey:true - }); + } + ); } else { response.success(); } diff --git a/cloud/POFriendRelation.js b/cloud/POFriendRelation.js index b3245ff2ca..68b84a5d4b 100644 --- a/cloud/POFriendRelation.js +++ b/cloud/POFriendRelation.js @@ -6,26 +6,24 @@ Parse.Cloud.beforeSave("POFriendRelation", function(request, response) { var friendUser = request.object.get("friendUser"); - var saveOptions = { - success: function(user) { - response.success(); - }, - error: function(user, error) { - response.error("Unable to save the user"); - } - } - function addAndRemoveChannels(addedChannel, removedChannel) { request.user.remove("channels", removedChannel); - request.user.save(null, { - success: function(user) { + request.user.save({},{ useMasterKey: true }).then( + function(user) { request.user.addUnique("channels", addedChannel); - request.user.save(null, saveOptions); + request.user.save(null, { useMasterKey: true }).then( + function(user) { + response.success(); + }, + function(user, error) { + response.error("Unable to save the user"); + } + ); }, - error: function(user, error) { + function(user, error) { response.error("Unable to save the user"); } - }); + ); } function updateChannelsIfNeeded() { @@ -49,25 +47,23 @@ Parse.Cloud.beforeSave("POFriendRelation", function(request, response) { if (request.user.id != friendUser.id) { var roleQuery = new Parse.Query(Parse.Role); roleQuery.equalTo("name", "user-" + request.user.id); - roleQuery.first({ - success: function(role) { - Parse.Cloud.useMasterKey(); + roleQuery.first({ useMasterKey: true }).then( + function(role) { role.relation("users").add(friendUser); - role.save(null, { - success: function(user) { + role.save({},{ useMasterKey: true }).then( + function(user) { updateChannelsIfNeeded(); }, - error: function(user, error) { + function(user, error) { response.error("Unable to save the role"); } - }); + ); }, - error: function(error) { + function(error) { console.log("Failed to save role for friend relation with error " + error.code + " : " + error.message); response.error("Unable to find the role"); - }, - useMasterKey:true - }); + } + ); } else { response.success(); } @@ -85,34 +81,34 @@ Parse.Cloud.afterSave("POFriendRelation", function(request) { var query = new Parse.Query("POFriendRequest"); query.equalTo("requestingUser", user); query.equalTo("requestedUser", friendUser); - query.find({ - success: function(results) { + query.find({ useMasterKey: true }).then( + function(results) { if (results.length > 0) { for (var i = 0; i < results.length; i++) { - results[i].destroy(); + results[i].destroy({ useMasterKey: true }); } } var friendQuery = new Parse.Query("POFriendRequest"); friendQuery.equalTo("requestingUser", friendUser); friendQuery.equalTo("requestedUser", user); - friendQuery.find({ - success: function(results) { + friendQuery.find({ useMasterKey: true }).then( + function(results) { if (results.length > 0) { for (var i = 0; i < results.length; i++) { - results[i].destroy(); + results[i].destroy({ useMasterKey: true }); } } }, - error: function(error) { + function(error) { console.log("error when destroying friend request"); } - }); + ); }, - error: function(error) { + function(error) { console.log("error when destroying friend request"); } - }); + ); }); @@ -122,27 +118,24 @@ Parse.Cloud.afterDelete("POFriendRelation", function(request) { var userPointer = request.object.get("user"); var friendUserPointer = request.object.get("friendUser"); - userPointer.fetch({ - success: function(user) { + userPointer.fetch({ useMasterKey: true }).then( + function(user) { var roleQuery = new Parse.Query(Parse.Role); roleQuery.equalTo("name", "user-" + user.id); - roleQuery.first({ - success: function(role) { - Parse.Cloud.useMasterKey(); + roleQuery.first({ useMasterKey: true }).then( + function(role) { role.relation("users").remove(friendUserPointer); - role.save(); + role.save({},{ useMasterKey: true }); }, - error: function(error) { + function(error) { console.log("Failed to remove role for friend relation with error " + error.code + " : " + error.message); - }, - useMasterKey:true - }); + } + ); user.remove("channels", "friend-" + friendUserPointer.id); user.save(null, {useMasterKey:true}); }, - error: function(myObject, error) { + function(myObject, error) { console.error("Unable to find user " + userPointer.id + " " + error.code + " : " + error.message); - }, - useMasterKey: true - }); + } + ); }); diff --git a/cloud/POFriendRequest.js b/cloud/POFriendRequest.js index 365d83e60d..e37df37f7a 100644 --- a/cloud/POFriendRequest.js +++ b/cloud/POFriendRequest.js @@ -14,8 +14,8 @@ Parse.Cloud.beforeSave("POFriendRequest", function(request, response) { var query = new Parse.Query("POFriendRelation"); query.equalTo("friendUserId", requestingUser); query.equalTo("userId", requestedUser); - query.find({ - success: function(results) { + query.find({ useMasterKey: true }).then( + function(results) { if (results.length > 0) { console.log("Not allowed to create a friend request when already friends."); response.error(JSON.stringify({ @@ -38,9 +38,9 @@ Parse.Cloud.beforeSave("POFriendRequest", function(request, response) { } var query = Parse.Query.or(queryFriendRequest, queryInverseFriendRequest); - query.find({ - success: function(results) { - if (results.length > 0) { + query.find({ useMasterKey: true }).then( + function(res) { + if (res.length > 0) { console.log("Friend request already exists."); response.error(JSON.stringify({ code: 1, @@ -50,18 +50,18 @@ Parse.Cloud.beforeSave("POFriendRequest", function(request, response) { response.success(); } }, - error: function(error) { - console.log("error when checking for existing relations, allowing to continute"); + function(error) { + console.log("error when checking for existing relations, allowing to continue"); response.success(); } - }); + ); } }, - error: function(error) { - console.log("error when checking for existing relations, allowing to continute"); + function(error) { + console.log("error when checking for existing relations, allowing to continue"); response.success(); } - }); + ); } else { console.log("Love thyself."); response.error(JSON.stringify({ @@ -76,17 +76,15 @@ Parse.Cloud.afterSave("POFriendRequest", function(request) { if (!request.object.existed()) { var roleQuery = new Parse.Query(Parse.Role); roleQuery.equalTo("name", "user-" + request.user.id); - roleQuery.first({ - success: function(role) { - Parse.Cloud.useMasterKey(); + roleQuery.first({ useMasterKey: true }).then( + function(role) { role.relation("users").add(request.object.get("requestedUser")); - role.save(); + role.save({},{ useMasterKey: true }); }, - error: function(error) { + function(error) { console.log("Failed to save role for friend request with error " + error.code + " : " + error.message); - }, - useMasterKey:true - }); + } + ); } }); @@ -99,24 +97,24 @@ Parse.Cloud.afterDelete("POFriendRequest", function(request) { queryInverseFriendRequest.equalTo("requestingUser", requestedUser); queryInverseFriendRequest.equalTo("requestedUser", requestingUser); - queryInverseFriendRequest.find({ - success: function(results) { - Parse.Object.destroyAll(results, { - success: function() {}, - error: function(error) { + queryInverseFriendRequest.find({ useMasterKey: true }).then( + function(results) { + Parse.Object.destroyAll({ useMasterKey: true }).then( + function() {}, + function(error) { console.error("Error deleting inverse friend relations " + error.code + ": " + error.message); } - }); + ); }, - error: function(error) { + function(error) { console.error("Error finding inverse friend relations " + error.code + ": " + error.message); } - }); + ); }); Parse.Cloud.define("requestedFriend", function(request, response) { - var pushCallbackCounter = new NetworkUtil.CallbackCounter(2, response); + var pushCallbackCounter = new NetworkUtil.CallbackCounter(1, response); var channels = ["user-" + request.params.requested_user]; @@ -124,14 +122,15 @@ Parse.Cloud.define("requestedFriend", function(request, response) { userId: request.user.id, userName: request.user.get("displayName") }; + PushNotifications.sendAndroidPush(channels, "ca.appcolony.distracteddriver.FRIEND_REQUEST", androidPushData, pushCallbackCounter); - - var alert = { - "loc-key": "notification-friend-request", - "loc-args": [request.user.get("displayName")] - }; - var iOSData = { - userId: request.user.id - }; - PushNotifications.sendIOSPush(channels, alert, true, "friendRequest", iOSData, pushCallbackCounter); + + // var alert = { + // "loc-key": "notification-friend-request", + // "loc-args": [request.user.get("displayName")] + // }; + // var iOSData = { + // userId: request.user.id + // }; + // PushNotifications.sendIOSPush(channels, alert, true, "friendRequest", iOSData, pushCallbackCounter); }); diff --git a/cloud/POPublicUser.js b/cloud/POPublicUser.js index eb5650878a..3cc40cfad1 100644 --- a/cloud/POPublicUser.js +++ b/cloud/POPublicUser.js @@ -26,8 +26,8 @@ Parse.Cloud.afterSave("POPublicUser", function(request) { if (facebookId) { var query = new Parse.Query(POPublicUser); query.equalTo("facebookIdHashed", facebookId); - query.find({ - success: function(results) { + query.find({ useMasterKey: true }).then( + function(results) { if (results.length > 0) { for (var i = 0; i < results.length; i++) { if (user.id != results[i].id) { @@ -37,6 +37,6 @@ Parse.Cloud.afterSave("POPublicUser", function(request) { } } } - }); + ); } }); diff --git a/cloud/POTrip.js b/cloud/POTrip.js index 088749f982..688c37259d 100644 --- a/cloud/POTrip.js +++ b/cloud/POTrip.js @@ -16,25 +16,25 @@ Parse.Cloud.afterSave("POTrip", function(request) { if (!trip.existed()) { Totals.getGlobalTotals(function(globalTotals) { Totals.updateTotals(trip, globalTotals); - globalTotals.save(); + globalTotals.save({},{ useMasterKey: true }); }); Totals.getDailyTotals(trip.get("startTime"), function(dailyTotals) { Totals.updateTotals(trip, dailyTotals); - dailyTotals.save(); + dailyTotals.save({},{ useMasterKey: true }); }); Totals.getMonthlyTotals(trip.get("startTime"), function(monthlyTotals) { Totals.updateTotals(trip, monthlyTotals); - monthlyTotals.save(); + monthlyTotals.save({},{ useMasterKey: true }); }); Totals.getUserTotals(trip.get("userId"), function(userTotals) { Totals.updateUserTotals(trip, userTotals); - userTotals.save(); + userTotals.save({},{ useMasterKey: true }); }); var userQuery = new Parse.Query("User"); - userQuery.get(trip.get("userId"), { - success: function(user) { + userQuery.get(trip.get("userId")).then( + function(user) { var community = user.get("community"); if (community != null) { CommunityTotals.getAllTimeTotals(community, function(globalTotals) { @@ -48,10 +48,10 @@ Parse.Cloud.afterSave("POTrip", function(request) { }); } }, - error: function(object, error) { + function(object, error) { console.error("Error when retrieving user to update stats " + error.code + " : " + error.message); } - }); + ); } }); @@ -92,7 +92,6 @@ function checkTripValidity(trip) { } Parse.Cloud.define("userAggregateData", function(request, response) { - Parse.Cloud.useMasterKey(); var counts = {}; counts.sumSMS = 0; @@ -108,8 +107,8 @@ Parse.Cloud.define("userAggregateData", function(request, response) { var query = new Parse.Query("UserTotals"); query.equalTo("user", user); - query.first({ - success: function(totals) { + query.first({ useMasterKey: true }).then( + function(totals) { counts.sumSMS = totals.get("missedSMSCount"); counts.sumCall = totals.get("missedCallCount"); counts.sumOther = totals.get("missedOtherCount"); @@ -118,10 +117,10 @@ Parse.Cloud.define("userAggregateData", function(request, response) { response.success(counts); }, - error: function(error) { + function(error) { //this is valid if the user doesn't have any trips console.log("Couldn't look up UserTotals for: "+request.params.userId); response.success(counts); } - }); + ); }); diff --git a/cloud/Promotion.js b/cloud/Promotion.js index e9e774c7af..f9cf5ce02f 100644 --- a/cloud/Promotion.js +++ b/cloud/Promotion.js @@ -15,11 +15,11 @@ Parse.Cloud.define("getPromotion", function(request, response) { promotionQuery.greaterThan("expiresAt", new Date()); promotionQuery.notEqualTo("viewedUsers", user); - promotionQuery.first({ - success: function(promotion) { + promotionQuery.first().then( + function(promotion) { if (promotion) { promotion.relation("viewedUsers").add(user); - promotion.save(); + promotion.save({},{ useMasterKey: true }); var promotionJSON = {"url" : promotion.get("url")}; response.success(promotionJSON); } else { @@ -27,9 +27,9 @@ Parse.Cloud.define("getPromotion", function(request, response) { response.success({}); } }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); response.error(); } - }); + ); }); diff --git a/cloud/PushNotifications.js b/cloud/PushNotifications.js index dc6c00dda7..3265a8707f 100644 --- a/cloud/PushNotifications.js +++ b/cloud/PushNotifications.js @@ -10,7 +10,6 @@ exports.sendAndroidPush = function(channels, action, data, callbackCounter) { pushData[key] = data[key]; }; } - sendPush(installationQuery, pushData, callbackCounter); } @@ -46,20 +45,14 @@ exports.sendIOSPush = function(channels, alert, contentAvailable, category, data } function sendPush(query, data, callbackCounter) { - var success = function() { - callbackCounter.success(); - } - - var error = function(error) { - callbackCounter.error(error); - } - Parse.Push.send({ where: query, data: data - }, - { - success: success, - error: error - }); + }, { useMasterKey: true }).then( + function(){ + callbackCounter.success(); + }, function(error) { // error + callbackCounter.error(error); + } + ); } diff --git a/cloud/Stats.js b/cloud/Stats.js index 2b79447896..adff2fe883 100644 --- a/cloud/Stats.js +++ b/cloud/Stats.js @@ -53,8 +53,6 @@ function sumArray(arr) { Parse.Cloud.define("stats", function(request, response) { - Parse.Cloud.useMasterKey(); - //number of queries that need to complete successfully to return success var queryCount = 4; var failureFlag = false; @@ -97,9 +95,8 @@ Parse.Cloud.define("stats", function(request, response) { }); var userQuery = new Parse.Query("POFriendRelation"); userQuery.equalTo("userId", user); - userQuery.find({ - success: function(friendRelations) { - + userQuery.find({ useMasterKey: true }).then( + function(friendRelations) { // success if (friendRelations && friendRelations.length > 0) { var friends = friendRelations.map(function(e) { return e.get("friendUser"); @@ -107,21 +104,19 @@ Parse.Cloud.define("stats", function(request, response) { var userTotalsQuery = new Parse.Query("UserTotals"); userTotalsQuery.containedIn("user", friends); - userTotalsQuery.find({ - success: function(userTotals) { - + userTotalsQuery.find({ useMasterKey: true }).then( + function(userTotals) { //success responseObj.friends = friendsStatsFromUserTotals(userTotals, dayIntervals, monthIntervals); - if (--queryCount == 0 && !failureFlag) { response.success(responseObj); } }, - error: function(error) { + function(error) { // error failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving monthly totals"); } - }); + ); } else { if (--queryCount == 0 && !failureFlag) { @@ -129,12 +124,12 @@ Parse.Cloud.define("stats", function(request, response) { } } }, - error: function(error) { + function(error) { // error failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error looking up friends"); } - }); + ); //global stats @@ -143,8 +138,8 @@ Parse.Cloud.define("stats", function(request, response) { monthlyQuery.lessThan("date",new Date()); monthlyQuery.addDescending("date"); monthlyQuery.limit(monthIntervals.length); //last 12 months of data - monthlyQuery.find({ - success: function(monthlyResults) { + monthlyQuery.find({ useMasterKey: true }).then( + function(monthlyResults) { responseObj.global.minutesDrivenMonths = []; responseObj.global.kmDrivenMonths = []; @@ -175,19 +170,19 @@ Parse.Cloud.define("stats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving monthly totals"); } - }); + ); var dailyQuery = new Parse.Query("DailyTotals"); dailyQuery.lessThan("date",new Date()); dailyQuery.addDescending("date"); dailyQuery.limit(dayIntervals.length); //last 7 days of data - dailyQuery.find({ - success: function(dailyResults) { + dailyQuery.find({ useMasterKey: true }).then( + function(dailyResults) { responseObj.global.minutesDrivenDays = []; responseObj.global.kmDrivenDays = []; for (var i = 0; i < dayIntervals.length; i++) { @@ -222,16 +217,16 @@ Parse.Cloud.define("stats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving daily totals"); } - }); + ); var globalQuery = new Parse.Query("GlobalTotals"); - globalQuery.first({ - success: function(results) { + globalQuery.first({ useMasterKey: true }).then( + function(results) { responseObj.global.missedMessages = results.get("missedSMSCount"); responseObj.global.missedCalls = results.get("missedCallCount"); responseObj.global.missedNotifications = results.get("missedOtherCount"); @@ -244,12 +239,12 @@ Parse.Cloud.define("stats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving global totals"); } - }); + ); }); function friendsStatsFromUserTotals(userTotals, dayIntervals, monthIntervals) { @@ -374,8 +369,8 @@ Parse.Cloud.define("communityStats", function(request, response) { monthlyQuery.lessThan("date", new Date()); monthlyQuery.addDescending("date"); monthlyQuery.limit(monthIntervals.length); //last 12 months of data - monthlyQuery.find({ - success: function(monthlyResults) { + monthlyQuery.find().then( + function(monthlyResults) { responseObj.minutesDrivenMonths = []; responseObj.kmDrivenMonths = []; @@ -406,20 +401,20 @@ Parse.Cloud.define("communityStats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving monthly totals"); } - }); + ); var dailyQuery = new Parse.Query("CommunityDailyTotals"); dailyQuery.equalTo("community", community); dailyQuery.lessThan("date",new Date()); dailyQuery.addDescending("date"); dailyQuery.limit(dayIntervals.length); //last 7 days of data - dailyQuery.find({ - success: function(dailyResults) { + dailyQuery.find({ useMasterKey: true }).then( + function(dailyResults) { responseObj.minutesDrivenDays = []; responseObj.kmDrivenDays = []; for (var i = 0; i < dayIntervals.length; i++) { @@ -454,17 +449,17 @@ Parse.Cloud.define("communityStats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving daily totals"); } - }); + ); var allTimeQuery = new Parse.Query("CommunityAllTimeTotals"); allTimeQuery.equalTo("community", community); - allTimeQuery.first({ - success: function(results) { + allTimeQuery.first({ useMasterKey: true }).then( + function(results) { responseObj.missedMessages = results.get("missedSMSCount"); responseObj.missedCalls = results.get("missedCallCount"); responseObj.missedNotifications = results.get("missedOtherCount"); @@ -477,12 +472,12 @@ Parse.Cloud.define("communityStats", function(request, response) { response.success(responseObj); } }, - error: function(error) { + function(error) { failureFlag = true; console.error("Got an error " + error.code + " : " + error.message); response.error("Error retrieving all time totals"); } - }); + ); }); Parse.Cloud.job("statForwardJob", function(request, status) { @@ -490,8 +485,8 @@ Parse.Cloud.job("statForwardJob", function(request, status) { // add rows for community totals and normal totals var communityQuery = new Parse.Query("Community"); - communityQuery.find({ - success: function(results) { + communityQuery.find({ useMasterKey: true }).then( + function(results) { var callCount = 2; callCount += results.length * 3; @@ -541,8 +536,8 @@ Parse.Cloud.job("statForwardJob", function(request, status) { }); }; }, - error: function(error) { + function(error) { status.error("error when loading communities to create total rows"); } - }); + ); }); diff --git a/cloud/Totals.js b/cloud/Totals.js index 86c641a733..1758aa9573 100644 --- a/cloud/Totals.js +++ b/cloud/Totals.js @@ -4,8 +4,8 @@ exports.getGlobalTotals = function(callback) { //retrieve the globalTotals object, create with defaults if necessary var GlobalTotals = Parse.Object.extend("GlobalTotals"); var totalQuery = new Parse.Query(GlobalTotals); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { var globalTotals = object; if (!globalTotals) { console.log("GlobalTotals not found, creating"); @@ -17,14 +17,14 @@ exports.getGlobalTotals = function(callback) { globalTotals.set("missedSMSCount", 0); globalTotals.set("missedCallCount", 0); globalTotals.set("missedOtherCount", 0); - globalTotals.save(); + globalTotals.save({},{ useMasterKey: true }); } callback(globalTotals); }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); } - }); + ); } exports.getDailyTotals = function(date, callback, failCallback) { @@ -34,8 +34,8 @@ exports.getDailyTotals = function(date, callback, failCallback) { var queryDate = new Date(date); queryDate.setHours(0, 0, 0, 0); //only year, month, day remain totalQuery.equalTo("date", queryDate); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { var dailyTotals = object; if (!dailyTotals) { console.log("DailyTotals not found, creating"); @@ -48,18 +48,18 @@ exports.getDailyTotals = function(date, callback, failCallback) { dailyTotals.set("missedSMSCount", 0); dailyTotals.set("missedCallCount", 0); dailyTotals.set("missedOtherCount", 0); - dailyTotals.save(); + dailyTotals.save({},{ useMasterKey: true }); } callback(dailyTotals); }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); if (failCallback) { failCallback(error); } } - }); + ); } exports.getMonthlyTotals = function(date, callback, failCallback) { @@ -70,8 +70,8 @@ exports.getMonthlyTotals = function(date, callback, failCallback) { queryDate.setHours(0, 0, 0, 0); queryDate.setDate(1); //only year, month remain totalQuery.equalTo("date", queryDate); - totalQuery.first({ - success: function(object) { + totalQuery.first().then( + function(object) { var monthlyTotals = object; if (!monthlyTotals) { console.log("MonthlyTotals not found, creating"); @@ -84,18 +84,18 @@ exports.getMonthlyTotals = function(date, callback, failCallback) { monthlyTotals.set("missedSMSCount", 0); monthlyTotals.set("missedCallCount", 0); monthlyTotals.set("missedOtherCount", 0); - monthlyTotals.save(); + monthlyTotals.save({},{ useMasterKey: true }); } callback(monthlyTotals); }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); if (failCallback) { failCallback(error); } } - }); + ); } exports.getUserTotals = function(userId, callback, failCallback) { @@ -106,8 +106,8 @@ exports.getUserTotals = function(userId, callback, failCallback) { id: userId }); totalQuery.equalTo("user", user); - totalQuery.first({ - success: function(object) { + totalQuery.first({ useMasterKey: true }).then( + function(object) { var userTotals = object; if (!userTotals) { @@ -132,21 +132,21 @@ exports.getUserTotals = function(userId, callback, failCallback) { userTotals.set("distanceTravelled", 0); userTotals.set("minutesTravelled", 0); userTotals.set("user", user); - userTotals.save(); + userTotals.save({},{ useMasterKey: true }); // console.log("UserTotals creating:"+JSON.stringify(userTotals)); } callback(userTotals); }, - error: function(error) { + function(error) { console.error("Got an error " + error.code + " : " + error.message); if (failCallback) { failCallback(error); } } - }); + ); } exports.updateTotals = function(trip, totals) { diff --git a/cloud/User.js b/cloud/User.js index 61b4332e63..76b24fe19f 100644 --- a/cloud/User.js +++ b/cloud/User.js @@ -77,30 +77,30 @@ Parse.Cloud.afterSave(Parse.User, function(request) { console.log("added new user"); Totals.getDailyTotals(new Date(), function(dailyTotals) { dailyTotals.increment("users"); - dailyTotals.save(); + dailyTotals.save({},{ useMasterKey: true }); }); Totals.getMonthlyTotals(new Date(), function(monthlyTotals) { monthlyTotals.increment("users"); - monthlyTotals.save(); + monthlyTotals.save({},{ useMasterKey: true }); }); Totals.getGlobalTotals(function(globalTotals) { globalTotals.increment("users"); - globalTotals.save(); + globalTotals.save({},{ useMasterKey: true }); }); //https://www.parse.com/questions/errors-when-trying-to-set-acls-in-user-beforesave var roleACL = new Parse.ACL(); roleACL.setPublicReadAccess(true); var role = new Parse.Role("user-" + user.id, roleACL); - role.save(); + role.save({},{ useMasterKey: true }); var userACL = new Parse.ACL(user); userACL.setRoleReadAccess("user-" + user.id, true); user.setACL(userACL); user.addUnique("channels", "user-" + user.id); - user.save(); + user.save({},{ useMasterKey: true }); } var dirtyKeys = request.object.get("dirtyKeys"); @@ -108,8 +108,8 @@ Parse.Cloud.afterSave(Parse.User, function(request) { if (dirtyKeys.indexOf("channels") > -1) { var installationQuery = new Parse.Query(Parse.Installation); installationQuery.equalTo("user", user); - installationQuery.find({ - success: function(installations) { + installationQuery.find({ useMasterKey: true }).then( + function(installations) { if (installations) { for (var i = 0; i < installations.length; i++) { var installation = installations[i]; @@ -120,11 +120,10 @@ Parse.Cloud.afterSave(Parse.User, function(request) { } } }, - error: function(error) { + function(error) { console.error("Unable to find installation " + error.code + " : " + error.message); - }, - useMasterKey:true - }); + } + ); } for (var i in dirtyKeys) { @@ -151,7 +150,7 @@ Parse.Cloud.afterSave(Parse.User, function(request) { //find the current public user var query = new Parse.Query(POPublicUser); query.equalTo("user", request.object); - query.first().then(function(object) { + query.first({ useMasterKey: true }).then(function(object) { //save public user with hashed phone number for search var publicUser = object; @@ -174,15 +173,15 @@ Parse.Cloud.define("verifyPhoneShortCode", function(request, response) { }); console.log("Saw " + request.params.userId + ". ShortCode: " + request.params.phoneShortCode); - userPointer.fetch({ - success: function(user) { + userPointer.fetch({useMasterKey:true}).then( + function(user) { var serverShortCode = user.get("phoneShortCode"); if (serverShortCode == request.params.phoneShortCode) { user.set("phoneNumberVerified", true); - user.save(null, { - success: function() { + user.save({},{ useMasterKey: true }).then( + function() { //load sha256 library var jssha = require('./jssha256.js'); @@ -198,7 +197,7 @@ Parse.Cloud.define("verifyPhoneShortCode", function(request, response) { var queryPublicUsersWithPhone = new Parse.Query(POPublicUser); queryPublicUsersWithPhone.equalTo("phoneNumberHashed", hashedPhoneNumber); - queryPublicUsersWithPhone.find(function(publicUsers) { + queryPublicUsersWithPhone.find({useMasterKey:true}).then(function(publicUsers) { //null out any existing users with this phone number for (var i = 0; i < publicUsers.length; i++) { @@ -228,7 +227,7 @@ Parse.Cloud.define("verifyPhoneShortCode", function(request, response) { //find the current public user var query = new Parse.Query(POPublicUser); query.equalTo("user", userPointer); - return query.first(); + return query.first({useMasterKey:true}); }).then(function(object) { @@ -239,7 +238,7 @@ Parse.Cloud.define("verifyPhoneShortCode", function(request, response) { publicUser.set("user", userPointer); } publicUser.set("phoneNumberHashed", hashedPhoneNumber); - return publicUser.save(null, {useMasterKey:true}); + return publicUser.save({}, {useMasterKey:true}); }).then(function() { @@ -251,61 +250,57 @@ Parse.Cloud.define("verifyPhoneShortCode", function(request, response) { response.error("Error saving phone number"); }); - }, - error: function(myObject, error) { + function(myObject, error) { console.error("Error when setting phoneNumberVerified " + error.code + " : " + error.message); response.error("Error when setting phoneNumberVerified"); } - }); + ); } else { console.error("Invalid shortcode sent for " + request.params.userId + ". Expecting: " + serverShortCode + " Recieved: " + request.params.phoneShortCode); response.error("Invalid shortcode sent"); } }, - error: function(myObject, error) { + function(myObject, error) { console.error("Unable to find user to verify " + error.code + " : " + error.message); response.error("Unable to find user to verify"); } - }); + ); }); Parse.Cloud.define("sendPhoneShortCode", function(request, response) { - Parse.Cloud.useMasterKey(); - console.log("user id: " + request.params.userId); var userPointer = new Parse.User({ id: request.params.userId }); - userPointer.fetch({ - success: function(user) { + userPointer.fetch({ useMasterKey: true }).then( + function(user) { console.log("user: " + user); var phoneShortCode = Math.floor((Math.random() * 900000) + 100000); user.set("phoneShortCode", phoneShortCode); - user.save(); + user.save({},{ useMasterKey: true }); var formattedPhoneShortCode = phoneShortCode.toString() formattedPhoneShortCode = formattedPhoneShortCode.slice(0, 3) + " " + formattedPhoneShortCode.slice(3); - //TODO send shortcode from twilio - twilio.sendSMS({ + twilio.sendMessage({ From: "+15873170710", To: user.get("phoneNumber"), Body: "Thanks for using OneTap! Your code is " + formattedPhoneShortCode - }, { - success: function(httpResponse) { + }).then( + function(httpResponse) { response.success(); }, - error: function(httpResponse) { + function(httpResponse) { response.error("unable to send shortcode from twilio: " + httpResponse); } - }); + ); }, - error: function(myObject, error) { + function(myObject, error) { console.error("Unable to find user to send shortcode " + error.code + " : " + error.message); response.error("Unable to find user to verify"); } - }); + ); }); diff --git a/cloud/main.js b/cloud/main.js index aff3726e9a..a8ea77bcc0 100644 --- a/cloud/main.js +++ b/cloud/main.js @@ -11,3 +11,7 @@ require('./Community.js'); require('./User.js'); require('./Installation.js'); +Parse.Cloud.define('hello', function(req, res) { + res.success('Hi'); +}); + diff --git a/index.js b/index.js index 3e34f10935..d6f1ca2898 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ // compatible API routes. var express = require('express'); -var morgan = require('morgan'); +// var morgan = require('morgan'); var ParseServer = require('parse-server').ParseServer; var path = require('path'); @@ -16,10 +16,25 @@ var api = new ParseServer({ databaseURI: databaseUri || 'mongodb://localhost:27017/dev', cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js', appId: process.env.APP_ID || 'myAppId', + verbose: process.env.VERBOSE || false, masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret! serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed liveQuery: { classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions + }, + push: { + android: { + senderId: process.env.FIREBASE_SENDER_ID || '', // The Sender ID of GCM + apiKey: process.env.FIREBASE_SERVER_KEY || '' // The Server API Key of GCM + } + }, + emailAdapter: { + module: 'parse-server-simple-mailgun-adapter', + options: { + fromAddress: process.env.MAILGUN_FROM_ADDRESS || '', // The address that your emails come from + domain: process.env.MAILGUN_DOMAIN || '',// Your domain from mailgun.com + apiKey: process.env.MAILGUN_API_KEY || '' // Your API key from mailgun.com + } } }); // Client-keys like the javascript key or the .NET key are not necessary with parse-server @@ -27,7 +42,7 @@ var api = new ParseServer({ // javascriptKey, restAPIKey, dotNetKey, clientKey var app = express(); -app.use(morgan('combined')); +// app.use(morgan('combined')); // Serve static assets from the /public folder app.use('/public', express.static(path.join(__dirname, '/public'))); diff --git a/scripts/parse_rest/cloudcode/cloud/main.js b/scripts/parse_rest/cloudcode/cloud/main.js index 1c5f7b48c3..878383e294 100644 --- a/scripts/parse_rest/cloudcode/cloud/main.js +++ b/scripts/parse_rest/cloudcode/cloud/main.js @@ -9,16 +9,16 @@ Parse.Cloud.define("hello", function(request, response) { Parse.Cloud.define("averageStars", function(request, response) { var query = new Parse.Query("Review"); query.equalTo("movie", request.params.movie); - query.find({ - success: function(results) { + query.find({ useMasterKey: true }).then( + function(results) { var sum = 0; for (var i = 0; i < results.length; ++i) { sum += results[i].get("stars"); } response.success(sum / results.length); }, - error: function() { + function() { response.error("movie lookup failed"); } - }); + ); });