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 case Conversation::MODE_DISPLAY:
84 $this->profile_owner = $a->getProfileOwner();
85 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
87 case Conversation::MODE_CHANNEL:
88 case Conversation::MODE_COMMUNITY:
89 case Conversation::MODE_CONTACTS:
90 $this->profile_owner = 0;
91 $this->writable = $writable;
94 Logger::info('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').');
106 public function getMode()
112 * Check if page is writable
116 public function isWritable()
118 return $this->writable;
122 * Check if page is a preview
126 public function isPreview()
128 return $this->preview;
136 public function getProfileOwner()
138 return $this->profile_owner;
142 * Add a thread to the conversation
144 * @param Post $item The item to insert
146 * @return mixed The inserted item on success
150 public function addParent(Post $item)
152 $item_id = $item->getId();
155 Logger::info('[ERROR] Conversation::addThread : Item has no ID!!');
159 if ($this->getParent($item->getId())) {
160 Logger::info('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').');
165 * Only add will be displayed
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() .').');
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() .').');
177 $item->setThread($this);
178 $this->parents[] = $item;
180 return end($this->parents);
184 * Get data in a form usable by a conversation template
186 * We should find a way to avoid using those arguments (at least most of them)
188 * @param array $conv_responses data
189 * @param string $formSecurityToken A 'contact_action' form security token
191 * @return mixed The data requested on success
195 public function getTemplateData(array $conv_responses, string $formSecurityToken)
199 foreach ($this->parents as $item) {
200 if ($item->getDataValue('network') === Protocol::MAIL && DI::userSession()->getLocalUserId() != $item->getDataValue('uid')) {
204 $item_data = $item->getTemplateData($conv_responses, $formSecurityToken);
207 Logger::info('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').');
210 $result[] = $item_data;
217 * Get a thread based on its item id
219 * @param integer $id Item id
221 * @return mixed The found item on success
224 private function getParent($id)
226 foreach ($this->parents as $item) {
227 if ($item->getId() == $id) {