<s> from lampost . server . user import User <EOL> from lampost . context . resource import m_requires <EOL> from lampost . context . config import m_configured <EOL> from lampost . util . lputil import ClientError <EOL> m_requires ( __name__ , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> m_configured ( __name__ , '<STR_LIT>' ) <EOL> _REQUEST_KEY = "<STR_LIT>" <EOL> _FRIEND_EMAIL_KEY = "<STR_LIT>" <EOL> _ALL_EMAIL_KEY = "<STR_LIT>" <EOL> class FriendService ( ) : <EOL> def _post_init ( self ) : <EOL> register ( '<STR_LIT>' , self . _check_friends ) <EOL> register ( '<STR_LIT>' , self . _delete_player ) <EOL> def friend_request ( self , source , target ) : <EOL> req_key = '<STR_LIT::>' . join ( [ source . dbo_id , target . dbo_id ] ) <EOL> if set_key_exists ( _REQUEST_KEY , req_key ) : <EOL> raise ClientError ( "<STR_LIT>" . format ( target . name ) ) <EOL> dispatch ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : source . dbo_id , '<STR_LIT>' : source . name } , target . dbo_id , source . dbo_id ) <EOL> add_set_key ( _REQUEST_KEY , req_key ) <EOL> def remove_request ( self , source_id , target_id ) : <EOL> delete_set_key ( '<STR_LIT>' , '<STR_LIT::>' . join ( [ source_id , target_id ] ) ) <EOL> def add_friend ( self , source_id , target_id ) : <EOL> add_set_key ( friend_key ( source_id ) , target_id ) <EOL> add_set_key ( friend_key ( target_id ) , source_id ) <EOL> def del_friend ( self , friend_one_id , friend_two_id ) : <EOL> delete_set_key ( friend_key ( friend_one_id ) , friend_two_id ) <EOL> delete_set_key ( friend_key ( friend_two_id ) , friend_one_id ) <EOL> def friend_list ( self , player_id ) : <EOL> return '<STR_LIT:U+0020>' . join ( [ user_manager . id_to_name ( friend_id ) for friend_id in fetch_set_keys ( friend_key ( player_id ) ) ] ) <EOL> def is_friend ( self , player_id , friend_id ) : <EOL> return set_key_exists ( friend_key ( player_id ) , friend_id ) <EOL> def update_notifies ( self , user_id , notifies ) : <EOL> if '<STR_LIT>' in notifies : <EOL> add_set_key ( _FRIEND_EMAIL_KEY , user_id ) <EOL> else : <EOL> delete_set_key ( _FRIEND_EMAIL_KEY , user_id ) <EOL> if '<STR_LIT>' in notifies : <EOL> add_set_key ( _ALL_EMAIL_KEY , user_id ) <EOL> else : <EOL> delete_set_key ( _ALL_EMAIL_KEY , user_id ) <EOL> def _check_friends ( self , player ) : <EOL> logged_in_players = set ( session_manager . player_session_map . keys ( ) ) <EOL> friends = set ( fetch_set_keys ( friend_key ( player . dbo_id ) ) ) <EOL> logged_in_friends = logged_in_players . intersection ( friends ) <EOL> for friend_id in logged_in_friends : <EOL> session_manager . player_session_map [ friend_id ] . append ( { '<STR_LIT>' : { '<STR_LIT:name>' : player . name } } ) <EOL> notify_user_ids = { get_index ( '<STR_LIT>' , player_id ) for player_id in friends . difference ( logged_in_friends ) } <EOL> notify_user_ids = notify_user_ids . intersection ( fetch_set_keys ( _FRIEND_EMAIL_KEY ) ) <EOL> notify_user_ids = notify_user_ids . union ( fetch_set_keys ( _ALL_EMAIL_KEY ) ) <EOL> logged_in_user_ids = { session_manager . player_session_map [ player_id ] . player . user_id for player_id in logged_in_players } <EOL> notify_user_ids = notify_user_ids . difference ( logged_in_user_ids ) <EOL> users = [ load_object ( user_id , User ) for user_id in notify_user_ids ] <EOL> if users : <EOL> email_sender . send_targeted_email ( "<STR_LIT>" . format ( player . name ) , <EOL> "<STR_LIT>" . format ( player . name , lampost_title ) , users ) <EOL> def _delete_player ( self , player_id ) : <EOL> for friend_id in fetch_set_keys ( friend_key ( player_id ) ) : <EOL> self . del_friend ( player_id , friend_id ) <EOL> def friend_key ( player_id ) : <EOL> return '<STR_LIT>' . format ( player_id ) </s>
