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\Util\Security;
15 * We should think about making this a SPL Iterator
17 class Thread extends BaseObject
19 private $parents = [];
21 private $writable = false;
22 private $profile_owner = 0;
23 private $preview = false;
28 * @param string $mode The mode
29 * @param boolean $preview Are we in the preview mode?
30 * @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
48 private function setMode($mode, $writable)
50 if ($this->getMode() == $mode) {
59 $this->profile_owner = local_user();
60 $this->writable = true;
63 $this->profile_owner = $a->profile['profile_uid'];
64 $this->writable = Security::canWriteToUserWall($this->profile_owner);
67 $this->profile_owner = $a->profile['uid'];
68 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
71 $this->profile_owner = 0;
72 $this->writable = $writable;
75 $this->profile_owner = 0;
76 $this->writable = $writable;
79 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
91 public function getMode()
97 * Check if page is writable
101 public function isWritable()
103 return $this->writable;
107 * Check if page is a preview
111 public function isPreview()
113 return $this->preview;
121 public function getProfileOwner()
123 return $this->profile_owner;
127 * Add a thread to the conversation
129 * @param Post $item The item to insert
131 * @return mixed The inserted item on success
135 public function addParent(Post $item)
137 $item_id = $item->getId();
140 Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
144 if ($this->getParent($item->getId())) {
145 Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
150 * Only add will be displayed
152 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
153 Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
157 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
158 Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
162 $item->setThread($this);
163 $this->parents[] = $item;
165 return end($this->parents);
169 * Get data in a form usable by a conversation template
171 * We should find a way to avoid using those arguments (at least most of them)
173 * @param array $conv_responses data
175 * @return mixed The data requested on success
179 public function getTemplateData($conv_responses)
183 foreach ($this->parents as $item) {
184 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
188 $item_data = $item->getTemplateData($conv_responses);
191 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
194 $result[] = $item_data;
201 * Get a thread based on its item id
203 * @param integer $id Item id
205 * @return mixed The found item on success
208 private function getParent($id)
210 foreach ($this->parents as $item) {
211 if ($item->getId() == $id) {