Compare commits

..

No commits in common. "61f6fe3e7b2231359d5e899578e9edc818746d09" and "097f871ed6af93becc16576ca60bbc1ced1b36d5" have entirely different histories.

4 changed files with 20 additions and 130 deletions

View file

@ -194,8 +194,6 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
}
}
$users = array_unique($users); // remove duplicates
if (empty($users)) { // todo remove after proper implementation, debugging for now
$rootDir = PROJECT_ROOT . '/';
// Save the raw input and parsed JSON to a file for inspection
@ -231,7 +229,15 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
}
}
$connector->sendActivity($user, $inboxActivity);
try {
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $inboxActivity);
if ($articleId !== null) {
$connector->sendActivity($user, $inboxActivity);
}
} catch (\Throwable $e) {
error_log("Inbox::postForUser Error sending activity to connector. Exception: " . $e->getMessage());
return false;
}
return "success";
}
@ -328,7 +334,7 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
$cache
);
if ($recipient === null || $recipient->id === null) {
error_log("Inbox::postForUser couldn't find recipient: $_recipientId");
error_log("Inbox::postForUser couldn't find user: $_recipientId");
return false;
}

View file

@ -194,9 +194,6 @@ class NewContent implements \Federator\Api\APIInterface
$users[] = $user->id;
}
}
$users = array_unique($users); // remove duplicates
if (empty($users)) { // todo remove after proper implementation, debugging for now
$rootDir = PROJECT_ROOT . '/';
// Save the raw input and parsed JSON to a file for inspection

View file

@ -91,9 +91,6 @@ class Test
// pretend that we are running from web directory
define('PROJECT_ROOT', dirname(__DIR__, 2));
$api = new \Federator\Api();
$api->loadPlugins();
$api->openDatabase();
for ($i = 1; $i < $argc; ++$i) {
switch ($argv[$i]) {
case 'fetchoutbox':
@ -112,18 +109,18 @@ class Test
/**
* print usage of test tool
* print usage of maintenance tool
*
* @return void
*/
public static function printUsage()
{
echo "usage php test.php <command> <parameter> [<command> <parameter>...]\n";
echo "usage php maintenance.php <command> <parameter> [<command> <parameter>...]\n";
echo "command can be one of:\n";
echo " fetchoutbox - fetch users outbox. parameter: username\n";
echo " upvote - upvote. parameter: URL to upvote\n";
echo " downvote - downvote. parameter: URL to downvote\n";
echo " comment - comment. parameter: URL to comment, text to comment\n";
echo " comment - downvote. parameter: URL to comment, text to comment\n";
echo " Run this after you updated the program files\n";
exit();
}
@ -139,16 +136,13 @@ class Test
public static function upvote($api, $_url)
{
$dbh = $api->getDatabase();
$inboxActivity = new \Federator\Data\ActivityPub\Common\Like();
$inboxActivity->setAActor('https://mastodon.local/users/admin');
$inboxActivity->setObject($_url);
$inboxActivity->setID("https://mastodon.local/users/admin#like/" . md5($_url));
$inboxActivity = new \Federator\Data\ActivityPub\Common\Create();
\Federator\Api\FedUsers\Inbox::postForUser(
$dbh,
$api->getConnector(),
null,
'admin@mastodon.local',
"grumpydevelop@192.168.178.143",
'grumpydevelop',
$_url,
$inboxActivity
);
}

View file

@ -314,7 +314,6 @@ class ContentNation implements Connector
}
$user = new \Federator\Data\User();
$user->externalid = $_name;
$user->id = $_name;
$user->iconMediaType = $r['iconMediaType'];
$user->iconURL = $r['iconURL'];
$user->imageMediaType = $r['imageMediaType'];
@ -676,9 +675,8 @@ class ContentNation implements Connector
public function sendActivity($sender, $activity)
{
$targetUrl = $this->service;
$targetRequestType = 'post'; // Default request type
// Convert ActivityPub activity to ContentNation JSON format and retrieve target url
$jsonData = self::activityToJson($this->main->getDatabase(), $this->service, $activity, $targetUrl, $targetRequestType);
$jsonData = self::activityToJson($this->main->getDatabase(), $this->service, $activity, $targetUrl);
if ($jsonData === false) {
error_log("ContentNation::sendActivity failed to convert activity to JSON");
@ -704,7 +702,7 @@ class ContentNation implements Connector
$path = $parsed['path'];
// Build the signature string
$signatureString = "(request-target): $targetRequestType {$path}\n" .
$signatureString = "(request-target): post {$path}\n" .
"host: {$extHost}\n" .
"date: {$date}\n" .
"digest: {$digest}";
@ -742,16 +740,7 @@ class ContentNation implements Connector
];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch ($targetRequestType) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
throw new \Exception("ContentNation::sendActivity Unsupported target request type: $targetRequestType");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
@ -776,13 +765,11 @@ class ContentNation implements Connector
* @param string $serviceUrl the service URL
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity
* @param string $targetUrl the target URL for the activity
* @param string $targetRequestType the target request type (e.g., 'post', 'delete', etc.)
* @return array<string, mixed>|false the json data or false on failure
*/
private function activityToJson($dbh, $serviceUrl, \Federator\Data\ActivityPub\Common\Activity $activity, string &$targetUrl, string &$targetRequestType)
private static function activityToJson($dbh, $serviceUrl, \Federator\Data\ActivityPub\Common\Activity $activity, string &$targetUrl)
{
$type = strtolower($activity->getType());
$targetRequestType = 'post'; // Default request type
switch ($type) {
case 'create':
case 'update':
@ -833,50 +820,6 @@ class ContentNation implements Connector
}
break;
case 'follow':
$profileUrl = $activity->getObject();
if (!is_string($profileUrl)) {
error_log("ContentNation::activityToJson Invalid profile URL: " . json_encode($profileUrl));
return false;
}
$receiverName = basename((string) (parse_url($profileUrl, PHP_URL_PATH) ?? ''));
$ourDomain = parse_url($profileUrl, PHP_URL_HOST);
if ($receiverName === "" || $ourDomain === "") {
error_log("ContentNation::activityToJson no profileName or domain found for object url: " . $profileUrl);
return false;
}
$receiver = $receiverName;
try {
$localUser = \Federator\DIO\User::getUserByName(
$dbh,
$receiver,
$this,
null
);
} catch (\Throwable $e) {
error_log("ContentNation::activityToJson get user by name: " . $receiver . ". Exception: " . $e->getMessage());
return false;
}
if ($localUser === null || $localUser->id === null) {
error_log("ContentNation::activityToJson couldn't find user: $receiver");
return false;
}
$targetUrl = $serviceUrl . '/api/profile/' . $localUser->id . '/fedfollow';
$type = 'follow';
$actor = $activity->getAActor();
$fedUser = \Federator\DIO\FedUser::getUserByName(
$dbh,
$actor,
null
);
$from = $fedUser->id;
return [
'type' => $type,
'id' => $activity->getID(),
'from' => $from,
'to' => $localUser->id,
];
case 'like':
case 'dislike':
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $activity);
@ -916,56 +859,6 @@ class ContentNation implements Connector
if (is_object($object)) {
$objType = strtolower($object->getType());
switch ($objType) {
case 'follow':
$profileUrl = $object->getObject();
if (!is_string($profileUrl)) {
error_log("ContentNation::activityToJson Invalid profile URL: " . json_encode($profileUrl));
return false;
}
$receiverName = basename((string) (parse_url($profileUrl, PHP_URL_PATH) ?? ''));
$ourDomain = parse_url($profileUrl, PHP_URL_HOST);
if ($receiverName === "" || $ourDomain === "") {
error_log("ContentNation::activityToJson no profileName or domain found for object url: " . $profileUrl);
return false;
}
$receiver = $receiverName;
try {
$localUser = \Federator\DIO\User::getUserByName(
$dbh,
$receiver,
$this,
null
);
} catch (\Throwable $e) {
error_log("ContentNation::activityToJson get user by name: " . $receiver . ". Exception: " . $e->getMessage());
return false;
}
if ($localUser === null || $localUser->id === null) {
error_log("ContentNation::activityToJson couldn't find user: $receiver");
return false;
}
$targetUrl = $serviceUrl . '/api/profile/' . $localUser->id . '/fedfollow';
$type = 'follow';
if ($object instanceof \Federator\Data\ActivityPub\Common\Activity) {
$actor = $object->getAActor();
if ($actor !== '') {
$fedUser = \Federator\DIO\FedUser::getUserByName(
$dbh,
$actor,
null
);
$from = $fedUser->id;
$targetRequestType = 'delete';
return [
'type' => $type,
'id' => $object->getID(),
'from' => $from,
'to' => $localUser->id,
];
}
}
return false;
case 'like':
case 'dislike':
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $activity);