Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CloudFederationApi): Start implementation of 1.1.0 of OCM spec #47199

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions apps/cloud_federation_api/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
'verb' => 'POST',
'root' => '/ocm',
],
// [
// 'name' => 'RequestHandler#inviteAccepted',
// 'url' => '/invite-accepted',
// 'verb' => 'POST',
// 'root' => '/ocm',
// ]
[
'name' => 'RequestHandler#inviteAccepted',
'url' => '/invite-accepted',
'verb' => 'POST',
'root' => '/ocm',
]
],
];
139 changes: 139 additions & 0 deletions apps/cloud_federation_api/lib/BackgroundJob/SendShareJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\CloudFederationAPI\BackgroundJob;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudFederationShare;


class SendShareJob extends Job
{
private bool $retainJob = true;

/** @var int max number of attempts to send the request */
private int $maxTry = 20;

/** @var int how much time should be between two tries (10 minutes) */
private int $interval = 600;
public function __construct(
private IQueryBuilder $queryBuilder,
private ICloudFederationProviderManager $cloudFederationProviderManager,
private ICloudFederationShare $cloudFederationShare,
ITimeFactory $time
) {
parent::__construct($time);
}

/**
* Run the job, then remove it from the jobList
*/
public function start(IJobList $jobList): void
{
if ($this->shouldRun($this->argument)) {
parent::start($jobList);
$jobList->remove($this, $this->argument);
if ($this->retainJob) {
$this->reAddJob($jobList, $this->argument);
}
}
}

protected function run($argument)
{
$description = $argument['description'];
$name = $argument['name'];
$owner = $argument['owner'];
$ownerDisplayName = $argument['ownerDisplayName'];
$permissions = $argument['permissions'];
$recipientProvider = $argument['recipientProvider'];
$resourceType = $argument['resourceType'];
$sender = $argument['sender'];
$senderDisplayName = $argument['senderDisplayender'];
$shareType = $argument['shareType'];
$sharedSecret = $argument['sharedSecret'];
$receiver = $argument['recipient'];
$uri = $argument['uri'];
$protocol = [
'name' => 'webdav',
'options' => [
'sharedSecret' => $sharedSecret,
],
'webdav' => [
'sharedSecret' => $sharedSecret,
'permissions' => $permissions,
'uri' => $uri

]
];

$this->cloudFederationShare->setDescription($description);
$this->cloudFederationShare->setOwner($owner);
$this->cloudFederationShare->setOwnerDisplayName($ownerDisplayName);
$this->cloudFederationShare->setProtocol($protocol);
$this->cloudFederationShare->setProviderId($recipientProvider);
$this->cloudFederationShare->setResourceName($name);
$this->cloudFederationShare->setResourceType($resourceType);
$this->cloudFederationShare->setSender($sender);
$this->cloudFederationShare->setSenderDisplayName($senderDisplayName);
$this->cloudFederationShare->setShareType($shareType);
$this->cloudFederationShare->setShareWith($receiver);

$reply = $this->cloudFederationProviderManager->sendCloudShare($this->cloudFederationShare);

$result = $reply->getStatus() === 201;
$try = (int)$argument['try'] + 1;
if ($result === true || $try > $this->maxTry) {
$this->retainJob = false;
}
}
/**
* Re-add background job with new arguments
*/
protected function reAddJob(IJobList $jobList, array $argument): void
{
$jobList->add(
SendShareJob::class,
[
'description' => $argument['description'],
'name' => $argument['name'],
'owner' => $argument['owner'],
'ownerDisplayName' => $argument['ownerDisplayName'],
'permissions' => $argument['permissions'],
'recipientProvider' => $argument['recipientProvider'],
'resourceType' => $argument['resourceType'],
'sender' => $argument['sender'],
'senderDisplayName' => $argument['senderDisplayender'],
'shareType' => $argument['shareType'],
'sharedSecret' => $argument['sharedSecret'],
'receiver' => $argument['recipient'],
'uri' => $argument['uri'],
'id' => $argument['id'],
'lastRun' => $this->time->getTime(),
'try' => (int)$argument['try'] + 1,
]
);
}

/**
* Test if it is time for the next run
*/
protected function shouldRun(array $argument): bool
{
$lastRun = (int)$argument['lastRun'];
return (($this->time->getTime() - $lastRun) > $this->interval);
}
}
// $this->queryBuilder->select('id', 'sender', 'email', 'name', 'status')
// ->from('federated_invites')
// ->where('token', $this->queryBuilder->expr()->eq($token, $this->queryBuilder->createNamedParameter($token)))
// ->andWhere('userId', $this->queryBuilder->expr()->eq($userId, $this->queryBuilder->createNamedParameter($userId)))
// ->andWhere('recipientProvider', $this->queryBuilder->expr()->eq($recipientProvider, $this->queryBuilder->createNamedParameter($recipientProvider)))
9 changes: 7 additions & 2 deletions apps/cloud_federation_api/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCP\OCM\IOCMProvider;

class Capabilities implements ICapability {
public const API_VERSION = '1.0-proposal1';
public const API_VERSION = '1.1.0';

public function __construct(
private IURLGenerator $urlGenerator,
Expand All @@ -31,12 +31,16 @@ public function __construct(
* enabled: bool,
* apiVersion: string,
* endPoint: string,
* provider: string,
* resourceTypes: array{
* name: string,
* shareTypes: string[],
* protocols: array<string, string>
* }[],
* },
* },
* capabilities: array{
* string,
* }
* }
* @throws OCMArgumentException
*/
Expand All @@ -45,6 +49,7 @@ public function getCapabilities() {

$this->provider->setEnabled(true);
$this->provider->setApiVersion(self::API_VERSION);
$this->provider->setCapabilities(['/invite-accepted', '/notifications', '/shares']);

$pos = strrpos($url, '/');
if ($pos === false) {
Expand Down
Loading