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