void Core::requestFriendship(const ToxId& friendId, const QString& message) { ToxPk friendPk = friendId.getPublicKey(); QString errorMessage = getFriendRequestErrorMessage(friendId, message); if (!errorMessage.isNull()) { emit failedToAddFriend(friendPk, errorMessage); profile.saveToxSave(); } ToxString cMessage(message); uint32_t friendNumber = tox_friend_add(tox, friendId.getBytes(), cMessage.data(), cMessage.size(), nullptr); if (friendNumber == std::numeric_limits<uint32_t>::max()) { qDebug() << "Failed to request friendship"; emit failedToAddFriend(friendPk); } else { qDebug() << "Requested friendship of " << friendNumber; Settings::getInstance().updateFriendAddress(friendId.toString()); emit friendAdded(friendNumber, friendPk); emit requestSent(friendPk, message); } profile.saveToxSave(); }
/** * @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{}; }
/** * @brief Compares the inequality of the Public Key. * @param other Tox ID to compare. * @return True if both Tox IDs have different public keys, false otherwise. */ bool ToxId::operator!=(const ToxId& other) const { return getPublicKey() != other.getPublicKey(); }
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(); }