Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
4.6KB

  1. <?php
  2. /**
  3. * @file src/Object/Thread.php
  4. */
  5. namespace Friendica\Object;
  6. use Friendica\BaseObject;
  7. use Friendica\Core\Logger;
  8. use Friendica\Core\Protocol;
  9. use Friendica\Object\Post;
  10. use Friendica\Util\Security;
  11. /**
  12. * A list of threads
  13. *
  14. * We should think about making this a SPL Iterator
  15. */
  16. class Thread extends BaseObject
  17. {
  18. private $parents = [];
  19. private $mode = null;
  20. private $writable = false;
  21. private $profile_owner = 0;
  22. private $preview = false;
  23. /**
  24. * Constructor
  25. *
  26. * @param string $mode The mode
  27. * @param boolean $preview Are we in the preview mode?
  28. * @param boolean $writable Override the writable check
  29. */
  30. public function __construct($mode, $preview, $writable = false)
  31. {
  32. $this->setMode($mode, $writable);
  33. $this->preview = $preview;
  34. }
  35. /**
  36. * Set the mode we'll be displayed on
  37. *
  38. * @param string $mode The mode to set
  39. * @param boolean $writable Override the writable check
  40. *
  41. * @return void
  42. */
  43. private function setMode($mode, $writable)
  44. {
  45. if ($this->getMode() == $mode) {
  46. return;
  47. }
  48. $a = self::getApp();
  49. switch ($mode) {
  50. case 'network':
  51. case 'notes':
  52. $this->profile_owner = local_user();
  53. $this->writable = true;
  54. break;
  55. case 'profile':
  56. $this->profile_owner = $a->profile['profile_uid'];
  57. $this->writable = Security::canWriteToUserWall($this->profile_owner);
  58. break;
  59. case 'display':
  60. $this->profile_owner = $a->profile['uid'];
  61. $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
  62. break;
  63. case 'community':
  64. $this->profile_owner = 0;
  65. $this->writable = $writable;
  66. break;
  67. case 'contacts':
  68. $this->profile_owner = 0;
  69. $this->writable = $writable;
  70. break;
  71. default:
  72. Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
  73. return false;
  74. break;
  75. }
  76. $this->mode = $mode;
  77. }
  78. /**
  79. * Get mode
  80. *
  81. * @return string
  82. */
  83. public function getMode()
  84. {
  85. return $this->mode;
  86. }
  87. /**
  88. * Check if page is writable
  89. *
  90. * @return boolean
  91. */
  92. public function isWritable()
  93. {
  94. return $this->writable;
  95. }
  96. /**
  97. * Check if page is a preview
  98. *
  99. * @return boolean
  100. */
  101. public function isPreview()
  102. {
  103. return $this->preview;
  104. }
  105. /**
  106. * Get profile owner
  107. *
  108. * @return integer
  109. */
  110. public function getProfileOwner()
  111. {
  112. return $this->profile_owner;
  113. }
  114. /**
  115. * Add a thread to the conversation
  116. *
  117. * @param object $item The item to insert
  118. *
  119. * @return mixed The inserted item on success
  120. * false on failure
  121. */
  122. public function addParent(Post $item)
  123. {
  124. $item_id = $item->getId();
  125. if (!$item_id) {
  126. Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
  127. return false;
  128. }
  129. if ($this->getParent($item->getId())) {
  130. Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
  131. return false;
  132. }
  133. /*
  134. * Only add will be displayed
  135. */
  136. if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
  137. Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
  138. return false;
  139. }
  140. if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
  141. Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
  142. return false;
  143. }
  144. $item->setThread($this);
  145. $this->parents[] = $item;
  146. return end($this->parents);
  147. }
  148. /**
  149. * Get data in a form usable by a conversation template
  150. *
  151. * We should find a way to avoid using those arguments (at least most of them)
  152. *
  153. * @param object $conv_responses data
  154. *
  155. * @return mixed The data requested on success
  156. * false on failure
  157. */
  158. public function getTemplateData($conv_responses)
  159. {
  160. $a = self::getApp();
  161. $result = [];
  162. $i = 0;
  163. foreach ($this->parents as $item) {
  164. if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
  165. continue;
  166. }
  167. $item_data = $item->getTemplateData($conv_responses);
  168. if (!$item_data) {
  169. Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
  170. return false;
  171. }
  172. $result[] = $item_data;
  173. }
  174. return $result;
  175. }
  176. /**
  177. * Get a thread based on its item id
  178. *
  179. * @param integer $id Item id
  180. *
  181. * @return mixed The found item on success
  182. * false on failure
  183. */
  184. private function getParent($id)
  185. {
  186. foreach ($this->parents as $item) {
  187. if ($item->getId() == $id) {
  188. return $item;
  189. }
  190. }
  191. return false;
  192. }
  193. }