]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
API: fix sender/recipient of PMs: check api_user before get user info.
[friendica.git] / src / Object / Thread.php
1 <?php
2 /**
3  * @file src/Object/Thread.php
4  */
5 namespace Friendica\Object;
6
7 use Friendica\BaseObject;
8 use Friendica\Object\Post;
9
10 require_once 'boot.php';
11 require_once 'include/text.php';
12
13 /**
14  * A list of threads
15  *
16  * We should think about making this a SPL Iterator
17  */
18 class Thread extends BaseObject
19 {
20         private $parents = [];
21         private $mode = null;
22         private $writable = false;
23         private $profile_owner = 0;
24         private $preview = false;
25
26         /**
27          * Constructor
28          *
29          * @param string  $mode    The mode
30          * @param boolean $preview Are we in the preview mode?
31          * @param boolean $writable Override the writable check
32          */
33         public function __construct($mode, $preview, $writable = false)
34         {
35                 $this->setMode($mode, $writable);
36                 $this->preview = $preview;
37         }
38
39         /**
40          * Set the mode we'll be displayed on
41          *
42          * @param string $mode The mode to set
43          * @param boolean $writable Override the writable check
44          *
45          * @return void
46          */
47         private function setMode($mode, $writable)
48         {
49                 if ($this->getMode() == $mode) {
50                         return;
51                 }
52
53                 $a = self::getApp();
54
55                 switch ($mode) {
56                         case 'network':
57                         case 'notes':
58                                 $this->profile_owner = local_user();
59                                 $this->writable = true;
60                                 break;
61                         case 'profile':
62                                 $this->profile_owner = $a->profile['profile_uid'];
63                                 $this->writable = can_write_wall($this->profile_owner);
64                                 break;
65                         case 'display':
66                                 $this->profile_owner = $a->profile['uid'];
67                                 $this->writable = can_write_wall($this->profile_owner) || $writable;
68                                 break;
69                         case 'community':
70                                 $this->profile_owner = 0;
71                                 $this->writable = $writable;
72                                 break;
73                         default:
74                                 logger('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
75                                 return false;
76                                 break;
77                 }
78                 $this->mode = $mode;
79         }
80
81         /**
82          * Get mode
83          *
84          * @return string
85          */
86         public function getMode()
87         {
88                 return $this->mode;
89         }
90
91         /**
92          * Check if page is writable
93          *
94          * @return boolean
95          */
96         public function isWritable()
97         {
98                 return $this->writable;
99         }
100
101         /**
102          * Check if page is a preview
103          *
104          * @return boolean
105          */
106         public function isPreview()
107         {
108                 return $this->preview;
109         }
110
111         /**
112          * Get profile owner
113          *
114          * @return integer
115          */
116         public function getProfileOwner()
117         {
118                 return $this->profile_owner;
119         }
120
121         /**
122          * Add a thread to the conversation
123          *
124          * @param object $item The item to insert
125          *
126          * @return mixed The inserted item on success
127          *               false on failure
128          */
129         public function addParent(Post $item)
130         {
131                 $item_id = $item->getId();
132
133                 if (!$item_id) {
134                         logger('[ERROR] Conversation::addThread : Item has no ID!!', LOGGER_DEBUG);
135                         return false;
136                 }
137
138                 if ($this->getParent($item->getId())) {
139                         logger('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', LOGGER_DEBUG);
140                         return false;
141                 }
142
143                 /*
144                  * Only add will be displayed
145                  */
146                 if ($item->getDataValue('network') === NETWORK_MAIL && local_user() != $item->getDataValue('uid')) {
147                         logger('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', LOGGER_DEBUG);
148                         return false;
149                 }
150
151                 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
152                         logger('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', LOGGER_DEBUG);
153                         return false;
154                 }
155
156                 $item->setThread($this);
157                 $this->parents[] = $item;
158
159                 return end($this->parents);
160         }
161
162         /**
163          * Get data in a form usable by a conversation template
164          *
165          * We should find a way to avoid using those arguments (at least most of them)
166          *
167          * @param object $conv_responses data
168          *
169          * @return mixed The data requested on success
170          *               false on failure
171          */
172         public function getTemplateData($conv_responses)
173         {
174                 $a = self::getApp();
175                 $result = [];
176                 $i = 0;
177
178                 foreach ($this->parents as $item) {
179                         if ($item->getDataValue('network') === NETWORK_MAIL && local_user() != $item->getDataValue('uid')) {
180                                 continue;
181                         }
182
183                         $item_data = $item->getTemplateData($conv_responses);
184
185                         if (!$item_data) {
186                                 logger('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', LOGGER_DEBUG);
187                                 return false;
188                         }
189                         $result[] = $item_data;
190                 }
191
192                 return $result;
193         }
194
195         /**
196          * Get a thread based on its item id
197          *
198          * @param integer $id Item id
199          *
200          * @return mixed The found item on success
201          *               false on failure
202          */
203         private function getParent($id)
204         {
205                 foreach ($this->parents as $item) {
206                         if ($item->getId() == $id) {
207                                 return $item;
208                         }
209                 }
210
211                 return false;
212         }
213 }