Lost portal.go file contents

a
This commit is contained in:
Karmanyaah Malhotra 2021-04-01 00:15:57 -04:00
parent dba9b05c88
commit 047f0819e4
6 changed files with 201 additions and 1584 deletions

View File

@ -24,7 +24,6 @@ import (
_ "github.com/mattn/go-sqlite3"
log "maunium.net/go/maulogger/v2"
"github.com/karmanyaahm/matrix-groupme-go/database/upgrades"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
@ -37,10 +36,11 @@ type Database struct {
log log.Logger
dialect string
User *UserQuery
Portal *PortalQuery
Puppet *PuppetQuery
Message *MessageQuery
User *UserQuery
Portal *PortalQuery
Puppet *PuppetQuery
Message *MessageQuery
Reaction *ReactionQuery
}
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{
// Logger: logger.Default.LogMode(logger.Info),
// Logger: baseLog,
DisableForeignKeyConstraintWhenMigrating: true,
NamingStrategy: schema.NamingStrategy{
NameReplacer: strings.NewReplacer("JID", "Jid", "MXID", "Mxid"),
},
@ -87,16 +89,17 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
db: db,
log: db.log.Sub("Message"),
}
db.Reaction = &ReactionQuery{
db: db,
log: db.log.Sub("Reaction"),
}
return db, nil
}
func (db *Database) Init() error {
println("actual upgrade")
err := db.AutoMigrate(&Portal{})
if err != nil {
return err
}
err = db.AutoMigrate(&Puppet{})
err := db.AutoMigrate(&Portal{}, &Puppet{})
if err != nil {
return err
}
@ -105,12 +108,12 @@ func (db *Database) Init() error {
return err
}
err = db.AutoMigrate(&mxRegistered{})
err = db.AutoMigrate(&Reaction{})
if err != nil {
return err
}
err = db.AutoMigrate(&mxUserProfile{})
err = db.AutoMigrate(&mxRegistered{}, &mxUserProfile{})
if err != nil {
return err
}
@ -123,7 +126,7 @@ func (db *Database) Init() error {
if err != nil {
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 {

View File

@ -76,8 +76,8 @@ type Message struct {
db *Database
log log.Logger
Chat PortalKey `gorm:"primaryKey;embedded;embeddedPrefix:chat_"`
JID types.GroupMeID `gorm:"primaryKey"`
Chat PortalKey `gorm:"embedded;embeddedPrefix:chat_"`
JID types.GroupMeID `gorm:"primaryKey;unique;notNull"`
MXID id.EventID `gorm:"primaryKey;unique;notNull"`
Sender types.GroupMeID `gorm:"notNull"`
Timestamp uint64 `gorm:"notNull;default:0"`

73
database/reactions.go Normal file
View 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)
}
}

BIN
output.go Normal file

Binary file not shown.

1675
portal.go

File diff suppressed because it is too large Load Diff

View File

@ -879,6 +879,10 @@ func (user *User) HandleTextMessage(message groupme.Message) {
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) {
user.HandleChatList()
//TODO: efficient