3 * @file src/Model/Conversation
5 namespace Friendica\Model;
7 use Friendica\Database\DBA;
8 use Friendica\Database\DBM;
9 use Friendica\Util\DateTimeFormat;
11 require_once "include/dba.php";
15 const PROTOCOL_UNKNOWN = 0;
16 const PROTOCOL_DFRN = 1;
17 const PROTOCOL_DIASPORA = 2;
18 const PROTOCOL_OSTATUS_SALMON = 3;
19 const PROTOCOL_OSTATUS_FEED = 4; // Deprecated
20 const PROTOCOL_GS_CONVERSATION = 5; // Deprecated
21 const PROTOCOL_SPLITTED_CONV = 6;
24 * @brief Store the conversation data
26 * @param array $arr Item array with conversation data
27 * @return array Item array with removed conversation data
29 public static function insert($arr) {
30 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
31 $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
33 if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
34 $conversation['reply-to-uri'] = $arr['parent-uri'];
36 if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
37 $conversation['reply-to-uri'] = $arr['thr-parent'];
40 if (isset($arr['conversation-uri'])) {
41 $conversation['conversation-uri'] = $arr['conversation-uri'];
44 if (isset($arr['conversation-href'])) {
45 $conversation['conversation-href'] = $arr['conversation-href'];
48 if (isset($arr['protocol'])) {
49 $conversation['protocol'] = $arr['protocol'];
52 if (isset($arr['source'])) {
53 $conversation['source'] = $arr['source'];
56 $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
57 $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
58 if (DBM::is_result($old_conv)) {
59 // Don't update when only the source has changed.
60 // Only do this when there had been no source before.
61 if ($old_conv['source'] != '') {
62 unset($old_conv['source']);
64 // Update structure data all the time but the source only when its from a better protocol.
65 if (isset($conversation['protocol']) && isset($conversation['source']) && ($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
66 unset($conversation['protocol']);
67 unset($conversation['source']);
69 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
70 logger('Conversation: update for '.$conversation['item-uri'].' from '.$old_conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
73 if (!DBA::insert('conversation', $conversation, true)) {
74 logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
79 unset($arr['conversation-uri']);
80 unset($arr['conversation-href']);
81 unset($arr['protocol']);
82 unset($arr['source']);