# Path: django_eventstream/eventstream.py
# def send_event(channel, event_type, data, skip_user_ids=None, async_publish=True, json_encode=True):
# 	from .event import Event
# 
# 	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
# 
# 	if have_channels():
# 		from .consumers import get_listener_manager
# 		# 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),
# 		json_encode=json_encode)
# 
# 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)
# 
# Path: examples/chat/chat/models.py
# class ChatRoom(models.Model):
#     eid = models.CharField(max_length=64, unique=True)
# 
# class ChatMessage(models.Model):
#     room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE)
#     user = models.CharField(max_length=64)
#     date = models.DateTimeField(auto_now=True, db_index=True)
#     text = models.TextField()
# 
#     def to_data(self):
#         out = {}
#         out['id'] = self.id
#         out['from'] = self.user
#         out['date'] = self.date
#         out['text'] = self.text
#         return out


import json
from django.http import HttpResponse, HttpResponseNotAllowed
from django.db import IntegrityError, transaction
from django.shortcuts import render, redirect
from django.core.serializers.json import DjangoJSONEncoder
from django_eventstream import send_event, get_current_event_id
from .models import ChatRoom, ChatMessage
        last_id = get_current_event_id(['room-{}'.format(room_id)])

        try:
            room = ChatRoom.objects.get(eid=room_id)
            cmsgs = ChatMessage.objects.filter(
                room=room).order_by('-date')[:50]
            msgs = []
            for msg in reversed(cmsgs):
                msgs.append(msg.to_data())
        except ChatRoom.DoesNotExist:
            msgs = []

        context = {}
        context['room_id'] = room_id
        context['last_id'] = last_id
        context['messages'] = msgs
        context['user'] = user
        return render(request, 'chat/chat.html', context)
    else:
        context = {}
        context['room_id'] = room_id or 'default'
        return render(request, 'chat/join.html', context)


def messages(request, room_id):
    if request.method == 'GET':
        last_id = get_current_event_id(['room-{}'.format(room_id)])

        try:
            room = ChatRoom.objects.get(eid=room_id)

# Path: django_eventstream/eventstream.py
# def send_event(channel, event_type, data, skip_user_ids=None, async_publish=True, json_encode=True):
# 	from .event import Event
# 
# 	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
# 
# 	if have_channels():
# 		from .consumers import get_listener_manager
# 		# 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),
# 		json_encode=json_encode)
# 
# 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)
# 
# Path: examples/chat/chat/models.py
# class ChatRoom(models.Model):
#     eid = models.CharField(max_length=64, unique=True)
# 
# class ChatMessage(models.Model):
#     room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE)
#     user = models.CharField(max_length=64)
#     date = models.DateTimeField(auto_now=True, db_index=True)
#     text = models.TextField()
# 
#     def to_data(self):
#         out = {}
#         out['id'] = self.id
#         out['from'] = self.user
#         out['date'] = self.date
#         out['text'] = self.text
#         return out


# Path: examples/chat/chat/views.py
import json
from django.http import HttpResponse, HttpResponseNotAllowed
from django.db import IntegrityError, transaction
from django.shortcuts import render, redirect
from django.core.serializers.json import DjangoJSONEncoder
from django_eventstream import send_event, get_current_event_id
from .models import ChatRoom, ChatMessage

        last_id = get_current_event_id(['room-{}'.format(room_id)])

        try:
            room = ChatRoom.objects.get(eid=room_id)
            cmsgs = ChatMessage.objects.filter(
                room=room).order_by('-date')[:50]
            msgs = []
            for msg in reversed(cmsgs):
                msgs.append(msg.to_data())
        except ChatRoom.DoesNotExist:
            msgs = []

        context = {}
        context['room_id'] = room_id
        context['last_id'] = last_id
        context['messages'] = msgs
        context['user'] = user
        return render(request, 'chat/chat.html', context)
    else:
        context = {}
        context['room_id'] = room_id or 'default'
        return render(request, 'chat/join.html', context)


def messages(request, room_id):
    if request.method == 'GET':
        last_id = get_current_event_id(['room-{}'.format(room_id)])

        try:
            room = ChatRoom.objects.get(eid=room_id)

            cmsgs = ChatMessage.objects.filter(