aboutsummaryrefslogtreecommitdiff
path: root/appservice/db.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-16 23:27:03 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-16 23:27:03 +0100
commitc3b941841e193c1d5c32f9d6226a95475d627249 (patch)
treeb3ac238623f6cafa08a063e886d97806cfe836bc /appservice/db.go
parenta11be80cf0c13263791f3e4f82fda461acd77130 (diff)
downloadeasybridge-c3b941841e193c1d5c32f9d6226a95475d627249.tar.gz
easybridge-c3b941841e193c1d5c32f9d6226a95475d627249.zip
Basic bridging going on both ways successfully
Diffstat (limited to 'appservice/db.go')
-rw-r--r--appservice/db.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/appservice/db.go b/appservice/db.go
index c2af428..fad78ba 100644
--- a/appservice/db.go
+++ b/appservice/db.go
@@ -1,7 +1,12 @@
package appservice
import (
+ "fmt"
+ "log"
+
+ "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
+
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
@@ -69,3 +74,111 @@ type DbPmRoomMap struct {
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,
+ RoomID: roomId,
+ }).RecordNotFound()
+ if must_create {
+ alias := roomAlias(protocol, roomId)
+ // Lookup alias
+ mx_room_id, err := mxDirectoryRoom(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 = mxCreateRoom(name, alias, []string{})
+ if err != nil {
+ log.Printf("Could not create room for %s: %s", name, err)
+ return "", err
+ }
+ }
+
+ room = DbRoomMap{
+ Protocol: protocol,
+ RoomID: roomId,
+ MxRoomID: mx_room_id,
+ }
+ db.Create(&room)
+ }
+ log.Printf("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{
+ MxUserID: usMxId,
+ Protocol: protocol,
+ AccountName: usAccount,
+ UserID: them,
+ }).RecordNotFound()
+ if must_create {
+ name := fmt.Sprintf("%s (%s)", them, protocol)
+
+ mx_room_id, err := mxCreateDirectRoomAs(name, []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{
+ MxUserID: usMxId,
+ Protocol: protocol,
+ AccountName: usAccount,
+ UserID: them,
+ MxRoomID: mx_room_id,
+ }
+ db.Create(&room)
+ }
+ log.Printf("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,
+ UserID: userId,
+ }).RecordNotFound()
+ if must_create {
+ username := userMxId(protocol, userId)
+
+ err := mxRegisterUser(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)
+ mxProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))
+
+ user = DbUserMap{
+ Protocol: protocol,
+ UserID: userId,
+ MxUserID: mxid,
+ }
+ db.Create(&user)
+ }
+
+ return user.MxUserID, nil
+}