2023-07-05 01:14:44 +02:00
|
|
|
const uuidv4 = require("uuid").v4
|
|
|
|
|
2022-05-27 02:09:46 +02:00
|
|
|
class DeviceInfo {
|
|
|
|
constructor(deviceInfo = null) {
|
2023-07-05 01:14:44 +02:00
|
|
|
this.id = null
|
|
|
|
this.userId = null
|
2023-04-09 01:01:24 +02:00
|
|
|
this.deviceId = null
|
2022-05-27 02:09:46 +02:00
|
|
|
this.ipAddress = null
|
|
|
|
|
|
|
|
// From User Agent (see: https://www.npmjs.com/package/ua-parser-js)
|
|
|
|
this.browserName = null
|
|
|
|
this.browserVersion = null
|
|
|
|
this.osName = null
|
|
|
|
this.osVersion = null
|
|
|
|
this.deviceType = null
|
|
|
|
|
|
|
|
// From client
|
|
|
|
this.clientVersion = null
|
|
|
|
this.manufacturer = null
|
|
|
|
this.model = null
|
|
|
|
this.sdkVersion = null // Android Only
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
this.clientName = null
|
|
|
|
this.deviceName = null
|
2022-05-27 02:09:46 +02:00
|
|
|
|
|
|
|
if (deviceInfo) {
|
|
|
|
this.construct(deviceInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(deviceInfo) {
|
|
|
|
for (const key in deviceInfo) {
|
|
|
|
if (deviceInfo[key] !== undefined && this[key] !== undefined) {
|
|
|
|
this[key] = deviceInfo[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
const obj = {
|
2023-07-05 01:14:44 +02:00
|
|
|
id: this.id,
|
|
|
|
userId: this.userId,
|
2023-04-09 01:01:24 +02:00
|
|
|
deviceId: this.deviceId,
|
2022-05-27 02:09:46 +02:00
|
|
|
ipAddress: this.ipAddress,
|
|
|
|
browserName: this.browserName,
|
|
|
|
browserVersion: this.browserVersion,
|
|
|
|
osName: this.osName,
|
|
|
|
osVersion: this.osVersion,
|
|
|
|
deviceType: this.deviceType,
|
|
|
|
clientVersion: this.clientVersion,
|
|
|
|
manufacturer: this.manufacturer,
|
|
|
|
model: this.model,
|
|
|
|
sdkVersion: this.sdkVersion,
|
2023-07-05 01:14:44 +02:00
|
|
|
clientName: this.clientName,
|
|
|
|
deviceName: this.deviceName
|
2022-05-27 02:09:46 +02:00
|
|
|
}
|
|
|
|
for (const key in obj) {
|
|
|
|
if (obj[key] === null || obj[key] === undefined) {
|
|
|
|
delete obj[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2023-02-04 20:23:13 +01:00
|
|
|
get deviceDescription() {
|
|
|
|
if (this.model) { // Set from mobile apps
|
|
|
|
if (this.sdkVersion) return `${this.model} SDK ${this.sdkVersion} / v${this.clientVersion}`
|
|
|
|
return `${this.model} / v${this.clientVersion}`
|
|
|
|
}
|
|
|
|
return `${this.osName} ${this.osVersion} / ${this.browserName}`
|
|
|
|
}
|
|
|
|
|
2023-04-09 01:01:24 +02:00
|
|
|
// When client doesn't send a device id
|
|
|
|
getTempDeviceId() {
|
|
|
|
const keys = [
|
2023-07-05 01:14:44 +02:00
|
|
|
this.userId,
|
2023-04-09 01:01:24 +02:00
|
|
|
this.browserName,
|
|
|
|
this.browserVersion,
|
|
|
|
this.osName,
|
|
|
|
this.osVersion,
|
|
|
|
this.clientVersion,
|
|
|
|
this.manufacturer,
|
|
|
|
this.model,
|
|
|
|
this.sdkVersion,
|
|
|
|
this.ipAddress
|
|
|
|
].map(k => k || '')
|
|
|
|
return 'temp-' + Buffer.from(keys.join('-'), 'utf-8').toString('base64')
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
setData(ip, ua, clientDeviceInfo, serverVersion, userId) {
|
|
|
|
this.id = uuidv4()
|
|
|
|
this.userId = userId
|
2023-07-09 18:39:15 +02:00
|
|
|
this.deviceId = clientDeviceInfo?.deviceId || this.id
|
2022-05-27 02:09:46 +02:00
|
|
|
this.ipAddress = ip || null
|
|
|
|
|
2023-04-09 01:01:24 +02:00
|
|
|
this.browserName = ua?.browser.name || null
|
|
|
|
this.browserVersion = ua?.browser.version || null
|
|
|
|
this.osName = ua?.os.name || null
|
|
|
|
this.osVersion = ua?.os.version || null
|
|
|
|
this.deviceType = ua?.device.type || null
|
2022-05-27 02:09:46 +02:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
this.clientVersion = clientDeviceInfo?.clientVersion || serverVersion
|
2023-04-09 01:01:24 +02:00
|
|
|
this.manufacturer = clientDeviceInfo?.manufacturer || null
|
|
|
|
this.model = clientDeviceInfo?.model || null
|
|
|
|
this.sdkVersion = clientDeviceInfo?.sdkVersion || null
|
2022-05-27 02:09:46 +02:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
this.clientName = clientDeviceInfo?.clientName || null
|
|
|
|
if (this.sdkVersion) {
|
|
|
|
if (!this.clientName) this.clientName = 'Abs Android'
|
|
|
|
this.deviceName = `${this.manufacturer || 'Unknown'} ${this.model || ''}`
|
|
|
|
} else if (this.model) {
|
|
|
|
if (!this.clientName) this.clientName = 'Abs iOS'
|
|
|
|
this.deviceName = `${this.manufacturer || 'Unknown'} ${this.model || ''}`
|
|
|
|
} else if (this.osName && this.browserName) {
|
|
|
|
if (!this.clientName) this.clientName = 'Abs Web'
|
|
|
|
this.deviceName = `${this.osName} ${this.osVersion || 'N/A'} ${this.browserName}`
|
|
|
|
} else if (!this.clientName) {
|
|
|
|
this.clientName = 'Unknown'
|
|
|
|
}
|
2023-04-09 01:01:24 +02:00
|
|
|
|
|
|
|
if (!this.deviceId) {
|
|
|
|
this.deviceId = this.getTempDeviceId()
|
|
|
|
}
|
2022-05-27 02:09:46 +02:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
|
|
|
update(deviceInfo) {
|
|
|
|
const deviceInfoJson = deviceInfo.toJSON ? deviceInfo.toJSON() : deviceInfo
|
|
|
|
const existingDeviceInfoJson = this.toJSON()
|
|
|
|
|
|
|
|
let hasUpdates = false
|
|
|
|
for (const key in deviceInfoJson) {
|
|
|
|
if (['id', 'deviceId'].includes(key)) continue
|
|
|
|
|
|
|
|
if (deviceInfoJson[key] !== existingDeviceInfoJson[key]) {
|
|
|
|
this[key] = deviceInfoJson[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in existingDeviceInfoJson) {
|
|
|
|
if (['id', 'deviceId'].includes(key)) continue
|
|
|
|
|
|
|
|
if (existingDeviceInfoJson[key] && !deviceInfoJson[key]) {
|
|
|
|
this[key] = null
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2022-05-27 02:09:46 +02:00
|
|
|
}
|
|
|
|
module.exports = DeviceInfo
|