Add IP Address to listening session to provide a more accurate history of the network location for each playback.

This commit is contained in:
Peter BALIVET 2025-06-23 10:38:43 +02:00
parent 108b2a60f5
commit 7fb2acc500
5 changed files with 81 additions and 2 deletions

View File

@ -89,7 +89,7 @@
<p v-if="hasDeviceInfo" class="font-semibold uppercase text-xs text-gray-400 tracking-wide mt-6 mb-2">{{ $strings.LabelDevice }}</p>
<p v-if="clientDisplayName" class="mb-1">{{ clientDisplayName }}</p>
<p v-if="deviceInfo.ipAddress" class="mb-1">{{ deviceInfo.ipAddress }}</p>
<p v-if="_session.ipAddress" class="mb-1">{{ _session.ipAddress }}</p>
<p v-if="osDisplayName" class="mb-1">{{ osDisplayName }}</p>
<p v-if="deviceInfo.browserName" class="mb-1">{{ deviceInfo.browserName }}</p>
<p v-if="deviceDisplayName" class="mb-1">{{ deviceDisplayName }}</p>
@ -136,7 +136,7 @@ export default {
return this._session.deviceInfo || {}
},
hasDeviceInfo() {
return Object.keys(this.deviceInfo).length
return Object.keys(this.deviceInfo).length || this._session.ipAddress
},
osDisplayName() {
if (!this.deviceInfo.osName) return null

View File

@ -62,6 +62,7 @@ class PlaybackSessionManager {
if (existingDevice.update(deviceInfo)) {
await Database.deviceModel.updateFromOld(existingDevice)
}
existingDevice.ipAddress = ip
return existingDevice
}
}
@ -181,6 +182,7 @@ class PlaybackSessionManager {
// New session from local
session = new PlaybackSession(sessionJson)
session.deviceInfo = deviceInfo
session.ipAddress = deviceInfo.ipAddress
if (session.mediaMetadata == null) {
session.mediaMetadata = {}

View File

@ -0,0 +1,67 @@
/**
* @typedef MigrationContext
* @property {import('sequelize').QueryInterface} queryInterface - a Sequelize QueryInterface object.
* @property {import('../Logger')} logger - a Logger object.
*
* @typedef MigrationOptions
* @property {MigrationContext} context - an object containing the migration context.
*/
const migrationVersion = '2.20.1'
const migrationName = `${migrationVersion}-add-ipaddress-to-playbacksession`
const loggerPrefix = `[${migrationVersion} migration]`
/**
* This migration script adds the ipAddress column to the playbackSessions table.
*
* @param {MigrationOptions} options - an object containing the migration context.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function up({ context: { queryInterface, logger } }) {
logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`)
if (await queryInterface.tableExists('playbackSessions')) {
const tableDescription = await queryInterface.describeTable('playbackSessions')
if (!tableDescription.ipAddress) {
logger.info(`${loggerPrefix} Adding ipAddress column to playbackSessions table`)
await queryInterface.addColumn('playbackSessions', 'ipAddress', {
type: queryInterface.sequelize.Sequelize.DataTypes.STRING,
allowNull: true
})
logger.info(`${loggerPrefix} Added ipAddress column to playbackSessions table`)
} else {
logger.info(`${loggerPrefix} ipAddress column already exists in playbackSessions table`)
}
} else {
logger.info(`${loggerPrefix} playbackSessions table does not exist`)
}
logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`)
}
/**
* This migration script removes the ipAddress column from the playbackSessions table.
*
* @param {MigrationOptions} options - an object containing the migration context.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function down({ context: { queryInterface, logger } }) {
logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`)
if (await queryInterface.tableExists('playbackSessions')) {
const tableDescription = await queryInterface.describeTable('playbackSessions')
if (tableDescription.ipAddress) {
logger.info(`${loggerPrefix} Removing ipAddress column from playbackSessions table`)
await queryInterface.removeColumn('playbackSessions', 'ipAddress')
logger.info(`${loggerPrefix} Removed ipAddress column from playbackSessions table`)
} else {
logger.info(`${loggerPrefix} ipAddress column does not exist in playbackSessions table`)
}
} else {
logger.info(`${loggerPrefix} playbackSessions table does not exist`)
}
logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`)
}
module.exports = { up, down }

View File

@ -16,6 +16,8 @@ class PlaybackSession extends Model {
this.displayTitle
/** @type {string} */
this.displayAuthor
/** @type {string} */
this.ipAddress
/** @type {number} */
this.duration
/** @type {number} */
@ -93,6 +95,7 @@ class PlaybackSession extends Model {
displayAuthor: playbackSessionExpanded.displayAuthor,
coverPath: playbackSessionExpanded.coverPath,
duration: playbackSessionExpanded.duration,
ipAddress: playbackSessionExpanded.ipAddress,
playMethod: playbackSessionExpanded.playMethod,
mediaPlayer: playbackSessionExpanded.mediaPlayer,
deviceInfo: playbackSessionExpanded.device?.getOldDevice() || null,
@ -141,6 +144,7 @@ class PlaybackSession extends Model {
displayTitle: oldPlaybackSession.displayTitle,
displayAuthor: oldPlaybackSession.displayAuthor,
duration: oldPlaybackSession.duration,
ipAddress: oldPlaybackSession.ipAddress,
playMethod: oldPlaybackSession.playMethod,
mediaPlayer: oldPlaybackSession.mediaPlayer,
startTime: oldPlaybackSession.startTime,
@ -184,6 +188,7 @@ class PlaybackSession extends Model {
displayTitle: DataTypes.STRING,
displayAuthor: DataTypes.STRING,
duration: DataTypes.FLOAT,
ipAddress: DataTypes.STRING,
playMethod: DataTypes.INTEGER,
mediaPlayer: DataTypes.STRING,
startTime: DataTypes.FLOAT,

View File

@ -23,6 +23,7 @@ class PlaybackSession {
this.playMethod = null
this.mediaPlayer = null
this.deviceInfo = null
this.ipAddress = null
this.serverVersion = null
this.date = null
@ -67,6 +68,7 @@ class PlaybackSession {
playMethod: this.playMethod,
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
ipAddress: this.ipAddress,
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
@ -101,6 +103,7 @@ class PlaybackSession {
playMethod: this.playMethod,
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
ipAddress: this.ipAddress,
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
@ -125,6 +128,7 @@ class PlaybackSession {
this.duration = session.duration
this.playMethod = session.playMethod
this.mediaPlayer = session.mediaPlayer || null
this.ipAddress = session.ipAddress || null
// Temp do not store old IDs
if (this.libraryId?.startsWith('lib_')) {
@ -222,6 +226,7 @@ class PlaybackSession {
this.mediaPlayer = mediaPlayer
this.deviceInfo = deviceInfo || new DeviceInfo()
this.ipAddress = this.deviceInfo.ipAddress
this.serverVersion = serverVersion
this.timeListening = 0