]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/lib/eventsnoticestream.php
Merge remote-tracking branch 'upstream/nightly' into nightly
[quix0rs-gnu-social.git] / plugins / Event / lib / eventsnoticestream.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5 class RawEventsNoticeStream extends NoticeStream
6 {
7     function getNoticeIds($offset, $limit, $since_id, $max_id)
8     {
9         $notice = new Notice();
10         $qry = null;
11
12         $qry =  'SELECT notice.* FROM notice ';
13         $qry .= 'INNER JOIN happening ON happening.uri = notice.uri ';
14         $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
15
16         if ($since_id != 0) {
17             $qry .= 'AND notice.id > ' . $since_id . ' ';
18         }
19
20         if ($max_id != 0) {
21             $qry .= 'AND notice.id <= ' . $max_id . ' ';
22         }
23
24         // NOTE: we sort by event time, not by notice time!
25         $qry .= 'ORDER BY happening.created DESC ';
26         if (!is_null($offset)) {
27             $qry .= "LIMIT $limit OFFSET $offset";
28         }
29
30         $notice->query($qry);
31         $ids = array();
32         while ($notice->fetch()) {
33             $ids[] = $notice->id;
34         }
35
36         $notice->free();
37         unset($notice);
38         return $ids;
39     }
40 }
41
42 class EventsNoticeStream extends ScopingNoticeStream
43 {
44     // possible values of RSVP in our database
45     protected $rsvp = ['Y', 'N', '?'];
46     protected $target = null;
47
48     function __construct(Profile $target, Profile $scoped=null, array $rsvp=array())
49     {
50         $stream = new RawEventsNoticeStream();
51
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
58         // Match RSVP against our possible values, given in the class variable
59         // and if no RSVPs are given is empty, assume we want all events, even
60         // without RSVPs from this profile.
61         $this->rsvp = array_intersect($this->rsvp, $rsvp);
62         $this->target = $target;
63
64         parent::__construct(new CachingNoticeStream($stream, $key), $scoped);
65     }
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
74         if (empty($this->rsvp)) {
75             // Don't filter on RSVP (for only events with RSVP if no responses
76             // are given (give ['Y', 'N', '?'] for only RSVP'd events!).
77             return true;
78         }
79
80         $rsvp = new RSVP();
81         $rsvp->profile_id = $this->target->getID();
82         $rsvp->event_uri  = $notice->getUri();
83         $rsvp->whereAddIn('response', $this->rsvp, $rsvp->columnType('response'));
84
85         // filter out if no RSVP match was found
86         return $rsvp->N > 0;
87     }
88 }