diff --git a/api.py b/api.py index b7f8873..1ab8815 100644 --- a/api.py +++ b/api.py @@ -28,8 +28,10 @@ import config def CreateChannel(google_user, client, instance_id, args): + instance = models.Instance.FindOrCreate(instance_id, client) + token = channel.create_channel( - client_id=str(client.key()) + '/' + instance_id, + client_id=str(instance), duration_minutes=config.CHANNEL_DURATION_SECONDS / 60) events = [] if google_user: diff --git a/channel.py b/channel.py index 4fb4259..8ffdcd2 100644 --- a/channel.py +++ b/channel.py @@ -25,23 +25,23 @@ class OnChannelConnect(webapp2.RequestHandler): @utils.local_namespace @db.transactional() def post(self): - client_key, instance_id = self.request.get('from').split('/', 1) - client = models.Client.get(client_key) - instance = models.Instance.FindOrCreate(instance_id, client) + instance_key = self.request.get('from') + instance = models.Instance.get(instance_key) + instance.active = True + instance.put() class OnChannelDisconnect(webapp2.RequestHandler): @utils.local_namespace def post(self): - client_key, instance_id = self.request.get('from').split('/', 1) - client = models.Client.get(client_key) - instance = models.Instance.FindOrCreate(instance_id, client) + instance_key = self.request.get('from') + instance = models.Instance.get(instance_key) subscriptions = models.Subscription.all().filter('instance =', instance) for subscription in subscriptions: subscription.delete() - models.Instance.get(instance).delete() + instance.delete() app = webapp2.WSGIApplication([ diff --git a/lib/models.py b/lib/models.py index 6d88bd9..2c26291 100644 --- a/lib/models.py +++ b/lib/models.py @@ -89,6 +89,7 @@ class Instance(db.Model): # parent=Client id_ = db.StringProperty(required=True) + active = db.BooleanProperty(required=True, default=False) @classmethod @db.transactional() @@ -112,11 +113,6 @@ class Instance(db.Model): else: return cls(parent=client, id_=instance_id).put() - def SendMessage(self, msg): - channel.send_message( - str(self.parent_key()) + '/' + self.id_, - json.dumps(msg, default=utils.EncodeJSON)) - class Subject(db.Model): @@ -231,7 +227,7 @@ class Subject(db.Model): obj, subscriptions = self.PutMessage(message, sender, sender_message_id, key) event = obj.ToEvent() for subscription in subscriptions: - subscription.instance.SendMessage(event) + subscription.SendMessage(event) def ToDict(self): ret = { @@ -284,6 +280,12 @@ class Subscription(db.Model): for subscription in subscriptions: subscription.delete() + def SendMessage(self, msg): + instance_key = Subscription.instance.get_value_for_datastore(self) + channel.send_message( + str(instance_key), + json.dumps(msg, default=utils.EncodeJSON)) + class Message(db.Model): # parent=Subject