groupme/database/message.go

137 lines
3.9 KiB
Go
Raw Normal View History

// mautrix-groupme - A Matrix-GroupMe puppeting bridge.
// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package database
import (
2019-01-11 19:17:31 +00:00
log "maunium.net/go/maulogger/v2"
2020-05-08 19:32:22 +00:00
"maunium.net/go/mautrix/id"
"github.com/beeper/groupme/groupmeExt"
"github.com/beeper/groupme/types"
)
type MessageQuery struct {
db *Database
log log.Logger
}
func (mq *MessageQuery) New() *Message {
return &Message{
db: mq.db,
log: mq.log,
}
}
func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
2021-02-13 05:53:35 +00:00
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Find(&messages)
if ans.Error != nil || len(messages) == 0 {
return nil
}
return
}
func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message {
2021-02-13 05:53:35 +00:00
var message Message
2021-02-21 05:58:50 +00:00
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ? AND jid = ?", chat.JID, chat.Receiver, jid).Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
2021-02-13 05:53:35 +00:00
return nil
}
return &message
}
2020-05-08 19:32:22 +00:00
func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
2021-02-13 05:53:35 +00:00
var message Message
2021-02-21 05:58:50 +00:00
ans := mq.db.Where("mxid = ?", mxid).Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
2021-02-13 05:53:35 +00:00
return &message
}
2021-02-13 05:53:35 +00:00
func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
var message Message
2021-02-21 05:58:50 +00:00
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Order("timestamp desc").Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
2021-02-13 05:53:35 +00:00
return &message
}
type Message struct {
db *Database
log log.Logger
2021-04-01 04:15:57 +00:00
Chat PortalKey `gorm:"embedded;embeddedPrefix:chat_"`
JID types.GroupMeID `gorm:"primaryKey;unique;notNull"`
2021-03-15 18:29:06 +00:00
MXID id.EventID `gorm:"primaryKey;unique;notNull"`
2021-02-22 03:46:17 +00:00
Sender types.GroupMeID `gorm:"notNull"`
Timestamp uint64 `gorm:"notNull;default:0"`
Content *groupmeExt.Message `gorm:"type:TEXT;notNull"`
2021-02-28 20:02:11 +00:00
Portal Portal `gorm:"foreignKey:chat_jid,chat_receiver;references:jid,receiver;constraint:onDelete:CASCADE;"`
}
2021-02-13 05:53:35 +00:00
// func (msg *Message) Scan(row Scannable) *Message {
// var content []byte
// err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.Timestamp, &content)
// if err != nil {
// if err != sql.ErrNoRows {
// msg.log.Errorln("Database scan failed:", err)
// }
// return nil
// }
// msg.decodeBinaryContent(content)
// return msg
// }
// func (msg *Message) decodeBinaryContent(content []byte) {
// msg.Content = &waProto.Message{}
// reader := bytes.NewReader(content)
// dec := json.NewDecoder(reader)
// err := dec.Decode(msg.Content)
// if err != nil {
// msg.log.Warnln("Failed to decode message content:", err)
// }
// }
// func (msg *Message) encodeBinaryContent() []byte {
// var buf bytes.Buffer
// enc := json.NewEncoder(&buf)
// err := enc.Encode(msg.Content)
// if err != nil {
// msg.log.Warnln("Failed to encode message content:", err)
// }
// return buf.Bytes()
// }
2019-01-21 21:55:16 +00:00
func (msg *Message) Insert() {
2021-02-13 05:53:35 +00:00
ans := msg.db.Create(&msg)
if ans.Error != nil {
2021-02-22 03:46:17 +00:00
msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, ans.Error)
}
}
func (msg *Message) Delete() {
2021-02-13 05:53:35 +00:00
ans := msg.db.Delete(&msg)
if ans.Error != nil {
2021-02-22 03:46:17 +00:00
msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, ans.Error)
}
}