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

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

namespace OCA\OJSXC;

use OCA\OJSXC\Db\IQRosterPush;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\StanzaMapper;
use OCP\IUserManager;

use OCP\IUser;
use OCP\IUserSession;

class Hooks {

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

	/**
	 * @var IQRosterPushMapper
	 */
	private $iqRosterPushMapper;

	private $host;

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

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

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

	public function __construct(IUserManager $userManager,
								IUserSession $userSession, $host,
								IQRosterPushMapper $iqRosterPushMapper,
								PresenceMapper $presenceMapper,
								StanzaMapper $stanzaMapper) {
		$this->userManager = $userManager;
		$this->userSession = $userSession;
		$this->host = $host;
		$this->iqRosterPushMapper = $iqRosterPushMapper;
		$this->presenceMapper = $presenceMapper;
		$this->stanzaMapper = $stanzaMapper;
	}

	public function register() {
		$this->userManager->listen('\OC\User', 'postCreateUser', [$this, 'onCreateUser']);
		$this->userManager->listen('\OC\User', 'postDelete', [$this, 'onDeleteUser']);
		$this->userSession->listen('\OC\User', 'changeUser', [$this, 'onChangeUser']);
	}

	/**
	 * @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) {
		$iq = new IQRosterPush();
		$iq->setJid($user->getUID());
		$iq->setName($user->getDisplayName());
		$iq->setSubscription('both');
		$iq->setFrom('');


		foreach ($this->userManager->search('') as $recipient) {
			if($recipient->getUID() !== $user->getUID()) {
				$iq->setTo($recipient->getUID());
				$this->iqRosterPushMapper->insert($iq);
			}
		}
	}

	/**
	 * @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) {
		$iq = new IQRosterPush();
		$iq->setJid($user->getUID());
		$iq->setName($user->getDisplayName());
		$iq->setSubscription('remove');
		$iq->setFrom('');


		foreach ($this->userManager->search('') as $recipient) {
			if($recipient->getUID() !== $user->getUID()) {
				$iq->setTo($recipient->getUID());
				$this->iqRosterPushMapper->insert($iq);
			}
		}

		// 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, '');

			} else if ($value === "false") {
				// if user is enabled, remove from roster
				$this->onDeleteUser($user);
			}
		} else if ($feature === "displayName") {
			// if the user was changed, resend the whole roster item
			$this->onCreateUser($user, '');
		}
	}

}