Jump to content

Help on Facebook Friendship System in CodeIgniter


Kirasiris
 Share

Recommended Posts

I'm working with a relationship table:

id (AI), user_id, friend_id, status, type.

The current behavior is that the Logged In user can send friendship requests to X user but if there is already a friendship request coming from the X user to the Logged In user; the logged in user instead of sending a new request to X user, he/she should accept the one that is already in the database(which is coming from the X-user).

Here is an image of what I'm talking about(in the image, the status column displays 'accepted' but try to imagine it as it was 'pending', please) :

Z67Gt.gif

 

So I currently have a function for adding friends for the current Logged-In user and it goes like this:

    public function follow($username){

            // Check Login
            if(!$this->session->userdata('user_id')){
                // Redirect to page
                redirect('users/login');
            }
        
            // Get item
            $item = $this->User_model->get_username($username);

            // Already friends?
            $friendship = $this->User_model->the_relationship($item->id);
            
            if ($friendship->status == 'accepted' || $friendship->status == 'pending') {

                // Create Message
                $this->session->set_flashdata('error', 'You are already following this user.');
                
                // Redirect to pages
                redirect('users/profile/'.$item->username);

                // BEGINNING OF - HERE IS WHERE THE PROBLEM IS
                
            } elseif($friendship->status == 'pending'){

                // Get item
                $item = $this->User_model->get_username($username);

                // Page Data
                $data = array(
                    'user_id'   => $item->id,
                    'friend_id' => $this->session->userdata('user_id'),
                    'status'    => 'accepted',
                );

                $this->User_model->updateFriendship($item->id, $data);

                /// END OF - HERE IS WHERE THE PROBLEM IS

            } else {

            // Get item
            $item = $this->User_model->get_username($username);
            
            // Page Data
            $data = array(
                'user_id'       => $this->session->userdata('user_id'),
                'friend_id'     => $item->id,
                'status'        => 'pending',
				'type'			=> 'friendship',
            );

            $this->Relationship_model->add($data);

            // Activity Array
            $data = array(
                'resource_id' => $this->db->insert_id(),
                'type'        => 'friendship',
                'action'      => 'sent',
                'user_id'     => $this->session->userdata('user_id'),
                'message'     => '(' . $this->session->userdata('username') . ') sent a friend request to ('.$item->username.') ',
            );

            // Insert Activity
            $this->Activity_model->add($data);

            // Set Message
            $this->session->set_flashdata('success', 'Friend request has been sent');

            // Redirect
            redirect('users/profile/'.$item->username);

        }
        
    }

and here are the two functions in the model which check the current status of the relationship between the current logged in user and the X user; the second function should update the status of the friendship if there is already one:

LEGEND:

$this->relationship: ci_relationship table

$this->type: friendship type

 

    // Relationship for single
    public function the_relationship($id){	
		$this->db->select('*');
        $this->db->from($this->relationship);
        $this->db->where('user_id', $this->session->userdata('user_id'));
        $this->db->where('friend_id', $id);
        $this->db->where('type', $this->type);

		$query = $this->db->get();

		if($query->num_rows() >= 1){
			return $query->row();
		} else {
			return false;
		}
    }

    // Update Friendship Status
    public function updateFriendship($id, $data){
        $this->db->where('user_id', $id);
        $this->db->where('friend_id', $this->session->userdata('user_id'));
        //$this->db->where('status', 'pending');
        $this->db->where('type', $this->type);
        $this->db->update($this->relationship, $data);
    }

I hope I could make myself clear.

Thanks in advance.

Edited by Kirasiris
Link to comment
Share on other sites

What is your specific question?

Also, to clarify, "Following" and "Friendship" are 2 different concepts:

 

Following

---------------

1 User -> Many Followers

For example:

User A -> Followed by User B

User B -> (Not Followed by User A)

This is possible in a Following Model

 

Friendship

-----------------

User A -> Friend to B

User B -> Friend to A

You state that this is friendship problem, but your code implies it is a Follower model.  In a friendship model, you would need a different db structure OR when a friendship is established from A -> B, then you need to also create an associated relationship from B -> A.  I don't see you doing that anywhere.  

This is important because, in a follower model, as illustrated, I might follow someone who does not follow me back.  With your table structure, that is implied.   

This is relevant to what I think your question concerns, which is recognizing that UserA has requested to follow UserB, but UserB already requested to Follow UserA.  

If that is your question, then the solution is to:

  • Create a New follow Request from User A -> UserB and set it as "accepted".
  • Set the follow Request from User B -> UserA = "accepted"
Link to comment
Share on other sites

Yes, that's the actual problem. I'm trying to create a system in which both users should acknowledge the friendship between them but so far I can not figure out the proper way to implement it.

I know it may be a bit complex but should it not be possible by just making a X user update an specific row without having to create another relationship? I mean your suggested task (the second one; "set the follow request from User B -> User A as accepted") requires me to create a function to set the request from User B to accepted and that should work as I want.... but it is not what I just tried to create with the code in my post? I mean it verifies if there's a relationship coming from you or X user and it should update it!. according to the function...

Link to comment
Share on other sites

Just speaking generally it depends on how you intend the system to work.  If I ask to follow you, and you accept the follow, does that mean we are friends? 

If that is how you want it to work, you can make it work even with your structure, but the queries and ORM code will be more complicated and less efficient.

You'll have to query and get all the "Accepted" status relationship rows for a user OR "Accepted" status relationship rows where the user_id = friend_id.  This is not going to be clean within the ORM, or with a raw SQL query.  It's messy, but you can certainly make it work.  

The implication in the code snippet you had, was that you want to query for 'Pending' rows where the friend_id = the user_id of the user with the Pending request.  If that request was found, you would need to update that relationship row to "accepted".  

It seems to me that you were on the right track -- you just need to add that conditional code.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.