2014-03-25 13:43:11 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# Copyright 2014, Ian Gulliver
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from google.appengine.api import channel
|
|
|
|
|
from google.appengine.ext import db
|
|
|
|
|
|
2014-05-01 11:33:29 -07:00
|
|
|
import utils
|
|
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
|
|
|
|
|
# Profile
|
|
|
|
|
# ↳ Client
|
|
|
|
|
#
|
|
|
|
|
# Subject
|
2014-05-11 15:43:45 +03:00
|
|
|
# ↳ Message
|
2014-03-25 13:43:11 -07:00
|
|
|
# ↳ Subscription (⤴︎ Client)
|
|
|
|
|
|
|
|
|
|
|
2014-05-16 23:07:38 +03:00
|
|
|
class DuplicateMessage(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2014-05-19 20:52:57 +03:00
|
|
|
class AccessDenied(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
class Profile(db.Model):
|
|
|
|
|
google_user = db.UserProperty()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def FromGoogleUser(cls, google_user):
|
|
|
|
|
if not google_user:
|
|
|
|
|
profile = Profile()
|
|
|
|
|
profile.put()
|
|
|
|
|
return profile
|
|
|
|
|
|
|
|
|
|
profiles = Profile.all().filter('google_user =', google_user).fetch(1)
|
|
|
|
|
if profiles:
|
|
|
|
|
return profiles[0]
|
|
|
|
|
else:
|
|
|
|
|
# TODO(flamingcow): Fetch-then-store uniqueness is a race.
|
|
|
|
|
profile = Profile(google_user=google_user)
|
|
|
|
|
profile.put()
|
|
|
|
|
return profile
|
|
|
|
|
|
|
|
|
|
def MergeFrom(self, source_profile):
|
2014-05-16 22:35:20 +03:00
|
|
|
# This is non-transactional and racy (new messages can be introduced by the
|
|
|
|
|
# old client after we start). This is hard to solve because a) we're not in
|
|
|
|
|
# a single hierarchy and b) we don't revoke the old client ID, so it can
|
|
|
|
|
# still be used.
|
|
|
|
|
for message in Message.all().filter('sender =', source_profile):
|
|
|
|
|
message.sender = self;
|
|
|
|
|
message.put()
|
2014-03-25 13:43:11 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Client(db.Model):
|
2014-05-06 22:46:07 -07:00
|
|
|
# parent=Profile
|
2014-05-01 09:57:50 -07:00
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
first_seen = db.DateTimeProperty(required=True, auto_now_add=True)
|
|
|
|
|
channel_active = db.BooleanProperty(required=True, default=False)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def FromProfile(cls, profile):
|
|
|
|
|
client = cls(parent=profile)
|
|
|
|
|
client.put()
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def FromGoogleUser(cls, google_user):
|
|
|
|
|
profile = Profile.FromGoogleUser(google_user)
|
|
|
|
|
return cls.FromProfile(profile)
|
|
|
|
|
|
2014-03-25 14:19:13 -07:00
|
|
|
def SendMessage(self, msg):
|
2014-05-06 22:46:07 -07:00
|
|
|
self.SendByKey(self.key(), msg)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def SendByKey(key, msg):
|
|
|
|
|
channel.send_message(str(key), json.dumps(msg, default=utils.EncodeJSON))
|
2014-03-25 14:19:13 -07:00
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
|
|
|
|
|
class Subject(db.Model):
|
2014-05-17 18:50:12 +03:00
|
|
|
|
|
|
|
|
name = db.StringProperty(required=True)
|
2014-05-17 19:13:59 +03:00
|
|
|
readable_only_by = db.ReferenceProperty(
|
|
|
|
|
reference_class=Profile, collection_name='readable_subject_set')
|
|
|
|
|
writable_only_by = db.ReferenceProperty(
|
|
|
|
|
reference_class=Profile, collection_name='writable_subject_set')
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-18 10:58:39 +03:00
|
|
|
next_message_id = db.IntegerProperty(required=True, default=1)
|
|
|
|
|
|
2014-05-06 22:46:07 -07:00
|
|
|
@classmethod
|
2014-05-19 20:52:57 +03:00
|
|
|
def FindOrCreate(cls, subject):
|
|
|
|
|
if 'readable_only_by' in subject:
|
|
|
|
|
readable_only_by = Profile.get(subject['readable_only_by'])
|
|
|
|
|
else:
|
|
|
|
|
readable_only_by = None
|
|
|
|
|
|
|
|
|
|
if 'writable_only_by' in subject:
|
|
|
|
|
writable_only_by = Profile.get(subject['writable_only_by'])
|
|
|
|
|
else:
|
|
|
|
|
writable_only_by = None
|
|
|
|
|
|
|
|
|
|
subjects = (
|
|
|
|
|
cls.all()
|
|
|
|
|
.filter('name =', subject['name'])
|
|
|
|
|
.filter('readable_only_by =', readable_only_by)
|
|
|
|
|
.filter('writable_only_by =', writable_only_by)
|
|
|
|
|
.fetch(1))
|
2014-05-17 18:50:12 +03:00
|
|
|
if subjects:
|
|
|
|
|
return subjects[0]
|
2014-05-19 20:52:57 +03:00
|
|
|
subject = cls(
|
|
|
|
|
name=subject['name'],
|
|
|
|
|
readable_only_by=readable_only_by,
|
|
|
|
|
writable_only_by=writable_only_by)
|
2014-05-13 19:12:06 +03:00
|
|
|
subject.put()
|
|
|
|
|
return subject
|
2014-05-06 22:46:07 -07:00
|
|
|
|
|
|
|
|
@db.transactional()
|
2014-05-11 15:43:45 +03:00
|
|
|
def GetRecentMessages(self, num_messages):
|
2014-05-06 22:46:07 -07:00
|
|
|
query = (
|
|
|
|
|
Message.all()
|
|
|
|
|
.ancestor(self)
|
2014-05-18 10:58:39 +03:00
|
|
|
.order('-id_'))
|
2014-05-06 22:46:07 -07:00
|
|
|
if num_messages <= 0:
|
|
|
|
|
num_messages = None
|
2014-05-10 19:20:14 +02:00
|
|
|
return reversed(query.fetch(limit=num_messages))
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-18 11:20:44 +03:00
|
|
|
@db.transactional()
|
|
|
|
|
def GetMessagesSince(self, last_id):
|
|
|
|
|
query = (
|
|
|
|
|
Message.all()
|
|
|
|
|
.ancestor(self)
|
|
|
|
|
.filter('id_ >', last_id)
|
|
|
|
|
.order('id_'))
|
|
|
|
|
return list(query)
|
|
|
|
|
|
2014-05-06 22:46:07 -07:00
|
|
|
@db.transactional()
|
2014-05-11 15:43:45 +03:00
|
|
|
def GetKey(self, key):
|
|
|
|
|
messages = (
|
|
|
|
|
Message.all()
|
|
|
|
|
.ancestor(self)
|
|
|
|
|
.filter('key_ =', key)
|
2014-05-18 10:58:39 +03:00
|
|
|
.order('-id_')
|
2014-05-11 15:43:45 +03:00
|
|
|
.fetch(1))
|
|
|
|
|
if messages:
|
|
|
|
|
return messages[0]
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
@db.transactional()
|
2014-05-16 23:07:38 +03:00
|
|
|
def PutMessage(self, message, sender, sender_message_id, key=None):
|
2014-05-15 16:30:08 +03:00
|
|
|
"""Internal helper for SendMessage().
|
|
|
|
|
|
|
|
|
|
Unless/until channel.send_message becomes transactional, we have to finish
|
|
|
|
|
the datastore work (and any retries) before we start transmitting to
|
|
|
|
|
channels.
|
|
|
|
|
"""
|
2014-05-18 21:58:40 +03:00
|
|
|
# We have to reload the Subject inside the transaction to get transactional
|
|
|
|
|
# ID generation
|
|
|
|
|
subject = Subject.get(self.key())
|
|
|
|
|
|
2014-05-16 23:07:38 +03:00
|
|
|
# sender_message_id should be universal across all subjects, but we check
|
|
|
|
|
# it within just this subject to allow in-transaction verification.
|
|
|
|
|
messages = (
|
|
|
|
|
Message.all()
|
2014-05-18 21:58:40 +03:00
|
|
|
.ancestor(subject)
|
2014-05-16 23:07:38 +03:00
|
|
|
.filter('sender_message_id =', sender_message_id)
|
|
|
|
|
.fetch(1))
|
|
|
|
|
if messages:
|
|
|
|
|
raise DuplicateMessage(sender_message_id)
|
|
|
|
|
|
2014-05-18 21:58:40 +03:00
|
|
|
message_id = subject.next_message_id
|
|
|
|
|
subject.next_message_id += 1
|
|
|
|
|
subject.put()
|
2014-05-18 10:58:39 +03:00
|
|
|
|
2014-05-16 23:07:38 +03:00
|
|
|
obj = Message(
|
2014-05-18 21:58:40 +03:00
|
|
|
parent=subject,
|
2014-05-16 23:07:38 +03:00
|
|
|
message=message,
|
|
|
|
|
sender=sender,
|
|
|
|
|
sender_message_id=sender_message_id,
|
2014-05-18 10:58:39 +03:00
|
|
|
id_=message_id,
|
2014-05-16 23:07:38 +03:00
|
|
|
key_=key)
|
2014-05-06 22:46:07 -07:00
|
|
|
obj.put()
|
|
|
|
|
|
2014-05-15 16:30:08 +03:00
|
|
|
return (
|
|
|
|
|
obj,
|
|
|
|
|
[Subscription.client.get_value_for_datastore(subscription)
|
2014-05-18 21:58:40 +03:00
|
|
|
for subscription in Subscription.all().ancestor(subject)])
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-16 23:07:38 +03:00
|
|
|
def SendMessage(self, message, sender, sender_message_id, key=None):
|
2014-05-19 20:52:57 +03:00
|
|
|
writable_only_by = Subject.writable_only_by.get_value_for_datastore(self)
|
|
|
|
|
if (writable_only_by and
|
|
|
|
|
writable_only_by != sender):
|
|
|
|
|
raise AccessDenied
|
2014-05-16 23:07:38 +03:00
|
|
|
obj, subscriptions = self.PutMessage(message, sender, sender_message_id, key)
|
2014-05-15 16:30:08 +03:00
|
|
|
event = obj.ToEvent()
|
|
|
|
|
for subscription in subscriptions:
|
|
|
|
|
Client.SendByKey(subscription, event)
|
2014-03-25 13:43:11 -07:00
|
|
|
|
2014-05-19 20:52:57 +03:00
|
|
|
def ToDict(self):
|
|
|
|
|
ret = {
|
|
|
|
|
'name': self.name,
|
|
|
|
|
}
|
|
|
|
|
readable_only_by = Subject.readable_only_by.get_value_for_datastore(self)
|
|
|
|
|
if readable_only_by:
|
|
|
|
|
ret['readable_only_by'] = readable_only_by
|
|
|
|
|
writable_only_by = Subject.writable_only_by.get_value_for_datastore(self)
|
|
|
|
|
if writable_only_by:
|
|
|
|
|
ret['writable_only_by'] = writable_only_by
|
|
|
|
|
return ret
|
|
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
|
|
|
|
|
class Subscription(db.Model):
|
2014-05-06 22:46:07 -07:00
|
|
|
# parent=Subject
|
2014-05-01 09:57:50 -07:00
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
client = db.ReferenceProperty(reference_class=Client)
|
2014-05-06 22:46:07 -07:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
@db.transactional()
|
2014-05-18 11:20:44 +03:00
|
|
|
def FindOrCreate(cls, subject, client, messages=0, last_id=None):
|
2014-05-19 20:52:57 +03:00
|
|
|
readable_only_by = (
|
|
|
|
|
Subject.readable_only_by.get_value_for_datastore(subject))
|
|
|
|
|
if (readable_only_by and
|
|
|
|
|
readable_only_by != client.parent_key()):
|
|
|
|
|
raise AccessDenied
|
|
|
|
|
|
2014-05-06 22:46:07 -07:00
|
|
|
subscriptions = (
|
|
|
|
|
cls.all(keys_only=True)
|
|
|
|
|
.ancestor(subject)
|
|
|
|
|
.filter('client =', client)
|
|
|
|
|
.fetch(1))
|
|
|
|
|
if not subscriptions:
|
|
|
|
|
cls(parent=subject, client=client).put()
|
2014-05-18 11:20:44 +03:00
|
|
|
events = []
|
|
|
|
|
if messages:
|
|
|
|
|
events.extend(m.ToEvent() for m in subject.GetRecentMessages(messages))
|
2014-05-20 10:26:10 -07:00
|
|
|
if last_id is not None:
|
2014-05-18 11:20:44 +03:00
|
|
|
events.extend(m.ToEvent() for m in subject.GetMessagesSince(last_id))
|
|
|
|
|
return events
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-10 15:47:33 +02:00
|
|
|
@classmethod
|
|
|
|
|
@db.transactional()
|
|
|
|
|
def Remove(cls, subject, client):
|
|
|
|
|
subscriptions = (
|
|
|
|
|
cls.all()
|
|
|
|
|
.ancestor(subject)
|
|
|
|
|
.filter('client =', client))
|
|
|
|
|
for subscription in subscriptions:
|
|
|
|
|
subscription.delete()
|
|
|
|
|
|
2014-05-06 22:46:07 -07:00
|
|
|
|
|
|
|
|
class Message(db.Model):
|
|
|
|
|
# parent=Subject
|
|
|
|
|
|
|
|
|
|
created = db.DateTimeProperty(required=True, auto_now_add=True)
|
|
|
|
|
message = db.TextProperty(required=True)
|
2014-05-10 15:47:33 +02:00
|
|
|
sender = db.ReferenceProperty(required=True, reference_class=Profile)
|
2014-05-16 23:07:38 +03:00
|
|
|
sender_message_id = db.StringProperty(required=True)
|
2014-05-18 10:58:39 +03:00
|
|
|
# id is reserved
|
|
|
|
|
id_ = db.IntegerProperty(required=True)
|
2014-05-11 15:43:45 +03:00
|
|
|
# key and key_name are reserved
|
|
|
|
|
key_ = db.StringProperty()
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-09 15:00:48 -07:00
|
|
|
def ToEvent(self):
|
2014-05-11 15:43:45 +03:00
|
|
|
ret = {
|
2014-05-09 15:00:48 -07:00
|
|
|
'event_type': 'message',
|
2014-05-18 10:58:39 +03:00
|
|
|
'id': self.id_,
|
2014-05-10 15:47:33 +02:00
|
|
|
'sender': str(Message.sender.get_value_for_datastore(self)),
|
2014-05-19 20:52:57 +03:00
|
|
|
'subject': self.parent().ToDict(),
|
2014-05-06 22:46:07 -07:00
|
|
|
'created': self.created,
|
|
|
|
|
'message': self.message,
|
|
|
|
|
}
|
2014-05-11 15:43:45 +03:00
|
|
|
if self.key_:
|
|
|
|
|
ret['key'] = self.key_
|
|
|
|
|
return ret
|