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.
 
 
 
 
 
 

208 line
4.2 KiB

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