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
5 changes: 5 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@

<background-jobs>
<job>OCA\Contacts\Cron\SocialUpdateRegistration</job>
<job>OCA\Contacts\Cron\UpdateOcmProviders</job>
</background-jobs>

<commands>
<command>OCA\Contacts\Command\OcmInvitesConfig</command>
</commands>

<settings>
<admin>OCA\Contacts\Settings\AdminSettings</admin>
</settings>
Expand Down
2 changes: 1 addition & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
preset: 'ts-jest',
moduleFileExtensions: ['js', 'vue', 'ts'],
collectCoverageFrom: [
'src/**/*.{js,vue}',
'src/**/*.{js,ts,vue}',
'!**/node_modules/**',
],
setupFilesAfterEnv: [
Expand Down
13 changes: 13 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
namespace OCA\Contacts\AppInfo;

use OCA\Contacts\Capabilities;
use OCA\Contacts\ConfigLexicon;
use OCA\Contacts\Dav\PatchPlugin;
use OCA\Contacts\Event\LoadContactsOcaApiEvent;
use OCA\Contacts\Listener\FederatedInviteAcceptedListener;
use OCA\Contacts\Listener\LoadContactsFilesActions;
use OCA\Contacts\Listener\LoadContactsOcaApi;
use OCA\Contacts\Listener\OcmDiscoveryListener;
use OCA\DAV\Events\SabrePluginAddEvent;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\OCM\Events\LocalOCMDiscoveryEvent;
use OCP\OCM\Events\OCMEndpointRequestEvent;
use OCP\OCM\Events\ResourceTypeRegisterEvent;

class Application extends App implements IBootstrap {
public const APP_ID = 'contacts';
Expand All @@ -33,8 +39,15 @@ public function __construct() {
#[\Override]
public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerConfigLexicon(ConfigLexicon::class);

$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadContactsFilesActions::class);
$context->registerEventListener(LoadContactsOcaApiEvent::class, LoadContactsOcaApi::class);
$context->registerEventListener(OCMEndpointRequestEvent::class, FederatedInviteAcceptedListener::class);
$ocmDiscoveryEvent = class_exists(LocalOCMDiscoveryEvent::class)
? LocalOCMDiscoveryEvent::class
: ResourceTypeRegisterEvent::class;
$context->registerEventListener($ocmDiscoveryEvent, OcmDiscoveryListener::class);
}

#[\Override]
Expand Down
189 changes: 189 additions & 0 deletions lib/Command/OcmInvitesConfig.php
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want a command for that? Why is the already existing command to set config values not enough?

If the reason is for documentation, then I think we should drop the custom command and simply extend the documentation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe we misunderstood your suggestion here. Anyway, a dedicated command (with documentation) seemed useful to us.

So is extending the docs the preferred way then?

Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

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

namespace OCA\Contacts\Command;

use OCA\Contacts\AppInfo\Application;
use OCA\Contacts\ConfigLexicon;
use OCA\Contacts\Service\FederatedInvitesService;
use OCP\IAppConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Manage OCM invite configuration.
*
* Usage:
* occ contacts:ocm-invites-config
* occ contacts:ocm-invites-config <key>
* occ contacts:ocm-invites-config <key> <value>
*
* Boolean values accept on/off, true/false, 1/0, and yes/no.
*/
class OcmInvitesConfig extends Command {
public function __construct(
private IAppConfig $appConfig,
) {
parent::__construct();
}

#[\Override]
protected function configure(): void {
$supportedOptions = implode(', ', $this->getSupportedOptions());
$help = <<<HELP
The <info>%command.name%</info> command reads and writes Contacts OCM invite settings.

Supported keys:
{$supportedOptions}

Key info:
<info>ocm_invites_enabled</info> - If set to true then the capability to send and accept invitations to exchange contact info is enabled in the app - bool, default false
<info>ocm_invites_optional_mail</info> - If set to true then sending an invitation by email is optional - bool, default false
<info>ocm_invites_cc_sender</info> - If set to true then the option to send a copy of the invitation to the sender is displayed - bool, default true
<info>ocm_invites_encoded_copy_button</info> - If set to true then the button to copy the encoded invitation is displayed - bool, default false
<info>ocm_invites_disable_ssrf_guard</info> - If set to true SSRF guard will be turned off. Warning: This is for development/testing purposes only!
In production environments the value of this key should always be false - bool, default false
<info>mesh_providers_service</info> - The url that returns the list of mesh providers that will be displayed on the WAYF page - string, default empty

Boolean values accept: on/off, true/false, 1/0, yes/no.

Examples:
<info>occ %command.name%</info>
<info>occ %command.name% ocm_invites_enabled on</info>
<info>occ %command.name% mesh_providers_service "https://mesh.example"</info>

HELP;

$this
->setName('contacts:ocm-invites-config')
->setDescription('Manage OCM invite configuration.')
->addArgument(
'option',
InputArgument::OPTIONAL,
'Config key to read or write. Omit to list supported keys.',
)
->addArgument(
'value',
InputArgument::OPTIONAL,
'Value to write. Omit to read the current value.',
)
->setHelp($help);
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$option = $input->getArgument('option');
$value = $input->getArgument('value');

if ($option === null) {
return $this->listAll($output);
}

if (!in_array($option, $this->getSupportedOptions(), true)) {
$output->writeln(sprintf(
'<error>Unknown OCM invite config key "%s". Allowed: %s.</error>',
$option,
implode(', ', $this->getSupportedOptions()),
));
return self::FAILURE;
}

if ($value === null) {
$output->writeln($this->getCurrentValue($option));
return self::SUCCESS;
}

if ($option === ConfigLexicon::MESH_PROVIDERS_SERVICE) {
$normalised = trim($value);
$current = $this->appConfig->getValueString(Application::APP_ID, $option);
if ($current === $normalised) {
$output->writeln(sprintf('%s is already "%s".', $option, $normalised));
return self::SUCCESS;
}

$this->appConfig->setValueString(Application::APP_ID, $option, $normalised);
$output->writeln(sprintf('%s: "%s"', $option, $normalised));
return self::SUCCESS;
}

$parsed = $this->parseBool($value);
if ($parsed === null) {
$output->writeln(sprintf(
'<error>Cannot parse "%s" as boolean. Use on/off, true/false, 1/0, or yes/no.</error>',
$value,
));
return self::INVALID;
}

$current = $this->appConfig->getValueBool(Application::APP_ID, $option);
if ($current === $parsed) {
$output->writeln(sprintf('%s is already %s.', $option, $this->formatBool($parsed)));
return self::SUCCESS;
}

$this->appConfig->setValueBool(Application::APP_ID, $option, $parsed);
$output->writeln(sprintf('%s: %s', $option, $this->formatBool($parsed)));
return self::SUCCESS;
}

private function listAll(OutputInterface $output): int {
$table = new Table($output);
$table->setHeaders(['option', 'type', 'value']);
foreach ($this->getSupportedOptions() as $key) {
$table->addRow([
$key,
$this->isBooleanOption($key) ? 'bool' : 'string',
$this->getCurrentValue($key),
]);
}
$table->render();
return self::SUCCESS;
}

private function getSupportedOptions(): array {
return [
ConfigLexicon::OCM_INVITES_ENABLED,
...FederatedInvitesService::OCM_INVITES_BOOL_KEYS,
ConfigLexicon::MESH_PROVIDERS_SERVICE,
];
}

private function isBooleanOption(string $option): bool {
return in_array($option, [
ConfigLexicon::OCM_INVITES_ENABLED,
...FederatedInvitesService::OCM_INVITES_BOOL_KEYS,
], true);
}

private function getCurrentValue(string $option): string {
if ($this->isBooleanOption($option)) {
return $this->formatBool($this->appConfig->getValueBool(Application::APP_ID, $option));
}

return $this->appConfig->getValueString(Application::APP_ID, $option);
}

private function formatBool(bool $value): string {
return $value ? 'on' : 'off';
}

private function parseBool(string $raw): ?bool {
$normalised = strtolower(trim($raw));
if (in_array($normalised, ['true', '1', 'on', 'yes'], true)) {
return true;
}
if (in_array($normalised, ['false', '0', 'off', 'no'], true)) {
return false;
}
return null;
}
}
109 changes: 109 additions & 0 deletions lib/ConfigLexicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

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

namespace OCA\Contacts;

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

/**
* Config Lexicon for contacts.
*
* Please add and manage your config keys in this file and keep the
* Lexicon up to date.
*
* {@see ILexicon}
*/
class ConfigLexicon implements ILexicon {
public const OCM_INVITES_ENABLED = 'ocm_invites_enabled';
public const OCM_INVITES_OPTIONAL_MAIL = 'ocm_invites_optional_mail';
public const OCM_INVITES_CC_SENDER = 'ocm_invites_cc_sender';
public const OCM_INVITES_ENCODED_COPY_BUTTON = 'ocm_invites_encoded_copy_button';
public const OCM_INVITES_DISABLE_SSRF_GUARD = 'ocm_invites_disable_ssrf_guard';
public const MESH_PROVIDERS_SERVICE = 'mesh_providers_service';
public const WAYF_ENDPOINT = 'wayf_endpoint';
public const FEDERATIONS_CACHE = 'federations_cache';

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

#[\Override]
public function getAppConfigs(): array {
return [
new Entry(
self::OCM_INVITES_ENABLED,
ValueType::BOOL,
defaultRaw: false,
definition: 'Whether OCM invites for contacts are enabled.',
lazy: true,
),
new Entry(
self::OCM_INVITES_OPTIONAL_MAIL,
ValueType::BOOL,
defaultRaw: false,
definition: 'Whether the recipient email field is optional when creating an OCM invite.',
lazy: true,
),
new Entry(
self::OCM_INVITES_CC_SENDER,
ValueType::BOOL,
defaultRaw: true,
definition: 'Whether the sender is CC-ed on the OCM invite email.',
lazy: true,
),
new Entry(
self::OCM_INVITES_ENCODED_COPY_BUTTON,
ValueType::BOOL,
defaultRaw: false,
definition: 'Whether the invite email "Open invite" button uses the encoded WAYF URL instead of the raw token.',
lazy: true,
),
new Entry(
self::OCM_INVITES_DISABLE_SSRF_GUARD,
ValueType::BOOL,
defaultRaw: false,
definition: 'Unsafe development override that disables private-host and localhost checks for OCM invite discovery.',
lazy: true,
),
new Entry(
self::MESH_PROVIDERS_SERVICE,
ValueType::STRING,
defaultRaw: '',
definition: 'Space-separated list of mesh provider service URLs used for WAYF discovery.',
lazy: true,
),
new Entry(
self::WAYF_ENDPOINT,
ValueType::STRING,
defaultRaw: '',
definition: 'Optional override for the base WAYF endpoint used in invite links.',
note: 'If empty, the app route is used at runtime.',
lazy: true,
),
new Entry(
self::FEDERATIONS_CACHE,
ValueType::ARRAY,
defaultRaw: [],
definition: 'Internal cron-maintained cache for discovered federations and expiry metadata.',
flags: IAppConfig::FLAG_SENSITIVE,
lazy: true,
),
];
}

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