~emersion/public-inbox

gamja: fetchHistoryBetween: Use the 'before' argument to stop instead of inferring from the limit. v1 NEEDS REVISION

Valentin Lorentz
Valentin Lorentz: 2
 fetchHistoryBetween: Use the 'before' argument to stop instead of inferring from the limit.
 Add support for msgid-based chathistory

 3 files changed, 51 insertions(+), 19 deletions(-)
Thanks a lot for taking the time to send this patch! Below are a few comments.

On Saturday, October 23rd, 2021 at 11:37, Valentin Lorentz <progval+git@progval.net> wrote:
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/25891/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gamja 1/2] fetchHistoryBetween: Use the 'before' argument to stop instead of inferring from the limit. Export this patch

Valentin Lorentz
---
 lib/client.js | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/client.js b/lib/client.js
index c80da50..fe0316a 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -750,12 +750,12 @@ export default class Client extends EventTarget {
			if (limit <= 0) {
				throw new Error("Cannot fetch all chat history: too many messages");
			}
			if (messages.length == max) {
				// There are still more messages to fetch
				after.time = messages[messages.length - 1].tags.time;
				return this.fetchHistoryBetween(target, after, before, limit);
			if (messages[0].tags.time <= before) {
				return null;
			}
			return null;
			// There are still more messages to fetch
			after.time = messages[messages.length - 1].tags.time;
			return this.fetchHistoryBetween(target, after, before, limit);
		});
	}

-- 
2.20.1

[PATCH gamja 2/2] Add support for msgid-based chathistory Export this patch

Valentin Lorentz
In addition to the default timestamp-based.
---
 components/app.js | 27 +++++++++++++++++++--------
 lib/client.js     | 33 +++++++++++++++++++++++++++------
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/components/app.js b/components/app.js
index 468157c..8e74b60 100644
--- a/components/app.js
+++ b/components/app.js
@@ -375,7 +375,7 @@ export default class App extends Component {
		// TODO: this doesn't trigger a redraw
		this.receipts.set(target, {
			...this.receipts.get(target),
			[type]: { time: msg.tags.time },
			[type]: { time: msg.tags.time, msgid: msg.tags.msgid },
		});
		this.saveReceipts();
	}
@@ -387,7 +387,7 @@ export default class App extends Component {
				return;
			}
			let delivery = receipts[type];
			if (!delivery || !delivery.time) {
			if (!delivery || !delivery.msgid || !delivery.time) {
				return;
			}
			if (!last || delivery.time > last.time) {
@@ -594,12 +594,18 @@ export default class App extends Component {
		switch (msg.command) {
		case irc.RPL_WELCOME:
			let lastReceipt = this.latestReceipt(ReceiptType.DELIVERED);
			if (lastReceipt && lastReceipt.time && client.enabledCaps["draft/chathistory"] && (!client.enabledCaps["soju.im/bouncer-networks"] || client.params.bouncerNetwork)) {
			if (lastReceipt && (lastReceipt.msgid || lastReceipt.time) && client.enabledCaps["draft/chathistory"] && (!client.enabledCaps["soju.im/bouncer-networks"] || client.params.bouncerNetwork)) {
				let now = irc.formatDate(new Date());
				client.fetchHistoryTargets(now, lastReceipt.time).then((targets) => {
				client.fetchHistoryTargets(now, lastReceipt).then((targets) => {
					targets.forEach((target) => {
						let from = lastReceipt;
						let to = { time: msg.tags.time || irc.formatDate(new Date()) };
						let to;
						if ("msgid" in msg.tags) {
							to = { msgid: msg.tags.msgid };
						}
						else {
							to = { time: msg.tags.time || irc.formatDate(new Date()) };
						}
						this.fetchBacklog(client, target.name, from, to);
					});
				});
@@ -1210,11 +1216,16 @@ export default class App extends Component {
			return;
		}

		let before;
		let before = {};
		if (buf.messages.length > 0) {
			before = buf.messages[0].tags["time"];
			if (buf.messages[0].tags.msgid) {
				before.msgid = buf.messages[0].tags.msgid;
			}
			if (buf.messages[0].tags.time) {
				before.time = buf.messages[0].tags.time;
			}
		} else {
			before = irc.formatDate(new Date());
			before.time = irc.formatDate(new Date());
		}

		// Avoids sending multiple CHATHISTORY commands in parallel
diff --git a/lib/client.js b/lib/client.js
index fe0316a..b98d75e 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -732,25 +732,46 @@ export default class Client extends EventTarget {
		return 100;
	}

	/* Fetch one page of history before the given date. */
	formatHistoryAnchor(anchor) {
		if (anchor.msgid) {
			return "msgid=" + anchor.msgid
		}
		else if (anchor.time) {
			return "timestamp=" + anchor.time
		}
		else {
			throw new Error("Unexpected history anchor: " + anchor);
		}
	}

	/* Fetch one page of history before the given anchor.
	 * 'before' must be an object with either a 'msgid' or 'timestamp' attribute. */
	fetchHistoryBefore(target, before, limit) {
		let max = Math.min(limit, this.chatHistoryPageSize());
		let params = ["BEFORE", target, "timestamp=" + before, max];
		let params = ["BEFORE", target, this.formatHistoryAnchor(before), max];
		return this.roundtripChatHistory(params).then((messages) => {
			return { more: messages.length >= max };
		});
	}

	/* Fetch history in ascending order. */
	/* Fetch history in ascending order between two anchors.
	 * 'before' and 'after' must an objects with either a 'msgid' or 'timestamp' attribute. */
	fetchHistoryBetween(target, after, before, limit) {
		let max = Math.min(limit, this.chatHistoryPageSize());
		let params = ["AFTER", target, "timestamp=" + after.time, max];
		let params = ["AFTER", target, this.formatHistoryAnchor(after), max];
		return this.roundtripChatHistory(params).then((messages) => {
			limit -= messages.length;
			if (limit <= 0) {
				throw new Error("Cannot fetch all chat history: too many messages");
			}
			if (messages[0].tags.time <= before) {
			for (message in messages) {
				if (before.msgid) {
					if (message.msgid == before.msgid) {
						return null;
					}
				}
			}
			if (before.time && messages[0].tags.time <= before.time) {
				return null;
			}
			// There are still more messages to fetch
@@ -762,7 +783,7 @@ export default class Client extends EventTarget {
	fetchHistoryTargets(t1, t2) {
		let msg = {
			command: "CHATHISTORY",
			params: ["TARGETS", "timestamp=" + t1, "timestamp=" + t2, 1000],
			params: ["TARGETS", this.formatHistoryAnchor(t1), this.formatHistoryAnchor(t2), 1000],
		};
		return this.fetchBatch(msg, "draft/chathistory-targets").then((batch) => {
			return batch.messages.map((msg) => {
-- 
2.20.1
Thanks a lot for taking the time to send this patch! Below are a few comments.

On Saturday, October 23rd, 2021 at 11:37, Valentin Lorentz <progval+git@progval.net> wrote: