Add support for deleting messages from discord
This commit is contained in:
@@ -9,6 +9,9 @@ CREATE TABLE portal (
|
||||
avatar TEXT NOT NULL,
|
||||
avatar_url TEXT,
|
||||
|
||||
type INT,
|
||||
dmuser TEXT,
|
||||
|
||||
first_event_id TEXT,
|
||||
|
||||
PRIMARY KEY (channel_id, receiver)
|
||||
|
||||
@@ -3,6 +3,8 @@ package database
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
@@ -20,7 +22,7 @@ type Portal struct {
|
||||
Avatar string
|
||||
AvatarURL id.ContentURI
|
||||
|
||||
Type int
|
||||
Type discordgo.ChannelType
|
||||
DMUser string
|
||||
|
||||
FirstEventID id.EventID
|
||||
@@ -28,8 +30,11 @@ type Portal struct {
|
||||
|
||||
func (p *Portal) Scan(row Scannable) *Portal {
|
||||
var mxid, avatarURL, firstEventID sql.NullString
|
||||
var typ sql.NullInt32
|
||||
|
||||
err := row.Scan(&p.Key.ChannelID, &p.Key.Receiver, &mxid, &p.Name,
|
||||
&p.Topic, &p.Avatar, &avatarURL, &typ, &p.DMUser, &firstEventID)
|
||||
|
||||
err := row.Scan(&p.Key.ChannelID, &p.Key.Receiver, &mxid, &p.Name, &p.Topic, &p.Avatar, &avatarURL, &firstEventID)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
p.log.Errorln("Database scan failed:", err)
|
||||
@@ -40,6 +45,7 @@ func (p *Portal) Scan(row Scannable) *Portal {
|
||||
|
||||
p.MXID = id.RoomID(mxid.String)
|
||||
p.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
|
||||
p.Type = discordgo.ChannelType(typ.Int32)
|
||||
p.FirstEventID = id.EventID(firstEventID.String)
|
||||
|
||||
return p
|
||||
@@ -47,11 +53,13 @@ func (p *Portal) Scan(row Scannable) *Portal {
|
||||
|
||||
func (p *Portal) Insert() {
|
||||
query := "INSERT INTO portal" +
|
||||
" (channel_id, receiver, mxid, name, topic, avatar, avatar_url, first_event_id)" +
|
||||
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
|
||||
" (channel_id, receiver, mxid, name, topic, avatar, avatar_url," +
|
||||
" type, dmuser, first_event_id)" +
|
||||
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)"
|
||||
|
||||
_, err := p.db.Exec(query, p.Key.ChannelID, p.Key.Receiver, p.MXID,
|
||||
p.Name, p.Topic, p.Avatar, p.AvatarURL.String(), p.FirstEventID.String())
|
||||
p.Name, p.Topic, p.Avatar, p.AvatarURL.String(), p.Type, p.DMUser,
|
||||
p.FirstEventID.String())
|
||||
|
||||
if err != nil {
|
||||
p.log.Warnfln("Failed to insert %s: %v", p.Key, err)
|
||||
@@ -60,12 +68,13 @@ func (p *Portal) Insert() {
|
||||
|
||||
func (p *Portal) Update() {
|
||||
query := "UPDATE portal SET" +
|
||||
" mxid=$1, name=$2, topic=$3, avatar=$4, avatar_url=$5, first_event_id=$6" +
|
||||
" WHERE channel_id=$7 AND receiver=$8"
|
||||
" mxid=$1, name=$2, topic=$3, avatar=$4, avatar_url=$5, type=$6," +
|
||||
" dmuser=$7, first_event_id=$8" +
|
||||
" WHERE channel_id=$9 AND receiver=$10"
|
||||
|
||||
_, err := p.db.Exec(query, p.MXID, p.Name, p.Topic, p.Avatar,
|
||||
p.AvatarURL.String(), p.FirstEventID.String(), p.Key.ChannelID,
|
||||
p.Key.Receiver)
|
||||
p.AvatarURL.String(), p.Type, p.DMUser, p.FirstEventID.String(),
|
||||
p.Key.ChannelID, p.Key.Receiver)
|
||||
|
||||
if err != nil {
|
||||
p.log.Warnfln("Failed to update %s: %v", p.Key, err)
|
||||
|
||||
Reference in New Issue
Block a user