add function for getting joined rooms

This commit is contained in:
James 2022-03-27 19:23:38 +01:00
parent de4a048ab2
commit c16d850676
3 changed files with 64 additions and 1 deletions

View File

@ -71,7 +71,8 @@ PowerShellVersion = '7.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'New-MatrixAccessToken',
'Remove-MatrixAccessToken'
'Remove-MatrixAccessToken',
'Get-MatrixJoinedRooms'
)
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

View File

@ -0,0 +1,10 @@
function Get-MatrixAuthHeaders {
param(
[Parameter(Mandatory)]
[SecureString]$AccessToken
)
return @{
Authorization="Bearer " + ($AccessToken | ConvertFrom-SecureString -AsPlainText)
}
}

View File

@ -0,0 +1,52 @@
function Get-MatrixJoinedRooms {
param(
[Parameter(Mandatory)]
[string]$ServerUrl,
[Parameter(Mandatory)]
[SecureString]$AccessToken
)
try {
$url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/joined_rooms"
}
catch {
Write-Error $Error[0]
return
}
$headers = Get-MatrixAuthHeaders -AccessToken $AccessToken
$res = Invoke-RestMethod -Uri $url -Headers $headers
$rooms = @()
foreach($roomId in $res.joined_rooms) {
$roomAliasUrl = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/rooms/$roomId/state/m.room.canonical_alias"
$mainAlias = $null
$altAliases = @()
Write-Debug "Retrieving aliases for room ID $roomId via request $roomAliasUrl"
try {
$aliasRes = Invoke-RestMethod -Uri $roomAliasUrl -Headers $headers
$mainAlias = $aliasRes.alias
$altAliases = $aliasRes.alt_aliases
if($null -eq $altAliases) {
$altAliases = @()
}
}
catch {
# Write-Warning $Error[0]
}
$room = [PSCustomObject]@{
RoomID = $roomId
MainAlias = $mainAlias
AltAliases = $altAliases
}
$rooms += $room
}
return $rooms
}