From 3cdc08d69df9eabb73a1bafbd506cea38d8bcf9b Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 16 Aug 2022 21:50:54 -0400 Subject: [PATCH 1/5] Add guildId to rssFeeds collection schema --- .../1650237364807-add-guildID-to-rssFeeds.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 migrations/1650237364807-add-guildID-to-rssFeeds.js diff --git a/migrations/1650237364807-add-guildID-to-rssFeeds.js b/migrations/1650237364807-add-guildID-to-rssFeeds.js new file mode 100644 index 0000000..19369e2 --- /dev/null +++ b/migrations/1650237364807-add-guildID-to-rssFeeds.js @@ -0,0 +1,67 @@ +const {MongoClient} = require('mongodb'); +const config = require('../config'); + +module.exports.up = async () => { + const mongoClient = new MongoClient(config.mongodb.url, {useUnifiedTopology: true}); + await mongoClient.connect(); + const db = mongoClient.db(config.mongodb.databaseName); + + // Update validator to add guildID property + await db.command({ + collMod: 'rssFeeds', + validator: { + $jsonSchema: { + bsonType: 'object', + required: ['name', 'url', 'guildID', 'channelId', 'lastCheck'], + properties: { + name: { + bsonType: 'string', + }, + url: { + bsonType: 'string', + }, + guildID: { + bsonType: 'string', + }, + channelId: { + bsonType: 'string', + }, + lastCheck: { + bsonType: 'date', + }, + }, + }, + }, + }); +}; + +module.exports.down = async () => { + const mongoClient = new MongoClient(config.mongodb.url, {useUnifiedTopology: true}); + await mongoClient.connect(); + const db = mongoClient.db(config.mongodb.databaseName); + + // Drop guildID from validator + await db.command({ + collMod: 'rssFeeds', + validator: { + $jsonSchema: { + bsonType: 'object', + required: ['name', 'url', 'channelId', 'lastCheck'], + properties: { + name: { + bsonType: 'string', + }, + url: { + bsonType: 'string', + }, + channelId: { + bsonType: 'string', + }, + lastCheck: { + bsonType: 'date', + }, + }, + }, + }, + }); +}; From 53ea6390c3a899fbd0856fa04d7243ed05102a76 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 16 Aug 2022 21:51:07 -0400 Subject: [PATCH 2/5] Write guild ID for newly created RSS feeds --- src/bot/commands/rss_feeds/createrssfeed.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bot/commands/rss_feeds/createrssfeed.js b/src/bot/commands/rss_feeds/createrssfeed.js index 420852d..379e555 100644 --- a/src/bot/commands/rss_feeds/createrssfeed.js +++ b/src/bot/commands/rss_feeds/createrssfeed.js @@ -14,8 +14,9 @@ const command = new Command('createrssfeed', async (message, args, context) => { // try to get the channel // NOTE: Although it reminds the user to make sure the channel is visible, it would still be able to find a channel regardless of its visiblity (but not read its contents) + let channel; try { - context.client.getChannel(channelId) || await context.client.getRESTChannel(channelId); + channel = context.client.getChannel(channelId) || await context.client.getRESTChannel(channelId); } catch (error) { message.channel.createMessage('Couldn\'t find the channel with the specified ID. Please make sure it is visible to me and the ID is correct.').catch(() => {}); log.error(`Failed to locate channel ${escape(channelId)}:`, error); @@ -35,6 +36,7 @@ const command = new Command('createrssfeed', async (message, args, context) => { name: rssFeedName, url: rssFeedUrl, channelId, + guildId: channel.guild.id || null, lastCheck: new Date(), }; From e2cf740bf645d86b145fe5919e07ea6705391238 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 16 Aug 2022 22:45:33 -0400 Subject: [PATCH 3/5] Filter by guild/DM channel when listing feeds --- src/bot/commands/rss_feeds/rssfeeds.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bot/commands/rss_feeds/rssfeeds.js b/src/bot/commands/rss_feeds/rssfeeds.js index bc2da2f..31b9c2f 100644 --- a/src/bot/commands/rss_feeds/rssfeeds.js +++ b/src/bot/commands/rss_feeds/rssfeeds.js @@ -3,12 +3,19 @@ import {escape} from '../../util/formatting'; const command = new Command('rssfeeds', async (message, args, {client, db}) => { const collection = db.collection('rssFeeds'); - let str = ''; - const feeds = await collection.find().toArray(); - if (feeds.length === 0) { + + const feeds = await collection.find({ + guildId: message.guildID, + // in DMs, filtering by guild isn't enough; filter to the specific DM + channelId: message.guildId ? undefined : message.channel.id, + }).toArray(); + + if (!feeds.length) { message.channel.createMessage('No feeds found.').catch(() => { }); return; } + + let str = ''; for (const feed of feeds) { const channelName = client.getChannel(feed.channelId).name; str += `Feed: **${escape(feed.name)}**\nChannel: **${escape(channelName)}**\nURL: <${feed.url}> \n \n`; From f7aa2b164e52eb7ce81bb27893de7462e452a04b Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 16 Aug 2022 22:47:01 -0400 Subject: [PATCH 4/5] Also filter by guild/DM channel when deleting --- src/bot/commands/rss_feeds/deleterssfeed.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/bot/commands/rss_feeds/deleterssfeed.js b/src/bot/commands/rss_feeds/deleterssfeed.js index 9f80029..144189e 100644 --- a/src/bot/commands/rss_feeds/deleterssfeed.js +++ b/src/bot/commands/rss_feeds/deleterssfeed.js @@ -11,7 +11,12 @@ const command = new Command('deleterssfeed', async (message, args, context) => { const {db} = context; const collection = db.collection('rssFeeds'); const rssFeedName = args[0]; - await collection.deleteOne({name: rssFeedName}) + await collection.deleteOne({ + guildId: message.guildID, + // in DMs, filtering by guild isn't enough; filter to the specific DM + channelId: message.guildID ? undefined : message.channel.id, + name: rssFeedName, + }) .then(result => { if (result.deletedCount === 0) { message.channel.createMessage('Could not find anything to delete. Did you spell the name of the feed correctly?').catch(() => {}); From 182abc0a4b3f6f0f38d654c0227a03f58ce5252f Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 28 Oct 2022 18:37:03 -0400 Subject: [PATCH 5/5] Fix capitalization of guildID field in migration --- migrations/1650237364807-add-guildID-to-rssFeeds.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/migrations/1650237364807-add-guildID-to-rssFeeds.js b/migrations/1650237364807-add-guildID-to-rssFeeds.js index 19369e2..5ce0a6f 100644 --- a/migrations/1650237364807-add-guildID-to-rssFeeds.js +++ b/migrations/1650237364807-add-guildID-to-rssFeeds.js @@ -6,13 +6,13 @@ module.exports.up = async () => { await mongoClient.connect(); const db = mongoClient.db(config.mongodb.databaseName); - // Update validator to add guildID property + // Update validator to add guildId property await db.command({ collMod: 'rssFeeds', validator: { $jsonSchema: { bsonType: 'object', - required: ['name', 'url', 'guildID', 'channelId', 'lastCheck'], + required: ['name', 'url', 'guildId', 'channelId', 'lastCheck'], properties: { name: { bsonType: 'string', @@ -20,7 +20,7 @@ module.exports.up = async () => { url: { bsonType: 'string', }, - guildID: { + guildId: { bsonType: 'string', }, channelId: {