mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-28 20:26:57 +02:00
Integrations: Use the integrations manager provided by the homeserver admin via .well-known
#2815
This commit is contained in:
@@ -50,6 +50,9 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
|
||||
// User id -> scalar token
|
||||
NSMutableDictionary<NSString*, WidgetManagerConfig*> *configs;
|
||||
|
||||
// User id -> MXSession
|
||||
NSMutableDictionary<NSString*, MXSession*> *matrixSessions;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -73,6 +76,7 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
matrixSessions = [NSMutableDictionary dictionary];
|
||||
widgetEventListener = [NSMutableDictionary dictionary];
|
||||
successBlockForWidgetCreation = [NSMutableDictionary dictionary];
|
||||
failureBlockForWidgetCreation = [NSMutableDictionary dictionary];
|
||||
@@ -367,6 +371,8 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
{
|
||||
__weak __typeof__(self) weakSelf = self;
|
||||
|
||||
matrixSessions[mxSession.matrixRestClient.credentials.userId] = mxSession;
|
||||
|
||||
NSString *hash = [NSString stringWithFormat:@"%p", mxSession];
|
||||
|
||||
id listener = [mxSession listenToEventsOfTypes:@[kWidgetMatrixEventTypeString, kWidgetModularEventTypeString] onEvent:^(MXEvent *event, MXTimelineDirection direction, id customObject) {
|
||||
@@ -421,6 +427,12 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
|
||||
- (void)removeMatrixSession:(MXSession *)mxSession
|
||||
{
|
||||
// Remove by value in a dict
|
||||
for (NSString *key in [matrixSessions allKeysForObject:mxSession])
|
||||
{
|
||||
[matrixSessions removeObjectForKey:key];
|
||||
}
|
||||
|
||||
// mxSession.myUser.userId and mxSession.matrixRestClient.credentials.userId may be nil here
|
||||
// So, use a kind of hash value instead
|
||||
NSString *hash = [NSString stringWithFormat:@"%p", mxSession];
|
||||
@@ -433,18 +445,59 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
[failureBlockForWidgetCreation removeObjectForKey:hash];
|
||||
}
|
||||
|
||||
- (MXSession*)matrixSessionForUser:(NSString*)userId
|
||||
{
|
||||
return matrixSessions[userId];
|
||||
}
|
||||
|
||||
- (void)deleteDataForUser:(NSString *)userId
|
||||
{
|
||||
[configs removeObjectForKey:userId];
|
||||
[self saveConfigs];
|
||||
}
|
||||
|
||||
#pragma mark - User integrations configuration
|
||||
|
||||
- (WidgetManagerConfig*)createWidgetManagerConfigForUser:(NSString*)userId
|
||||
{
|
||||
WidgetManagerConfig *config;
|
||||
|
||||
MXSession *session = [self matrixSessionForUser:userId];
|
||||
|
||||
// Find the integrations settings for the user
|
||||
|
||||
// First, look at matrix account
|
||||
// TODO in another user story
|
||||
|
||||
// Then, try to the homeserver configuration
|
||||
MXWellknownIntegrationsManager *integrationsManager = session.homeserverWellknown.integrations.managers.firstObject;
|
||||
if (integrationsManager)
|
||||
{
|
||||
config = [[WidgetManagerConfig alloc] initWithApiUrl:integrationsManager.apiUrl uiUrl:integrationsManager.uiUrl];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback on app settings
|
||||
config = [self createWidgetManagerConfigWithAppSettings];
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
- (WidgetManagerConfig*)createWidgetManagerConfigWithAppSettings
|
||||
{
|
||||
NSString *apiUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"integrationsRestUrl"];
|
||||
NSString *uiUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"integrationsUiUrl"];
|
||||
|
||||
return [[WidgetManagerConfig alloc] initWithApiUrl:apiUrl uiUrl:uiUrl];
|
||||
}
|
||||
|
||||
#pragma mark - Modular interface
|
||||
|
||||
- (WidgetManagerConfig*)configForUser:(NSString*)userId
|
||||
{
|
||||
// Return a default config by default
|
||||
return configs[userId] ? configs[userId] : [WidgetManagerConfig new];
|
||||
return configs[userId] ? configs[userId] : [self createWidgetManagerConfigForUser:userId];
|
||||
}
|
||||
|
||||
- (BOOL)hasIntegrationManagerForUser:(NSString*)userId
|
||||
@@ -691,7 +744,7 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain";
|
||||
|
||||
NSLog(@"[WidgetManager] migrate scalarTokens to integrationManagerConfigs for %@", userId);
|
||||
|
||||
WidgetManagerConfig *config = [WidgetManagerConfig new];
|
||||
WidgetManagerConfig *config = [self createWidgetManagerConfigWithAppSettings];
|
||||
config.scalarToken = scalarToken;
|
||||
|
||||
configs[userId] = config;
|
||||
|
||||
Reference in New Issue
Block a user