3 * @file src/Model/Conversation
5 namespace Friendica\Model;
7 use Friendica\Database\DBA;
8 use Friendica\Util\DateTimeFormat;
10 require_once "include/dba.php";
15 * These constants represent the parcel format used to transport a conversation independently of the message protocol.
16 * It currently is stored in the "protocol" field for legacy reasons.
18 const PARCEL_UNKNOWN = 0;
19 const PARCEL_DFRN = 1;
20 const PARCEL_DIASPORA = 2;
21 const PARCEL_SALMON = 3;
22 const PARCEL_FEED = 4; // Deprecated
23 const PARCEL_SPLIT_CONVERSATION = 6;
26 * @brief Store the conversation data
28 * @param array $arr Item array with conversation data
29 * @return array Item array with removed conversation data
31 public static function insert($arr) {
32 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
33 $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
35 if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
36 $conversation['reply-to-uri'] = $arr['parent-uri'];
38 if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
39 $conversation['reply-to-uri'] = $arr['thr-parent'];
42 if (isset($arr['conversation-uri'])) {
43 $conversation['conversation-uri'] = $arr['conversation-uri'];
46 if (isset($arr['conversation-href'])) {
47 $conversation['conversation-href'] = $arr['conversation-href'];
50 if (isset($arr['protocol'])) {
51 $conversation['protocol'] = $arr['protocol'];
54 if (isset($arr['source'])) {
55 $conversation['source'] = $arr['source'];
58 $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
59 $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
60 if (DBA::isResult($old_conv)) {
61 // Don't update when only the source has changed.
62 // Only do this when there had been no source before.
63 if ($old_conv['source'] != '') {
64 unset($old_conv['source']);
66 // Update structure data all the time but the source only when its from a better protocol.
67 if (isset($conversation['protocol']) && isset($conversation['source']) && ($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
68 unset($conversation['protocol']);
69 unset($conversation['source']);
71 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
72 logger('Conversation: update for '.$conversation['item-uri'].' from '.$old_conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
75 if (!DBA::insert('conversation', $conversation, true)) {
76 logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
81 unset($arr['conversation-uri']);
82 unset($arr['conversation-href']);
83 unset($arr['protocol']);
84 unset($arr['source']);