"""Messages.""" import gettext import json import config def create_message_string(message, data): """Create string message to go to SNS endpoints. Based on: https://github.com/theorchard/profile-users-scripts/blob/master/src/ApiCalls/Sns.js#L9 And: https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html Args: message (str): contents of notification data (dict): metadata about notification Returns: str: formatted message for Google and Apple notifications """ # noqa:E501 android_data = dict(data, body=message) android_message = { 'data': android_data } ios_message = { 'aps': { 'alert': { 'body': message }, 'sound': 'default' }, 'body': data } sns_message = { 'GCM': json.dumps(android_message), 'APNS': json.dumps(ios_message), 'default': message } return json.dumps(sns_message) def get_translations(identity_lc): """Get translations based on user locale.""" user_locale = config.DEFAULT_LANGUAGE if identity_lc: user_locale = identity_lc.replace('-', '_') if identity_lc.startswith('zh') else identity_lc[:2] # noqa:E501 translation = gettext.translation( config.I18N_DOMAIN, localedir=config.I18N_FOLDER_NAME, languages=[user_locale], fallback=True) return translation.gettext def get_platform_and_follower_type(platform_name): """Get platform formatted name and type of follower.""" for platform, follower_type in config.SOCIAL_PLATFORM_MAPPING.items(): if platform_name == platform.lower(): return platform, follower_type raise Exception(f'Unknown platform {platform_name}')