diff --git a/api.py b/api.py index 3c5cb7f..0f46eed 100644 --- a/api.py +++ b/api.py @@ -32,7 +32,7 @@ class InvalidInstanceID(Exception): def CreateChannel(google_user, client, instance_id, args): - models.Instance.FindOrCreate(instance_id, client) + models.Instance.FindOrCreate(instance_id) token = channel.create_channel( client_id=instance_id, @@ -95,7 +95,7 @@ def Subscribe(google_user, client, instance_id, args): ret = { 'result': 'ok', 'events': models.Subscription.FindOrCreate( - subject, instance, messages, last_id), + subject, client, instance, messages, last_id), } except models.AccessDenied: logging.exception('Subscribe access denied') diff --git a/channel.py b/channel.py index 7d84690..39db032 100644 --- a/channel.py +++ b/channel.py @@ -33,17 +33,23 @@ class OnChannelConnect(webapp2.RequestHandler): class OnChannelDisconnect(webapp2.RequestHandler): + @staticmethod + @db.transactional() + def DeleteInstance(instance_id): + instance = models.Instance.FromID(instance_id) + ret = instance.key() + instance.delete() + return ret + @utils.local_namespace def post(self): instance_id = self.request.get('from') - instance = models.Instance.FromID(instance_id) + instance_key = self.DeleteInstance(instance_id) - subscriptions = models.Subscription.all().filter('instance =', instance) + subscriptions = models.Subscription.all().filter('instance =', instance_key) for subscription in subscriptions: subscription.delete() - instance.delete() - app = webapp2.WSGIApplication([ ('/_ah/channel/connected/', OnChannelConnect), diff --git a/lib/models.py b/lib/models.py index 32b221f..1328e62 100644 --- a/lib/models.py +++ b/lib/models.py @@ -87,24 +87,21 @@ class Client(db.Model): class Instance(db.Model): - client = db.ReferenceProperty(required=True) active = db.BooleanProperty(required=True, default=False) @classmethod @db.transactional() def FromID(cls, instance_id): - # TODO: assert client equality here if possible return cls.get_by_key_name(instance_id) @classmethod @db.transactional() - def FindOrCreate(cls, instance_id, client): + def FindOrCreate(cls, instance_id): instance = cls.get_by_key_name(instance_id) if instance: - # TODO: assert client equality here return instance else: - return cls(key_name=instance_id, client=client).put() + return cls(key_name=instance_id).put() class Subject(db.Model): @@ -242,13 +239,11 @@ class Subscription(db.Model): @classmethod @db.transactional() - def FindOrCreate(cls, subject, instance, messages=0, last_id=None): + def FindOrCreate(cls, subject, client, instance, messages=0, last_id=None): readable_only_by = ( Subject.readable_only_by.get_value_for_datastore(subject)) - client_key = ( - Instance.client.get_value_for_datastore(instance)) if (readable_only_by and - readable_only_by != client_key.parent()): + readable_only_by != client.parent_key()): raise AccessDenied subscriptions = (