website/pkg/models/ip_addresses.go
2024-04-25 18:55:02 -07:00

75 lines
1.8 KiB
Go

package models
import (
"net/http"
"time"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/utility"
)
// IPAddress table to log which networks users have logged in from.
type IPAddress struct {
ID uint64 `gorm:"primaryKey"`
UserID uint64 `gorm:"index"`
IPAddress string `gorm:"index"`
NumberVisits uint64 // count of times their LastLoginAt pinged from this address
CreatedAt time.Time // first time seen
UpdatedAt time.Time // last time seen
}
// PingIPAddress logs or upserts the user's current IP address into the IPAddress table.
func PingIPAddress(r *http.Request, user *User, incrementVisit bool) error {
var (
addr = utility.IPAddress(r)
ip *IPAddress
)
// Have we seen it before?
ip, err := LoadUserIPAddress(user, addr)
if err != nil {
// Insert it.
log.Debug("User %s IP %s seen for the first time", user.Username, addr)
ip = &IPAddress{
UserID: user.ID,
IPAddress: addr,
CreatedAt: time.Now(),
}
result := DB.Create(ip)
if result.Error != nil {
return result.Error
}
}
// Are we refreshing the NumberVisits count? Note: this happens each
// time the main website will refresh the user LastLoginAt.
if incrementVisit || ip.NumberVisits == 0 {
ip.NumberVisits++
}
// Ping the update.
ip.UpdatedAt = time.Now()
return ip.Save()
}
func LoadUserIPAddress(user *User, ipAddr string) (*IPAddress, error) {
var ip = &IPAddress{}
var result = DB.Model(&IPAddress{}).Where(
"user_id = ? AND ip_address = ?",
user.ID, ipAddr,
).First(&ip)
return ip, result.Error
}
// Save photo.
func (ip *IPAddress) Save() error {
result := DB.Save(ip)
return result.Error
}
// Delete the DB entry.
func (ip *IPAddress) Delete() error {
return DB.Delete(ip).Error
}