3 * @file src/Object/Thread.php
5 namespace Friendica\Object;
7 use Friendica\BaseObject;
8 use Friendica\Core\Protocol;
9 use Friendica\Object\Post;
10 use Friendica\Util\Security;
12 require_once 'boot.php';
13 require_once 'include/text.php';
18 * We should think about making this a SPL Iterator
20 class Thread extends BaseObject
22 private $parents = [];
24 private $writable = false;
25 private $profile_owner = 0;
26 private $preview = false;
31 * @param string $mode The mode
32 * @param boolean $preview Are we in the preview mode?
33 * @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
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['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('[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 object $item The item to insert
132 * @return mixed The inserted item on success
135 public function addParent(Post $item)
137 $item_id = $item->getId();
140 logger('[ERROR] Conversation::addThread : Item has no ID!!', LOGGER_DEBUG);
144 if ($this->getParent($item->getId())) {
145 logger('[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('[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('[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 object $conv_responses data
175 * @return mixed The data requested on success
178 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('[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) {