containsParameter('peer')) { throw new MissingRpcArgumentException('peer'); } try { $address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); } catch(InvalidArgumentException $e) { throw new InvalidRpcArgumentException('peer', $e->getMessage()); } if($rpcRequest->containsParameter('relationship')) { $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); if($relationship === null) { throw new InvalidRpcArgumentException('peer', 'Invalid relationship type'); } } 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->produceResponse(false); } // Create the contact ContactManager::createContact($peer, $address, $relationship); } catch (DatabaseOperationException $e) { throw new StandardRpcException('Failed to add contact', StandardError::INTERNAL_SERVER_ERROR, $e); } // Return success return $rpcRequest->produceResponse(true); } }