Fix some bugs

This commit is contained in:
Tulir Asokan 2023-07-02 23:47:31 +03:00
parent 63284481d7
commit 2b37a6d718
6 changed files with 21 additions and 11 deletions

View file

@ -40,6 +40,7 @@ func (user *User) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
if mxid != user.MXID { if mxid != user.MXID {
return errors.New("mismatching mxid") return errors.New("mismatching mxid")
} }
user.DoublePuppetIntent = nil
user.AccessToken = accessToken user.AccessToken = accessToken
return user.startCustomMXID(false) return user.startCustomMXID(false)
} }
@ -129,7 +130,7 @@ func (user *User) clearCustomMXID() {
} }
func (user *User) startCustomMXID(reloginOnFail bool) error { func (user *User) startCustomMXID(reloginOnFail bool) error {
if len(user.AccessToken) == 0 { if len(user.AccessToken) == 0 || user.DoublePuppetIntent != nil {
return nil return nil
} }
intent, err := user.newDoublePuppetIntent() intent, err := user.newDoublePuppetIntent()

View file

@ -74,20 +74,20 @@ type Message struct {
func (msg *Message) Scan(row dbutil.Scannable) (*Message, error) { func (msg *Message) Scan(row dbutil.Scannable) (*Message, error) {
var ts int64 var ts int64
err := row.Scan(&msg.Chat.ID, &msg.Chat.Receiver, &msg.ID, &msg.MXID, &msg.Sender, &msg.Timestamp) err := row.Scan(&msg.Chat.ID, &msg.Chat.Receiver, &msg.ID, &msg.MXID, &msg.Sender, &ts)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {
return nil, err return nil, err
} }
if ts != 0 { if ts != 0 {
msg.Timestamp = time.UnixMilli(ts) msg.Timestamp = time.UnixMicro(ts)
} }
return msg, nil return msg, nil
} }
func (msg *Message) sqlVariables() []any { func (msg *Message) sqlVariables() []any {
return []any{msg.Chat.ID, msg.Chat.Receiver, msg.ID, msg.MXID, msg.Sender, msg.Timestamp.UnixMilli()} return []any{msg.Chat.ID, msg.Chat.Receiver, msg.ID, msg.MXID, msg.Sender, msg.Timestamp.UnixMicro()}
} }
func (msg *Message) Insert(ctx context.Context) error { func (msg *Message) Insert(ctx context.Context) error {

View file

@ -112,7 +112,7 @@ func (portal *Portal) sqlVariables() []any {
if portal.OtherUserID != "" { if portal.OtherUserID != "" {
otherUserID = &portal.OtherUserID otherUserID = &portal.OtherUserID
} }
return []any{portal.ID, portal.Receiver, selfUserID, otherUserID, mxid, portal.Name, portal.NameSet, portal.AvatarID, portal.AvatarMXC, portal.AvatarSet, portal.Encrypted, portal.InSpace} return []any{portal.ID, portal.Receiver, selfUserID, otherUserID, mxid, portal.Name, portal.NameSet, portal.AvatarID, &portal.AvatarMXC, portal.AvatarSet, portal.Encrypted, portal.InSpace}
} }
func (portal *Portal) Insert(ctx context.Context) error { func (portal *Portal) Insert(ctx context.Context) error {

View file

@ -44,7 +44,7 @@ func (pq *PuppetQuery) GetAll(ctx context.Context) ([]*Puppet, error) {
} }
func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) { func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) {
return get[*Puppet](pq, ctx, "SELECT id, receiver, phone, name, name_set, avatar_id, avatar_mxc, avatar_set, contact_info_set FROM puppet WHERE phone=$1 AND receiver=$2", key.ID, key.Receiver) return get[*Puppet](pq, ctx, "SELECT id, receiver, phone, name, name_set, avatar_id, avatar_mxc, avatar_set, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2", key.ID, key.Receiver)
} }
type Puppet struct { type Puppet struct {
@ -71,7 +71,7 @@ func (puppet *Puppet) Scan(row dbutil.Scannable) (*Puppet, error) {
} }
func (puppet *Puppet) sqlVariables() []any { func (puppet *Puppet) sqlVariables() []any {
return []any{puppet.ID, puppet.Receiver, puppet.Phone, puppet.Name, puppet.NameSet, puppet.AvatarID, puppet.AvatarMXC, puppet.AvatarSet, puppet.ContactInfoSet} return []any{puppet.ID, puppet.Receiver, puppet.Phone, puppet.Name, puppet.NameSet, puppet.AvatarID, &puppet.AvatarMXC, puppet.AvatarSet, puppet.ContactInfoSet}
} }
func (puppet *Puppet) Insert(ctx context.Context) error { func (puppet *Puppet) Insert(ctx context.Context) error {

View file

@ -305,6 +305,7 @@ func (portal *Portal) isOutgoingMessage(evt *binary.Message) id.EventID {
defer portal.outgoingMessagesLock.Unlock() defer portal.outgoingMessagesLock.Unlock()
evtID, ok := portal.outgoingMessages[evt.TmpId] evtID, ok := portal.outgoingMessages[evt.TmpId]
if ok { if ok {
delete(portal.outgoingMessages, evt.TmpId)
portal.markHandled(evt, map[string]id.EventID{"": evtID}, true) portal.markHandled(evt, map[string]id.EventID{"": evtID}, true)
return evtID return evtID
} }
@ -492,14 +493,17 @@ func (portal *Portal) UpdateName(name string, updateInfo bool) bool {
func (portal *Portal) UpdateMetadata(user *User, info *binary.Conversation) []id.UserID { func (portal *Portal) UpdateMetadata(user *User, info *binary.Conversation) []id.UserID {
participants := portal.SyncParticipants(user, info) participants := portal.SyncParticipants(user, info)
if portal.IsPrivateChat() {
return participants
}
update := false update := false
if portal.SelfUserID != info.SelfParticipantID {
portal.SelfUserID = info.SelfParticipantID
update = true
}
if portal.MXID != "" { if portal.MXID != "" {
update = portal.addToPersonalSpace(user) || update update = portal.addToPersonalSpace(user) || update
} }
if portal.shouldSetDMRoomMetadata() {
update = portal.UpdateName(info.Name, false) || update update = portal.UpdateName(info.Name, false) || update
}
// TODO avatar // TODO avatar
if update { if update {
err := portal.Update(context.TODO()) err := portal.Update(context.TODO())

View file

@ -438,8 +438,12 @@ func (user *User) Connect() bool {
} else if user.Session == nil { } else if user.Session == nil {
return false return false
} }
if len(user.AccessToken) == 0 {
user.tryAutomaticDoublePuppeting()
}
user.zlog.Debug().Msg("Connecting to Google Messages") user.zlog.Debug().Msg("Connecting to Google Messages")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnecting, Error: WAConnecting}) user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnecting, Error: WAConnecting})
user.createClient()
err := user.Client.Connect(user.Session.WebAuthKey) err := user.Client.Connect(user.Session.WebAuthKey)
if err != nil { if err != nil {
user.zlog.Err(err).Msg("Error connecting to Google Messages") user.zlog.Err(err).Msg("Error connecting to Google Messages")
@ -523,6 +527,7 @@ func (user *User) HandleEvent(event interface{}) {
// These should be here // These should be here
user.zlog.Info().Msg(v.URL) user.zlog.Info().Msg(v.URL)
case *events.PairSuccessful: case *events.PairSuccessful:
user.tryAutomaticDoublePuppeting()
user.Phone = v.PairDeviceData.Mobile.RegistrationID user.Phone = v.PairDeviceData.Mobile.RegistrationID
user.Session.PhoneInfo = v.PairDeviceData.Mobile user.Session.PhoneInfo = v.PairDeviceData.Mobile
user.Session.BrowserInfo = v.PairDeviceData.Browser user.Session.BrowserInfo = v.PairDeviceData.Browser