3 * @copyright Copyright (C) 2020, Friendica
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\Core\Logger;
25 use Friendica\Core\Protocol;
27 use Friendica\Protocol\Activity;
28 use Friendica\Util\Security;
33 * We should think about making this a SPL Iterator
38 private $parents = [];
40 private $writable = false;
41 private $profile_owner = 0;
42 private $preview = false;
47 * @param string $mode The mode
48 * @param boolean $preview Are we in the preview mode?
49 * @param boolean $writable Override the writable check
52 public function __construct($mode, $preview, $writable = false)
54 $this->setMode($mode, $writable);
55 $this->preview = $preview;
59 * Set the mode we'll be displayed on
61 * @param string $mode The mode to set
62 * @param boolean $writable Override the writable check
67 private function setMode($mode, $writable)
69 if ($this->getMode() == $mode) {
78 $this->profile_owner = local_user();
79 $this->writable = true;
82 $this->profile_owner = $a->profile['uid'];
83 $this->writable = Security::canWriteToUserWall($this->profile_owner);
86 $this->profile_owner = $a->profile['uid'];
87 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
90 $this->profile_owner = 0;
91 $this->writable = $writable;
94 $this->profile_owner = 0;
95 $this->writable = $writable;
98 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
110 public function getMode()
116 * Check if page is writable
120 public function isWritable()
122 return $this->writable;
126 * Check if page is a preview
130 public function isPreview()
132 return $this->preview;
140 public function getProfileOwner()
142 return $this->profile_owner;
146 * Add a thread to the conversation
148 * @param Post $item The item to insert
150 * @return mixed The inserted item on success
154 public function addParent(Post $item)
156 $item_id = $item->getId();
159 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
163 if ($this->getParent($item->getId())) {
164 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
169 * Only add will be displayed
171 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
172 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
176 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
177 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
181 $item->setThread($this);
182 $this->parents[] = $item;
184 return end($this->parents);
188 * Get data in a form usable by a conversation template
190 * We should find a way to avoid using those arguments (at least most of them)
192 * @param array $conv_responses data
194 * @return mixed The data requested on success
198 public function getTemplateData($conv_responses)
202 foreach ($this->parents as $item) {
203 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
207 $item_data = $item->getTemplateData($conv_responses);
210 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
213 $result[] = $item_data;
220 * Get a thread based on its item id
222 * @param integer $id Item id
224 * @return mixed The found item on success
227 private function getParent($id)
229 foreach ($this->parents as $item) {
230 if ($item->getId() == $id) {