]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / src / Object / Thread.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Session;
27 use Friendica\DI;
28 use Friendica\Protocol\Activity;
29 use Friendica\Security\Security;
30
31 /**
32  * A list of threads
33  *
34  * We should think about making this a SPL Iterator
35  */
36 class Thread
37 {
38         /** @var Post[] */
39         private $parents = [];
40         private $mode = null;
41         private $writable = false;
42         private $profile_owner = 0;
43         private $preview = false;
44
45         /**
46          * Constructor
47          *
48          * @param string  $mode     The mode
49          * @param boolean $preview  Are we in the preview mode?
50          * @param boolean $writable Override the writable check
51          * @throws \Exception
52          */
53         public function __construct($mode, $preview, $writable = false)
54         {
55                 $this->setMode($mode, $writable);
56                 $this->preview = $preview;
57         }
58
59         /**
60          * Set the mode we'll be displayed on
61          *
62          * @param string  $mode     The mode to set
63          * @param boolean $writable Override the writable check
64          *
65          * @return void
66          * @throws \Exception
67          */
68         private function setMode($mode, $writable)
69         {
70                 if ($this->getMode() == $mode) {
71                         return;
72                 }
73
74                 $a = DI::app();
75
76                 switch ($mode) {
77                         case 'network':
78                         case 'notes':
79                                 $this->profile_owner = Session::getLocalUser();
80                                 $this->writable = true;
81                                 break;
82                         case 'profile':
83                                 $this->profile_owner = $a->getProfileOwner();
84                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
85                                 break;
86                         case 'display':
87                                 $this->profile_owner = $a->getProfileOwner();
88                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
89                                 break;
90                         case 'community':
91                                 $this->profile_owner = 0;
92                                 $this->writable = $writable;
93                                 break;
94                         case 'contacts':
95                                 $this->profile_owner = 0;
96                                 $this->writable = $writable;
97                                 break;
98                         default:
99                                 Logger::info('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').');
100                                 return false;
101                                 break;
102                 }
103                 $this->mode = $mode;
104         }
105
106         /**
107          * Get mode
108          *
109          * @return string
110          */
111         public function getMode()
112         {
113                 return $this->mode;
114         }
115
116         /**
117          * Check if page is writable
118          *
119          * @return boolean
120          */
121         public function isWritable()
122         {
123                 return $this->writable;
124         }
125
126         /**
127          * Check if page is a preview
128          *
129          * @return boolean
130          */
131         public function isPreview()
132         {
133                 return $this->preview;
134         }
135
136         /**
137          * Get profile owner
138          *
139          * @return integer
140          */
141         public function getProfileOwner()
142         {
143                 return $this->profile_owner;
144         }
145
146         /**
147          * Add a thread to the conversation
148          *
149          * @param Post $item The item to insert
150          *
151          * @return mixed The inserted item on success
152          *               false on failure
153          * @throws \Exception
154          */
155         public function addParent(Post $item)
156         {
157                 $item_id = $item->getId();
158
159                 if (!$item_id) {
160                         Logger::info('[ERROR] Conversation::addThread : Item has no ID!!');
161                         return false;
162                 }
163
164                 if ($this->getParent($item->getId())) {
165                         Logger::info('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').');
166                         return false;
167                 }
168
169                 /*
170                  * Only add will be displayed
171                  */
172                 if ($item->getDataValue('network') === Protocol::MAIL && Session::getLocalUser() != $item->getDataValue('uid')) {
173                         Logger::info('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').');
174                         return false;
175                 }
176
177                 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
178                         Logger::info('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').');
179                         return false;
180                 }
181
182                 $item->setThread($this);
183                 $this->parents[] = $item;
184
185                 return end($this->parents);
186         }
187
188         /**
189          * Get data in a form usable by a conversation template
190          *
191          * We should find a way to avoid using those arguments (at least most of them)
192          *
193          * @param array $conv_responses data
194          * @param string $formSecurityToken A security Token to avoid CSF attacks
195          *
196          * @return mixed The data requested on success
197          *               false on failure
198          * @throws \Exception
199          */
200         public function getTemplateData($conv_responses, string $formSecurityToken)
201         {
202                 $result = [];
203
204                 foreach ($this->parents as $item) {
205                         if ($item->getDataValue('network') === Protocol::MAIL && Session::getLocalUser() != $item->getDataValue('uid')) {
206                                 continue;
207                         }
208
209                         $item_data = $item->getTemplateData($conv_responses, $formSecurityToken);
210
211                         if (!$item_data) {
212                                 Logger::info('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').');
213                                 return false;
214                         }
215                         $result[] = $item_data;
216                 }
217
218                 return $result;
219         }
220
221         /**
222          * Get a thread based on its item id
223          *
224          * @param integer $id Item id
225          *
226          * @return mixed The found item on success
227          *               false on failure
228          */
229         private function getParent($id)
230         {
231                 foreach ($this->parents as $item) {
232                         if ($item->getId() == $id) {
233                                 return $item;
234                         }
235                 }
236
237                 return false;
238         }
239 }