Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hooks.php « lib « build - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1b7db59a1a9375fcb866eba64cff57f314a2ffa0 (plain)
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
<?php

namespace OCA\OJSXC;

use function foo\func;
use OCA\OJSXC\AppInfo\Application;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\StanzaMapper;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;

use OCP\IUser;
use OCP\IUserSession;

class Hooks
{

	/**
	 * @var IUserManager
	 */
	private $userManager;

	/**
	 * @var IUserSession
	 */
	private $userSession;

	/**
	 * @var PresenceMapper
	 */
	private $presenceMapper;

	/**
	 * @var StanzaMapper
	 */
	private $stanzaMapper;

	/**
	 * @var RosterPush
	 */
	private $rosterPush;

	/**
	 * @var IGroupManager
	 */
	private $groupManager;

	public function __construct(
		IUserManager $userManager,
								IUserSession $userSession,
								RosterPush $rosterPush,
								PresenceMapper $presenceMapper,
								StanzaMapper $stanzaMapper,
								IGroupManager $groupManager
	) {
		$this->userManager = $userManager;
		$this->userSession = $userSession;
		$this->rosterPush = $rosterPush;
		$this->presenceMapper = $presenceMapper;
		$this->stanzaMapper = $stanzaMapper;
		$this->groupManager = $groupManager;
	}

	public static function getInstance()
	{
		$app = new Application();
		return $app->getContainer()->query('UserHooks');
	}

	public static function register()
	{
		\OC::$server->getUserManager()->listen('\OC\User', 'postCreateUser', function (IUser $user, $password) {
			self::getInstance()->onCreateUser($user, $password);
		});

		\OC::$server->getUserManager()->listen('\OC\User', 'postDelete', function (IUser $user) {
			self::getInstance()->onDeleteUser($user);
		});

		\OC::$server->getUserSession()->listen('\OC\User', 'changeUser', function (IUser $user, $feature, $value) {
			self::getInstance()->onChangeUser($user, $feature, $value);
		});

		\OC::$server->getGroupManager()->listen('\OC\Group', 'postAddUser', function (IGroup $group, IUser $user) {
			self::getInstance()->onAddUserToGroup($group, $user);
		});

		\OC::$server->getGroupManager()->listen('\OC\Group', 'postRemoveUser', function (IGroup $group, IUser $user) {
			self::getInstance()->onRemoveUserFromGroup($group, $user);
		});
	}

	/**
	 * @brief when a new user is created, the roster of the users must be updated,
	 * by sending a roster push.
	 * Note that this can still be useful when the roster and contacts menu are
	 * merged, for the internal state.
	 * @see https://tools.ietf.org/html/rfc6121#section-2.1.6
	 * @param IUser $user
	 * @param string $password
	 */
	public function onCreateUser(IUser $user, $password)
	{
		$this->rosterPush->createOrUpdateRosterItem($user);
	}

	/**
	 * @brief when a new user is created, the roster of the users must be updated,
	 * by sending a roster push.
	 * Note that this can still be useful when the roster and contacts menu are
	 * merged, for the internal state. E.g. JSXC removes a chat window, when it
	 * receives this stanza.
	 * @see https://tools.ietf.org/html/rfc6121#section-2.1.6
	 * @param IUser $user
	 */
	public function onDeleteUser(IUser $user)
	{
		$this->rosterPush->removeRosterItem($user->getUID());

		// delete the presence record of this user
		$this->presenceMapper->deletePresence($user->getUID());

		// delete all stanzas addressed to this user
		$this->stanzaMapper->deleteByTo($user->getUID());
	}

	/**
	 * @brief when a use is changed, adapt the roster of the users.
	 * Note that this can still be useful when the roster and contacts menu are
	 * merged, for the internal state. E.g. JSXC removes a chat window, when it
	 * receives this stanza.
	 * @see https://tools.ietf.org/html/rfc6121#section-2.1.6
	 * @param IUser $user
	 * @param string $feature feature which was changed. Enabled and displayName are supported.
	 * @param string $value
	 */
	public function onChangeUser(IUser $user, $feature, $value)
	{
		if ($feature === "enabled") {
			if ($value === "true") {
				// if user is enabled, add to roster
				$this->onCreateUser($user, '');
			} elseif ($value === "false") {
				// if user is enabled, remove from roster
				$this->onDeleteUser($user);
			}
		} elseif ($feature === "displayName") {
			// if the user was changed, resend the whole roster item
			$this->onCreateUser($user, '');
		}
	}

	public function onAddUserToGroup(IGroup $group, IUser $user)
	{
		$this->rosterPush->createOrUpdateRosterItem($user);
		$this->rosterPush->addUserToGroup($user, $group);
	}

	public function onRemoveUserFromGroup(IGroup $group, IUser $user)
	{
		if (Application::contactsStoreApiSupported()) {
			$this->rosterPush->removeRosterItemForUsersInGroup($group, $user->getUID());
		}
		$this->rosterPush->removeUserFromGroup($user, $group);
	}
}