BIG ASS COMMIT
This commit is contained in:
@ -17,8 +17,6 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
@ -42,50 +40,54 @@ func (pq *PuppetQuery) New() *Puppet {
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet")
|
||||
if err != nil || rows == nil {
|
||||
ans := pq.db.Find(&puppets)
|
||||
if ans.Error != nil || len(puppets) == 0 {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
puppets = append(puppets, pq.New().Scan(rows))
|
||||
}
|
||||
// defer rows.Close()
|
||||
// for rows.Next() {
|
||||
// puppets = append(puppets, pq.New().Scan(rows))
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet {
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE jid=$1", jid)
|
||||
if row == nil {
|
||||
func (pq *PuppetQuery) Get(jid types.GroupMeID) *Puppet {
|
||||
puppet := Puppet{}
|
||||
ans := pq.db.Where("jid = ?", jid).Take(puppet)
|
||||
if ans.Error != nil {
|
||||
return nil
|
||||
}
|
||||
return pq.New().Scan(row)
|
||||
return &puppet
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid=$1", mxid)
|
||||
if row == nil {
|
||||
puppet := Puppet{}
|
||||
ans := pq.db.Where("custom_mxid = ?", mxid).Take(puppet)
|
||||
if ans.Error != nil {
|
||||
return nil
|
||||
}
|
||||
return pq.New().Scan(row)
|
||||
return &puppet
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) {
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid<>''")
|
||||
if err != nil || rows == nil {
|
||||
|
||||
ans := pq.db.Find(&puppets, "custom_mxid <> ''")
|
||||
if ans.Error != nil || len(puppets) != 0 {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
puppets = append(puppets, pq.New().Scan(rows))
|
||||
}
|
||||
// defer rows.Close()
|
||||
// for rows.Next() {
|
||||
// puppets = append(puppets, pq.New().Scan(rows))
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
//Puppet is comment
|
||||
type Puppet struct {
|
||||
db *Database
|
||||
log log.Logger
|
||||
|
||||
JID types.WhatsAppID
|
||||
JID types.GroupMeID `gorm:"primaryKey"`
|
||||
Avatar string
|
||||
AvatarURL id.ContentURI
|
||||
Displayname string
|
||||
@ -94,45 +96,45 @@ type Puppet struct {
|
||||
CustomMXID id.UserID
|
||||
AccessToken string
|
||||
NextBatch string
|
||||
EnablePresence bool
|
||||
EnableReceipts bool
|
||||
EnablePresence bool `gorm:"notNull;default:true"`
|
||||
EnableReceipts bool `gorm:"notNull;default:true"`
|
||||
}
|
||||
|
||||
func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
||||
var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
||||
var quality sql.NullInt64
|
||||
var enablePresence, enableReceipts sql.NullBool
|
||||
err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
puppet.log.Errorln("Database scan failed:", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
puppet.Displayname = displayname.String
|
||||
puppet.Avatar = avatar.String
|
||||
puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
|
||||
puppet.NameQuality = int8(quality.Int64)
|
||||
puppet.CustomMXID = id.UserID(customMXID.String)
|
||||
puppet.AccessToken = accessToken.String
|
||||
puppet.NextBatch = nextBatch.String
|
||||
puppet.EnablePresence = enablePresence.Bool
|
||||
puppet.EnableReceipts = enableReceipts.Bool
|
||||
return puppet
|
||||
}
|
||||
// func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
||||
// var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
||||
// var quality sql.NullInt64
|
||||
// var enablePresence, enableReceipts sql.NullBool
|
||||
// err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts)
|
||||
// if err != nil {
|
||||
// if err != sql.ErrNoRows {
|
||||
// puppet.log.Errorln("Database scan failed:", err)
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
// puppet.Displayname = displayname.String
|
||||
// puppet.Avatar = avatar.String
|
||||
// puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
|
||||
// puppet.NameQuality = int8(quality.Int64)
|
||||
// puppet.CustomMXID = id.UserID(customMXID.String)
|
||||
// puppet.AccessToken = accessToken.String
|
||||
// puppet.NextBatch = nextBatch.String
|
||||
// puppet.EnablePresence = enablePresence.Bool
|
||||
// puppet.EnableReceipts = enableReceipts.Bool
|
||||
// return puppet
|
||||
// }
|
||||
|
||||
func (puppet *Puppet) Insert() {
|
||||
_, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts)
|
||||
if err != nil {
|
||||
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
|
||||
// _, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
// puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts)
|
||||
ans := puppet.db.Create(&puppet)
|
||||
if ans.Error != nil {
|
||||
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, ans.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func (puppet *Puppet) Update() {
|
||||
_, err := puppet.db.Exec("UPDATE puppet SET displayname=$1, name_quality=$2, avatar=$3, avatar_url=$4, custom_mxid=$5, access_token=$6, next_batch=$7, enable_presence=$8, enable_receipts=$9 WHERE jid=$10",
|
||||
puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts, puppet.JID)
|
||||
if err != nil {
|
||||
puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err)
|
||||
ans := puppet.db.Where("jid = ?", puppet.JID).Updates(&puppet)
|
||||
if ans.Error != nil {
|
||||
puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, ans.Error)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user