From f013eb1c98e3d32324a331c1d410a373f5215629 Mon Sep 17 00:00:00 2001 From: vulet Date: Sun, 28 Jun 2020 15:07:58 +0800 Subject: [PATCH] chore(config): tidy up --- commands/beg.js | 4 ++-- commands/boo.js | 4 ++-- commands/clap.js | 4 ++-- commands/copy.js | 4 ++-- commands/flood.js | 10 +++++----- commands/fren.js | 8 ++++---- commands/help.js | 10 ++++++++-- commands/mordy.js | 4 ++-- commands/notify.js | 12 ++++++------ commands/pin.js | 6 +++--- commands/plemara.js | 4 ++-- commands/redact.js | 4 ++-- commands/reply.js | 4 ++-- commands/tip.js | 4 ++-- commands/unfren.js | 8 ++++---- commands/unpin.js | 6 +++--- config.js | 19 +++++++++++-------- main.js | 23 ++++++++++++++--------- 18 files changed, 76 insertions(+), 62 deletions(-) diff --git a/commands/beg.js b/commands/beg.js index 0ddc0e2..4b3d37c 100644 --- a/commands/beg.js +++ b/commands/beg.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, data: { status: `@10grans@fedi.cc beg` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/boo.js b/commands/boo.js index 0801ac2..16c7c77 100644 --- a/commands/boo.js +++ b/commands/boo.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}/unfavourite`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unfavourite`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', diff --git a/commands/clap.js b/commands/clap.js index ea47753..8870d8a 100644 --- a/commands/clap.js +++ b/commands/clap.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}/favourite`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/favourite`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', diff --git a/commands/copy.js b/commands/copy.js index 1768cb6..6aa6782 100644 --- a/commands/copy.js +++ b/commands/copy.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}/reblog`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/reblog`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', diff --git a/commands/flood.js b/commands/flood.js index d682713..ee33a4d 100644 --- a/commands/flood.js +++ b/commands/flood.js @@ -5,8 +5,8 @@ exports.runQuery = function (matrixClient, room, registrar) { setInterval(() => { axios({ method: 'GET', - url: `${registrar.config.fediverse}/api/v1/timelines/home`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/timelines/home`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((events) => { const event = fs.readFileSync('timeline.json', 'utf8'); fs.writeFileSync('timeline.json', events.data[0].created_at, 'utf8'); @@ -14,7 +14,7 @@ exports.runQuery = function (matrixClient, room, registrar) { if (events.data[0].reblog === null) { matrixClient.sendHtmlNotice(room.roomId, '', - `${events.data[0].account.acct} + `${events.data[0].account.acct}
${events.data[0].content}
${events.data[0].media_attachments.map(media => ``+`${media.description}`+'' @@ -24,9 +24,9 @@ exports.runQuery = function (matrixClient, room, registrar) { } else { matrixClient.sendHtmlNotice(room.roomId, '', - ` + ` ${events.data[0].account.acct} - has repeated: + has repeated:
${events.data[0].reblog.account.acct}
${events.data[0].content}
${events.data[0].media_attachments.map(media => diff --git a/commands/fren.js b/commands/fren.js index 62ed399..1a1e57e 100644 --- a/commands/fren.js +++ b/commands/fren.js @@ -1,17 +1,17 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { - axios.get(`${registrar.config.fediverse}/api/v1/accounts/${userInput}`).then((findUID) => { + axios.get(`${registrar.config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/accounts/${findUID.data.id}/follow`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/follow`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }) .then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', `Subscribed: -
${registrar.config.fediverse}/${response.data.id}`); +
${registrar.config.fediverse.domain}/${response.data.id}`); }); }).catch((e) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/help.js b/commands/help.js index b9a2691..db86db0 100644 --- a/commands/help.js +++ b/commands/help.js @@ -12,7 +12,13 @@ exports.runQuery = function (matrixClient, room) { + '+clap [post id] : favorite
' + '+boo [post id] : unfavorite
' + '
channel commands
' - + '+flood : turn on timeline
' - + '+notify : show notifications
' + + '+flood : turn on timeline in channel
' + + '+notify : show notifications in channel
' + + '+archive [URL] : archive content
' + + '+rearchive [URL] : re-archive content
' + + '+nitter [status URL] : redirect twitter to nitter, also embed tweet
' + + '+invidious [video URL] : redirect youtube to invidious, also embed description
' + + '
--- contributors🐱 ---' + + '--- cryptomooners ---
' + '
--- docs by lint ---
'); }; diff --git a/commands/mordy.js b/commands/mordy.js index be0c2bd..727b06d 100644 --- a/commands/mordy.js +++ b/commands/mordy.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, data: { status: `@mordekai ${userInput}`, content_type: `text/markdown`, diff --git a/commands/notify.js b/commands/notify.js index 6007964..f9e7fd5 100644 --- a/commands/notify.js +++ b/commands/notify.js @@ -5,8 +5,8 @@ exports.runQuery = function (matrixClient, room, registrar) { setInterval(() => { axios({ method: 'GET', - url: `${registrar.config.fediverse}/api/v1/notifications`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/notifications`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((notifications) => { const event = fs.readFileSync('notification.json', 'utf8'); fs.writeFileSync('notification.json', notifications.data[0].created_at, 'utf8'); @@ -15,14 +15,14 @@ exports.runQuery = function (matrixClient, room, registrar) { if (notifications.data[0].type === 'follow') { matrixClient.sendHtmlNotice(room.roomId, '', - ` + ` ${notifications.data[0].account.acct} has followed you.
${notifications.data[0].account.note}`); } else if (notifications.data[0].type === 'favourite') { matrixClient.sendHtmlNotice(room.roomId, '', - ` + ` ${notifications.data[0].account.acct} has favorited your post: @@ -30,7 +30,7 @@ exports.runQuery = function (matrixClient, room, registrar) { } else if (notifications.data[0].type === 'mention') { matrixClient.sendHtmlNotice(room.roomId, '', - ` + ` ${notifications.data[0].account.acct} has mentioned you:
${notifications.data[0].status.content} @@ -39,7 +39,7 @@ exports.runQuery = function (matrixClient, room, registrar) { } else if (notifications.data[0].type === 'reblog') { matrixClient.sendHtmlNotice(room.roomId, '', - ` + ` ${notifications.data[0].account.acct} has repeated your post:
diff --git a/commands/pin.js b/commands/pin.js index 7a1e632..074bb03 100644 --- a/commands/pin.js +++ b/commands/pin.js @@ -3,13 +3,13 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}/pin`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/pin`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', `Pinned: -
+
${response.data.content}
`); }) diff --git a/commands/plemara.js b/commands/plemara.js index 550e7e4..342f81b 100644 --- a/commands/plemara.js +++ b/commands/plemara.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, data: { status: userInput, content_type: `text/markdown` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/redact.js b/commands/redact.js index 52f6b35..8debe4a 100644 --- a/commands/redact.js +++ b/commands/redact.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'DELETE', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', diff --git a/commands/reply.js b/commands/reply.js index 3f40162..76fe5d4 100644 --- a/commands/reply.js +++ b/commands/reply.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, address, flaggedInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, data: { status: flaggedInput, in_reply_to_id: address, content_type: `text/markdown` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/tip.js b/commands/tip.js index 44cb095..478b998 100644 --- a/commands/tip.js +++ b/commands/tip.js @@ -3,8 +3,8 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, address, flaggedInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, data: { status: `@10grans@fedi.cc tip `+ flaggedInput + ` to `+address }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/unfren.js b/commands/unfren.js index c18a671..1d9d124 100644 --- a/commands/unfren.js +++ b/commands/unfren.js @@ -1,17 +1,17 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { - axios.get(`${registrar.config.fediverse}/api/v1/accounts/${userInput}`).then((findUID) => { + axios.get(`${registrar.config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/accounts/${findUID.data.id}/unfollow`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/unfollow`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }) .then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', `Unsubscribed: -
${registrar.config.fediverse}/${response.data.id}`); +
${registrar.config.fediverse.domain}/${response.data.id}`); }); }).catch((e) => { matrixClient.sendHtmlNotice(room.roomId, diff --git a/commands/unpin.js b/commands/unpin.js index 2871f2e..3ffe2cf 100644 --- a/commands/unpin.js +++ b/commands/unpin.js @@ -3,13 +3,13 @@ const axios = require('axios'); exports.runQuery = function (matrixClient, room, userInput, registrar) { axios({ method: 'POST', - url: `${registrar.config.fediverse}/api/v1/statuses/${userInput}/unpin`, - headers: { Authorization: `Bearer ${registrar.config.fediverseToken}` }, + url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unpin`, + headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` }, }).then((response) => { matrixClient.sendHtmlNotice(room.roomId, '', `Unpinned: -
+
${response.data.content}
`); }) diff --git a/config.js b/config.js index af08d07..077cb1b 100644 --- a/config.js +++ b/config.js @@ -1,10 +1,13 @@ module.exports = { - matrixServer: 'https://server.com', - userId: '@matrixUser:server.com', - matrixUser: 'hello', - matrixPass: 'password', - fediverse: 'https://server.com', - fediverseToken: 'access_token', + matrix: { + domain: 'https://your_homeserver.com', + user: 'your_user', + password: 'your_password', + }, + fediverse: { + domain: 'https://your_federation.com', + token: 'your_federation_token', + }, archive: { domain: 'archive.is', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)' @@ -12,11 +15,11 @@ module.exports = { nitter: { domain: 'nitter.net', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', - domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com' ] + domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'wwww.twitter.com' ] }, invidious: { domain: 'invidio.us', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', - domains: [ 'invidio.us', 'www.invidio.us', 'youtube.com', 'www.youtube.com'] + domains: [ 'invidio.us', 'www.invidio.us', 'youtube.com', 'www.youtube.com' ] } }; diff --git a/main.js b/main.js index 35bb0a1..5da9ab7 100644 --- a/main.js +++ b/main.js @@ -4,36 +4,41 @@ const registrar = require('./registrar.js'); const auth = { type: 'm.login.password', - user: registrar.config.matrixUser, - password: registrar.config.matrixPass, + user: registrar.config.matrix.user, + password: registrar.config.matrix.password, }; -axios.post(`${registrar.config.matrixServer}/_matrix/client/r0/login`, auth).then((response) => { - CreateClient(response.data.access_token); +axios.post(`${registrar.config.matrix.domain}/_matrix/client/r0/login`, auth).then((response) => { + CreateClient(response.data.access_token, response.data.user_id); }).catch((e) => { console.log(e); }); -let CreateClient = (token) => { +let CreateClient = (token, user_id) => { const matrixClient = sdk.createClient({ - baseUrl: registrar.config.matrixServer, + baseUrl: registrar.config.matrix.domain, accessToken: token, - userId: registrar.config.userId, + userId: user_id, timelineSupport: true, }); matrixClient.on('RoomMember.membership', (event, member) => { - if (member.membership === 'invite' && member.userId === registrar.config.userId) { + if (member.membership === 'invite' && member.userId === matrixClient.credentials.userId) { matrixClient.joinRoom(member.roomId).done(() => { console.log('Auto-joined %s', member.roomId); }); } + if (member.membership === 'leave' && member.userId === matrixClient.credentials.userId) { + matrixClient.forget(member.roomId).then(() => { + console.log('Kicked %s', member.roomId); + }); + } }); matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => { if (toStartOfTimeline) return; if (event.getType() !== 'm.room.message') return; - if (event.getSender() === registrar.config.userId) return; + if (event.getSender() === matrixClient.credentials.userId) return; if (event.event.unsigned.age > 10000) return; if (event.event.content.body.charAt(0) === '+') { console.log(`Logs: ${event.event.sender} - ${event.event.content.body}`);