eventsnoticestream.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. class RawEventsNoticeStream extends NoticeStream
  18. {
  19. public function getNoticeIds($offset, $limit, $since_id, $max_id)
  20. {
  21. $notice = new Notice();
  22. $notice->selectAdd();
  23. $notice->selectAdd('notice.*');
  24. $notice->joinAdd(['uri', 'happening:uri']);
  25. $notice->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  26. Notice::addWhereSinceId($notice, $since_id, 'notice.id', 'happening.created');
  27. Notice::addWhereMaxId($notice, $max_id, 'notice.id', 'happening.created');
  28. // NOTE: we sort by event time, not by notice time!
  29. $notice->orderBy('happening.created DESC');
  30. if (!is_null($offset)) {
  31. $notice->limit($offset, $limit);
  32. }
  33. $ids = [];
  34. if ($notice->find()) {
  35. while ($notice->fetch()) {
  36. $ids[] = $notice->id;
  37. }
  38. }
  39. $notice->free();
  40. unset($notice);
  41. return $ids;
  42. }
  43. }
  44. class EventsNoticeStream extends ScopingNoticeStream
  45. {
  46. // possible values of RSVP in our database
  47. protected $rsvp = ['Y', 'N', '?'];
  48. protected $target = null;
  49. public function __construct(Profile $target, Profile $scoped = null, array $rsvp = [])
  50. {
  51. $stream = new RawEventsNoticeStream();
  52. if ($target->sameAs($scoped)) {
  53. $key = 'happening:ids_for_user_own:'.$target->getID();
  54. } else {
  55. $key = 'happening:ids_for_user:'.$target->getID();
  56. }
  57. // Match RSVP against our possible values, given in the class variable
  58. // and if no RSVPs are given is empty, assume we want all events, even
  59. // without RSVPs from this profile.
  60. $this->rsvp = array_intersect($this->rsvp, $rsvp);
  61. $this->target = $target;
  62. parent::__construct(new CachingNoticeStream($stream, $key), $scoped);
  63. }
  64. protected function filter(Notice $notice)
  65. {
  66. if (!parent::filter($notice)) {
  67. // if not in our scope, return false
  68. return false;
  69. }
  70. if (empty($this->rsvp)) {
  71. // Don't filter on RSVP (for only events with RSVP if no responses
  72. // are given (give ['Y', 'N', '?'] for only RSVP'd events!).
  73. return true;
  74. }
  75. $rsvp = new RSVP();
  76. $rsvp->profile_id = $this->target->getID();
  77. $rsvp->event_uri = $notice->getUri();
  78. $rsvp->whereAddIn('response', $this->rsvp, $rsvp->columnType('response'));
  79. // filter out if no RSVP match was found
  80. return $rsvp->N > 0;
  81. }
  82. }