app/controllers/users_controller.rb
# show user information, user friends and user balance (balance is only shown for friends)
show user information, user friends and user balance (balance is only shown for friends)
show user information user friends and user balance ( balance is only shown for friends )
def show
    id = params[:id]
    @user2 = User.find_by_id(id)
    if !@user2
      logger.debug2  "invalid request. User with id #{id} was not found"
      save_flash_key '.invalid_request'
      redirect_to :action => :index
      return
    end
    logger.debug2  "@user2 = #{@user2.id} #{@user2.user_name}"
    if !(login_user = @users.find { |u| u.provider == @user2.provider })
      logger.debug2 "invalid request. Not connected with a #{@user2.provider} account"
      save_flash_key '.invalid_request'
      redirect_to :action => :index
      return
    elsif login_user_ids.index(@user2.user_id)
    elsif (@user2.friend?(@users) <= 6)
    else
      logger.warn2 "invalid request. Did not find any mutual friends between @user2 #{@user2.user_id} #{@user2.short_user_name} and login_user #{login_user.user_id} #{login_user.short_user_name}"
      save_flash_key '.invalid_request'
      redirect_to :action => :index
    end
    @page_values = {}
    @user_nav_links = []
    if !@user2.balance_at or @user2.balance_at.to_yyyymmdd != Sequence.get_last_exchange_rate_date
      @user2.recalculate_balance
      @user2.reload
    end
    logger.debug2 "@user2.friend?(@users) = #{@user2.friend?(@users)}"
    if (@user2.friend?(@users) <= 2)
      if @users.find { |user| user.user_id == @user2.user_id }
        tabs = %w(gifts balance) 
      else
        tabs = %w(friends gifts balance) 
      end
    else
      tabs = [] 
    end
    logger.debug2 "tabs = #{tabs}"
    if tabs.size <= 1
      tab = tabs.first
    else
      tab = params[:tab].to_s || tabs.first
      tab = tabs.first unless tabs.index(tab)
    end
    logger.debug2  "tab = #{tab}"
    last_row_id = params[:last_row_id].to_s
    last_row_id = nil if last_row_id == ''
    if last_row_id =~ /^[0-9]+$/
      last_row_id = last_row_id.to_i
    else
      last_row_id = nil
    end
    if last_row_id and get_next_set_of_rows_error?(last_row_id)
      logger.debug2  "return empty ajax response with dummy row with correct last_row_id to client"
      @api_gifts = @users2 = []
      @last_row_id = get_last_row_id()
      respond_to do |format|
        format.js {}
      end
      return
    end
    @user_nav_links << ["tabs", tabs] if tabs.size > 1
    logger.debug2 "@user_nav_links = #{@user_nav_links}"
    if %w(gifts balance).index(tab)
      statuses = %w(open closed all)
      status = params[:status] || 'all'
      status = 'all' unless %w(open closed all).index(status)
      directions = %w(giver receiver both)
      direction = params[:direction] || 'both'
      direction = 'both' unless %w(giver receiver both).index(direction)
      logger.debug2  "balance filters: status = #{status}, direction = #{direction}"
      if %w(gifts balance).index(tab)
        @user_nav_links << ["deal_status", statuses]
        @user_nav_links << ["deal_direction", directions]
      end
      logger.debug2 "@user_nav_links = #{@user_nav_links}"
      @page_values[:status] = status
      @page_values[:direction] = direction
      logger.debug "status = #{status}, direction = #{direction}"
      api_gifts = ApiGift.where('(user_id_giver = ? or user_id_receiver = ?) and gifts.deleted_at is null',
                            @user2.user_id, @user2.user_id).references(:gifts, :api_gifts).includes(:gift, :giver, :receiver).find_all do |ag|
        (((status == 'all') or ((status == 'open') and !ag.gift.received_at) or ((status == 'closed') and ag.gift.received_at)) and
            (direction == 'both' or (direction == 'giver' and ag.user_id_giver == @user2.user_id) or (direction == 'receiver' and ag.user_id_receiver == @user2.user_id)))
      end
      .sort_by { |ag| [(ag.gift.received_at || ag.created_at), ag.id]}
      .reverse
      @api_gifts, @last_row_id = get_next_set_of_rows(api_gifts, last_row_id)
      logger.debug2  "@gifts.size = #{@api_gifts.size}, @last_row_id = #{@last_row_id}" if debug_ajax?
    end
    if tab == 'friends'
      logger.debug2 'simple friends search - just return login users friends'
      logger.debug2 "user2 = #{@user2.user_id} #{@user2.short_user_name}"
      users = User.app_friends(User.cache_friend_info([@user2]), [2,3]).sort_by_user_name
      logger.debug2 "users = " + users.collect { |u| u.user_id}.join(', ')
      @users2, @last_row_id = get_next_set_of_rows(users, last_row_id)
      logger.debug2  "@users2.size = #{@users2.size}, @last_row_id = #{@last_row_id}" if debug_ajax?
    end 
    if tab == 'gifts'
      @first_comment_id = nil
    end 
    @page_values[:tab] = tab
    respond_to do |format|
      format.html {}
      format.js {}
    end
  end
def show id = params [ :id ] @user2 = User . find_by_id ( id ) if ! @user2 logger . debug2 "invalid request. User with id #{id} was not found" save_flash_key '.invalid_request' redirect_to :action => :index return end logger . debug2 "@user2 = #{@user2.id} #{@user2.user_name}" if ! ( login_user = @users . find { | u | u . provider == @user2 . provider } ) logger . debug2 "invalid request. Not connected with a #{@user2.provider} account" save_flash_key '.invalid_request' redirect_to :action => :index return elsif login_user_ids . index ( @user2 . user_id ) elsif ( @user2 . friend? ( @users ) <= 6 ) else logger . warn2 "invalid request. Did not find any mutual friends between @user2 #{@user2.user_id} #{@user2.short_user_name} and login_user #{login_user.user_id} #{login_user.short_user_name}" save_flash_key '.invalid_request' redirect_to :action => :index end @page_values = { } @user_nav_links = [ ] if ! @user2 . balance_at or @user2 . balance_at . to_yyyymmdd != Sequence . get_last_exchange_rate_date @user2 . recalculate_balance @user2 . reload end logger . debug2 "@user2.friend?(@users) = #{@user2.friend?(@users)}" if ( @user2 . friend? ( @users ) <= 2 ) if @users . find { | user | user . user_id == @user2 . user_id } tabs = %w( gifts balance ) else tabs = %w( friends gifts balance ) end else tabs = [ ] end logger . debug2 "tabs = #{tabs}" if tabs . size <= 1 tab = tabs . first else tab = params [ :tab ] . to_s || tabs . first tab = tabs . first unless tabs . index ( tab ) end logger . debug2 "tab = #{tab}" last_row_id = params [ :last_row_id ] . to_s last_row_id = nil if last_row_id == '' if last_row_id =~ / ^[0-9]+$ / last_row_id = last_row_id . to_i else last_row_id = nil end if last_row_id and get_next_set_of_rows_error? ( last_row_id ) logger . debug2 "return empty ajax response with dummy row with correct last_row_id to client" @api_gifts = @users2 = [ ] @last_row_id = get_last_row_id ( ) respond_to do | format | format . js { } end return end @user_nav_links << [ "tabs" , tabs ] if tabs . size > 1 logger . debug2 "@user_nav_links = #{@user_nav_links}" if %w( gifts balance ) . index ( tab ) statuses = %w( open closed all ) status = params [ :status ] || 'all' status = 'all' unless %w( open closed all ) . index ( status ) directions = %w( giver receiver both ) direction = params [ :direction ] || 'both' direction = 'both' unless %w( giver receiver both ) . index ( direction ) logger . debug2 "balance filters: status = #{status}, direction = #{direction}" if %w( gifts balance ) . index ( tab ) @user_nav_links << [ "deal_status" , statuses ] @user_nav_links << [ "deal_direction" , directions ] end logger . debug2 "@user_nav_links = #{@user_nav_links}" @page_values [ :status ] = status @page_values [ :direction ] = direction logger . debug "status = #{status}, direction = #{direction}" api_gifts = ApiGift . where ( '(user_id_giver = ? or user_id_receiver = ?) and gifts.deleted_at is null' , @user2 . user_id , @user2 . user_id ) . references ( :gifts , :api_gifts ) . includes ( :gift , :giver , :receiver ) . find_all do | ag | ( ( ( status == 'all' ) or ( ( status == 'open' ) and ! ag . gift . received_at ) or ( ( status == 'closed' ) and ag . gift . received_at ) ) and ( direction == 'both' or ( direction == 'giver' and ag . user_id_giver == @user2 . user_id ) or ( direction == 'receiver' and ag . user_id_receiver == @user2 . user_id ) ) ) end . sort_by { | ag | [ ( ag . gift . received_at || ag . created_at ) , ag . id ] } . reverse @api_gifts , @last_row_id = get_next_set_of_rows ( api_gifts , last_row_id ) logger . debug2 "@gifts.size = #{@api_gifts.size}, @last_row_id = #{@last_row_id}" if debug_ajax? end if tab == 'friends' logger . debug2 'simple friends search - just return login users friends' logger . debug2 "user2 = #{@user2.user_id} #{@user2.short_user_name}" users = User . app_friends ( User . cache_friend_info ( [ @user2 ] ) , [ 2 , 3 ] ) . sort_by_user_name logger . debug2 "users = " + users . collect { | u | u . user_id } . join ( ', ' ) @users2 , @last_row_id = get_next_set_of_rows ( users , last_row_id ) logger . debug2 "@users2.size = #{@users2.size}, @last_row_id = #{@last_row_id}" if debug_ajax? end if tab == 'gifts' @first_comment_id = nil end @page_values [ :tab ] = tab respond_to do | format | format . html { } format . js { } end end
show user information, user friends and user balance (balance is only shown for friends)
show user information user friends and user balance ( balance is only shown for friends )
# check user.id # check if login users are allowed to see @user2 # ok - login user # ok - friends or friend of friend ## not friend. Must have a mutual friend to allow user/show #friends1 = User.app_friends([login_user]).collect { |f| f.user_id_receiver } ## todo: google+ friend invitation to a stalker #friends2 = User.app_friends([@user2]).collect { |f| f.user_id_receiver } #mutual_friends = friends1 & friends2 #if mutual_friends.size == 0 #  return #end # ok - found mutual friends # recalculate balance once every day # todo: should only recalculate user balance from @user2.balance_at and to today # get params: tab, last_row_id and todo: filters # tab: blank = friends or balance - only friends can see balance # my account - friends information available in Friends menu # friend - friend and balance information are allowed # non friend - do not display any information (friends, balance and gifts information not allowed) # http request: return first 10 gifts (last_row_id = nil) # ajax request: return next 10 gifts (last_row_id != nil) # problem with ajax request. # can be invalid last_row_id - can be too many get-more-rows ajax requests - max one request every 3 seconds - more info in log # return "empty" ajax response with dummy row with correct last_row_id to client # initialize array with user navigation links. 0-3 sections with links. Up to 9 links. # show balance for @user2 - only friends can see balance information # show gifts for @user2 - only friends can see gifts for @user2 # filters: status (open, closed and all) and direction (giver, receiver and both) # add nav links with deal status and deal direction # find gifts with @user2 as giver or receiver # this select only shows gifts for @user2.provider - that is not gifts across providers # apply status and direction filters # return next 10 gifts - first 10 for http request - next 10 for ajax request # show friends for @user2 - sort by user_name # users = @user2.app_friends.collect { |f| f.friend }.sort { |a,b| a.user_name <=> b.user_name} # todo: google+ - should show @user2's friends and followers - should not show users stalking user2 # users = User.all # uncomment to test ajax # return next 10 users - first 10 for http request - next 10 for ajax request # friends # show 4 last comments for each gift # gifts # debug nav links # logger.debug2 "params          = #{params}" # logger.debug2 "@user_nav_links = #{@user_nav_links}" # logger.debug2 "@page_values    = #{@page_values}" # format.json { render json: @comment, status: :created, location: @comment }