eventsnoticestream.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $qry = null;
  23. $qry = 'SELECT notice.* FROM notice ';
  24. $qry .= 'INNER JOIN happening ON happening.uri = notice.uri ';
  25. $qry .= 'AND notice.is_local <> ' . Notice::GATEWAY . ' ';
  26. if ($since_id != 0) {
  27. $qry .= 'AND notice.id > ' . $since_id . ' ';
  28. }
  29. if ($max_id != 0) {
  30. $qry .= 'AND notice.id <= ' . $max_id . ' ';
  31. }
  32. // NOTE: we sort by event time, not by notice time!
  33. $qry .= 'ORDER BY happening.created DESC ';
  34. if (!is_null($offset)) {
  35. $qry .= "LIMIT $limit OFFSET $offset";
  36. }
  37. $notice->query($qry);
  38. $ids = array();
  39. while ($notice->fetch()) {
  40. $ids[] = $notice->id;
  41. }
  42. $notice->free();
  43. unset($notice);
  44. return $ids;
  45. }
  46. }
  47. class EventsNoticeStream extends ScopingNoticeStream
  48. {
  49. // possible values of RSVP in our database
  50. protected $rsvp = ['Y', 'N', '?'];
  51. protected $target = null;
  52. public function __construct(Profile $target, Profile $scoped = null, array $rsvp = [])
  53. {
  54. $stream = new RawEventsNoticeStream();
  55. if ($target->sameAs($scoped)) {
  56. $key = 'happening:ids_for_user_own:'.$target->getID();
  57. } else {
  58. $key = 'happening:ids_for_user:'.$target->getID();
  59. }
  60. // Match RSVP against our possible values, given in the class variable
  61. // and if no RSVPs are given is empty, assume we want all events, even
  62. // without RSVPs from this profile.
  63. $this->rsvp = array_intersect($this->rsvp, $rsvp);
  64. $this->target = $target;
  65. parent::__construct(new CachingNoticeStream($stream, $key), $scoped);
  66. }
  67. protected function filter(Notice $notice)
  68. {
  69. if (!parent::filter($notice)) {
  70. // if not in our scope, return false
  71. return false;
  72. }
  73. if (empty($this->rsvp)) {
  74. // Don't filter on RSVP (for only events with RSVP if no responses
  75. // are given (give ['Y', 'N', '?'] for only RSVP'd events!).
  76. return true;
  77. }
  78. $rsvp = new RSVP();
  79. $rsvp->profile_id = $this->target->getID();
  80. $rsvp->event_uri = $notice->getUri();
  81. $rsvp->whereAddIn('response', $this->rsvp, $rsvp->columnType('response'));
  82. // filter out if no RSVP match was found
  83. return $rsvp->N > 0;
  84. }
  85. }