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