NotificationStream.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. *
  4. * Notification stream
  5. *
  6. */
  7. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  8. class NotificationStream
  9. {
  10. protected $target = null;
  11. /**
  12. * Constructor
  13. *
  14. * @param Profile $target Profile to get a stream for
  15. */
  16. function __construct(Profile $target)
  17. {
  18. $this->target = $target;
  19. }
  20. /**
  21. * Get IDs in a range
  22. *
  23. * @param int $offset Offset from start
  24. * @param int $limit Limit of number to get
  25. * @param int $since_id Since this notice
  26. * @param int $max_id Before this notice
  27. *
  28. * @return Array IDs found
  29. */
  30. function getNotificationIds($offset, $limit, $since_id, $max_id)
  31. {
  32. $current_profile = Profile::current();
  33. $notification = new QvitterNotification();
  34. $notification->selectAdd();
  35. $notification->selectAdd('id');
  36. $notification->whereAdd(sprintf('qvitternotification.to_profile_id = "%s"', $notification->escape($this->target->id)));
  37. $notification->whereAdd(sprintf('qvitternotification.created >= "%s"', $notification->escape($this->target->created)));
  38. // if the user only want notifications from users they follow
  39. $only_show_notifications_from_users_i_follow = Profile_prefs::getConfigData($current_profile, 'qvitter', 'only_show_notifications_from_users_i_follow');
  40. if($only_show_notifications_from_users_i_follow == '1') {
  41. $notification->whereAdd(sprintf('qvitternotification.from_profile_id IN (SELECT subscribed FROM subscription WHERE subscriber = %u)', $current_profile->id));
  42. }
  43. // the user might have opted out from notifications from profiles they have muted
  44. $hide_notifications_from_muted_users = Profile_prefs::getConfigData($current_profile, 'qvitter', 'hide_notifications_from_muted_users');
  45. if($hide_notifications_from_muted_users == '1') {
  46. $muted_ids = QvitterMuted::getMutedIDs($current_profile->id,0,10000); // get all (hopefully not more than 10 000...)
  47. if($muted_ids !== false && count($muted_ids) > 0) {
  48. $ids_imploded = implode(',',$muted_ids);
  49. $notification->whereAdd('qvitternotification.from_profile_id NOT IN ('.$ids_imploded.')');
  50. }
  51. }
  52. // the user might have opted out from certain notification types
  53. $disable_notify_replies_and_mentions = Profile_prefs::getConfigData($current_profile, 'qvitter', 'disable_notify_replies_and_mentions');
  54. $disable_notify_favs = Profile_prefs::getConfigData($current_profile, 'qvitter', 'disable_notify_favs');
  55. $disable_notify_repeats = Profile_prefs::getConfigData($current_profile, 'qvitter', 'disable_notify_repeats');
  56. $disable_notify_follows = Profile_prefs::getConfigData($current_profile, 'qvitter', 'disable_notify_follows');
  57. if($disable_notify_replies_and_mentions == '1') {
  58. $notification->whereAdd('qvitternotification.ntype != "mention"');
  59. $notification->whereAdd('qvitternotification.ntype != "reply"');
  60. }
  61. if($disable_notify_favs == '1') {
  62. $notification->whereAdd('qvitternotification.ntype != "like"');
  63. }
  64. if($disable_notify_repeats == '1') {
  65. $notification->whereAdd('qvitternotification.ntype != "repeat"');
  66. }
  67. if($disable_notify_follows == '1') {
  68. $notification->whereAdd('qvitternotification.ntype != "follow"');
  69. }
  70. $notification->limit($offset, $limit);
  71. $notification->orderBy('qvitternotification.created DESC');
  72. if($since_id) {
  73. $notification->whereAdd(sprintf('qvitternotification.id > %d', $notification->escape($since_id)));
  74. }
  75. if($max_id) {
  76. $notification->whereAdd(sprintf('qvitternotification.id <= %d', $notification->escape($max_id)));
  77. }
  78. if (!$notification->find()) {
  79. return array();
  80. }
  81. $ids = $notification->fetchAll('id');
  82. return $ids;
  83. }
  84. function getNotifications($offset, $limit, $sinceId, $maxId)
  85. {
  86. $all = array();
  87. do {
  88. $ids = $this->getNotificationIds($offset, $limit, $sinceId, $maxId);
  89. $notifications = QvitterNotification::pivotGet('id', $ids);
  90. // By default, takes out false values
  91. $notifications = array_filter($notifications);
  92. $all = array_merge($all, $notifications);
  93. if (count($notifications < count($ids))) {
  94. $offset += $limit;
  95. $limit -= count($notifications);
  96. }
  97. } while (count($notifications) < count($ids) && count($ids) > 0);
  98. return new ArrayWrapper($all);
  99. }
  100. }