]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
change isreplyto to inreplyto
[friendica.git] / src / Object / Thread.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Content\Conversation;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
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 Conversation::MODE_NETWORK:
78                         case Conversation::MODE_NOTES:
79                                 $this->profile_owner = DI::userSession()->getLocalUserId();
80                                 $this->writable = true;
81                                 break;
82                         case Conversation::MODE_PROFILE:
83                         case Conversation::MODE_DISPLAY:
84                                 $this->profile_owner = $a->getProfileOwner();
85                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
86                                 break;
87                         case Conversation::MODE_CHANNEL:
88                         case Conversation::MODE_COMMUNITY:
89                         case Conversation::MODE_CONTACTS:
90                                 $this->profile_owner = 0;
91                                 $this->writable = $writable;
92                                 break;
93                         default:
94                                 Logger::info('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').');
95                                 return false;
96                                 break;
97                 }
98                 $this->mode = $mode;
99         }
100
101         /**
102          * Get mode
103          *
104          * @return string
105          */
106         public function getMode()
107         {
108                 return $this->mode;
109         }
110
111         /**
112          * Check if page is writable
113          *
114          * @return boolean
115          */
116         public function isWritable()
117         {
118                 return $this->writable;
119         }
120
121         /**
122          * Check if page is a preview
123          *
124          * @return boolean
125          */
126         public function isPreview()
127         {
128                 return $this->preview;
129         }
130
131         /**
132          * Get profile owner
133          *
134          * @return integer
135          */
136         public function getProfileOwner()
137         {
138                 return $this->profile_owner;
139         }
140
141         /**
142          * Add a thread to the conversation
143          *
144          * @param Post $item The item to insert
145          *
146          * @return mixed The inserted item on success
147          *               false on failure
148          * @throws \Exception
149          */
150         public function addParent(Post $item)
151         {
152                 $item_id = $item->getId();
153
154                 if (!$item_id) {
155                         Logger::info('[ERROR] Conversation::addThread : Item has no ID!!');
156                         return false;
157                 }
158
159                 if ($this->getParent($item->getId())) {
160                         Logger::info('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').');
161                         return false;
162                 }
163
164                 /*
165                  * Only add will be displayed
166                  */
167                 if ($item->getDataValue('network') === Protocol::MAIL && DI::userSession()->getLocalUserId() != $item->getDataValue('uid')) {
168                         Logger::info('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').');
169                         return false;
170                 }
171
172                 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
173                         Logger::info('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').');
174                         return false;
175                 }
176
177                 $item->setThread($this);
178                 $this->parents[] = $item;
179
180                 return end($this->parents);
181         }
182
183         /**
184          * Get data in a form usable by a conversation template
185          *
186          * We should find a way to avoid using those arguments (at least most of them)
187          *
188          * @param array $conv_responses data
189          * @param string $formSecurityToken A 'contact_action' form security token
190          *
191          * @return mixed The data requested on success
192          *               false on failure
193          * @throws \Exception
194          */
195         public function getTemplateData(array $conv_responses, string $formSecurityToken)
196         {
197                 $result = [];
198
199                 foreach ($this->parents as $item) {
200                         if ($item->getDataValue('network') === Protocol::MAIL && DI::userSession()->getLocalUserId() != $item->getDataValue('uid')) {
201                                 continue;
202                         }
203
204                         $item_data = $item->getTemplateData($conv_responses, $formSecurityToken);
205
206                         if (!$item_data) {
207                                 Logger::info('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').');
208                                 return false;
209                         }
210                         $result[] = $item_data;
211                 }
212
213                 return $result;
214         }
215
216         /**
217          * Get a thread based on its item id
218          *
219          * @param integer $id Item id
220          *
221          * @return mixed The found item on success
222          *               false on failure
223          */
224         private function getParent($id)
225         {
226                 foreach ($this->parents as $item) {
227                         if ($item->getId() == $id) {
228                                 return $item;
229                         }
230                 }
231
232                 return false;
233         }
234 }