From 7bf9a0ca25dcc41c42a9d808cc224acb455dd713 Mon Sep 17 00:00:00 2001
From: Simon Ser <contact@emersion.fr>
Date: Tue, 21 Sep 2021 19:20:39 +0200
Subject: [PATCH] Auto-join previous channels on reconnect

Closes: https://todo.sr.ht/~emersion/gamja/47
---
 components/app.js | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/components/app.js b/components/app.js
index 050b201..6d0303e 100644
--- a/components/app.js
+++ b/components/app.js
@@ -581,13 +581,6 @@ export default class App extends Component {
 		let target, channel, affectedBuffers;
 		switch (msg.command) {
 		case irc.RPL_WELCOME:
-			if (this.state.connectParams.autojoin.length > 0) {
-				client.send({
-					command: "JOIN",
-					params: [this.state.connectParams.autojoin.join(",")],
-				});
-			}
-
 			let lastReceipt = this.latestReceipt(ReceiptType.DELIVERED);
 			if (lastReceipt && lastReceipt.time && client.enabledCaps["draft/chathistory"] && (!client.enabledCaps["soju.im/bouncer-networks"] || client.params.bouncerNetwork)) {
 				let now = irc.formatDate(new Date());
@@ -604,13 +597,32 @@ export default class App extends Component {
 		case irc.ERR_NOMOTD:
 			// These messages are used to indicate the end of the ISUPPORT list
 
-			// Restore opened user query buffers
+			// Restore opened channel and user buffers
+			let join = [];
 			for (let buf of this.bufferStore.list(client.params)) {
-				if (buf.name === "*" || client.isChannel(buf.name)) {
+				if (buf.name === "*") {
 					continue;
 				}
-				this.createBuffer(serverID, buf.name);
-				this.whoUserBuffer(buf.name, serverID);
+
+				if (client.isChannel(buf.name)) {
+					if (client.enabledCaps["soju.im/bouncer-networks"]) {
+						continue;
+					}
+					join.push(buf.name);
+				} else {
+					this.createBuffer(serverID, buf.name);
+					this.whoUserBuffer(buf.name, serverID);
+				}
+			}
+
+			// Auto-join channels given at connect-time
+			join = join.concat(this.state.connectParams.autojoin);
+
+			if (join.length > 0) {
+				client.send({
+					command: "JOIN",
+					params: [join.join(",")],
+				});
 			}
 		case "MODE":
 			target = msg.params[0];