Search code examples
database-designsocial-networking

Database design for Social network friends list, friends circle, post sharing


I want user to view by recent Post in the order they were posted, but only if the user that created the post gave the user permission to view them. Much like google+ circle list.

Here is my original design, any suggestion on improvement, would be appreciated.

USER
Id
Name
...


POST
Id
Message
IsCloseFriendCanView
IsFamilyCanView
IsAcquaintanceCanView
IsDisabledComment
IsPostLock
CreatedByUserId
Created_Time
LastComment_Time 
...

POST_COMMENT
Id
PostId
Message
CreatedByUserId
Created_Time


FRIEND
Id
UserId
FriendUserId
IsCloseFriend
IsFamily
IsAcquaintance
IsBlocked
....

FRIEND_LIST (custom friends list only)
Id
UserId (owner)
FriendId
FriendListTypeId

FRIEND_LIST_TYPE 
Id
UserId
Name
...

POST_FRIEND (CAN VIEW)
Id
PostId
FriendId
....


POST_FRIEND_LIST (CAN VIEW - custom)
Id
PostId
FriendListTypeId
....

EDIT: updated


Solution

  • Not sure if this is what you want but seems you will also need a table holding the relation between user and friends

    e.g.

    FRIEND
    user_id
    friend_id
    close_friend
    

    And on POST you would need something to identify whether it is visible to "All Friends","Close Friends" or "Public".

    Then assuming $this_user is a logged in user or some dummy value e.g. -1 if not logged in, you can select a list of valid posts which will show

    1. posts created by the logged in user
    2. posts created by friends of the logged in user visible to all friends
    3. posts created by friends of the logged in user visible to close friends
    4. posts set as public

    using something like

    select p.id, p.message, 
      from POST p
     where createdbyuserid=$this_user 
        or (visible='F' 
            and exists (select 1 
                     from friend f 
                    where p.createdbyuserid=f.user_id 
                      and f.friend_id=$this_user)
           )
        or (visible='C' 
            and exists (select 1 
                     from friend f 
                    where p.createdbyuserid=f.user_id 
                      and f.friend_id=$this_user
                      and f.close_friend='Y')
           )
        or (visible='P')
     order by created_time
    

    If no user is logged in, then $this_user would be set to -1 so only the public posts would be visible.

    Then you use the result from this select to get the relevant comments for each post in a subsequent select.