/
TransferOwnership.php
175 lines (153 loc) · 5.61 KB
/
TransferOwnership.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021, Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Command\User;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\RoomService;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TransferOwnership extends Base {
private RoomService $roomService;
private ParticipantService $participantService;
private Manager $manager;
private IUserManager $userManager;
public function __construct(ParticipantService $participantService,
Manager $manager,
IUserManager $userManager) {
parent::__construct();
$this->participantService = $participantService;
$this->manager = $manager;
$this->userManager = $userManager;
}
protected function configure(): void {
$this
->setName('talk:user:transfer-ownership')
->setDescription('Adds the destination-user with the same participant type to all (not one-to-one) conversations of source-user')
->addArgument(
'source-user',
InputArgument::REQUIRED,
'Owner of conversations which shall be moved'
)
->addArgument(
'destination-user',
InputArgument::REQUIRED,
'User who will be the new owner of the conversations'
)
->addOption(
'include-non-moderator',
null,
InputOption::VALUE_NONE,
'Also include conversations where the source-user is a normal user'
)
->addOption(
'remove-source-user',
null,
InputOption::VALUE_NONE,
'Remove the source-user from the conversations'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$sourceUID = $input->getArgument('source-user');
$destinationUID = $input->getArgument('destination-user');
$destinationUser = $this->userManager->get($destinationUID);
if ($destinationUser === null) {
$output->writeln('<error>Destination user could not be found.</error>');
return 1;
}
$includeNonModeratorRooms = $input->getOption('include-non-moderator');
$removeSourceUser = $input->getOption('remove-source-user');
$modified = 0;
$rooms = $this->manager->getRoomsForActor(Attendee::ACTOR_USERS, $sourceUID);
foreach ($rooms as $room) {
if ($room->getType() !== Room::TYPE_GROUP && $room->getType() !== Room::TYPE_PUBLIC) {
// Skip one-to-one, changelog and any other room types
continue;
}
$sourceParticipant = $this->participantService->getParticipantByActor($room, Attendee::ACTOR_USERS, $sourceUID);
if ($sourceParticipant->getAttendee()->getParticipantType() === Participant::USER_SELF_JOINED) {
continue;
}
if (!$includeNonModeratorRooms && !$sourceParticipant->hasModeratorPermissions()) {
continue;
}
try {
$destinationParticipant = $this->participantService->getParticipantByActor($room, Attendee::ACTOR_USERS, $destinationUser->getUID());
$targetType = $this->shouldUpdateParticipantType($sourceParticipant->getAttendee()->getParticipantType(), $destinationParticipant->getAttendee()->getParticipantType());
if ($targetType !== null) {
$this->participantService->updateParticipantType(
$room,
$destinationParticipant,
$sourceParticipant->getAttendee()->getParticipantType()
);
$modified++;
}
} catch (ParticipantNotFoundException $e) {
$this->participantService->addUsers($room, [
[
'actorType' => Attendee::ACTOR_USERS,
'actorId' => $destinationUser->getUID(),
'displayName' => $destinationUser->getDisplayName(),
'participantType' => $sourceParticipant->getAttendee()->getParticipantType(),
]
]);
$modified++;
}
if ($removeSourceUser) {
$this->participantService->removeAttendee($room, $sourceParticipant, Room::PARTICIPANT_REMOVED);
}
}
$output->writeln('<info>Added or promoted user ' . $destinationUser->getUID() . ' in ' . $modified . ' rooms.</info>');
return 0;
}
protected function shouldUpdateParticipantType(int $sourceParticipantType, int $destinationParticipantType): ?int {
if ($sourceParticipantType === Participant::OWNER) {
if ($destinationParticipantType === Participant::OWNER) {
return null;
}
return $sourceParticipantType;
}
if ($sourceParticipantType === Participant::MODERATOR) {
if ($destinationParticipantType === Participant::OWNER || $destinationParticipantType === Participant::MODERATOR) {
return null;
}
return $sourceParticipantType;
}
if ($sourceParticipantType === Participant::USER) {
if ($destinationParticipantType !== Participant::USER_SELF_JOINED) {
return null;
}
return $sourceParticipantType;
}
return null;
}
}