<commit_before>class Api::V1::ActivitiesController < Api::ApiController

  doorkeeper_for :create, :update, :destroy
  before_action :find_activity, except: [:index, :create, :uids_and_flags]

  def index
    # FIXME: original API doesn't support index
    @activities = Activity.all
  end

  # GET
  def show
    render json: @activity, meta: {status: 'success', message: nil, errors: nil}
  end

  # PATCH, PUT
  def update
    if @activity.update(activity_params)
      @status = :success
      @message = "Activity Updated"
    else
      @status = :failed
      @message = "Activity Update Failed"
    end

    render json: @activity, meta: {status: @status, message: @message, errors: @activity.errors}

  end

  # POST
  def create
    activity = Activity.new(activity_params)
    activity.set_owner(current_user) if activity.ownable?

    if activity.valid? && activity.save
      @status = :success
      @response_status = :ok
      @message = "Activity Created"
    else
      @status = :failed
      @response_status = :unprocessable_entity
      @message = "Activity Create Failed"
    end

    render json: activity,
      meta: {status: @status, message: @message, errors: activity.errors},
      status: @response_status
  end

  # DELETE
  def destroy

    if @activity.destroy!
      render json: Activity.new, meta: {status: 'success', message: "Activity Destroy Successful", errors: nil}
    else
      render json: @activity, meta: {status: 'failed', message: "Activity Destroy Failed", errors: @activity.errors}
    end

  end

  def follow_up_activity_name_and_supporting_info
    follow_up_activity_name = @activity.follow_up_activity&.name
    supporting_info = @activity.supporting_info
    render json: {follow_up_activity_name: follow_up_activity_name, supporting_info: supporting_info}
  end

  def supporting_info
    supporting_info = @activity.supporting_info
    render json: {supporting_info: supporting_info}
  end

  def uids_and_flags
    uids_and_flags_arr = Activity.all.map do |activity|
      { uid: activity.uid, flag: activity.flag }
    end
    render json: uids_and_flags_arr
  end

  private

  def find_activity
    @activity = Activity.find_by_uid(params[:id]) || Activity.find_by_id(params[:id])
  end

  def activity_params
    params.delete(:access_token)
    params.delete(:activity) # read only and therefore static<commit_msg>reformat uids_and_flags as obj instead of arr<commit_after>class Api::V1::ActivitiesController < Api::ApiController

  doorkeeper_for :create, :update, :destroy
  before_action :find_activity, except: [:index, :create, :uids_and_flags]

  def index
    # FIXME: original API doesn't support index
    @activities = Activity.all
  end

  # GET
  def show
    render json: @activity, meta: {status: 'success', message: nil, errors: nil}
  end

  # PATCH, PUT
  def update
    if @activity.update(activity_params)
      @status = :success
      @message = "Activity Updated"
    else
      @status = :failed
      @message = "Activity Update Failed"
    end

    render json: @activity, meta: {status: @status, message: @message, errors: @activity.errors}

  end

  # POST
  def create
    activity = Activity.new(activity_params)
    activity.set_owner(current_user) if activity.ownable?

    if activity.valid? && activity.save
      @status = :success
      @response_status = :ok
      @message = "Activity Created"
    else
      @status = :failed
      @response_status = :unprocessable_entity
      @message = "Activity Create Failed"
    end

    render json: activity,
      meta: {status: @status, message: @message, errors: activity.errors},
      status: @response_status
  end

  # DELETE
  def destroy

    if @activity.destroy!
      render json: Activity.new, meta: {status: 'success', message: "Activity Destroy Successful", errors: nil}
    else
      render json: @activity, meta: {status: 'failed', message: "Activity Destroy Failed", errors: @activity.errors}
    end

  end

  def follow_up_activity_name_and_supporting_info
    follow_up_activity_name = @activity.follow_up_activity&.name
    supporting_info = @activity.supporting_info
    render json: {follow_up_activity_name: follow_up_activity_name, supporting_info: supporting_info}
  end

  def supporting_info
    supporting_info = @activity.supporting_info
    render json: {supporting_info: supporting_info}
  end

  def uids_and_flags
    uids_and_flags_obj = {}
    Activity.all.each do |activity|
      uids_and_flags_obj[activity.uid] = {flag: activity.flag}
    end
    render json: uids_and_flags_obj
  end

  private

  def find_activity
    @activity = Activity.find_by_uid(params[:id]) || Activity.find_by_id(params[:id])
  end

  def activity_params
    params.delete(:access_token)
    params.delete(:activity) # read only and therefore static
{'fraction_non_alphanumeric': 0.09451599501454092, 'fraction_numerical': 0.0004154549231408392, 'mean_word_length': 3.6476833976833976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '817480', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1455, 'n_words': 392}