User safety settings

This commit is contained in:
Marcin Kurczewski 2013-10-14 00:25:40 +02:00
parent 11072779d9
commit 23fc89c30c
7 changed files with 157 additions and 22 deletions

View file

@ -31,7 +31,7 @@ body {
color: black;
}
#top-nav ul {
#top-nav ul.main-nav {
margin: 0 -0.75em;
padding: 0;
list-style-type: none;
@ -41,36 +41,63 @@ body {
margin: 0 1.5em;
}
#top-nav li {
#top-nav li.main-nav-item {
display: inline-block;
}
#top-nav li input,
#top-nav li a {
#top-nav li.main-nav-item a {
color: black;
display: inline-block;
margin-bottom: 3px;
text-decoration: none;
}
#top-nav li a {
#top-nav li.main-nav-item a {
padding: 0.2em 0.75em;
outline: 0;
}
#top-nav li.search {
background: white;
margin: 0 0.25em;
padding: 0.2em 0.5em;
}
#top-nav li a:focus,
#top-nav li a:hover {
#top-nav li.main-nav-item a:focus,
#top-nav li.main-nav-item a:hover {
color: firebrick;
border-bottom: 3px solid firebrick;
margin-bottom: 0;
}
#top-nav li.safety {
float: right;
}
#top-nav li.safety ul {
list-style-type: none;
margin: 0;
padding: 0;
}
#top-nav li.safety li {
display: inline-block;
}
#top-nav li.safety a {
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid black;
}
#top-nav li.safety span {
display: none;
}
#top-nav li.safety .safety-safe .enabled { background: lime; }
#top-nav li.safety .safety-safe .disabled { background: green; }
#top-nav li.safety .safety-sketchy .enabled { background: yellow; }
#top-nav li.safety .safety-sketchy .disabled { background: olive; }
#top-nav li.safety .safety-unsafe .enabled { background: red; }
#top-nav li.safety .safety-unsafe .disabled { background: maroon; }
#top-nav li.search {
float: right;
background: white;
margin: 0 0.25em;
padding: 0.2em 0.5em;
}
#top-nav li.search input {
border: 0;
}

View file

@ -14,3 +14,29 @@ if ($.when.all === undefined)
return deferred;
}
}
$(function()
{
$('.safety a').click(function(e)
{
e.preventDefault();
var aDom = $(this);
if (aDom.hasClass('inactive'))
return;
aDom.addClass('inactive');
var url = $(this).attr('href') + '?json';
$.get(url, function(data)
{
if (data['success'])
{
window.location.reload();
}
else
{
alert(data['errorMessage']);
}
});
});
});

View file

@ -79,13 +79,13 @@ class PostController
if ($this->config->browsing->endlessScrolling)
$this->context->scripts []= 'paginator-endless.js';
#redirect requests in form of /posts/?query=... to canonical address
//redirect requests in form of /posts/?query=... to canonical address
$formQuery = InputHelper::get('query');
if ($formQuery !== null)
{
$this->context->transport->searchQuery = $formQuery;
if (strpos($formQuery, '/') !== false)
throw new SimpleException('Search query contains invalid characters.');
throw new SimpleException('Search query contains invalid characters');
$url = \Chibi\UrlHelper::route('post', 'list', ['query' => urlencode($formQuery)]);
\Chibi\UrlHelper::forward($url);
return;
@ -102,19 +102,24 @@ class PostController
{
$dbQuery->from('post');
/* safety */
$allowedSafety = array_filter(PostSafety::getAll(), function($safety)
{
return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety));
return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)) and
$this->context->user->hasEnabledSafety($safety);
});
//todo safety [user choice]
$dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')');
foreach ($allowedSafety as $s)
$dbQuery->put($s);
/* hidden */
if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden'))
$dbQuery->andNot('hidden');
/* search tokens */
$tokens = array_filter(array_unique(explode(' ', $query)), function($x) { return $x != ''; });
if (count($tokens) > $this->config->browsing->maxSearchTokens)
throw new SimpleException('Too many search tokens (maximum: ' . $this->config->browsing->maxSearchTokens . ')');

View file

@ -19,4 +19,23 @@ class UserController
$this->context->subTitle = $name;
throw new SimpleException('Not implemented');
}
/**
* @route /user/toggle-safety/{safety}
*/
public function toggleSafetyAction($safety)
{
if (!$this->context->loggedIn)
throw new SimpleException('Not logged in');
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleExcetpion('Invalid safety');
$this->context->user->enableSafety($safety,
!$this->context->user->hasEnabledSafety($safety));
R::store($this->context->user);
$this->context->transport->success = true;
}
}

View file

@ -1,7 +1,7 @@
<?php
class Model_User extends RedBean_SimpleModel
{
public function avatarUrl($size = 32)
public function getAvatarUrl($size = 32)
{
$subject = !empty($this->email)
? $this->email
@ -10,4 +10,45 @@ class Model_User extends RedBean_SimpleModel
$url = 'http://www.gravatar.com/avatar/' . $hash . '?s=' . $size . '&d=retro';
return $url;
}
public function getSetting($key)
{
$settings = json_decode($this->settings, true);
return isset($settings[$key])
? $settings[$key]
: null;
}
public function setSetting($key, $value)
{
$settings = json_decode($this->settings, true);
$settings[$key] = $value;
$settings = json_encode($settings);
if (strlen($settings) > 200)
throw new SimpleException('Too much data');
$this->settings = $settings;
}
public function hasEnabledSafety($safety)
{
return $this->getSetting('safety-' . $safety) !== false;
}
public function enableSafety($safety, $enabled)
{
if (!$enabled)
{
$this->setSetting('safety-' . $safety, false);
$anythingEnabled = false;
foreach (PostSafety::getAll() as $safety)
if (self::hasEnabledSafety($safety))
$anythingEnabled = true;
if (!$anythingEnabled)
$this->setSetting('safety-' . PostSafety::Safe, true);
}
else
{
$this->setSetting('safety-' . $safety, true);
}
}
}

View file

@ -18,7 +18,7 @@
<body>
<nav id="top-nav">
<div class="main-wrapper">
<ul>
<ul class="main-nav">
<?php
$nav = [];
@ -57,12 +57,29 @@
foreach ($nav as $navItem)
{
list ($text, $link) = $navItem;
echo '<li>';
echo '<li class="main-nav-item">';
echo '<a href="' . $link . '">' . $text . '</a>';
echo '</li>';
}
?>
<li>
<?php if ($this->context->loggedIn): ?>
<li class="safety">
<ul>
<?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety))): ?>
<li class="safety-<?php echo strtolower(PostSafety::toString($safety)) ?>">
<a href="<?php echo \Chibi\UrlHelper::route('user', 'toggle-safety', ['safety' => $safety]) ?>" class="<?php echo $this->context->user->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>">
<span><?php echo PostSafety::toString($safety) ?></span>
</a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</li>
<?php endif ?>
<li class="search">
<form name="search" action="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>" method="get">
<input type="search" name="query" placeholder="Search&hellip;" value="<?php echo isset($this->context->transport->searchQuery) ? htmlspecialchars($this->context->transport->searchQuery) : '' ?>">
</form>

View file

@ -95,7 +95,7 @@
<?php foreach ($this->context->transport->post->via('favoritee')->sharedUser as $user): ?>
<li>
<a href="<?php echo \Chibi\UrlHelper::route('user', 'view', ['name' => $user->name]) ?>">
<img src="<?php echo $user->avatarUrl() ?>" alt="<?php echo $user->name ?>">
<img src="<?php echo $user->getAvatarUrl() ?>" alt="<?php echo $user->name ?>">
</a>
</li>
<?php endforeach ?>