Files
lincodex/backend/internal/usecase/assessment/signal_notice.go
T

98 lines
3.9 KiB
Go

package assessment
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/port/outbound"
)
// CurrentSignalNoticeVersion is the version of the notice text that lists the
// collected assessment signals and the measures the page applies during an
// attempt (fullscreen, hiding the task when the candidate leaves, copy, print,
// and paste restrictions). Change it together with the client text so that
// every candidate acknowledges the new wording. Acknowledgements are stored per
// version, so one given for an earlier version never counts for this one.
//
// Version 4 added the screenshot-key and developer-tools hints, the User-Agent
// digest, and the one-open-page rule, and says plainly that a screenshot or a
// photo of the screen cannot be prevented; version 3 described the watermark of
// the task screen and text, which version 2 announced as planned; version 2
// added the attempt measures to the text; version 1 listed signals only.
const CurrentSignalNoticeVersion int32 = 4
// ErrSignalNoticeVersionMismatch reports an acknowledgement for a notice
// version other than the one the server requires.
var ErrSignalNoticeVersionMismatch = errors.New("assessment signal notice version is not current")
// SignalNoticeState is the acknowledgement state of the current notice version.
type SignalNoticeState struct {
Version int32
Acknowledged bool
AcknowledgedAt time.Time
}
// GetSignalNotice reads whether the candidate acknowledged the current notice.
// The record is not a consent and does not influence signal storage.
func (service *SignalService) GetSignalNotice(ctx context.Context, actorID uuid.UUID) (SignalNoticeState, error) {
const location = "SignalService.GetSignalNotice"
state := SignalNoticeState{Version: CurrentSignalNoticeVersion}
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireAttemptCandidate(ctx, unit, actorID, location); err != nil {
return err
}
acknowledgedAt, err := unit.AssessmentSignalNotices().Get(ctx, actorID, CurrentSignalNoticeVersion)
if errors.Is(err, outbound.ErrAssessmentSignalNoticeNotAcknowledged) {
return nil
}
if err != nil {
return fmt.Errorf("%s - read acknowledgement: %w", location, err)
}
state.Acknowledged, state.AcknowledgedAt = true, acknowledgedAt
return nil
})
if err != nil {
return SignalNoticeState{}, fmt.Errorf("%s - transaction: %w", location, err)
}
return state, nil
}
// AcknowledgeSignalNotice records the first acknowledgement of the current
// notice version. A repeat keeps the stored time, so a retry after a lost
// response succeeds. Another version is refused without storing anything.
func (service *SignalService) AcknowledgeSignalNotice(
ctx context.Context,
actorID uuid.UUID,
version int32,
) (SignalNoticeState, error) {
const location = "SignalService.AcknowledgeSignalNotice"
if version != CurrentSignalNoticeVersion {
return SignalNoticeState{}, fmt.Errorf("%s - compare version: %w", location, ErrSignalNoticeVersionMismatch)
}
state := SignalNoticeState{Version: CurrentSignalNoticeVersion, Acknowledged: true}
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireAttemptCandidate(ctx, unit, actorID, location); err != nil {
return err
}
now, err := unit.AssessmentBank().DatabaseTime(ctx)
if err != nil {
return fmt.Errorf("%s - read database time: %w", location, err)
}
state.AcknowledgedAt, err = unit.AssessmentSignalNotices().Acknowledge(
ctx, actorID, CurrentSignalNoticeVersion, now.UTC().Truncate(time.Microsecond),
)
if err != nil {
return fmt.Errorf("%s - store acknowledgement: %w", location, err)
}
return nil
})
if err != nil {
return SignalNoticeState{}, fmt.Errorf("%s - transaction: %w", location, err)
}
return state, nil
}