2022-03-29 12:10:16 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2023-02-18 20:53:51 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-03-29 12:10:16 +00:00
|
|
|
|
2023-08-16 21:54:38 +00:00
|
|
|
"go.mau.fi/util/dbutil"
|
2022-03-29 12:10:16 +00:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2022-05-28 20:03:24 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2022-03-29 12:10:16 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 20:53:51 +00:00
|
|
|
type GuildBridgingMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// GuildBridgeNothing tells the bridge to never bridge messages, not even checking if a portal exists.
|
|
|
|
GuildBridgeNothing GuildBridgingMode = iota
|
|
|
|
// GuildBridgeIfPortalExists tells the bridge to bridge messages in channels that already have portals.
|
|
|
|
GuildBridgeIfPortalExists
|
|
|
|
// GuildBridgeCreateOnMessage tells the bridge to create portals as soon as a message is received.
|
|
|
|
GuildBridgeCreateOnMessage
|
|
|
|
// GuildBridgeEverything tells the bridge to proactively create portals on startup and when receiving channel create notifications.
|
|
|
|
GuildBridgeEverything
|
|
|
|
|
|
|
|
GuildBridgeInvalid GuildBridgingMode = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseGuildBridgingMode(str string) GuildBridgingMode {
|
|
|
|
str = strings.ToLower(str)
|
|
|
|
str = strings.ReplaceAll(str, "-", "")
|
|
|
|
str = strings.ReplaceAll(str, "_", "")
|
|
|
|
switch str {
|
|
|
|
case "nothing", "0":
|
|
|
|
return GuildBridgeNothing
|
|
|
|
case "ifportalexists", "1":
|
|
|
|
return GuildBridgeIfPortalExists
|
|
|
|
case "createonmessage", "2":
|
|
|
|
return GuildBridgeCreateOnMessage
|
|
|
|
case "everything", "3":
|
|
|
|
return GuildBridgeEverything
|
|
|
|
default:
|
|
|
|
return GuildBridgeInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gbm GuildBridgingMode) String() string {
|
|
|
|
switch gbm {
|
|
|
|
case GuildBridgeNothing:
|
|
|
|
return "nothing"
|
|
|
|
case GuildBridgeIfPortalExists:
|
|
|
|
return "if-portal-exists"
|
|
|
|
case GuildBridgeCreateOnMessage:
|
|
|
|
return "create-on-message"
|
|
|
|
case GuildBridgeEverything:
|
|
|
|
return "everything"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gbm GuildBridgingMode) Description() string {
|
|
|
|
switch gbm {
|
|
|
|
case GuildBridgeNothing:
|
|
|
|
return "never bridge messages"
|
|
|
|
case GuildBridgeIfPortalExists:
|
|
|
|
return "bridge messages in existing portals"
|
|
|
|
case GuildBridgeCreateOnMessage:
|
|
|
|
return "bridge all messages and create portals on first message"
|
|
|
|
case GuildBridgeEverything:
|
|
|
|
return "bridge all messages and create portals proactively"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:58:09 +00:00
|
|
|
type GuildQuery struct {
|
|
|
|
db *Database
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2023-02-18 20:53:51 +00:00
|
|
|
guildSelect = "SELECT dcid, mxid, plain_name, name, name_set, avatar, avatar_url, avatar_set, bridging_mode FROM guild"
|
2022-05-27 12:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (gq *GuildQuery) New() *Guild {
|
|
|
|
return &Guild{
|
|
|
|
db: gq.db,
|
|
|
|
log: gq.log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (gq *GuildQuery) GetByID(dcid string) *Guild {
|
|
|
|
query := guildSelect + " WHERE dcid=$1"
|
|
|
|
return gq.New().Scan(gq.db.QueryRow(query, dcid))
|
2022-05-27 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (gq *GuildQuery) GetByMXID(mxid id.RoomID) *Guild {
|
|
|
|
query := guildSelect + " WHERE mxid=$1"
|
|
|
|
return gq.New().Scan(gq.db.QueryRow(query, mxid))
|
|
|
|
}
|
2022-05-27 12:58:09 +00:00
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (gq *GuildQuery) GetAll() []*Guild {
|
|
|
|
rows, err := gq.db.Query(guildSelect)
|
|
|
|
if err != nil {
|
|
|
|
gq.log.Errorln("Failed to query guilds:", err)
|
2022-05-27 12:58:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
var guilds []*Guild
|
2022-05-27 12:58:09 +00:00
|
|
|
for rows.Next() {
|
2022-05-28 20:03:24 +00:00
|
|
|
guild := gq.New().Scan(rows)
|
|
|
|
if guild != nil {
|
|
|
|
guilds = append(guilds, guild)
|
|
|
|
}
|
2022-05-27 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return guilds
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:10:16 +00:00
|
|
|
type Guild struct {
|
|
|
|
db *Database
|
|
|
|
log log.Logger
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
ID string
|
|
|
|
MXID id.RoomID
|
2022-07-08 17:48:36 +00:00
|
|
|
PlainName string
|
2022-05-28 20:03:24 +00:00
|
|
|
Name string
|
|
|
|
NameSet bool
|
|
|
|
Avatar string
|
|
|
|
AvatarURL id.ContentURI
|
|
|
|
AvatarSet bool
|
|
|
|
|
2023-02-18 20:53:51 +00:00
|
|
|
BridgingMode GuildBridgingMode
|
2022-03-29 12:10:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
func (g *Guild) Scan(row dbutil.Scannable) *Guild {
|
2022-05-28 20:03:24 +00:00
|
|
|
var mxid sql.NullString
|
|
|
|
var avatarURL string
|
2023-02-18 20:53:51 +00:00
|
|
|
err := row.Scan(&g.ID, &mxid, &g.PlainName, &g.Name, &g.NameSet, &g.Avatar, &avatarURL, &g.AvatarSet, &g.BridgingMode)
|
2022-03-29 12:10:16 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
g.log.Errorln("Database scan failed:", err)
|
2022-05-28 20:03:24 +00:00
|
|
|
panic(err)
|
2022-03-29 12:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-18 20:53:51 +00:00
|
|
|
if g.BridgingMode < GuildBridgeNothing || g.BridgingMode > GuildBridgeEverything {
|
|
|
|
panic(fmt.Errorf("invalid guild bridging mode %d in guild %s", g.BridgingMode, g.ID))
|
|
|
|
}
|
2022-05-28 20:03:24 +00:00
|
|
|
g.MXID = id.RoomID(mxid.String)
|
|
|
|
g.AvatarURL, _ = id.ParseContentURI(avatarURL)
|
2022-03-29 12:10:16 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (g *Guild) mxidPtr() *id.RoomID {
|
|
|
|
if g.MXID != "" {
|
|
|
|
return &g.MXID
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-08 17:48:36 +00:00
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (g *Guild) Insert() {
|
|
|
|
query := `
|
2023-02-18 20:53:51 +00:00
|
|
|
INSERT INTO guild (dcid, mxid, plain_name, name, name_set, avatar, avatar_url, avatar_set, bridging_mode)
|
2022-07-08 17:48:36 +00:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
2022-05-28 20:03:24 +00:00
|
|
|
`
|
2023-02-18 20:53:51 +00:00
|
|
|
_, err := g.db.Exec(query, g.ID, g.mxidPtr(), g.PlainName, g.Name, g.NameSet, g.Avatar, g.AvatarURL.String(), g.AvatarSet, g.BridgingMode)
|
2022-05-28 20:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
g.log.Warnfln("Failed to insert %s: %v", g.ID, err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 12:10:16 +00:00
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (g *Guild) Update() {
|
|
|
|
query := `
|
2023-02-18 20:53:51 +00:00
|
|
|
UPDATE guild SET mxid=$1, plain_name=$2, name=$3, name_set=$4, avatar=$5, avatar_url=$6, avatar_set=$7, bridging_mode=$8
|
2022-07-08 17:48:36 +00:00
|
|
|
WHERE dcid=$9
|
2022-05-28 20:03:24 +00:00
|
|
|
`
|
2023-02-18 20:53:51 +00:00
|
|
|
_, err := g.db.Exec(query, g.mxidPtr(), g.PlainName, g.Name, g.NameSet, g.Avatar, g.AvatarURL.String(), g.AvatarSet, g.BridgingMode, g.ID)
|
2022-03-29 12:10:16 +00:00
|
|
|
if err != nil {
|
2022-05-28 20:03:24 +00:00
|
|
|
g.log.Warnfln("Failed to update %s: %v", g.ID, err)
|
|
|
|
panic(err)
|
2022-03-29 12:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Guild) Delete() {
|
2022-05-28 20:03:24 +00:00
|
|
|
_, err := g.db.Exec("DELETE FROM guild WHERE dcid=$1", g.ID)
|
2022-03-29 12:10:16 +00:00
|
|
|
if err != nil {
|
2022-05-28 20:03:24 +00:00
|
|
|
g.log.Warnfln("Failed to delete %s: %v", g.ID, err)
|
|
|
|
panic(err)
|
2022-03-29 12:10:16 +00:00
|
|
|
}
|
|
|
|
}
|