<commit_before>=begin
  Camaleon CMS is a content management system
  Copyright (C) 2015 by <NAME>
  Email: <EMAIL>
  This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the  GNU Affero General Public License (GPLv3) for more details.
=end
class CamaleonCms::UserDecorator < CamaleonCms::ApplicationDecorator
  include CamaleonCms::CustomFieldsConcern
  delegate_all
  require 'faraday'

  # return the identifier
  def the_username
    object.username
  end

  # return the fullname
  def the_name
    object.fullname
  end

  # return the role title of this user for current site
  def the_role
    object.get_role(h.current_site).name.titleize
  end

  # return the avatar for this user, default: assets/admin/img/no_image.jpg
  def the_avatar
    avatar_exists? ? object.get_meta("avatar") : h.asset_url("camaleon_cms/admin/img/no_image.jpg")
  end

  # return the slogan for this user, default: Hello World
  def the_slogan
    object.get_meta("slogan", "Hello World")
  end

  # generate all seo attributes for profile page
  def the_seo
    h.cama_build_seo({ image: (the_avatar rescue nil), title: the_name, object: self })
  end

  # return front url for this user
  def the_url(*args)
    args = args.extract_options!
    args[:user_id] = the_id
    args[:user_name] = the_name.parameterize
    args[:locale] = get_locale unless args.include?(:locale)
    args[:format] = "html"
    as_path = args.delete(:as_path)
    h.cama_url_to_fixed("cama_profile_#{as_path.present? ? "path" : "url"}", args)
  end

  # return the url for the profile in the admin module
  def the_admin_profile_url
    h.cama_admin_profile_url(object.id)
  end

  # return all contents created by this user in current site
  def the_contents
    h.current_site.posts.where(user_id: object.id)
  end

  # cache identifier, the format is: [current-site-prefix]/[object-id]-[object-last_updated]/[current locale]
  # key: additional key for the model
  def cache_prefix(key = "")
    "#{h.current_site.cache_prefix}/user#{object.id}#{"/#{key}" if key.present?}"
  end

  private

  def avatar_exists?
    if object.get_meta('avatar').present?
      File.exist?(h.cama_url_to_file_path(object.get_meta('avatar'))) || Faraday.head(object.get_meta('avatar')).status == 200
    else
      false
    end
  end
end<commit_msg>removed faraday dependency (pending avatar file verification)<commit_after>=begin
  Camaleon CMS is a content management system
  Copyright (C) 2015 by <NAME>
  Email: <EMAIL>
  This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the  GNU Affero General Public License (GPLv3) for more details.
=end
class CamaleonCms::UserDecorator < CamaleonCms::ApplicationDecorator
  include CamaleonCms::CustomFieldsConcern
  delegate_all
  require 'faraday'

  # return the identifier
  def the_username
    object.username
  end

  # return the fullname
  def the_name
    object.fullname
  end

  # return the role title of this user for current site
  def the_role
    object.get_role(h.current_site).name.titleize
  end

  # return the avatar for this user, default: assets/admin/img/no_image.jpg
  def the_avatar
    avatar_exists? ? object.get_meta("avatar") : h.asset_url("camaleon_cms/admin/img/no_image.jpg")
  end

  # return the slogan for this user, default: Hello World
  def the_slogan
    object.get_meta("slogan", "Hello World")
  end

  # generate all seo attributes for profile page
  def the_seo
    h.cama_build_seo({ image: (the_avatar rescue nil), title: the_name, object: self })
  end

  # return front url for this user
  def the_url(*args)
    args = args.extract_options!
    args[:user_id] = the_id
    args[:user_name] = the_name.parameterize
    args[:locale] = get_locale unless args.include?(:locale)
    args[:format] = "html"
    as_path = args.delete(:as_path)
    h.cama_url_to_fixed("cama_profile_#{as_path.present? ? "path" : "url"}", args)
  end

  # return the url for the profile in the admin module
  def the_admin_profile_url
    h.cama_admin_profile_url(object.id)
  end

  # return all contents created by this user in current site
  def the_contents
    h.current_site.posts.where(user_id: object.id)
  end

  # cache identifier, the format is: [current-site-prefix]/[object-id]-[object-last_updated]/[current locale]
  # key: additional key for the model
  def cache_prefix(key = "")
    "#{h.current_site.cache_prefix}/user#{object.id}#{"/#{key}" if key.present?}"
  end

  private

  def avatar_exists?
    # TODO change verification
    # if object.get_meta('avatar').present?
    #   File.exist?(h.cama_url_to_file_path(object.get_meta('avatar'))) || Faraday.head(object.get_meta('avatar')).status == 200
    # else
    #   false
    # end
    object.get_meta('avatar').present?
  end
end
{'fraction_non_alphanumeric': 0.08990192517253905, 'fraction_numerical': 0.0032691609153650564, 'mean_word_length': 3.939013452914798, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '7650331', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1737, 'n_words': 549}