easybridge/appservice/db.go

203 lines
4.6 KiB
Go
Raw Normal View History

2020-02-16 18:30:49 +00:00
package appservice
import (
"fmt"
2020-02-16 18:30:49 +00:00
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2020-02-17 18:02:26 +00:00
log "github.com/sirupsen/logrus"
2020-02-17 08:41:08 +00:00
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
2020-02-17 18:02:26 +00:00
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
2020-02-16 18:30:49 +00:00
)
var db *gorm.DB
func InitDb() error {
var err error
db, err = gorm.Open(config.DbType, config.DbPath)
if err != nil {
return err
}
2020-02-16 21:07:41 +00:00
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")
2020-02-16 18:30:49 +00:00
return nil
}
2020-02-16 21:07:41 +00:00
// User mapping between protocol user IDs and puppeted matrix ids
type DbUserMap struct {
gorm.Model
Protocol string
2020-02-17 18:02:26 +00:00
UserID connector.UserID
2020-02-16 21:07:41 +00:00
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
2020-02-17 18:02:26 +00:00
MxUserID string
Protocol string
2020-02-16 21:07:41 +00:00
AccountName string
// User id to reach them
2020-02-16 21:57:30 +00:00
UserID connector.UserID
2020-02-16 21:07:41 +00:00
// Bridged room for PMs
MxRoomID string `gorm:"index:mxroomoid"`
}
// ----
func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
var room DbRoomMap
// Check if room exists in our mapping,
// If not create it
must_create := db.First(&room, DbRoomMap{
Protocol: protocol,
2020-02-17 18:02:26 +00:00
RoomID: roomId,
}).RecordNotFound()
if must_create {
alias := roomAlias(protocol, roomId)
// Lookup alias
mx_room_id, err := mx.DirectoryRoom(fmt.Sprintf("#%s:%s", alias, config.MatrixDomain))
// If no alias found, create room
if err != nil {
name := fmt.Sprintf("%s (%s)", roomId, protocol)
mx_room_id, err = mx.CreateRoom(name, alias, []string{})
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
return "", err
}
}
room = DbRoomMap{
Protocol: protocol,
2020-02-17 18:02:26 +00:00
RoomID: roomId,
MxRoomID: mx_room_id,
}
db.Create(&room)
}
2020-02-17 18:02:00 +00:00
log.Debugf("Got room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}
func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMxId string, usAccount string) (string, error) {
var room DbPmRoomMap
must_create := db.First(&room, DbPmRoomMap{
2020-02-17 18:02:26 +00:00
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
2020-02-17 18:02:26 +00:00
UserID: them,
}).RecordNotFound()
if must_create {
name := fmt.Sprintf("%s (%s)", them, protocol)
mx_room_id, err := mx.CreateDirectRoomAs([]string{usMxId}, themMxId)
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
return "", err
}
//err = mxRoomJoinAs(mx_room_id, themMxId)
//if err != nil {
// log.Printf("Could not join %s as %s", mx_room_id, themMxId)
// return "", err
//}
room = DbPmRoomMap{
2020-02-17 18:02:26 +00:00
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
2020-02-17 18:02:26 +00:00
UserID: them,
MxRoomID: mx_room_id,
}
db.Create(&room)
}
2020-02-17 18:02:00 +00:00
log.Debugf("Got PM room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}
func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
var user DbUserMap
must_create := db.First(&user, DbUserMap{
Protocol: protocol,
2020-02-17 18:02:26 +00:00
UserID: userId,
}).RecordNotFound()
if must_create {
username := userMxId(protocol, userId)
err := mx.RegisterUser(username)
if err != nil {
if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
log.Printf("Could not register %s: %s", username, err)
return "", err
}
}
mxid := fmt.Sprintf("@%s:%s", username, config.MatrixDomain)
mx.ProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))
user = DbUserMap{
Protocol: protocol,
2020-02-17 18:02:26 +00:00
UserID: userId,
MxUserID: mxid,
}
db.Create(&user)
}
return user.MxUserID, nil
}
2020-02-17 14:30:01 +00:00
func dbIsPmRoom(mxRoomId string) *DbPmRoomMap {
var pm_room DbPmRoomMap
if db.First(&pm_room, DbPmRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
return nil
} else {
return &pm_room
}
}
func dbIsPublicRoom(mxRoomId string) *DbRoomMap {
var room DbRoomMap
if db.First(&room, DbRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
return nil
} else {
return &room
}
}