pygomx: dm support
This commit is contained in:
parent
d657fea159
commit
4c9ba7dcdb
7 changed files with 121 additions and 3 deletions
|
|
@ -87,6 +87,19 @@ func (mxc *MXClient) _storeDirectMap() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (mxc *MXClient) GetUserDM(mxid string) []string {
|
||||
var res = make([]string, 0)
|
||||
|
||||
for room, uids := range mxc._directMap {
|
||||
for _, uid := range uids {
|
||||
if uid.String() == mxid {
|
||||
res = append(res, room.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (mxc *MXClient) _onEventMember(ctx context.Context, evt *event.Event) {
|
||||
if evt.GetStateKey() == mxc.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
||||
if evt.Content.AsMember().IsDirect {
|
||||
|
|
@ -108,8 +121,18 @@ func (mxc *MXClient) _onEventMember(ctx context.Context, evt *event.Event) {
|
|||
Str("inviter", evt.Sender.String()).
|
||||
Msg("Failed to join room after invite")
|
||||
}
|
||||
} else if evt.Content.AsMember().Membership == event.MembershipJoin {
|
||||
out, err := json.Marshal(evt)
|
||||
if err != nil {
|
||||
log.Error().Err(err).
|
||||
Str("id", evt.ID.String()).
|
||||
Str("joiner", evt.Sender.String()).
|
||||
Msg("Marshalling error")
|
||||
return
|
||||
}
|
||||
mxc.OnEvent(string(out))
|
||||
} else {
|
||||
fmt.Printf("\nGot member event: %#v\n", evt)
|
||||
fmt.Printf("\nGot member event: %s\n%#v\n", evt.GetStateKey(), evt)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -161,6 +184,23 @@ func (mxc *MXClient) LeaveRoomAndForget(ctx context.Context, room id.RoomID) err
|
|||
return nil
|
||||
}
|
||||
|
||||
func (mxc *MXClient) CreateDM(ctx context.Context, uid id.UserID) (resp *mautrix.RespCreateRoom, err error) {
|
||||
req := mautrix.ReqCreateRoom{
|
||||
IsDirect: true,
|
||||
Preset: "trusted_private_chat",
|
||||
Invite: []id.UserID{uid},
|
||||
}
|
||||
|
||||
resp, err = mxc.CreateRoom(context.Background(), &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mxc.AddDirectRoom(uid, resp.RoomID)
|
||||
err = mxc._storeDirectMap()
|
||||
return
|
||||
}
|
||||
|
||||
type sendmessage_data struct {
|
||||
RoomId id.RoomID `json:"roomid"`
|
||||
Type event.Type `json:"type"`
|
||||
|
|
|
|||
|
|
@ -435,7 +435,54 @@ func apiv0_createroom(cid C.int, data *C.char) *C.char {
|
|||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
||||
out, err := json.Marshal(resp)
|
||||
type roomResult struct {
|
||||
RoomId id.RoomID `json:"roomid"`
|
||||
IsDirect bool `json:"is_direct"`
|
||||
}
|
||||
|
||||
out, err := json.Marshal(roomResult{RoomId: resp.RoomID, IsDirect: false})
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
s := string(out)
|
||||
return C.CString(s)
|
||||
}
|
||||
|
||||
//export apiv0_createdm
|
||||
func apiv0_createdm(cid C.int, uid *C.char) *C.char {
|
||||
cli, err := getClient(int(cid))
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
||||
mxid := C.GoString(uid)
|
||||
|
||||
resp, err := cli.CreateDM(context.Background(), id.UserID(mxid))
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
||||
type roomResult struct {
|
||||
RoomId id.RoomID `json:"roomid"`
|
||||
IsDirect bool `json:"is_direct"`
|
||||
}
|
||||
|
||||
out, err := json.Marshal(roomResult{RoomId: resp.RoomID, IsDirect: true})
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
s := string(out)
|
||||
return C.CString(s)
|
||||
}
|
||||
|
||||
//export apiv0_getuserdm
|
||||
func apiv0_getuserdm(cid C.int, userid *C.char) *C.char {
|
||||
cli, err := getClient(int(cid))
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
list := cli.GetUserDM(C.GoString(userid))
|
||||
out, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue