github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/cache.go (about)

     1  package client
     2  
     3  import (
     4  	"github.com/LagrangeDev/LagrangeGo/client/entity"
     5  )
     6  
     7  // GetUid 获取缓存中对应uin的uid
     8  func (c *QQClient) GetUid(uin uint32, groupUin ...uint32) string {
     9  	if c.cache.FriendCacheIsEmpty() {
    10  		if err := c.RefreshFriendCache(); err != nil {
    11  			return ""
    12  		}
    13  	}
    14  	return c.cache.GetUid(uin, groupUin...)
    15  }
    16  
    17  // GetUin 获取缓存中对应的uin
    18  func (c *QQClient) GetUin(uid string, groupUin ...uint32) uint32 {
    19  	if len(groupUin) == 0 && c.cache.FriendCacheIsEmpty() {
    20  		if err := c.RefreshFriendCache(); err != nil {
    21  			return 0
    22  		}
    23  	}
    24  	if len(groupUin) != 0 && c.cache.GroupMemberCacheIsEmpty(groupUin[0]) {
    25  		if err := c.RefreshGroupMembersCache(groupUin[0]); err != nil {
    26  			return 0
    27  		}
    28  	}
    29  	return c.cache.GetUin(uid, groupUin...)
    30  }
    31  
    32  // GetCachedFriendInfo 获取好友信息(缓存)
    33  func (c *QQClient) GetCachedFriendInfo(uin uint32) (*entity.Friend, error) {
    34  	if c.cache.FriendCacheIsEmpty() {
    35  		if err := c.RefreshFriendCache(); err != nil {
    36  			return nil, err
    37  		}
    38  	}
    39  	return c.cache.GetFriend(uin), nil
    40  }
    41  
    42  // GetCachedGroupInfo 获取群信息(缓存)
    43  func (c *QQClient) GetCachedGroupInfo(groupUin uint32) (*entity.Group, error) {
    44  	if c.cache.GroupInfoCacheIsEmpty() {
    45  		if err := c.RefreshAllGroupsInfo(); err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  	return c.cache.GetGroupInfo(groupUin), nil
    50  }
    51  
    52  // GetCachedAllGroupsInfo 获取所有群信息(缓存)
    53  func (c *QQClient) GetCachedAllGroupsInfo() (map[uint32]*entity.Group, error) {
    54  	if c.cache.GroupInfoCacheIsEmpty() {
    55  		if err := c.RefreshAllGroupsInfo(); err != nil {
    56  			return nil, err
    57  		}
    58  	}
    59  	return c.cache.GetAllGroupsInfo(), nil
    60  }
    61  
    62  // GetCachedMemberInfo 获取群成员信息(缓存)
    63  func (c *QQClient) GetCachedMemberInfo(uin, groupUin uint32) (*entity.GroupMember, error) {
    64  	if c.cache.GroupMemberCacheIsEmpty(groupUin) {
    65  		if err := c.RefreshGroupMembersCache(groupUin); err != nil {
    66  			return nil, err
    67  		}
    68  	}
    69  	return c.cache.GetGroupMember(uin, groupUin), nil
    70  }
    71  
    72  // GetCachedMembersInfo 获取指定群所有群成员信息(缓存)
    73  func (c *QQClient) GetCachedMembersInfo(groupUin uint32) (map[uint32]*entity.GroupMember, error) {
    74  	if c.cache.GroupMemberCacheIsEmpty(groupUin) {
    75  		if err := c.RefreshGroupMembersCache(groupUin); err != nil {
    76  			return nil, err
    77  		}
    78  	}
    79  	return c.cache.GetGroupMembers(groupUin), nil
    80  }
    81  
    82  // RefreshFriendCache 刷新好友缓存
    83  func (c *QQClient) RefreshFriendCache() error {
    84  	friendsData, err := c.GetFriendsData()
    85  	if err != nil {
    86  		return err
    87  	}
    88  	c.cache.RefreshAllFriend(friendsData)
    89  	return nil
    90  }
    91  
    92  // RefreshGroupMembersCache 刷新指定群的群成员员缓存
    93  func (c *QQClient) RefreshGroupMembersCache(groupUin uint32) error {
    94  	groupData, err := c.GetGroupMembersData(groupUin)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	c.cache.RefreshGroupMembers(groupUin, groupData)
    99  	return nil
   100  }
   101  
   102  // RefreshAllGroupMembersCache 刷新所有群的群成员缓存
   103  func (c *QQClient) RefreshAllGroupMembersCache() error {
   104  	groupsData, err := c.GetAllGroupsMembersData()
   105  	if err != nil {
   106  		return err
   107  	}
   108  	c.cache.RefreshAllGroupMembers(groupsData)
   109  	return nil
   110  }
   111  
   112  // RefreshAllGroupsInfo 刷新所有群信息缓存
   113  func (c *QQClient) RefreshAllGroupsInfo() error {
   114  	groupsData, err := c.GetAllGroupsInfo()
   115  	if err != nil {
   116  		return err
   117  	}
   118  	c.cache.RefreshAllGroup(groupsData)
   119  	return nil
   120  }
   121  
   122  // GetFriendsData 获取好友列表数据
   123  func (c *QQClient) GetFriendsData() (map[uint32]*entity.Friend, error) {
   124  	friends, err := c.FetchFriends()
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	friendsData := make(map[uint32]*entity.Friend, len(friends))
   129  	for _, friend := range friends {
   130  		friendsData[friend.Uin] = friend
   131  	}
   132  	c.info("获取%d个好友", len(friendsData))
   133  	return friendsData, err
   134  }
   135  
   136  // GetGroupMembersData 获取指定群所有成员信息
   137  func (c *QQClient) GetGroupMembersData(groupUin uint32) (map[uint32]*entity.GroupMember, error) {
   138  	groupMembers := make(map[uint32]*entity.GroupMember)
   139  	members, token, err := c.FetchGroupMember(groupUin, "")
   140  	if err != nil {
   141  		return groupMembers, err
   142  	}
   143  	for _, member := range members {
   144  		groupMembers[member.Uin] = member
   145  	}
   146  	for token != "" {
   147  		members, token, err = c.FetchGroupMember(groupUin, token)
   148  		if err != nil {
   149  			return groupMembers, err
   150  		}
   151  		for _, member := range members {
   152  			groupMembers[member.Uin] = member
   153  		}
   154  	}
   155  	return groupMembers, err
   156  }
   157  
   158  // GetAllGroupsMembersData 获取所有群的群成员信息
   159  func (c *QQClient) GetAllGroupsMembersData() (map[uint32]map[uint32]*entity.GroupMember, error) {
   160  	groups, err := c.FetchGroups()
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	groupsData := make(map[uint32]map[uint32]*entity.GroupMember, len(groups))
   165  	for _, group := range groups {
   166  		groupMembersData, err := c.GetGroupMembersData(group.GroupUin)
   167  		if err != nil {
   168  			return nil, err
   169  		}
   170  		groupsData[group.GroupUin] = groupMembersData
   171  	}
   172  	c.info("获取%d个群的成员信息", len(groupsData))
   173  	return groupsData, err
   174  }
   175  
   176  func (c *QQClient) GetAllGroupsInfo() (map[uint32]*entity.Group, error) {
   177  	groupsInfo, err := c.FetchGroups()
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  	groupsData := make(map[uint32]*entity.Group, len(groupsInfo))
   182  	for _, group := range groupsInfo {
   183  		groupsData[group.GroupUin] = group
   184  	}
   185  	c.info("获取%d个群信息", len(groupsData))
   186  	return groupsData, err
   187  }