Working double puppeting

This commit is contained in:
Brandon Watson 2023-09-11 21:58:26 -05:00
parent d9fc3014b4
commit 9fdf405504
4 changed files with 125 additions and 67 deletions

View File

@ -157,6 +157,9 @@ bridge:
default_bridge_receipts: true
default_bridge_presence: true
# Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth
login_shared_secret_map:
example.com: null
#
# If set, custom puppets will be enabled automatically for local users
# instead of users having to find an access token and run `login-matrix`

126
portal.go
View File

@ -49,14 +49,9 @@ import (
"github.com/beeper/groupme/groupmeext"
)
const MessageSendRetries = 5
const MediaUploadRetries = 5
const BadGatewaySleep = 5 * time.Second
const MaxMessageAgeToCreatePortal = 5 * 60 // 5 minutes
const recentlyHandledLength = 100
//var timeout = errors.New("message sending timed out")
type PortalMessage struct {
chat database.PortalKey
source *User
@ -94,21 +89,6 @@ type Portal struct {
hasRelaybot *bool
}
type BridgeInfoSection struct {
ID string `json:"id"`
DisplayName string `json:"displayname,omitempty"`
AvatarURL id.ContentURIString `json:"avatar_url,omitempty"`
ExternalURL string `json:"external_url,omitempty"`
}
type BridgeInfoContent struct {
BridgeBot id.UserID `json:"bridgebot"`
Creator id.UserID `json:"creator,omitempty"`
Protocol BridgeInfoSection `json:"protocol"`
Network *BridgeInfoSection `json:"network,omitempty"`
Channel BridgeInfoSection `json:"channel"`
}
// Public Properties
func (portal *Portal) IsEncrypted() bool {
@ -215,7 +195,8 @@ func (portal *Portal) SyncDM(user *User, dm *groupme.Chat) {
//}
update := false
update = portal.UpdateName(dm.OtherUser.Name, "", false) || update
update = portal.updateMetadata(user) || update
update = portal.UpdateAvatar(user, dm.OtherUser.AvatarURL, false) || update
if update {
@ -643,7 +624,7 @@ func (portal *Portal) handleMessage(msg PortalMessage) {
return
}
portal.HandleTextMessage(msg.source, msg.data)
// portal.handleReaction(msg.data.ID.String(), msg.data.FavoritedBy)
portal.handleReaction(msg.data.ID, msg.data.SenderID, msg.data.FavoritedBy)
}
func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, source *User, message *groupme.Message) (msg *event.MessageEventContent, sendText bool, err error) {
@ -825,12 +806,93 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
// return nil, true, errors.New("Unknown type")
}
// Private Methods
func (portal *Portal) createMatrixRoomForDM(user *User, puppet *Puppet) {
func (portal *Portal) handleReaction(msgID groupme.ID, senderId groupme.ID, ppl []string) {
fmt.Println("hello")
//reactions := portal.bridge.DB.Reaction.GetByTargetGMID(portal.Key, msgID, senderId)
//newLikes := newReactions(reactions, ppl)
//removeLikes := oldReactions(reactions, ppl)
//
//var eventID id.EventID
//if len(newLikes) > 0 {
// message := portal.bridge.DB.Message.GetByGMID(portal.Key, msgID)
// if message == nil {
// portal.log.Errorln("Received reaction for unknown message", msgID)
// return
// }
// eventID = message.MXID
//}
//
//for _, gmid := range newLikes {
// intent := portal.getReactionIntent(gmid)
// resp, err := portal.sendReaction(intent, eventID, "❤")
// if err != nil {
// portal.log.Errorln("Something wrong with sending reaction", msgID, gmid, err)
// continue
// }
//
// newReaction := portal.bridge.DB.Reaction.New()
// newReaction.MXID = resp.EventID
// newReaction.GMID = msgID
// newReaction.MXID = eventID
// newReaction.Chat.GMID = gmid
//
// portal.bridge.DB.Reaction.Insert()
// newReaction.Insert()
//
//}
//
//for _, reaction := range removeLikes {
// if len(reaction.Chat.GMID) == 0 {
// portal.log.Warnln("Reaction user state wrong", reaction.MXID, msgID)
// continue
// }
// intent := portal.getReactionIntent(reaction.Chat.GMID)
// _, err := intent.RedactEvent(portal.MXID, reaction.MXID)
// if err != nil {
// portal.log.Errorln("Something wrong with reaction redaction", reaction.MXID)
// continue
// }
// reaction.Delete()
//
//}
}
//func oldReactions(a []*database.Reaction, b []string) (ans []*database.Reaction) {
// for _, i := range a {
// flag := false
// for _, j := range b {
// if i.Chat.GMID == j {
// flag = true
// break
// }
// }
// if !flag {
// ans = append(ans, i)
// }
// }
//
// return
//}
//
//func newReactions(a []*database.Reaction, b []string) (ans []string) {
// for _, j := range b {
// flag := false
// for _, i := range a {
// if i.PuppetJID == j {
// flag = true
// break
// }
// }
// if !flag {
// ans = append(ans, j)
// }
// }
//
// return
//}
// Private Methods
func (portal *Portal) createMatrixRoom(user *User) error {
portal.roomCreateLock.Lock()
defer portal.roomCreateLock.Unlock()
@ -943,7 +1005,11 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i
func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI {
if portal.IsPrivateChat() {
if info.UserID == user.GetGMID() { //from me
return portal.bridge.GetPuppetByGMID(user.GMID).DefaultIntent()
puppet := portal.bridge.GetPuppetByGMID(user.GMID)
if len(puppet.CustomMXID) > 0 {
return puppet.customIntent
}
return puppet.DefaultIntent()
}
return portal.MainIntent()
} else if len(info.UserID.String()) == 0 {
@ -954,8 +1020,8 @@ func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appse
return portal.bridge.GetPuppetByGMID(info.UserID).IntentFor(portal)
}
func (portal *Portal) getReactionIntent(jid groupme.ID) *appservice.IntentAPI {
return portal.bridge.GetPuppetByGMID(jid).IntentFor(portal)
func (portal *Portal) getReactionIntent(gmid groupme.ID) *appservice.IntentAPI {
return portal.bridge.GetPuppetByGMID(gmid).IntentFor(portal)
}
func (portal *Portal) startHandling(source *User, info *groupme.Message) *appservice.IntentAPI {
@ -1431,8 +1497,10 @@ func (portal *Portal) syncParticipants(group *groupme.Group) {
if user != nil {
changed = levels.EnsureUserLevel(user.MXID, expectedLevel) || changed
}
if len(puppet.Displayname) == 0 {
puppet.Sync(nil, participant, false, false)
}
}
if changed {
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
if err != nil {

View File

@ -82,10 +82,6 @@ func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
return puppet.bridge.AS.Intent(puppet.MXID)
}
//func (puppet *Puppet) SetRoomMetadata(name, avatarURL string) bool {
//
//}
func (puppet *Puppet) UpdateAvatar(source *User, forcePortalSync bool) bool {
changed := source.updateAvatar(puppet.GMID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent())
if !changed || puppet.Avatar == "unauthorized" {
@ -180,8 +176,4 @@ func (puppet *Puppet) Sync(source *User, member *groupme.Member, forceAvatarSync
if update {
puppet.Update()
}
//puppet.log.Debugfln("Syncing info through %s", source.GMID)
// TODO
}

53
user.go
View File

@ -102,12 +102,6 @@ func (cl ChatList) Swap(i, j int) {
cl[i], cl[j] = cl[j], cl[i]
}
type FakeMessage struct {
Text string
ID string
Alert bool
}
// Public Properties
func (user *User) GetPermissionLevel() bridgeconfig.PermissionLevel {
@ -471,7 +465,13 @@ func (user *User) HandleChatList() {
user.log.Errorln("chat sync error", err) //TODO: handle
return
}
user.log.Debugln("Group")
for _, chat := range chats {
user.log.Debugln(" " + chat.Name)
for _, mem := range chat.Members {
user.log.Debugln(" " + mem.Nickname)
}
chatMap[chat.ID] = chat
}
user.GroupList = chatMap
@ -487,24 +487,22 @@ func (user *User) HandleChatList() {
}
user.ChatList = dmMap
//userMap := map[groupme.ID]groupme.User{}
//users, err := user.Client.IndexAllRelations()
//if err != nil {
// user.log.Errorln("Error syncing user list, continuing sync", err)
//}
//fmt.Println("Relations:")
//for _, u := range users {
// fmt.Println(" " + u.ID.String() + " " + u.Name)
// puppet := user.bridge.GetPuppetByGMID(u.ID)
// // "" for overall user not related to one group
// puppet.Sync(user, &groupme.Member{
// UserID: u.ID,
// Nickname: u.Name,
// ImageURL: u.AvatarURL,
// }, false, false)
// userMap[u.ID] = *u
//}
//user.RelationList = userMap
userMap := map[groupme.ID]*groupme.User{}
users, err := user.Client.IndexAllRelations()
if err != nil {
user.log.Errorln("Error syncing user list, continuing sync", err)
}
for _, u := range users {
puppet := user.bridge.GetPuppetByGMID(u.ID)
// "" for overall user not related to one group
puppet.Sync(user, &groupme.Member{
UserID: u.ID,
Nickname: u.Name,
ImageURL: u.AvatarURL,
}, false, false)
userMap[u.ID] = u
}
user.RelationList = userMap
user.log.Infoln("Chat list received")
user.chatListReceived <- struct{}{}
@ -527,10 +525,6 @@ func (user *User) handleMessageLoop() {
ImageURL: msg.data.AvatarURL,
}, false, false)
}
//err := puppet.IntentFor(portal).EnsureJoined(portal.MXID)
//if err != nil {
// user.log.Warnln("Unable to ensure user puppet joined")
//}
portal.messages <- msg
case <-user.syncStart:
user.log.Debugln("Processing of incoming messages is locked")
@ -790,7 +784,8 @@ func (user *User) getDirectChats() map[id.UserID][]id.RoomID {
return res
}
func (user *User) updateAvatar(gmdi groupme.ID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool, log log.Logger, intent *appservice.IntentAPI) bool {
func (user *User) updateAvatar(gmdi groupme.ID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool,
log log.Logger, intent *appservice.IntentAPI) bool {
return false
}