|
1 | 1 | import fs from "node:fs/promises"; |
2 | 2 | import path from "node:path"; |
3 | 3 | import { readJsonFileWithFallback, writeJsonFileAtomically } from "openclaw/plugin-sdk/json-store"; |
| 4 | +import { timestampMsToIsoString } from "openclaw/plugin-sdk/number-runtime"; |
4 | 5 | import type { MatrixConfig } from "../../types.js"; |
5 | 6 | import { resolveMatrixStoragePaths } from "../client/storage.js"; |
6 | 7 | import type { MatrixAuth } from "../client/types.js"; |
@@ -95,6 +96,14 @@ function resolveRetryAfterMs(params: {
|
95 | 96 | return remaining > 0 ? remaining : undefined; |
96 | 97 | } |
97 | 98 | |
| 99 | +function resolveStartupVerificationTimestamp(nowMs: unknown): string { |
| 100 | +return ( |
| 101 | +timestampMsToIsoString(nowMs) ?? |
| 102 | +timestampMsToIsoString(Date.now()) ?? |
| 103 | +"1970-01-01T00:00:00.000Z" |
| 104 | +); |
| 105 | +} |
| 106 | + |
98 | 107 | function shouldHonorCooldown(params: { |
99 | 108 | state: MatrixStartupVerificationState | null; |
100 | 109 | verification: MatrixOwnDeviceVerificationStatus; |
@@ -189,6 +198,7 @@ export async function ensureMatrixStartupVerification(params: {
|
189 | 198 | ); |
190 | 199 | const cooldownMs = cooldownHours * 60 * 60 * 1000; |
191 | 200 | const nowMs = params.nowMs ?? Date.now(); |
| 201 | +const attemptedAt = resolveStartupVerificationTimestamp(nowMs); |
192 | 202 | const state = await readStartupVerificationState(statePath); |
193 | 203 | const stateCooldownMs = resolveStateCooldownMs(state, cooldownMs); |
194 | 204 | if (shouldHonorCooldown({ state, verification, stateCooldownMs, nowMs })) { |
@@ -208,7 +218,7 @@ export async function ensureMatrixStartupVerification(params: {
|
208 | 218 | await writeJsonFileAtomically(statePath, { |
209 | 219 | userId: verification.userId, |
210 | 220 | deviceId: verification.deviceId, |
211 | | -attemptedAt: new Date(nowMs).toISOString(), |
| 221 | + attemptedAt, |
212 | 222 | outcome: "requested", |
213 | 223 | requestId: request.id, |
214 | 224 | transactionId: request.transactionId, |
@@ -224,7 +234,7 @@ export async function ensureMatrixStartupVerification(params: {
|
224 | 234 | await writeJsonFileAtomically(statePath, { |
225 | 235 | userId: verification.userId, |
226 | 236 | deviceId: verification.deviceId, |
227 | | -attemptedAt: new Date(nowMs).toISOString(), |
| 237 | + attemptedAt, |
228 | 238 | outcome: "failed", |
229 | 239 | error, |
230 | 240 | } satisfies MatrixStartupVerificationState).catch(() => {}); |
|