3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Object;
24 use Friendica\Content\Conversation;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
28 use Friendica\Protocol\Activity;
29 use Friendica\Security\Security;
34 * We should think about making this a SPL Iterator
39 private $parents = [];
41 private $writable = false;
42 private $profile_owner = 0;
43 private $preview = false;
48 * @param string $mode The mode
49 * @param boolean $preview Are we in the preview mode?
50 * @param boolean $writable Override the writable check
53 public function __construct($mode, $preview, $writable = false)
55 $this->setMode($mode, $writable);
56 $this->preview = $preview;
60 * Set the mode we'll be displayed on
62 * @param string $mode The mode to set
63 * @param boolean $writable Override the writable check
68 private function setMode($mode, $writable)
70 if ($this->getMode() == $mode) {
77 case Conversation::MODE_NETWORK:
78 case Conversation::MODE_NOTES:
79 $this->profile_owner = DI::userSession()->getLocalUserId();
80 $this->writable = true;
82 case Conversation::MODE_PROFILE:
83 $this->profile_owner = $a->getProfileOwner();
84 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
86 case Conversation::MODE_DISPLAY:
87 $this->profile_owner = $a->getProfileOwner();
88 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
90 case Conversation::MODE_CHANNEL:
91 $this->profile_owner = 0;
92 $this->writable = $writable;
94 case Conversation::MODE_COMMUNITY:
95 $this->profile_owner = 0;
96 $this->writable = $writable;
98 case Conversation::MODE_CONTACTS:
99 $this->profile_owner = 0;
100 $this->writable = $writable;
103 Logger::info('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').');
115 public function getMode()
121 * Check if page is writable
125 public function isWritable()
127 return $this->writable;
131 * Check if page is a preview
135 public function isPreview()
137 return $this->preview;
145 public function getProfileOwner()
147 return $this->profile_owner;
151 * Add a thread to the conversation
153 * @param Post $item The item to insert
155 * @return mixed The inserted item on success
159 public function addParent(Post $item)
161 $item_id = $item->getId();
164 Logger::info('[ERROR] Conversation::addThread : Item has no ID!!');
168 if ($this->getParent($item->getId())) {
169 Logger::info('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').');
174 * Only add will be displayed
176 if ($item->getDataValue('network') === Protocol::MAIL && DI::userSession()->getLocalUserId() != $item->getDataValue('uid')) {
177 Logger::info('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').');
181 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
182 Logger::info('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').');
186 $item->setThread($this);
187 $this->parents[] = $item;
189 return end($this->parents);
193 * Get data in a form usable by a conversation template
195 * We should find a way to avoid using those arguments (at least most of them)
197 * @param array $conv_responses data
198 * @param string $formSecurityToken A 'contact_action' form security token
200 * @return mixed The data requested on success
204 public function getTemplateData(array $conv_responses, string $formSecurityToken)
208 foreach ($this->parents as $item) {
209 if ($item->getDataValue('network') === Protocol::MAIL && DI::userSession()->getLocalUserId() != $item->getDataValue('uid')) {
213 $item_data = $item->getTemplateData($conv_responses, $formSecurityToken);
216 Logger::info('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').');
219 $result[] = $item_data;
226 * Get a thread based on its item id
228 * @param integer $id Item id
230 * @return mixed The found item on success
233 private function getParent($id)
235 foreach ($this->parents as $item) {
236 if ($item->getId() == $id) {