Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ ExampleData
/Test-*
log/*.log
config/conf.json
update-notification.marker
38 changes: 29 additions & 9 deletions resources/UpdateInfo.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Get-UpdateShouldNotify {

$result = [UpdateShouldNotifyResult]@{
ShouldNotify = $true
Message = ''
}

# If no update is available, no need to notify
Expand All @@ -26,27 +27,46 @@ function Get-UpdateShouldNotify {
# Define marker file path
$markerFilePath = "$PSScriptRoot\update-notification.marker"

$currentVersion = $UpdateStatus.CurrentVersion

# Check if marker file exists
if (Test-Path $markerFilePath) {
$versionChanged = $false
$markerVersion = Get-Content -Path $markerFilePath -Raw -ErrorAction SilentlyContinue
if ($null -ne $markerVersion) {
# Trim version and compare with current version
$markerVersion = $markerVersion.Trim()
$versionChanged = $markerVersion -ne $currentVersion
}

$markerFile = Get-Item $markerFilePath
$timeSinceLastNotification = (Get-Date) - $markerFile.LastWriteTime

# If less than 24 hours have passed since last notification, don't notify
# If version has changed, always notify regardless of time
if ($versionChanged) {
$result.Message = "Version changed from $markerVersion to $currentVersion since last notification. Proceeding to notify."
# Update the marker file with current version
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
return $result
}

# If less than 24 hours have passed since last notification for the same version, don't notify
if ($timeSinceLastNotification.TotalHours -lt 24) {
$result.ShouldNotify = $false
$result.Message = "Update notification suppressed. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago."

return $result
}
}

# Create or touch the marker file to indicate notification was sent
if (Test-Path $markerFilePath) {
(Get-Item $markerFilePath).LastWriteTime = Get-Date
# If more than 24 hours have passed, proceed to notify and update the marker file contents
else {
$result.Message = "Update notification marker file found. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago. Proceeding to notify."
# Update the marker file with current version - Also updates the file's modtime as a side effect.
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
}
}
else {
New-Item -Path $markerFilePath -ItemType File -Force | Out-Null
$result.Message = "Created update notification marker file at $markerFilePath"
# Create the marker file to indicate notification was sent and store current version
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
$result.Message = "Created update notification marker file at $markerFilePath with version $currentVersion"
}

return $result
Expand Down
Loading