-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy patheventstream.py
178 lines (142 loc) · 4.99 KB
/
eventstream.py
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
import copy
import json
import logging
from django.core.serializers.json import DjangoJSONEncoder
from .storage import EventDoesNotExist
from .eventresponse import EventResponse
from .utils import (
make_id,
publish_event,
publish_kick,
get_storage,
get_channelmanager,
)
from django.conf import settings
logger = logging.getLogger(__name__)
class EventPermissionError(Exception):
def __init__(self, message, channels=None):
super(Exception, self).__init__(message)
if channels is None:
channels = []
self.channels = copy.deepcopy(channels)
# Configuration de la connexion Redis
redis_client = None
if hasattr(settings, "EVENTSTREAM_REDIS"):
try:
import redis
except ImportError:
raise ImportError(
"You must install the redis package to use RedisListener for multiprocess event handling. \n pip install redis"
)
redis_client = redis.Redis(**settings.EVENTSTREAM_REDIS)
def send_event(
channel, event_type, data, skip_user_ids=None, async_publish=True, json_encode=True
):
from .event import Event
from .views import get_listener_manager
if json_encode:
data = json.dumps(data, cls=DjangoJSONEncoder)
if skip_user_ids is None:
skip_user_ids = []
storage = get_storage()
channelmanager = get_channelmanager()
if channelmanager.is_channel_reliable(channel) and storage:
e = storage.append_event(channel, event_type, data)
pub_id = str(e.id)
pub_prev_id = str(e.id - 1)
else:
e = Event(channel, event_type, data)
pub_id = None
pub_prev_id = None
# Publish event to Redis Pub/Sub if enabled
if redis_client:
redis_message = {
"channel": channel,
"event_type": event_type,
"data": data,
}
redis_client.publish("events_channel", json.dumps(redis_message))
else:
# Send to local listeners
get_listener_manager().add_to_queues(channel, e)
# Publish through grip proxy
publish_event(
channel,
event_type,
data,
pub_id,
pub_prev_id,
skip_user_ids=skip_user_ids,
blocking=(not async_publish),
)
def get_events(request, limit=100, user=None):
if user is None:
user = request.user
resp = EventResponse()
resp.is_next = request.is_next
resp.is_recover = request.is_recover
resp.user = user
if len(request.channels) == 0:
return resp
limit_per_type = int(limit / len(request.channels))
if limit_per_type < 1:
limit_per_type = 1
storage = get_storage()
channelmanager = get_channelmanager()
inaccessible_channels = []
for channel in request.channels:
if not channelmanager.can_read_channel(user, channel):
inaccessible_channels.append(channel)
if len(inaccessible_channels) > 0:
msg = "Permission denied to channels: %s" % (", ".join(inaccessible_channels))
raise EventPermissionError(msg, channels=inaccessible_channels)
for channel in request.channels:
reset = False
last_id = request.channel_last_ids.get(channel)
more = False
if channelmanager.is_channel_reliable(channel) and storage:
if last_id is not None:
try:
events = storage.get_events(
channel, int(last_id), limit=limit_per_type + 1
)
if len(events) >= limit_per_type + 1:
events = events[:limit_per_type]
more = True
except EventDoesNotExist as e:
reset = True
events = []
last_id = str(e.current_id)
else:
events = []
last_id = str(storage.get_current_id(channel))
else:
events = []
last_id = None
resp.channel_items[channel] = events
if last_id is not None:
resp.channel_last_ids[channel] = last_id
if reset:
resp.channel_reset.add(channel)
if more:
last_id_before_limit = events[-1].id
request.channel_last_ids[channel] = last_id_before_limit
resp.channel_more.add(channel)
return resp
def get_current_event_id(channels):
storage = get_storage()
if not storage:
raise ValueError("get_current_event_id requires storage to be enabled")
cur_ids = {}
for channel in channels:
cur_ids[channel] = str(storage.get_current_id(channel))
return make_id(cur_ids)
def channel_permission_changed(user, channel):
from .views import get_listener_manager
channelmanager = get_channelmanager()
if not channelmanager.can_read_channel(user, channel):
user_id = user.id if user else "anonymous"
# kick local listeners
get_listener_manager().kick(user_id, channel)
# kick users connected to grip proxy
publish_kick(user_id, channel)