Somewhat correctly setting up rooms and puppets
This commit is contained in:
		@@ -228,7 +228,7 @@ func (puppet *Puppet) tryRelogin(cause error, action string) bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (puppet *Puppet) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) {
 | 
			
		||||
	puppet.log.Warnln("Sync error:", err)
 | 
			
		||||
	puppet.log.Warnln("SyncGroup error:", err)
 | 
			
		||||
	if errors.Is(err, mautrix.MUnknownToken) {
 | 
			
		||||
		if !puppet.tryRelogin(err, "syncing") {
 | 
			
		||||
			return 0, err
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										331
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										331
									
								
								main.go
									
									
									
									
									
								
							@@ -18,8 +18,11 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "embed"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/beeper/groupme-lib"
 | 
			
		||||
	"go.mau.fi/util/configupgrade"
 | 
			
		||||
	"maunium.net/go/mautrix/bridge/bridgeconfig"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"maunium.net/go/mautrix"
 | 
			
		||||
@@ -43,6 +46,8 @@ var (
 | 
			
		||||
//go:embed example-config.yaml
 | 
			
		||||
var ExampleConfig string
 | 
			
		||||
 | 
			
		||||
const unstableFeatureBatchSending = "org.matrix.msc2716"
 | 
			
		||||
 | 
			
		||||
type GMBridge struct {
 | 
			
		||||
	bridge.Bridge
 | 
			
		||||
	Config       *config.Config
 | 
			
		||||
@@ -150,7 +155,331 @@ func (br *GMBridge) GetConfigPtr() interface{} {
 | 
			
		||||
	return br.Config
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const unstableFeatureBatchSending = "org.matrix.msc2716"
 | 
			
		||||
func (bridge *GMBridge) GetPortalByGMID(key database.PortalKey) *Portal {
 | 
			
		||||
	bridge.portalsLock.Lock()
 | 
			
		||||
	defer bridge.portalsLock.Unlock()
 | 
			
		||||
	portal, ok := bridge.portalsByGMID[key]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return bridge.loadDBPortal(bridge.DB.Portal.GetByGMID(key), &key)
 | 
			
		||||
	}
 | 
			
		||||
	return portal
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetAllPortals() []*Portal {
 | 
			
		||||
	return br.dbPortalsToPortals(br.DB.Portal.GetAll())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetAllIPortals() (iportals []bridge.Portal) {
 | 
			
		||||
	portals := br.GetAllPortals()
 | 
			
		||||
	iportals = make([]bridge.Portal, len(portals))
 | 
			
		||||
	for i, portal := range portals {
 | 
			
		||||
		iportals[i] = portal
 | 
			
		||||
	}
 | 
			
		||||
	return iportals
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetAllPortalsByGMID(gmid groupme.ID) []*Portal {
 | 
			
		||||
	return br.dbPortalsToPortals(br.DB.Portal.GetAllByGMID(gmid))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPortalByMXID(mxid id.RoomID) *Portal {
 | 
			
		||||
	bridge.portalsLock.Lock()
 | 
			
		||||
	defer bridge.portalsLock.Unlock()
 | 
			
		||||
	portal, ok := bridge.portalsByMXID[mxid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return bridge.loadDBPortal(bridge.DB.Portal.GetByMXID(mxid), nil)
 | 
			
		||||
	}
 | 
			
		||||
	return portal
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
 | 
			
		||||
	p := br.GetPortalByMXID(mxid)
 | 
			
		||||
	if p == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) getUserByMXID(userID id.UserID, onlyIfExists bool) *User {
 | 
			
		||||
	_, isPuppet := br.ParsePuppetMXID(userID)
 | 
			
		||||
	if isPuppet || userID == br.Bot.UserID {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	br.usersLock.Lock()
 | 
			
		||||
	defer br.usersLock.Unlock()
 | 
			
		||||
	user, ok := br.usersByMXID[userID]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		userIDPtr := &userID
 | 
			
		||||
		if onlyIfExists {
 | 
			
		||||
			userIDPtr = nil
 | 
			
		||||
		}
 | 
			
		||||
		return br.loadDBUser(br.DB.User.GetByMXID(userID), userIDPtr)
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetUserByMXID(userID id.UserID) *User {
 | 
			
		||||
	return br.getUserByMXID(userID, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetIUser(userID id.UserID, create bool) bridge.User {
 | 
			
		||||
	u := br.getUserByMXID(userID, !create)
 | 
			
		||||
	if u == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return u
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetUserByMXIDIfExists(userID id.UserID) *User {
 | 
			
		||||
	return br.getUserByMXID(userID, true)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetUserByGMID(gmid groupme.ID) *User {
 | 
			
		||||
	bridge.usersLock.Lock()
 | 
			
		||||
	defer bridge.usersLock.Unlock()
 | 
			
		||||
	user, ok := bridge.usersByGMID[gmid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return bridge.loadDBUser(bridge.DB.User.GetByGMID(gmid), nil)
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetAllUsers() []*User {
 | 
			
		||||
	br.usersLock.Lock()
 | 
			
		||||
	defer br.usersLock.Unlock()
 | 
			
		||||
	dbUsers := br.DB.User.GetAll()
 | 
			
		||||
	output := make([]*User, len(dbUsers))
 | 
			
		||||
	for index, dbUser := range dbUsers {
 | 
			
		||||
		user, ok := br.usersByMXID[dbUser.MXID]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			user = br.loadDBUser(dbUser, nil)
 | 
			
		||||
		}
 | 
			
		||||
		output[index] = user
 | 
			
		||||
	}
 | 
			
		||||
	return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) loadDBUser(dbUser *database.User, mxid *id.UserID) *User {
 | 
			
		||||
	if dbUser == nil {
 | 
			
		||||
		if mxid == nil {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		dbUser = br.DB.User.New()
 | 
			
		||||
		dbUser.MXID = *mxid
 | 
			
		||||
		dbUser.Insert()
 | 
			
		||||
	}
 | 
			
		||||
	user := br.NewUser(dbUser)
 | 
			
		||||
	br.usersByMXID[user.MXID] = user
 | 
			
		||||
	if len(user.GMID) > 0 {
 | 
			
		||||
		br.usersByGMID[user.GMID] = user
 | 
			
		||||
	}
 | 
			
		||||
	if len(user.ManagementRoom) > 0 {
 | 
			
		||||
		br.managementRooms[user.ManagementRoom] = user
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) NewUser(dbUser *database.User) *User {
 | 
			
		||||
	user := &User{
 | 
			
		||||
		User:   dbUser,
 | 
			
		||||
		bridge: br,
 | 
			
		||||
		log:    br.Log.Sub("User").Sub(string(dbUser.MXID)),
 | 
			
		||||
 | 
			
		||||
		chatListReceived: make(chan struct{}, 1),
 | 
			
		||||
		syncPortalsDone:  make(chan struct{}, 1),
 | 
			
		||||
		syncStart:        make(chan struct{}, 1),
 | 
			
		||||
		messageInput:     make(chan PortalMessage),
 | 
			
		||||
		messageOutput:    make(chan PortalMessage, br.Config.Bridge.PortalMessageBuffer),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user.PermissionLevel = user.bridge.Config.Bridge.Permissions.Get(user.MXID)
 | 
			
		||||
	user.Whitelisted = user.PermissionLevel >= bridgeconfig.PermissionLevelUser
 | 
			
		||||
	user.Admin = user.PermissionLevel >= bridgeconfig.PermissionLevelAdmin
 | 
			
		||||
	user.BridgeState = br.NewBridgeStateQueue(user)
 | 
			
		||||
	go user.handleMessageLoop()
 | 
			
		||||
	go user.runMessageRingBuffer()
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
 | 
			
		||||
	return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetAllPuppets() []*Puppet {
 | 
			
		||||
	return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	output := make([]*Puppet, len(dbPuppets))
 | 
			
		||||
	for index, dbPuppet := range dbPuppets {
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		puppet, ok := bridge.puppets[dbPuppet.GMID]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
			bridge.puppets[dbPuppet.GMID] = puppet
 | 
			
		||||
			if len(dbPuppet.CustomMXID) > 0 {
 | 
			
		||||
				bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		output[index] = puppet
 | 
			
		||||
	}
 | 
			
		||||
	return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) FormatPuppetMXID(gmid groupme.ID) id.UserID {
 | 
			
		||||
	return id.NewUserID(
 | 
			
		||||
		bridge.Config.Bridge.FormatUsername(gmid.String()),
 | 
			
		||||
		bridge.Config.Homeserver.Domain)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
 | 
			
		||||
	return &Puppet{
 | 
			
		||||
		Puppet: dbPuppet,
 | 
			
		||||
		bridge: bridge,
 | 
			
		||||
		log:    bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.GMID)),
 | 
			
		||||
 | 
			
		||||
		MXID: bridge.FormatPuppetMXID(dbPuppet.GMID),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) IsGhost(id id.UserID) bool {
 | 
			
		||||
	_, ok := br.ParsePuppetMXID(id)
 | 
			
		||||
	return ok
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetIGhost(id id.UserID) bridge.Ghost {
 | 
			
		||||
	p := br.GetPuppetByMXID(id)
 | 
			
		||||
	if p == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (groupme.ID, bool) {
 | 
			
		||||
	if userIDRegex == nil {
 | 
			
		||||
		userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
 | 
			
		||||
			bridge.Config.Bridge.FormatUsername("([0-9]+)"),
 | 
			
		||||
			bridge.Config.Homeserver.Domain))
 | 
			
		||||
	}
 | 
			
		||||
	match := userIDRegex.FindStringSubmatch(string(mxid))
 | 
			
		||||
	if match == nil || len(match) != 2 {
 | 
			
		||||
		return "", false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return groupme.ID(match[1]), true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
 | 
			
		||||
	gmid, ok := bridge.ParsePuppetMXID(mxid)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bridge.GetPuppetByGMID(gmid)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByGMID(gmid groupme.ID) *Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	puppet, ok := bridge.puppets[gmid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		dbPuppet := bridge.DB.Puppet.Get(gmid)
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			dbPuppet = bridge.DB.Puppet.New()
 | 
			
		||||
			dbPuppet.GMID = gmid
 | 
			
		||||
			dbPuppet.Insert()
 | 
			
		||||
		}
 | 
			
		||||
		puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
		bridge.puppets[puppet.GMID] = puppet
 | 
			
		||||
		if len(puppet.CustomMXID) > 0 {
 | 
			
		||||
			bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return puppet
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	puppet, ok := bridge.puppetsByCustomMXID[mxid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		dbPuppet := bridge.DB.Puppet.GetByCustomMXID(mxid)
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
		bridge.puppets[puppet.GMID] = puppet
 | 
			
		||||
		bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
 | 
			
		||||
	}
 | 
			
		||||
	return puppet
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal {
 | 
			
		||||
	bridge.portalsLock.Lock()
 | 
			
		||||
	defer bridge.portalsLock.Unlock()
 | 
			
		||||
	output := make([]*Portal, len(dbPortals))
 | 
			
		||||
	for index, dbPortal := range dbPortals {
 | 
			
		||||
		if dbPortal == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		portal, ok := bridge.portalsByGMID[dbPortal.Key]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			portal = bridge.loadDBPortal(dbPortal, nil)
 | 
			
		||||
		}
 | 
			
		||||
		output[index] = portal
 | 
			
		||||
	}
 | 
			
		||||
	return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) loadDBPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal {
 | 
			
		||||
	if dbPortal == nil {
 | 
			
		||||
		if key == nil {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		dbPortal = bridge.DB.Portal.New()
 | 
			
		||||
		dbPortal.Key = *key
 | 
			
		||||
		dbPortal.Insert()
 | 
			
		||||
	}
 | 
			
		||||
	portal := bridge.NewPortal(dbPortal)
 | 
			
		||||
	bridge.portalsByGMID[portal.Key] = portal
 | 
			
		||||
	if len(portal.MXID) > 0 {
 | 
			
		||||
		bridge.portalsByMXID[portal.MXID] = portal
 | 
			
		||||
	}
 | 
			
		||||
	return portal
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) NewManualPortal(key database.PortalKey) *Portal {
 | 
			
		||||
	portal := &Portal{
 | 
			
		||||
		Portal: bridge.DB.Portal.New(),
 | 
			
		||||
		bridge: bridge,
 | 
			
		||||
		log:    bridge.Log.Sub(fmt.Sprintf("Portal/%s", key)),
 | 
			
		||||
 | 
			
		||||
		recentlyHandled: make([]string, recentlyHandledLength),
 | 
			
		||||
 | 
			
		||||
		messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer),
 | 
			
		||||
	}
 | 
			
		||||
	portal.Key = key
 | 
			
		||||
	go portal.handleMessageLoop()
 | 
			
		||||
	return portal
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) NewPortal(dbPortal *database.Portal) *Portal {
 | 
			
		||||
	portal := &Portal{
 | 
			
		||||
		Portal: dbPortal,
 | 
			
		||||
		bridge: bridge,
 | 
			
		||||
		log:    bridge.Log.Sub(fmt.Sprintf("Portal/%s", dbPortal.Key)),
 | 
			
		||||
 | 
			
		||||
		recentlyHandled: make([]string, recentlyHandledLength),
 | 
			
		||||
 | 
			
		||||
		messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer),
 | 
			
		||||
	}
 | 
			
		||||
	go portal.handleMessageLoop()
 | 
			
		||||
	return portal
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) CheckFeatures(versions *mautrix.RespVersions) (string, bool) {
 | 
			
		||||
	if br.Config.Bridge.HistorySync.Backfill {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										150
									
								
								puppet.go
									
									
									
									
									
								
							
							
						
						
									
										150
									
								
								puppet.go
									
									
									
									
									
								
							@@ -17,7 +17,6 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
@@ -26,7 +25,6 @@ import (
 | 
			
		||||
	"github.com/beeper/groupme-lib"
 | 
			
		||||
 | 
			
		||||
	"maunium.net/go/mautrix/appservice"
 | 
			
		||||
	"maunium.net/go/mautrix/bridge"
 | 
			
		||||
	"maunium.net/go/mautrix/id"
 | 
			
		||||
 | 
			
		||||
	"github.com/beeper/groupme/database"
 | 
			
		||||
@@ -34,146 +32,6 @@ import (
 | 
			
		||||
 | 
			
		||||
var userIDRegex *regexp.Regexp
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (groupme.ID, bool) {
 | 
			
		||||
	if userIDRegex == nil {
 | 
			
		||||
		userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
 | 
			
		||||
			bridge.Config.Bridge.FormatUsername("([0-9]+)"),
 | 
			
		||||
			bridge.Config.Homeserver.Domain))
 | 
			
		||||
	}
 | 
			
		||||
	match := userIDRegex.FindStringSubmatch(string(mxid))
 | 
			
		||||
	if match == nil || len(match) != 2 {
 | 
			
		||||
		return "", false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return groupme.ID(match[1]), true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
 | 
			
		||||
	gmid, ok := bridge.ParsePuppetMXID(mxid)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bridge.GetPuppetByGMID(gmid)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByGMID(gmid groupme.ID) *Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	puppet, ok := bridge.puppets[gmid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		dbPuppet := bridge.DB.Puppet.Get(gmid)
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			dbPuppet = bridge.DB.Puppet.New()
 | 
			
		||||
			dbPuppet.GMID = gmid
 | 
			
		||||
			dbPuppet.Insert()
 | 
			
		||||
		}
 | 
			
		||||
		puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
		bridge.puppets[puppet.GMID] = puppet
 | 
			
		||||
		if len(puppet.CustomMXID) > 0 {
 | 
			
		||||
			bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return puppet
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	puppet, ok := bridge.puppetsByCustomMXID[mxid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		dbPuppet := bridge.DB.Puppet.GetByCustomMXID(mxid)
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
		bridge.puppets[puppet.GMID] = puppet
 | 
			
		||||
		bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
 | 
			
		||||
	}
 | 
			
		||||
	return puppet
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetIDoublePuppet() bridge.DoublePuppet {
 | 
			
		||||
	p := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if p == nil || p.CustomIntent() == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetIGhost() bridge.Ghost {
 | 
			
		||||
	if user.GMID.String() == "" {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	p := user.bridge.GetPuppetByGMID(user.GMID)
 | 
			
		||||
	if p == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) IsGhost(id id.UserID) bool {
 | 
			
		||||
	_, ok := br.ParsePuppetMXID(id)
 | 
			
		||||
	return ok
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetIGhost(id id.UserID) bridge.Ghost {
 | 
			
		||||
	p := br.GetPuppetByMXID(id)
 | 
			
		||||
	if p == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (puppet *Puppet) GetMXID() id.UserID {
 | 
			
		||||
	return puppet.MXID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
 | 
			
		||||
	return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetAllPuppets() []*Puppet {
 | 
			
		||||
	return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
 | 
			
		||||
	bridge.puppetsLock.Lock()
 | 
			
		||||
	defer bridge.puppetsLock.Unlock()
 | 
			
		||||
	output := make([]*Puppet, len(dbPuppets))
 | 
			
		||||
	for index, dbPuppet := range dbPuppets {
 | 
			
		||||
		if dbPuppet == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		puppet, ok := bridge.puppets[dbPuppet.GMID]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			puppet = bridge.NewPuppet(dbPuppet)
 | 
			
		||||
			bridge.puppets[dbPuppet.GMID] = puppet
 | 
			
		||||
			if len(dbPuppet.CustomMXID) > 0 {
 | 
			
		||||
				bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		output[index] = puppet
 | 
			
		||||
	}
 | 
			
		||||
	return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) FormatPuppetMXID(gmid groupme.ID) id.UserID {
 | 
			
		||||
	return id.NewUserID(
 | 
			
		||||
		bridge.Config.Bridge.FormatUsername(gmid.String()),
 | 
			
		||||
		bridge.Config.Homeserver.Domain)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
 | 
			
		||||
	return &Puppet{
 | 
			
		||||
		Puppet: dbPuppet,
 | 
			
		||||
		bridge: bridge,
 | 
			
		||||
		log:    bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.GMID)),
 | 
			
		||||
 | 
			
		||||
		MXID: bridge.FormatPuppetMXID(dbPuppet.GMID),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Puppet struct {
 | 
			
		||||
	*database.Puppet
 | 
			
		||||
 | 
			
		||||
@@ -192,10 +50,18 @@ type Puppet struct {
 | 
			
		||||
	syncLock sync.Mutex
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Public Properties
 | 
			
		||||
 | 
			
		||||
func (puppet *Puppet) GetMXID() id.UserID {
 | 
			
		||||
	return puppet.MXID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (puppet *Puppet) PhoneNumber() string {
 | 
			
		||||
	return puppet.GMID.String()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Public Methods
 | 
			
		||||
 | 
			
		||||
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
 | 
			
		||||
	if puppet.customIntent == nil || portal.Key.GMID == puppet.GMID {
 | 
			
		||||
		return puppet.DefaultIntent()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										759
									
								
								user.go
									
									
									
									
									
								
							
							
						
						
									
										759
									
								
								user.go
									
									
									
									
									
								
							@@ -58,9 +58,9 @@ type User struct {
 | 
			
		||||
	ConnectionErrors int
 | 
			
		||||
	CommunityID      string
 | 
			
		||||
 | 
			
		||||
	ChatList     map[groupme.ID]groupme.Chat
 | 
			
		||||
	GroupList    map[groupme.ID]groupme.Group
 | 
			
		||||
	RelationList map[groupme.ID]groupme.User
 | 
			
		||||
	ChatList     map[groupme.ID]*groupme.Chat
 | 
			
		||||
	GroupList    map[groupme.ID]*groupme.Group
 | 
			
		||||
	RelationList map[groupme.ID]*groupme.User
 | 
			
		||||
 | 
			
		||||
	cleanDisconnection  bool
 | 
			
		||||
	batteryWarningsSent int
 | 
			
		||||
@@ -81,36 +81,35 @@ type User struct {
 | 
			
		||||
	syncWait  sync.WaitGroup
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) getUserByMXID(userID id.UserID, onlyIfExists bool) *User {
 | 
			
		||||
	_, isPuppet := br.ParsePuppetMXID(userID)
 | 
			
		||||
	if isPuppet || userID == br.Bot.UserID {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	br.usersLock.Lock()
 | 
			
		||||
	defer br.usersLock.Unlock()
 | 
			
		||||
	user, ok := br.usersByMXID[userID]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		userIDPtr := &userID
 | 
			
		||||
		if onlyIfExists {
 | 
			
		||||
			userIDPtr = nil
 | 
			
		||||
		}
 | 
			
		||||
		return br.loadDBUser(br.DB.User.GetByMXID(userID), userIDPtr)
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
type Chat struct {
 | 
			
		||||
	Portal          *Portal
 | 
			
		||||
	LastMessageTime uint64
 | 
			
		||||
	Group           *groupme.Group
 | 
			
		||||
	DM              *groupme.Chat
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetUserByMXID(userID id.UserID) *User {
 | 
			
		||||
	return br.getUserByMXID(userID, false)
 | 
			
		||||
type ChatList []Chat
 | 
			
		||||
 | 
			
		||||
func (cl ChatList) Len() int {
 | 
			
		||||
	return len(cl)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetIUser(userID id.UserID, create bool) bridge.User {
 | 
			
		||||
	u := br.getUserByMXID(userID, !create)
 | 
			
		||||
	if u == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return u
 | 
			
		||||
func (cl ChatList) Less(i, j int) bool {
 | 
			
		||||
	return cl[i].LastMessageTime > cl[j].LastMessageTime
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
	return user.PermissionLevel
 | 
			
		||||
}
 | 
			
		||||
@@ -123,128 +122,39 @@ func (user *User) GetMXID() id.UserID {
 | 
			
		||||
	return user.MXID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetGMID() groupme.ID {
 | 
			
		||||
	if len(user.GMID) == 0 {
 | 
			
		||||
		u, err := user.Client.MyUser(context.TODO())
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			user.log.Errorln("Failed to get own GroupMe ID:", err)
 | 
			
		||||
			return ""
 | 
			
		||||
		}
 | 
			
		||||
		user.GMID = u.ID
 | 
			
		||||
	}
 | 
			
		||||
	return user.GMID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetCommandState() map[string]interface{} {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetUserByMXIDIfExists(userID id.UserID) *User {
 | 
			
		||||
	return br.getUserByMXID(userID, true)
 | 
			
		||||
func (user *User) GetIDoublePuppet() bridge.DoublePuppet {
 | 
			
		||||
	p := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if p == nil || p.CustomIntent() == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bridge *GMBridge) GetUserByGMID(gmid groupme.ID) *User {
 | 
			
		||||
	bridge.usersLock.Lock()
 | 
			
		||||
	defer bridge.usersLock.Unlock()
 | 
			
		||||
	user, ok := bridge.usersByGMID[gmid]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return bridge.loadDBUser(bridge.DB.User.GetByGMID(gmid), nil)
 | 
			
		||||
func (user *User) GetIGhost() bridge.Ghost {
 | 
			
		||||
	if user.GMID.String() == "" {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) addToGMIDMap() {
 | 
			
		||||
	user.bridge.usersLock.Lock()
 | 
			
		||||
	user.bridge.usersByGMID[user.GMID] = user
 | 
			
		||||
	user.bridge.usersLock.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) removeFromGMIDMap() {
 | 
			
		||||
	user.bridge.usersLock.Lock()
 | 
			
		||||
	jidUser, ok := user.bridge.usersByGMID[user.GMID]
 | 
			
		||||
	if ok && user == jidUser {
 | 
			
		||||
		delete(user.bridge.usersByGMID, user.GMID)
 | 
			
		||||
	p := user.bridge.GetPuppetByGMID(user.GMID)
 | 
			
		||||
	if p == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	user.bridge.usersLock.Unlock()
 | 
			
		||||
	user.bridge.Metrics.TrackLoginState(user.GMID, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) GetAllUsers() []*User {
 | 
			
		||||
	br.usersLock.Lock()
 | 
			
		||||
	defer br.usersLock.Unlock()
 | 
			
		||||
	dbUsers := br.DB.User.GetAll()
 | 
			
		||||
	output := make([]*User, len(dbUsers))
 | 
			
		||||
	for index, dbUser := range dbUsers {
 | 
			
		||||
		user, ok := br.usersByMXID[dbUser.MXID]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			user = br.loadDBUser(dbUser, nil)
 | 
			
		||||
		}
 | 
			
		||||
		output[index] = user
 | 
			
		||||
	}
 | 
			
		||||
	return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) loadDBUser(dbUser *database.User, mxid *id.UserID) *User {
 | 
			
		||||
	if dbUser == nil {
 | 
			
		||||
		if mxid == nil {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		dbUser = br.DB.User.New()
 | 
			
		||||
		dbUser.MXID = *mxid
 | 
			
		||||
		dbUser.Insert()
 | 
			
		||||
	}
 | 
			
		||||
	user := br.NewUser(dbUser)
 | 
			
		||||
	br.usersByMXID[user.MXID] = user
 | 
			
		||||
	if len(user.GMID) > 0 {
 | 
			
		||||
		br.usersByGMID[user.GMID] = user
 | 
			
		||||
	}
 | 
			
		||||
	if len(user.ManagementRoom) > 0 {
 | 
			
		||||
		br.managementRooms[user.ManagementRoom] = user
 | 
			
		||||
	}
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (br *GMBridge) NewUser(dbUser *database.User) *User {
 | 
			
		||||
	user := &User{
 | 
			
		||||
		User:   dbUser,
 | 
			
		||||
		bridge: br,
 | 
			
		||||
		log:    br.Log.Sub("User").Sub(string(dbUser.MXID)),
 | 
			
		||||
 | 
			
		||||
		chatListReceived: make(chan struct{}, 1),
 | 
			
		||||
		syncPortalsDone:  make(chan struct{}, 1),
 | 
			
		||||
		syncStart:        make(chan struct{}, 1),
 | 
			
		||||
		messageInput:     make(chan PortalMessage),
 | 
			
		||||
		messageOutput:    make(chan PortalMessage, br.Config.Bridge.PortalMessageBuffer),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user.PermissionLevel = user.bridge.Config.Bridge.Permissions.Get(user.MXID)
 | 
			
		||||
	user.Whitelisted = user.PermissionLevel >= bridgeconfig.PermissionLevelUser
 | 
			
		||||
	user.Admin = user.PermissionLevel >= bridgeconfig.PermissionLevelAdmin
 | 
			
		||||
	user.BridgeState = br.NewBridgeStateQueue(user)
 | 
			
		||||
	go user.handleMessageLoop()
 | 
			
		||||
	go user.runMessageRingBuffer()
 | 
			
		||||
	return user
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) (ok bool) {
 | 
			
		||||
	extraContent := make(map[string]interface{})
 | 
			
		||||
	if isDirect {
 | 
			
		||||
		extraContent["is_direct"] = true
 | 
			
		||||
	}
 | 
			
		||||
	customPuppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if customPuppet != nil && customPuppet.CustomIntent() != nil {
 | 
			
		||||
		extraContent["fi.mau.will_auto_accept"] = true
 | 
			
		||||
	}
 | 
			
		||||
	_, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: user.MXID}, extraContent)
 | 
			
		||||
	var httpErr mautrix.HTTPError
 | 
			
		||||
	if err != nil && errors.As(err, &httpErr) && httpErr.RespError != nil && strings.Contains(httpErr.RespError.Err, "is already in the room") {
 | 
			
		||||
		user.bridge.StateStore.SetMembership(roomID, user.MXID, event.MembershipJoin)
 | 
			
		||||
		ok = true
 | 
			
		||||
		return
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		user.log.Warnfln("Failed to invite user to %s: %v", roomID, err)
 | 
			
		||||
	} else {
 | 
			
		||||
		ok = true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if customPuppet != nil && customPuppet.CustomIntent() != nil {
 | 
			
		||||
		err = customPuppet.CustomIntent().EnsureJoined(roomID, appservice.EnsureJoinedParams{IgnoreCache: true})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			user.log.Warnfln("Failed to auto-join %s: %v", roomID, err)
 | 
			
		||||
			ok = false
 | 
			
		||||
		} else {
 | 
			
		||||
			ok = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
	return p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetSpaceRoom() id.RoomID {
 | 
			
		||||
@@ -334,6 +244,55 @@ func (user *User) SetManagementRoom(roomID id.RoomID) {
 | 
			
		||||
	user.Update()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HasSession() bool {
 | 
			
		||||
	return len(user.Token) > 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsConnected() bool {
 | 
			
		||||
	// TODO: better connection check
 | 
			
		||||
	return user.Conn != nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsLoggedIn() bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsLoginInProgress() bool {
 | 
			
		||||
	// return user.Conn != nil && user.Conn.IsLoginInProgress()
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) ShouldCallSynchronously() bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetPortalByGMID(gmid groupme.ID) *Portal {
 | 
			
		||||
	return user.bridge.GetPortalByGMID(user.PortalKey(gmid))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Public Methods
 | 
			
		||||
 | 
			
		||||
func (user *User) Login(token string) error {
 | 
			
		||||
	user.Token = token
 | 
			
		||||
 | 
			
		||||
	user.addToGMIDMap()
 | 
			
		||||
	user.PostLogin()
 | 
			
		||||
	if user.Connect() {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return errors.New("failed to connect")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) PostLogin() {
 | 
			
		||||
	user.bridge.Metrics.TrackConnectionState(user.GMID, true)
 | 
			
		||||
	user.bridge.Metrics.TrackLoginState(user.GMID, true)
 | 
			
		||||
	user.bridge.Metrics.TrackBufferLength(user.MXID, 0)
 | 
			
		||||
	user.log.Debugln("Locking processing of incoming messages and starting post-login sync")
 | 
			
		||||
	user.syncWait.Add(1)
 | 
			
		||||
	user.syncStart <- struct{}{}
 | 
			
		||||
	go user.intPostLogin()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) Connect() bool {
 | 
			
		||||
	if user.Conn != nil {
 | 
			
		||||
		return true
 | 
			
		||||
@@ -375,76 +334,257 @@ func (user *User) RestoreSession() bool {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HasSession() bool {
 | 
			
		||||
	return len(user.Token) > 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsConnected() bool {
 | 
			
		||||
	// TODO: better connection check
 | 
			
		||||
	return user.Conn != nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsLoggedIn() bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) IsLoginInProgress() bool {
 | 
			
		||||
	// return user.Conn != nil && user.Conn.IsLoginInProgress()
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetGMID() groupme.ID {
 | 
			
		||||
	if len(user.GMID) == 0 {
 | 
			
		||||
		u, err := user.Client.MyUser(context.TODO())
 | 
			
		||||
func (user *User) UpdateDirectChats(chats map[id.UserID][]id.RoomID) {
 | 
			
		||||
	if !user.bridge.Config.Bridge.SyncDirectChatList {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	puppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if puppet == nil || puppet.CustomIntent() == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	intent := puppet.CustomIntent()
 | 
			
		||||
	method := http.MethodPatch
 | 
			
		||||
	if chats == nil {
 | 
			
		||||
		chats = user.getDirectChats()
 | 
			
		||||
		method = http.MethodPut
 | 
			
		||||
	}
 | 
			
		||||
	user.log.Debugln("Updating m.direct list on homeserver")
 | 
			
		||||
	var err error
 | 
			
		||||
	if user.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareAsmux {
 | 
			
		||||
		urlPath := intent.BuildClientURL("unstable", "com.beeper.asmux", "dms")
 | 
			
		||||
		_, err = intent.MakeFullRequest(mautrix.FullRequest{
 | 
			
		||||
			Method:      method,
 | 
			
		||||
			URL:         urlPath,
 | 
			
		||||
			Headers:     http.Header{"X-Asmux-Auth": {user.bridge.AS.Registration.AppToken}},
 | 
			
		||||
			RequestJSON: chats,
 | 
			
		||||
		})
 | 
			
		||||
	} else {
 | 
			
		||||
		existingChats := make(map[id.UserID][]id.RoomID)
 | 
			
		||||
		err = intent.GetAccountData(event.AccountDataDirectChats.Type, &existingChats)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			user.log.Errorln("Failed to get own GroupMe ID:", err)
 | 
			
		||||
			return ""
 | 
			
		||||
			user.log.Warnln("Failed to get m.direct list to update it:", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		user.GMID = u.ID
 | 
			
		||||
		for userID, rooms := range existingChats {
 | 
			
		||||
			if _, ok := user.bridge.ParsePuppetMXID(userID); !ok {
 | 
			
		||||
				// This is not a ghost user, include it in the new list
 | 
			
		||||
				chats[userID] = rooms
 | 
			
		||||
			} else if _, ok := chats[userID]; !ok && method == http.MethodPatch {
 | 
			
		||||
				// This is a ghost user, but we're not replacing the whole list, so include it too
 | 
			
		||||
				chats[userID] = rooms
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		err = intent.SetAccountData(event.AccountDataDirectChats.Type, &chats)
 | 
			
		||||
	}
 | 
			
		||||
	return user.GMID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) Login(token string) error {
 | 
			
		||||
	user.Token = token
 | 
			
		||||
 | 
			
		||||
	user.addToGMIDMap()
 | 
			
		||||
	user.PostLogin()
 | 
			
		||||
	if user.Connect() {
 | 
			
		||||
		return nil
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Warnln("Failed to update m.direct list:", err)
 | 
			
		||||
	}
 | 
			
		||||
	return errors.New("failed to connect")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Chat struct {
 | 
			
		||||
	Portal          *Portal
 | 
			
		||||
	LastMessageTime uint64
 | 
			
		||||
	Group           *groupme.Group
 | 
			
		||||
	DM              *groupme.Chat
 | 
			
		||||
func (user *User) PortalKey(gmid groupme.ID) database.PortalKey {
 | 
			
		||||
	return database.NewPortalKey(gmid, user.GMID)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ChatList []Chat
 | 
			
		||||
// Handlers
 | 
			
		||||
 | 
			
		||||
func (cl ChatList) Len() int {
 | 
			
		||||
	return len(cl)
 | 
			
		||||
func (user *User) HandleTextMessage(message groupme.Message) {
 | 
			
		||||
	id := database.ParsePortalKey(message.GroupID.String())
 | 
			
		||||
 | 
			
		||||
	if id == nil {
 | 
			
		||||
		id = database.ParsePortalKey(message.ConversationID.String())
 | 
			
		||||
	}
 | 
			
		||||
	if id == nil {
 | 
			
		||||
		user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user.messageInput <- PortalMessage{*id, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cl ChatList) Less(i, j int) bool {
 | 
			
		||||
	return cl[i].LastMessageTime > cl[j].LastMessageTime
 | 
			
		||||
func (user *User) HandleLike(msg groupme.Message) {
 | 
			
		||||
	user.HandleTextMessage(msg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cl ChatList) Swap(i, j int) {
 | 
			
		||||
	cl[i], cl[j] = cl[j], cl[i]
 | 
			
		||||
func (user *User) HandleJoin(id groupme.ID) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
	//TODO: efficient
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) PostLogin() {
 | 
			
		||||
	user.bridge.Metrics.TrackConnectionState(user.GMID, true)
 | 
			
		||||
	user.bridge.Metrics.TrackLoginState(user.GMID, true)
 | 
			
		||||
	user.bridge.Metrics.TrackBufferLength(user.MXID, 0)
 | 
			
		||||
	user.log.Debugln("Locking processing of incoming messages and starting post-login sync")
 | 
			
		||||
	user.syncWait.Add(1)
 | 
			
		||||
	user.syncStart <- struct{}{}
 | 
			
		||||
	go user.intPostLogin()
 | 
			
		||||
func (user *User) HandleGroupName(group groupme.ID, newName string) {
 | 
			
		||||
	p := user.GetPortalByGMID(group)
 | 
			
		||||
	if p != nil {
 | 
			
		||||
		p.UpdateName(newName, "", false)
 | 
			
		||||
		//get more info abt actual user TODO
 | 
			
		||||
	}
 | 
			
		||||
	//bugs atm with above?
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupTopic(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupMembership(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
	//TODO
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupAvatar(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleLikeIcon(_ groupme.ID, _, _ int, _ string) {
 | 
			
		||||
	//TODO
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleNewNickname(groupID, userID groupme.ID, name string) {
 | 
			
		||||
	puppet := user.bridge.GetPuppetByGMID(userID)
 | 
			
		||||
	if puppet != nil {
 | 
			
		||||
		puppet.UpdateName(groupme.Member{
 | 
			
		||||
			Nickname: name,
 | 
			
		||||
			UserID:   userID,
 | 
			
		||||
		}, false)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleNewAvatarInGroup(groupID, userID groupme.ID, url string) {
 | 
			
		||||
	puppet := user.bridge.GetPuppetByGMID(userID)
 | 
			
		||||
	puppet.UpdateAvatar(user, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleMembers(_ groupme.ID, _ []groupme.Member, _ bool) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleError(err error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleJSONParseError(err error) {
 | 
			
		||||
	user.log.Errorln("GroupMe JSON parse error:", err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleChatList() {
 | 
			
		||||
	chatMap := map[groupme.ID]*groupme.Group{}
 | 
			
		||||
	chats, err := user.Client.IndexAllGroups()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Errorln("chat sync error", err) //TODO: handle
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, chat := range chats {
 | 
			
		||||
		chatMap[chat.ID] = chat
 | 
			
		||||
	}
 | 
			
		||||
	user.GroupList = chatMap
 | 
			
		||||
 | 
			
		||||
	dmMap := map[groupme.ID]*groupme.Chat{}
 | 
			
		||||
	dms, err := user.Client.IndexAllChats()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Errorln("chat sync error", err) //TODO: handle
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, dm := range dms {
 | 
			
		||||
		dmMap[dm.OtherUser.ID] = dm
 | 
			
		||||
	}
 | 
			
		||||
	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
 | 
			
		||||
 | 
			
		||||
	user.log.Infoln("Chat list received")
 | 
			
		||||
	user.chatListReceived <- struct{}{}
 | 
			
		||||
	go user.syncPortals(false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Private Methods
 | 
			
		||||
 | 
			
		||||
func (user *User) handleMessageLoop() {
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case msg := <-user.messageOutput:
 | 
			
		||||
			user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
 | 
			
		||||
			puppet := user.bridge.GetPuppetByGMID(msg.data.UserID)
 | 
			
		||||
			portal := user.bridge.GetPortalByGMID(msg.chat)
 | 
			
		||||
			if puppet != nil {
 | 
			
		||||
				puppet.Sync(nil, &groupme.Member{
 | 
			
		||||
					UserID:   msg.data.UserID,
 | 
			
		||||
					Nickname: msg.data.Name,
 | 
			
		||||
					ImageURL: msg.data.AvatarURL,
 | 
			
		||||
				}, false, false)
 | 
			
		||||
			}
 | 
			
		||||
			portal.messages <- msg
 | 
			
		||||
		case <-user.syncStart:
 | 
			
		||||
			user.log.Debugln("Processing of incoming messages is locked")
 | 
			
		||||
			user.bridge.Metrics.TrackSyncLock(user.GMID, true)
 | 
			
		||||
			user.syncWait.Wait()
 | 
			
		||||
			user.bridge.Metrics.TrackSyncLock(user.GMID, false)
 | 
			
		||||
			user.log.Debugln("Processing of incoming messages unlocked")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) addToGMIDMap() {
 | 
			
		||||
	user.bridge.usersLock.Lock()
 | 
			
		||||
	user.bridge.usersByGMID[user.GMID] = user
 | 
			
		||||
	user.bridge.usersLock.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) removeFromGMIDMap() {
 | 
			
		||||
	user.bridge.usersLock.Lock()
 | 
			
		||||
	jidUser, ok := user.bridge.usersByGMID[user.GMID]
 | 
			
		||||
	if ok && user == jidUser {
 | 
			
		||||
		delete(user.bridge.usersByGMID, user.GMID)
 | 
			
		||||
	}
 | 
			
		||||
	user.bridge.usersLock.Unlock()
 | 
			
		||||
	user.bridge.Metrics.TrackLoginState(user.GMID, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) (ok bool) {
 | 
			
		||||
	extraContent := make(map[string]interface{})
 | 
			
		||||
	if isDirect {
 | 
			
		||||
		extraContent["is_direct"] = true
 | 
			
		||||
	}
 | 
			
		||||
	customPuppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if customPuppet != nil && customPuppet.CustomIntent() != nil {
 | 
			
		||||
		extraContent["fi.mau.will_auto_accept"] = true
 | 
			
		||||
	}
 | 
			
		||||
	_, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: user.MXID}, extraContent)
 | 
			
		||||
	var httpErr mautrix.HTTPError
 | 
			
		||||
	if err != nil && errors.As(err, &httpErr) && httpErr.RespError != nil && strings.Contains(httpErr.RespError.Err, "is already in the room") {
 | 
			
		||||
		user.bridge.StateStore.SetMembership(roomID, user.MXID, event.MembershipJoin)
 | 
			
		||||
		ok = true
 | 
			
		||||
		return
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		user.log.Warnfln("Failed to invite user to %s: %v", roomID, err)
 | 
			
		||||
	} else {
 | 
			
		||||
		ok = true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if customPuppet != nil && customPuppet.CustomIntent() != nil {
 | 
			
		||||
		err = customPuppet.CustomIntent().EnsureJoined(roomID, appservice.EnsureJoinedParams{IgnoreCache: true})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			user.log.Warnfln("Failed to auto-join %s: %v", roomID, err)
 | 
			
		||||
			ok = false
 | 
			
		||||
		} else {
 | 
			
		||||
			ok = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) tryAutomaticDoublePuppeting() {
 | 
			
		||||
@@ -553,51 +693,6 @@ func (user *User) intPostLogin() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleChatList() {
 | 
			
		||||
	chatMap := map[groupme.ID]groupme.Group{}
 | 
			
		||||
	chats, err := user.Client.IndexAllGroups()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Errorln("chat sync error", err) //TODO: handle
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, chat := range chats {
 | 
			
		||||
		chatMap[chat.ID] = *chat
 | 
			
		||||
	}
 | 
			
		||||
	user.GroupList = chatMap
 | 
			
		||||
 | 
			
		||||
	dmMap := map[groupme.ID]groupme.Chat{}
 | 
			
		||||
	dms, err := user.Client.IndexAllChats()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Errorln("chat sync error", err) //TODO: handle
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, dm := range dms {
 | 
			
		||||
		dmMap[dm.OtherUser.ID] = *dm
 | 
			
		||||
	}
 | 
			
		||||
	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)
 | 
			
		||||
	}
 | 
			
		||||
	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{}{}
 | 
			
		||||
	go user.syncPortals(false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Syncs chats & group messages
 | 
			
		||||
func (user *User) syncPortals(createAll bool) {
 | 
			
		||||
	user.log.Infoln("Reading chat list")
 | 
			
		||||
@@ -612,15 +707,18 @@ func (user *User) syncPortals(createAll bool) {
 | 
			
		||||
		chats = append(chats, Chat{
 | 
			
		||||
			Portal:          portal,
 | 
			
		||||
			LastMessageTime: uint64(group.UpdatedAt.ToTime().Unix()),
 | 
			
		||||
			Group:           &group,
 | 
			
		||||
			Group:           group,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	for _, dm := range user.ChatList {
 | 
			
		||||
		portal := user.bridge.GetPortalByGMID(database.NewPortalKey(dm.LastMessage.ConversationID, user.GMID))
 | 
			
		||||
		portal := user.bridge.GetPortalByGMID(database.NewPortalKey(dm.OtherUser.ID, user.GMID))
 | 
			
		||||
		portal.Name = dm.OtherUser.Name
 | 
			
		||||
		portal.NameSet = true
 | 
			
		||||
 | 
			
		||||
		chats = append(chats, Chat{
 | 
			
		||||
			Portal:          portal,
 | 
			
		||||
			LastMessageTime: uint64(dm.UpdatedAt.ToTime().Unix()),
 | 
			
		||||
			DM:              &dm,
 | 
			
		||||
			DM:              dm,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -655,13 +753,16 @@ func (user *User) syncPortals(createAll bool) {
 | 
			
		||||
		go func(chat Chat, i int) {
 | 
			
		||||
			create := (int64(chat.LastMessageTime) >= user.lastReconnection && user.lastReconnection > 0) || i < limit
 | 
			
		||||
			if len(chat.Portal.MXID) > 0 || create || createAll {
 | 
			
		||||
				chat.Portal.Sync(user, chat.Group)
 | 
			
		||||
				if chat.Group != nil {
 | 
			
		||||
					chat.Portal.SyncGroup(user, chat.Group)
 | 
			
		||||
				} else {
 | 
			
		||||
					chat.Portal.SyncDM(user, chat.DM)
 | 
			
		||||
				}
 | 
			
		||||
				//err := chat.Portal.BackfillHistory(user, chat.LastMessageTime)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					chat.Portal.log.Errorln("Error backfilling history:", err)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			wg.Done()
 | 
			
		||||
		}(chat, i)
 | 
			
		||||
 | 
			
		||||
@@ -687,70 +788,8 @@ func (user *User) getDirectChats() map[id.UserID][]id.RoomID {
 | 
			
		||||
	return res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) UpdateDirectChats(chats map[id.UserID][]id.RoomID) {
 | 
			
		||||
	if !user.bridge.Config.Bridge.SyncDirectChatList {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	puppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
 | 
			
		||||
	if puppet == nil || puppet.CustomIntent() == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	intent := puppet.CustomIntent()
 | 
			
		||||
	method := http.MethodPatch
 | 
			
		||||
	if chats == nil {
 | 
			
		||||
		chats = user.getDirectChats()
 | 
			
		||||
		method = http.MethodPut
 | 
			
		||||
	}
 | 
			
		||||
	user.log.Debugln("Updating m.direct list on homeserver")
 | 
			
		||||
	var err error
 | 
			
		||||
	if user.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareAsmux {
 | 
			
		||||
		urlPath := intent.BuildClientURL("unstable", "com.beeper.asmux", "dms")
 | 
			
		||||
		_, err = intent.MakeFullRequest(mautrix.FullRequest{
 | 
			
		||||
			Method:      method,
 | 
			
		||||
			URL:         urlPath,
 | 
			
		||||
			Headers:     http.Header{"X-Asmux-Auth": {user.bridge.AS.Registration.AppToken}},
 | 
			
		||||
			RequestJSON: chats,
 | 
			
		||||
		})
 | 
			
		||||
	} else {
 | 
			
		||||
		existingChats := make(map[id.UserID][]id.RoomID)
 | 
			
		||||
		err = intent.GetAccountData(event.AccountDataDirectChats.Type, &existingChats)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			user.log.Warnln("Failed to get m.direct list to update it:", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		for userID, rooms := range existingChats {
 | 
			
		||||
			if _, ok := user.bridge.ParsePuppetMXID(userID); !ok {
 | 
			
		||||
				// This is not a ghost user, include it in the new list
 | 
			
		||||
				chats[userID] = rooms
 | 
			
		||||
			} else if _, ok := chats[userID]; !ok && method == http.MethodPatch {
 | 
			
		||||
				// This is a ghost user, but we're not replacing the whole list, so include it too
 | 
			
		||||
				chats[userID] = rooms
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		err = intent.SetAccountData(event.AccountDataDirectChats.Type, &chats)
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		user.log.Warnln("Failed to update m.direct list:", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleError(err error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) ShouldCallSynchronously() bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleJSONParseError(err error) {
 | 
			
		||||
	user.log.Errorln("GroupMe JSON parse error:", err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) PortalKey(gmid groupme.ID) database.PortalKey {
 | 
			
		||||
	return database.NewPortalKey(gmid, user.GMID)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) GetPortalByGMID(gmid groupme.ID) *Portal {
 | 
			
		||||
	return user.bridge.GetPortalByGMID(user.PortalKey(gmid))
 | 
			
		||||
func (user *User) updateAvatar(gmdi groupme.ID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool, log log.Logger, intent *appservice.IntentAPI) bool {
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) runMessageRingBuffer() {
 | 
			
		||||
@@ -765,103 +804,3 @@ func (user *User) runMessageRingBuffer() {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) handleMessageLoop() {
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case msg := <-user.messageOutput:
 | 
			
		||||
			user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
 | 
			
		||||
			puppet := user.bridge.GetPuppetByGMID(msg.data.UserID)
 | 
			
		||||
			portal := user.bridge.GetPortalByGMID(msg.chat)
 | 
			
		||||
			if puppet != nil {
 | 
			
		||||
				puppet.Sync(nil, &groupme.Member{
 | 
			
		||||
					UserID:   msg.data.UserID,
 | 
			
		||||
					Nickname: msg.data.Name,
 | 
			
		||||
					ImageURL: msg.data.AvatarURL,
 | 
			
		||||
				}, false, false)
 | 
			
		||||
			}
 | 
			
		||||
			portal.messages <- msg
 | 
			
		||||
		case <-user.syncStart:
 | 
			
		||||
			user.log.Debugln("Processing of incoming messages is locked")
 | 
			
		||||
			user.bridge.Metrics.TrackSyncLock(user.GMID, true)
 | 
			
		||||
			user.syncWait.Wait()
 | 
			
		||||
			user.bridge.Metrics.TrackSyncLock(user.GMID, false)
 | 
			
		||||
			user.log.Debugln("Processing of incoming messages unlocked")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleTextMessage(message groupme.Message) {
 | 
			
		||||
	id := database.ParsePortalKey(message.GroupID.String())
 | 
			
		||||
 | 
			
		||||
	if id == nil {
 | 
			
		||||
		id = database.ParsePortalKey(message.ConversationID.String())
 | 
			
		||||
	}
 | 
			
		||||
	if id == nil {
 | 
			
		||||
		user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user.messageInput <- PortalMessage{*id, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleLike(msg groupme.Message) {
 | 
			
		||||
	user.HandleTextMessage(msg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleJoin(id groupme.ID) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
	//TODO: efficient
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupName(group groupme.ID, newName string) {
 | 
			
		||||
	p := user.GetPortalByGMID(group)
 | 
			
		||||
	if p != nil {
 | 
			
		||||
		p.UpdateName(newName, "", false)
 | 
			
		||||
		//get more info abt actual user TODO
 | 
			
		||||
	}
 | 
			
		||||
	//bugs atm with above?
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupTopic(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
func (user *User) HandleGroupMembership(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
	//TODO
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleGroupAvatar(_ groupme.ID, _ string) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleLikeIcon(_ groupme.ID, _, _ int, _ string) {
 | 
			
		||||
	//TODO
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleNewNickname(groupID, userID groupme.ID, name string) {
 | 
			
		||||
	puppet := user.bridge.GetPuppetByGMID(userID)
 | 
			
		||||
	if puppet != nil {
 | 
			
		||||
		puppet.UpdateName(groupme.Member{
 | 
			
		||||
			Nickname: name,
 | 
			
		||||
			UserID:   userID,
 | 
			
		||||
		}, false)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleNewAvatarInGroup(groupID, userID groupme.ID, url string) {
 | 
			
		||||
	puppet := user.bridge.GetPuppetByGMID(userID)
 | 
			
		||||
	puppet.UpdateAvatar(user, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (user *User) HandleMembers(_ groupme.ID, _ []groupme.Member, _ bool) {
 | 
			
		||||
	user.HandleChatList()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type FakeMessage struct {
 | 
			
		||||
	Text  string
 | 
			
		||||
	ID    string
 | 
			
		||||
	Alert bool
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user