Files
cosmopolite/api.py

198 lines
5.0 KiB
Python
Raw Normal View History

2014-03-25 13:43:11 -07:00
# 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 logging
2014-03-25 13:43:11 -07:00
import webapp2
from google.appengine.api import channel
from google.appengine.ext import db
from cosmopolite.lib import auth
from cosmopolite.lib import models
from cosmopolite.lib import security
from cosmopolite.lib import session
from cosmopolite.lib import utils
import config
def CreateChannel(google_user, client, instance_id, args):
2014-05-23 15:00:28 -07:00
models.Instance.FindOrCreate(instance_id)
token = channel.create_channel(
client_id=instance_id,
duration_minutes=config.CHANNEL_DURATION_SECONDS / 60)
events = []
if google_user:
events.append({
'event_type': 'login',
'profile': str(client.parent_key()),
'google_user': google_user.email(),
})
else:
events.append({
'event_type': 'logout',
'profile': str(client.parent_key()),
})
return {
'token': token,
'events': events,
}
def Pin(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id)
subject = args['subject']
message = args['message']
sender_message_id = args['sender_message_id']
try:
models.Subject.FindOrCreate(subject).Pin(
message, client.parent_key(), sender_message_id, instance)
except models.DuplicateMessage:
logging.warning('Duplicate pin: %s', sender_message_id)
return {
'result': 'duplicate_message',
}
except models.AccessDenied:
logging.warning('Pin access denied')
return {
'result': 'access_denied',
}
return {
'result': 'ok',
}
def SendMessage(google_user, client, instance_id, args):
subject = args['subject']
message = args['message']
sender_message_id = args['sender_message_id']
try:
models.Subject.FindOrCreate(subject).SendMessage(
message, client.parent_key(), sender_message_id)
except models.DuplicateMessage:
logging.warning('Duplicate message: %s', sender_message_id)
return {
'result': 'duplicate_message',
}
except models.AccessDenied:
logging.warning('SendMessage access denied')
return {
'result': 'access_denied',
}
return {
'result': 'ok',
}
def Subscribe(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id)
if not instance or not instance.active:
# Probably a race with the channel opening
return {
'result': 'retry',
}
subject = models.Subject.FindOrCreate(args['subject'])
messages = args.get('messages', 0)
last_id = args.get('last_id', None)
try:
return {
'result': 'ok',
'events': models.Subscription.FindOrCreate(
2014-05-23 15:00:28 -07:00
subject, client, instance, messages, last_id),
}
except models.AccessDenied:
logging.warning('Subscribe access denied')
return {
'result': 'access_denied',
}
def Unpin(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id)
subject = args['subject']
sender_message_id = args['sender_message_id']
try:
models.Subject.FindOrCreate(subject).Unpin(
client.parent_key(), sender_message_id, instance.key())
except models.AccessDenied:
logging.warning('Pin access denied')
return {
'result': 'access_denied',
}
return {
'result': 'ok',
}
def Unsubscribe(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id)
subject = models.Subject.FindOrCreate(args['subject'])
models.Subscription.Remove(subject, instance)
return {}
class APIWrapper(webapp2.RequestHandler):
_COMMANDS = {
'createChannel': CreateChannel,
'pin': Pin,
'sendMessage': SendMessage,
'subscribe': Subscribe,
'unpin': Unpin,
'unsubscribe': Unsubscribe,
}
2014-03-25 13:43:11 -07:00
@utils.chaos_monkey
@utils.expects_json
2014-03-25 13:43:11 -07:00
@utils.returns_json
@utils.local_namespace
@security.google_user_xsrf_protection
@security.weak_security_checks
@session.session_required
def post(self):
ret = {
'status': 'ok',
'responses': [],
'events': [],
}
for command in self.request_json['commands']:
callback = self._COMMANDS[command['command']]
result = callback(
self.verified_google_user,
self.client,
self.request_json['instance_id'],
command.get('arguments', {}))
# Magic: if result contains "events", haul them up a level so the
# client can see them as a single stream.
ret['events'].extend(result.pop('events', []))
ret['responses'].append(result)
return ret
2014-03-25 13:43:11 -07:00
app = webapp2.WSGIApplication([
(config.URL_PREFIX + '/api', APIWrapper),
2014-03-25 13:43:11 -07:00
])