forked from zotero/dataserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifierObserver.inc.php
157 lines (132 loc) · 3.93 KB
/
NotifierObserver.inc.php
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
<?php
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2013 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
class Zotero_NotifierObserver {
private static $messageReceivers = [];
private static $continued = false;
public static function init($messageReceiver=null) {
Zotero_Notifier::registerObserver(
__CLASS__,
["library", "publications", "apikey-library"],
"NotifierObserver"
);
// Send notifications to Redis by default
self::$messageReceivers[] = function ($topic, $message) {
$redis = Z_Redis::get('notifications');
if (!$redis) {
Z_Core::logError('Error: Failed to get Redis client for notifications');
return;
};
$redis->publish($topic, $message);
};
}
public static function setContinued() {
self::$continued = true;
}
public static function addMessageReceiver($messageReceiver) {
self::$messageReceivers[] = $messageReceiver;
}
public static function notify($event, $type, $ids, $extraData) {
if ($type == "library" || $type == "publications") {
switch ($event) {
case "modify":
$event = "topicUpdated";
break;
case "delete":
$event = "topicDeleted";
break;
default:
return;
}
foreach ($ids as $id) {
$libraryID = $id;
// For most libraries, get topic from URI
if ($event != 'topicDeleted') {
// Convert 'http://zotero.org/users/...' to '/users/...'
$topic = str_replace(
Zotero_URI::getBaseURI(), "/", Zotero_URI::getLibraryURI($libraryID)
);
if ($type == 'publications') {
$topic .= '/publications';
}
}
// For deleted libraries (groups), the URI-based method fails,
// so just build from parts
else {
$topic = '/' . Zotero_Libraries::getType($libraryID) . "s/"
. Zotero_Libraries::getLibraryTypeID($libraryID);
}
$message = [
"event" => $event,
"topic" => $topic
];
if (self::$continued) {
$message['continued'] = true;
}
if (!empty($extraData[$id])) {
foreach ($extraData[$id] as $key => $val) {
$message[$key] = $val;
}
}
self::send($topic, $message);
}
}
else if ($type == "apikey-library") {
switch ($event) {
case "add":
$event = "topicAdded";
break;
case "remove":
$event = "topicRemoved";
break;
default:
return;
}
foreach ($ids as $id) {
list($apiKeyID, $libraryID) = explode("-", $id);
// Get topic from URI
$topic = str_replace(
Zotero_URI::getBaseURI(), "/", Zotero_URI::getLibraryURI($libraryID)
);
$message = [
"event" => $event,
"apiKeyID" => $apiKeyID,
"topic" => $topic
];
if (self::$continued) {
$message['continued'] = true;
}
if (!empty($extraData[$id])) {
foreach ($extraData[$id] as $key => $val) {
$message[$key] = $val;
}
}
self::send("api-key:$apiKeyID", $message);
}
}
}
private static function send($channel, $message) {
foreach (self::$messageReceivers as $receiver) {
$receiver(
$channel,
json_encode($message, JSON_UNESCAPED_SLASHES)
);
}
}
}