2010-09-02 10:29:21 +04:00
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
import urllib
|
2010-09-15 01:18:46 +04:00
|
|
|
import sys
|
2010-09-03 01:02:32 +04:00
|
|
|
import httplib2
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
from pylons import config, request, response, session
|
|
|
|
from pylons.controllers.util import abort, redirect
|
|
|
|
from pylons.decorators.util import get_pylons
|
|
|
|
|
|
|
|
from linkdrop.lib.base import BaseController
|
|
|
|
from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg
|
2010-09-03 02:24:50 +04:00
|
|
|
from linkdrop.lib.oauth import get_provider
|
2010-09-15 01:18:46 +04:00
|
|
|
from linkdrop.lib.links import sign_link
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
from linkdrop.model.meta import Session
|
|
|
|
from linkdrop.model import Account, History
|
|
|
|
from linkdrop.model.types import UTCDateTime
|
2010-09-09 00:18:53 +04:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class SendController(BaseController):
|
|
|
|
"""
|
|
|
|
Send
|
|
|
|
====
|
|
|
|
|
|
|
|
The 'send' namespace is used to send updates to our supported services.
|
|
|
|
|
|
|
|
"""
|
|
|
|
__api_controller__ = True # for docs
|
|
|
|
|
|
|
|
@api_response
|
|
|
|
@json_exception_response
|
|
|
|
def send(self):
|
2010-09-03 01:02:32 +04:00
|
|
|
result = {}
|
|
|
|
error = None
|
2010-09-10 04:36:13 +04:00
|
|
|
# If we don't have a key in our session we bail early with a
|
2010-09-02 10:29:21 +04:00
|
|
|
# 401
|
2010-09-10 04:36:13 +04:00
|
|
|
domain = request.POST.get('domain')
|
|
|
|
message = request.POST.get('message')
|
|
|
|
username = request.POST.get('username')
|
2010-09-15 01:18:46 +04:00
|
|
|
shorturl = request.POST.get('shorturl')
|
2010-09-10 04:36:13 +04:00
|
|
|
userid = request.POST.get('userid')
|
2010-09-15 01:18:46 +04:00
|
|
|
to = request.POST.get('to')
|
2010-09-10 04:36:13 +04:00
|
|
|
if not domain or not message:
|
|
|
|
error = {
|
|
|
|
'reason': "'domain' and 'message' request params are not optional",
|
|
|
|
'code': 409
|
2010-09-03 01:02:32 +04:00
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
2010-09-10 04:36:13 +04:00
|
|
|
keys = session.get('account_keys', '').split(',')
|
|
|
|
if not keys:
|
2010-09-08 08:21:26 +04:00
|
|
|
error = {'provider': domain,
|
2010-09-10 04:36:13 +04:00
|
|
|
'reason': "no user session exists, auth required",
|
2010-09-08 08:21:26 +04:00
|
|
|
'code': 401
|
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
2010-09-02 10:29:21 +04:00
|
|
|
|
2010-09-03 02:24:50 +04:00
|
|
|
provider = get_provider(domain)
|
2010-09-02 10:29:21 +04:00
|
|
|
# even if we have a session key, we must have an account for that
|
|
|
|
# user for the specified domain.
|
|
|
|
try:
|
2010-09-10 04:36:13 +04:00
|
|
|
q = Session.query(Account).filter(Account.key.in_(keys)).filter(Account.domain==domain)
|
2010-09-09 00:18:53 +04:00
|
|
|
if username:
|
|
|
|
q = q.filter(Account.username==username)
|
|
|
|
if userid:
|
|
|
|
q = q.filter(Account.userid==userid)
|
|
|
|
acct = q.one()
|
|
|
|
except MultipleResultsFound:
|
|
|
|
error = {'provider': domain,
|
2010-09-10 04:36:13 +04:00
|
|
|
'reason': "Multiple accounts for %s were found, username or userid required" % domain,
|
2010-09-09 00:18:53 +04:00
|
|
|
'code': 409
|
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
2010-09-02 10:29:21 +04:00
|
|
|
except NoResultFound:
|
2010-09-03 01:02:32 +04:00
|
|
|
error = {'provider': domain,
|
2010-09-10 04:36:13 +04:00
|
|
|
'reason': "no user account for that domain",
|
2010-09-03 01:02:32 +04:00
|
|
|
'code': 401
|
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
# send the item.
|
2010-09-08 04:48:59 +04:00
|
|
|
result, error = provider.api(acct).sendmessage(message, request.POST)
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
if error:
|
|
|
|
assert not result
|
|
|
|
log.info("send failure: %r", error)
|
|
|
|
else:
|
|
|
|
# create a new record in the history table.
|
|
|
|
assert result
|
|
|
|
history = History()
|
|
|
|
history.account = acct
|
|
|
|
history.published = UTCDateTime.now()
|
2010-09-08 06:30:55 +04:00
|
|
|
for key, val in request.POST.items():
|
|
|
|
setattr(history, key, val)
|
2010-09-02 10:29:21 +04:00
|
|
|
Session.add(history)
|
2010-09-15 01:18:46 +04:00
|
|
|
link = sign_link(shorturl, acct.username)
|
2010-09-02 10:29:21 +04:00
|
|
|
Session.commit()
|
|
|
|
result['linkdrop'] = history.id
|
2010-09-15 01:18:46 +04:00
|
|
|
result['shorturl'] = shorturl
|
|
|
|
result['from'] = userid
|
|
|
|
result['to'] = to
|
2010-09-02 10:29:21 +04:00
|
|
|
log.info("send success - linkdrop id is %s", history.id)
|
|
|
|
# no redirects requests, just return the response.
|
|
|
|
return {'result': result, 'error': error}
|