3 * @file src/Object/Thread.php
5 namespace Friendica\Object;
7 use Friendica\BaseObject;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
11 use Friendica\Protocol\Activity;
12 use Friendica\Util\Security;
17 * We should think about making this a SPL Iterator
19 class Thread extends BaseObject
21 private $parents = [];
23 private $writable = false;
24 private $profile_owner = 0;
25 private $preview = false;
30 * @param string $mode The mode
31 * @param boolean $preview Are we in the preview mode?
32 * @param boolean $writable Override the writable check
35 public function __construct($mode, $preview, $writable = false)
37 $this->setMode($mode, $writable);
38 $this->preview = $preview;
42 * Set the mode we'll be displayed on
44 * @param string $mode The mode to set
45 * @param boolean $writable Override the writable check
50 private function setMode($mode, $writable)
52 if ($this->getMode() == $mode) {
61 $this->profile_owner = local_user();
62 $this->writable = true;
65 $this->profile_owner = $a->profile['profile_uid'];
66 $this->writable = Security::canWriteToUserWall($this->profile_owner);
69 $this->profile_owner = $a->profile['uid'];
70 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
73 $this->profile_owner = 0;
74 $this->writable = $writable;
77 $this->profile_owner = 0;
78 $this->writable = $writable;
81 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
93 public function getMode()
99 * Check if page is writable
103 public function isWritable()
105 return $this->writable;
109 * Check if page is a preview
113 public function isPreview()
115 return $this->preview;
123 public function getProfileOwner()
125 return $this->profile_owner;
129 * Add a thread to the conversation
131 * @param Post $item The item to insert
133 * @return mixed The inserted item on success
137 public function addParent(Post $item)
139 $item_id = $item->getId();
142 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
146 if ($this->getParent($item->getId())) {
147 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
152 * Only add will be displayed
154 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
155 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
159 if ($item->getDataValue('verb') === Activity::LIKE || $item->getDataValue('verb') === Activity::DISLIKE) {
160 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
164 $item->setThread($this);
165 $this->parents[] = $item;
167 return end($this->parents);
171 * Get data in a form usable by a conversation template
173 * We should find a way to avoid using those arguments (at least most of them)
175 * @param array $conv_responses data
177 * @return mixed The data requested on success
181 public function getTemplateData($conv_responses)
185 foreach ($this->parents as $item) {
186 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
190 $item_data = $item->getTemplateData($conv_responses);
193 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
196 $result[] = $item_data;
203 * Get a thread based on its item id
205 * @param integer $id Item id
207 * @return mixed The found item on success
210 private function getParent($id)
212 foreach ($this->parents as $item) {
213 if ($item->getId() == $id) {