Fix message format for feed items with no title or link (#737)

* Allow empty title on feed item

* Allow bare URL

* Add tests for new format.

* changelog
This commit is contained in:
Will Hunt 2023-05-04 09:34:25 +01:00 committed by GitHub
parent 2110e88210
commit 7223a86b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

1
changelog.d/737.bugfix Normal file
View File

@ -0,0 +1 @@
Fix feed message format when the item does not contain a title or link.

View File

@ -41,6 +41,7 @@ const MAX_TEMPLATE_LENGTH = 1024;
const DEFAULT_TEMPLATE = "New post in $FEEDNAME"; const DEFAULT_TEMPLATE = "New post in $FEEDNAME";
const DEFAULT_TEMPLATE_WITH_CONTENT = "New post in $FEEDNAME: $LINK" const DEFAULT_TEMPLATE_WITH_CONTENT = "New post in $FEEDNAME: $LINK"
const DEFAULT_TEMPLATE_WITH_ONLY_TITLE = "New post in $FEEDNAME: $TITLE"
@Connection @Connection
export class FeedConnection extends BaseConnection implements IConnection { export class FeedConnection extends BaseConnection implements IConnection {
@ -145,6 +146,8 @@ export class FeedConnection extends BaseConnection implements IConnection {
return entry.title || ""; return entry.title || "";
case "$LINK": case "$LINK":
return entry.link ? `[${entry.title ?? entry.link}](${entry.link})` : ""; return entry.link ? `[${entry.title ?? entry.link}](${entry.link})` : "";
case "$URL":
return entry.link || "";
case "$AUTHOR": case "$AUTHOR":
return entry.author || ""; return entry.author || "";
case "$DATE": case "$DATE":
@ -183,8 +186,10 @@ export class FeedConnection extends BaseConnection implements IConnection {
let message; let message;
if (this.state.template) { if (this.state.template) {
message = this.templateFeedEntry(this.state.template, entry); message = this.templateFeedEntry(this.state.template, entry);
} else if (entry.title && entry.link) { } else if (entry.link) {
message = this.templateFeedEntry(DEFAULT_TEMPLATE_WITH_CONTENT, entry); message = this.templateFeedEntry(DEFAULT_TEMPLATE_WITH_CONTENT, entry);
} else if (entry.title) {
message = this.templateFeedEntry(DEFAULT_TEMPLATE_WITH_ONLY_TITLE, entry);
} else { } else {
message = this.templateFeedEntry(DEFAULT_TEMPLATE, entry); message = this.templateFeedEntry(DEFAULT_TEMPLATE, entry);
} }

View File

@ -54,6 +54,32 @@ describe("FeedConnection", () => {
expect(matrixEvt.content.external_url).to.be.undefined; expect(matrixEvt.content.external_url).to.be.undefined;
expect(matrixEvt.content.body).to.equal("New post in Test feed"); expect(matrixEvt.content.body).to.equal("New post in Test feed");
}); });
it("will handle simple feed message with a missing title ", async () => {
const [connection, intent] = createFeed({
url: FEED_URL,
});
await connection.handleFeedEntry({
...FEED_ENTRY_DEFAULTS,
title: null,
});
const matrixEvt =intent.sentEvents[0];
expect(matrixEvt).to.not.be.undefined;
expect(matrixEvt.roomId).to.equal(ROOM_ID);
expect(matrixEvt.content.body).to.equal("New post in Test feed: [foo/bar](foo/bar)");
});
it("will handle simple feed message with a missing link ", async () => {
const [connection, intent] = createFeed({
url: FEED_URL,
});
await connection.handleFeedEntry({
...FEED_ENTRY_DEFAULTS,
link: null,
});
const matrixEvt =intent.sentEvents[0];
expect(matrixEvt).to.not.be.undefined;
expect(matrixEvt.roomId).to.equal(ROOM_ID);
expect(matrixEvt.content.body).to.equal("New post in Test feed: Foo");
});
it("will handle simple feed message with all the template options possible ", async () => { it("will handle simple feed message with all the template options possible ", async () => {
const [connection, intent] = createFeed({ const [connection, intent] = createFeed({
url: FEED_URL, url: FEED_URL,