This commit is contained in:
Alex 2020-02-16 22:57:30 +01:00
parent d2ccd6763a
commit a11be80cf0
5 changed files with 113 additions and 24 deletions

View file

@ -84,23 +84,32 @@ func (a *Account) Event(event *Event) {
if err != nil { if err != nil {
log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id) log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id)
} }
} else if event.Type == EVENT_MESSAGE { } else {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
mx_room_id := ""
if len(event.Room) > 0 { if len(event.Room) > 0 {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room) mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil { if err != nil {
return return
} }
err = mxSendMessageAs(mx_room_id, event.Text, mx_user_id)
if err != nil {
log.Printf("Could not send %s as %s", event.Text, mx_user_id)
}
} else { } else {
// TODO mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName)
if err != nil {
return
}
}
typ := "m.text"
if event.Type == EVENT_ACTION {
typ = "m.emote"
}
err = mxSendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
if err != nil {
log.Printf("Could not send %s as %s", event.Text, mx_user_id)
} }
} }
// TODO
} }
// ---- // ----
@ -142,6 +151,44 @@ func dbGetMxRoom(protocol string, roomId RoomID) (string, error) {
return room.MxRoomID, nil return room.MxRoomID, nil
} }
func dbGetMxPmRoom(protocol string, them 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 UserID) (string, error) { func dbGetMxUser(protocol string, userId UserID) (string, error) {
var user DbUserMap var user DbUserMap

View file

@ -63,7 +63,7 @@ type DbPmRoomMap struct {
AccountName string AccountName string
// User id to reach them // User id to reach them
UserID connector.RoomID UserID connector.UserID
// Bridged room for PMs // Bridged room for PMs
MxRoomID string `gorm:"index:mxroomoid"` MxRoomID string `gorm:"index:mxroomoid"`

View file

@ -135,6 +135,9 @@ func mxCreateRoom(name string, alias string, invite []string) (string, error) {
CreationContent: map[string]interface{} { CreationContent: map[string]interface{} {
"m.federate": false, "m.federate": false,
}, },
PowerLevels: map[string]interface{} {
"invite": 100,
},
} }
var rep CreateRoomResponse var rep CreateRoomResponse
err := mxPostApiCall("/_matrix/client/r0/createRoom", &rq, &rep) err := mxPostApiCall("/_matrix/client/r0/createRoom", &rq, &rep)
@ -144,6 +147,28 @@ func mxCreateRoom(name string, alias string, invite []string) (string, error) {
return rep.RoomId, nil return rep.RoomId, nil
} }
func mxCreateDirectRoomAs(name string, invite []string, as_user string) (string, error) {
rq := CreateRoomNoAliasRequest{
Preset: "private_chat",
Name: name,
Topic: "",
Invite: invite,
CreationContent: map[string]interface{} {
"m.federate": false,
},
PowerLevels: map[string]interface{} {
"invite": 100,
},
IsDirect: true,
}
var rep CreateRoomResponse
err := mxPostApiCall("/_matrix/client/r0/createRoom?user_id=" + url.QueryEscape(as_user), &rq, &rep)
if err != nil {
return "", err
}
return rep.RoomId, nil
}
func mxRoomInvite(room string, user string) error { func mxRoomInvite(room string, user string) error {
rq := RoomInviteRequest{ rq := RoomInviteRequest{
UserId: user, UserId: user,
@ -177,10 +202,10 @@ func mxRoomLeaveAs(room string, user string) error {
return err return err
} }
func mxSendMessageAs(room string, body string, user string) error { func mxSendMessageAs(room string, typ string, body string, user string) error {
txn_id := time.Now().UnixNano() txn_id := time.Now().UnixNano()
rq := RoomSendRequest{ rq := RoomSendMessageRequest{
MsgType: "m.text", MsgType: typ,
Body: body, Body: body,
} }
var rep RoomSendResponse var rep RoomSendResponse

View file

@ -62,13 +62,18 @@ func checkTokenAndLog(handler http.Handler) http.Handler {
} }
func handleTxn(w http.ResponseWriter, r *http.Request) { func handleTxn(w http.ResponseWriter, r *http.Request) {
var txn mxlib.Transaction if r.Method == "PUT" {
err := json.NewDecoder(r.Body).Decode(&txn) var txn mxlib.Transaction
if err != nil { err := json.NewDecoder(r.Body).Decode(&txn)
http.Error(w, err.Error(), http.StatusBadRequest) if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("JSON decode error: %s\n", err)
return
}
log.Printf("Got transaction %#v\n", txn)
fmt.Fprintf(w, "{}\n")
} else {
http.Error(w, "Expected PUT request", http.StatusBadRequest)
} }
log.Printf("Got transaction %#v\n", txn)
fmt.Fprintf(w, "{}\n")
} }

View file

@ -18,7 +18,7 @@ type Transaction struct {
} }
type Event struct { type Event struct {
Content map[string]string `json:"content"` Content map[string]interface{} `json:"content"`
Type string `json:"type"` Type string `json:"type"`
EventId string `json:"event_id"` EventId string `json:"event_id"`
RoomId string `json:"room_id"` RoomId string `json:"room_id"`
@ -47,6 +47,17 @@ type CreateRoomRequest struct {
Topic string `json:"topic"` Topic string `json:"topic"`
Invite []string `json:"invite"` Invite []string `json:"invite"`
CreationContent map[string]interface{} `json:"creation_content"` CreationContent map[string]interface{} `json:"creation_content"`
PowerLevels map[string]interface{} `json:"power_level_content_override"`
}
type CreateRoomNoAliasRequest struct {
Preset string `json:"preset"`
Name string `json:"name"`
Topic string `json:"topic"`
Invite []string `json:"invite"`
CreationContent map[string]interface{} `json:"creation_content"`
PowerLevels map[string]interface{} `json:"power_level_content_override"`
IsDirect bool `json:"is_direct"`
} }
type CreateRoomResponse struct { type CreateRoomResponse struct {
@ -70,10 +81,11 @@ type RoomJoinResponse struct {
RoomId string `json:"room_id"` RoomId string `json:"room_id"`
} }
type RoomSendRequest struct { type RoomSendMessageRequest struct {
MsgType string `json:"msgtype"` MsgType string `json:"msgtype"`
Body string `json:"body"` Body string `json:"body"`
} }
type RoomSendResponse struct { type RoomSendResponse struct {
EventId string `json:"event_id"` EventId string `json:"event_id"`
} }