eventsnoticestream.php 2.5 KB

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