PoshBot.FYI.psm1

# # Dot source public/private functions
# $public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'public/*.ps1') -Recurse -ErrorAction Stop)
# $private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'private/*.ps1') -Recurse -ErrorAction Stop)
# foreach ($import in @($public + $private)) {
# try {
# . $import.FullName
# }
# catch {
# throw "Unable to dot source [$($import.FullName)]"
# }
# }

# Export-ModuleMember -Function $public.Basename
function New-PoshBotFYI {
    [PoshBot.BotCommand(
        CommandName = 'fyi'
    )]
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromRemainingArguments)]
        [string]$FYI
    )

    # Get current FYIs
    $currentFYIs = @(Get-PoshBotStatefulData -Name FYIs -ValueOnly)
    if (-not $currentFYIs) {
       $currentFYIs = @()
    }

    # Resolve channel and calling user
    $channel = $global:PoshBotContext.ToName
    if ([string]::IsNullOrEmpty($channel)) {
        $channel = 'unknown'
    }
    $addedBy = $global:PoshBotContext.FromName
    if ([string]::IsNullOrEmpty($addedBy)) {
        $addedBy = 'unknown'
    }

    # Add FYI
    $item = [pscustomobject]@{
        PSTypeName  = 'PoshBot.FYI.Item'
        FYI         = $FYI
        AddedBy     = $addedBy
        Channel     = $channel
        LastUpdated = [datetime]::UtcNow.ToString('u')
    }
    $currentFYIs += $item
    Set-PoshBotStatefulData -Value $currentFYIs -Name FYIs -Depth 10

    $response = "OK, new FYI [$FYI] added."
    New-PoshBotTextResponse -Text $response -AsCode
}
function Search-PoshBotFYI {
    [PoshBot.BotCommand(
        CommandName = 'how'
    )]
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromRemainingArguments)]
        [string]$FYI
    )

    # Get current FYIs
    $currentFYIs = @(Get-PoshBotStatefulData -Name FYIs -ValueOnly)
    if (-not $currentFYIs) {
        $currentFYIs = @()
        New-PoshBotTextResponse -Text 'There are no FYIs currently registered :(' -AsCode
        return
    }

    # Search FYIs
    $results = [System.Collections.ArrayList]::new()
    foreach ($item in $currentFYIs) {
        $searchResult = Communary.PASM\Select-FuzzyString -Search $FYI -Data $item.FYI -CalculateScore
        if ($searchResult) {
            $searchResult | Add-Member -MemberType NoteProperty -Name Item -Value $item
            $results.Add($searchResult) > $null
        }
    }

    if ($results.Count -eq 0) {
        New-PoshBotCardResponse -Type Warning -Text "Couldn't find a matching FYI :("
        return
    }

    # Sort by relevance
    $relevantResults = $results |
        Sort-Object -Property Score -Descending |
        Select-Object -First 10 |
        Sort-Object -Property @{e={$_.item.LastUpdated}} -Descending

    # Return response
    $nl = [Environment]::NewLine
    $response = @("Here is what I found:$nl")
    $relevantResults | ForEach-Object {
        $response += '{0}At *{1}* `@{2}` wrote in `#{3}`: {4}' -f $nl, $_.Item.LastUpdated, $_.Item.AddedBy, $_.Item.Channel, $_.Item.FYI
    }
    $text = $response -join $nl
    Write-Output $text
}