Let user turn off different notification types using parse.com's cloud code - ios

First time posting on here so bear with me. I am taking a dive into parse.com's cloud code without any knowledge of javascript so I could use a little help.
I got an afterSave push notification working with Cloud Code but I need a way to allow users to subscribe/unsubscribe from different types of notifications. The way I am currently attempting to do this is by storing bool values in the parse user table for the different types of notifications but I'm having trouble getting that value in cloud code. Here is my cloud code:
Parse.Cloud.afterSave("Comment", function(request){
var comment = request.object;
var fromUser = request.user;
var onIdea = comment.get("ideaPointer");
var ideaOwner = onIdea.get("owner");
var getNotification = ideaOwner.get("getCommentNotifications");
var message = fromUser.getUsername() + " commented on your idea.";
if (getNotification){
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", ideaOwner);
Parse.Push.send({
where: pushQuery,
data: {
alert: message,
ideaId: onIdea.id
}
});
}
});
Here is the error that gets printed to the logs when a comment is saved:
Result: TypeError: Cannot call method 'get' of undefined at main.js:6:34
Here is the line it is having a problem with because it was working before I added it along with the if statement:
var getNotification = ideaOwner.get("getCommentNotifications");
getCommentNotifications is the bool value in the user table.
I'm also not sure if my if statement is written correctly or not:
if (getNotification){}
I have also verified that getCommentNotifications value for the ideaOwner I'm testing on isn't empty.
Any help with this issue or ideas on a better way to allow users to subscribe/unsubscribe from different notifications types would be much appreciated.

The other ends of the those pointers must be fetched. If they really are pointers, then you can treat them as incompletely initialized objects, so...
Parse.Cloud.afterSave("Comment", function(request){
var comment = request.object;
var fromUser = request.user;
var onIdea = comment.get("ideaPointer");
onIdea.fetch().then(function(onIdeaObject) {
var ideaOwner = onIdea.get("owner");
return ideaOwner.fetch();
}).then(function(ideaOwnerObject) {
var getNotification = ideaOwnerObject.get("getCommentNotifications");
if (getNotification) {
var message = fromUser.getUsername() + " commented on your idea.";
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", ideaOwnerObject);
return Parse.Push.send({ where: pushQuery, data: { alert: message, ideaId: onIdea.id } });
}
});
});

Related

Stripe IOS SDK not matching AllResponseFields?

I'm trying to do a simple count check for a customer to see if they need to add a source before moving onto another page. However, my calls to the STPCustomer object's attributes are not matching the information for the full response.
I've tried clearing the customer cache and reloading the customer, but uh... still no match.
Here's a truncated like, essentials code. The class has the STPPaymentContextDelegate attached.
private var customerContext = STPCustomerContext?
private var paymentContext = STPPaymentContext?
func setupStripe() {
self.paymentContext = STPPaymentContext(customerContext: self.customerContext!)
self.paymentContext?.hostViewController = self
self.paymentContext?.delegate = self
self.paymentView = STPPaymentMethodsViewController(configuration: STPPaymentConfiguration.shared(), theme: STPTheme.default(), customerContext: self.customerContext!, delegate: self)
}
func getCustomerSources() {
if let customer = customerContext.retrieveCustomer({ (customer, error) in
if customer != nil {
print(customer.sources.count)
print(customer.sources)
print(customer.allResponseFields)
}
})
}
When I run getCustomerSources() on the test customer, I am expecting:
1
[ba_1Dvf46LrBVaGM6Sq9qIYhOlJ]
AnyHashable("id"): cus_number, AnyHashable("email"): rosa_diaz#gmail.com, AnyHashable("default_source"): ba_1Dvf46LrBVaGM6Sq9qIYhOlJ, AnyHashable("created"): 1548220659, AnyHashable("description"): LiG8WbVhT8SVhta8LdfUuWzOwQn2, AnyHashable("livemode"): 0, AnyHashable("object"): customer, AnyHashable("sources"): {
data = (
{
"bank_name" = "STRIPE TEST BANK";
country = US;
currency = usd;
customer = "cus_EOQzwwGPjzopjS";
fingerprint = 1AQMB9nzeGSGXHst;
id = "ba_1Dvf46LrBVaGM6Sq9qIYhOlJ";
last4 = 6789;
metadata = {
};
object = "bank_account";
"routing_number" = 110000000;
status = verified;
}
);
"has_more" = 0;
object = list;
"total_count" = 1;
url = "/v1/customers/cus_EOQzwwGPjzopjS/sources";
}])
However, instead of my 1 and ba_1Dvf46LrBVaGM6Sq9qIYhOlJ, I am getting 0 and []. But the allResponseFields section is the same.
Any ideas as to why there's a discrepancy between what the STPCustomer object attributes are returning and what the actual response is telling me?
it's hard to say what's going on without knowing more about the error parameter that your callback receives. It sounds like there might be an error parsing the API response, and printing the error passed into your retrieveCustomer block should give you a better idea about what's going on. If the error persists I'd recommend contacting support#stripe.com with details, as they can help directly with iOS app questions!

Passing data to ActionCable disconnect method

Is there a way I can pass data to the disconnect method of my client-side ActionCable channel? I need the ID of the current user so that I can turn off their online status indicator. I've tried and read everything I can find on ActionCable, but I don't understand how to pass data to methods other than received.
Live.appearance = Live.cable.subscriptions.create('AppearanceChannel', {
// need to pass the user's ID here:
disconnected: function(data) {
var userId = data['appearances'][0];
var appearanceContainer = $('.user-status-indicator').filter('#' + userId);
appearanceContainer.remove();
},
received: function(data) {
var userId = data['appearances'][0];
var appearanceContainer = $('.appearance-label').filter('#' + userId);
appearanceContainer.append('<div class="user-status-indicator" id="${userId}"></div>');
}
});

console.log not showing in Parse Logs page

In the below cloud code i would like to get a feedback of the saveAll function but after calling the code from my client in the parse Logs page i can only see:
I2014-10-08T15:28:32.930Z] v249: Ran cloud function acceptMeetingBis for user dyGu143Xho with:
Input: {"meetingId":"bUSTGNhOer"}
Result: Meeting accepted
Here is my cloud code:
Parse.Cloud.define("acceptMeetingBis", function(request, response) {
var userAcceptingTheMeeting = request.user;
var meetingId = request.params.meetingId;
var changedObjects = [];
var queryForMeeting = new Parse.Query("MeetingObject");
queryForMeeting.equalTo("objectId", meetingId);
queryForMeeting.first({
success: function(meeting) {
var userCreatorOfMeeting = meeting.get("user");
userAcceptingTheMeeting.increment("acceptedMeetings", +1);
changedObjects.push(userAcceptingTheMeeting);
meeting.add("participantsObjectId", userAcceptingTheMeeting.id);
if (meeting.get("participantsObjectId").length === meeting.get("meetingNumberOfPersons")) {
meeting.set("isAvailable", false);
}
changedObjects.push(meeting);
Parse.Object.saveAll(changedObjects, {
success: function(objects) {
console.log("Successfully saved objects"); //this line doesn't show up
response.success("objects saved");
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error(error);
}
});
//future query and push notifications will go here
response.success("Meeting accepted");
},
error: function() {
response.error("Failed to accept the meeting");
}
});
});
I will also need to add some push and another nested query after the saveAll() but before doing/trying that i would like to know if this is the right method to use or if i have to build the code in a different way. I'm new to javascript and honestly i'm struggling to understand some concepts, like promises. Any help would be much appreciated.
Your call to
Parse.Object.saveAll
is asynchronous, and you call
response.success("Meeting accepted")
immediately after making the asynchronous call, which ends the cloud code running of the method. If you simply replace the
response.success("objects saved")
with
response.success("Meeting accepted")
you should get what you want.
I didn't see the rest of your question about promises. You should check out Parse's documentation on chaining promises, which is what you want here.
Essentially, here's what you'll want to do:
Parse.Cloud.define("acceptMeetingBis", function(request, response) {
var userAcceptingTheMeeting = request.user;
var meetingId = request.params.meetingId;
var changedObjects = [];
var meetingToAccept;
var queryForMeeting = new Parse.Query("MeetingObject");
queryForMeeting.get(meetingId).then(function(meeting) {
meetingToAccept = meeting;
var userCreatorOfMeeting = meeting.get("user");
userAcceptingTheMeeting.increment("acceptedMeetings", +1);
return userAcceptingTheMeeting.save();
}).then(function(userWhoAcceptedMeetingNowSaved) {
meetingToAccept.add("participantsObjectId", userWhoAcceptedMeetingNowSaved.id);
if (meetingToAccept.get("participantsObjectId").length === meetingToAccept.get("meetingNumberOfPersons")) {
meetingToAccept.set("isAvailable", false);
}
return meetingToAccept.save();
}).then(function(savedMeeting) {
response.success("Meeting accepted");
}, function(error) {
response.error("Failed to accept the meeting");
});
});
For each asynchronous action you want to do, perform it at the end of one of the .then functions and return the result (it returns a promise). Keep adding .then functions until you're done all the work you want to do, at which point call response.success.

SAPUI5 get username/userid from OData request

I am currently focussing a problem which I thought it would be easy to solve. but I didnt. There are controls which allow us to show the username or logged in user, such as the lovely shell-headitems:
var oShell = new sap.ui.ux3.Shell("myShell", {
headerItems: [
new sap.ui.commons.TextView({
text: oController.getUserName() }),
],
});
It looks like this:
In here we define headerItems, which are in my opinion foreseen to show a username / or the currently logged in user. but how can I receive it? my idea is to get it from the odata request, which was made earlier. It requires me to enter username and password -> thus I want to read this username in my controller-method, but how?
getUserName : function() {
// return navigator.userAgent;
var model = sap.ui.getCore().getModel();
return model.getProperty('sUser'); // doesnt work :(
},
I also tried to get it from navigator.userAgent() but this information does not belong to the user.
Anybody knows how to receive it?
And yes: I searched in google and found some threads discussing about users/login but none of these threads solved my issue. Otherwise I thought about transferring sy-uname from SAP to the frontend, but how could you send a single Text? I don't want to build a complete service for this single transaction.
If you do not provide sUser and sPassword during oData-Model-Initialization it will be empty during runtime. You cannot access it from the model, though I realized an own service for this.
The username is in the sap.ui.model.odata.ODataMetadata of ODataModel.
var getUserName = function() {
var model = sap.ui.getCore().getModel();
var sUser = model.oMetadata.sUser;
// Display user logic here.
};
oModel.attachMetadataLoaded(null,getUserName);
Update answer for comment question from zyrex:
var user = new sap.ui.commons.TextView();
var getUserNameCallBack = function(userName) {
user.setText(userName);
}
oController.getUserName(getUserNameCallBack);
Controller method:
getUserName: function(callback) {
var userName = '';
$.getJSON(sServiceUrl).done(function(data) {
userName = data.d.Name;
callback(userName);
});
}

How to add campaign tracking data to any url automatically?

I get a bunch of different URL from my sources and what I would like is to redirect to the same URL, but with campaign data added to URL (to track the referred clicks).
For example I have these URLs:
www.example.com/category/product/name.html
www.example.com/id_product=5
I want to add at the end the following: utm_source=SOURCE&utm_medium=MEDIUM&utm_campaign=CAMPAIGN
And the URLs to become
www.example.com/category/product/name.html?utm_source=SOURCE&utm_medium=MEDIUM&utm_campaign=CAMPAIGN
www.example.com/id_product=5&utm_source=SOURCE&utm_medium=MEDIUM&utm_campaign=CAMPAIGN
How to I correctly check and cover all the cases if a URL string has parameters, and add mine?
I want to do it in node.js
Thank you
Elaborating on #snkashis, a similar but arguably more elegant solution, again using node's url module, is:
var addQueryParams = function (cleanUrl) {
var obj = url.parse(cleanUrl, true, false);
obj.query['utm_source'] = 'SOURCE';
obj.query['utm_medium'] = 'MEDIUM';
obj.query['utm_campaign'] = 'CAMPAIGN';
delete obj.search; // this makes format compose the search string out of the query object
var trackedUrl = url.format(obj);
return trackedUrl;
};
This works, because url.format first looks for search and, if it can't find it, it composes the query string from the query object
(taken from node url module documentation http://nodejs.org/api/url.html#url_url_format_urlobj )
search will be used in place of query
query (object; see querystring) will only be used if search is absent.
Here is a example showing different scenarios using Node's URL module.
var url = require('url');
var exurls = ["www.example.com/category/product/name.html","www.example.com/id_product=5?hasparam=yes"]
var to_append = "utm_source=SOURCE&utm_medium=MEDIUM&utm_campaign=CAMPAIGN";
for (i=0;i<exurls.length;i++) {
var parsedobj = url.parse(exurls[i],true,false);
//Below checks if param obj is empty.
if (Object.keys(parsedobj.query).length!==0) {
var newpath = parsedobj.href+"&"+to_append;
}
else {
var newpath = parsedobj.href+"?"+to_append;
}
console.log(newpath);
}
Connect will help you:
var connect = require('connect');
var app = connect();
app.use(connect.query());
app.use(function (req, res, next) {
console.log(req.query);
res.end(JSON.stringify(req.query));
});
app.listen(3000);

Resources