This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
/
ChatRedisService.php
131 lines (111 loc) · 3.87 KB
/
ChatRedisService.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
<?php
namespace Destiny\Chat;
use Destiny\Common\Application;
use Destiny\Common\Config;
use Destiny\Common\Exception;
use Destiny\Common\Service;
use Destiny\Common\Session\SessionCredentials;
use Destiny\Redis\RedisUtils;
use Redis;
/**
* @method static ChatRedisService instance()
*/
class ChatRedisService extends Service {
/**
* @var integer
*/
public $redisdb;
/**
* @var integer
*/
public $maxlife;
/**
* @var Redis
*/
public $redis;
function afterConstruct() {
parent::afterConstruct();
$this->maxlife = intval(ini_get('session.gc_maxlifetime'));
$this->redisdb = Config::$a['redis']['database'];
$this->redis = Application::instance()->getRedis();
}
private function stripRedisUserIpPrefixes(array $keys) {
return array_filter(array_map(function($n) {
return intval(substr($n, strlen('CHAT:userips-')));
}, $keys), function($n){
return $n != null && $n > 0;
});
}
/**
* Finds all users who share the same IP
* @throws Exception
*/
public function findUserIdsByUsersIp(int $userid): array {
$keys = RedisUtils::callScript('check-sameip-users', [$userid]);
return $this->stripRedisUserIpPrefixes($keys);
}
/**
* Find all users by ip
* @throws Exception
*/
public function findUserIdsByIP(string $ipaddress): array {
$keys = RedisUtils::callScript('check-ip', [$ipaddress]);
return $this->stripRedisUserIpPrefixes($keys);
}
/**
* Find all users by ip (wildcard)
* @throws Exception
*/
public function findUserIdsByIPWildcard(string $ipaddress): array {
$keys = RedisUtils::callScript('check-ip-wildcard', [$ipaddress]);
return $this->stripRedisUserIpPrefixes($keys);
}
/**
* @return array $ipaddresses The addresses found
*/
public function getIPByUserId(int $userid): array {
$redis = Application::instance()->getRedis();
return $redis->zRange('CHAT:userips-' . $userid, 0, -1);
}
/**
* Updates the session ttl so it does not expire
*/
public function renewChatSessionExpiration(string $sessionId) {
$this->redis->expire("CHAT:session-$sessionId", $this->maxlife);
}
public function setChatSession(SessionCredentials $credentials, string $sessionId) {
$this->redis->set("CHAT:session-$sessionId", json_encode($credentials->getData()), $this->maxlife);
}
public function removeChatSession(string $sessionId): int {
return $this->redis->delete("CHAT:session-$sessionId");
}
public function sendRefreshUser(SessionCredentials $credentials): int {
return $this->redis->publish("refreshuser-$this->redisdb", json_encode($credentials->getData()));
}
public function sendBroadcast(string $message): int {
return $this->redis->publish("broadcast-$this->redisdb", json_encode(['data' => $message], JSON_FORCE_OBJECT));
}
public function sendUnban(int $userId) {
$this->redis->publish("unbanuserid-$this->redisdb", "$userId");
}
/**
* Notifies the chat to refresh the bans
* so it actually notices the bans being removed
*/
public function sendPurgeBans() {
$this->redis->publish("refreshbans-$this->redisdb", 'doesnotmatter');
}
public function publishPrivateMessage(array $d): int {
return $this->redis->publish("privmsg-$this->redisdb", json_encode([
'messageid' => "{$d['messageid']}",
'message' => $d['message'],
'username' => $d['username'],
//'userid' => $d['userid'],
//'targetusername' => $d['targetusername'],
'targetuserid' => "{$d['targetuserid']}"
]));
}
public function getChatLog(): array {
return $this->redis->lRange('CHAT:chatlog', 0, -1);
}
}