2010-11-04 02:18:18 +03:00
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (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.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is Raindrop.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is
|
|
|
|
# Mozilla Messaging, Inc..
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
#
|
|
|
|
|
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-10-15 02:23:16 +04:00
|
|
|
import copy
|
|
|
|
from urlparse import urlparse
|
2010-10-16 00:55:52 +04:00
|
|
|
from paste.deploy.converters import asbool
|
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-10-05 01:44:15 +04:00
|
|
|
from linkdrop.lib import constants
|
2010-09-02 10:29:21 +04:00
|
|
|
|
|
|
|
from linkdrop.model.meta import Session
|
2010-09-23 04:44:26 +04:00
|
|
|
from linkdrop.model import History, Link
|
2010-09-02 10:29:21 +04:00
|
|
|
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')
|
2010-09-30 10:37:46 +04:00
|
|
|
message = request.POST.get('message', '')
|
2010-09-10 04:36:13 +04:00
|
|
|
username = request.POST.get('username')
|
2010-10-15 02:23:16 +04:00
|
|
|
longurl = request.POST.get('link')
|
2010-10-16 00:55:52 +04:00
|
|
|
shorten = asbool(request.POST.get('shorten', 0))
|
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-30 10:37:46 +04:00
|
|
|
if not domain:
|
2010-09-10 04:36:13 +04:00
|
|
|
error = {
|
2010-10-01 04:36:12 +04:00
|
|
|
'message': "'domain' is not optional",
|
|
|
|
'code': constants.INVALID_PARAMS
|
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-10-01 04:36:12 +04:00
|
|
|
'message': "no user session exists, auth required",
|
|
|
|
'status': 401
|
2010-09-08 08:21:26 +04:00
|
|
|
}
|
|
|
|
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.
|
2010-09-23 04:44:26 +04:00
|
|
|
acct = None
|
|
|
|
for k in keys:
|
|
|
|
a = session.get(k)
|
|
|
|
if a and a.get('domain') == domain and (a.get('username')==username or a.get('userid')==userid):
|
|
|
|
acct = a
|
|
|
|
break
|
|
|
|
if not acct:
|
2010-09-09 00:18:53 +04:00
|
|
|
error = {'provider': domain,
|
2010-10-01 04:36:12 +04:00
|
|
|
'message': "not logged in or no user account for that domain",
|
|
|
|
'status': 401
|
2010-09-03 01:02:32 +04:00
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
2010-09-02 10:29:21 +04:00
|
|
|
|
2010-10-15 02:23:16 +04:00
|
|
|
args = copy.copy(request.POST)
|
2010-10-16 00:55:52 +04:00
|
|
|
if shorten and not shorturl and longurl:
|
2010-10-15 02:23:16 +04:00
|
|
|
u = urlparse(longurl)
|
|
|
|
if not u.scheme:
|
|
|
|
longurl = 'http://' + longurl
|
2010-10-16 02:17:43 +04:00
|
|
|
shorturl = Link.get_or_create(longurl).short_url
|
2010-10-15 02:23:16 +04:00
|
|
|
args['shorturl'] = shorturl
|
|
|
|
|
2010-09-02 10:29:21 +04:00
|
|
|
# send the item.
|
2010-11-06 00:36:35 +03:00
|
|
|
try:
|
|
|
|
result, error = provider.api(acct).sendmessage(message, args)
|
|
|
|
except ValueError, e:
|
|
|
|
# XXX we need to handle this better, but if for some reason the
|
|
|
|
# oauth values are bad we will get a ValueError raised
|
|
|
|
error = {'provider': domain,
|
2010-11-10 01:01:24 +03:00
|
|
|
'message': "not logged in or no user account for that domain",
|
2010-11-06 00:36:35 +03:00
|
|
|
'status': 401
|
|
|
|
}
|
|
|
|
return {'result': result, 'error': error}
|
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
|
2010-10-19 00:58:21 +04:00
|
|
|
if asbool(config.get('history_enabled', True)):
|
2010-10-21 00:03:58 +04:00
|
|
|
# this is faster, but still want to look further into SA perf
|
|
|
|
#data = {
|
|
|
|
# 'json_attributes': json.dumps(dict(request.POST)),
|
|
|
|
# 'account_id': acct.get('id'),
|
|
|
|
# 'published': UTCDateTime.now()
|
|
|
|
#}
|
|
|
|
#Session.execute("INSERT DELAYED INTO history (json_attributes, account_id, published) VALUES (:json_attributes, :account_id, :published)",
|
|
|
|
# data)
|
|
|
|
|
2010-10-19 00:58:21 +04:00
|
|
|
history = History()
|
|
|
|
history.account_id = acct.get('id')
|
|
|
|
history.published = UTCDateTime.now()
|
|
|
|
for key, val in request.POST.items():
|
|
|
|
setattr(history, key, val)
|
|
|
|
Session.add(history)
|
|
|
|
Session.commit()
|
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
|
|
|
# no redirects requests, just return the response.
|
|
|
|
return {'result': result, 'error': error}
|