Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion server/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ func ForceLeave(d *sql.DB) error {
count := 0

// Select all users who are currently in the room
rows, err := tx.Query(`SELECT ainsID FROM log WHERE isEnter=1 AND time<?`, yesterdayStartTime)
rows, err := tx.Query(`
SELECT ainsID
FROM log l
WHERE time = (
SELECT MAX(time)
FROM log
WHERE ainsID = l.ainsID
AND time >= ?
)
AND isEnter = 1
`, yesterdayStartTime)
Comment on lines +95 to +105
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query logic may miss users who entered more than 24 hours ago and never left. The subquery SELECT MAX(time) FROM log WHERE ainsID = l.ainsID AND time >= ? only considers log entries from the last 24 hours. If a user entered 2+ days ago and has had no log activity since, they won't be selected for force-leave.

Consider removing the AND time >= ? condition from the subquery to check the user's latest log entry regardless of when it occurred. The outer condition isEnter = 1 will then correctly identify all users currently in the room based on their most recent log entry.

Copilot uses AI. Check for mistakes.
if err != nil {
return fmt.Errorf("failed to query users in room: %w", err)
}
Expand Down
18 changes: 10 additions & 8 deletions server/utils/csvexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const (
TimestampMillisecondDivisor = 1000
)

const Enter = 1
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant Enter should follow Go naming conventions for exported identifiers. Since this constant represents the integer value for the "enter" state and is only used within this package, it should be unexported (lowercase). Consider renaming it to enterValue or enterStatus to better indicate what the constant represents and to make it package-private.

Copilot uses AI. Check for mistakes.

// csvExport exports log data as CSV text
func csvExport(d *sql.DB) (string, error) {
rows, err := d.Query(`select time,log.sid,name,log.isenter,ext from log,users where log.sid=users.sid`)
rows, err := d.Query(`SELECT log.time,log.ainsID,users.name,log.isenter,log.ext FROM log,users WHERE log.ainsID=users.ainsID`)
if err != nil {
return "", err
}
Expand All @@ -27,18 +29,18 @@ func csvExport(d *sql.DB) (string, error) {
for rows.Next() {
var (
ts int64
sid string
ainsID string
name string
isenter int64
isEnter int64
ext string
)
if err := rows.Scan(&ts, &sid, &name, &isenter, &ext); err != nil {
if err := rows.Scan(&ts, &ainsID, &name, &isEnter, &ext); err != nil {
return "", err
}

datefmted := time.Unix(ts/TimestampMillisecondDivisor, 0).Format("2006-01-02 15:04:05")
entstr := "Leave"
if isenter == 1 {
if isEnter == Enter {
entstr = "Enter"
}

Expand All @@ -61,9 +63,9 @@ func csvExport(d *sql.DB) (string, error) {
}
mess = strings.Replace(mess, "\"", "\"\"", -1)

csv += fmt.Sprintf("%v,%v,%v,%v,%v,\"%v\"\n", datefmted, sid, name, entstr, useage, mess)
csv += fmt.Sprintf("%v,%v,%v,%v,%v,\"%v\"\n", datefmted, ainsID, name, entstr, useage, mess)
} else {
csv += fmt.Sprintf("%v,%v,%v,%v,,\n", datefmted, sid, name, entstr)
csv += fmt.Sprintf("%v,%v,%v,%v,,\n", datefmted, ainsID, name, entstr)
}
}
err = rows.Close()
Expand Down Expand Up @@ -101,7 +103,7 @@ func sendMonthlyLog(d *sql.DB) error {
return err
}

if _, err = d.Exec(`delete from log`); err != nil {
if _, err = d.Exec(`DELETE FROM log`); err != nil {
return err
}
return nil
Expand Down
Loading