Public
Cron
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { discordWebhook } from "https://esm.town/v/pvh/discordWebhook";
import { BrowserWebSocketClientAdapter } from "npm:@automerge/automerge-repo-network-websocket";
import { isValidAutomergeUrl, Repo } from "npm:@automerge/automerge-repo/slim";
/* set up Automerge's internal wasm guts manually */
import { automergeWasmBase64 } from "npm:@automerge/automerge/automerge.wasm.base64.js";
import * as automerge from "npm:@automerge/automerge/slim";
await automerge.next.initializeBase64Wasm(automergeWasmBase64);
export default async function() {
const repo = new Repo({ network: [new BrowserWebSocketClientAdapter("wss://sync.automerge.org")] });
const listHandle = repo.find("3NUkYztG95r7cc8uvmyGgW7KkK9j");
const listDoc = await listHandle.doc();
// iterate over all the watchedDocs keys in listDoc
// send a notification to the channel for any document that is different
// from the last time we looked at it
for (const docId of Object.keys(listDoc.watchedDocs)) {
const watchedHandle = repo.find(docId);
const watchedDoc = await watchedHandle.doc();
const heads = watchedHandle.heads();
const {
heads: pastHeads = [],
notifiedAt = Infinity,
} = listDoc.watchedDocs[docId];
// This is our first observation. Just record the current heads.
if (heads.length === 0) {
listHandle.change(doc => {
doc.watchedDocs[watchedHandle.documentId] = {
heads,
notifiedAt: Date.now(),
};
});
continue;
}
// compare heads and pastHeads
const headsSame = pastHeads.every((h, i) => h === heads[i]);
if (headsSame) {
console.log(`[${docId}]: heads are unchanged from ${pastHeads.join(",")}`);
continue;
}
// We don't want to notify if someone is still working on the doc.
/*
const lastEditTime = // ask alexg
// note that this logic relies on the author's system clock
const timeSinceEdit = Date.now() - lastEditTime;
const quietTime = 10000;
const doneEditing = timePassed > longEnough;
// if the heads are the same and it has been more than 10s
const editingFinished = headsSame && timePassed > longEnough;
*/
const editingFinished = true;
if (!editingFinished) {
continue;
}
const diff = automerge.diff(watchedDoc, pastHeads, heads);
console.log("changes", diff);
// Assemble a message for Discord to post.
const title = watchedDoc.title || "Untitled";
const authors = watchedDoc.authors || ["pvh"];
const url = `https://patchwork.inkandswitch.com/#foo--${docId}?type=essay`;
// const content = `${title} has been edited by ${authors.join("&")}.\n${url}`
const content = `${docId} has been edited.`;
const webhookDetails = {
url: Deno.env.get("discordWebhook"),
content,
embeds: [
{
title,
url,
},
{
title: "Diff",
fields: [
{
name: "Raw JSON",
value: "```json\n" + JSON.stringify(diff, null, 2).slice(0, 200) + "\n```",
},
],
},
],
};
console.log(`[${docId}]: sending `, webhookDetails);
await discordWebhook(webhookDetails);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 22, 2024