Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

return [
'ocs' => [
['name' => 'RemoteActivity#receiveActivities', 'url' => '/api/v2/remote', 'verb' => 'POST'],
['name' => 'RemoteActivity#receiveActivity', 'url' => '/api/v2/remote/{token}', 'verb' => 'POST'],
['name' => 'APIv1#get', 'url' => '/activity', 'verb' => 'GET', 'root' => '/cloud'],
['name' => 'APIv2#getDefault', 'url' => '/api/v2/activity', 'verb' => 'GET'],
Expand Down
3 changes: 2 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OC\DB\ConnectionAdapter;
use OC\SystemConfig;
use OCA\Activity\Capabilities;
use OCA\Activity\ConfigLexicon;
use OCA\Activity\Consumer;
use OCA\Activity\Dashboard\ActivityWidget;
use OCA\Activity\Data;
Expand Down Expand Up @@ -141,7 +142,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserDeletedEvent::class, UserDeleted::class);
$context->registerEventListener(PostLoginEvent::class, SetUserDefaults::class);
$context->registerDashboardWidget(ActivityWidget::class);

$context->registerConfigLexicon(ConfigLexicon::class);
$this->registerFilesActivity($context);
}

Expand Down
105 changes: 94 additions & 11 deletions lib/BackgroundJob/RemoteActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@
use OCA\Activity\Extension\Files;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;

class RemoteActivity extends QueuedJob {
public function __construct(
ITimeFactory $timeFactory,
private readonly IClientService $clientService,
private readonly ICloudIdManager $cloudIdManager,
private readonly LoggerInterface $logger,
) {
parent::__construct($timeFactory);
}

#[\Override]
protected function run($argument) {
call_user_func_array($this->sendActivity(...), $argument);
if (is_array($argument) && array_key_exists('instances', $argument)) {
foreach ($argument['instances'] as $instance => $entries) {
$this->sendActivities($instance, $entries);
}
} else {
call_user_func_array($this->sendActivity(...), $argument);
}
}

protected function sendActivity($target, $token, $path, $internalType, $time, $actor, $secondPath = '') {
$client = $this->clientService->newClient();

$cloudId = $this->cloudIdManager->resolveCloudId($target);
$type = $this->translateType($internalType, $secondPath);

Expand Down Expand Up @@ -65,28 +70,106 @@ protected function sendActivity($target, $token, $path, $internalType, $time, $a
];
}

$this->sendRemote($cloudId->getRemote(), $token, $fields);
}

protected function sendActivities(string $instance, array $entries) {
if (count($entries) === 1) {
$this->sendActivity(...$entries[0]);
return;
}

$items = [];
foreach ($entries as $entry) {
[$target, $token, $path, $internalType, $time, $actor, $secondPath] = array_merge($entry, array_fill(0, 6, ''));
$cloudId = $this->cloudIdManager->resolveCloudId($target);

$type = $this->translateType($internalType, $secondPath);

$item = [
'to' => [
'type' => 'Person',
'name' => $cloudId->getUser(),
],
'actor' => [
'type' => 'Person',
'name' => $actor,
],
'type' => $type,
'share' => [
'type' => 'Document',
'token' => $token,
],
'updated' => date(\DateTime::W3C, (int)$time),
];

if ($type === 'Move') {
$item['target'] = [
'type' => 'Document',
'name' => $path,
];
$item['origin'] = [
'type' => 'Document',
'name' => $secondPath,
];
} else {
$item['object'] = [
'type' => 'Document',
'name' => $path,
];
}

$items[] = $item;
}

$this->sendRemoteCollection($instance, $items);
}

public function sendRemoteCollection(string $instance, array $items): void {
if (empty($items)) {
return;
}

$collection = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Collection',
'totalItems' => count($items),
'items' => $items
];

$this->sendRemote($instance, null, $collection);
}

public function sendRemote(string $instance, ?string $token, array $data): void {
$client = $this->clientService->newClient();

try {
$client->post(
$this->getServerURL($cloudId, $token), [
'body' => $fields,
$this->getServerURL($instance, $token), [
'body' => $data,
'timeout' => 10,
'connect_timeout' => 10,
]
);
} catch (ClientException) {
} catch (ClientException $e) {
$this->logger->warning('Failed to send remote activity', ['exception' => $e]);
}
}

/**
* @param string $token
* @return string
* returns the url to reach remote activity apps ocs endpoint
*
* will use the receiveActivities() endpoint is token is NULL
*/
protected function getServerURL(ICloudId $cloudId, $token) {
$remote = $cloudId->getRemote();
protected function getServerURL(string $remote, ?string $token): string {
if (!str_starts_with($remote, 'http')) {
$remote = 'https://' . $remote;
}

if ($token === null) {
return rtrim($remote, '/') . '/ocs/v2.php/apps/activity/api/v2/remote';
}

return rtrim($remote, '/') . '/ocs/v2.php/apps/activity/api/v2/remote/' . $token;
}

Expand Down
43 changes: 43 additions & 0 deletions lib/ConfigLexicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Activity;

use OCP\Config\Lexicon\Entry;
use OCP\Config\Lexicon\ILexicon;
use OCP\Config\Lexicon\Strictness;
use OCP\Config\ValueType;

class ConfigLexicon implements ILexicon {
public const TRUSTED_SERVER_TTL = 'trusted_server_ttl';

#[\Override]
public function getStrictness(): Strictness {
return Strictness::IGNORE;
}

/**
* @inheritDoc
*/
#[\Override]
public function getAppConfigs(): array {
return [
new Entry(key: self::TRUSTED_SERVER_TTL, type: ValueType::INT, defaultRaw: 600, definition: 'TTL on activities coming from a trusted server', lazy: true),
];
}

/**
* @inheritDoc
*/
#[\Override]
public function getUserConfigs(): array {
return [
];
}
}
Loading
Loading