3 * @file src/Object/Thread.php
5 namespace Friendica\Object;
7 use Friendica\Core\Logger;
8 use Friendica\Core\Protocol;
10 use Friendica\Protocol\Activity;
11 use Friendica\Util\Security;
16 * We should think about making this a SPL Iterator
20 private $parents = [];
22 private $writable = false;
23 private $profile_owner = 0;
24 private $preview = false;
29 * @param string $mode The mode
30 * @param boolean $preview Are we in the preview mode?
31 * @param boolean $writable Override the writable check
34 public function __construct($mode, $preview, $writable = false)
36 $this->setMode($mode, $writable);
37 $this->preview = $preview;
41 * Set the mode we'll be displayed on
43 * @param string $mode The mode to set
44 * @param boolean $writable Override the writable check
49 private function setMode($mode, $writable)
51 if ($this->getMode() == $mode) {
60 $this->profile_owner = local_user();
61 $this->writable = true;
64 $this->profile_owner = $a->profile['uid'];
65 $this->writable = Security::canWriteToUserWall($this->profile_owner);
68 $this->profile_owner = $a->profile['uid'];
69 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
72 $this->profile_owner = 0;
73 $this->writable = $writable;
76 $this->profile_owner = 0;
77 $this->writable = $writable;
80 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
92 public function getMode()
98 * Check if page is writable
102 public function isWritable()
104 return $this->writable;
108 * Check if page is a preview
112 public function isPreview()
114 return $this->preview;
122 public function getProfileOwner()
124 return $this->profile_owner;
128 * Add a thread to the conversation
130 * @param Post $item The item to insert
132 * @return mixed The inserted item on success
136 public function addParent(Post $item)
138 $item_id = $item->getId();
141 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
145 if ($this->getParent($item->getId())) {
146 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
151 * Only add will be displayed
153 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
154 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
158 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
159 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
163 $item->setThread($this);
164 $this->parents[] = $item;
166 return end($this->parents);
170 * Get data in a form usable by a conversation template
172 * We should find a way to avoid using those arguments (at least most of them)
174 * @param array $conv_responses data
176 * @return mixed The data requested on success
180 public function getTemplateData($conv_responses)
184 foreach ($this->parents as $item) {
185 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
189 $item_data = $item->getTemplateData($conv_responses);
192 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
195 $result[] = $item_data;
202 * Get a thread based on its item id
204 * @param integer $id Item id
206 * @return mixed The found item on success
209 private function getParent($id)
211 foreach ($this->parents as $item) {
212 if ($item->getId() == $id) {