From 0b18b231bb8f4a4ee7d1b6c4503a357ad3e9b1ed Mon Sep 17 00:00:00 2001 From: zhaoYangguang <1163765691@qq.com> Date: Fri, 6 Nov 2020 19:42:34 +0800 Subject: [PATCH] Optimize the synchronization of skype group names without topics --- portal.go | 98 +++++++++++++++++++++++++++++++++++++++++-------------- user.go | 30 ++++++++++++++++- 2 files changed, 103 insertions(+), 25 deletions(-) diff --git a/portal.go b/portal.go index d3ad95b..5328fc7 100644 --- a/portal.go +++ b/portal.go @@ -533,19 +533,37 @@ func (portal *Portal) UpdateMetadata(user *User) bool { } portalName := "" + noRoomTopic := false names := strings.Split(metadata.Name, ", ") for _, name := range names { - if strings.Index(name, ":") > 0 { - key := "8:" + name + skypeExt.NewUserSuffix - if key == user.JID { - continue - } - if contact, ok := user.Conn.Store.Contacts[key]; ok { - portalName += contact.DisplayName - } + key := "8:" + name + skypeExt.NewUserSuffix + if key == user.JID { + noRoomTopic = true } } - if len(portalName) < 1 { + if noRoomTopic { + for index, participant := range metadata.Participants { + fmt.Println() + fmt.Printf("metadata.Participants1: %+v", participant) + fmt.Println() + + if participant.JID == user.JID { + continue + } + if contact, ok := user.Conn.Store.Contacts[participant.JID]; ok { + if len(portalName) == 0 { + portalName = contact.DisplayName + } else { + if index > 5 { + portalName = portalName + ", ..." + break + } else { + portalName = portalName + ", " + contact.DisplayName + } + } + } + } + } else { portalName = metadata.Name } // portal.Topic = "" @@ -1085,25 +1103,36 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { metadata, err = user.Conn.GetGroupMetaData(portal.Key.JID) if err == nil { portalName := "" + noRoomTopic := false names := strings.Split(metadata.Name, ", ") for _, name := range names { - if strings.Index(name, ":") > 0 { - key := "8:" + name + skypeExt.NewUserSuffix - if key == user.JID { - continue - } - if contact, ok := user.Conn.Store.Contacts[key]; ok { - if len(portalName) > 0 { - portalName = portalName + ", " + contact.DisplayName - } else { - portalName = contact.DisplayName - } - } - } else if name == "..." { - portalName = portalName + ", ..." + key := "8:" + name + skypeExt.NewUserSuffix + if key == user.JID { + noRoomTopic = true } } - if len(portalName) > 0 { + if noRoomTopic { + for index, participant := range metadata.Participants { + fmt.Println() + fmt.Printf("metadata.Participants2: %+v", participant) + fmt.Println() + + if participant.JID == user.JID { + continue + } + if contact, ok := user.Conn.Store.Contacts[participant.JID]; ok { + if len(portalName) == 0 { + portalName = contact.DisplayName + } else { + if index > 5 { + portalName = portalName + ", ..." + break + } else { + portalName = portalName + ", " + contact.DisplayName + } + } + } + } portal.Name = portalName } else { portal.Name = metadata.Name @@ -2391,6 +2420,27 @@ func (portal *Portal) GetMatrixUsers() ([]id.UserID, error) { return users, nil } +func (portal *Portal) GetPuppets() ([]struct { + DisplayName *string `json:"display_name"` + AvatarURL *string `json:"avatar_url"` +}, error) { + members, err := portal.MainIntent().JoinedMembers(portal.MXID) + if err != nil { + return nil, errors.Wrap(err, "failed to get member list") + } + var puppets []struct { + DisplayName *string `json:"display_name"` + AvatarURL *string `json:"avatar_url"` + } + for userID := range members.Joined { + _, isPuppet := portal.bridge.ParsePuppetMXID(userID) + if isPuppet && userID != portal.bridge.Bot.UserID { + puppets = append(puppets, members.Joined[userID]) + } + } + return puppets, nil +} + func (portal *Portal) CleanupIfEmpty() { users, err := portal.GetMatrixUsers() if err != nil { diff --git a/user.go b/user.go index 54aa7a5..f567ffe 100644 --- a/user.go +++ b/user.go @@ -1007,8 +1007,36 @@ func (user *User) HandleChatUpdate(cmd skype.Resource) { topicContent := skype.ChatTopicContent{} //把xml数据解析成bs对象 xml.Unmarshal([]byte(cmd.Content), &topicContent) + portalName := "" + noRoomTopic := false + names := strings.Split(cmd.ThreadTopic, ", ") + for _, name := range names { + key := "8:" + name + skypeExt.NewUserSuffix + if key == user.JID { + noRoomTopic = true + } + } + if noRoomTopic { + participants, _ := portal.GetPuppets() + for index, participant := range participants { + if *participant.DisplayName != user.Conn.LoginInfo.Username { + if len(portalName) == 0 { + portalName = *participant.DisplayName + } else { + if index > 5 { + portalName = portalName + ", ..." + break + } else { + portalName = *participant.DisplayName + ", " + portalName + } + } + } + } + } else { + portalName = cmd.ThreadTopic + } cmd.SendId = topicContent.Initiator + skypeExt.NewUserSuffix - go portal.UpdateName(cmd.ThreadTopic, cmd.SendId) + go portal.UpdateName(portalName, cmd.SendId) case skypeExt.ChatPictureUpdate: topicContent := skype.ChatPictureContent{} //把xml数据解析成bs对象