Skip to content

Commit

Permalink
Add command to list feeds in JSON and YAML format (#876)
Browse files Browse the repository at this point in the history
* Add command to list feeds in YAML format

* Add line breaks

* Add changelog

* Add a period in the help

* Add $URL to templates and add feed list format docs

* Use an optional arg on yaml list instead of a separate function

* Update changelog to include JSON format
  • Loading branch information
Twi1ightSparkle authored Jan 4, 2024
1 parent 0175f9e commit fde2db2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog.d/876.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add command to list feeds in JSON and YAML format to easily export all feeds from a room.
33 changes: 17 additions & 16 deletions docs/setup/feeds.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Each feed will only be checked once, regardless of the number of rooms to which

No entries will be bridged upon the “initial sync” -- all entries that exist at the moment of setup will be considered to be already seen.

Please note that Hookshot **must** be configued with Redis to retain seen entries between restarts. By default, Hookshot will
Please note that Hookshot **must** be configured with Redis to retain seen entries between restarts. By default, Hookshot will
run an "initial sync" on each startup and will not process any entries from feeds from before the first sync.

## Usage
Expand All @@ -28,34 +28,35 @@ run an "initial sync" on each startup and will not process any entries from feed

To add a feed to your room:

- Invite the bot user to the room.
- Make sure the bot able to send state events (usually the Moderator power level in clients)
- Say `!hookshot feed <URL>` where `<URL>` links to an RSS/Atom feed you want to subscribe to.
- Invite the bot user to the room.
- Make sure the bot able to send state events (usually the Moderator power level in clients)
- Say `!hookshot feed <URL>` where `<URL>` links to an RSS/Atom feed you want to subscribe to.

### Listing feeds

You can list all feeds that a room you're in is currently subscribed to with `!hookshot feed list`.
It requires no special permissions from the user issuing the command.
It requires no special permissions from the user issuing the command. Optionally you can format the list as `json` or
`yaml` with `!hookshot feed list <format>`.

### Removing feeds

To remove a feed from a room, say `!hookshot feed remove <URL>`, with the URL specifying which feed you want to unsubscribe from.


### Feed templates

You can optionally give a feed a specific template to use when sending a message into a room. A template
may include any of the following tokens:

|Token |Description |
|----------|--------------------------------------------|
|$FEEDNAME | Either the label, title or url of the feed.|
|$FEEDURL | The URL of the feed. |
|$FEEDTITLE| The title of the feed. |
|$TITLE | The title of the feed entry. |
|$LINK | The link of the feed entry. |
|$AUTHOR | The author of the feed entry. |
|$DATE | The publish date (`pubDate`) of the entry. |
|$SUMMARY | The summary of the entry. |
| Token | Description |
| ---------- | ---------------------------------------------------------- |
| $FEEDNAME | Either the label, title or url of the feed. |
| $FEEDURL | The URL of the feed. |
| $FEEDTITLE | The title of the feed. |
| $TITLE | The title of the feed entry. |
| $URL | The URL of the feed entry. |
| $LINK | The link of the feed entry. Formatted as `[$TITLE]($URL)`. |
| $AUTHOR | The author of the feed entry. |
| $DATE | The publish date (`pubDate`) of the entry. |
| $SUMMARY | The summary of the entry. |

If not specified, the default template is `New post in $FEEDNAME: $LINK`.
25 changes: 19 additions & 6 deletions src/Connections/SetupConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GitLabRepoConnection } from "./GitlabRepo";
import { IConnection, IConnectionState, ProvisionConnectionOpts } from "./IConnection";
import { ApiError, Logger } from "matrix-appservice-bridge";
import { Intent } from "matrix-bot-sdk";
import YAML from 'yaml';
const md = new markdown();
const log = new Logger("SetupConnection");

Expand Down Expand Up @@ -326,8 +327,11 @@ export class SetupConnection extends CommandConnection {
return this.client.sendHtmlNotice(this.roomId, md.renderInline(`Room configured to bridge \`${url}\``));
}

@botCommand("feed list", { help: "Show feeds currently subscribed to.", category: "feeds"})
public async onFeedList() {
@botCommand("feed list", { help: "Show feeds currently subscribed to. Supported formats `json` and `yaml`.", optionalArgs: ["format"], category: "feeds"})
public async onFeedList(format?: string) {
const useJsonFormat = format?.toLowerCase() === 'json';
const useYamlFormat = format?.toLowerCase() === 'yaml';

const feeds: FeedConnectionState[] = await this.client.getRoomState(this.roomId).catch((err: any) => {
if (err.body.errcode === 'M_NOT_FOUND') {
return []; // not an error to us
Expand All @@ -345,16 +349,25 @@ export class SetupConnection extends CommandConnection {
const feedDescriptions = feeds.sort(
(a, b) => (a.label ?? a.url).localeCompare(b.label ?? b.url)
).map(feed => {
if (useJsonFormat || useYamlFormat) {
return feed;
}
if (feed.label) {
return `[${feed.label}](${feed.url})`;
}
return feed.url;
});

return this.client.sendHtmlNotice(this.roomId, md.render(
'Currently subscribed to these feeds:\n\n' +
feedDescriptions.map(desc => ` - ${desc}`).join('\n')
));
let message = 'Currently subscribed to these feeds:\n';
if (useJsonFormat) {
message += `\`\`\`json\n${JSON.stringify(feedDescriptions, null, 4)}\n\`\`\``
} else if (useYamlFormat) {
message += `\`\`\`yaml\n${YAML.stringify(feedDescriptions)}\`\`\``
} else {
message += feedDescriptions.map(desc => `- ${desc}`).join('\n')
}

return this.client.sendHtmlNotice(this.roomId, md.render(message));
}
}

Expand Down

0 comments on commit fde2db2

Please sign in to comment.