Merge branch 'streak-window' into 'main'

Make STREAK_WINDOW configurable

See merge request soapbox-pub/ditto!640
This commit is contained in:
Alex Gleason 2025-02-07 17:55:02 +00:00
commit 7860947552
4 changed files with 13 additions and 5 deletions

View file

@ -1,7 +1,9 @@
import { Conf } from '@/config.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
const kysely = await Storages.kysely(); const kysely = await Storages.kysely();
const statsQuery = kysely.selectFrom('author_stats').select('pubkey'); const statsQuery = kysely.selectFrom('author_stats').select('pubkey');
const { streakWindow } = Conf;
for await (const { pubkey } of statsQuery.stream(10)) { for await (const { pubkey } of statsQuery.stream(10)) {
const eventsQuery = kysely const eventsQuery = kysely
@ -21,14 +23,14 @@ for await (const { pubkey } of statsQuery.stream(10)) {
if (!end) { if (!end) {
const now = Math.floor(Date.now() / 1000); const now = Math.floor(Date.now() / 1000);
if (now - createdAt > 86400) { if (now - createdAt > streakWindow) {
break; // streak broken break; // streak broken
} }
end = createdAt; end = createdAt;
} }
if (start && (start - createdAt > 86400)) { if (start && (start - createdAt > streakWindow)) {
break; // streak broken break; // streak broken
} }

View file

@ -357,6 +357,10 @@ class Conf {
return Number(Deno.env.get('PROFILE_FIELDS_VALUE_LENGTH') || 2047); return Number(Deno.env.get('PROFILE_FIELDS_VALUE_LENGTH') || 2047);
}, },
}; };
/** Maximum time between events before a streak is broken, *in seconds*. */
static get streakWindow(): number {
return Number(Deno.env.get('STREAK_WINDOW') || 129600);
}
} }
const optionalBooleanSchema = z const optionalBooleanSchema = z

View file

@ -3,6 +3,7 @@ import { Insertable, Kysely, UpdateObject } from 'kysely';
import { SetRequired } from 'type-fest'; import { SetRequired } from 'type-fest';
import { z } from 'zod'; import { z } from 'zod';
import { Conf } from '@/config.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { findQuoteTag, findReplyTag, getTagSet } from '@/utils/tags.ts'; import { findQuoteTag, findReplyTag, getTagSet } from '@/utils/tags.ts';
@ -46,7 +47,7 @@ async function handleEvent1(kysely: Kysely<DittoTables>, event: NostrEvent, x: n
if (start && end) { // Streak exists. if (start && end) { // Streak exists.
if (now <= end) { if (now <= end) {
// Streak cannot go backwards in time. Skip it. // Streak cannot go backwards in time. Skip it.
} else if (now - end > 86400) { } else if (now - end > Conf.streakWindow) {
// Streak is broken. Start a new streak. // Streak is broken. Start a new streak.
start = now; start = now;
end = now; end = now;

View file

@ -72,15 +72,16 @@ async function renderAccount(
let streakDays = 0; let streakDays = 0;
let streakStart = event.author_stats?.streak_start ?? null; let streakStart = event.author_stats?.streak_start ?? null;
let streakEnd = event.author_stats?.streak_end ?? null; let streakEnd = event.author_stats?.streak_end ?? null;
const { streakWindow } = Conf;
if (streakStart && streakEnd) { if (streakStart && streakEnd) {
const broken = nostrNow() - streakEnd > 86400; const broken = nostrNow() - streakEnd > streakWindow;
if (broken) { if (broken) {
streakStart = null; streakStart = null;
streakEnd = null; streakEnd = null;
} else { } else {
const delta = streakEnd - streakStart; const delta = streakEnd - streakStart;
streakDays = Math.max(Math.ceil(delta / 86400), 1); streakDays = Math.max(Math.ceil(delta / streakWindow), 1);
} }
} }