From b067459739c79ac6bda58012bfe83eeba822d51e Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 15 Sep 2023 16:49:25 -0500 Subject: [PATCH] Fixed bug where users were not authenticated until restart --- portal.go | 102 ++++-------------------------------------------------- user.go | 25 ++++++------- 2 files changed, 16 insertions(+), 111 deletions(-) diff --git a/portal.go b/portal.go index bd4e10e..0f47695 100644 --- a/portal.go +++ b/portal.go @@ -189,8 +189,6 @@ func (portal *Portal) SyncDM(user *User, dm *groupme.Chat) { } update := false - - update = portal.updateMetadata(user) || update update = portal.UpdateAvatar(user, dm.OtherUser.AvatarURL, false) || update if update { @@ -618,7 +616,6 @@ func (portal *Portal) handleMessage(msg PortalMessage) { return } portal.HandleTextMessage(msg.source, msg.data) - 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) { @@ -794,91 +791,6 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment // return nil, true, errors.New("Unknown type") } -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 { @@ -911,18 +823,14 @@ func (portal *Portal) createMatrixRoom(user *User) error { Type: event.StateHalfShotBridge, Content: event.Content{Parsed: bridgeInfo}, StateKey: &bridgeInfoStateKey, - }} - - spaceIDStr := user.SpaceId - initialState = append(initialState, &event.Event{ + }, { Type: event.StateSpaceParent, - StateKey: &spaceIDStr, + StateKey: &user.SpaceId, Content: event.Content{Parsed: &event.SpaceParentEventContent{ Via: []string{portal.bridge.AS.HomeserverDomain}, Canonical: true, }}, - }) - initialState = append(initialState, &event.Event{ + }, { Type: event.StateJoinRules, Content: event.Content{Parsed: &event.JoinRulesEventContent{ JoinRule: event.JoinRuleRestricted, @@ -931,7 +839,9 @@ func (portal *Portal) createMatrixRoom(user *User) error { Type: event.JoinRuleAllowRoomMembership, }}, }}, - }) + }} + + //initialState = append(initialState, event.Event) if !portal.AvatarURL.IsEmpty() { initialState = append(initialState, &event.Event{ Type: event.StateRoomAvatar, diff --git a/user.go b/user.go index fadfdf9..07656e6 100644 --- a/user.go +++ b/user.go @@ -226,8 +226,8 @@ func (user *User) Login(token string) error { user.Token = token user.addToGMIDMap() - user.PostLogin() if user.Connect() { + user.PostLogin() return nil } return errors.New("failed to connect") @@ -260,6 +260,14 @@ func (user *User) Connect() bool { conn := groupme.NewPushSubscription(context.Background()) user.Conn = &conn user.Conn.StartListening(context.Background(), groupmeext.NewFayeClient(user.log)) + user.Client = groupmeext.NewClient(user.Token) + if len(user.GMID) == 0 { + myuser, err := user.Client.MyUser(context.TODO()) + if err != nil { + log.Fatal(err) //TODO + } + user.GMID = myuser.ID + } user.Conn.AddFullHandler(user) @@ -422,13 +430,7 @@ 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 @@ -602,14 +604,7 @@ func (user *User) postConnPing() bool { func (user *User) intPostLogin() { defer user.syncWait.Done() user.lastReconnection = time.Now().Unix() - user.Client = groupmeext.NewClient(user.Token) - if len(user.GMID) == 0 { - myuser, err := user.Client.MyUser(context.TODO()) - if err != nil { - log.Fatal(err) //TODO - } - user.GMID = myuser.ID - } + user.Update() user.tryAutomaticDoublePuppeting()