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.
 
 
 
 
 
 

172 lines
3.8 KiB

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