content/hifi-content/robin/dev/domains/loadtest/Checkin/DomainEnter/checkinServer.js
2022-02-14 02:04:11 +01:00

106 lines
No EOL
3.3 KiB
JavaScript

// checkinClient.js
//
// Created by Robin Wilson on 2018-08-24
//
// Copyright 2018 High Fidelity, Inc.
//
// Entity server script used in tandem with checkinClient.js to search for username in a
// google sheet column and mark the user present.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
(function () {
var entityID,
url = "",
event = null,
on = false;
// mirrored in the google app script code
var SUCCESS = "Success";
var NOT_FOUND_USERNAME = "Not found username";
var FAILURE = "There was a problem please let Hifi know";
// Collections
var currentProperties = {},
userData = {},
userdataProperties = {};
function CheckinServer() {
}
CheckinServer.prototype = {
remotelyCallable: [
"greeterSearch",
"hasUsername"
],
preload: function (id) {
entityID = id;
currentProperties = Entities.getEntityProperties(entityID);
userData = currentProperties.userData;
try {
// set properties from the userData
userdataProperties = JSON.parse(userData);
url = userdataProperties.checkinData.url;
event = userdataProperties.checkinData.event;
on = userdataProperties.checkinData.on;
} catch (e) {
//
}
},
hasUsername: function (id, params) {
if (on) {
var paramString = this.encodeURLParams({
username: params[1],
displayName: params[2],
date: new Date(),
event: event
});
var request = new XMLHttpRequest();
request.open('GET', url + "?" + paramString);
request.timeout = 10000;
request.ontimeout = function () {
Entities.callEntityClientMethod(params[0], entityID, "invalid");
};
request.onreadystatechange = function () { // called after load when readyState = 4
print("STATE CHANGED");
if (request.readyState === 4) {
if (request.response === SUCCESS) {
Entities.callEntityClientMethod(params[0], entityID, "success");
} else if (request.response === NOT_FOUND_USERNAME || request.response === FAILURE) {
Entities.callEntityClientMethod(params[0], entityID, "invalid");
} else {
Entities.callEntityClientMethod(params[0], entityID, "invalid");
}
}
};
request.send();
}
},
encodeURLParams: function (params) {
var paramPairs = [];
for (var key in params) {
paramPairs.push(key + "=" + params[key]);
}
return paramPairs.join("&");
},
unload: function () {
}
};
return new CheckinServer();
});