-
Notifications
You must be signed in to change notification settings - Fork 55
/
31forget.pl
259 lines (207 loc) · 8.1 KB
/
31forget.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use List::Util qw( any none );
test "Forgotten room messages cannot be paginated",
requires => [ local_user_and_room_fixtures(), local_user_fixture() ],
do => sub {
my ( $creator, $room_id, $user ) = @_;
matrix_join_room( $user, $room_id )
->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "sup" )
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 1 );
})->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( chunk ) );
log_if_fail "Chunk", $body->{chunk};
$body->{chunk}[0]{content}{body} eq "sup" or die "Wrong message";
matrix_leave_room( $user, $room_id )
})->then( sub {
matrix_get_room_state( $creator, $room_id,
type => "m.room.member",
state_key => $user->user_id
)
})->then( sub {
my ( $content ) = @_;
assert_eq( $content->{membership}, "leave",
"membership state" );
matrix_forget_room( $user, $room_id )
})->then( sub {
matrix_get_room_state( $creator, $room_id,
type => "m.room.member",
state_key => $user->user_id
)
})->then( sub {
my ( $content ) = @_;
assert_eq( $content->{membership}, "leave",
"membership state" );
matrix_get_room_messages( $user, $room_id, limit => 1 )
->main::expect_http_403;
});
};
test "Forgetting room does not show up in v2 /sync",
requires => [ local_user_and_room_fixtures(), local_user_fixture() ],
do => sub {
my ( $creator, $room_id, $user ) = @_;
matrix_join_room( $user, $room_id )
->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "sup" )
})->then( sub {
matrix_leave_room( $user, $room_id )
})->then( sub {
matrix_forget_room( $user, $room_id )
})->then( sub {
matrix_sync( $user )
})->then( sub {
my ( $body ) = @_;
log_if_fail "Sync response", $body;
die "Did not expect room in archived" if $body->{rooms}->{archived}->{$room_id};
die "Did not expect room in joined" if $body->{rooms}->{joined}->{$room_id};
die "Did not expect room in invited" if $body->{rooms}->{invited}->{$room_id};
Future->done( 1 );
});
};
test "Can forget room you've been kicked from",
requires => [ local_user_and_room_fixtures(), local_user_fixture() ],
do => sub {
my ( $creator, $room_id, $user ) = @_;
matrix_join_room( $user, $room_id )
->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "sup" );
})->then( sub {
do_request_json_for( $creator,
method => "POST",
uri => "/r0/rooms/$room_id/kick",
content => { user_id => $user->user_id },
);
})->then( sub {
matrix_forget_room( $user, $room_id )
})->then( sub {
matrix_sync( $user )
})->then( sub {
my ( $body ) = @_;
log_if_fail "Sync response", $body;
die "Did not expect room in archived" if $body->{rooms}->{archived}->{$room_id};
die "Did not expect room in joined" if $body->{rooms}->{joined}->{$room_id};
die "Did not expect room in invited" if $body->{rooms}->{invited}->{$room_id};
Future->done( 1 );
});
};
test "Can't forget room you're still in",
requires => [ local_user_and_room_fixtures(), local_user_fixture() ],
do => sub {
my ( $creator, $room_id, $user ) = @_;
matrix_join_room( $user, $room_id )
->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "sup" );
})->then( sub {
matrix_forget_room( $user, $room_id )
})->main::expect_http_4xx;
};
test "Can re-join room if re-invited - history_visibility = shared",
requires => [ local_user_fixture(), local_user_fixture() ],
do => sub {
my ( $creator, $user ) = @_;
my ( $room_id );
matrix_create_room( $creator, invite => [ $user->user_id ] )->then( sub {
( $room_id ) = @_;
log_if_fail "room_id", $room_id;
matrix_put_room_state( $creator, $room_id,
type => "m.room.join_rules",
state_key => "",
content => {
join_rule => "invite",
}
)
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "before leave" );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 100 );
})->then( sub {
matrix_leave_room( $user, $room_id );
})->then( sub {
matrix_forget_room( $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id )->main::expect_http_403;
})->then( sub {
matrix_invite_user_to_room( $creator, $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 100 );
})->then( sub {
my ( $body ) = @_;
any { $_->{type} eq "m.room.message" && $_->{content}->{body} eq "before leave" } @{ $body->{chunk} }
or die "Should have seen before leave message";
matrix_send_room_text_message( $creator, $room_id, body => "after rejoin" );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 1 );
})->then( sub {
my ( $body ) = @_;
log_if_fail "body", $body;
@{ $body->{chunk} } == 1 or die "Expected event";
$body->{chunk}[0]->{type} eq "m.room.message" && $body->{chunk}[0]->{content}{body} eq "after rejoin"
or die "Got wrong event";
Future->done( 1 );
});
};
test "Can re-join room if re-invited - history_visibility joined",
requires => [ local_user_fixture(), local_user_fixture() ],
do => sub {
my ( $creator, $user ) = @_;
my ( $room_id );
matrix_create_room( $creator, invite => [ $user->user_id ] )->then( sub {
( $room_id ) = @_;
log_if_fail "room_id", $room_id;
matrix_put_room_state( $creator, $room_id,
type => "m.room.join_rules",
state_key => "",
content => {
join_rule => "invite",
}
)
})->then( sub {
matrix_set_room_history_visibility( $creator, $room_id, "joined");
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "before leave" );
})->then( sub {
matrix_leave_room( $user, $room_id );
})->then( sub {
matrix_forget_room( $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id )->main::expect_http_403;
})->then( sub {
matrix_invite_user_to_room( $creator, $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 100 );
})->then( sub {
my ( $body ) = @_;
none { $_->{type} eq "m.room.message" } @{ $body->{chunk} }
or die "Should not have seen any m.room.message events";
matrix_send_room_text_message( $creator, $room_id, body => "after rejoin" );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 1 );
})->then( sub {
my ( $body ) = @_;
log_if_fail "body", $body;
@{ $body->{chunk} } == 1 or die "Expected event";
$body->{chunk}[0]->{type} eq "m.room.message" && $body->{chunk}[0]->{content}{body} eq "after rejoin"
or die "Got wrong event";
Future->done( 1 );
});
};
push our @EXPORT, qw( matrix_forget_room );
sub matrix_forget_room
{
my ( $user, $room_id ) = @_;
is_User( $user ) or croak "Expected a User; got $user";
do_request_json_for( $user,
method => "POST",
uri => "/r0/rooms/$room_id/forget",
content => {},
)->then_done(1);
}