Android Parse.com getting the id of a user - android

I'm working in an android app adding the following/follower system, I'm using the lines in the Parse.com, I have a user profile, and when I click on the follow button the app send the both users id (the current user and the other user) in a row. I'm using pointers targeting the _user class.
This is my current code :
Intent i = get intent();
String userId = i.getStringExtra("userid");
ParseObject follow = new ParseObject("Follow");
follow.put("from",ParseUser.getCurrentUser());
follow.put("to", userId);
follow.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
btn_follow.setImageDrawable(getResources().getDrawable(R.drawable.ic_following));
}
});
The btn_follow change the image. which means the following has been done. but my Follow class is empty.
I have changed my Follow table from :
...|from|to|
To :
...|from|to|
This worked well the both users id were saved, but I need pointers not string so I put from and to to pointers again and deleted the line
follow.put("to", userId);
This worked well also but it only saved the current user and not the user I'm trying to follow.
So my problem is with the ID of the user I want to follow.
In the guide they are saying
"ParseUser otheruser = ..."
(I don't no what to put in the 3 points to get the other user id)

You need to assign a ParseObject (or ParseUser) to create a pointer in database. For this you need to create ParseUser without data, just with id like this:
follow.put("to", ParseObject.createWithoutData("_User", userId));
Also, you should check the ParseException e in callback to see whats going on.

Related

How to retrieve data from a child node in Android Firebase

I already have a onDataChange written to get values from a different child node,but what I need is to change the key to let's say "new" and then retrieve the data from that child
Any inputs would be appreciated ..
As far as i understand, you want to change a field and bring back the changed data.
for e.g, for this data structure, if you want the change Username, create a User modal with different name, User newUser = new User("https://...","newUserName). And change the field in User modal
public String Profile;
public String newKey;
Then give the new modal to that uid,
FirebaseDatabase.getInstance().getReference().child("Users").child("ECWHIksxJ0Q5SUIIrev4BjnjmrJ3").setValue(newUser, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
DatabaseReference yourRef = databaseReference.getRef();
}
});
Now you have the changed data's reference. Did you try something like this?
Update:
If you want to update multiple value/field with single action, you should create a map and put every path into it with values.
In firebase, every table, every specific field is an path. Consider the data structure shown above. If you want change Username field:
HashMap<String, Object> updateMap = new HashMap<>();
updateMap.put("/Users/ECWHIksxJ0Q5SUIIrev4BjnjmrJ3/Username", "newUserName");
For different user:
updateMap.put("/Users/DIFFERENT_USER_KEY/Username", "newUserName2");
For diffrent table:
updateMap.put("/YourOtherTable/DifferentPath/DifferentField", true);
Then get your database root reference and update all at once.
FirebaseDatabase.getInstance().getReference().updateChildren(updateMap).addOnCompleteListener(new OnCompleteListener...)
This is very simple way to update a lot of things in one action, but it's causing problems when writing firebase rules. Because you get your root reference and try to set a value to it. The choice is yours.

Cannot save a ParseUser that is not authenticated, updating an object with a relation with a ParseUser

I am workin in an Android Project using parse.com and I have a table Requirement which has a column called "user_assigned" which is a relation to ParseUser. This user is in charge to modify the requirement even if he didn't create the Requirement, but when this user try to update the requirement values, it returns "Cannot save a ParseUser that is not authenticated"
P.D. All the ACL in the requirement are public, write and read.
// setting write permission
ParseACL postACL = new ParseACL(ParseUser.getCurrentUser());
postACL.setPublicWriteAccess(true);
postACL.setPublicReadAccess(true);
requirement.setACL(postACL);
requirement.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
callback.onFinish((Exception) e, requirement);
}
I don't want to change the user assigned I want to change other fields but the way I get and change the relation from the table is
ParseRelation<ParseUser> relation = requirement.getRelation("user_assigned");
relation.add(requirement.getUserAssigned());
I found a solution,
What is happening: 3 tables, Project, User and Requirements where a Requirement has a pointer to the Project and the Project has another pointer to an User called projectOwner. The problem happen when another user different to the projectOwner read some values of the projectOwner associated with the Requirements. If the new user wants to save some changes in the Requirements parse return the error "Cannot save a ParseUser that is not authenticated".
To fix it, i only read the objectId of the projectOwner and after I got the rest of the attributes of the ParseUser calling
ParseUser.getQuery();
query.get(objectId)
In other words, in my class Project, i have a method to get the attributes. When I want to get the projectUser (pointer) i got it like this:
User uTemp = (User) this.getParseUser("owner");
if(uTemp != null){
// this.setOwner(uTemp.extractAttributes());
uTemp = uTemp.getObjectId();
uTemp = UserDAO.getUserByObjectId(uTemp.getObjectId());
this.setOwner(uTemp);
}
I could fixed thanks to this, I hope it can be helpful.

Parse: How to add object to the user, who is not not the current user

i have created my app, in which I have created several users. The challenge is, I need to construct the app in a way that, while I am logged in as a user (say User A), I can add input an object into another user (say User B). So, while am logged in as user A, I can input the data that will be saved into one of the empty field, of User B. Can you assist which way, I can arrange this? In short, how can I create an object to associate with the User B.
Here is the thing, while I am logged in as User A, I want to add "Scores" for both user A and B. for instance, if the user A's score is 7, and B's score is 20, I could add the scores while logged in the activity A, and they will be added in the field "Scores" of the corresponding users.
you should go for parse cloud code which is written in java script
refer this link to know more
https://parse.com/docs/js/guide#cloud-code
then you just edit main.js
deploy and call that cloud code from your code
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("updateTable", function(request, response) {
var currentUserId=request.params.currentLoggedinUser;
var userToBeUpdatedId=request.params.userToBeUpdated;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userToBeUpdatedId });
user.addUnique("followers",currentUserId);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
deploy this code using comand "parse deploy" from terminal.
call this code from your code like
ParseCloud.callFunction("updateTable", params);
i hope it will help.
The way that Parse is setup by default is so that only the current users data can be editted in the User table. You could probably change the settings for the table however, this could cause security issues.
What I would suggest would be to create a 'Scores' tables. There you could have a row for each user. with a 'userID' column which contained the objectId of the user to reference the user. You could then have a score column which you could update whenever you wanted.
Alternatively to storing the objectId of the user in the userID column you could make an association between the Scores row and the user. See the Parse documentation for how to create associations. https://parse.com/docs/android_guide#users-associations

Prevent duplicate entries on Parse.com?

I am building an android app with Facebook login, once logged, it'll save information from Facebook to Parse like Facebook ID, name and profile picture. Is there a way to make a column unique so that it can only have 1 kind of FB ID? or check if FB ID exist in my User table? I know it might seem like a very general request, but I've been googling for hours and can't find any specific solution..
I've already faced this problem in iOS, and seems that Android can be solved in the same way.
So, you can mix the FB API with the parse function
ParseFacebookUtils.logIn(String facebookId, String accessToken, Date expirationDate, LogInCallback callback)
relying to facebook app you can obtain the facebookId, accessToken ( i think that for the expirationDate you can set a far date, like what i've done for iOS )
Another solution is:
ParseFacebookUtils.logIn(Arrays.asList(Permissions.User.EMAIL),
this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
Do your stuff...
}
});
it should automatically detect if an user with that facebook id already exist in your _User table or not ( i can confirm this on iOS, you can check if the user is new or still exist with user.isNew() ). If you need you could also save the retrieved facebookId in a separated field, but i think you could avoid it for this scope
Hope it helps

Parse.Com Error saving relation

ParseRelation<ParseUser> relation = profile_user.getRelation(ParseConstants.KEY_LIKES);
relation.add(ParseUser.getCurrentUser());
profile_user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException arg0) {
Toast.makeText(ViewProfilePic.this, "Liked!", Toast.LENGTH_SHORT).show();
}
});
In this program when a person visits a particular profile he can like that profile.
profile_user is a ParseUser object which is a particular profile. If the user clicks on the like button the current user should be added to the profile_user 's "likes relation. But this does not happen . Please help!
I found the solution. By default parse does not allow you to save unauthenticated users for security reasons I.e only the current user value can be modified. I overcame this using parse cloud code.

Categories

Resources