-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathTextBlocksMigrationService.php
More file actions
128 lines (106 loc) · 3.78 KB
/
TextBlocksMigrationService.php
File metadata and controls
128 lines (106 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\UserMigration\Service;
use JsonException;
use OCA\Mail\Service\TextBlockService;
use OCA\Mail\UserMigration\MailAccountMigrator;
use OCP\IL10N;
use OCP\IUser;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\UserMigrationException;
use Symfony\Component\Console\Output\OutputInterface;
class TextBlocksMigrationService {
public const TEXT_BLOCKS_FILE = MailAccountMigrator::EXPORT_ROOT . '/text_blocks.json';
public function __construct(
private readonly TextBlockService $textBlockService,
private readonly IL10N $l10n,
) {
}
/**
* Export all text blocks the user created itself.
* This does not include those shared with others.
*
* @throws UserMigrationException
*/
public function exportTextBlocks(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
$output->writeln(
$this->l10n->t('Exporting text blocks for user %s', [$user->getUID()]),
OutputInterface::VERBOSITY_VERBOSE
);
$textBlocks = $this->textBlockService->findAll($user->getUID());
try {
$exportDestination->addFileContents(self::TEXT_BLOCKS_FILE, json_encode($textBlocks, JSON_THROW_ON_ERROR));
} catch (JsonException|UserMigrationException $exception) {
throw new UserMigrationException(
"Failed to export text blocks for user {$user->getUID()}",
previous: $exception
);
}
}
/**
* Import all text blocks the user created itself on export.
* This does not include those shared with others.
*
* @throws UserMigrationException
*/
public function importTextBlocks(IUser $user, IImportSource $importSource, OutputInterface $output): void {
$output->writeln(
$this->l10n->t('Importing text blocks for user %s', [$user->getUID()]),
OutputInterface::VERBOSITY_VERBOSE
);
try {
$textBlocksFileContent = $importSource->getFileContents(self::TEXT_BLOCKS_FILE);
} catch (UserMigrationException) {
$output->writeln(
$this->l10n->t('Text blocks for user %s not found. Continue...', [$user->getUID()]),
OutputInterface::VERBOSITY_VERBOSE
);
return;
}
$textBlocks = json_decode($textBlocksFileContent, true);
$this->validateTextBlocks($textBlocks);
foreach ($textBlocks as $textBlock) {
$output->writeln(
$this->l10n->t('Importing text block %s for user %s', [$textBlock['title'], $user->getUID()]),
OutputInterface::VERBOSITY_VERBOSE
);
$this->textBlockService->create($user->getUID(), $textBlock['title'], $textBlock['content']);
}
}
public function deleteAllTextBlocks(IUser $user, OutputInterface $output): void {
$output->writeln(
$this->l10n->t('Delete existing text blocks for user %s', [$user->getUID()]),
OutputInterface::VERBOSITY_VERBOSE
);
$this->textBlockService->deleteByUserId($user->getUID());
}
/**
* Validate the parsed text blocks to ensure they
* have the expected structure and types.
*
* @throws UserMigrationException
*/
private function validateTextBlocks(mixed $textBlocks): void {
$textBlocksArrayIsValid = is_array($textBlocks) && array_is_list($textBlocks);
if (!$textBlocksArrayIsValid) {
throw new UserMigrationException('Invalid text blocks export structure');
}
foreach ($textBlocks as $textBlock) {
$textBlockArrayIsValid = is_array($textBlock);
$titleIsValid = $textBlockArrayIsValid
&& array_key_exists('title', $textBlock)
&& is_string($textBlock['title']);
$contentIsValid = $textBlockArrayIsValid
&& array_key_exists('content', $textBlock)
&& is_string($textBlock['content']);
if (!$titleIsValid || !$contentIsValid) {
throw new UserMigrationException('Invalid text block entry');
}
}
}
}