refactor(gorm): remove ...At fields from model
This commit is contained in:
parent
d38c57b830
commit
6b6c775ab3
5 changed files with 29 additions and 99 deletions
|
@ -32,9 +32,9 @@ En tant qu'utilisateur, vous avez le droit de :
|
||||||
- avoir accès à vos données sauvegardées
|
- avoir accès à vos données sauvegardées
|
||||||
- modifier ces données
|
- modifier ces données
|
||||||
|
|
||||||
Pour exercer vos droits concernant l'accès aux données ou à la modification des données, contacter me@anhgelus.world.
|
Pour exercer vos droits concernant l'accès aux données ou à la modification des données, contactez me@anhgelus.world.
|
||||||
|
|
||||||
Concernant la suppression des données, entraîne de facto une remise à zéro de votre profile sur un serveur, contacter le propriétaire du serveur.
|
La suppression des données entraîne de facto une remise à zéro de votre profile sur un serveur, contactez le propriétaire du serveur pour exercer ce droit.
|
||||||
|
|
||||||
Concernant la suppression intégrale des données, contacter me@anhgelus.world.
|
Concernant la suppression intégrale des données, contacter me@anhgelus.world.
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,11 @@ package config
|
||||||
import (
|
import (
|
||||||
"github.com/anhgelus/gokord"
|
"github.com/anhgelus/gokord"
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"gorm.io/gorm"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GuildConfig struct {
|
type GuildConfig struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primarykey"`
|
||||||
GuildID string `gorm:"not null;unique"`
|
GuildID string `gorm:"not null;unique"`
|
||||||
XpRoles []XpRole
|
XpRoles []XpRole
|
||||||
DisabledChannels string
|
DisabledChannels string
|
||||||
|
@ -16,7 +15,7 @@ type GuildConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type XpRole struct {
|
type XpRole struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primarykey"`
|
||||||
XP uint
|
XP uint
|
||||||
RoleID string
|
RoleID string
|
||||||
GuildConfigID uint
|
GuildConfigID uint
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/anhgelus/gokord"
|
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"github.com/anhgelus/les-copaings-bot/config"
|
"github.com/anhgelus/les-copaings-bot/config"
|
||||||
"github.com/anhgelus/les-copaings-bot/exp"
|
"github.com/anhgelus/les-copaings-bot/exp"
|
||||||
|
@ -162,8 +161,11 @@ func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *r
|
||||||
|
|
||||||
func OnLeave(_ *discordgo.Session, e *discordgo.GuildMemberRemove) {
|
func OnLeave(_ *discordgo.Session, e *discordgo.GuildMemberRemove) {
|
||||||
utils.SendDebug("Leave event", "user_id", e.User.ID)
|
utils.SendDebug("Leave event", "user_id", e.User.ID)
|
||||||
|
if e.User.Bot {
|
||||||
|
return
|
||||||
|
}
|
||||||
c := user.GetCopaing(e.User.ID, e.GuildID)
|
c := user.GetCopaing(e.User.ID, e.GuildID)
|
||||||
if err := gokord.DB.Where("guild_id = ?", e.GuildID).Delete(c).Error; err != nil {
|
if err := c.Delete(); err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert(
|
||||||
"events.go - deleting user from db", err.Error(),
|
"events.go - deleting user from db", err.Error(),
|
||||||
"user_id", e.User.ID,
|
"user_id", e.User.ID,
|
||||||
|
|
|
@ -4,32 +4,21 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/anhgelus/gokord"
|
"github.com/anhgelus/gokord"
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Copaing struct {
|
type Copaing struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primarykey"`
|
||||||
DiscordID string `gorm:"not null"`
|
DiscordID string `gorm:"not null"`
|
||||||
//XP []CopaingXP
|
XP []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"`
|
||||||
XP uint `gorm:"default:0"`
|
GuildID string `gorm:"not null"`
|
||||||
GuildID string `gorm:"not null"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type leftCopaing struct {
|
type CopaingXP struct {
|
||||||
ID uint
|
ID uint `gorm:"primarykey"`
|
||||||
StopDelete chan<- interface{}
|
XP uint `gorm:"default:0"`
|
||||||
|
CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//type CopaingXP struct {
|
|
||||||
// gorm.Model
|
|
||||||
// XP uint `gorm:"default:0"`
|
|
||||||
// CopaingID uint
|
|
||||||
//}
|
|
||||||
|
|
||||||
var (
|
|
||||||
leftCopaingsMap = map[string]*leftCopaing{}
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LastEvent = "last_event"
|
LastEvent = "last_event"
|
||||||
AlreadyRemoved = "already_removed"
|
AlreadyRemoved = "already_removed"
|
||||||
|
@ -52,50 +41,11 @@ func GetCopaing(discordID string, guildID string) *Copaing {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Copaing) Load() error {
|
func (c *Copaing) Load() error {
|
||||||
// check if user left in the past 48 hours
|
return gokord.DB.
|
||||||
k := c.GuildID + ":" + c.DiscordID
|
Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
|
||||||
l, ok := leftCopaingsMap[k]
|
Preload("XP").
|
||||||
if !ok || l == nil {
|
FirstOrCreate(c).
|
||||||
// if not, common first or create
|
Error
|
||||||
return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
|
|
||||||
}
|
|
||||||
// else, getting last data
|
|
||||||
tmp := Copaing{
|
|
||||||
Model: gorm.Model{
|
|
||||||
ID: c.ID,
|
|
||||||
},
|
|
||||||
DiscordID: c.DiscordID,
|
|
||||||
GuildID: c.GuildID,
|
|
||||||
}
|
|
||||||
if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil {
|
|
||||||
// if error, avoid getting old data and use new one
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/member.go - Getting user in soft delete", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", c.DiscordID,
|
|
||||||
"last_id", l.ID,
|
|
||||||
)
|
|
||||||
return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
|
|
||||||
}
|
|
||||||
// resetting internal data
|
|
||||||
tmp.Model = gorm.Model{}
|
|
||||||
l.StopDelete <- true
|
|
||||||
leftCopaingsMap[k] = nil
|
|
||||||
// creating new data
|
|
||||||
err := gokord.DB.Create(&tmp).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// delete old data
|
|
||||||
if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil {
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/member.go - Deleting user in soft delete", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", c.DiscordID,
|
|
||||||
"last_id", l.ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Copaing) Save() error {
|
func (c *Copaing) Save() error {
|
||||||
|
@ -105,3 +55,7 @@ func (c *Copaing) Save() error {
|
||||||
func (c *Copaing) GenKey(key string) string {
|
func (c *Copaing) GenKey(key string) string {
|
||||||
return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key)
|
return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Copaing) Delete() error {
|
||||||
|
return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error
|
||||||
|
}
|
||||||
|
|
33
user/xp.go
33
user/xp.go
|
@ -10,7 +10,6 @@ import (
|
||||||
"github.com/anhgelus/les-copaings-bot/exp"
|
"github.com/anhgelus/les-copaings-bot/exp"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"gorm.io/gorm"
|
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -105,20 +104,20 @@ func (c *Copaing) AddXPAlreadyRemoved(xp uint) uint {
|
||||||
utils.SendAlert("user/xp.go - Getting redis client (set)", err.Error())
|
utils.SendAlert("user/xp.go - Getting redis client (set)", err.Error())
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
exp := xp + c.XPAlreadyRemoved()
|
nxp := xp + c.XPAlreadyRemoved()
|
||||||
err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), exp, 0).Err()
|
err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), nxp, 0).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert(
|
||||||
"user/xp.go - Setting already removed",
|
"user/xp.go - Setting already removed",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
"exp already removed",
|
"exp already removed",
|
||||||
exp,
|
nxp,
|
||||||
"base_key",
|
"base_key",
|
||||||
c.GenKey(""),
|
c.GenKey(""),
|
||||||
)
|
)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return exp
|
return nxp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Copaing) XPAlreadyRemoved() uint {
|
func (c *Copaing) XPAlreadyRemoved() uint {
|
||||||
|
@ -159,27 +158,3 @@ func (c *Copaing) XPAlreadyRemoved() uint {
|
||||||
}
|
}
|
||||||
return uint(xp)
|
return uint(xp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Copaing) Reset() {
|
|
||||||
gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Copaing) AfterDelete(db *gorm.DB) error {
|
|
||||||
id := c.ID
|
|
||||||
dID := c.DiscordID
|
|
||||||
gID := c.GuildID
|
|
||||||
k := c.GuildID + ":" + c.DiscordID
|
|
||||||
ch := utils.NewTimer(48*time.Hour, func(stop chan<- interface{}) {
|
|
||||||
if err := db.Unscoped().Where("id = ?", id).Delete(c).Error; err != nil {
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/xp.go - Removing user from database", err.Error(),
|
|
||||||
"discord_id", dID,
|
|
||||||
"guild_id", gID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
stop <- true
|
|
||||||
leftCopaingsMap[k] = nil
|
|
||||||
})
|
|
||||||
leftCopaingsMap[k] = &leftCopaing{id, ch}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue