Friend list through relations - swift

I'm creating a social media app. I'm able to allow the current user to search for a PFUser and add the user to a friendship relation. I'm struggling on accessing the friendship relation and getting all the friends to create a table view right now. Could someone help me with this?

A Relation type in Parse represents just like what it literal meaning is. It's just a relation that contains no data. If you want to access the data inside the relation. You will need to perform query on it like so:
let query = relation.query() // I assume relation is an instance of PFRelation you want
Actually, Parse tutorial provides us a very comprehensive guide and you should check that first: https://parse.com/docs/ios/guide#relations

NOTE: This answer provides alternatives to using Relations to make a Friend System
I have created a friend system in two ways using Parse and both a function of your specific needs.
The first time I implemented a friend system. I had a table of Users and a table of Relationships. The Relationships table stored the usernames (or ObjectIds) of the two users in a relationship and the state of that relationship (friends, request sent, etc). The problem with this is that the queries can be kinda complicated, and if you have a lot of users, this may end up being too slow.
The second option is storing friend information in the User table itself. For each user, you add the columns with they type Array: Friends, RequestSent, and RequestReceived. Anytime a user sends a request they update their own user row and send a message to CloudCode to update the other affected user. Take a look at this example:
User A sends a request to User B:
User A adds user B's name to RequestSent
User A sends a message to cloud code that he/she wants to add user B
CloudCode adds User A's name to User B's RequestReceived
User B wants to accept User A's request
User B adds user A's name to Friends
User B removes user A's name from RequestSent
User B sends a message to CloudCode that he/she wants to accept User A's friend Request
CloudCode adds user A's name to Friends
CloudCode removes user A's name from RequestReceived
With this option, you never perform any server side queries. You only ever perform get operations. The downside to this option is if the logged-in user has thousands of friends/requests, it will take a while to download that information.
Note: The reason you have to use CloudCode is that a User can only change information about him/herself. The other option is to have CloudCode manage all the adding/removing so better checks can be made.
I found with this method that you can sometimes have one user who is listed a a friend in another users row but not their own. Controlling everything from CloudCode could eliminate this kind of error.

Related

LoopBack Relational Mapping : Events and Invitations

I'm trying to model a simple event application.
Basically:
A User can create an Event
A User can invite many Users to an Event
A User can accept/decline an Invitation
A User can retrieve an Event if he's the owner or is participating (accepted Invitation). The difference should somehow be visible in the returned objects
A User can retrieve Event's current accepted/declined Invitations (actually Users, not foreign keys)
A User can remove himself from an Event
To many things to think about so it's all mixed up in my head and I'm losing the whole picture. I don't fully understand how I should implement accept/decline Invitation and how I should retrieve information about current participants. What are the relations behind?
I think I know how to do it with references only, but my front-end is mobile so I don't want to make a lot of requests to get every object by reference after the first fetch...
If someone could lighten me up...
Thanks :)
You should have an invitation model and an event model with the following fields.
Invitation : eventId, userInvited, accepted
An invitation belongs to an event and belongs to a user
Event : your event details, event owner
Then for a user you can do a remote method that gets the events with
Invitation.find({where: {userInvited: user.id}}, include: 'event')
.then((event) => event.id)
And with the event, gets all the users
.then((eventId) => Invitation.find({where: {eventId}}, include: 'user'))
.then((invitations) => invitations.map((invitation) => invitation.toJSON().user))
It's a quick draft of how to achieve what you want.

Problems of inviting friends through Facebook email

I want my users to invite their friends by sending them Facebook private massage. I am thinking of using send button, but the problem is that each invitation url is different and unique, so if I use send button to do it, I might need to create many send buttons each of which carries an unique href. I think this should work? But ideally, I want users to just select their friends in a multi friend selector and everything's done by just clicking the sending button. Any ideas?
Thanks for any help!
You can generate a unique URL for the message that includes the IDs for all the users the user wants to send the message to. Then, when the recipient accesses the URL and authenticates themselves, you can cross reference the URL and their User ID to what you have in your database.
Without knowing what the message contains, its purpose and what you want to achieve, this is the best approach I can think of.
You won't be able to tell who the message is actually sent to, as Facebook doesn't return the User IDs in the callback, but if you have read_inbox permissions, you should be able to look the User IDs up that way.
i think an better way to approach this is set up an invite system, have an invitation code field in your register.php that you sign into, store that info in user account database and set an number of times it can be used

How to share data between iPhones?

This seems like a simple problem, but the more I plan it out, the harder it seems. The basic idea is this:
Person A wants to give person B permission to see his/her data on person B's device. Person A uses either person B's phone #, email, or Facebook info to send a request to person B. A has a unique ID for their device, but A does not know B's ID before the request. B might not have the app a well thus no account or ID exists.
What would be the best method for this handshake using phone #, email, or Facebook with a JSON, schemaless backend? I do have support for notifications, I'm just not sure how to properly and securely make the transfer so person A can have person B's ID to always send data to them in the future.
There are just so many factors, I'm having trouble wrapping my head around it.
It was pretty rough to be honest. I used Parse as my backend and hooked up my users using Facebook & email. I had an intermediate table for sharing. Each user had an array of user IDs that they were sharing with. I enabled push notifications so that when you shared with another person, they got a notification and an option to return sharing. Thus, each user had an array of people they wanted to share with. The sticky part was the handshake which I still don't like how I did. I basically check if each user has the other's user ID in their sharing array. In my opinion, its very sub optimal, but it works for now.

Core Data convert a superclass instance into a subclass instance?

What's the best way to programmatically convert an NSManagedObject-subclass (User) instance into an instance of its subclass (AccountUser)?
Setup
AccountUser inherits from User : NSManagedObject
When I sign up or log into the app for the first time, I become an AccountUser. Then, I download all of my friends and store them as User objects.
Both User & AccountUser have attributes firstName, lastName, etc. AccountUser has some extra things, like accessToken.
Problem
My friend John logs in on my device. Since he's my friend, he's already stored as a User. But now, I want to convert him into an AccountUser. What's the best way to do this programmatically? I have lots of attributes and relationships to preserve, so creating a new AccountUser object from a User object and then deleting the original User object is a lot to do. If I just create an AccountUser without deleting the User, things get messy. E.g. when I fetch User by ID, I get two objects back: one is the AccountUser, the other is the User.
One way to do it is to simply create a new AccountUser object and copy the relevant fields from the existing User object into it. Then deal with the old User object appropriately -- delete it, save it, whatever makes sense. It'd make sense to give AccountUser an initializer that takes an instance of User, like -initWithUser:.
Another (probably better) option would involve using a single class (User) for all users. If there's additional information to be stored for some users (currently AccountUsers), create a new Account class and associate an instance of Account with the users that require it. So the User objects that represent you and your friend John would each have associated Account objects, and all the other users wouldn't. When one of your other friends logs into your phone, you can create a new Account object for that person and associate it with their existing User object.
Maybe you can do it this way
Add the User entity a boolean property called "is_account_user".
Set a relationship between User and account user.
if a user has an account user, create the AcountUser entity and relate it to the User.
If the user has no account info then the "is_account_user" will be NO;
When you fetch for a user you can check if is_account_user is YES, and if so you can get his AcountUser with the relationship.
Hope it helps

Creating Facebook test user friendship returns "(#532) You can't add this user as a friend"

I am writing a script to create test data for my application. The script first creates a collection of test users, and then assigns friendships between them.
I create a friendship between two test users with
https://graph.facebook.com/TEST_USER_1_ID/friends/TEST_USER_2_ID?method=post&access_token=TEST_USER_1_ACCESS_TOKEN
However for random users, any friend request involving the user has the response
{"error":{"type":"OAuthException","message":"(#532) You can't add this user as a friend"}}
I can't find anything about this error message.
That's because a normal user can't be friends with a test user. The test user is visible only within the scope of an application, not outside of it.