Completed
Pull Request — master (#133)
by Joas
03:01
created

Hooks::setDefaultsForUser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 20
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 OCA\Activity\AppInfo\Application;
26
use OCP\IDBConnection;
27
28
/**
29
 * Handles the stream and mail queue of a user when he is being deleted
30
 */
31
class Hooks {
32
	/**
33
	 * Delete remaining activities and emails when a user is deleted
34
	 *
35
	 * @param array $params The hook params
36
	 */
37 1
	static public function deleteUser($params) {
38 1
		$connection = \OC::$server->getDatabaseConnection();
39 1
		self::deleteUserStream($params['uid']);
40 1
		self::deleteUserMailQueue($connection, $params['uid']);
41 1
	}
42
43
	/**
44
	 * Delete all items of the stream
45
	 *
46
	 * @param string $user
47
	 */
48 1
	static protected function deleteUserStream($user) {
49
		// Delete activity entries
50 1
		$app = new Application();
51
		/** @var Data $activityData */
52 1
		$activityData = $app->getContainer()->query(Data::class);
53 1
		$activityData->deleteActivities(array('affecteduser' => $user));
54 1
	}
55
56
	/**
57
	 * Delete all mail queue entries
58
	 *
59
	 * @param IDBConnection $connection
60
	 * @param string $user
61
	 */
62 1
	static protected function deleteUserMailQueue(IDBConnection $connection, $user) {
63
		// Delete entries from mail queue
64 1
		$queryBuilder = $connection->getQueryBuilder();
65
66 1
		$queryBuilder->delete('activity_mq')
67 1
			->where($queryBuilder->expr()->eq('amq_affecteduser', $queryBuilder->createParameter('user')))
68 1
			->setParameter('user', $user);
69 1
		$queryBuilder->execute();
70 1
	}
71
72
	static public function setDefaultsForUser($params) {
73
		$config = \OC::$server->getConfig();
74
		if ($config->getUserValue($params['uid'], 'activity','notify_setting_batchtime', null) !== null) {
75
			// Already has settings
76
			return;
77
		}
78
79
		foreach ($config->getAppKeys('activity') as $key) {
80
			if (strpos($key, 'notify_') !== 0) {
81
				continue;
82
			}
83
84
			$config->setUserValue(
85
				$params['uid'],
86
				'activity',
87
				$key,
88
				$config->getAppValue('activity', $key)
89
			);
90
		}
91
	}
92
}
93