GCal: Better Callback management

- when callback of an old channel is received this channel is canceled automatically
This commit is contained in:
Martin Bauer 2014-06-20 08:57:04 +02:00
parent 5fc206112d
commit 5639c5fd82
2 changed files with 49 additions and 7 deletions

View File

@ -332,3 +332,36 @@ def stopAllGCalSubscriptions( service=None ):
for dbChannel in GCalPushChannel.objects.all():
print("Stopping %s expiry at %d " % ( dbChannel.id, dbChannel.expiration ) )
GCalPushChannel.stop( service, dbChannel.toGChannel() )
def checkIfGoogleCallbackIsValid( token, channelID, resourceID, service=None ):
if service is None: service = getServiceObject()
allChannels = GCalPushChannel.objects.all()
if len(allChannels) == 0:
return False # no known subscriptions -> callback has to be from an old channel
if len(allChannels) > 1:
logger.warning( "Multiple GCal subscriptions! This is strange and probably an error. "
"All channels are closed and one new is created. ")
stopAllGCalSubscriptions( service )
checkGCalSubscription()
allChannels = GCalPushChannel.objects.all()
assert( len(allChannels) == 1 )
theChannel = allChannels[0]
if channelID != theChannel.id or resourceID != theChannel.resource_id or token != theChannel.token:
logger.warning( "Got GCal Response from an unexpected Channel"
"Got (%s,%s,%s) "
"expected (%s,%s,%s) "
"Old Channel is stopped."
% ( channelID, resourceID,token, theChannel.id, theChannel.resource_id, theChannel.token ))
channelToStop = GCalPushChannel( id = channelID, resource_id = resourceID, token = token )
GCalPushChannel.stop( service, channelToStop.toGChannel() )
return False
return True

View File

@ -3,6 +3,7 @@ from eventplanner_gcal.google_sync import syncFromGoogleToLocal, syncFromLocalTo
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from pprint import pformat
from eventplanner_gcal.google_sync import checkIfGoogleCallbackIsValid
import logging
@ -16,15 +17,23 @@ def runSync( request ):
@csrf_exempt
def gcalApiCallback( request ):
# TODO check channel info here
requestMetaStr = pformat( request.META )
logger.info( "Received Google Callback with the following headers:\n" + requestMetaStr )
token = ""
if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META: token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN']
channelID = ""
if 'HTTP_X_GOOG_CHANNEL_ID' in request.META: channelID = request.META['HTTP_X_GOOG_CHANNEL_ID']
resourceID = ""
if 'HTTP_X_GOOG_RESOURCE_ID' in request.META: resourceID = request.META['HTTP_X_GOOG_RESOURCE_ID']
valid = checkIfGoogleCallbackIsValid( token, channelID, resourceID)
if not valid:
return HttpResponse('<h1>Old Channel - no update triggered</h1>')
logger.info( "Received Google Callback with the following headers Token: %s ID %s ResID %s " % ( token, channelID, resourceID ) )
result = syncFromGoogleToLocal()
logger.info("Finished processing callback from GCal - New Information present: %1 " %(result, ) )
logger.info("Finished processing callback from GCal - New Information present: %d " %(result, ) )
return HttpResponse('<h1>Callback successful</h1>')