Exemplo n.º 1
0
	def on_authenticate_facebook(self, response):
		try:
			access_token, expires_in = \
				response['accessToken'], response['expiresIn']
			signed_request = parse_signed_request(
				response['signedRequest'], app.config['FACEBOOK_SECRET'],
				expires_in
			)
			if not signed_request:
				return None, None
			graph = GraphAPI(access_token)
			profile = graph.get_object('me')
			user = User.get_by_facebook(profile['id'])
			if not user or user.status == User.NOT_REGISTERED_YET:
				if not user:
					user = User()
					user.facebook_account = FacebookAccount()
				user.email = profile['email']
				user.facebook_account.name = user.nickname = profile['name']
				user.facebook_account.uid = profile['id']
				user.status = User.NORMAL
				db.session.add(user)
				db.session.commit()
			self.online_users[self] = self.session['user_id'] = user.id
			self.facebook_access_token = access_token, signed_request['expires_at']
			return None, user.to_json()
		except GraphAPIError, e:
			return {'_type': 'GraphAPIError', 'msg': str(GraphAPIError)},
Exemplo n.º 2
0
	def on_create_room(self, raw_attendees, user):
		chat_room = ChatRoom()
		chat_room.creator = user
		for raw_attendee in raw_attendees:
			attendee_id = raw_attendee.get('id', None)
			if attendee_id:
				attendee = User.get(attendee_id)
			elif raw_attendee['type'] == 'facebook':
				# TODO race condition may occurs need cacth exceptions
				attendee = User.get_by_facebook(raw_attendee['uid'])
				if not attendee:
					attendee = User()
					facebook_account = attendee.facebook_account =\
						FacebookAccount()
					facebook_account.name = attendee.nickname =\
						raw_attendee['nickname']
					attendee.email = raw_attendee.get('email', None)
					facebook_account.uid = raw_attendee['uid']
					db.session.add(attendee)
					db.session.commit()
			else:
				# TODO email account?
				pass
			chat_room.users.append(attendee)
		db.session.add(chat_room)
		db.session.commit()
		json = chat_room.to_json()
		json['message'] = chat_room.messages.first()
		return None, chat_room.to_json()