dms kind work now?
This commit is contained in:
parent
23c5e89c4f
commit
96557c9973
@ -37,19 +37,25 @@ type PortalKey struct {
|
|||||||
func ParsePortalKey(inp types.GroupMeID) *PortalKey {
|
func ParsePortalKey(inp types.GroupMeID) *PortalKey {
|
||||||
parts := strings.Split(inp, "+")
|
parts := strings.Split(inp, "+")
|
||||||
|
|
||||||
if len(parts) != 2 {
|
if len(parts) == 1 {
|
||||||
return nil
|
if i, err := strconv.Atoi(inp); i == 0 || err != nil {
|
||||||
}
|
return nil
|
||||||
if i, err := strconv.Atoi(parts[0]); i == 0 || err != nil {
|
}
|
||||||
return nil
|
return &PortalKey{inp, inp}
|
||||||
}
|
} else if len(parts) == 2 {
|
||||||
if i, err := strconv.Atoi(parts[1]); i == 0 || err != nil {
|
if i, err := strconv.Atoi(parts[0]); i == 0 || err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if i, err := strconv.Atoi(parts[1]); i == 0 || err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return &PortalKey{
|
return &PortalKey{
|
||||||
JID: parts[0],
|
JID: parts[1],
|
||||||
Receiver: parts[1],
|
Receiver: parts[0],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ func (c Client) IndexAllChats() ([]*groupme.Chat, error) {
|
|||||||
|
|
||||||
func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) {
|
func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) {
|
||||||
if private {
|
if private {
|
||||||
i, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{
|
ans, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{
|
||||||
SinceID: groupme.ID(lastMessageID),
|
SinceID: groupme.ID(lastMessageID),
|
||||||
//Limit: num,
|
//Limit: num,
|
||||||
})
|
})
|
||||||
@ -45,7 +45,11 @@ func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFrom
|
|||||||
if e != nil {
|
if e != nil {
|
||||||
return nil, e
|
return nil, e
|
||||||
}
|
}
|
||||||
return i.Messages, nil
|
|
||||||
|
for i, j := 0, len(ans.Messages)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
ans.Messages[i], ans.Messages[j] = ans.Messages[j], ans.Messages[i]
|
||||||
|
}
|
||||||
|
return ans.Messages, nil
|
||||||
} else {
|
} else {
|
||||||
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
|
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
|
||||||
AfterID: groupme.ID(lastMessageID),
|
AfterID: groupme.ID(lastMessageID),
|
||||||
|
@ -165,6 +165,8 @@ func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, pu
|
|||||||
func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) {
|
func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) {
|
||||||
portal.MXID = roomID
|
portal.MXID = roomID
|
||||||
portal.Topic = "WhatsApp private chat"
|
portal.Topic = "WhatsApp private chat"
|
||||||
|
portal.Key = database.PortalKey{puppet.JID, inviter.JID}
|
||||||
|
|
||||||
_, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic)
|
_, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic)
|
||||||
if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
|
if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
|
||||||
m, _ := mx.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID)
|
m, _ := mx.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID)
|
||||||
|
62
portal.go
62
portal.go
@ -159,8 +159,7 @@ func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal {
|
|||||||
const recentlyHandledLength = 100
|
const recentlyHandledLength = 100
|
||||||
|
|
||||||
type PortalMessage struct {
|
type PortalMessage struct {
|
||||||
chat string
|
chat database.PortalKey
|
||||||
group bool
|
|
||||||
source *User
|
source *User
|
||||||
data *groupme.Message
|
data *groupme.Message
|
||||||
timestamp uint64
|
timestamp uint64
|
||||||
@ -270,17 +269,15 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI {
|
func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI {
|
||||||
if info.UserID.String() == user.GetJID() { //from me
|
if portal.IsPrivateChat() {
|
||||||
return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal) //TODO why is this
|
if info.UserID.String() == user.GetJID() { //from me
|
||||||
} else if portal.IsPrivateChat() {
|
return portal.bridge.GetPuppetByJID(user.JID).DefaultIntent()
|
||||||
|
}
|
||||||
return portal.MainIntent()
|
return portal.MainIntent()
|
||||||
} else if len(info.UserID.String()) == 0 {
|
} else if len(info.UserID.String()) == 0 {
|
||||||
// if len(info.Source.GetParticipant()) != 0 {
|
|
||||||
// info.SenderJid = info.Source.GetParticipant()
|
|
||||||
// } else {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
println("TODO weird uid stuff")
|
println("TODO weird uid stuff")
|
||||||
|
} else if info.UserID.String() == user.GetJID() { //from me
|
||||||
|
return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal)
|
||||||
}
|
}
|
||||||
return portal.bridge.GetPuppetByJID(info.UserID.String()).IntentFor(portal)
|
return portal.bridge.GetPuppetByJID(info.UserID.String()).IntentFor(portal)
|
||||||
}
|
}
|
||||||
@ -305,7 +302,7 @@ func (portal *Portal) startHandling(source *User, info *groupme.Message) *appser
|
|||||||
if intent != nil {
|
if intent != nil {
|
||||||
portal.log.Debugfln("Starting handling of %s (ts: %d)", info.ID, info.CreatedAt)
|
portal.log.Debugfln("Starting handling of %s (ts: %d)", info.ID, info.CreatedAt)
|
||||||
} else {
|
} else {
|
||||||
portal.log.Debugfln("Not handling %s: sender is not known")
|
portal.log.Debugfln("Not handling %s: sender is not known", info.ID.String())
|
||||||
}
|
}
|
||||||
return intent
|
return intent
|
||||||
}
|
}
|
||||||
@ -742,7 +739,8 @@ func (portal *Portal) beginBackfill() func() {
|
|||||||
portal.privateChatBackfillInvitePuppet = nil
|
portal.privateChatBackfillInvitePuppet = nil
|
||||||
portal.backfillLock.Unlock()
|
portal.backfillLock.Unlock()
|
||||||
if privateChatPuppet != nil && privateChatPuppetInvited {
|
if privateChatPuppet != nil && privateChatPuppetInvited {
|
||||||
_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
|
//_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
|
||||||
|
//why this shouldn't really happen NOTE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -865,7 +863,7 @@ func (portal *Portal) handleHistory(user *User, messages []*groupme.Message) {
|
|||||||
if portal.privateChatBackfillInvitePuppet != nil && message.UserID.String() == user.JID && portal.IsPrivateChat() {
|
if portal.privateChatBackfillInvitePuppet != nil && message.UserID.String() == user.JID && portal.IsPrivateChat() {
|
||||||
portal.privateChatBackfillInvitePuppet()
|
portal.privateChatBackfillInvitePuppet()
|
||||||
}
|
}
|
||||||
portal.handleMessage(PortalMessage{portal.Key.JID, portal.Key.JID == portal.Key.Receiver, user, message, uint64(message.CreatedAt.ToTime().Unix())})
|
portal.handleMessage(PortalMessage{portal.Key, user, message, uint64(message.CreatedAt.ToTime().Unix())})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -941,7 +939,6 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
|
|||||||
portal.log.Infoln("Creating Matrix room. Info source:", user.MXID)
|
portal.log.Infoln("Creating Matrix room. Info source:", user.MXID)
|
||||||
|
|
||||||
var metadata *groupme.Group
|
var metadata *groupme.Group
|
||||||
fmt.Println(portal.IsPrivateChat(), portal.Key, portal.Key.Receiver, portal.Key.JID)
|
|
||||||
return nil
|
return nil
|
||||||
if portal.IsPrivateChat() {
|
if portal.IsPrivateChat() {
|
||||||
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID)
|
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID)
|
||||||
@ -1030,6 +1027,8 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
} else if len(resp.RoomID) == 0 {
|
||||||
|
return errors.New("Empty room ID")
|
||||||
}
|
}
|
||||||
portal.MXID = resp.RoomID
|
portal.MXID = resp.RoomID
|
||||||
portal.Update()
|
portal.Update()
|
||||||
@ -2142,8 +2141,10 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) ([]*g
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
info := groupme.Message{
|
info := groupme.Message{
|
||||||
SourceGUID: evt.ID.String(), //TODO Figure out for multiple messages
|
GroupID: groupme.ID(portal.Key.String()),
|
||||||
GroupID: groupme.ID(portal.Key.JID),
|
ConversationID: groupme.ID(portal.Key.String()),
|
||||||
|
ChatID: groupme.ID(portal.Key.String()),
|
||||||
|
RecipientID: groupme.ID(portal.Key.JID),
|
||||||
}
|
}
|
||||||
replyToID := content.GetReplyTo()
|
replyToID := content.GetReplyTo()
|
||||||
if len(replyToID) > 0 {
|
if len(replyToID) > 0 {
|
||||||
@ -2318,10 +2319,10 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, i := range info {
|
for _, i := range info {
|
||||||
portal.log.Debugln("Sending event", evt.ID, "to WhatsApp", info[0].ID)
|
portal.log.Debugln("Sending event", evt.ID, "to GroupMe", info[0].ID)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
i, err = portal.sendRaw(sender, evt, info[0], false) //TODO deal with multiple messages for longer messages
|
i, err = portal.sendRaw(sender, evt, info[0], -1) //TODO deal with multiple messages for longer messages
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Warnln("Unable to handle message from Matrix", evt.ID)
|
portal.log.Warnln("Unable to handle message from Matrix", evt.ID)
|
||||||
//TODO handle deleted room and such
|
//TODO handle deleted room and such
|
||||||
@ -2333,21 +2334,30 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *groupme.Message, isRetry bool) (*groupme.Message, error) {
|
func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *groupme.Message, retries int) (*groupme.Message, error) {
|
||||||
|
if retries == -1 {
|
||||||
|
retries = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
var m *groupme.Message
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if portal.IsPrivateChat() {
|
||||||
|
m, err = sender.Client.CreateDirectMessage(context.TODO(), info)
|
||||||
|
} else {
|
||||||
|
m, err = sender.Client.CreateMessage(context.TODO(), info.GroupID, info)
|
||||||
|
}
|
||||||
|
|
||||||
m, err := sender.Client.CreateMessage(context.TODO(), info.GroupID, info)
|
|
||||||
id := ""
|
id := ""
|
||||||
if m != nil {
|
if m != nil {
|
||||||
id = m.ID.String()
|
id = m.ID.String()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Warnln(err, id, info.GroupID.String())
|
portal.log.Warnln(err, id, info.GroupID.String())
|
||||||
}
|
|
||||||
if isRetry && err != nil {
|
if retries > 0 {
|
||||||
m, err = sender.Client.CreateMessage(context.TODO(), info.GroupID, info)
|
return portal.sendRaw(sender, evt, info, retries-1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
// errChan := make(chan error, 1)
|
// errChan := make(chan error, 1)
|
||||||
|
26
user.go
26
user.go
@ -864,7 +864,7 @@ func (user *User) handleMessageLoop() {
|
|||||||
case msg := <-user.messageOutput:
|
case msg := <-user.messageOutput:
|
||||||
user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
|
user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
|
||||||
puppet := user.bridge.GetPuppetByJID(msg.data.UserID.String())
|
puppet := user.bridge.GetPuppetByJID(msg.data.UserID.String())
|
||||||
portal := user.bridge.GetPortalByJID(database.GroupPortalKey(msg.chat))
|
portal := user.bridge.GetPortalByJID(msg.chat)
|
||||||
if puppet != nil {
|
if puppet != nil {
|
||||||
puppet.Sync(user, portal.MXID, groupme.Member{
|
puppet.Sync(user, portal.MXID, groupme.Member{
|
||||||
UserID: msg.data.UserID,
|
UserID: msg.data.UserID,
|
||||||
@ -912,25 +912,17 @@ func (user *User) handleMessageLoop() {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
func (user *User) HandleTextMessage(message groupme.Message) {
|
func (user *User) HandleTextMessage(message groupme.Message) {
|
||||||
var group bool
|
id := database.ParsePortalKey(message.GroupID.String())
|
||||||
var id string
|
|
||||||
|
|
||||||
if message.GroupID.String() != "" {
|
if id == nil {
|
||||||
group = true
|
id = database.ParsePortalKey(message.ConversationID.String())
|
||||||
id = message.GroupID.String()
|
}
|
||||||
} else if message.ConversationID.String() != "" {
|
if id == nil {
|
||||||
group = false
|
user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message")
|
||||||
pk := database.ParsePortalKey(message.ConversationID.String())
|
|
||||||
if pk == nil {
|
|
||||||
user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
id = pk.JID
|
|
||||||
} else {
|
|
||||||
user.log.Errorln("Message received without conversation or groupid")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.messageInput <- PortalMessage{id, group, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
|
|
||||||
|
user.messageInput <- PortalMessage{*id, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) HandleLike(msg groupme.Message) {
|
func (user *User) HandleLike(msg groupme.Message) {
|
||||||
|
Loading…
Reference in New Issue
Block a user