]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
Merge branch 'testing' into moveaccount
[quix0rs-gnu-social.git] / lib / useractivitystream.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010 StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * Class for activity streams
22  *
23  * Includes faves, notices, and subscriptions.
24  *
25  * We extend atomusernoticefeed since it does some nice setup for us.
26  *
27  */
28
29 class UserActivityStream extends AtomUserNoticeFeed
30 {
31     public $activities = array();
32
33     function __construct($user, $indent = true)
34     {
35         parent::__construct($user, null, $indent);
36
37         $subscriptions = $this->getSubscriptions();
38         $subscribers   = $this->getSubscribers();
39         $groups        = $this->getGroups();
40         $faves         = $this->getFaves();
41         $notices       = $this->getNotices();
42
43         $objs = array_merge($subscriptions, $subscribers, $groups, $faves, $notices);
44
45         // Sort by create date
46
47         usort($objs, 'UserActivityStream::compareObject');
48
49         foreach ($objs as $obj) {
50             $this->activities[] = $obj->asActivity();
51         }
52     }
53     
54     function renderEntries()
55     {
56         foreach ($this->activities as $act) {
57             // Only show the author sub-element if it's different from default user
58             $act->outputTo($this, false, ($act->actor->id != $this->user->uri));
59         }
60     }
61
62     function compareObject($a, $b)
63     {
64         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
65         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
66
67         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
68     }
69
70     function getSubscriptions()
71     {
72         $subs = array();
73
74         $sub = new Subscription();
75
76         $sub->subscriber = $this->user->id;
77
78         if ($sub->find()) {
79             while ($sub->fetch()) {
80                 if ($sub->subscribed != $this->user->id) {
81                     $subs[] = clone($sub);
82                 }
83             }
84         }
85
86         return $subs;
87     }
88
89     function getSubscribers()
90     {
91         $subs = array();
92
93         $sub = new Subscription();
94
95         $sub->subscribed = $this->user->id;
96
97         if ($sub->find()) {
98             while ($sub->fetch()) {
99                 if ($sub->subscriber != $this->user->id) {
100                     $subs[] = clone($sub);
101                 }
102             }
103         }
104
105         return $subs;
106     }
107
108     function getFaves()
109     {
110         $faves = array();
111
112         $fave = new Fave();
113
114         $fave->user_id = $this->user->id;
115
116         if ($fave->find()) {
117             while ($fave->fetch()) {
118                 $faves[] = clone($fave);
119             }
120         }
121
122         return $faves;
123     }
124
125     function getNotices()
126     {
127         $notices = array();
128
129         $notice = new Notice();
130
131         $notice->profile_id = $this->user->id;
132
133         if ($notice->find()) {
134             while ($notice->fetch()) {
135                 $notices[] = clone($notice);
136             }
137         }
138
139         return $notices;
140     }
141
142     function getGroups()
143     {
144         $groups = array();
145
146         $gm = new Group_member();
147
148         $gm->profile_id = $this->user->id;
149
150         if ($gm->find()) {
151             while ($gm->fetch()) {
152                 $groups[] = clone($gm);
153             }
154         }
155
156         return $groups;
157     }
158 }