Compare commits

..

No commits in common. "96bb1efe166b225020b0ad51f6cb03cbc13fa8e2" and "f8539e479e4496be7eb5828043a5bec6960c8f6b" have entirely different histories.

12 changed files with 118 additions and 503 deletions

View file

@ -43,7 +43,7 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
/**
* handle post call
*
* @param string|null $_user user to add data to inbox
* @param string|null $_user user to add data to inbox @unused-param
* @return string|false response
*/
public function post($_user)
@ -107,10 +107,6 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
&& (filter_var($receiver, FILTER_VALIDATE_URL) !== false);
});
if (isset($_user)) {
$receivers[] = $dbh->real_escape_string($_user); // Add the target user to the receivers list
}
// Special handling for Follow and Undo follow activities
if (strtolower($inboxActivity->getType()) === 'follow') {
// For Follow, the object should hold the target
@ -130,7 +126,6 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
}
}
}
$ourDomain = $config['generic']['externaldomain'];
foreach ($receivers as $receiver) {
if ($receiver === '' || !is_string($receiver)) {
@ -162,19 +157,18 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
$users = array_merge($users, array_column($followers, 'id'));
}
} else {
$ourDomain = $config['generic']['externaldomain'];
// check if receiver is an actor url from our domain
if (!str_contains($receiver, $ourDomain) && $receiver !== $_user) {
if (!str_contains($receiver, $ourDomain)) {
continue;
}
if ($receiver !== $_user) {
$receiverName = basename((string) (parse_url($receiver, PHP_URL_PATH) ?? ''));
$ourDomain = parse_url($receiver, PHP_URL_HOST);
if ($receiverName === null || $ourDomain === null) {
error_log("Inbox::post no receiverName or domain found for receiver: " . $receiver);
continue;
}
$receiver = $receiverName;
}
$receiver = $receiverName . '@' . $ourDomain;
try {
$localUser = \Federator\DIO\User::getUserByName(
$dbh,
@ -215,30 +209,6 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
]);
error_log("Inbox::post enqueued job for user: $user->id with token: $token");
}
if (empty($users)) {
$type = strtolower($inboxActivity->getType());
if ($type === 'undo' || $type === 'delete') {
$token = \Resque::enqueue('inbox', 'Federator\\Jobs\\InboxJob', [
'user' => $user->id,
'recipientId' => "",
'activity' => $inboxActivity->toObject(),
]);
error_log("Inbox::post enqueued job for user: $user->id with token: $token");
} else {
error_log("Inbox::post no users found for activity, doing nothing: " . json_encode($inboxActivity, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}
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";
}
@ -271,56 +241,6 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
return false;
}
$type = strtolower($inboxActivity->getType());
if ($_recipientId === '') {
if ($type === 'undo' || $type === 'delete') {
switch ($type) {
case 'delete':
// Delete Note/Post
$object = $inboxActivity->getObject();
if (is_string($object)) {
\Federator\DIO\Posts::deletePost($dbh, $object);
} elseif (is_object($object)) {
$objectId = $object->getID();
\Federator\DIO\Posts::deletePost($dbh, $objectId);
} else {
error_log("Inbox::postForUser Error in Delete Post for user $user->id, object is not a string or object");
error_log(" object of type " . gettype($object));
return false;
}
break;
case 'undo':
$object = $inboxActivity->getObject();
if (is_object($object)) {
switch (strtolower($object->getType())) {
case 'like':
case 'dislike':
// Undo Like/Dislike (remove like/dislike)
$targetId = $object->getID();
// \Federator\DIO\Votes::removeVote($dbh, $user->id, $targetId, 'dislike');
\Federator\DIO\Posts::deletePost($dbh, $targetId);
break;
case 'note':
case 'article':
// Undo Note (remove note)
$noteId = $object->getID();
\Federator\DIO\Posts::deletePost($dbh, $noteId);
break;
}
}
break;
default:
error_log("Inbox::postForUser Unhandled activity type $type for user $user->id");
break;
}
return true;
}
}
$atPos = strpos($_recipientId, '@');
if ($atPos !== false) {
$_recipientId = substr($_recipientId, 0, $atPos);
@ -346,16 +266,18 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
FILE_APPEND
);
$type = $inboxActivity->getType();
switch ($type) {
case 'follow':
case 'Follow':
$success = \Federator\DIO\Followers::addExternalFollow($dbh, $inboxActivity->getID(), $user->id, $recipient->id);
if ($success === false) {
error_log("Inbox::postForUser Failed to add follower for user $user->id");
error_log("Inbox::postForUser: Failed to add follower for user $user->id");
}
break;
case 'delete':
case 'Delete':
// Delete Note/Post
$object = $inboxActivity->getObject();
if (is_string($object)) {
@ -366,11 +288,11 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
}
break;
case 'undo':
case 'Undo':
$object = $inboxActivity->getObject();
if (is_object($object)) {
switch (strtolower($object->getType())) {
case 'follow':
switch ($object->getType()) {
case 'Follow':
$success = false;
if ($object instanceof \Federator\Data\ActivityPub\Common\Activity) {
$actor = $object->getAActor();
@ -379,60 +301,75 @@ class Inbox implements \Federator\Api\FedUsers\FedUsersInterface
}
}
if ($success === false) {
error_log("Inbox::postForUser Failed to remove follower for user $user->id");
error_log("Inbox::postForUser: Failed to remove follower for user $user->id");
}
break;
case 'like':
case 'dislike':
// Undo Like/Dislike (remove like/dislike)
$targetId = $object->getID();
case 'Like':
// Undo Like (remove like)
if (method_exists($object, 'getObject')) {
$targetId = $object->getObject();
if (is_string($targetId)) {
// \Federator\DIO\Votes::removeVote($dbh, $user->id, $targetId, 'like');
\Federator\DIO\Posts::deletePost($dbh, $targetId);
} else {
error_log("Inbox::postForUser: Error in Undo Like for user $user->id, targetId is not a string");
}
}
break;
case 'Dislike':
// Undo Dislike (remove dislike)
if (method_exists($object, 'getObject')) {
$targetId = $object->getObject();
if (is_string($targetId)) {
// \Federator\DIO\Votes::removeVote($dbh, $user->id, $targetId, 'dislike');
\Federator\DIO\Posts::deletePost($dbh, $targetId);
} else {
error_log("Inbox::postForUser: Error in Undo Dislike for user $user->id, targetId is not a string");
}
}
break;
case 'note':
case 'Note':
// Undo Note (remove note)
if (method_exists($object, 'getID')) {
$noteId = $object->getID();
\Federator\DIO\Posts::deletePost($dbh, $noteId);
}
break;
}
}
break;
case 'like':
case 'dislike':
// Add Like/Dislike
case 'Like':
// Add Like
$targetId = $inboxActivity->getObject();
if (is_string($targetId)) {
// \Federator\DIO\Votes::addVote($dbh, $user->id, $targetId, 'like');
\Federator\DIO\Posts::savePost($dbh, $user->id, $inboxActivity);
} else {
error_log("Inbox::postForUser: Error in Add Like for user $user->id, targetId is not a string");
return false;
}
break;
case 'Dislike':
// Add Dislike
$targetId = $inboxActivity->getObject();
if (is_string($targetId)) {
// \Federator\DIO\Votes::addVote($dbh, $user->id, $targetId, 'dislike');
\Federator\DIO\Posts::savePost($dbh, $user->id, $inboxActivity);
} else {
error_log("Inbox::postForUser Error in Add Like/Dislike for user $user->id, targetId is not a string");
error_log("Inbox::postForUser: Error in Add Dislike for user $user->id, targetId is not a string");
return false;
}
break;
case 'create':
case 'update':
$object = $inboxActivity->getObject();
if (is_object($object)) {
switch (strtolower($object->getType())) {
case 'note':
case 'Note':
// Post Note
\Federator\DIO\Posts::savePost($dbh, $user->id, $inboxActivity);
break;
case 'article':
\Federator\DIO\Posts::savePost($dbh, $user->id, $inboxActivity);
break;
default:
\Federator\DIO\Posts::savePost($dbh, $user->id, $inboxActivity);
break;
}
}
break;
default:
error_log("Inbox::postForUser Unhandled activity type $type for user $user->id");
error_log("Inbox::postForUser: Unhandled activity type $type for user $user->id");
break;
}

View file

@ -100,9 +100,8 @@ class NewContent implements \Federator\Api\APIInterface
return false;
}
$articleId = "";
if (isset($allHeaders['X-Sender'])) {
$newActivity = $connector->jsonToActivity($input, $articleId);
$newActivity = $connector->jsonToActivity($input);
} else {
error_log("NewContent::post No X-Sender header found");
return false;
@ -213,7 +212,6 @@ class NewContent implements \Federator\Api\APIInterface
'user' => $posterName,
'recipientId' => $receiver,
'activity' => $newActivity->toObject(),
'articleId' => $articleId,
]);
error_log("Inbox::post enqueued job for receiver: $receiver with token: $token");
}
@ -234,11 +232,9 @@ class NewContent implements \Federator\Api\APIInterface
* @param string $_user user that triggered the post
* @param string $_recipientId recipient of the post
* @param \Federator\Data\ActivityPub\Common\Activity $newActivity the activity that we received
* @param string $articleId the original id of the article (if applicable)
* (used to identify the article in the remote system)
* @return boolean response
*/
public static function postForUser($dbh, $connector, $cache, $host, $_user, $_recipientId, $newActivity, $articleId)
public static function postForUser($dbh, $connector, $cache, $host, $_user, $_recipientId, $newActivity)
{
if (!isset($_user)) {
error_log("NewContent::postForUser no user given");
@ -276,10 +272,10 @@ class NewContent implements \Federator\Api\APIInterface
FILE_APPEND
);
$type = strtolower($newActivity->getType());
$type = $newActivity->getType();
switch ($type) {
case 'follow':
case 'Follow':
// $success = false;
$actor = $newActivity->getAActor();
if ($actor !== '') {
@ -297,7 +293,7 @@ class NewContent implements \Federator\Api\APIInterface
} */
break;
case 'delete':
case 'Delete':
// Delete Note/Post
$object = $newActivity->getObject();
if (is_string($object)) {
@ -308,11 +304,11 @@ class NewContent implements \Federator\Api\APIInterface
}
break;
case 'undo':
case 'Undo':
$object = $newActivity->getObject();
if (is_object($object)) {
switch (strtolower($object->getType())) {
case 'follow':
switch ($object->getType()) {
case 'Follow':
$success = false;
if ($object instanceof \Federator\Data\ActivityPub\Common\Activity) {
$actor = $object->getAActor();
@ -336,8 +332,8 @@ class NewContent implements \Federator\Api\APIInterface
error_log("NewContent::postForUser Failed to remove follower for user $user->id");
}
break;
case 'like':
case 'dislike':
case 'Like':
case 'Dislike':
if (method_exists($object, 'getObject')) {
$targetId = $object->getObject();
if (is_string($targetId)) {
@ -348,13 +344,13 @@ class NewContent implements \Federator\Api\APIInterface
}
}
break;
case 'note':
case 'Note':
// Undo Note (remove note)
$noteId = $object->getID();
\Federator\DIO\Posts::deletePost($dbh, $noteId);
break;
case 'article':
case 'Article':
$articleId = $object->getID();
\Federator\DIO\Posts::deletePost($dbh, $articleId);
// also remove latest saved article-update
@ -385,30 +381,30 @@ class NewContent implements \Federator\Api\APIInterface
}
break;
case 'like':
case 'dislike':
case 'Like':
case 'Dislike':
// Add Like/Dislike
$targetId = $newActivity->getObject();
if (is_string($targetId)) {
// \Federator\DIO\Votes::addVote($dbh, $user->id, $targetId, 'like');
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity, $articleId);
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity);
} else {
error_log("NewContent::postForUser Error in Add Like/Dislike for recipient $recipient->id, targetId is not a string");
return false;
}
break;
case 'create':
case 'update':
case 'Create':
case 'Update':
$object = $newActivity->getObject();
if (is_object($object)) {
switch (strtolower($object->getType())) {
case 'note':
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity, $articleId);
switch ($object->getType()) {
case 'Note':
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity);
break;
case 'article':
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity, $articleId);
case 'Article':
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity);
$idPart = strrchr($recipient->id, '@');
if ($idPart === false) {
@ -427,7 +423,7 @@ class NewContent implements \Federator\Api\APIInterface
break;
default:
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity, $articleId);
\Federator\DIO\Posts::savePost($dbh, $user->id, $newActivity);
break;
}
}
@ -535,6 +531,7 @@ class NewContent implements \Federator\Api\APIInterface
$response = curl_exec($ch);
curl_close($ch);
// Log the response for debugging if needed
if ($response === false) {
throw new \Exception("Failed to send activity: " . curl_error($ch));
} else {

View file

@ -70,20 +70,9 @@ interface Connector
* Convert jsonData to Activity format
*
* @param array<string, mixed> $jsonData the json data from our platfrom
* @param string $articleId the original id of the article (if applicable)
* (used to identify the article in the remote system)
* @return \Federator\Data\ActivityPub\Common\Activity|false
*/
public function jsonToActivity(array $jsonData, &$articleId);
/**
* send target-friendly json from ActivityPub activity
*
* @param \Federator\Data\FedUser $sender the user of the sender
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity
* @return boolean did we successfully send the activity?
*/
public function sendActivity($sender, $activity);
public function jsonToActivity(array $jsonData);
/**
* check if the headers include a valid signature

View file

@ -10,6 +10,17 @@ namespace Federator\Data\ActivityPub\Common;
class Delete extends Activity
{
/**
* object overwrite
* @var string
*/
private $object = "";
public function setFObject(string $object): void
{
$this->object = $object;
}
public function __construct()
{
parent::__construct('Delete');
@ -22,6 +33,10 @@ class Delete extends Activity
*/
public function fromJson($json): bool
{
if (array_key_exists('object', $json)) {
$this->object = $json['object'];
unset($json['object']);
}
return parent::fromJson($json);
}
/**
@ -31,6 +46,9 @@ class Delete extends Activity
public function toObject()
{
$return = parent::toObject();
if ($this->object !== "") {
$return['object'] = $this->object;
}
return $return;
}
}

View file

@ -67,9 +67,6 @@ class Factory
case 'Inbox':
$return = new Common\Inbox();
break;
case 'Tombstone':
$return = new Common\APObject("Tombstone");
break;
/*case 'Question':
$return = new Common\Question();
break;

View file

@ -216,15 +216,13 @@ class Posts
* @param \mysqli $dbh
* @param string $userId
* @param \Federator\Data\ActivityPub\Common\Activity $post
* @param string|null $articleId the original id of the article
* (used to identify the source article in the remote system)
* @return bool
*/
public static function savePost($dbh, $userId, $post, $articleId = null)
public static function savePost($dbh, $userId, $post)
{
$sql = 'INSERT INTO posts (
`id`, `url`, `user_id`, `actor`, `type`, `object`, `to`, `cc`, `published`, `article_id`
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`id`, `url`, `user_id`, `actor`, `type`, `object`, `to`, `cc`, `published`
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
`url` = VALUES(`url`),
`user_id` = VALUES(`user_id`),
@ -233,8 +231,7 @@ class Posts
`object` = VALUES(`object`),
`to` = VALUES(`to`),
`cc` = VALUES(`cc`),
`published` = VALUES(`published`),
`article_id` = VALUES(`article_id`)';
`published` = VALUES(`published`)';
$stmt = $dbh->prepare($sql);
if ($stmt === false) {
throw new \Federator\Exceptions\ServerError();
@ -251,9 +248,6 @@ class Posts
if ($objectJson === false) {
$objectJson = null;
}
if (is_object($object)) {
$id = $object->getID();
}
$to = $post->getTo();
$cc = $post->getCC();
$toJson = is_array($to) ? json_encode($to) : (is_string($to) ? json_encode([$to]) : null);
@ -262,7 +256,7 @@ class Posts
$publishedStr = $published ? gmdate('Y-m-d H:i:s', $published) : gmdate('Y-m-d H:i:s');
$stmt->bind_param(
"ssssssssss",
"sssssssss",
$id,
$url,
$userId,
@ -271,8 +265,7 @@ class Posts
$objectJson,
$toJson,
$ccJson,
$publishedStr,
$articleId,
$publishedStr
);
$result = $stmt->execute();
$stmt->close();
@ -299,40 +292,4 @@ class Posts
$stmt->close();
return $affectedRows > 0;
}
/** retrieve original article id of post
*
* @param \mysqli $dbh
* @param \Federator\Data\ActivityPub\Common\Activity $post
* @return string|null
*/
public static function getOriginalArticleId($dbh, $post)
{
$sql = 'SELECT `article_id` FROM posts WHERE id = ?';
$stmt = $dbh->prepare($sql);
if ($stmt === false) {
throw new \Federator\Exceptions\ServerError();
}
$id = $post->getID();
$object = $post->getObject();
if (is_object($object)) {
$inReplyTo = $object->getInReplyTo();
if ($inReplyTo !== "") {
$id = $inReplyTo; // Use inReplyTo as ID if it's a string
} else {
$id = $object->getObject();
}
} elseif (is_string($object)) {
$id = $object; // If object is a string, use it directly
}
$stmt->bind_param("s", $id);
$articleId = null;
$ret = $stmt->bind_result($articleId);
$stmt->execute();
if ($ret) {
$stmt->fetch();
}
$stmt->close();
return $articleId;
}
}

View file

@ -56,7 +56,6 @@ class NewContentJob extends \Federator\Api
$user = $this->args['user'];
$recipientId = $this->args['recipientId'];
$activity = $this->args['activity'];
$articleId = $this->args['articleId'] ?? null;
$activity = \Federator\Data\ActivityPub\Factory::newActivityFromJson($activity);
@ -68,7 +67,7 @@ class NewContentJob extends \Federator\Api
$ourUrl = 'https://' . $domain;
\Federator\Api\V1\NewContent::postForUser($this->dbh, $this->connector, $this->cache, $ourUrl, $user, $recipientId, $activity, $articleId);
\Federator\Api\V1\NewContent::postForUser($this->dbh, $this->connector, $this->cache, $ourUrl, $user, $recipientId, $activity);
return true;
}
}

View file

@ -368,11 +368,9 @@ class ContentNation implements Connector
* Convert jsonData to Activity format
*
* @param array<string, mixed> $jsonData the json data from our platfrom
* @param string $articleId the original id of the article (if applicable)
* (used to identify the article in the remote system)
* @return \Federator\Data\ActivityPub\Common\Activity|false
*/
public function jsonToActivity($jsonData, &$articleId)
public function jsonToActivity($jsonData)
{
$returnActivity = false;
// Common fields for all activity types
@ -484,7 +482,6 @@ class ContentNation implements Connector
$returnActivity->setID($ap['id']);
$returnActivity->setURL($ap['url']);
}
$articleId = $jsonData['object']['id']; // Set the article ID for the activity
break;
case 'comment':
@ -517,7 +514,6 @@ class ContentNation implements Connector
$returnActivity->setID($ap['id']);
$returnActivity->setURL($ap['url']);
}
$articleId = $jsonData['object']['articleId']; // Set the article ID for the activity
break;
case 'vote':
@ -552,7 +548,6 @@ class ContentNation implements Connector
$returnActivity->setID($ap['id']);
$returnActivity->setURL($ap['url']);
}
$articleId = $jsonData['object']['articleId']; // Set the article ID for the activity
break;
default:
@ -664,250 +659,6 @@ class ContentNation implements Connector
return $returnJson;
}
/**
* send CN-friendly json from ActivityPub activity
*
* @param \Federator\Data\FedUser $sender the user of the sender
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity
* @return boolean did we successfully send the activity?
*/
public function sendActivity($sender, $activity)
{
$targetUrl = $this->service;
// Convert ActivityPub activity to ContentNation JSON format and retrieve target url
$jsonData = self::activityToJson($this->main->getDatabase(), $this->service, $activity, $targetUrl);
if ($jsonData === false) {
error_log("ContentNation::sendActivity failed to convert activity to JSON");
return false;
}
$json = json_encode($jsonData, JSON_UNESCAPED_SLASHES);
if ($json === false) {
throw new \Exception('Failed to encode JSON: ' . json_last_error_msg());
}
$digest = 'SHA-256=' . base64_encode(hash('sha256', $json, true));
$date = gmdate('D, d M Y H:i:s') . ' GMT';
$parsed = parse_url($targetUrl);
if ($parsed === false) {
throw new \Exception('Failed to parse URL: ' . $targetUrl);
}
if (!isset($parsed['host']) || !isset($parsed['path'])) {
throw new \Exception('Invalid target URL: missing host or path');
}
$extHost = $parsed['host'];
$path = $parsed['path'];
// Build the signature string
$signatureString = "(request-target): post {$path}\n" .
"host: {$extHost}\n" .
"date: {$date}\n" .
"digest: {$digest}";
$pKeyPath = PROJECT_ROOT . '/' . $this->main->getConfig()['keys']['federatorPrivateKeyPath'];
$privateKeyPem = file_get_contents($pKeyPath);
if ($privateKeyPem === false) {
http_response_code(500);
throw new \Federator\Exceptions\PermissionDenied("Private key couldn't be determined");
}
$pkeyId = openssl_pkey_get_private($privateKeyPem);
if ($pkeyId === false) {
throw new \Exception('Invalid private key');
}
openssl_sign($signatureString, $signature, $pkeyId, OPENSSL_ALGO_SHA256);
$signature_b64 = base64_encode($signature);
$signatureHeader = 'algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' . $signature_b64 . '"';
$ch = curl_init($targetUrl);
if ($ch === false) {
throw new \Exception('Failed to initialize cURL');
}
$headers = [
'Host: ' . $extHost,
'Date: ' . $date,
'Digest: ' . $digest,
'Content-Type: application/json',
'Signature: ' . $signatureHeader,
'Accept: application/json',
'Username: ' . $sender->id,
];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
throw new \Exception("Failed to send activity: " . curl_error($ch));
} else {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != 200 && $httpcode != 202) {
throw new \Exception("Unexpected HTTP code $httpcode: $response");
}
}
return true;
}
/**
* Convert ActivityPub activity to ContentNation JSON format
*
* @param \mysqli $dbh database handle
* @param string $serviceUrl the service URL
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity
* @param string $targetUrl the target URL for the activity
* @return array<string, mixed>|false the json data or false on failure
*/
private static function activityToJson($dbh, $serviceUrl, \Federator\Data\ActivityPub\Common\Activity $activity, string &$targetUrl)
{
$type = strtolower($activity->getType());
switch ($type) {
case 'create':
case 'update':
$object = $activity->getObject();
if (is_object($object)) {
$objType = strtolower($object->getType());
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $activity);
if ($articleId === null) {
error_log("ContentNation::activityToJson Failed to get original article ID for create/update activity");
}
switch ($objType) {
case 'article':
// We don't support article create/update at this point in time
error_log("ContentNation::activityToJson Unsupported create/update object type: {$objType}");
break;
case 'note':
$targetUrl = $serviceUrl . '/api/article/' . $articleId . '/comment';
$type = 'comment';
$inReplyTo = $object->getInReplyTo();
if ($inReplyTo !== '') {
$target = $inReplyTo;
} else {
$target = $object->getObject();
}
$comment = null;
if (is_string($target)) {
if (strpos($target, '#') !== false) {
$parts = explode('#', $target);
if (count($parts) > 0) {
$comment = $parts[count($parts) - 1];
}
}
} else {
error_log("ContentNation::activityToJson Unsupported target type for comment with id: " . $activity->getID() . " Type: " . gettype($target));
return false;
}
return [
'type' => $type,
'id' => $activity->getID(),
'parent' => $comment,
'subject' => $object->getSummary(),
'comment' => $object->getContent(),
];
default:
error_log("ContentNation::activityToJson Unsupported create/update object type: {$objType}");
return false;
}
}
break;
case 'like':
case 'dislike':
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $activity);
if ($articleId === null) {
error_log("ContentNation::activityToJson Failed to get original article ID for vote activity");
}
$voteValue = $type === 'like' ? true : false;
$activityType = 'vote';
$inReplyTo = $activity->getInReplyTo();
if ($inReplyTo !== '') {
$target = $inReplyTo;
} else {
$target = $activity->getObject();
}
$comment = null;
if (is_string($target)) {
if (strpos($target, '#') !== false) {
$parts = explode('#', $target);
if (count($parts) > 0) {
$comment = $parts[count($parts) - 1];
}
}
} else {
error_log("ContentNation::activityToJson Unsupported target type for vote with id: " . $activity->getID() . " Type: " . gettype($target));
return false;
}
$targetUrl = $serviceUrl . '/api/article/' . $articleId . '/vote';
return [
'vote' => $voteValue,
'type' => $activityType,
'id' => $activity->getID(),
'comment' => $comment,
];
case 'undo':
$object = $activity->getObject();
if (is_object($object)) {
$objType = strtolower($object->getType());
switch ($objType) {
case 'like':
case 'dislike':
$articleId = \Federator\DIO\Posts::getOriginalArticleId($dbh, $activity);
if ($articleId === null) {
error_log("ContentNation::activityToJson Failed to get original article ID for undo vote activity");
}
$activityType = 'vote';
$inReplyTo = $object->getInReplyTo();
if ($inReplyTo !== '') {
$target = $inReplyTo;
} else {
$target = $object->getObject();
}
$comment = null;
if (is_string($target)) {
if (strpos($target, '#') !== false) {
$parts = explode('#', $target);
if (count($parts) > 0) {
$comment = $parts[count($parts) - 1];
}
}
} else {
error_log("ContentNation::activityToJson Unsupported target type for undo vote with id: " . $activity->getID() . " Type: " . gettype($target));
return false;
}
$targetUrl = $serviceUrl . '/api/article/' . $articleId . '/vote';
return [
'vote' => null,
'type' => $activityType,
'id' => $object->getID(),
'comment' => $comment,
];
case 'note':
// We don't support comment deletions at this point in time
error_log("ContentNation::activityToJson Unsupported undo object type: {$objType}");
break;
default:
error_log("ContentNation::activityToJson Unsupported create/update object type: {$objType}");
return false;
}
}
break;
default:
error_log("ContentNation::activityToJson Unsupported activity type: {$type}");
return false;
}
return false;
}
/**
* check if the headers include a valid signature
*

View file

@ -73,11 +73,9 @@ class DummyConnector implements Connector
* Convert jsonData to Activity format
*
* @param array<string, mixed> $jsonData the json data from our platfrom @unused-param
* @param string $articleId the original id of the article (if applicable)
* (used to identify the article in the remote system) @unused-param
* @return \Federator\Data\ActivityPub\Common\Activity|false
*/
public function jsonToActivity(array $jsonData, &$articleId) {
public function jsonToActivity(array $jsonData) {
return false;
}
@ -110,18 +108,6 @@ class DummyConnector implements Connector
return $user;
}
/**
* send target-friendly json from ActivityPub activity
*
* @param \Federator\Data\FedUser $sender the user of the sender @unused-param
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity @unused-param
* @return boolean did we successfully send the activity?
*/
public function sendActivity($sender, $activity)
{
return false;
}
/**
* check if the headers include a valid signature
*

View file

@ -125,11 +125,9 @@ class RedisCache implements Cache
* Convert jsonData to Activity format
*
* @param array<string, mixed> $jsonData the json data from our platfrom @unused-param
* @param string $articleId the original id of the article (if applicable)
* (used to identify the article in the remote system) @unused-param
* @return \Federator\Data\ActivityPub\Common\Activity|false
*/
public function jsonToActivity(array $jsonData, &$articleId)
public function jsonToActivity(array $jsonData)
{
error_log("rediscache::jsonToActivity not implemented");
return false;
@ -355,18 +353,6 @@ class RedisCache implements Cache
$this->redis->setEx($key, $this->publicKeyPemTTL, $publicKeyPem);
}
/**
* send target-friendly json from ActivityPub activity
*
* @param \Federator\Data\FedUser $sender the user of the sender @unused-param
* @param \Federator\Data\ActivityPub\Common\Activity $activity the activity @unused-param
* @return boolean did we successfully send the activity?
*/
public function sendActivity($sender, $activity)
{
return false;
}
/**
* check if the headers include a valid signature
*

View file

@ -1,2 +0,0 @@
alter table posts add `article_id` varchar(255) null default null comment 'The optional original article id (of non-federated system, e.g. CN)';
update settings set `value`="2025-05-27" where `key`="database_version";