2017-08-04 11:05:07 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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 "Widget.h"
|
|
|
|
|
|
|
|
#import "WidgetManager.h"
|
|
|
|
|
|
|
|
@implementation Widget
|
|
|
|
|
|
|
|
- (instancetype)initWithWidgetEvent:(MXEvent *)widgetEvent inMatrixSession:(MXSession*)mxSession
|
|
|
|
{
|
|
|
|
if (![widgetEvent.type isEqualToString:kWidgetEventTypeString])
|
|
|
|
{
|
2017-08-30 09:28:51 +00:00
|
|
|
// The Widget class works only with modular, aka "im.vector.modular.widgets", widgets
|
2017-08-04 11:05:07 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
_widgetId = widgetEvent.stateKey;
|
|
|
|
_widgetEvent = widgetEvent;
|
2017-08-09 15:31:15 +00:00
|
|
|
_mxSession = mxSession;
|
2017-08-04 11:05:07 +00:00
|
|
|
|
|
|
|
MXJSONModelSetString(_type, widgetEvent.content[@"type"]);
|
|
|
|
MXJSONModelSetString(_url, widgetEvent.content[@"url"]);
|
|
|
|
MXJSONModelSetString(_name, widgetEvent.content[@"name"]);
|
|
|
|
MXJSONModelSetDictionary(_data, widgetEvent.content[@"data"]);
|
|
|
|
|
|
|
|
// Format the url string with user data
|
2017-09-21 13:36:09 +00:00
|
|
|
if (_url)
|
2017-09-19 08:04:19 +00:00
|
|
|
{
|
2017-10-25 14:46:03 +00:00
|
|
|
NSString *userId = mxSession.myUser.userId;
|
|
|
|
NSString *displayName = mxSession.myUser.displayname ? mxSession.myUser.displayname : mxSession.myUser.userId;
|
|
|
|
NSString *avatarUrl = mxSession.myUser.avatarUrl ? mxSession.myUser.avatarUrl : @"";
|
|
|
|
|
|
|
|
// Escape everything to build a valid URL string
|
|
|
|
userId = [userId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
displayName = [displayName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
avatarUrl = [avatarUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
_url = [_url stringByReplacingOccurrencesOfString:@"$matrix_user_id" withString:userId];
|
|
|
|
_url = [_url stringByReplacingOccurrencesOfString:@"$matrix_display_name" withString:displayName];
|
|
|
|
_url = [_url stringByReplacingOccurrencesOfString:@"$matrix_avatar_url" withString:avatarUrl];
|
2017-09-21 13:36:09 +00:00
|
|
|
|
|
|
|
// And their scalar token
|
|
|
|
NSString *scalarToken = [[WidgetManager sharedManager] scalarTokenForMXSession:mxSession];
|
|
|
|
if (scalarToken)
|
|
|
|
{
|
|
|
|
_url = [_url stringByAppendingString:[NSString stringWithFormat:@"&scalar_token=%@", scalarToken]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Some widget can live without scalar token (ex: Jitsi widget)
|
|
|
|
NSLog(@"[Widget] Note: There is no scalar token for %@", self);
|
|
|
|
}
|
2017-12-08 15:46:46 +00:00
|
|
|
|
|
|
|
// Integrate widget data into widget url
|
|
|
|
for (NSString *key in _data)
|
|
|
|
{
|
|
|
|
NSString *paramKey = [NSString stringWithFormat:@"$%@", key];
|
|
|
|
NSString *value = [_data[key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
_url = [_url stringByReplacingOccurrencesOfString:paramKey
|
|
|
|
withString:value];
|
|
|
|
}
|
2017-09-19 08:04:19 +00:00
|
|
|
}
|
2017-08-04 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isActive
|
|
|
|
{
|
|
|
|
return (_type != nil && _url != nil);
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:31:15 +00:00
|
|
|
- (NSString *)roomId
|
|
|
|
{
|
|
|
|
return _widgetEvent.roomId;
|
|
|
|
}
|
|
|
|
|
2017-08-16 09:47:03 +00:00
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"<Widget: %p> id: %@ - type: %@ - name: %@ - url: %@", self, _widgetId, _type, _name, _url];
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:05:07 +00:00
|
|
|
@end
|