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.

103 lines
3.4KB

  1. <?php
  2. /**
  3. * @file src/Model/Conversation
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\Core\Logger;
  7. use Friendica\Core\Protocol;
  8. use Friendica\Database\DBA;
  9. use Friendica\Util\DateTimeFormat;
  10. class Conversation
  11. {
  12. /*
  13. * These constants represent the parcel format used to transport a conversation independently of the message protocol.
  14. * It currently is stored in the "protocol" field for legacy reasons.
  15. */
  16. const PARCEL_ACTIVITYPUB = 0;
  17. const PARCEL_DFRN = 1;
  18. const PARCEL_DIASPORA = 2;
  19. const PARCEL_SALMON = 3;
  20. const PARCEL_FEED = 4; // Deprecated
  21. const PARCEL_SPLIT_CONVERSATION = 6;
  22. const PARCEL_TWITTER = 67;
  23. const PARCEL_UNKNOWN = 255;
  24. public static function getByItemUri($item_uri)
  25. {
  26. return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]);
  27. }
  28. /**
  29. * @brief Store the conversation data
  30. *
  31. * @param array $arr Item array with conversation data
  32. * @return array Item array with removed conversation data
  33. */
  34. public static function insert(array $arr)
  35. {
  36. if (in_array(defaults($arr, 'network', Protocol::PHANTOM),
  37. [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) {
  38. $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
  39. if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
  40. $conversation['reply-to-uri'] = $arr['parent-uri'];
  41. }
  42. if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
  43. $conversation['reply-to-uri'] = $arr['thr-parent'];
  44. }
  45. if (isset($arr['conversation-uri'])) {
  46. $conversation['conversation-uri'] = $arr['conversation-uri'];
  47. }
  48. if (isset($arr['conversation-href'])) {
  49. $conversation['conversation-href'] = $arr['conversation-href'];
  50. }
  51. if (isset($arr['protocol'])) {
  52. $conversation['protocol'] = $arr['protocol'];
  53. }
  54. if (isset($arr['source'])) {
  55. $conversation['source'] = $arr['source'];
  56. }
  57. $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
  58. $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
  59. if (DBA::isResult($old_conv)) {
  60. // Don't update when only the source has changed.
  61. // Only do this when there had been no source before.
  62. if ($old_conv['source'] != '') {
  63. unset($old_conv['source']);
  64. }
  65. // Update structure data all the time but the source only when its from a better protocol.
  66. if (empty($conversation['source']) || (!empty($old_conv['source']) &&
  67. ($old_conv['protocol'] < defaults($conversation, 'protocol', PARCEL_UNKNOWN)))) {
  68. unset($conversation['protocol']);
  69. unset($conversation['source']);
  70. }
  71. if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
  72. Logger::log('Conversation: update for ' . $conversation['item-uri'] . ' from ' . $old_conv['protocol'] . ' to ' . $conversation['protocol'] . ' failed',
  73. Logger::DEBUG);
  74. }
  75. } else {
  76. if (!DBA::insert('conversation', $conversation, true)) {
  77. Logger::log('Conversation: insert for ' . $conversation['item-uri'] . ' (protocol ' . $conversation['protocol'] . ') failed',
  78. Logger::DEBUG);
  79. }
  80. }
  81. }
  82. unset($arr['conversation-uri']);
  83. unset($arr['conversation-href']);
  84. unset($arr['protocol']);
  85. unset($arr['source']);
  86. return $arr;
  87. }
  88. }