Ejemplo n.º 1
0
def get_all_user_requests(data_wrapper, settings, logger, manager):
    user_data = helpers.wrap_user_data(data_wrapper.user_id,
                                       data_wrapper.user_name)

    raw_target_user_id_or_name = helpers.strip_at_symbol_for_name(
        data_wrapper.get_param(1))
    target_user_id_or_name = helpers.wrap_user_id_or_name(
        raw_target_user_id_or_name)

    (user_requests,
     target_data) = manager.get_user_requests_for(user_data,
                                                  target_user_id_or_name)

    target_user_name = None
    if target_data:
        target_user_name = target_data.Name.Value
    else:
        target_user_name = target_user_id_or_name.Value

    message = None
    if user_requests:
        map_lambda = lambda request: format_processed_song_request(
            settings, request)
        processed_requests = map(map_lambda, user_requests)
        formatted_requests = ("{0} ".format(
            config.DefaultDelimeter).join(processed_requests))

        message = (settings.GotUserSongRequestsMessage.format(
            target_user_name, len(user_requests), formatted_requests))
    else:
        message = (settings.NoUserSongRequestsMessage.format(target_user_name))

    logger.info(message)
    manager.get_messenger().send_message(user_data.Id.Value, message)
Ejemplo n.º 2
0
def cancel_request(data_wrapper, settings, manager):
    user_data = helpers.wrap_user_data(data_wrapper.user_id,
                                       data_wrapper.user_name)

    # User can cancel request only for yourself.
    target_user_id_or_name = helpers.wrap_user_id_or_name(data_wrapper.user_id)

    param_count = data_wrapper.get_param_count()
    param_number_to_process = 1

    request_number = SongRequestNumber.All
    if param_count > 1:
        raw_request_number = data_wrapper.get_param(param_number_to_process)
        request_number = SongRequestNumber.TryParse(raw_request_number,
                                                    SongRequestNumber.All,
                                                    CSharpSettings(settings))

    reason = ""
    if param_count > 1:
        seems_like_request_number = data_wrapper.get_param(
            param_number_to_process)
        is_request_number = (
            seems_like_request_number.isdigit()
            or settings.is_all_parameter(seems_like_request_number))
        start_reason = 1 if not is_request_number else 2
        for i in range(start_reason, param_count):
            reason += " " + data_wrapper.get_param(i)
        reason = reason.strip()

    request_decision = SongRequestDecision.CreateWithUtcNow(
        user_data, target_user_id_or_name, request_number, reason)

    manager.cancel_request(request_decision)
Ejemplo n.º 3
0
def reset_option_for_user(data_wrapper, settings, logger, manager):
    user_data = helpers.wrap_user_data(data_wrapper.user_id,
                                       data_wrapper.user_name)

    raw_target_user_id_or_name = helpers.strip_at_symbol_for_name(
        data_wrapper.get_param(2))
    target_user_id_or_name = helpers.wrap_user_id_or_name(
        raw_target_user_id_or_name)

    target_data = manager.remove_all_user_requests_for(user_data,
                                                       target_user_id_or_name)

    if target_data:
        target_user_name = target_data.Name.Value
        message = (settings.ResetUserSongRequestOptionsMessage.format(
            target_user_name, user_data.Name.Value))
        logger.info(message)
        manager.get_messenger().send_message(user_data.Id.Value, message)
Ejemplo n.º 4
0
def approve_or_reject_request(command, data_wrapper, settings, manager):
    user_data = helpers.wrap_user_data(data_wrapper.user_id,
                                       data_wrapper.user_name)

    raw_target_user_id_or_name = helpers.strip_at_symbol_for_name(
        data_wrapper.get_param(1))
    target_user_id_or_name = helpers.wrap_user_id_or_name(
        raw_target_user_id_or_name)

    param_number_to_process = 2
    param_count = data_wrapper.get_param_count()

    request_number = SongRequestNumber.All
    if param_count > 1:
        raw_request_number = data_wrapper.get_param(param_number_to_process)
        request_number = SongRequestNumber.TryParse(raw_request_number,
                                                    SongRequestNumber.All,
                                                    CSharpSettings(settings))

    reason = ""
    if param_count > 1:
        seems_like_request_number = data_wrapper.get_param(
            param_number_to_process)
        is_request_number = (
            seems_like_request_number.isdigit()
            or settings.is_all_parameter(seems_like_request_number))
        if param_count > 2:
            start_reason = 2 if not is_request_number else 3
            for i in range(start_reason, param_count):
                reason += " " + data_wrapper.get_param(i)
            reason = reason.strip()
        elif not is_request_number:
            reason = seems_like_request_number

    request_decision = SongRequestDecision.CreateWithUtcNow(
        user_data, target_user_id_or_name, request_number, reason)

    if command == settings.CommandApproveSongRequest:
        manager.approve_request(request_decision)
    elif command == settings.CommandRejectSongRequest:
        manager.reject_request(request_decision)
    else:
        raise ValueError("Unexpected command to handle: {0}.".format(command))
def ProcessAddSongRequestCommand(command, data_wrapper):
    # Input example: !sr https://www.youtube.com/watch?v=CAEUnn0HNLM <Anything>
    # Command <YouTube link> <Anything>
    if ShouldSkipCommandProcessing(data_wrapper):
        return

    raw_user_id = data_wrapper.user_id
    raw_user_name = data_wrapper.user_name
    song_link = helpers.wrap_http_link(data_wrapper.get_param(1))
    if ScriptSettings.EnableLinkValidation:
        validation_result = YouTubeLinkRe.match(song_link.Value)
        if not validation_result:
            message = (ScriptSettings.FailedToValidateLinkMessage.format(
                raw_user_name, song_link.Value))
            Logger().info(message)
            Manager.get_messenger().send_message(raw_user_id, message)
            return

    user_data = helpers.wrap_user_data(raw_user_id, raw_user_name)

    Manager.add_request(user_data, song_link)