def postRoutedFriendRequest(self, data, decrypted): node = Node(public_key=data['public_key']) node.set('data/friends', node.getFriends(), True) nodeComm = ManagerCommunicator(node) yadaserver = YadaServer() serverFriend = Node( node.getFriend(yadaserver.matchFriend(node)['public_key'])) friendTest = Node.db.friends.find({ 'public_key': data['public_key'], 'friend.routed_public_key': decrypted['routed_public_key'] }) if friendTest.count() == 0: nodeComm.routeRequestThroughNode( serverFriend, decrypted['routed_public_key'], decrypted.get('name', decrypted['routed_public_key']), decrypted.get('avatar', '')) Node.db.friends.update( { 'public_key': data['public_key'], 'friend.routed_public_key': decrypted['routed_public_key'] }, {"$set": { "friend.subscribed": "*" }}) friend = Node.db.friends.find({ 'public_key': data['public_key'], 'friend.routed_public_key': decrypted['routed_public_key'] }) return {"status": "request sent", "friend": friend[0]['friend']} return {"status": "already friends"}
def onMessage(self, inbound, isBinary): """ As soon as any data is received, write it back. """ print 'got data!' f = open('/home/phablet/yadaserver.log', 'a') try: inboundObj = json.loads(inbound) if "METHOD" in inboundObj and inboundObj[ 'METHOD'] == "CREATE_IDENTITY": f.write('got identity message') f.close() n = Node({}, {"name": ""}) n._data['idlabel'] = inboundObj['DATA'] n.save() return if "METHOD" in inboundObj and inboundObj[ 'METHOD'] == "UPDATE_IDENTITY": f.write('got update identity message') f.close() print json.dumps(inboundObj, indent=2) n = Node(inboundObj['DATA']) n.save() return except: pass try: f.write('|---|%s|---|\n' % inbound) #f.write(json.dumps(inbound)) #response = nodeComm.handlePacket(inbound) #returnData = json.dumps(response) """ This is generally where a qr scanner would forward the object to postFriend api endpoint. instead, we're going to """ print "about to start notify.py" r = requests.get(inbound) friend = r.text call(['python', '/home/phablet/notify.py', friend]) self.sendMessage('OK') except: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) f.write(''.join('!! ' + line for line in lines)) self.transport.write('Input not valid.') f.close()
def createIdentity(self, data, decrypted): node = Node({}, decrypted) node.save() return { 'public_key': node.get('public_key'), 'method': 'GET', 'data': encrypt(node.get('private_key'), node.get('private_key'), json.dumps(node.get())) }