Home>

In Weibo,Every user will have a watchlist,A fan list.Users can view their followers,Fan list,You can also check others ’attention,Fan list.Also, show the attention status of everyone in the list and the current viewer.

Focus on the four relationship states produced by the relationship

attention Fan Two-way attention (interchange) No relationship

demand analysis

In Weibo,Every user will have a watchlist,A fan list.Users can view their followers,Fan list,You can also check others ’attention,Fan list.Also, show the attention status of everyone in the list and the current viewer.The possibility of state is the four relation states mentioned above.

The problem can be viewed in two situations:

1. Look at your concerns.Fan list

2. Look at the attention of others,Fan list

Look at their concerns,Fan list:

This situation is relatively simple.Look at your own watchlist,The relationship status of the people on the list cannot be "unconnected" and "fan". It can only be "follow" and "two-way attention". Similarly, the fan list has only two states.

See others' attention,Fan list:

This is the most complicated situation,If you look at other people's watchlist,People on the list and themselves may have all four of the above relationship states.

Analyze from a collection of graphs

As shown in FIG.The circle on the left indicates the user's watchlist,The circle on the right represents the fan list,The circles below represent the list (collection) to be viewed. Use follow, fans, find to indicate these three sets.

When viewing your own list,In fact, it means that the find set is a subset of one of the above sets.Such as viewing your own fans,Indicates that find is a subset of fans, check your attention,Indicates that find is a subset of follow.

When viewing a list of others,At this point, the intersection of the three sets is generated in the figure.To query if a user in a collection is your fan,In the collection,Maybe not.It means that it may be any kind of relationship state,The root of the problem is,We need to calculate the status of the relationship between each user and the current user.Requires solving four relationship states,We must ask for the three small intersections in the lower part of the picture.

The set to query intersects with my mutual fan The set to query intersects with my attention Intersect with my fans for the set to query

Users who are not in these three minor intersections are users who are not related.

Suppose we use the following set of names:

Attention set

follow:userid fan collection fans:userid

Mutual powder collection (temporary)

fofa:userid The set to query (temporary) find:userid

The set to query intersects with my attention (temporary)

find_inter_follow:userid the set of queries to intersect with my fans (temporary) find_inter_fans:userid

The set to query intersects with my mutual fan (temporary)

find_inter_fofa:userid

The others in find are not following

Store relationships using sorted set

score is used to store the time of attention,Each user stores two collections.follow:userid stores the user's followers,fans:userid stores users' fans.Then we can design a function to find the set of these states.

The function returns:

"findset" =>$findset, //set to query
"fofaset" =>$fofaset, //set of mutual powder
"findinterfollowset" =>$findinterfollowset, //The set to be queried intersects with my attention
"findinterfansset" =>$findinterfansset //The set to be queried is with my fans

Find the above four sets,You can judge the relationship status,First determine whether each other is pink.If it's not mutual powder,Then judge whether it is my concern,if not,Then judge whether it is my fan.If it is not, it is okay.This will find the status.

/*
* userid:current user id
* targetuserid:the id of the person being viewed
* findtype:Which list are you looking at
* findstart:the position where the list starts
* findend:the end of the list for pagination
* /
function getchunksets ($redis, $userid, $targetuserid, $findtype, $findstart, $findend) {
 $fanskey="fans:". $userid;
 $followkey="follow:". $userid;
 $findkey="find:". $userid;
 $targetkey=$findtype. ":". $targetuserid;
 $fofakey="find_inter_fofa:". $userid;
 $findinterfollowkey="find_inter_follow:". $userid;
 $findinterfanskey="find_inter_fans:". $userid;
 //Find the collection element to be queried
 $findset=$redis->zrevrange ($targetkey, $findstart, $findend, true);
 //The set to be queried intersects with my attention
 $findinterfollowset=array ();
 //Interview with the set and my fans
 $findinterfansset=array ();
 //Clear the temporary collection first
 $redis->del ($findkey);
 $redis->del ($fofakey);
 $redis->del ($findinterfollowkey);
 $redis->del ($findinterfanskey);
 //save
 foreach ($findset as $uid =>$score) {
  $redis->zadd ($findkey, $score, $uid);
 }
 //Find mutual powder collection
 if ($userid!=$targetuserid) {//See others
  $redis->zinter ($fofakey, array ($findkey, $fanskey, $followkey));
  /*
  * If you are not looking at your own list,Also asked
  * 1:The collection to query intersects with my attention
  * 2:I want to check the episodes with my fans
  * /
  $redis->zinter ($findinterfollowkey, array ($findkey, $followkey));
  $redis->zinter ($findinterfanskey, array ($findkey, $fanskey));
  $findinterfollowset=$redis->zrevrange ($findinterfollowkey, 0, -1);
  $findinterfansset=$redis->zrevrange ($findinterfanskey, 0, -1);
 } else {
  if ($findtype == "fans") {//Look at the fan list yourself
  $redis->zinter ($fofakey, array ($findkey, $followkey));
  } else if ($findtype == "follow") {//Look at your own watchlist
  $redis->zinter ($fofakey, array ($findkey, $fanskey));
  }
 }
 //Mutual powder collection
 $fofaset=$redis->zrevrange ($fofakey, 0, -1);
 return array (
  "findset" =>$findset, //set to query
  "fofaset" =>$fofaset, //set of mutual powder
  "findinterfollowset" =>$findinterfollowset, //The set to be queried intersects with my attention
  "findinterfansset" =>$findinterfansset //The set to be queried is with my fans
 );
 }

The above function has found the required set,Then the relationship state is judged.

/*
* isself:whether to view your own list
* findtype:Seeing fans or watchlist 1:follow, 2:followers
* userinfoarr:array of user details
* /
function getuserinfolist ($isself, $findtype, $userinfoarr, $findset, $fofaset, $interfansset, $interfollowset) {
 $userinfolist=array ();
 foreach ($findset as $userid =>$favotime) {
  if (! in_array ($userid, array_keys ($userinfoarr))) continue;
  $userinfo=new userinfo ($userinfoarr [$userid]);
  $userinfo=$userinfo->format ();
  if (in_array ($userid, $fofaset)) {
  $userinfo ["favoflag"]=3;//Follow each other
  } else {
  if ($isself) {
   $userinfo ["favoflag"]=$findtype;
  } else {
   if (in_array ($userid, $interfansset)) {
   $userinfo ["favoflag"]=2;//My fans
   } else if (in_array ($userid, $interfollowset)) {
   $userinfo ["favoflag"]=1;//My followers
   } else {
   $userinfo ["favoflag"]=0;//No relationship
   }
  }
  }
  $userinfo ["favotime"]=$favotime;
  array_push ($userinfolist, $userinfo);
 }
 return $userinfolist;
 }
  • Previous Android common dialog usage example analysis
  • Next Five tips to speed up ASP