-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.py
56 lines (46 loc) · 1.67 KB
/
channel.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
import webapp2_extras.appengine.auth.models as auth_models
import datetime
from google.appengine.ext import ndb
import logging
from google.appengine.api import channel
import json
class Channel(ndb.Model):
token = ndb.StringProperty()
tokenExpires = ndb.DateTimeProperty()
@classmethod
def makeMessage(cls,html):
epoch = datetime.datetime.utcfromtimestamp(0)
delta = datetime.datetime.now() - epoch
timestamp = delta.total_seconds()
message = {
'notificationHTML': html,
'timestamp': timestamp
}
return json.dumps(message)
@classmethod
def createChannel(cls, clientID):
now = datetime.datetime.now()
c = Channel()
c.token = channel.create_channel(clientID, duration_minutes=1440)
c.tokenExpires = now + datetime.timedelta(days=1)
logging.info('Creating new token: %s' % c.token)
c.put()
return c
@classmethod
def broadcast(cls, messageHTML):
q = cls.query()
channels = q.fetch(100)
for c in channels:
if c.tokenExpires > datetime.datetime.now():
channel.send_message(c.token, cls.makeMessage(messageHTML))
@classmethod
def getChannel(cls, clientID):
q = cls.query()
channels = q.fetch(10)
channelToReturn = None
for c in channels:
if c.tokenExpires > datetime.datetime.now():
channelToReturn = c
if channelToReturn is None:
channelToReturn = cls.createChannel(clientID)
return channelToReturn