Skip to content

Commit 1b61870

Browse files
committed
Re-send channel welcome (RPL_NAMREPLY, ...) when re-joining
When joining a channel for the second time, join events are part of the timeline (not just the state events list), but IRC clients registered the previous PART so they erased the nick list.
1 parent 97b277e commit 1b61870

File tree

2 files changed

+121
-9
lines changed

2 files changed

+121
-9
lines changed

lib/matrix_client/poller.ex

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ defmodule M51.MatrixClient.Poller do
335335
)
336336
when is_binary(state_key) do
337337
state = M51.IrcConn.Supervisor.matrix_state(sup_pid)
338+
irc_state = M51.IrcConn.Supervisor.state(sup_pid)
338339
channel = M51.MatrixClient.State.room_irc_channel(state, room_id)
339340
send = make_send_function(sup_pid, event, write)
340341

@@ -357,12 +358,28 @@ defmodule M51.MatrixClient.Poller do
357358
)
358359

359360
if !state_event and !was_already_member do
360-
send.(%M51.Irc.Command{
361-
tags: %{"account" => target},
362-
source: nick2nuh(target),
363-
command: "JOIN",
364-
params: [channel, target, target]
365-
})
361+
my_nick = M51.IrcConn.State.nick(irc_state)
362+
363+
if sender == my_nick do
364+
old_canonical_alias = nil
365+
canonical_alias_sender = nil
366+
367+
send_channel_welcome(
368+
sup_pid,
369+
room_id,
370+
canonical_alias_sender,
371+
old_canonical_alias,
372+
write,
373+
event
374+
)
375+
else
376+
send.(%M51.Irc.Command{
377+
tags: %{"account" => target},
378+
source: nick2nuh(target),
379+
command: "JOIN",
380+
params: [channel, target, target]
381+
})
382+
end
366383
end
367384

368385
"leave" ->
@@ -1013,14 +1030,15 @@ defmodule M51.MatrixClient.Poller do
10131030
state = M51.IrcConn.Supervisor.matrix_state(sup_pid)
10141031
nick = M51.IrcConn.State.nick(irc_state)
10151032
channel = M51.MatrixClient.State.room_irc_channel(state, room_id)
1016-
send = make_send_function(sup_pid, event, write)
1033+
send_join = make_send_function(sup_pid, event, write)
1034+
send_nonjoin = make_send_function(sup_pid, nil, write)
10171035

10181036
make_numeric = fn numeric, params ->
10191037
%M51.Irc.Command{source: "server.", command: numeric, params: [nick | params]}
10201038
end
10211039

10221040
send_numeric = fn numeric, params ->
1023-
send.(make_numeric.(numeric, params))
1041+
send_nonjoin.(make_numeric.(numeric, params))
10241042
end
10251043

10261044
# Join the new channel
@@ -1031,7 +1049,7 @@ defmodule M51.MatrixClient.Poller do
10311049
%M51.Matrix.RoomMember{display_name: nil}
10321050
)
10331051

1034-
send.(%M51.Irc.Command{
1052+
send_join.(%M51.Irc.Command{
10351053
tags: %{"account" => nick},
10361054
source: nick2nuh(nick),
10371055
command: "JOIN",

test/matrix_client/poller_test.exs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,92 @@ defmodule M51.MatrixClient.PollerTest do
478478
)
479479
end
480480

481+
test "re-joined room" do
482+
state_events = [
483+
%{
484+
"content" => %{"alias" => "#test:example.org"},
485+
"event_id" => "$event1",
486+
"origin_server_ts" => 1_632_644_251_623,
487+
"sender" => "@nick:example.org",
488+
"state_key" => "",
489+
"type" => "m.room.canonical_alias",
490+
"unsigned" => %{}
491+
},
492+
%{
493+
"content" => %{"avatar_url" => nil, "displayname" => "Name 2", "membership" => "join"},
494+
"event_id" => "$event2",
495+
"origin_server_ts" => 1_632_648_797_438,
496+
"sender" => "nick2:example.org",
497+
"state_key" => "nick2:example.org",
498+
"type" => "m.room.member",
499+
"unsigned" => %{}
500+
},
501+
%{
502+
"content" => %{"avatar_url" => nil, "displayname" => "My Name", "membership" => "join"},
503+
"event_id" => "$event3",
504+
"origin_server_ts" => 1_632_648_797_438,
505+
"sender" => "mynick:example.com",
506+
"state_key" => "mynick:example.com",
507+
"type" => "m.room.member",
508+
"unsigned" => %{}
509+
}
510+
]
511+
512+
timeline_events = [
513+
%{
514+
"content" => %{"membership" => "leave"},
515+
"event_id" => "$event3",
516+
"origin_server_ts" => 1_632_648_797_438,
517+
"sender" => "mynick:example.com",
518+
"state_key" => "mynick:example.com",
519+
"type" => "m.room.member",
520+
"unsigned" => %{}
521+
},
522+
%{
523+
"content" => %{"avatar_url" => nil, "displayname" => "My Name", "membership" => "join"},
524+
"event_id" => "$event3",
525+
"origin_server_ts" => 1_632_648_797_438,
526+
"sender" => "mynick:example.com",
527+
"state_key" => "mynick:example.com",
528+
"type" => "m.room.member",
529+
"unsigned" => %{}
530+
}
531+
]
532+
533+
M51.MatrixClient.Poller.handle_events(self(), %{
534+
"rooms" => %{
535+
"join" => %{
536+
"!testid:example.org" => %{
537+
"state" => %{"events" => state_events},
538+
"timeline" => %{"events" => timeline_events}
539+
}
540+
}
541+
}
542+
})
543+
544+
# first welcome
545+
assert_line(":mynick:[email protected] JOIN :#test:example.org\r\n")
546+
assert_line(":server. 331 mynick:example.com :#test:example.org\r\n")
547+
548+
assert_line(
549+
":server. 353 mynick:example.com = #test:example.org :mynick:example.com nick2:example.org\r\n"
550+
)
551+
552+
assert_line(":server. 366 mynick:example.com #test:example.org :End of /NAMES list\r\n")
553+
554+
assert_line(":mynick:[email protected] PART :#test:example.org\r\n")
555+
556+
# second welcome
557+
assert_line(":mynick:[email protected] JOIN :#test:example.org\r\n")
558+
assert_line(":server. 331 mynick:example.com :#test:example.org\r\n")
559+
560+
assert_line(
561+
":server. 353 mynick:example.com = #test:example.org :mynick:example.com nick2:example.org\r\n"
562+
)
563+
564+
assert_line(":server. 366 mynick:example.com #test:example.org :End of /NAMES list\r\n")
565+
end
566+
481567
test "room name suppression" do
482568
M51.IrcConn.State.add_capabilities(:process_ircconn_state, [
483569
:channel_rename,
@@ -725,6 +811,14 @@ defmodule M51.MatrixClient.PollerTest do
725811
"@msgid=$event7 :mynick:[email protected] JOIN :#test:example.com\r\n"
726812
)
727813

814+
assert_line(":server. 331 mynick:example.com :#test:example.com\r\n")
815+
816+
assert_line(
817+
":server. 353 mynick:example.com = #test:example.com :mynick:example.com nick2:example.com\r\n"
818+
)
819+
820+
assert_line(":server. 366 mynick:example.com #test:example.com :End of /NAMES list\r\n")
821+
728822
assert_line(
729823
"@msgid=$event4 :mynick:[email protected] KICK #test:example.com nick2:example.com :I don't like you\r\n"
730824
)

0 commit comments

Comments
 (0)