Files
bundesmessenger-ios/matrixConsole/API/MatrixHandler.m
T

140 lines
5.1 KiB
Objective-C

/*
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MatrixHandler.h"
#import "AppDelegate.h"
static MatrixHandler *sharedHandler = nil;
@implementation MatrixHandler
+ (MatrixHandler *)sharedHandler {
@synchronized(self) {
if(sharedHandler == nil)
{
sharedHandler = [[super allocWithZone:NULL] init];
}
}
return sharedHandler;
}
- (MXSession*)mxSession {
// Only the first account is presently used
MXKAccount *account = [[MXKAccountManager sharedManager].accounts firstObject];
return account.mxSession;
}
- (MXRestClient*)mxRestClient {
// Only the first account is presently used
MXKAccount *account = [[MXKAccountManager sharedManager].accounts firstObject];
return account.mxRestClient;
}
// FIXME GFO Move the following methods in SDK and Remove MatrixHandler class
#pragma mark - Room handling
- (void)startPrivateOneToOneRoomWithUserId:(NSString*)userId {
if (self.mxRestClient) {
MXRoom* mxRoom = [self.mxSession privateOneToOneRoomWithUserId:userId];
// if the room exists
if (mxRoom) {
// open it
[[AppDelegate theDelegate].masterTabBarController showRoom:mxRoom.state.roomId];
} else {
// create a new room
[self.mxSession createRoom:nil
visibility:kMXRoomVisibilityPrivate
roomAlias:nil
topic:nil
success:^(MXRoom *room) {
// invite the other user only if it is defined and not onself
if (userId && ![self.mxSession.myUser.userId isEqualToString:userId]) {
// add the user
[room inviteUser:userId success:^{
} failure:^(NSError *error) {
NSLog(@"[MatrixHandler] %@ invitation failed (roomId: %@): %@", userId, room.state.roomId, error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
// Open created room
[[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId];
} failure:^(NSError *error) {
NSLog(@"[MatrixHandler] Create room failed: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
}
}
- (CGFloat)getPowerLevel:(MXRoomMember *)roomMember inRoom:(MXRoom *)room {
CGFloat powerLevel = 0;
// Customize banned and left (kicked) members
if (roomMember.membership == MXMembershipLeave || roomMember.membership == MXMembershipBan) {
powerLevel = 0;
} else {
// Handle power level display
//self.userPowerLevel.hidden = NO;
MXRoomPowerLevels *roomPowerLevels = room.state.powerLevels;
int maxLevel = 0;
for (NSString *powerLevel in roomPowerLevels.users.allValues) {
int level = [powerLevel intValue];
if (level > maxLevel) {
maxLevel = level;
}
}
NSUInteger userPowerLevel = [roomPowerLevels powerLevelOfUserWithUserID:roomMember.userId];
float userPowerLevelFloat = 0.0;
if (userPowerLevel) {
userPowerLevelFloat = userPowerLevel;
}
powerLevel = maxLevel ? userPowerLevelFloat / maxLevel : 1;
}
return powerLevel;
}
#pragma mark - Presence
// return the presence ring color
// nil means there is no ring to display
- (UIColor*)getPresenceRingColor:(MXPresence)presence {
switch (presence) {
case MXPresenceOnline:
return [UIColor colorWithRed:0.2 green:0.9 blue:0.2 alpha:1.0];
case MXPresenceUnavailable:
return [UIColor colorWithRed:0.9 green:0.9 blue:0.0 alpha:1.0];
case MXPresenceOffline:
return [UIColor colorWithRed:0.9 green:0.2 blue:0.2 alpha:1.0];
case MXPresenceUnknown:
case MXPresenceFreeForChat:
case MXPresenceHidden:
default:
return nil;
}
}
@end