2023-06-08 06:09:24 -04:00
|
|
|
import { ReceiptType, Unread } from "./state.js";
|
2022-02-11 10:59:56 -05:00
|
|
|
|
2021-05-26 12:43:11 -04:00
|
|
|
const PREFIX = "gamja_";
|
|
|
|
|
|
|
|
class Item {
|
|
|
|
constructor(k) {
|
|
|
|
this.k = PREFIX + k;
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
2021-06-10 12:11:11 -04:00
|
|
|
let v = localStorage.getItem(this.k);
|
2021-05-26 12:43:11 -04:00
|
|
|
if (!v) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return JSON.parse(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
put(v) {
|
|
|
|
if (v) {
|
|
|
|
localStorage.setItem(this.k, JSON.stringify(v));
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(this.k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const autoconnect = new Item("autoconnect");
|
2021-11-07 11:53:10 -05:00
|
|
|
export const naggedProtocolHandler = new Item("naggedProtocolHandler");
|
2022-02-21 09:44:17 -05:00
|
|
|
export const settings = new Item("settings");
|
2021-05-26 12:43:11 -04:00
|
|
|
|
2022-02-11 10:24:32 -05:00
|
|
|
function debounce(f, delay) {
|
|
|
|
let timeout = null;
|
|
|
|
return (...args) => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
timeout = null;
|
|
|
|
f(...args);
|
|
|
|
}, delay);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-23 06:02:36 -04:00
|
|
|
export class Buffer {
|
|
|
|
raw = new Item("buffers");
|
|
|
|
m = null;
|
|
|
|
|
|
|
|
constructor() {
|
2021-09-21 12:26:42 -04:00
|
|
|
let obj = this.raw.load();
|
2021-08-23 06:02:36 -04:00
|
|
|
this.m = new Map(Object.entries(obj || {}));
|
2022-02-11 10:24:32 -05:00
|
|
|
|
2022-02-18 12:22:00 -05:00
|
|
|
let saveImmediately = this.save.bind(this);
|
|
|
|
this.save = debounce(saveImmediately, 500);
|
|
|
|
|
|
|
|
document.addEventListener("visibilitychange", () => {
|
|
|
|
if (document.visibilityState === "hidden") {
|
|
|
|
saveImmediately();
|
|
|
|
}
|
|
|
|
});
|
2021-08-23 06:02:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
key(buf) {
|
2022-04-22 06:04:11 -04:00
|
|
|
// TODO: use case-mapping here somehow
|
2021-08-23 06:02:36 -04:00
|
|
|
return JSON.stringify({
|
2022-04-22 06:04:11 -04:00
|
|
|
name: buf.name.toLowerCase(),
|
2021-08-23 06:02:36 -04:00
|
|
|
server: {
|
|
|
|
bouncerNetwork: buf.server.bouncerNetwork,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
if (this.m.size > 0) {
|
|
|
|
this.raw.put(Object.fromEntries(this.m));
|
|
|
|
} else {
|
|
|
|
this.raw.put(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:17:58 -04:00
|
|
|
get(buf) {
|
|
|
|
return this.m.get(this.key(buf));
|
|
|
|
}
|
|
|
|
|
2021-08-23 06:02:36 -04:00
|
|
|
put(buf) {
|
2021-08-23 06:25:43 -04:00
|
|
|
let key = this.key(buf);
|
|
|
|
|
2022-02-11 10:20:42 -05:00
|
|
|
let updated = !this.m.has(key);
|
|
|
|
let prev = this.m.get(key) || {};
|
|
|
|
|
2023-06-08 05:54:16 -04:00
|
|
|
let unread = prev.unread || Unread.NONE;
|
2022-02-11 10:20:42 -05:00
|
|
|
if (buf.unread !== undefined && buf.unread !== prev.unread) {
|
|
|
|
unread = buf.unread;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let receipts = { ...prev.receipts };
|
|
|
|
if (buf.receipts) {
|
|
|
|
Object.keys(buf.receipts).forEach((k) => {
|
2022-02-12 04:24:34 -05:00
|
|
|
// Use a not-equals comparison here so that no-op receipt
|
|
|
|
// changes are correctly handled
|
2022-02-11 15:07:49 -05:00
|
|
|
if (!receipts[k] || receipts[k].time < buf.receipts[k].time) {
|
2022-02-11 10:20:42 -05:00
|
|
|
receipts[k] = buf.receipts[k];
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
});
|
2022-02-11 10:59:56 -05:00
|
|
|
if (receipts[ReceiptType.DELIVERED] < receipts[ReceiptType.READ]) {
|
|
|
|
receipts[ReceiptType.DELIVERED] = receipts[ReceiptType.READ];
|
|
|
|
updated = true;
|
|
|
|
}
|
2022-02-11 10:20:42 -05:00
|
|
|
}
|
|
|
|
|
2023-06-08 05:54:16 -04:00
|
|
|
let closed = prev.closed || false;
|
|
|
|
if (buf.closed !== undefined && buf.closed !== prev.closed) {
|
|
|
|
closed = buf.closed;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:20:42 -05:00
|
|
|
if (!updated) {
|
2022-02-11 12:21:17 -05:00
|
|
|
return false;
|
2021-08-23 06:25:43 -04:00
|
|
|
}
|
|
|
|
|
2021-08-23 06:02:36 -04:00
|
|
|
this.m.set(this.key(buf), {
|
|
|
|
name: buf.name,
|
2022-02-11 10:20:42 -05:00
|
|
|
unread,
|
|
|
|
receipts,
|
2023-06-08 05:54:16 -04:00
|
|
|
closed,
|
2021-08-23 06:02:36 -04:00
|
|
|
server: {
|
|
|
|
bouncerNetwork: buf.server.bouncerNetwork,
|
|
|
|
},
|
|
|
|
});
|
2021-08-23 06:25:43 -04:00
|
|
|
|
2021-08-23 06:02:36 -04:00
|
|
|
this.save();
|
2022-02-11 12:21:17 -05:00
|
|
|
return true;
|
2021-08-23 06:02:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(buf) {
|
|
|
|
this.m.delete(this.key(buf));
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:17:58 -04:00
|
|
|
list(server) {
|
2023-04-04 10:51:23 -04:00
|
|
|
// Some gamja versions would store the same buffer multiple times
|
|
|
|
let names = new Set();
|
2021-08-23 06:02:36 -04:00
|
|
|
let buffers = [];
|
|
|
|
for (const buf of this.m.values()) {
|
2023-04-04 10:46:58 -04:00
|
|
|
if (buf.server.bouncerNetwork !== server.bouncerNetwork) {
|
2021-08-23 06:02:36 -04:00
|
|
|
continue;
|
|
|
|
}
|
2023-04-04 10:51:23 -04:00
|
|
|
if (names.has(buf.name)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-23 06:02:36 -04:00
|
|
|
buffers.push(buf);
|
2023-04-04 10:51:23 -04:00
|
|
|
names.add(buf.name);
|
2021-08-23 06:02:36 -04:00
|
|
|
}
|
|
|
|
return buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear(server) {
|
|
|
|
if (server) {
|
2023-04-04 11:00:05 -04:00
|
|
|
for (const buf of this.list(server)) {
|
2021-08-23 06:02:36 -04:00
|
|
|
this.m.delete(this.key(buf));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.m = new Map();
|
|
|
|
}
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
}
|