Completed
Pull Request — master (#32303)
by Victor
10:25
created

NotificationManager::getFileNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing\Ocm;
23
24
use OCA\FederatedFileSharing\Ocm\Notification\FileNotification;
25
use OCP\Constants;
26
27
/**
28
 * Class NotificationManager
29
 *
30
 * @package OCA\FederatedFileSharing\Ocm
31
 */
32
class NotificationManager {
33
	/**
34
	 * @param string $type
35
	 *
36
	 * @return FileNotification
37
	 */
38
	public function getFileNotification($type) {
39
		$notification = new FileNotification();
40
		$notification->setNotificationType($type);
41
		return $notification;
42
	}
43
44
	/**
45
	 * @param string $remoteId
46
	 * @param string $token
47
	 * @param string $action
48
	 * @param array $data
49
	 *
50
	 * @return FileNotification
51
	 */
52
	public function convertToOcmFileNotification($remoteId, $token, $action, $data = []) {
53
		$notification = new FileNotification();
54
		$map = [
55
			'accept' => FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED,
56
			'decline' => FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED,
57
			'unshare' => FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED,
58
			'revoke' => FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO,
59
			'permissions' => FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION,
60
			'reshare' => FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE
61
		];
62
		$messages = [
63
			'accept' => "Recipient accepted the share",
64
			'decline' => "Recipient declined the share or unshared it from themself",
65
			'unshare' => "File was unshared",
66
			'revoke' => "Tell the owner (or the sender of a reshare) that the reshare was unshared",
67
			'permissions' => "Tell the owner (or the sender of the reshare) that the permmissions changed",
68
			'reshare' => "Recipient of a share ask the owner to reshare the file with another user"
69
		];
70
71
		$notification->setNotificationType($map[$action]);
72
		$notification->setProviderId($remoteId);
73
		$notification->addNotificationData('sharedSecret', $token);
74
		$notification->addNotificationData('message', $messages[$action]);
75
76
		if ($action === 'permissions') {
77
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
78
			$notification->addNotificationData('permission', $ocmPermissions);
79
		}
80
81
		if ($action === 'reshare') {
82
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
83
			$notification->addNotificationData('permission', $ocmPermissions);
84
			$notification->addNotificationData('senderId', $data['senderId']);
85
			$notification->addNotificationData('shareWith', $data['shareWith']);
86
		}
87
88
		return $notification;
89
	}
90
91
	/**
92
	 * Maps numeric permissions to an array of string permissions
93
	 *
94
	 * @param int $permissions
95
	 *
96
	 * @return array
97
	 */
98
	protected function toOcmPermissions($permissions) {
99
		$permissions = (int) $permissions;
100
		$ocmPermissions = [];
101
		if ($permissions & Constants::PERMISSION_READ) {
102
			$ocmPermissions[] = 'read';
103
		}
104
		if (($permissions & Constants::PERMISSION_CREATE)
105
			|| ($permissions & Constants::PERMISSION_UPDATE)
106
		) {
107
			$ocmPermissions[] = 'write';
108
		}
109
		if ($permissions & Constants::PERMISSION_SHARE) {
110
			$ocmPermissions[] = 'share';
111
		}
112
		return $ocmPermissions;
113
	}
114
}
115