3 * @file src/Model/Conversation
5 namespace Friendica\Model;
7 use Friendica\Database\DBM;
10 require_once "include/dba.php";
14 const PROTOCOL_UNKNOWN = 0;
15 const PROTOCOL_DFRN = 1;
16 const PROTOCOL_DIASPORA = 2;
17 const PROTOCOL_OSTATUS_SALMON = 3;
18 const PROTOCOL_OSTATUS_FEED = 4; // Deprecated
19 const PROTOCOL_GS_CONVERSATION = 5; // Deprecated
20 const PROTOCOL_SPLITTED_CONV = 6;
23 * @brief Store the conversation data
25 * @param array $arr Item array with conversation data
26 * @return array Item array with removed conversation data
28 public static function insert($arr) {
29 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
30 $conversation = ['item-uri' => $arr['uri'], 'received' => DBM::date()];
32 if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
33 $conversation['reply-to-uri'] = $arr['parent-uri'];
35 if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
36 $conversation['reply-to-uri'] = $arr['thr-parent'];
39 if (isset($arr['conversation-uri'])) {
40 $conversation['conversation-uri'] = $arr['conversation-uri'];
43 if (isset($arr['conversation-href'])) {
44 $conversation['conversation-href'] = $arr['conversation-href'];
47 if (isset($arr['protocol'])) {
48 $conversation['protocol'] = $arr['protocol'];
51 if (isset($arr['source'])) {
52 $conversation['source'] = $arr['source'];
55 $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
56 FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
57 if (DBM::is_result($old_conv)) {
58 // Don't update when only the source has changed.
59 // Only do this when there had been no source before.
60 if ($old_conv['source'] != '') {
61 unset($old_conv['source']);
63 // Update structure data all the time but the source only when its from a better protocol.
64 if (($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
65 unset($conversation['protocol']);
66 unset($conversation['source']);
68 if (!dba::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
69 logger('Conversation: update for '.$conversation['item-uri'].' from '.$old_conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
72 if (!dba::insert('conversation', $conversation, true)) {
73 logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
78 unset($arr['conversation-uri']);
79 unset($arr['conversation-href']);
80 unset($arr['protocol']);
81 unset($arr['source']);