Slight optimization; comments

This commit is contained in:
Zach Fox 2019-12-06 10:32:45 -08:00
parent 04fe2988f5
commit 2b7d66c710
3 changed files with 30 additions and 17 deletions

View file

@ -3616,28 +3616,18 @@ void DomainServer::handleOctreeFileReplacementRequest(QSharedPointer<ReceivedMes
} }
void DomainServer::processAvatarZonePresencePacket(QSharedPointer<ReceivedMessage> message) { void DomainServer::processAvatarZonePresencePacket(QSharedPointer<ReceivedMessage> message) {
QUuid avatar = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); QUuid avatarID = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID));
QUuid zone = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); QUuid zoneID = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID));
if (avatar.isNull()) { if (avatarID.isNull()) {
qCWarning(domain_server) << "Ignoring null avatar presence"; qCWarning(domain_server) << "Ignoring null avatar presence";
return; return;
} }
auto limitedNodeList = DependencyManager::get<LimitedNodeList>();
auto matchingNode = limitedNodeList->nodeWithUUID(avatar);
if (!matchingNode) {
qCWarning(domain_server) << "Ignoring avatar presence for unknown avatar" << avatar;
return;
}
QString verifiedUsername = matchingNode->getPermissions().getVerifiedUserName();
if (verifiedUsername.isEmpty()) { // Silently bail for users who are not logged in.
return;
}
static const int SCREENSHARE_EXPIRATION_SECONDS = 24 * 60 * 60; static const int SCREENSHARE_EXPIRATION_SECONDS = 24 * 60 * 60;
screensharePresence(zone.isNull() ? "" : zone.toString(), verifiedUsername, avatar, SCREENSHARE_EXPIRATION_SECONDS); screensharePresence(zoneID.isNull() ? "" : zoneID.toString(), avatarID, SCREENSHARE_EXPIRATION_SECONDS);
} }
void DomainServer::screensharePresence(QString roomname, QString username, QUuid avatarID, int expirationSeconds) { void DomainServer::screensharePresence(QString roomname, QUuid avatarID, int expirationSeconds) {
if (!DependencyManager::get<AccountManager>()->hasValidAccessToken()) { if (!DependencyManager::get<AccountManager>()->hasValidAccessToken()) {
static std::once_flag presenceAuthorityWarning; static std::once_flag presenceAuthorityWarning;
std::call_once(presenceAuthorityWarning, [] { std::call_once(presenceAuthorityWarning, [] {
@ -3645,10 +3635,25 @@ void DomainServer::screensharePresence(QString roomname, QString username, QUuid
}); });
return; return;
} }
auto limitedNodeList = DependencyManager::get<LimitedNodeList>();
auto matchingNode = limitedNodeList->nodeWithUUID(avatarID);
if (!matchingNode) {
qCWarning(domain_server) << "Ignoring avatar presence for unknown avatar ID" << avatarID;
return;
}
QString verifiedUsername = matchingNode->getPermissions().getVerifiedUserName();
if (verifiedUsername.isEmpty()) { // Silently bail for users who are not logged in.
return;
}
JSONCallbackParameters callbackParams; JSONCallbackParameters callbackParams;
callbackParams.callbackReceiver = this; callbackParams.callbackReceiver = this;
callbackParams.jsonCallbackMethod = "handleSuccessfulScreensharePresence"; callbackParams.jsonCallbackMethod = "handleSuccessfulScreensharePresence";
callbackParams.errorCallbackMethod = "handleFailedScreensharePresence"; callbackParams.errorCallbackMethod = "handleFailedScreensharePresence";
// Construct `callbackData`, which is data that will be available to the callback functions.
// In this case, the "success" callback needs access to the "roomname" (the zone ID) and the
// relevant avatar's UUID.
QJsonObject callbackData; QJsonObject callbackData;
callbackData.insert("roomname", roomname); callbackData.insert("roomname", roomname);
callbackData.insert("avatarID", avatarID.toString()); callbackData.insert("avatarID", avatarID.toString());
@ -3656,7 +3661,7 @@ void DomainServer::screensharePresence(QString roomname, QString username, QUuid
const QString PATH = "api/v1/domains/%1/screenshare"; const QString PATH = "api/v1/domains/%1/screenshare";
QString domain_id = uuidStringWithoutCurlyBraces(getID()); QString domain_id = uuidStringWithoutCurlyBraces(getID());
QJsonObject json, screenshare; QJsonObject json, screenshare;
screenshare["username"] = username; screenshare["username"] = verifiedUsername;
screenshare["roomname"] = roomname; screenshare["roomname"] = roomname;
if (expirationSeconds > 0) { if (expirationSeconds > 0) {
screenshare["expiration"] = expirationSeconds; screenshare["expiration"] = expirationSeconds;
@ -3677,6 +3682,7 @@ void DomainServer::handleSuccessfulScreensharePresence(QNetworkReply* requestRep
return; return;
} }
// Tell the client that we just authorized to screenshare which zone ID in which they are authorized to screenshare.
auto nodeList = DependencyManager::get<LimitedNodeList>(); auto nodeList = DependencyManager::get<LimitedNodeList>();
auto packet = NLPacket::create(PacketType::AvatarZonePresence, NUM_BYTES_RFC4122_UUID, true); auto packet = NLPacket::create(PacketType::AvatarZonePresence, NUM_BYTES_RFC4122_UUID, true);
packet->write(QUuid(callbackData["roomname"].toString()).toRfc4122()); packet->write(QUuid(callbackData["roomname"].toString()).toRfc4122());

View file

@ -78,7 +78,7 @@ public:
bool isAssetServerEnabled(); bool isAssetServerEnabled();
void screensharePresence(QString roomname, QString username, QUuid avatarID, int expiration_seconds = 0); void screensharePresence(QString roomname, QUuid avatarID, int expiration_seconds = 0);
public slots: public slots:
/// Called by NodeList to inform us a node has been added /// Called by NodeList to inform us a node has been added

View file

@ -39,6 +39,7 @@ ScreenshareScriptingInterface::ScreenshareScriptingInterface() {
_requestScreenshareInfoRetryTimer->setInterval(SCREENSHARE_INFO_REQUEST_RETRY_TIMEOUT_MS); _requestScreenshareInfoRetryTimer->setInterval(SCREENSHARE_INFO_REQUEST_RETRY_TIMEOUT_MS);
connect(_requestScreenshareInfoRetryTimer, &QTimer::timeout, this, &ScreenshareScriptingInterface::requestScreenshareInfo); connect(_requestScreenshareInfoRetryTimer, &QTimer::timeout, this, &ScreenshareScriptingInterface::requestScreenshareInfo);
// This packet listener handles the packet containing information about the latest zone ID in which we are allowed to share.
auto nodeList = DependencyManager::get<NodeList>(); auto nodeList = DependencyManager::get<NodeList>();
PacketReceiver& packetReceiver = nodeList->getPacketReceiver(); PacketReceiver& packetReceiver = nodeList->getPacketReceiver();
packetReceiver.registerListener(PacketType::AvatarZonePresence, this, "processAvatarZonePresencePacketOnClient"); packetReceiver.registerListener(PacketType::AvatarZonePresence, this, "processAvatarZonePresencePacketOnClient");
@ -56,8 +57,11 @@ void ScreenshareScriptingInterface::processAvatarZonePresencePacketOnClient(QSha
return; return;
} }
// Set the last known authorized screenshare zone ID to the zone that the Domain Server just told us about.
_lastAuthorizedZoneID = zone; _lastAuthorizedZoneID = zone;
// If we had previously started the screenshare process but knew that we weren't going to be authorized to screenshare,
// let's continue the screenshare process here.
if (_waitingForAuthorization) { if (_waitingForAuthorization) {
requestScreenshareInfo(); requestScreenshareInfo();
} }
@ -65,6 +69,9 @@ void ScreenshareScriptingInterface::processAvatarZonePresencePacketOnClient(QSha
static const int MAX_NUM_SCREENSHARE_INFO_REQUEST_RETRIES = 5; static const int MAX_NUM_SCREENSHARE_INFO_REQUEST_RETRIES = 5;
void ScreenshareScriptingInterface::requestScreenshareInfo() { void ScreenshareScriptingInterface::requestScreenshareInfo() {
// If the screenshare zone that we're currently in (i.e. `startScreenshare()` was called) is different from
// the zone in which we are authorized to screenshare...
// ...return early here and wait for the DS to send us a packet containing this zone's ID.
if (_screenshareZoneID != _lastAuthorizedZoneID) { if (_screenshareZoneID != _lastAuthorizedZoneID) {
qDebug() << "Client not yet authorized to screenshare. Waiting for authorization message from domain server..."; qDebug() << "Client not yet authorized to screenshare. Waiting for authorization message from domain server...";
_waitingForAuthorization = true; _waitingForAuthorization = true;