Somewhat correctly setting up rooms and puppets

This commit is contained in:
2023-09-07 14:01:41 -05:00
parent d6f94287d4
commit 86c6bab1eb
5 changed files with 1315 additions and 1279 deletions

150
puppet.go
View File

@@ -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()