easybridge/appservice/db.go
2020-02-16 22:07:41 +01:00

71 lines
1.5 KiB
Go

package appservice
import (
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var db *gorm.DB
func InitDb() error {
var err error
db, err = gorm.Open(config.DbType, config.DbPath)
if err != nil {
return err
}
db.AutoMigrate(&DbUserMap{})
db.Model(&DbUserMap{}).AddIndex("idx_protocol_user", "protocol", "user_id")
db.AutoMigrate(&DbRoomMap{})
db.Model(&DbRoomMap{}).AddIndex("idx_protocol_room", "protocol", "room_id")
db.AutoMigrate(&DbPmRoomMap{})
db.Model(&DbPmRoomMap{}).AddIndex("idx_protocol_user_account_user", "protocol", "user_id", "mx_user_id", "account_name")
return nil
}
// User mapping between protocol user IDs and puppeted matrix ids
type DbUserMap struct {
gorm.Model
Protocol string
UserID connector.UserID
MxUserID string `gorm:"index:mxuserid"`
}
// Room mapping between Matrix rooms and outside rooms
type DbRoomMap struct {
gorm.Model
// Network protocol
Protocol string
// Room id on the bridged network
RoomID connector.RoomID
// Bridged room matrix id
MxRoomID string `gorm:"index:mxroomid"`
}
// Room mapping between Matrix rooms and private messages
type DbPmRoomMap struct {
gorm.Model
// User id and account name of the local end viewed on Matrix
MxUserID string
Protocol string
AccountName string
// User id to reach them
UserID connector.RoomID
// Bridged room for PMs
MxRoomID string `gorm:"index:mxroomoid"`
}