Streaks: dont spam check

This commit is contained in:
thororen1234 2026-05-10 19:53:59 -04:00
parent d3a22be94a
commit dc4b8f287f
No known key found for this signature in database
2 changed files with 34 additions and 2 deletions

View file

@ -91,8 +91,23 @@ export default definePlugin({
if (!recipientId) return;
const me = UserStore.getCurrentUser()?.id;
if ((message.author.id === me || message.author.id === recipientId) && useAuthorizationStore.getState().isAuthorized()) {
useStreaksStore.getState().update(recipientId);
if (!useAuthorizationStore.getState().isAuthorized()) return;
const today = moment().format("YYYY-MM-DD");
const cached = useStreaksStore.getState().streaks[recipientId];
const myFlag = cached && cached.today_date === today && (cached.user_a_id === me ? cached.user_a_today : cached.user_b_today);
const theirFlag = cached && cached.today_date === today && (cached.user_a_id === me ? cached.user_b_today : cached.user_a_today);
if (message.author.id === me) {
if (!myFlag) {
useStreaksStore.getState().update(recipientId);
}
} else if (message.author.id === recipientId) {
if (!theirFlag) {
setTimeout(() => {
useStreaksStore.getState().refresh(recipientId);
}, 1000);
}
}
},
},

View file

@ -26,6 +26,7 @@ export interface StreaksState {
streaks: Record<string, RemoteStreak>;
fetch: () => Promise<void>;
update: (recipientId: string) => Promise<void>;
refresh: (recipientId: string) => Promise<void>;
migrate: () => Promise<void>;
clear: () => void;
}
@ -72,6 +73,22 @@ export const useStreaksStore = proxyLazy(() => zustandCreate((set: any, get: any
console.error("Failed to update streak", e);
}
},
async refresh(recipientId: string) {
const { token } = useAuthorizationStore.getState();
if (!token) return;
try {
const res = await fetch(`${API_URL}/streaks/${recipientId}`, {
headers: { Authorization: `Bearer ${token}` }
});
if (res.ok) {
const streak: RemoteStreak = await res.json();
set({ streaks: { ...get().streaks, [recipientId]: streak } });
}
} catch (e) {
console.error("Failed to refresh streak", e);
}
},
async migrate() {
const { token } = useAuthorizationStore.getState();
if (!token) return;