UserNodeRecord.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. class UserNodeRecord {
  4. private $id;//用户id
  5. private $set;//关注列表
  6. private $f_set;//粉丝列表
  7. private $mutual_set;//互相关注列表
  8. public function __construct($userID) {
  9. $this->id = (string)$userID;
  10. $this->set = new ARedisSet($this->id);
  11. $this->f_set = new ARedisSet($this->id.'_follow_by');
  12. }
  13. //关注某个用户
  14. public function follow($user_id) {
  15. $user = (string)$user_id;
  16. if($user != $this->id){
  17. $this->set->add($user);
  18. $user_follow_by_set = new ARedisSet($user.'_follow_by'); //被关注用户粉丝列表
  19. $user_follow_by_set->add($this->id);
  20. $c_user = RUser::get(new MongoId($this->id));
  21. $z_message = new ZMessage();
  22. $from_user = Yii::app()->params['sys_user'];
  23. $m_data = array(
  24. 'from_user' => $from_user,
  25. 'to_user' => $user,
  26. 'content' => '<a href="http://www.wozhua.mobi/user/'.$this->id.'">'.$c_user->user_name.'</a> 关注了你' ,
  27. 'pics' => array(),
  28. 'voice' => array(),
  29. 'video'=> array()
  30. );
  31. $z_message->addMessage($m_data);
  32. }
  33. }
  34. //取消关注
  35. public function unfollow($user) {
  36. $user = (string)$user;
  37. if ($this->is_following($user)) {
  38. $this->set->remove($user);
  39. $user_follow_by_set = new ARedisSet($user.'_follow_by'); //被关注用户粉丝列表
  40. $user_follow_by_set->remove($this->id);
  41. }
  42. }
  43. //获取关注列表
  44. public function following() {
  45. return $this->set->getData();
  46. }
  47. //获取粉丝列表
  48. public function followed_by() {
  49. return $this->f_set->getData();
  50. }
  51. //判断是否互相关注
  52. public function is_mutual($user){
  53. $user = (string)$user;
  54. return in_array($user,$this->mutual_set);
  55. }
  56. //判断是否关注某用户
  57. public function is_following($user) {
  58. $user = (string)$user;
  59. return $this->set->contains($user);
  60. }
  61. //判断是否被某用户关注
  62. public function is_followed_by($user) {
  63. $user = (string)$user;
  64. return $this->f_set->contains($user);
  65. }
  66. //获取互相关注列表
  67. public function mutual_follow() {
  68. return $this->set->inter($this->f_set);
  69. }
  70. //获取用户关注数
  71. public function follow_count() {
  72. return $this->set->getCount();
  73. }
  74. //获取粉丝数
  75. public function follower_count() {
  76. return $this->f_set->getCount();
  77. }
  78. //判断用户关系
  79. public function relation($user){
  80. $user = (string)$user;
  81. if ($this->is_following($user)) {//关注
  82. if($this->is_followed_by($user)){
  83. return 3;
  84. }else{
  85. return 1;
  86. }
  87. }elseif ($this->is_followed_by($user)) {//被关注
  88. return 2;
  89. }else{
  90. return 0;
  91. }
  92. }
  93. }