From 016ca27f2ded56bdda5e6ac90e4c4bc64eb2c3c0 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 23 Aug 2021 12:02:36 +0200 Subject: [PATCH] Restore opened user query buffers Closes: https://todo.sr.ht/~emersion/gamja/93 --- components/app.js | 20 ++++++++++++-- store.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/components/app.js b/components/app.js index d10f2c8..315752a 100644 --- a/components/app.js +++ b/components/app.js @@ -170,6 +170,7 @@ export default class App extends Component { this.saveReceipts = debounce(this.saveReceipts.bind(this), 500); this.receipts = store.receipts.load(); + this.bufferStore = new store.Buffer(); configPromise.then((config) => { this.handleConfig(config); @@ -264,13 +265,14 @@ export default class App extends Component { } createBuffer(serverID, name) { + let client = this.clients.get(serverID); let id = null; this.setState((state) => { - let client = this.clients.get(serverID); let updated; [id, updated] = State.createBuffer(state, name, serverID, client); return updated; }); + this.bufferStore.put({ name, server: client.params }); return id; } @@ -540,6 +542,15 @@ export default class App extends Component { }); } + // Restore opened user query buffers + for (let buf of this.bufferStore.load(client.params)) { + if (buf.name === "*" || client.isChannel(buf.name)) { + continue; + } + this.createBuffer(serverID, buf.name); + client.who(buf.name); + } + let lastReceipt = this.latestReceipt(ReceiptType.READ); if (lastReceipt && lastReceipt.time && client.enabledCaps["draft/chathistory"] && (!client.enabledCaps["soju.im/bouncer-networks"] || client.params.bouncerNetwork)) { let now = irc.formatDate(new Date()); @@ -869,9 +880,12 @@ export default class App extends Component { for (let serverID of this.clients.keys()) { this.close({ server: serverID, name: SERVER_BUFFER }); } + this.bufferStore.clear(); + } else { + this.bufferStore.clear(client.params); } - // TODO: only clear local storage if this server is stored there + // TODO: only clear autoconnect if this server is stored there if (buf.server == 1) { store.autoconnect.put(null); } @@ -889,6 +903,8 @@ export default class App extends Component { this.receipts.delete(buf.name); this.saveReceipts(); + + this.bufferStore.delete({ name: buf.name, server: client.params }); break; } } diff --git a/store.js b/store.js index 5d4639f..7fd43f4 100644 --- a/store.js +++ b/store.js @@ -35,3 +35,71 @@ export const receipts = { rawReceipts.put(Object.fromEntries(m)); }, }; + +export class Buffer { + raw = new Item("buffers"); + m = null; + + constructor() { + var obj = this.raw.load(); + this.m = new Map(Object.entries(obj || {})); + } + + key(buf) { + return JSON.stringify({ + name: buf.name, + server: { + url: buf.server.url, + nick: buf.server.nick, + bouncerNetwork: buf.server.bouncerNetwork, + }, + }); + } + + save() { + if (this.m.size > 0) { + this.raw.put(Object.fromEntries(this.m)); + } else { + this.raw.put(null); + } + } + + put(buf) { + this.m.set(this.key(buf), { + name: buf.name, + server: { + url: buf.server.url, + nick: buf.server.nick, + bouncerNetwork: buf.server.bouncerNetwork, + }, + }); + this.save(); + } + + delete(buf) { + this.m.delete(this.key(buf)); + this.save(); + } + + load(server) { + let buffers = []; + for (const buf of this.m.values()) { + if (buf.server.url !== server.url || buf.server.nick !== server.nick || buf.server.bouncerNetwork !== server.bouncerNetwork) { + continue; + } + buffers.push(buf); + } + return buffers; + } + + clear(server) { + if (server) { + for (const buf of this.load(server)) { + this.m.delete(this.key(buf)); + } + } else { + this.m = new Map(); + } + this.save(); + } +}