From c16d850676a15cb12b29bdc2d1d5766319620b11 Mon Sep 17 00:00:00 2001 From: Thumbscrew Date: Sun, 27 Mar 2022 19:23:38 +0100 Subject: [PATCH] add function for getting joined rooms --- PSMatrix.psd1 | 3 +- private/Get-MatrixAuthHeaders.ps1 | 10 ++++++ public/Get-MatrixJoinedRooms.ps1 | 52 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 private/Get-MatrixAuthHeaders.ps1 create mode 100644 public/Get-MatrixJoinedRooms.ps1 diff --git a/PSMatrix.psd1 b/PSMatrix.psd1 index 9a65448..68b668a 100644 --- a/PSMatrix.psd1 +++ b/PSMatrix.psd1 @@ -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. diff --git a/private/Get-MatrixAuthHeaders.ps1 b/private/Get-MatrixAuthHeaders.ps1 new file mode 100644 index 0000000..a3e2666 --- /dev/null +++ b/private/Get-MatrixAuthHeaders.ps1 @@ -0,0 +1,10 @@ +function Get-MatrixAuthHeaders { + param( + [Parameter(Mandatory)] + [SecureString]$AccessToken + ) + + return @{ + Authorization="Bearer " + ($AccessToken | ConvertFrom-SecureString -AsPlainText) + } +} \ No newline at end of file diff --git a/public/Get-MatrixJoinedRooms.ps1 b/public/Get-MatrixJoinedRooms.ps1 new file mode 100644 index 0000000..877cbe9 --- /dev/null +++ b/public/Get-MatrixJoinedRooms.ps1 @@ -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 +} \ No newline at end of file