Merge pull request #11025 from MrPetovan/task/11022-improve-connector-hooks

Improve connector hooks
This commit is contained in:
Philipp 2021-11-27 12:37:47 +01:00 committed by GitHub
commit a5ab184fb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 46 deletions

View file

@ -477,7 +477,18 @@ Hook data:
- **uri** (input): the profile URI. - **uri** (input): the profile URI.
- **network** (input): the target network (can be empty for auto-detection). - **network** (input): the target network (can be empty for auto-detection).
- **uid** (input): the user to return the contact data for (can be empty for public contacts). - **uid** (input): the user to return the contact data for (can be empty for public contacts).
- **result** (output): Set by the hook function to indicate a successful detection. - **result** (output): Leave null if address isn't relevant to the connector, set to contact array if probe is successful, false otherwise.
### item_by_link
Called when trying to probe an item from a given URI.
If any registered hook function sets the `item_id` key of the hook data array, it will be returned immediately.
Hook functions should also return immediately if the hook data contains an existing `item_id`.
Hook data:
- **uri** (input): the item URI.
- **uid** (input): the user to return the item data for (can be empty for public contacts).
- **item_id** (output): Leave null if URI isn't relevant to the connector, set to created item array if probe is successful, false otherwise.
### support_follow ### support_follow

View file

@ -3262,6 +3262,18 @@ class Item
return $item_id; return $item_id;
} }
$hookData = [
'uri' => $uri,
'uid' => $uid,
'item_id' => null,
];
Hook::callAll('item_by_link', $hookData);
if (isset($hookData['item_id'])) {
return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
}
if ($fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri)) { if ($fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri)) {
$item_id = self::searchByLink($fetched_uri, $uid); $item_id = self::searchByLink($fetched_uri, $uid);
} else { } else {

View file

@ -682,26 +682,18 @@ class Probe
'uri' => $uri, 'uri' => $uri,
'network' => $network, 'network' => $network,
'uid' => $uid, 'uid' => $uid,
'result' => [], 'result' => null,
]; ];
Hook::callAll('probe_detect', $hookData); Hook::callAll('probe_detect', $hookData);
if ($hookData['result']) { if (isset($hookData['result'])) {
if (!is_array($hookData['result'])) { return is_array($hookData['result']) ? $hookData['result'] : [];
return [];
} else {
return $hookData['result'];
}
} }
$parts = parse_url($uri); $parts = parse_url($uri);
if (!empty($parts['scheme']) && !empty($parts['host'])) { if (empty($parts['scheme']) || !empty($parts['host']) && strstr($uri, '@')) {
if (in_array($parts['host'], ['twitter.com', 'mobile.twitter.com'])) {
return self::twitter($uri);
}
} elseif (strstr($uri, '@')) {
// If the URI starts with "mailto:" then jump directly to the mail detection // If the URI starts with "mailto:" then jump directly to the mail detection
if (strpos($uri, 'mailto:') !== false) { if (strpos($uri, 'mailto:') !== false) {
$uri = str_replace('mailto:', '', $uri); $uri = str_replace('mailto:', '', $uri);
@ -711,12 +703,6 @@ class Probe
if ($network == Protocol::MAIL) { if ($network == Protocol::MAIL) {
return self::mail($uri, $uid); return self::mail($uri, $uid);
} }
if (Strings::endsWith($uri, '@twitter.com')
|| Strings::endsWith($uri, '@mobile.twitter.com')
) {
return self::twitter($uri);
}
} else { } else {
Logger::info('URI was not detectable', ['uri' => $uri]); Logger::info('URI was not detectable', ['uri' => $uri]);
return []; return [];
@ -1742,33 +1728,6 @@ class Probe
return $data; return $data;
} }
/**
* Check for twitter contact
*
* @param string $uri
*
* @return array twitter data
*/
private static function twitter($uri)
{
if (preg_match('=([^@]+)@(?:mobile\.)?twitter\.com$=i', $uri, $matches)) {
$nick = $matches[1];
} elseif (preg_match('=^https?://(?:mobile\.)?twitter\.com/(.+)=i', $uri, $matches)) {
$nick = $matches[1];
} else {
return [];
}
$data = [];
$data['url'] = 'https://twitter.com/' . $nick;
$data['addr'] = $nick . '@twitter.com';
$data['nick'] = $data['name'] = $nick;
$data['network'] = Protocol::TWITTER;
$data['baseurl'] = 'https://twitter.com';
return $data;
}
/** /**
* Checks HTML page for RSS feed link * Checks HTML page for RSS feed link
* *