containsParameter('peer')) { return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required peer parameter'); } try { $address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); } catch(InvalidArgumentException $e) { throw new StandardException('Invalid peer address', StandardError::RPC_INVALID_ARGUMENTS, $e); } if($rpcRequest->containsParameter('relationship')) { $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); if($relationship === null) { throw new StandardException('Invalid relationship type', StandardError::RPC_INVALID_ARGUMENTS); } } else { $relationship = ContactRelationshipType::MUTUAL; } try { $peer = $request->getPeer(); if($peer->getAddress() == $address) { return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Cannot add self as contact'); } // Resolve the peer, this would throw a StandardException if something goes wrong Socialbox::resolvePeer($address); // Check if the contact already exists if(ContactManager::isContact($peer, $address)) { return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists'); } // Create the contact ContactManager::createContact($peer, $address, $relationship); } catch (DatabaseOperationException $e) { throw new StandardException('Failed to add contact', StandardError::INTERNAL_SERVER_ERROR, $e); } // Return success return $rpcRequest->produceResponse(true); } }