Skip to content

Commit

Permalink
Merge pull request #82 from taller2-2c2018/Issue#81
Browse files Browse the repository at this point in the history
Send messages when interacting with stories
  • Loading branch information
fcancel authored Jul 12, 2018
2 parents 632ad50 + d8495da commit 420046b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/appserver/externalcommunication/FirebaseCloudMessaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests

from appserver.logger import LoggerFactory
from appserver.threading.RunAsync import run_async

LOGGER = LoggerFactory().get_logger(__name__)
URL = 'https://fcm.googleapis.com/fcm/send'
Expand All @@ -15,6 +16,7 @@
class FirebaseCloudMessaging(object):

@staticmethod
@run_async
def send_notification(title, body, list_of_firebase_ids):
try:
json = {
Expand Down
20 changes: 14 additions & 6 deletions src/appserver/service/StoryService.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from appserver.rules.RelevanceEngine import RelevanceEngine
from appserver.rules.StoryRelevance import StoryRelevance
from appserver.service.FileService import FileService
from appserver.threading.RunAsync import run_async
from appserver.time.Time import Time
from appserver.transformer.MobileTransformer import MobileTransformer
from appserver.validator.jsonValidator import JsonValidator
Expand Down Expand Up @@ -102,6 +103,7 @@ def __send_new_story_notification(user):
StoryService.__send_notification_to_friends(facebook_id_poster, title, body)

@staticmethod
@run_async
def __send_notification_to_friends(facebook_id_poster, title, body):
list_of_friends = UserRepository.get_friendship_list(facebook_id_poster)
if facebook_id_poster in list_of_friends:
Expand Down Expand Up @@ -204,18 +206,21 @@ def post_comment(header, json_comment, story_id):
story = FileService.add_file_to_dictionary(story, 'file_id')
story = StoryService.__add_profile_data_to_story(story)
story_for_mobile = MobileTransformer.database_story_to_mobile(story)
StoryService.__send_new_comment_notification(facebook_user_id, body=story_for_mobile)
StoryService.__send_new_comment_notification(facebook_user_id, story_body=story_for_mobile)

return ApplicationResponse.created('Comment created successfully')

@staticmethod
def __send_new_comment_notification(facebook_user_id, body):
@run_async
def __send_new_comment_notification(facebook_user_id, story_body):
LOGGER.info('Sending comment notification')
user = UserRepository.get_profile(facebook_user_id)
user_name = user['first_name'] + ' ' + user['last_name']
title = user_name + ' ha realizado un comentario en tu historia'
story_creator_firebase_id = UserRepository.get_profile(story_body['mFacebookUserId'])['firebase_id']

StoryService.__send_notification_to_friends(facebook_user_id, title, body)
FirebaseCloudMessaging.send_notification(title=title, body=story_body,
list_of_firebase_ids=list(story_creator_firebase_id))

@staticmethod
def post_reaction(header, json_reaction, story_id):
Expand Down Expand Up @@ -253,18 +258,21 @@ def post_reaction(header, json_reaction, story_id):
story = FileService.add_file_to_dictionary(story, 'file_id')
story = StoryService.__add_profile_data_to_story(story)
story_for_mobile = MobileTransformer.database_story_to_mobile(story)
StoryService.__send_new_reaction_notification(facebook_user_id, reaction, body=story_for_mobile)
StoryService.__send_new_reaction_notification(facebook_user_id, reaction, story_body=story_for_mobile)

return ApplicationResponse.created('Reaction created successfully')

@staticmethod
def __send_new_reaction_notification(facebook_user_id, reaction, body):
@run_async
def __send_new_reaction_notification(facebook_user_id, reaction, story_body):
LOGGER.info('Sending reaction notification')
user = UserRepository.get_profile(facebook_user_id)
user_name = user['first_name'] + ' ' + user['last_name']
title = user_name + ' ha reaccionado con un ' + reaction + ' a tu historia'
story_creator_firebase_id = UserRepository.get_profile(story_body['mFacebookUserId'])['firebase_id']

StoryService.__send_notification_to_friends(facebook_user_id, title, body)
FirebaseCloudMessaging.send_notification(title=title, body=story_body,
list_of_firebase_ids=list(story_creator_firebase_id))

@staticmethod
def __add_profile_data_to_stories(filtered_stories):
Expand Down
13 changes: 5 additions & 8 deletions src/appserver/service/UserService.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from appserver.repository.userRepository import UserRepository
from appserver.service.FileService import FileService
from appserver.service.StoryService import StoryService
from appserver.threading.RunAsync import run_async
from appserver.transformer.MobileTransformer import MobileTransformer
from appserver.validator.databaseValidator import DatabaseValidator
from appserver.validator.jsonValidator import JsonValidator
Expand Down Expand Up @@ -120,17 +121,15 @@ def send_user_friendship_request(request_json, request_header):
return ApplicationResponse.bad_request(message='Target username doesn\'t exist')

@staticmethod
@run_async
def __send_notification_of_friendship_request(facebook_id_requester, facebook_id_target, requester_message):
requester = UserRepository.get_profile(facebook_id_requester)
requester_name = requester['first_name'] + ' ' + requester['last_name']
target = UserRepository.get_profile(facebook_id_target)
title = requester_name + ' quiere ser tu amigo'
body = {'mMessage': requester_message}

try:
FirebaseCloudMessaging.send_notification(title=title, body=body, list_of_firebase_ids=list(target['firebase_id']))
except:
LOGGER.warn('No firebase Id found for user, not sending push notification')
FirebaseCloudMessaging.send_notification(title=title, body=body, list_of_firebase_ids=list(target['firebase_id']))

@staticmethod
def get_friendship_requests(request_header):
Expand Down Expand Up @@ -213,17 +212,15 @@ def reject_friendship_request(request_header, target_user):
return ApplicationResponse.bad_request(message='Friendship request couldn\'t be found')

@staticmethod
@run_async
def __send_notification_of_friendship_accepted(facebook_id_acceptor, facebook_id_target):
acceptor = UserRepository.get_profile(facebook_id_acceptor)
acceptor_name = acceptor['first_name'] + ' ' + acceptor['last_name']
target = UserRepository.get_profile(facebook_id_target)
title = acceptor_name + ' ha aceptado tu solicitud de amistad'
body = {'mMessage': 'Tú y ' + acceptor_name + ' ahora son amigos'}

try:
FirebaseCloudMessaging.send_notification(title=title, body=body, list_of_firebase_ids=list(target['firebase_id']))
except:
LOGGER.warn('No firebase Id found for user, not sending push notification')
FirebaseCloudMessaging.send_notification(title=title, body=body, list_of_firebase_ids=list(target['firebase_id']))

@staticmethod
def modify_user_profile(request_json, request_header):
Expand Down

0 comments on commit 420046b

Please sign in to comment.