From 3c027699a5e2c25a2dcfa7e192f7e0089c0592e8 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 20:52:43 +0000 Subject: [PATCH 1/7] Receive participation messages and processes them --- boot.php | 2 +- src/Database/DBStructure.php | 10 +++++ src/Protocol/Diaspora.php | 77 +++++++++++++++++++++++++++++++++++- src/Worker/Notifier.php | 8 +++- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/boot.php b/boot.php index b10ba7a63f..51594b2172 100644 --- a/boot.php +++ b/boot.php @@ -43,7 +43,7 @@ define('FRIENDICA_PLATFORM', 'Friendica'); define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_VERSION', '3.6-dev'); define('DFRN_PROTOCOL_VERSION', '2.23'); -define('DB_UPDATE_VERSION', 1240); +define('DB_UPDATE_VERSION', 1241); define('NEW_UPDATE_ROUTINE_VERSION', 1170); /** diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 11bf40aec4..6ceeb573fe 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -1299,6 +1299,16 @@ class DBStructure { "created" => array("created"), ) ); + $database["participation"] = array( + "fields" => array( + "item" => array("type" => "int(10) unsigned", "not null" => "1", "primary" => "1", "relation" => array("item" => "id")), + "contact" => array("type" => "int(10) unsigned", "not null" => "1", "relation" => array("contact" => "id")), + "server" => array("type" => "varchar(60)", "not null" => "1", "primary" => "1"), + ), + "indexes" => array( + "PRIMARY" => array("item", "server") + ) + ); $database["pconfig"] = array( "fields" => array( "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 23220d04cd..49134e2220 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -99,6 +99,54 @@ class Diaspora return $relay; } + /** + * @brief Return a list of participating contacts for a thread + * + * This is used for the participation feature. + * One of the parameters is a contact array. + * This is done to avoid duplicates. + * + * @param integer $thread The id of the thread + * @param array $contacts The previously fetched contacts + * + * @return array of relay servers + */ + public static function ParticipationsForThread($thread, $contacts) + { + $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch)); + $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, + `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` + INNER JOIN `contact` ON `contact`.`id` = `participation`.`contact` + LEFT JOIN `fcontact` ON `fcontact`.`url` = `contact`.`url` + WHERE `participation`.`item` = ?", $thread); + + while ($contact = dba::fetch($r)) { + if (!empty($contact['fnetwork'])) { + $contact['network'] = $contact['fnetwork']; + } + unset($contact['fnetwork']); + + if (empty($contact['batch']) && !empty($contact['fbatch'])) { + $contact['batch'] = $contact['fbatch']; + } + unset($contact['fbatch']); + + $exists = false; + foreach ($contacts as $entry) { + if ($entry['batch'] == $contact['batch']) { + $exists = true; + } + } + + if (!$exists) { + $contacts[] = $contact; + } + } + dba::close($r); + + return $contacts; + } + /** * @brief repairs a signature that was double encoded * @@ -542,7 +590,7 @@ class Diaspora case "message": return self::receiveMessage($importer, $fields); - case "participation": // Not implemented + case "participation": return self::receiveParticipation($importer, $fields); case "photo": // Not implemented @@ -2128,7 +2176,32 @@ class Diaspora */ private static function receiveParticipation($importer, $data) { - // I'm not sure if we can fully support this message type + $author = strtolower(notags(unxmlify($data->author))); + $parent_guid = notags(unxmlify($data->parent_guid)); + + $contact_id = Contact::getIdForURL($author); + if (!$contact_id) { + logger('Author not found: '.$author); + return false; + } + + $item = dba::selectFirst('item', ['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]); + if (!DBM::is_result($item)) { + logger('Item not found: '.$parent_guid); + return false; + } + + $author_parts = explode('@', $author); + if (isset($author_parts[1])) { + $server = $author_parts[1]; + } else { + // Should never happen + $server = $author; + } + + logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server); + dba::insert('participation', ['item' => $item['id'], 'contact' => $contact_id, 'server' => $server]); + return true; } diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 710bcfa13b..da6d45084e 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -522,6 +522,11 @@ class Notifier { intval($owner['uid']), intval(CONTACT_IS_SHARING) ); + + // Fetch the participation list + // The function will ensure that there are no duplicates + $r1 = Diaspora::ParticipationsForThread($item_id, $r1); + } $r2 = q("SELECT `id`, `name`,`network` FROM `contact` @@ -531,7 +536,8 @@ class Notifier { intval(CONTACT_IS_SHARING) ); - $r = array_merge($r2,$r1,$r0); + + $r = array_merge($r2, $r1, $r0); if (DBM::is_result($r)) { logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG); From 9e37b49beafebd9c5ecb5961dd77dd55c0d6375f Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 21:02:39 +0000 Subject: [PATCH 2/7] copy and paste ... --- src/Protocol/Diaspora.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 49134e2220..747a1b2e1f 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -113,7 +113,6 @@ class Diaspora */ public static function ParticipationsForThread($thread, $contacts) { - $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch)); $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` INNER JOIN `contact` ON `contact`.`id` = `participation`.`contact` From fc9453b7e99fd5b87fde4206086ab51b0b1ea9cb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 23:20:19 +0000 Subject: [PATCH 3/7] Corrected field names --- database.sql | 15 +++++++++++++-- src/Database/DBStructure.php | 6 +++--- src/Protocol/Diaspora.php | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/database.sql b/database.sql index 48c2e7ecee..79e81f1d22 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 3.6-dev (Asparagus) --- DB_UPDATE_VERSION 1239 +-- DB_UPDATE_VERSION 1241 -- ------------------------------------------ @@ -650,9 +650,10 @@ CREATE TABLE IF NOT EXISTS `notify-threads` ( -- CREATE TABLE IF NOT EXISTS `oembed` ( `url` varbinary(255) NOT NULL, + `maxwidth` int(11) NOT NULL, `content` mediumtext, `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00', - PRIMARY KEY(`url`), + PRIMARY KEY(`url`,`maxwidth`), INDEX `created` (`created`) ) DEFAULT COLLATE utf8mb4_general_ci; @@ -669,6 +670,16 @@ CREATE TABLE IF NOT EXISTS `parsed_url` ( INDEX `created` (`created`) ) DEFAULT COLLATE utf8mb4_general_ci; +-- +-- TABLE participation +-- +CREATE TABLE IF NOT EXISTS `participation` ( + `iid` int(10) unsigned NOT NULL, + `server` varchar(60) NOT NULL, + `cid` int(10) unsigned NOT NULL, + PRIMARY KEY(`iid`,`server`) +) DEFAULT COLLATE utf8mb4_general_ci; + -- -- TABLE pconfig -- diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 6ceeb573fe..2d9b4fc13b 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -1301,12 +1301,12 @@ class DBStructure { ); $database["participation"] = array( "fields" => array( - "item" => array("type" => "int(10) unsigned", "not null" => "1", "primary" => "1", "relation" => array("item" => "id")), - "contact" => array("type" => "int(10) unsigned", "not null" => "1", "relation" => array("contact" => "id")), + "iid" => array("type" => "int(10) unsigned", "not null" => "1", "primary" => "1", "relation" => array("item" => "id")), "server" => array("type" => "varchar(60)", "not null" => "1", "primary" => "1"), + "cid" => array("type" => "int(10) unsigned", "not null" => "1", "relation" => array("contact" => "id")), ), "indexes" => array( - "PRIMARY" => array("item", "server") + "PRIMARY" => array("iid", "server") ) ); $database["pconfig"] = array( diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 747a1b2e1f..b2a4389058 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -115,9 +115,9 @@ class Diaspora { $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` - INNER JOIN `contact` ON `contact`.`id` = `participation`.`contact` + INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid` LEFT JOIN `fcontact` ON `fcontact`.`url` = `contact`.`url` - WHERE `participation`.`item` = ?", $thread); + WHERE `participation`.`iid` = ?", $thread); while ($contact = dba::fetch($r)) { if (!empty($contact['fnetwork'])) { From 4c104a6cc273f72e90c6e2ba1eaeade0d59986b6 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 23:25:30 +0000 Subject: [PATCH 4/7] Added table definition --- doc/database.md | 1 + doc/database/db_particiation.md | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 doc/database/db_particiation.md diff --git a/doc/database.md b/doc/database.md index 00a40fb247..e778a0a721 100644 --- a/doc/database.md +++ b/doc/database.md @@ -35,6 +35,7 @@ Database Tables | [notify-threads](help/database/db_notify-threads) | | | [oembed](help/database/db_oembed) | cache for OEmbed queries | | [parsed_url](help/database/db_parsed_url) | cache for "parse_url" queries | +| [participation](help/database/db_participation) | Storage for participation messages from Diaspora | | [pconfig](help/database/db_pconfig) | personal (per user) configuration storage | | [photo](help/database/db_photo) | photo storage | | [poll](help/database/db_poll) | data for polls | diff --git a/doc/database/db_particiation.md b/doc/database/db_particiation.md new file mode 100644 index 0000000000..27f0632411 --- /dev/null +++ b/doc/database/db_particiation.md @@ -0,0 +1,10 @@ +Table participation +=================== + +| Field | Description | Type | Null | Key | Default | Extra | +|-------------|------------------|------------------|------|-----|---------------------|-------| +| iid | item id | int(10) unsigned | NO | PRI | | | +| server | Name of server | varchar(60) | NO | PRI | | | +| cid | contact id | int(10) unsigned | NO | | | | + +Return to [database documentation](help/database) From 06751470ce2e677b1d5f6e9da6286acf7a95e0ac Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 23:26:55 +0000 Subject: [PATCH 5/7] Changed case --- src/Protocol/Diaspora.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index b2a4389058..817a87ccb7 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -111,7 +111,7 @@ class Diaspora * * @return array of relay servers */ - public static function ParticipationsForThread($thread, $contacts) + public static function participationsForThread($thread, $contacts) { $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` From b638ed9e8676852fd98fc0b18ef1a2b85438c9d6 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 12 Jan 2018 23:43:08 +0000 Subject: [PATCH 6/7] Corrected function name --- src/Protocol/Diaspora.php | 2 +- src/Worker/Notifier.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 817a87ccb7..4d89143452 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -111,7 +111,7 @@ class Diaspora * * @return array of relay servers */ - public static function participationsForThread($thread, $contacts) + public static function participantsForThread($thread, $contacts) { $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index da6d45084e..307178bfbd 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -525,7 +525,7 @@ class Notifier { // Fetch the participation list // The function will ensure that there are no duplicates - $r1 = Diaspora::ParticipationsForThread($item_id, $r1); + $r1 = Diaspora::participantsForThread($item_id, $r1); } From a78cbf617468bd2d24ec7e61a42798ed5b5ef291 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Jan 2018 09:19:57 +0000 Subject: [PATCH 7/7] The changed field name has to be changed here as well --- src/Protocol/Diaspora.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 4d89143452..bf14b44d22 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -2199,7 +2199,7 @@ class Diaspora } logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server); - dba::insert('participation', ['item' => $item['id'], 'contact' => $contact_id, 'server' => $server]); + dba::insert('participation', ['iid' => $item['id'], 'cid' => $contact_id, 'server' => $server]); return true; }