diff options
author | Matthew Hodgson <matthew@matrix.org> | 2017-11-17 01:53:09 +0000 |
---|---|---|
committer | Matthew Hodgson <matthew@matrix.org> | 2017-11-17 01:54:02 +0000 |
commit | 9d50b6f0eade4f544ee7864383aa2a17bdc65a34 (patch) | |
tree | efccd22e9aadf3fd27285631055cfc90c90d397d /scripts/sync_room_to_group.pl | |
parent | Fix auth handler #2678 (diff) | |
download | synapse-9d50b6f0eade4f544ee7864383aa2a17bdc65a34.tar.xz |
quick and dirty room membership<->group membership sync script
Diffstat (limited to 'scripts/sync_room_to_group.pl')
-rwxr-xr-x | scripts/sync_room_to_group.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/scripts/sync_room_to_group.pl b/scripts/sync_room_to_group.pl new file mode 100755 index 0000000000..3366c2c68f --- /dev/null +++ b/scripts/sync_room_to_group.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use JSON::XS; +use LWP::UserAgent; +use URI::Escape; + +if (@ARGV < 4) { + die "usage: $0 <homeserver url> <access_token> <room_id|room_alias> <group_id>\n"; +} + +my ($hs, $access_token, $room_id, $group_id) = @ARGV; +my $ua = LWP::UserAgent->new(); +$ua->timeout(10); + +if ($room_id =~ /^#/) { + $room_id = uri_escape($room_id); + $room_id = decode_json($ua->get("${hs}/_matrix/client/r0/directory/room/${room_id}?access_token=${access_token}")->decoded_content)->{room_id}; +} + +my $room_users = [ keys %{decode_json($ua->get("${hs}/_matrix/client/r0/rooms/${room_id}/joined_members?access_token=${access_token}")->decoded_content)->{joined}} ]; +my $group_users = [ map { $_->{user_id} } @{decode_json($ua->get("${hs}/_matrix/client/unstable/groups/${group_id}/users?access_token=${access_token}" )->decoded_content)->{chunk}} ]; + +my $diff = {}; +foreach my $user (@$room_users) { $diff->{$user}++ } +foreach my $user (@$group_users) { $diff->{$user}-- } + +foreach my $user (keys %$diff) { + if ($diff->{$user} == 1) { + warn "inviting $user"; + $ua->put("${hs}/_matrix/client/unstable/groups/${group_id}/admin/users/invite/${user}?access_token=${access_token}", Content=>'{}'); + } + elsif ($diff->{$user} == -1) { + warn "removing $user"; + $ua->put("${hs}/_matrix/client/unstable/groups/${group_id}/admin/users/remove/${user}?access_token=${access_token}", Content=>'{}'); + } +} \ No newline at end of file |