Skip to content

Commit ee648fc

Browse files
committed
Allow targeting channels
1 parent 2d82ae5 commit ee648fc

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

files_wcf/js/Bastelstu.be/_Push.js

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ define([ 'Bastelstu.be/core' ], function (core) {
4343
onMessage(message, callback) {
4444
return core.Promise.reject(new Error('Unsupported'))
4545
}
46+
47+
getFeatureFlags() {
48+
return [ ]
49+
}
4650
}
4751

4852
return new Push()

files_wcf/lib/system/push/PushHandler.class.php

+43-28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class PushHandler extends \wcf\system\SingletonFactory {
1919
*/
2020
private $deferred = array();
2121

22+
/**
23+
* Channels to join for this page view.
24+
*
25+
* @var string[]
26+
*/
27+
private $channels = [ ];
28+
2229
/**
2330
* Returns whether a push service is enabled.
2431
*
@@ -43,52 +50,42 @@ public function isRunning() {
4350
return (boolean) $backend::getInstance()->isRunning();
4451
}
4552

53+
/**
54+
* @see \wcf\system\push\PushHandler::getFeatureFlags()
55+
*/
56+
public function getFeatureFlags() {
57+
$backend = PUSH_BACKEND;
58+
if (method_exists($backend, 'getFeatureFlags')) {
59+
return $backend::getInstance()->getFeatureFlags();
60+
}
61+
62+
return [ ];
63+
}
64+
4665
/**
4766
* Sends a message to the connected clients. Returns `true` on success and `false`
4867
* otherwise.
4968
*
50-
* If `$userIDs` is an empty array the message will be sent to every connected client.
51-
* Otherwise the message will only be sent to clients with the given userID.
52-
*
53-
* `$payload` will be made available in the JavaScript code as a parameter. Note that
54-
* a specific push backend may choose to ignore the payload, if it cannot transmit it.
55-
* Make sure that you properly handle this in your code.
56-
*
57-
* ATTENTION: Do NOT (!) send any security related information via sendMessage.
58-
* Not every push service can validate whether the userID given was forged by a malicious client!
69+
* $message must be an array containing the following:
70+
* message: string - The message to send.
71+
* payload: ?array - Additional data to send.
72+
* target: ?array - Targets to send to. The message is send to the Union of Targets.
5973
*
60-
* @param string $message
61-
* @param array<integer> $userIDs
62-
* @param array $payload
74+
* @param array $message
6375
* @return boolean
6476
*/
6577
public function sendMessage($message, array $userIDs = array(), array $payload = array()) {
6678
if (!$this->isEnabled()) return false;
67-
if (!\wcf\data\package\Package::isValidPackageName($message)) return false;
68-
$userIDs = array_unique(\wcf\util\ArrayUtil::toIntegerArray($userIDs));
6979

7080
$backend = PUSH_BACKEND;
7181
return (boolean) $backend::getInstance()->sendMessage($message, $userIDs, $payload);
7282
}
7383

7484
/**
75-
* Registers a deferred message. Returns `true` on any well-formed message and `false`
76-
* otherwise.
77-
* Deferred messages will be sent on shutdown. This can be useful if your handler depends
78-
* on data that may not be written to database yet or to achieve a better performance as the
79-
* page is delivered first.
80-
*
81-
* ATTENTION: Use this method only if your messages are not critical as you cannot check
82-
* whether your message was delivered successfully.
83-
* ATTENTION: Do NOT (!) send any security related information via sendDeferredMessage.
84-
* Not every push service can validate whether the userID given was forged by a malicious client!
85-
*
86-
* @see \wcf\system\push\PushHandler::sendMessage()
85+
* @deprecated
8786
*/
8887
public function sendDeferredMessage($message, array $userIDs = array(), array $payload = array()) {
8988
if (!$this->isEnabled()) return false;
90-
if (!\wcf\data\package\Package::isValidPackageName($message)) return false;
91-
$userIDs = array_unique(\wcf\util\ArrayUtil::toIntegerArray($userIDs));
9289

9390
$this->deferred[] = array(
9491
'message' => $message,
@@ -107,4 +104,22 @@ public function __destruct() {
107104
$this->sendMessage($data['message'], $data['userIDs'], $data['payload']);
108105
}
109106
}
107+
108+
/**
109+
* Joins the given channel for this request.
110+
*
111+
* @param string $name
112+
*/
113+
public function joinChannel($name) {
114+
$this->channels[$name] = $name;
115+
}
116+
117+
/**
118+
* Returns the list of joined channels.
119+
*
120+
* @return string[]
121+
*/
122+
public function getChannels() {
123+
return array_values($this->channels);
124+
}
110125
}

package.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<packagedescription><![CDATA[Push provides an easy to use push server API.]]></packagedescription>
66
<packagedescription language="de"><![CDATA[Push stellt ein einfach zu verwendendes Push-Server-API zur Verfügung.]]></packagedescription>
77
<isapplication>0</isapplication>
8-
<version>2.0.0 RC 1</version>
9-
<date>2016-12-18</date>
8+
<version>2.1.0 Alpha 1</version>
9+
<date>2017-09-25</date>
1010
<license><![CDATA[BSD 3-Clause License <http://opensource.org/licenses/BSD-3-Clause>]]></license>
1111
</packageinformation>
1212

@@ -16,7 +16,7 @@
1616
</authorinformation>
1717

1818
<requiredpackages>
19-
<requiredpackage minversion="3.0.0 RC 1">com.woltlab.wcf</requiredpackage>
19+
<requiredpackage minversion="3.0.8">com.woltlab.wcf</requiredpackage>
2020
<requiredpackage minversion="2.4.1">be.bastelstu.core-js</requiredpackage>
2121
</requiredpackages>
2222

@@ -29,6 +29,11 @@
2929
<instruction type="templateListener" />
3030
</instructions>
3131

32+
<instructions type="update" fromversion="2.0.0 RC 1">
33+
<instruction type="file" application="wcf">files_wcf.tar</instruction>
34+
<instruction type="templateListener" />
35+
</instructions>
36+
3237
<instructions type="update" fromversion="1.1.1">
3338
<instruction type="file" application="wcf">files_wcf.tar</instruction>
3439
<instruction type="templateListener">templateListener.xml</instruction>

0 commit comments

Comments
 (0)