-
Notifications
You must be signed in to change notification settings - Fork 2
/
rt_notifications.module
225 lines (194 loc) · 6.67 KB
/
rt_notifications.module
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* @file
* Contains rt_notifications.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Block\BlockPluginInterface;
/**
* Action constants
*/
const RTN_ACTION_NODE_CREATE = 'node_create';
const RTN_ACTION_NODE_UPDATE = 'node_update';
const RTN_ACTION_NODE_DELETE = 'node_delete';
/**
* Implements hook_requirements().
*/
function rt_notifications_requirements($phase) {
$requirements = [];
$websocket_server_config = Drupal::config('rtn.websocket');
$host = $websocket_server_config->get('host');
$port = $websocket_server_config->get('port');
$connection_is_configured = ($host && $port) ? TRUE : FALSE;
// Check if server connection is set:
$requirements['Real-Time Notifications'] = [
'title' => t('Real-Time Notifications server connection'),
'value' => $connection_is_configured ? "$host:$port" : t('Not configured!'),
];
if (!$connection_is_configured) {
$requirements['Real-Time Notifications']['severity'] = REQUIREMENT_ERROR;
$requirements['Real-Time Notifications']['description'] = t('Missing <a href="/admin/config/system/realtime-notifications">server configuration</a>!');
}
return $requirements;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function rt_notifications_form_user_form_alter(&$form, FormStateInterface $form_state) {
$form['field_rtn_select_users']['#states'] = [
'visible' => array(
':input[name="field_rtn_user_selection_mode"]' => array('value' => 'uid'),
),
];
// Remove own uid and anonymous user from the list.
$build_info = $form_state->getBuildInfo();
/** @var Drupal\user\ProfileForm $object */
$object = $build_info['callback_object'];
$form_user_id = $object->getEntity()->id();
$current_uid = \Drupal::currentuser()->id();
$user_ids = &$form['field_rtn_select_users']['widget']['#options'];
// If creating a new user or editing someone else's profile.
if (!$form_user_id || $form_user_id != $current_uid) {
unset($user_ids[$form_user_id], $user_ids[0]);
}
else {
unset($user_ids[$current_uid], $user_ids[0]);
}
$form['field_rtn_user_settings'] = [
'#type' => 'details',
'#title' => t('Real-Time Notifications'),
'#open' => TRUE,
'#weight' => 20,
];
foreach (['field_rtn_actions', 'field_rtn_select_users', 'field_rtn_user_selection_mode'] as $field) {
if (isset($form[$field])) {
$form['field_rtn_user_settings'][$field] = $form[$field];
unset($form[$field]);
}
}
}
/**
* Get list of subscribers (user IDs).
*
* @param $uid
* User ID.
*
* @param $action
* Action machine name.
*
* @return array
*/
function rt_notifications_get_subscribers($uid, $action) {
$cid = "rt_notifications_subscribers:$action:$uid";
$tags = ['rt_notifications_subscribers'];
$cache_backend = \Drupal::cache();
$db = \Drupal::database();
if ($cache = $cache_backend->get($cid)) {
$subscribers = $cache->data;
}
else {
// Get all explicitly subscribed users.
$query = $db->select('user__field_rtn_actions', 'rtna');
$query->join('user__field_rtn_select_users', 'rtnsu', 'rtna.entity_id = rtnsu.entity_id');
$uid_subscribers = $query->fields('rtnsu', ['entity_id'])
->condition('rtna.field_rtn_actions_value', $action)
->condition('rtnsu.field_rtn_select_users_target_id', $uid)
->execute()->fetchCol();
// Get users subscribed to "All".
$query = $db->select('user__field_rtn_user_selection_mode', 'rtnusm');
$query->join('user__field_rtn_actions', 'rtna', 'rtnusm.entity_id = rtna.entity_id');
$all_subscribers = $query->fields('rtnusm', ['entity_id'])
->condition('rtnusm.field_rtn_user_selection_mode_value', 'all')
->condition('rtna.field_rtn_actions_value', $action)
->execute()->fetchCol();
$subscribers = array_unique(array_merge($uid_subscribers, $all_subscribers));
sort($subscribers);
$cache_backend->set($cid, $subscribers, CacheBackendInterface::CACHE_PERMANENT, $tags);
}
return $subscribers;
}
/**
* Implements hook_ENTITY_TYPE_insert()) for users.
*/
function rt_notifications_user_insert(EntityInterface $entity) {
Cache::invalidateTags(['rt_notifications_subscribers']);
}
/**
* Implements hook_ENTITY_TYPE_update() for users.
*/
function rt_notifications_user_update(EntityInterface $entity) {
Cache::invalidateTags(['rt_notifications_subscribers']);
}
/**
* Implements hook_ENTITY_TYPE_delete()) for users.
*/
function rt_notifications_user_delete(EntityInterface $entity) {
Cache::invalidateTags(['rt_notifications_subscribers']);
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function rt_notifications_node_insert(EntityInterface $entity) {
_send_message_to_subscribers($entity, RTN_ACTION_NODE_CREATE);
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function rt_notifications_node_update(EntityInterface $entity) {
_send_message_to_subscribers($entity, RTN_ACTION_NODE_UPDATE);
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function rt_notifications_node_delete(EntityInterface $entity) {
_send_message_to_subscribers($entity, RTN_ACTION_NODE_DELETE);
}
/**
* Implements hook_theme().
*/
function rt_notifications_theme($existing, $type, $theme, $path) {
return [
'rt_notifications_block' => [
'variables' => [
'notifications' => [],
'count' => 0,
],
],
];
}
/**
* Implements hook_block_view_BASE_ID_alter().
*/
function rt_notifications_block_view_rt_notifications_block_alter(array &$build, BlockPluginInterface $block) {
$build['#contextual_links'] = [];
}
/**
* Helper function for sending message via WebSocket.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param $action
*/
function _send_message_to_subscribers(EntityInterface $entity, $action) {
$webSocketConnector = \Drupal::service('rtn.websocket_client');
$webSocketConnector->connect();
$client = $webSocketConnector->getClient();
$action_str = str_replace('node_', '', $action) . 'd';
// Send message to all users which are subscribed on logged-in user and this action.
if ($client) {
$entity_title = $entity->get('title')->value;
$current_user_id = \Drupal::currentUser()->id();
$current_user_name = \Drupal::currentUser()->getDisplayName();
$subscribed_users = rt_notifications_get_subscribers($current_user_id, $action);
$text = "The user %current_user_name has %action_str the node %entity_title";
$variables = [
'%current_user_name' => $current_user_name,
'%action_str' => $action_str,
'%entity_title' => $entity_title,
];
$webSocketConnector->sendMessage($subscribed_users, $text, $variables);
$webSocketConnector->disconnect();
}
}