What I am trying to achieve is a complex privacy routine, i have the following tables.
posts
posts_privacy
privacy_global
followerlist
We have a news feed sort of thing on our site, which gets the 10 latests posts from the people a user is following, there are 3 levels of privacy, first is privacy_global where the user can set a privacy options on his posts , 0 , 1 and 2 for "Only me", "Everyone", "followers only" respectively, then there is posts_privacy where the user can choose to list people he wants to show to or hide the post from before posting it, this setting overrides the privacy_global settings and posts_privacy instead if set.
now what my problem is that, when i apply privacy in the news feed using php I basically ask for the posts like this.
SELECT `post`.*,
`users`.`fullname`,
`users`.`username`,
`posts_privacy`.`hide`,
`posts_privacy`.`show`,
FROM `post`
LEFT JOIN `posts_privacy` ON `post`.`id`=`posts_privacy`.`postid`
INNER JOIN `users` ON `post`.`userid` = `users`.`id`
WHERE (`post`.`userid` IN (1,2,3,4,5,6)
AND 12 NOT IN (
SELECT `hide` FROM `posts_privacy` WHERE `postid`=`post`.`id`)
OR `show`= 12
)
GROUP BY `post`.`id`
ORDER BY `time` DESC LIMIT 10"
I cant figure out how to apply all the other conditions, before this i was using another query and returning serialized arrays of people that the post needs to be hidden or shown to, then unserialized and proccessed it in php and removed that post from the array before being sent to the browser, that created a hole in the news feed, instead of loading 10 posts only 6 posts appear becasue php removed the 4 that had privacy before they made it to the browser, my goal here is to do EVERYTHING all the privacy conditions in SQL before the posts leave the database, so I dont have this issue with the counts of the posts in the newsfeed.
I tried looking into MySQL functions and procedures but i was told that they cant process queries or return tables.
any advice as to how to approach this issue?
EDIT: If it helps, the system i am trying to make is simlar to that on Facebook, where the user can set post specfic privacy, but also has global settings that get overridded when that is done.
Structure of tables:
followerslist:
id PRIMARY KEY BIGINT(20)
userid BIGINT(20) << id of the user who clicks the follow button
targetid BIGINT(20) << id of the user who gets followed
post:
id PRIMARY KEY BIGINT(20)
content TEXT
title VARCHAR(100)
time DATETIME
userid BIGINT(20)
posts_privacy:
id PRIMARY KEY BIGINT(20)
postid BIGINT(20)
hide BIGINT(20)
show BIGINT(20)