Private/Test-GhostPostChange.ps1
|
function Test-GhostPostChange { <# .SYNOPSIS Tells whether a post on the site differs from the post a Markdown file describes. .DESCRIPTION Sync-GhostPost uses this to leave a post alone when nothing would change. An update that changes nothing is not harmless: it moves updated_at, which themes show as the date the post was updated, and it sets off webhooks. Only the content and the fields that the file sets are compared; everything else is not touched by an update either. The answer is false only when every one of them is known to be equal. Whenever that cannot be established, the answer is true and the post is updated, which is always correct. A tag given as text is compared with the name of the tag, an author given as text with the email address of the author. A tag or an author given as an object has to match in every property it has. .EXAMPLE Test-GhostPostChange -Post $post -Parameters $file.Parameters -Fields $fields -Markdown $file.Markdown Returns true when the post has to be updated to match the file. #> [CmdletBinding()] [OutputType([bool])] param( # The post as Get-GhostPost returned it, with its lexical content. [Parameter(Mandatory)] [object] $Post, # The parameters the file sets, as Read-GhostPostFile returned them. [Parameter(Mandatory)] [System.Collections.IDictionary] $Parameters, # Parameter name = name of the field in the Ghost API, for the fields that are compared. [Parameter(Mandatory)] [System.Collections.IDictionary] $Fields, # The Markdown of the file. [Parameter(Mandatory)] [AllowEmptyString()] [string] $Markdown ) # The content: the Markdown has to be the one and only card of the post. $children = $null $lexical = $Post.PSObject.Properties['lexical'] if ($lexical -and $lexical.Value) { try { $children = @((ConvertFrom-Json -InputObject $lexical.Value).root.children) } catch { Write-Debug -Message "The content of the post cannot be read: $($_.Exception.Message)" } } if ($null -eq $children) { return $true } if ([string]::IsNullOrWhiteSpace($Markdown)) { if ($children.Count -ne 0) { return $true } } else { if ($children.Count -ne 1 -or $null -eq $children[0]) { return $true } $type = $children[0].PSObject.Properties['type'] $stored = $children[0].PSObject.Properties['markdown'] if (-not $type -or $type.Value -ne 'markdown' -or -not $stored -or [string] $stored.Value -cne $Markdown) { return $true } } # The fields that the file sets. foreach ($name in $Parameters.Keys) { if ($Fields.Keys -notcontains $name) { continue } $property = $Post.PSObject.Properties[$Fields[$name]] if (-not $property) { return $true } $current = $property.Value $desired = $Parameters[$name] if ($name -eq 'Tags' -or $name -eq 'Authors') { $key = if ($name -eq 'Tags') { 'name' } else { 'email' } $currentItems = @($current) $desiredItems = @($desired) if ($currentItems.Count -ne $desiredItems.Count) { return $true } for ($index = 0; $index -lt $desiredItems.Count; $index++) { $wanted = $desiredItems[$index] $actual = $currentItems[$index] if ($wanted -is [string]) { $wanted = [pscustomobject]@{ $key = $wanted } } # The full type name: [pscustomobject] stands for PSObject, which matches more than objects. if ($wanted -isnot [System.Management.Automation.PSCustomObject] -or $null -eq $actual) { return $true } foreach ($detail in $wanted.PSObject.Properties) { $actualDetail = $actual.PSObject.Properties[$detail.Name] if (-not $actualDetail -or [string] $actualDetail.Value -cne [string] $detail.Value) { return $true } } } continue } if ($name -eq 'PublishedAt') { # Ghost keeps dates to the second. try { $difference = ([datetime] $desired).ToUniversalTime() - ([datetime] $current).ToUniversalTime() } catch { return $true } if ([System.Math]::Abs($difference.TotalSeconds) -ge 1) { return $true } continue } # The value as it would be sent: an empty string clears a field, which Ghost stores as null. $desired = ConvertTo-ApiValue -Value $desired if ($null -eq $desired) { if (-not [string]::IsNullOrEmpty([string] $current)) { return $true } } elseif ($desired -is [bool]) { if ($current -isnot [bool] -or $current -ne $desired) { return $true } } elseif ([string] $current -cne [string] $desired) { return $true } } $false } |