diff --git a/src/equicordplugins/streaks/index.tsx b/src/equicordplugins/streaks/index.tsx index 4576d5f6a..1aea227e8 100644 --- a/src/equicordplugins/streaks/index.tsx +++ b/src/equicordplugins/streaks/index.tsx @@ -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); + } } }, }, diff --git a/src/equicordplugins/streaks/stores/StreaksStore.ts b/src/equicordplugins/streaks/stores/StreaksStore.ts index ea9916976..119dab752 100644 --- a/src/equicordplugins/streaks/stores/StreaksStore.ts +++ b/src/equicordplugins/streaks/stores/StreaksStore.ts @@ -26,6 +26,7 @@ export interface StreaksState { streaks: Record; fetch: () => Promise; update: (recipientId: string) => Promise; + refresh: (recipientId: string) => Promise; migrate: () => Promise; 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;