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.

168 lines
3.8KB

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