/** * @brief Checks if we have a friend by address */ bool Core::hasFriendWithAddress(const QString &addr) const { // Valid length check if (addr.length() != (TOX_ADDRESS_SIZE * 2)) { return false; } QString pubkey = addr.left(TOX_PUBLIC_KEY_SIZE * 2); return hasFriendWithPublicKey(pubkey); }
/** * @brief Checks that sending friendship request is correct and returns error message accordingly * @param friendId Id of a friend which request is destined to * @param message Friendship request message * @return Returns empty string if sending request is correct, according error message otherwise */ QString Core::getFriendRequestErrorMessage(const ToxId& friendId, const QString& message) const { if (!friendId.isValid()) { return tr("Invalid Tox ID", "Error while sending friendship request"); } if (message.isEmpty()) { return tr("You need to write a message with your request", "Error while sending friendship request"); } if (message.length() > TOX_MAX_FRIEND_REQUEST_LENGTH) { return tr("Your message is too long!", "Error while sending friendship request"); } if (hasFriendWithPublicKey(friendId.getPublicKey())) { return tr("Friend is already added", "Error while sending friendship request"); } return QString{}; }
void Core::requestFriendship(const ToxId& friendAddress, const QString& message) { ToxPk friendPk = friendAddress.getPublicKey(); if (!friendAddress.isValid()) { emit failedToAddFriend(friendPk, tr("Invalid Tox ID")); } else if (message.isEmpty()) { emit failedToAddFriend(friendPk, tr("You need to write a message with your request")); } else if (message.size() > TOX_MAX_FRIEND_REQUEST_LENGTH) { emit failedToAddFriend(friendPk, tr("Your message is too long!")); } else if (hasFriendWithPublicKey(friendPk)) { emit failedToAddFriend(friendPk, tr("Friend is already added")); } else { CString cMessage(message); uint32_t friendId = tox_friend_add(tox, friendAddress.getBytes(), cMessage.data(), cMessage.size(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { qDebug() << "Failed to request friendship"; emit failedToAddFriend(friendPk); } else { qDebug() << "Requested friendship of " << friendId; // Update our friendAddresses Settings::getInstance().updateFriendAddress(friendAddress.toString()); // TODO: start: this really shouldn't be in Core QString inviteStr = tr("/me offers friendship."); if (message.length()) inviteStr = tr("/me offers friendship, \"%1\"").arg(message); Profile* profile = Nexus::getProfile(); if (profile->isHistoryEnabled()) { profile->getHistory()->addNewMessage(friendAddress.toString(), inviteStr, getSelfId().getPublicKey().toString(), QDateTime::currentDateTime(), true, QString()); } // TODO: end emit friendAdded(friendId, friendAddress.getPublicKey()); emit friendshipChanged(friendId); } } profile.saveToxSave(); }