1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license AGPL-3.0 |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\Activity; |
24
|
|
|
|
25
|
|
|
use OCP\Activity\IManager; |
26
|
|
|
use OCP\IConfig; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class UserSettings |
30
|
|
|
* |
31
|
|
|
* @package OCA\Activity |
32
|
|
|
*/ |
33
|
|
|
class UserSettings { |
34
|
|
|
/** @var IManager */ |
35
|
|
|
protected $manager; |
36
|
|
|
|
37
|
|
|
/** @var IConfig */ |
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
/** @var Data */ |
41
|
|
|
protected $data; |
42
|
|
|
|
43
|
|
|
const EMAIL_SEND_HOURLY = 0; |
44
|
|
|
const EMAIL_SEND_DAILY = 1; |
45
|
|
|
const EMAIL_SEND_WEEKLY = 2; |
46
|
|
|
const EMAIL_SEND_ASAP = 3; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param IManager $manager |
50
|
|
|
* @param IConfig $config |
51
|
|
|
*/ |
52
|
25 |
|
public function __construct(IManager $manager, IConfig $config) { |
53
|
25 |
|
$this->manager = $manager; |
54
|
25 |
|
$this->config = $config; |
55
|
25 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get a setting for a user |
59
|
|
|
* |
60
|
|
|
* Falls back to some good default values if the user does not have a preference |
61
|
|
|
* |
62
|
|
|
* @param string $user |
63
|
|
|
* @param string $method Should be one of 'stream', 'email' or 'setting' |
64
|
|
|
* @param string $type One of the activity types, 'batchtime' or 'self' |
65
|
|
|
* @return bool|int |
66
|
|
|
*/ |
67
|
5 |
|
public function getUserSetting($user, $method, $type) { |
68
|
5 |
|
if ($method === 'email' && $this->config->getAppValue('activity', 'enable_email', 'yes') === 'no') { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
$defaultSetting = $this->getDefaultFromSetting($method, $type); |
73
|
5 |
|
if (is_bool($defaultSetting)) { |
74
|
5 |
|
return (bool) $this->config->getUserValue( |
75
|
5 |
|
$user, |
76
|
5 |
|
'activity', |
77
|
5 |
|
'notify_' . $method . '_' . $type, |
78
|
5 |
|
$defaultSetting |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return (int) $this->config->getUserValue( |
83
|
|
|
$user, |
84
|
|
|
'activity', |
85
|
|
|
'notify_' . $method . '_' . $type, |
86
|
|
|
$defaultSetting |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $method |
92
|
|
|
* @param string $type |
93
|
|
|
* @return bool|int |
94
|
|
|
*/ |
95
|
|
|
public function getConfigSetting($method, $type) { |
96
|
|
|
$defaultSetting = $this->getDefaultFromSetting($method, $type); |
97
|
|
|
if (is_bool($defaultSetting)) { |
98
|
|
|
return (bool) $this->config->getAppValue( |
99
|
|
|
'activity', |
100
|
|
|
'notify_' . $method . '_' . $type, |
101
|
|
|
$defaultSetting |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return (int) $this->config->getAppValue( |
106
|
|
|
'activity', |
107
|
|
|
'notify_' . $method . '_' . $type, |
108
|
|
|
$defaultSetting |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a good default setting for a preference |
114
|
|
|
* |
115
|
|
|
* @param string $method Should be one of 'stream', 'email' or 'setting' |
116
|
|
|
* @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail' |
117
|
|
|
* @return bool|int |
118
|
|
|
*/ |
119
|
11 |
|
protected function getDefaultFromSetting($method, $type) { |
120
|
11 |
|
if ($method === 'setting') { |
121
|
9 |
|
if ($type === 'batchtime') { |
122
|
1 |
|
return 3600; |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
if ($type === 'self') { |
126
|
6 |
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
if ($type === 'selfemail') { |
130
|
2 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
try { |
137
|
7 |
|
$setting = $this->manager->getSettingById($type); |
138
|
7 |
|
return ($method === 'stream') ? $setting->isDefaultEnabledStream() : $setting->isDefaultEnabledMail(); |
139
|
5 |
|
} catch (\InvalidArgumentException $e) { |
140
|
5 |
|
return false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get a list with enabled notification types for a user |
146
|
|
|
* |
147
|
|
|
* @param string $user Name of the user |
148
|
|
|
* @param string $method Should be one of 'stream' or 'email' |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
5 |
|
public function getNotificationTypes($user, $method) { |
152
|
5 |
|
$notificationTypes = array(); |
153
|
|
|
|
154
|
5 |
|
$settings = $this->manager->getSettings(); |
155
|
5 |
|
foreach ($settings as $setting) { |
156
|
5 |
|
if ($this->getUserSetting($user, $method, $setting->getIdentifier())) { |
157
|
5 |
|
$notificationTypes[] = $setting->getIdentifier(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
5 |
|
return $notificationTypes; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Filters the given user array by their notification setting |
166
|
|
|
* |
167
|
|
|
* @param array $users |
168
|
|
|
* @param string $method |
169
|
|
|
* @param string $type |
170
|
|
|
* @return array Returns a "username => b:true" Map for method = stream |
171
|
|
|
* Returns a "username => i:batchtime" Map for method = email |
172
|
|
|
*/ |
173
|
|
|
public function filterUsersBySetting($users, $method, $type) { |
174
|
|
|
if (empty($users) || !is_array($users)) { |
175
|
|
|
return []; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($method === 'email' && $this->config->getAppValue('activity', 'enable_email', 'yes') === 'no') { |
179
|
|
|
return []; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$filteredUsers = array(); |
183
|
|
|
$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_' . $method . '_' . $type, $users); |
184
|
|
|
foreach ($potentialUsers as $user => $value) { |
185
|
|
|
if ($value) { |
186
|
|
|
$filteredUsers[$user] = true; |
187
|
|
|
} |
188
|
|
|
unset($users[array_search($user, $users, true)]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Get the batch time setting from the database |
192
|
|
|
if ($method === 'email') { |
193
|
|
|
$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_setting_batchtime', array_keys($filteredUsers)); |
194
|
|
|
foreach ($potentialUsers as $user => $value) { |
195
|
|
|
$filteredUsers[$user] = $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (empty($users)) { |
200
|
|
|
return $filteredUsers; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// If the setting is enabled by default, |
204
|
|
|
// we add all users that didn't set the preference yet. |
205
|
|
|
if ($this->getDefaultFromSetting($method, $type)) { |
206
|
|
|
foreach ($users as $user) { |
207
|
|
|
if ($method === 'stream') { |
208
|
|
|
$filteredUsers[$user] = true; |
209
|
|
|
} else { |
210
|
|
|
$filteredUsers[$user] = $this->getDefaultFromSetting('setting', 'batchtime'); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $filteredUsers; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|