<commit_before>
require './Server/Screen_Name/model'

# =====================================================
# Create
# =====================================================


post '/Screen_Name' do
  puts params[:screen_name]
  begin
    Screen_Name.create(params)
  rescue Ok::Invalid => e
    json false, e.msg
  end
end # === post

post('/me' do
  params[:user] = request[:user]
  sn = Screen_Name.create(c);
  name = sn.data[:screen_name]

  json true, "Your new life has been created: #{name}" , {screen_name: name}
end


# =====================================================
# Read
# =====================================================


# ==== Redirect to canonical screen name.
get '/:type/:screen_name' do
  type = params[:type]
  sn   = params[:screen_name]
  pass if ['me', 'bot'].include?(type)

  canon = Screen_Name.to_canon(sn);

  if canon == sn
    pass
  else
    redirect 301, "/#{type}/#{canon}"
  end
end

  app.get('/me/:screen_name', function (req, resp, next) {

    F.run('read life by screen name', {screen_name: req.params.screen_name}, function (o) {
      var life = o.val;
      if (!life)
        return next();

      var data      = req.OK.template_data('Screen_Name/me');
      data['title'] = "The life of " + life.data.screen_name;
      req.OK.render_template();
    });
    return;

  });



# =====================================================
# Update
# =====================================================


  OK.put('/screen_names/:name', function (req, resp, next) {
    var n = req.params.name;
    var new_vals = _.pick(req.body,
                          'read_able',
                          'read_able_list',
                          'about',
                          'at_url',
                          'at_pass_phrase',
                          'bot_url',
                          'bot_pass_phrase'
                         );

     var r = New_River(next);

     r

     .on_next('invalid', function (j) {
       write.json_fail(resp, j.invalid_msg);
     })

     .job('update screen name:', n, [Screen_Name, 'update', req.user, n, new_vals])

     .run(function (r) {

       var c   = req.user;
       var msg = null;

       switch (c.sanitized_data.read_able) {
         case 'W':
           msg = "Updated settings: Anyone online may see this homepage.";
         break;
         case 'N':
           msg = "Updated settings: no one but you can see this homepage.";
         break;
         case 'S':
           msg = "Updated settings: The following may see your homepage: " + c.sanitized_data.read_able_list.join(', ');
         break;
         default:
           if (c.sanitized_data.about) {
           msg = 'Your "About Me" section has been updated.';
         } else if (c.sanitized_data.new_screen_name) {
           msg = 'Your screen name has been updated.';
         } else {
           msg = 'Your web app info has been updated.';
         };

       };

       var public = {};
       _.each(new_vals, function (v, k) {
         if (c.sanitized_data.hasOwnProperty(k))
           public[k] = c.sanitized_data[k];
         if (k.indexOf('pass_phrase') > -1 && (public[k] || '').trim().length > 0)
           public[k] = '[hidden]';
       });

       write.json_success(resp, msg, {updated: public});
     });

  });


# =====================================================
# Trash/Delete
# =====================================================
<commit_msg>Imported: trash/undo /screen_name/:name<commit_after>
require './Server/Screen_Name/model'

# =====================================================
# Create
# =====================================================


post '/Screen_Name' do
  puts params[:screen_name]
  begin
    Screen_Name.create(params)
  rescue Ok::Invalid => e
    json false, e.msg
  end
end # === post

post('/me' do
  params[:user] = request[:user]
  sn = Screen_Name.create(c);
  name = sn.data[:screen_name]

  json true, "Your new life has been created: #{name}" , {screen_name: name}
end


# =====================================================
# Read
# =====================================================


# ==== Redirect to canonical screen name.
get '/:type/:screen_name' do
  type = params[:type]
  sn   = params[:screen_name]
  pass if ['me', 'bot'].include?(type)

  canon = Screen_Name.to_canon(sn);

  if canon == sn
    pass
  else
    redirect 301, "/#{type}/#{canon}"
  end
end

get '/me/:screen_name' do

  begin
    life = Screen_Name.read_by_screen_name params.screen_name
    html 'Screen_Name/me', title: "The life of #{life.data[:screen_name]}"
  rescue Screen_Name::Not_Found => e
    pass
  end

end



# =====================================================
# Update
# =====================================================


put '/screen_names/:name' do
  n = params[:name]
  begin
    sn = Screen_Name.update params, request[:user]
    c   = request[:user]
    msg = case sn.data[:read_able]
          when 'W'
            "Updated settings: Anyone online may see this homepage."
          when 'N'
            "Updated settings: no one but you can see this homepage."
          when 'S'
            "Updated settings: The following may see your homepage: " + c.sanitized_data.read_able_list.join(', ')
          else
            if c.sanitized_data.about
              'Your "About Me" section has been updated.'
            elsif (c.sanitized_data.new_screen_name)
              'Your screen name has been updated.'
            else
              'Your web app info has been updated.'
            end
          end # === case

    json true,  msg, updated: sn.to_public

  rescue Screen_Name::Invalid => e

    json false, e.msg

  end # === begin

end # === post /screen_name/:name


# =====================================================
# Trash/Delete
# =====================================================

{'fraction_non_alphanumeric': 0.26079535756955113, 'fraction_numerical': 0.00136542072025943, 'mean_word_length': 2.579718998167379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7529489', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1668, 'n_words': 425}