123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- //
- // NIMKitDataProviderImpl.m
- // NIMKit
- //
- // Created by chris on 2016/10/31.
- // Copyright © 2016年 NetEase. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "NIMKit.h"
- #import "NIMKitDataProviderImpl.h"
- #import "NIMKitInfoFetchOption.h"
- #import "UIImage+NIMKit.h"
- #pragma mark - kit data request
- @interface NIMKitDataRequest : NSObject
- @property (nonatomic,strong) NSMutableSet *failedUserIds;
- @property (nonatomic,assign) NSInteger maxMergeCount; //最大合并数
- - (void)requestUserIds:(NSArray *)userIds;
- @end
- @implementation NIMKitDataRequest{
- NSMutableArray *_requstUserIdArray; //待请求池
- BOOL _isRequesting;
- }
- - (instancetype)init{
- self = [super init];
- if (self) {
- _failedUserIds = [[NSMutableSet alloc] init];
- _requstUserIdArray = [[NSMutableArray alloc] init];
- }
- return self;
- }
- - (void)requestUserIds:(NSArray *)userIds
- {
- for (NSString *userId in userIds)
- {
- if (![_requstUserIdArray containsObject:userId] && ![_failedUserIds containsObject:userId])
- {
- [_requstUserIdArray addObject:userId];
- }
- }
- [self request];
- }
- - (void)request
- {
- static NSUInteger MaxBatchReuqestCount = 10;
- if (_isRequesting || [_requstUserIdArray count] == 0) {
- return;
- }
- _isRequesting = YES;
- NSArray *userIds = [_requstUserIdArray count] > MaxBatchReuqestCount ?
- [_requstUserIdArray subarrayWithRange:NSMakeRange(0, MaxBatchReuqestCount)] : [_requstUserIdArray copy];
-
- __weak typeof(self) weakSelf = self;
- [[NIMSDK sharedSDK].userManager fetchUserInfos:userIds
- completion:^(NSArray *users, NSError *error) {
- [weakSelf afterReuquest:userIds];
- if (!error && users.count)
- {
- [[NIMKit sharedKit] notfiyUserInfoChanged:userIds];
- }
- else if ([weakSelf shouldAddToFailedUsers:error])
- {
- [weakSelf.failedUserIds addObjectsFromArray:userIds];
- }
- }];
- }
- - (void)afterReuquest:(NSArray *)userIds
- {
- _isRequesting = NO;
- [_requstUserIdArray removeObjectsInArray:userIds];
- [self request];
-
- }
- - (BOOL)shouldAddToFailedUsers:(NSError *)error
- {
- //没有错误这种异常情况走到这个路径里也不对,不再请求
- return error.code != NIMRemoteErrorCodeTimeoutError || !error;
- }
- @end
- #pragma mark - data provider impl
- @interface NIMKitDataProviderImpl()<NIMUserManagerDelegate,NIMTeamManagerDelegate,NIMLoginManagerDelegate>
- @property (nonatomic,strong) UIImage *defaultUserAvatar;
- @property (nonatomic,strong) UIImage *defaultTeamAvatar;
- @property (nonatomic,strong) NIMKitDataRequest *request;
- @end
- @implementation NIMKitDataProviderImpl
- - (instancetype)init{
- self = [super init];
- if (self) {
- _request = [[NIMKitDataRequest alloc] init];
- _request.maxMergeCount = 20;
- [[NIMSDK sharedSDK].userManager addDelegate:self];
- [[NIMSDK sharedSDK].teamManager addDelegate:self];
- [[NIMSDK sharedSDK].loginManager addDelegate:self];
- }
- return self;
- }
- - (void)dealloc
- {
- [[NIMSDK sharedSDK].userManager removeDelegate:self];
- [[NIMSDK sharedSDK].teamManager removeDelegate:self];
- [[NIMSDK sharedSDK].loginManager removeDelegate:self];
- }
- #pragma mark - public api
- - (NIMKitInfo *)infoByUser:(NSString *)userId
- option:(NIMKitInfoFetchOption *)option
- {
- //优先检测是否为机器人
- NIMKitInfo *info = [self infoByRobot:userId];
- if (info == nil)
- {
- NIMSession *session = option.message.session?:option.session;
- info = [self infoByUser:userId session:session option:option];
- }
- return info;
- }
- - (NIMKitInfo *)infoByTeam:(NSString *)teamId
- option:(NIMKitInfoFetchOption *)option
- {
- NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:teamId];
- NIMKitInfo *info = [[NIMKitInfo alloc] init];
- info.showName = team.teamName;
- info.infoId = teamId;
- info.avatarImage = self.defaultTeamAvatar;
- info.avatarUrlString = team.thumbAvatarUrl;
- return info;
- }
- #pragma mark - 用户信息拼装
- //会话中用户信息
- - (NIMKitInfo *)infoByUser:(NSString *)userId
- session:(NIMSession *)session
- option:(NIMKitInfoFetchOption *)option
- {
- NIMSessionType sessionType = session.sessionType;
- NIMKitInfo *info;
-
- switch (sessionType) {
- case NIMSessionTypeP2P:
- {
- info = [self userInfoInP2P:userId option:option];
- }
- break;
- case NIMSessionTypeTeam:
- {
- info = [self userInfo:userId inTeam:session.sessionId option:option];
- }
- break;
- case NIMSessionTypeChatroom:
- {
- info = [self userInfo:userId inChatroom:session.sessionId option:option];
- }
- break;
- default:
- NSAssert(0, @"invalid type");
- break;
- }
-
- if (!info)
- {
- if (!userId.length)
- {
- NSLog(@"warning: fetch user failed because userid is empty");
- }
- else
- {
- [self.request requestUserIds:@[userId]];
- }
-
- info = [[NIMKitInfo alloc] init];
- info.infoId = userId;
- info.showName = userId; //默认值
- info.avatarImage = self.defaultUserAvatar;
- }
- return info;
- }
- #pragma mark - P2P 用户信息
- - (NIMKitInfo *)userInfoInP2P:(NSString *)userId
- option:(NIMKitInfoFetchOption *)option
- {
- NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:userId];
- NIMUserInfo *userInfo = user.userInfo;
- NIMKitInfo *info;
- if (userInfo)
- {
- info = [[NIMKitInfo alloc] init];
- info.infoId = userId;
- NSString *name = [self nickname:user
- memberInfo:nil
- option:option];
- info.showName = name?:userId;
- info.avatarUrlString = userInfo.thumbAvatarUrl;
- info.avatarImage = self.defaultUserAvatar;
- }
- return info;
- }
- #pragma mark - 群组用户信息
- - (NIMKitInfo *)userInfo:(NSString *)userId
- inTeam:(NSString *)teamId
- option:(NIMKitInfoFetchOption *)option
- {
- NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:userId];
- NIMUserInfo *userInfo = user.userInfo;
- NIMTeamMember *member = [[NIMSDK sharedSDK].teamManager teamMember:userId
- inTeam:teamId];
-
- NIMKitInfo *info;
-
- if (userInfo || member)
- {
- info = [[NIMKitInfo alloc] init];
- info.infoId = userId;
-
- NSString *name = [self nickname:user
- memberInfo:member
- option:option];
- info.showName = name?:userId;
- info.avatarUrlString = userInfo.thumbAvatarUrl;
- info.avatarImage = self.defaultUserAvatar;
- }
- return info;
- }
- #pragma mark - 聊天室用户信息
- - (NIMKitInfo *)userInfo:(NSString *)userId
- inChatroom:(NSString *)roomId
- option:(NIMKitInfoFetchOption *)option
- {
- NIMKitInfo *info = [[NIMKitInfo alloc] init];
- info.infoId = userId;
-
- if ([userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
- {
-
- switch ([NIMSDK sharedSDK].loginManager.currentAuthMode) {
- case NIMSDKAuthModeChatroom:
- {
- NSAssert([NIMKit sharedKit].independentModeExtraInfo, @"in mode NIMSDKAuthModeChatroom , must has independentModeExtraInfo");
- info.showName = [NIMKit sharedKit].independentModeExtraInfo.myChatroomNickname;
- info.avatarUrlString = [NIMKit sharedKit].independentModeExtraInfo.myChatroomAvatar;
- }
- break;
- case NIMSDKAuthModeIM:
- {
- NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:userId];
- info.showName = user.userInfo.nickName;
- info.avatarUrlString = user.userInfo.thumbAvatarUrl;
- }
- break;
- default:
- {
- NSAssert(0, @"invalid mode");
- }
- break;
- }
-
- }
- else
- {
- NSAssert(option.message, @"message must has value in chatroom");
- NIMMessageChatroomExtension *ext = [option.message.messageExt isKindOfClass:[NIMMessageChatroomExtension class]] ?
- (NIMMessageChatroomExtension *)option.message.messageExt : nil;
- info.showName = ext.roomNickname;
- info.avatarUrlString = ext.roomAvatar;
- }
- info.avatarImage = self.defaultUserAvatar;
- return info;
- }
- //机器人
- - (NIMKitInfo *)infoByRobot:(NSString *)userId
- {
- NIMKitInfo *info = nil;
- if ([[NIMSDK sharedSDK].robotManager isValidRobot:userId])
- {
- NIMRobot *robot = [[NIMSDK sharedSDK].robotManager robotInfo:userId];
- if (robot)
- {
- info = [[NIMKitInfo alloc] init];
- info.infoId = userId;
- info.showName = robot.nickname;
- info.avatarUrlString = robot.thumbAvatarUrl;
- info.avatarImage = self.defaultUserAvatar;
- }
- }
- return info;
- }
- //昵称优先级
- - (NSString *)nickname:(NIMUser *)user
- memberInfo:(NIMTeamMember *)memberInfo
- option:(NIMKitInfoFetchOption *)option
- {
- NSString *name = nil;
- do{
- if (!option.forbidaAlias && [user.alias length])
- {
- name = user.alias;
- break;
- }
- if (memberInfo && [memberInfo.nickname length])
- {
- name = memberInfo.nickname;
- break;
- }
-
- if ([user.userInfo.nickName length])
- {
- name = user.userInfo.nickName;
- break;
- }
- }while (0);
- return name;
- }
- #pragma mark - avatar
- - (UIImage *)defaultTeamAvatar
- {
- if (!_defaultTeamAvatar)
- {
- _defaultTeamAvatar = [UIImage nim_imageInKit:@"avatar_team"];
- }
- return _defaultTeamAvatar;
- }
- - (UIImage *)defaultUserAvatar
- {
- if (!_defaultUserAvatar)
- {
- _defaultUserAvatar = [UIImage nim_imageInKit:@"avatar_user"];
- }
- return _defaultUserAvatar;
- }
- //将个人信息和群组信息变化通知给 NIMKit 。
- //如果您的应用不托管个人信息给云信,则需要您自行在上层监听个人信息变动,并将变动通知给 NIMKit。
- #pragma mark - NIMUserManagerDelegate
- - (void)onFriendChanged:(NIMUser *)user
- {
- [self notifyUser:user];
- }
- - (void)onUserInfoChanged:(NIMUser *)user
- {
- [self notifyUser:user];
- }
- - (void)notifyUser:(NIMUser *)user
- {
- if (!user)
- {
- NSLog(@"warning: notify user failed because user is empty");
- }
- else
- {
- [[NIMKit sharedKit] notfiyUserInfoChanged:@[user.userId]];
- }
-
- }
- #pragma mark - NIMTeamManagerDelegate
- - (void)onTeamAdded:(NIMTeam *)team
- {
- [self notifyTeamInfo:team];
- }
- - (void)onTeamUpdated:(NIMTeam *)team
- {
- [self notifyTeamInfo:team];
- }
- - (void)onTeamRemoved:(NIMTeam *)team
- {
- [self notifyTeamInfo:team];
- }
- - (void)onTeamMemberChanged:(NIMTeam *)team
- {
- [self notifyTeamMember:team];
- }
- - (void)notifyTeamInfo:(NIMTeam *)team
- {
- if (!team.teamId.length)
- {
- NSLog(@"warning: notify teamid failed because teamid is empty");
- }
- else
- {
- [[NIMKit sharedKit] notifyTeamInfoChanged:@[team.teamId]];
- }
- }
- - (void)notifyTeamMember:(NIMTeam *)team
- {
- if (!team.teamId.length)
- {
- NSLog(@"warning: notify team member failed because teamid is empty");
- }
- else
- {
- [[NIMKit sharedKit] notifyTeamMemebersChanged:@[team.teamId]];
- }
- }
- #pragma mark - NIMLoginManagerDelegate
- - (void)onLogin:(NIMLoginStep)step
- {
- if (step == NIMLoginStepSyncOK) {
- [[NIMKit sharedKit] notifyTeamInfoChanged:nil];
- [[NIMKit sharedKit] notifyTeamMemebersChanged:nil];
- }
- }
- @end
|