Public/Get-SlackGroupHistory.ps1

function Get-RyverGroupHistory {
    <#
    .SYNOPSIS
        Get history from a Ryver group
 
    .DESCRIPTION
        Get history from a Ryver group
 
    .PARAMETER Token
        Specify a token for authorization.
 
        See 'Authentication' section here for more information: https://api.ryver.com/web
        Test tokens are a simple way to use this
 
    .PARAMETER Id
        One or more group IDs to extract history from.
 
    .PARAMETER Before
        Return history from before this date
 
    .PARAMETER After
        Return history from after this date
 
    .PARAMETER Inclusive
        If specified, include history from the date specified in Before and/or After parameters
 
    .PARAMETER Count
        Number of messages to return per query. Defaults to 100. Max 1000
 
    .PARAMETER Paging
        If specified, and more data is available with a given 'Count', continue querying Ryver until
            we have retrieved all the data available.
 
        WARNING: This parameter is experimental
 
    .PARAMETER MaxQueries
        Limit the count of API queries to this number. Only used if you enable -Paging
 
    .PARAMETER Raw
        If specified, we provide raw output and do not parse any responses
 
    .FUNCTIONALITY
        Ryver
    #>

    [CmdletBinding(
        HelpUri = 'https://tlindsay42.github.io/PSRyver/Public/Get-RyverGroupHistory/'
    )]
    param (
        $Token = $Script:PSRyver.Token,
        [parameter( ValueFromPipelineByPropertyName = $true)] # a bit iffy... ID is common...
        [Alias('ID' )]
        [String[]]$GroupID,

        [ValidateRange(1, 1000)]
        [Int] $Count = 100,
        [Switch]$Inclusive,
        [DateTime]$Before,
        [DateTime]$After,
        [Switch]$Paging,
        [Int] $MaxQueries,
        [Switch]$Raw

    )
    begin {
        function ConvertTo-UnixTime {
            param ($Date)
            $unixEpochStart = new-object DateTime 1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc)
            [Int]($Date.ToUniversalTime() - $unixEpochStart).TotalSeconds
        }

        Write-Verbose "$($PSBoundParameters | Remove-SensitiveData | Out-String)"

        $body = @{
            channel = $null
            count   = $count
        }
        if ($Paging) {
            $PageDirection = 'Backward'
        }
        $BeforeTS = $null
        $AfterTS = $null
        if ($PSBoundParameters.ContainsKey('Before')) {
            $BeforeTS = ConvertTo-UnixTime -Date $Before
            $body.add('latest', $BeforeTS)
        }
        if ($PSBoundParameters.ContainsKey('After')) {
            $AfterTS = ConvertTo-UnixTime -Date $After
            $body.add('oldest', $AfterTS)
            if (-not $PSBoundParameters.ContainsKey('Before') -and $Paging) {
                $PageDirection = 'Forward'
            }
        }
        if ($Inclusive) {
            $body.add('inclusive', 1)
        }
        $params = @{
            Token  = $Token
            Method = 'groups.history'
            Body   = $body
        }
        $Queries = 1

    }
    process {
        foreach ($ID in $GroupID) {
            $has_more = $false
            $Params.body.channel = $ID
            do {
                if ($has_more) {
                    if ($Params.Body.oldest) {
                        [void]$Params.Body.remove('oldest')
                    }
                    if ($Params.Body.latest) {
                        [void]$Params.Body.remove('latest')
                    }
                    if ($PageDirection -eq 'Forward') {

                        $ts = $response.messages.ts | Sort-Object | Select-Object -last 1
                        $Params.body.oldest = $ts
                        Write-Debug "Paging Forward.`n$(
                            [pscustomobject]@{
                                After = $After
                                Before = $Before
                                LastTS = $response.messages[-1].ts
                                SortLast = $response.messages.ts | Sort-Object | Select-Object -last 1
                                SortFirst = $response.messages.ts | Sort-Object | Select-Object -first 1
                                ts = $ts
                            } | Out-String
                        )"

                    }
                    elseif ($PageDirection -eq 'Backward') {
                        $ts = $response.messages[-1].ts
                        if ($AfterTS -and $ts -lt $AfterTS) {
                            Write-Debug "TS is less than AfterTS, breaking!"
                            break
                        }
                        $Params.body.latest = $ts
                        Write-Debug "Paging Forward.`n$(
                            [pscustomobject]@{
                                After = $After
                                Before = $Before
                                LastTS = $response.messages[-1].ts
                                SortLast = $response.messages.ts | Sort-Object | Select-Object -last 1
                                SortFirst = $response.messages.ts | Sort-Object | Select-Object -first 1
                                ts = $ts
                            } | Out-String
                        )"

                    }

                    $has_more = $false
                    Write-Debug "Body is now:$($params.body | out-string)"
                }
                $response = Invoke-RyverRestMethod @params

                Write-Debug "$($Response | Format-List -Property * | Out-String)"

                if ($response.OK) {

                    if ($response.psobject.properties.name -contains 'has_more' -and $response.has_more) {
                        Write-Debug 'Paging engaged!'
                        $has_more = $true
                    }

                    if ($Raw) {
                        $link = "$($Script:PSRyver.ArchiveUri)/$($response.group)/p$($response.ts -replace '\.')"
                        $response | Add-Member -MemberType NoteProperty -Name link -Value $link
                        $response
                    }
                    else {
                        #Order our messages appropriately according to page direction
                        if ($Paging -and $PageDirection -eq 'Forward') {
                            Format-RyverV1MessageObject -InputObject $Response | Sort-Object TimeStamp
                        }
                        else {
                            Format-RyverV1MessageObject -InputObject $Response
                        }
                    }
                }
                else {
                    $response
                }
                $Queries++
            }
            until (
                -not $Paging -or
                -not $has_more -or
                ($MaxQueries -and $Queries -gt $MaxQueries)
            )
        }
    }
}