This commit also points to my fork of discordgo which makes it look like the official client which is the only way to get the actually contents of a dm when not authorized as a bot.
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
log "maunium.net/go/maulogger/v2"
|
|
"maunium.net/go/mautrix/id"
|
|
)
|
|
|
|
type PortalQuery struct {
|
|
db *Database
|
|
log log.Logger
|
|
}
|
|
|
|
func (pq *PortalQuery) New() *Portal {
|
|
return &Portal{
|
|
db: pq.db,
|
|
log: pq.log,
|
|
}
|
|
}
|
|
|
|
func (pq *PortalQuery) GetAll() []*Portal {
|
|
return pq.getAll("SELECT * FROM portal")
|
|
}
|
|
|
|
func (pq *PortalQuery) GetByID(key PortalKey) *Portal {
|
|
return pq.get("SELECT * FROM portal WHERE id=$1 AND channel_id=$2", key.ID, key.ChannelID)
|
|
}
|
|
|
|
func (pq *PortalQuery) GetByMXID(mxid id.RoomID) *Portal {
|
|
return pq.get("SELECT * FROM portal WHERE mxid=$1", mxid)
|
|
}
|
|
|
|
func (pq *PortalQuery) GetAllByDID(did string) []*Portal {
|
|
return pq.getAll("SELECT * FROM portal WHERE id=$1", did)
|
|
}
|
|
|
|
func (pq *PortalQuery) getAll(query string, args ...interface{}) []*Portal {
|
|
rows, err := pq.db.Query(query, args...)
|
|
if err != nil || rows == nil {
|
|
return nil
|
|
}
|
|
defer rows.Close()
|
|
|
|
portals := []*Portal{}
|
|
for rows.Next() {
|
|
portals = append(portals, pq.New().Scan(rows))
|
|
}
|
|
|
|
return portals
|
|
}
|
|
|
|
func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
|
|
row := pq.db.QueryRow(query, args...)
|
|
if row == nil {
|
|
return nil
|
|
}
|
|
|
|
return pq.New().Scan(row)
|
|
}
|