Lost portal.go file contents
a
This commit is contained in:
parent
dba9b05c88
commit
047f0819e4
@ -24,7 +24,6 @@ import (
|
|||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
log "maunium.net/go/maulogger/v2"
|
log "maunium.net/go/maulogger/v2"
|
||||||
"github.com/karmanyaahm/matrix-groupme-go/database/upgrades"
|
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
@ -37,10 +36,11 @@ type Database struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
dialect string
|
dialect string
|
||||||
|
|
||||||
User *UserQuery
|
User *UserQuery
|
||||||
Portal *PortalQuery
|
Portal *PortalQuery
|
||||||
Puppet *PuppetQuery
|
Puppet *PuppetQuery
|
||||||
Message *MessageQuery
|
Message *MessageQuery
|
||||||
|
Reaction *ReactionQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
|
func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
|
||||||
@ -59,6 +59,8 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
|
|||||||
gdb, err := gorm.Open(conn, &gorm.Config{
|
gdb, err := gorm.Open(conn, &gorm.Config{
|
||||||
// Logger: logger.Default.LogMode(logger.Info),
|
// Logger: logger.Default.LogMode(logger.Info),
|
||||||
// Logger: baseLog,
|
// Logger: baseLog,
|
||||||
|
|
||||||
|
DisableForeignKeyConstraintWhenMigrating: true,
|
||||||
NamingStrategy: schema.NamingStrategy{
|
NamingStrategy: schema.NamingStrategy{
|
||||||
NameReplacer: strings.NewReplacer("JID", "Jid", "MXID", "Mxid"),
|
NameReplacer: strings.NewReplacer("JID", "Jid", "MXID", "Mxid"),
|
||||||
},
|
},
|
||||||
@ -87,16 +89,17 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
|
|||||||
db: db,
|
db: db,
|
||||||
log: db.log.Sub("Message"),
|
log: db.log.Sub("Message"),
|
||||||
}
|
}
|
||||||
|
db.Reaction = &ReactionQuery{
|
||||||
|
db: db,
|
||||||
|
log: db.log.Sub("Reaction"),
|
||||||
|
}
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) Init() error {
|
func (db *Database) Init() error {
|
||||||
println("actual upgrade")
|
println("actual upgrade")
|
||||||
err := db.AutoMigrate(&Portal{})
|
err := db.AutoMigrate(&Portal{}, &Puppet{})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = db.AutoMigrate(&Puppet{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -105,12 +108,12 @@ func (db *Database) Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.AutoMigrate(&mxRegistered{})
|
err = db.AutoMigrate(&Reaction{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.AutoMigrate(&mxUserProfile{})
|
err = db.AutoMigrate(&mxRegistered{}, &mxUserProfile{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -123,7 +126,7 @@ func (db *Database) Init() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
|
return nil //upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Scannable interface {
|
type Scannable interface {
|
||||||
|
@ -76,8 +76,8 @@ type Message struct {
|
|||||||
db *Database
|
db *Database
|
||||||
log log.Logger
|
log log.Logger
|
||||||
|
|
||||||
Chat PortalKey `gorm:"primaryKey;embedded;embeddedPrefix:chat_"`
|
Chat PortalKey `gorm:"embedded;embeddedPrefix:chat_"`
|
||||||
JID types.GroupMeID `gorm:"primaryKey"`
|
JID types.GroupMeID `gorm:"primaryKey;unique;notNull"`
|
||||||
MXID id.EventID `gorm:"primaryKey;unique;notNull"`
|
MXID id.EventID `gorm:"primaryKey;unique;notNull"`
|
||||||
Sender types.GroupMeID `gorm:"notNull"`
|
Sender types.GroupMeID `gorm:"notNull"`
|
||||||
Timestamp uint64 `gorm:"notNull;default:0"`
|
Timestamp uint64 `gorm:"notNull;default:0"`
|
||||||
|
73
database/reactions.go
Normal file
73
database/reactions.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/karmanyaahm/matrix-groupme-go/types"
|
||||||
|
log "maunium.net/go/maulogger/v2"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ReactionQuery struct {
|
||||||
|
db *Database
|
||||||
|
log log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mq *ReactionQuery) New() *Reaction {
|
||||||
|
return &Reaction{
|
||||||
|
db: mq.db,
|
||||||
|
log: mq.log,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mq *ReactionQuery) GetByJID(jid types.GroupMeID) (reactions []*Reaction) {
|
||||||
|
ans := mq.db.Model(&Reaction{}).
|
||||||
|
Joins("Users"). // TODO: Do this in seperate function?
|
||||||
|
Where("message_jid = ?", jid).
|
||||||
|
Limit(1).Find(&reactions)
|
||||||
|
|
||||||
|
if ans.Error != nil || ans.RowsAffected == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, reaction := range reactions {
|
||||||
|
reaction.db = mq.db
|
||||||
|
reaction.log = mq.log
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans := mq.db.Model(&Reaction{}).
|
||||||
|
// Joins("INNER JOIN users on users.mxid = reactions.user_mxid").
|
||||||
|
// Where("reactions.message_jid = ? AND users.jid = ?", jid, uid).
|
||||||
|
// Limit(1).Find(&reactions)
|
||||||
|
|
||||||
|
type Reaction struct {
|
||||||
|
db *Database
|
||||||
|
log log.Logger
|
||||||
|
|
||||||
|
MXID id.EventID `gorm:"primaryKey"`
|
||||||
|
|
||||||
|
//Message
|
||||||
|
MessageJID types.GroupMeID `gorm:"notNull"`
|
||||||
|
MessageMXID id.EventID `gorm:"notNull"`
|
||||||
|
|
||||||
|
Message Message `gorm:"foreignKey:MessageMXID,MessageJID;references:MXID,JID;"`
|
||||||
|
|
||||||
|
//User
|
||||||
|
UserMXID id.UserID `gorm:"notNull"`
|
||||||
|
User User `gorm:"foreignKey:UserMXID;references:MXID;"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (reaction *Reaction) Insert() {
|
||||||
|
ans := reaction.db.Create(&reaction)
|
||||||
|
if ans.Error != nil {
|
||||||
|
reaction.log.Warnfln("Failed to insert %s@%s: %v", reaction.MXID, reaction.MessageJID, ans.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (reaction *Reaction) Delete() {
|
||||||
|
ans := reaction.db.Delete(&reaction)
|
||||||
|
if ans.Error != nil {
|
||||||
|
reaction.log.Warnfln("Failed to insert %s@%s: %v", reaction.MXID, reaction.MessageJID, ans.Error)
|
||||||
|
}
|
||||||
|
}
|
4
user.go
4
user.go
@ -879,6 +879,10 @@ func (user *User) HandleTextMessage(message groupme.Message) {
|
|||||||
user.messageInput <- PortalMessage{id, group, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
|
user.messageInput <- PortalMessage{id, group, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (user *User) HandleLike(msg groupme.Message) {
|
||||||
|
user.HandleTextMessage(msg)
|
||||||
|
}
|
||||||
|
|
||||||
func (user *User) HandleJoin(id groupme.ID) {
|
func (user *User) HandleJoin(id groupme.ID) {
|
||||||
user.HandleChatList()
|
user.HandleChatList()
|
||||||
//TODO: efficient
|
//TODO: efficient
|
||||||
|
Loading…
Reference in New Issue
Block a user