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
37 private $parents = [];
39 private $writable = false;
40 private $profile_owner = 0;
41 private $preview = false;
46 * @param string $mode The mode
47 * @param boolean $preview Are we in the preview mode?
48 * @param boolean $writable Override the writable check
51 public function __construct($mode, $preview, $writable = false)
53 $this->setMode($mode, $writable);
54 $this->preview = $preview;
58 * Set the mode we'll be displayed on
60 * @param string $mode The mode to set
61 * @param boolean $writable Override the writable check
66 private function setMode($mode, $writable)
68 if ($this->getMode() == $mode) {
77 $this->profile_owner = local_user();
78 $this->writable = true;
81 $this->profile_owner = $a->profile['uid'];
82 $this->writable = Security::canWriteToUserWall($this->profile_owner);
85 $this->profile_owner = $a->profile['uid'];
86 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
89 $this->profile_owner = 0;
90 $this->writable = $writable;
93 $this->profile_owner = 0;
94 $this->writable = $writable;
97 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
109 public function getMode()
115 * Check if page is writable
119 public function isWritable()
121 return $this->writable;
125 * Check if page is a preview
129 public function isPreview()
131 return $this->preview;
139 public function getProfileOwner()
141 return $this->profile_owner;
145 * Add a thread to the conversation
147 * @param Post $item The item to insert
149 * @return mixed The inserted item on success
153 public function addParent(Post $item)
155 $item_id = $item->getId();
158 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
162 if ($this->getParent($item->getId())) {
163 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
168 * Only add will be displayed
170 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
171 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
175 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
176 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
180 $item->setThread($this);
181 $this->parents[] = $item;
183 return end($this->parents);
187 * Get data in a form usable by a conversation template
189 * We should find a way to avoid using those arguments (at least most of them)
191 * @param array $conv_responses data
193 * @return mixed The data requested on success
197 public function getTemplateData($conv_responses)
201 foreach ($this->parents as $item) {
202 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
206 $item_data = $item->getTemplateData($conv_responses);
209 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
212 $result[] = $item_data;
219 * Get a thread based on its item id
221 * @param integer $id Item id
223 * @return mixed The found item on success
226 private function getParent($id)
228 foreach ($this->parents as $item) {
229 if ($item->getId() == $id) {