]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Merge pull request #8279 from MrPetovan/task/frio-mobile-actions-buttons
[friendica.git] / src / Object / Thread.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\DI;
27 use Friendica\Protocol\Activity;
28 use Friendica\Util\Security;
29
30 /**
31  * A list of threads
32  *
33  * We should think about making this a SPL Iterator
34  */
35 class Thread
36 {
37         private $parents = [];
38         private $mode = null;
39         private $writable = false;
40         private $profile_owner = 0;
41         private $preview = false;
42
43         /**
44          * Constructor
45          *
46          * @param string  $mode     The mode
47          * @param boolean $preview  Are we in the preview mode?
48          * @param boolean $writable Override the writable check
49          * @throws \Exception
50          */
51         public function __construct($mode, $preview, $writable = false)
52         {
53                 $this->setMode($mode, $writable);
54                 $this->preview = $preview;
55         }
56
57         /**
58          * Set the mode we'll be displayed on
59          *
60          * @param string  $mode     The mode to set
61          * @param boolean $writable Override the writable check
62          *
63          * @return void
64          * @throws \Exception
65          */
66         private function setMode($mode, $writable)
67         {
68                 if ($this->getMode() == $mode) {
69                         return;
70                 }
71
72                 $a = DI::app();
73
74                 switch ($mode) {
75                         case 'network':
76                         case 'notes':
77                                 $this->profile_owner = local_user();
78                                 $this->writable = true;
79                                 break;
80                         case 'profile':
81                                 $this->profile_owner = $a->profile['uid'];
82                                 $this->writable = Security::canWriteToUserWall($this->profile_owner);
83                                 break;
84                         case 'display':
85                                 $this->profile_owner = $a->profile['uid'];
86                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
87                                 break;
88                         case 'community':
89                                 $this->profile_owner = 0;
90                                 $this->writable = $writable;
91                                 break;
92                         case 'contacts':
93                                 $this->profile_owner = 0;
94                                 $this->writable = $writable;
95                                 break;
96                         default:
97                                 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
98                                 return false;
99                                 break;
100                 }
101                 $this->mode = $mode;
102         }
103
104         /**
105          * Get mode
106          *
107          * @return string
108          */
109         public function getMode()
110         {
111                 return $this->mode;
112         }
113
114         /**
115          * Check if page is writable
116          *
117          * @return boolean
118          */
119         public function isWritable()
120         {
121                 return $this->writable;
122         }
123
124         /**
125          * Check if page is a preview
126          *
127          * @return boolean
128          */
129         public function isPreview()
130         {
131                 return $this->preview;
132         }
133
134         /**
135          * Get profile owner
136          *
137          * @return integer
138          */
139         public function getProfileOwner()
140         {
141                 return $this->profile_owner;
142         }
143
144         /**
145          * Add a thread to the conversation
146          *
147          * @param Post $item The item to insert
148          *
149          * @return mixed The inserted item on success
150          *               false on failure
151          * @throws \Exception
152          */
153         public function addParent(Post $item)
154         {
155                 $item_id = $item->getId();
156
157                 if (!$item_id) {
158                         Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
159                         return false;
160                 }
161
162                 if ($this->getParent($item->getId())) {
163                         Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
164                         return false;
165                 }
166
167                 /*
168                  * Only add will be displayed
169                  */
170                 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
171                         Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
172                         return false;
173                 }
174
175                 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
176                         Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
177                         return false;
178                 }
179
180                 $item->setThread($this);
181                 $this->parents[] = $item;
182
183                 return end($this->parents);
184         }
185
186         /**
187          * Get data in a form usable by a conversation template
188          *
189          * We should find a way to avoid using those arguments (at least most of them)
190          *
191          * @param array $conv_responses data
192          *
193          * @return mixed The data requested on success
194          *               false on failure
195          * @throws \Exception
196          */
197         public function getTemplateData($conv_responses)
198         {
199                 $result = [];
200
201                 foreach ($this->parents as $item) {
202                         if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
203                                 continue;
204                         }
205
206                         $item_data = $item->getTemplateData($conv_responses);
207
208                         if (!$item_data) {
209                                 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
210                                 return false;
211                         }
212                         $result[] = $item_data;
213                 }
214
215                 return $result;
216         }
217
218         /**
219          * Get a thread based on its item id
220          *
221          * @param integer $id Item id
222          *
223          * @return mixed The found item on success
224          *               false on failure
225          */
226         private function getParent($id)
227         {
228                 foreach ($this->parents as $item) {
229                         if ($item->getId() == $id) {
230                                 return $item;
231                         }
232                 }
233
234                 return false;
235         }
236 }