Allow subscribe to return events from a subject even when not creating a subscription

This commit is contained in:
Ian Gulliver
2014-06-01 21:14:58 -07:00
parent c004d27e9d
commit 5079aac9a6
3 changed files with 37 additions and 10 deletions

9
api.py
View File

@@ -114,16 +114,17 @@ def SendMessage(google_user, client, instance_id, args):
def Subscribe(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id)
subject = models.Subject.FindOrCreate(args['subject'])
messages = args.get('messages', 0)
last_id = args.get('last_id', None)
if not instance or not instance.active:
# Probably a race with the channel opening
return {
'result': 'retry',
'events': subject.GetEvents(messages, last_id),
}
subject = models.Subject.FindOrCreate(args['subject'])
messages = args.get('messages', 0)
last_id = args.get('last_id', None)
try:
return {
'result': 'ok',

View File

@@ -297,6 +297,15 @@ class Subject(db.Model):
ret['writable_only_by'] = str(writable_only_by)
return ret
@db.transactional()
def GetEvents(self, messages, last_id):
events = [m.ToEvent() for m in self.GetPins()]
if messages:
events.extend(m.ToEvent() for m in self.GetRecentMessages(messages))
if last_id is not None:
events.extend(m.ToEvent() for m in self.GetMessagesSince(last_id))
return events
class Subscription(db.Model):
# parent=Subject
@@ -319,12 +328,7 @@ class Subscription(db.Model):
.fetch(1))
if not subscriptions:
cls(parent=subject, instance=instance).put()
events = [m.ToEvent() for m in subject.GetPins()]
if messages:
events.extend(m.ToEvent() for m in subject.GetRecentMessages(messages))
if last_id is not None:
events.extend(m.ToEvent() for m in subject.GetMessagesSince(last_id))
return events
return subject.GetEvents(messages, last_id)
@classmethod
@db.transactional()

View File

@@ -117,6 +117,28 @@ asyncTest('Message round trip', function() {
cosmo.subscribe(subject, -1);
});
asyncTest('Message round trip without channel', function() {
expect(2);
var subject = randstring();
var message = randstring();
var callbacks = {
'onMessage': function(e) {
equal(e['subject']['name'], subject, 'subject matches');
equal(e['message'], message, 'message matches');
cosmo.shutdown();
start();
}
};
var cosmo = new Cosmopolite(callbacks, null, randstring());
cosmo.channelState_ = Cosmopolite.ChannelState_.OPENING;
cosmo.sendMessage(subject, message);
cosmo.subscribe(subject, -1);
});
asyncTest('Complex object', function() {
expect(2);