3 * @file src/Model/Conversation
5 namespace Friendica\Model;
7 use Friendica\Database\DBM;
10 require_once "include/dba.php";
15 * @brief Store the conversation data
17 * @param array $arr Item array with conversation data
18 * @return array Item array with removed conversation data
20 public static function insert($arr) {
21 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
22 $conversation = ['item-uri' => $arr['uri'], 'received' => DBM::date()];
24 if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
25 $conversation['reply-to-uri'] = $arr['parent-uri'];
27 if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
28 $conversation['reply-to-uri'] = $arr['thr-parent'];
31 if (isset($arr['conversation-uri'])) {
32 $conversation['conversation-uri'] = $arr['conversation-uri'];
35 if (isset($arr['conversation-href'])) {
36 $conversation['conversation-href'] = $arr['conversation-href'];
39 if (isset($arr['protocol'])) {
40 $conversation['protocol'] = $arr['protocol'];
43 if (isset($arr['source'])) {
44 $conversation['source'] = $arr['source'];
47 $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
48 FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
49 if (DBM::is_result($old_conv)) {
50 // Don't update when only the source has changed.
51 // Only do this when there had been no source before.
52 if ($old_conv['source'] != '') {
53 unset($old_conv['source']);
55 // Update structure data all the time but the source only when its from a better protocol.
56 if (($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
57 unset($conversation['protocol']);
58 unset($conversation['source']);
60 if (!dba::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
61 logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
64 if (!dba::insert('conversation', $conversation, true)) {
65 logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
70 unset($arr['conversation-uri']);
71 unset($arr['conversation-href']);
72 unset($arr['protocol']);
73 unset($arr['source']);