Compare commits
1 Commits
709ef9885b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 02354b3a7c |
@@ -10,21 +10,15 @@ try {
|
|||||||
async function fetchMatrix(url, options = {}) {
|
async function fetchMatrix(url, options = {}) {
|
||||||
options.headers ||= {};
|
options.headers ||= {};
|
||||||
options.headers.Authorization ||= `Bearer ${ACCESS_TOKEN}`;
|
options.headers.Authorization ||= `Bearer ${ACCESS_TOKEN}`;
|
||||||
url = SERVER_URL + url;
|
if (!(url instanceof URL)) url = new URL(url, SERVER_URL);
|
||||||
do {
|
console.debug("fetch", url.toString());
|
||||||
console.debug("fetch", url);
|
var res = await fetch(url, options);
|
||||||
try {
|
console.debug("status:", res.status);
|
||||||
var res = await fetch(url, options);
|
if (res.status == 429) {
|
||||||
if (res.ok) break;
|
var {retry_after_ms} = await res.json();
|
||||||
console.warn("status:", res.status);
|
|
||||||
if (res.status == 429) {
|
|
||||||
var {retry_after_ms} = await res.json();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
await new Promise(r => setTimeout(r, retry_after_ms || 60000));
|
await new Promise(r => setTimeout(r, retry_after_ms || 60000));
|
||||||
} while (true)
|
return await fetchMatrix(url, options);
|
||||||
|
}
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status} ${await res.text()}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status} ${await res.text()}`);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -44,7 +38,7 @@ async function checkNotifications() {
|
|||||||
limit = 50;
|
limit = 50;
|
||||||
} while (next_token !== null)
|
} while (next_token !== null)
|
||||||
|
|
||||||
console.debug("new notifications:", new_notifications);
|
if (new_notifications.length) console.debug("new notifications:", new_notifications);
|
||||||
if (!new_notifications.length) return;
|
if (!new_notifications.length) return;
|
||||||
for (let n of new_notifications) {
|
for (let n of new_notifications) {
|
||||||
try {
|
try {
|
||||||
@@ -58,8 +52,13 @@ async function checkNotifications() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function addNotificationToRoom(notification) {
|
async function addNotificationToRoom({room_id, event}) {
|
||||||
var text = `https://matrix.to/#/${encodeURIComponent(notification.room_id)}/${encodeURIComponent(notification.event.event_id)} ${new Date(notification.ts).toLocaleString()}`;
|
var url = `https://matrix.to/#/${encodeURIComponent(room_id)}/${encodeURIComponent(event.event_id)}`;
|
||||||
|
var text = `${new Date(event.origin_server_ts).toLocaleString()}
|
||||||
|
${await getAlias(room_id)}
|
||||||
|
${event.sender}
|
||||||
|
${event.content?.body}
|
||||||
|
${url}`;
|
||||||
await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(ROOM_ID)}/send/m.room.message/${Math.random()}`, {
|
await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(ROOM_ID)}/send/m.room.message/${Math.random()}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -67,13 +66,24 @@ async function addNotificationToRoom(notification) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
msgtype: "m.text",
|
msgtype: "m.text",
|
||||||
format: "org.matrix.custom.html",
|
body: text
|
||||||
body: text,
|
//format: "org.matrix.custom.html",
|
||||||
formatted_body: `<p>${text}</p>\n<pre><code class=\"language-json\">${JSON.stringify(notification.event, null, '\t')}\n</code></pre>\n`
|
//formatted_body: `<p>${text}</p>\n<pre><code class=\"language-json\">${JSON.stringify(event, null, '\t')}\n</code></pre>\n`
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getAlias(room_id) {
|
||||||
|
try {
|
||||||
|
var data = await fetchMatrix(`/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.canonical_alias/`).then(res => res.json());
|
||||||
|
console.debug(data);
|
||||||
|
return data.alias || room_id;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return room_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user