3 * @file src/Object/Thread.php
5 namespace Friendica\Object;
7 use Friendica\BaseObject;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Object\Post;
11 use Friendica\Util\Security;
16 * We should think about making this a SPL Iterator
18 class Thread extends BaseObject
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
33 public function __construct($mode, $preview, $writable = false)
35 $this->setMode($mode, $writable);
36 $this->preview = $preview;
40 * Set the mode we'll be displayed on
42 * @param string $mode The mode to set
43 * @param boolean $writable Override the writable check
47 private function setMode($mode, $writable)
49 if ($this->getMode() == $mode) {
58 $this->profile_owner = local_user();
59 $this->writable = true;
62 $this->profile_owner = $a->profile['profile_uid'];
63 $this->writable = Security::canWriteToUserWall($this->profile_owner);
66 $this->profile_owner = $a->profile['uid'];
67 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
70 $this->profile_owner = 0;
71 $this->writable = $writable;
74 $this->profile_owner = 0;
75 $this->writable = $writable;
78 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
90 public function getMode()
96 * Check if page is writable
100 public function isWritable()
102 return $this->writable;
106 * Check if page is a preview
110 public function isPreview()
112 return $this->preview;
120 public function getProfileOwner()
122 return $this->profile_owner;
126 * Add a thread to the conversation
128 * @param object $item The item to insert
130 * @return mixed The inserted item on success
133 public function addParent(Post $item)
135 $item_id = $item->getId();
138 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
142 if ($this->getParent($item->getId())) {
143 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
148 * Only add will be displayed
150 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
151 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
155 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
156 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
160 $item->setThread($this);
161 $this->parents[] = $item;
163 return end($this->parents);
167 * Get data in a form usable by a conversation template
169 * We should find a way to avoid using those arguments (at least most of them)
171 * @param object $conv_responses data
173 * @return mixed The data requested on success
176 public function getTemplateData($conv_responses)
182 foreach ($this->parents as $item) {
183 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
187 $item_data = $item->getTemplateData($conv_responses);
190 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
193 $result[] = $item_data;
200 * Get a thread based on its item id
202 * @param integer $id Item id
204 * @return mixed The found item on success
207 private function getParent($id)
209 foreach ($this->parents as $item) {
210 if ($item->getId() == $id) {