Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report the latest feed entry when fetching a feed for the first time #809

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/809.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report the latest feed item when subscribing to a new feed
4 changes: 4 additions & 0 deletions src/Stores/MemoryStorageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class MemoryStorageProvider extends MSP implements IBridgeStorageProvider
super();
}

get isPersistent(): boolean {
return false;
}

async storeFeedGuids(url: string, ...guids: string[]): Promise<void> {
let set = this.feedGuids.get(url);
if (!set) {
Expand Down
4 changes: 4 additions & 0 deletions src/Stores/RedisStorageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class RedisStorageProvider extends RedisStorageContextualProvider impleme
});
}

get isPersistent(): boolean {
return true;
}

public async connect(): Promise<void> {
try {
await this.redis.ping();
Expand Down
1 change: 1 addition & 0 deletions src/Stores/StorageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SerializedGitlabDiscussionThreads } from "../Gitlab/Types";
export const MAX_FEED_ITEMS = 10_000;

export interface IBridgeStorageProvider extends IAppserviceStorageProvider, IStorageProvider, ProvisioningStore {
get isPersistent(): boolean;
connect?(): Promise<void>;
disconnect?(): Promise<void>;
setGithubIssue(repo: string, issueNumber: string, data: IssuesGetResponseData, scope?: string): Promise<void>;
Expand Down
25 changes: 18 additions & 7 deletions src/feeds/FeedReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MessageQueue } from "../MessageQueue";
import axios from "axios";
import Metrics from "../Metrics";
import { randomUUID } from "crypto";
import { readFeed } from "../libRs";
import { FeedItem, readFeed } from "../libRs";
import { IBridgeStorageProvider } from "../Stores/StorageProvider";
import UserAgent from "../UserAgent";

Expand Down Expand Up @@ -204,6 +204,8 @@ export class FeedReader {
// If undefined, we got a not-modified.
log.debug(`Found ${feed.items.length} entries in ${url}`);

const newEntries: FeedEntry[] = [];

for (const item of feed.items) {
// Some feeds have a nasty habit of leading a empty tag there, making us parse it as garbage.
if (!item.hashId) {
Expand All @@ -212,11 +214,7 @@ export class FeedReader {
}
const hashId = `md5:${item.hashId}`;
newGuids.push(hashId);

if (initialSync) {
log.debug(`Skipping entry ${item.id ?? hashId} since we're performing an initial sync`);
continue;
}

if (await this.storage.hasSeenFeedGuid(url, hashId)) {
log.debug('Skipping already seen entry', item.id ?? hashId);
continue;
Expand All @@ -235,9 +233,22 @@ export class FeedReader {
};

log.debug('New entry:', entry);
newEntries.push(entry);
seenEntriesChanged = true;
}

this.queue.push<FeedEntry>({ eventName: 'feed.entry', sender: 'FeedReader', data: entry });
if (initialSync) {
if (this.storage.isPersistent) {
try {
const latest = newEntries.sort((a, b) => new Date(b.pubdate!).getTime() - new Date(a.pubdate!).getTime())[0];
this.queue.push<FeedEntry>({ eventName: 'feed.entry', sender: 'FeedReader', data: latest });
} catch (err: unknown) {
// no pubdates available, or they parse incorrectly. Null sweat
log.debug(`Could not determine the latest entry in ${url} (${err}), won't report anything`);
}
}
} else {
newEntries.forEach(entry => this.queue.push<FeedEntry>({ eventName: 'feed.entry', sender: 'FeedReader', data: entry }));
}

if (seenEntriesChanged) {
Expand Down
Loading