mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 21:15:32 +09:00
fix(app): keep the guild verification timer stable across switches (#1738)
This commit is contained in:
1 parent
a1b595d52f
commit
8836565c32
2 files changed
+26
-20
No files matched your search
@@ -32,14 +32,18 @@ export const VerificationBarrier = observer(({channel}: Props) => {
|
|||||||
case VerificationFailureReason.ACCOUNT_TOO_NEW:
|
case VerificationFailureReason.ACCOUNT_TOO_NEW:
|
||||||
return (
|
return (
|
||||||
<AccountTooNewBarrier
|
<AccountTooNewBarrier
|
||||||
initialTimeRemaining={verificationStatus.timeRemaining || 0}
|
initialTimeRemaining={
|
||||||
|
verificationStatus.verificationEndsAt ? Math.max(0, verificationStatus.verificationEndsAt - Date.now()) : 0
|
||||||
|
}
|
||||||
data-flx="channel.verification-barrier.account-too-new-barrier"
|
data-flx="channel.verification-barrier.account-too-new-barrier"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
case VerificationFailureReason.NOT_MEMBER_LONG_ENOUGH:
|
case VerificationFailureReason.NOT_MEMBER_LONG_ENOUGH:
|
||||||
return (
|
return (
|
||||||
<NotMemberLongEnoughBarrier
|
<NotMemberLongEnoughBarrier
|
||||||
initialTimeRemaining={verificationStatus.timeRemaining || 0}
|
initialTimeRemaining={
|
||||||
|
verificationStatus.verificationEndsAt ? Math.max(0, verificationStatus.verificationEndsAt - Date.now()) : 0
|
||||||
|
}
|
||||||
data-flx="channel.verification-barrier.not-member-long-enough-barrier"
|
data-flx="channel.verification-barrier.not-member-long-enough-barrier"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -50,7 +54,9 @@ export const VerificationBarrier = observer(({channel}: Props) => {
|
|||||||
case VerificationFailureReason.TIMED_OUT:
|
case VerificationFailureReason.TIMED_OUT:
|
||||||
return (
|
return (
|
||||||
<TimeoutBarrier
|
<TimeoutBarrier
|
||||||
initialTimeRemaining={verificationStatus.timeRemaining || 0}
|
initialTimeRemaining={
|
||||||
|
verificationStatus.verificationEndsAt ? Math.max(0, verificationStatus.verificationEndsAt - Date.now()) : 0
|
||||||
|
}
|
||||||
data-flx="channel.verification-barrier.timeout-barrier"
|
data-flx="channel.verification-barrier.timeout-barrier"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export type VerificationFailureReason = ValueOf<typeof VerificationFailureReason
|
|||||||
interface VerificationStatus {
|
interface VerificationStatus {
|
||||||
canAccess: boolean;
|
canAccess: boolean;
|
||||||
reason?: VerificationFailureReason;
|
reason?: VerificationFailureReason;
|
||||||
timeRemaining?: number;
|
verificationEndsAt?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
class GuildVerification {
|
class GuildVerification {
|
||||||
@@ -81,10 +81,11 @@ class GuildVerification {
|
|||||||
for (const guild of guilds) {
|
for (const guild of guilds) {
|
||||||
const status = this.computeVerificationStatus(guild);
|
const status = this.computeVerificationStatus(guild);
|
||||||
newVerificationStatus[guild.id] = status;
|
newVerificationStatus[guild.id] = status;
|
||||||
if (!status.canAccess && status.timeRemaining && status.timeRemaining > 0) {
|
const delay = status.verificationEndsAt ? status.verificationEndsAt - Date.now() : undefined;
|
||||||
|
if (!status.canAccess && delay && delay > 0) {
|
||||||
newTimers[guild.id] = setTimeout(() => {
|
newTimers[guild.id] = setTimeout(() => {
|
||||||
this.recomputeGuild(guild.id);
|
this.recomputeGuild(guild.id);
|
||||||
}, status.timeRemaining);
|
}, delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.verificationStatus = Object.freeze(newVerificationStatus);
|
this.verificationStatus = Object.freeze(newVerificationStatus);
|
||||||
@@ -106,10 +107,11 @@ class GuildVerification {
|
|||||||
}
|
}
|
||||||
const newTimers = {...this.timers};
|
const newTimers = {...this.timers};
|
||||||
delete newTimers[guildId];
|
delete newTimers[guildId];
|
||||||
if (!status.canAccess && status.timeRemaining && status.timeRemaining > 0) {
|
const delay = status.verificationEndsAt ? status.verificationEndsAt - Date.now() : undefined;
|
||||||
|
if (!status.canAccess && delay && delay > 0) {
|
||||||
newTimers[guildId] = setTimeout(() => {
|
newTimers[guildId] = setTimeout(() => {
|
||||||
this.recomputeGuild(guildId);
|
this.recomputeGuild(guildId);
|
||||||
}, status.timeRemaining);
|
}, delay);
|
||||||
}
|
}
|
||||||
this.verificationStatus = Object.freeze(newVerificationStatus);
|
this.verificationStatus = Object.freeze(newVerificationStatus);
|
||||||
this.timers = newTimers;
|
this.timers = newTimers;
|
||||||
@@ -124,12 +126,11 @@ class GuildVerification {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (member?.communicationDisabledUntil) {
|
if (member?.communicationDisabledUntil) {
|
||||||
const timeoutUntil = member.communicationDisabledUntil;
|
const timeoutUntil = member.communicationDisabledUntil;
|
||||||
const timeRemaining = timeoutUntil.getTime() - now;
|
if (timeoutUntil.getTime() > now) {
|
||||||
if (timeRemaining > 0) {
|
|
||||||
return {
|
return {
|
||||||
canAccess: false,
|
canAccess: false,
|
||||||
reason: VerificationFailureReason.TIMED_OUT,
|
reason: VerificationFailureReason.TIMED_OUT,
|
||||||
timeRemaining,
|
verificationEndsAt: timeoutUntil.getTime(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,18 +165,16 @@ class GuildVerification {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (verificationLevel >= GuildVerificationLevel.MEDIUM) {
|
if (verificationLevel >= GuildVerificationLevel.MEDIUM) {
|
||||||
const accountAge = Date.now() - user.createdAt.getTime();
|
const verificationEndsAt = user.createdAt.getTime() + FIVE_MINUTES_MS;
|
||||||
if (accountAge < FIVE_MINUTES_MS) {
|
if (verificationEndsAt > now) {
|
||||||
const timeRemaining = FIVE_MINUTES_MS - accountAge;
|
return {canAccess: false, reason: VerificationFailureReason.ACCOUNT_TOO_NEW, verificationEndsAt};
|
||||||
return {canAccess: false, reason: VerificationFailureReason.ACCOUNT_TOO_NEW, timeRemaining};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (verificationLevel >= GuildVerificationLevel.HIGH) {
|
if (verificationLevel >= GuildVerificationLevel.HIGH) {
|
||||||
if (member?.joinedAt) {
|
if (member?.joinedAt) {
|
||||||
const membershipDuration = Date.now() - member.joinedAt.getTime();
|
const verificationEndsAt = member.joinedAt.getTime() + TEN_MINUTES_MS;
|
||||||
if (membershipDuration < TEN_MINUTES_MS) {
|
if (verificationEndsAt > now) {
|
||||||
const timeRemaining = TEN_MINUTES_MS - membershipDuration;
|
return {canAccess: false, reason: VerificationFailureReason.NOT_MEMBER_LONG_ENOUGH, verificationEndsAt};
|
||||||
return {canAccess: false, reason: VerificationFailureReason.NOT_MEMBER_LONG_ENOUGH, timeRemaining};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,7 +195,8 @@ class GuildVerification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getTimeRemaining(guildId: string): number | null {
|
getTimeRemaining(guildId: string): number | null {
|
||||||
return this.verificationStatus[guildId]?.timeRemaining ?? null;
|
const verificationEndsAt = this.verificationStatus[guildId]?.verificationEndsAt;
|
||||||
|
return verificationEndsAt ? Math.max(0, verificationEndsAt - Date.now()) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user