La nostra soluzione per sfocare i contenuti NSFW

Nel forum di Blender Artists abbiamo una politica sui contenuti piuttosto liberale che consente nudità e violenza (entro certi limiti). Sebbene la maggior parte dei membri sia a suo agio con questo tipo di contenuti, esistono ovviamente pubblici e situazioni in cui non è appropriato (per noi principalmente scuole e bambini). Dato che facciamo ampio uso di gallerie a piastrelle con il plugin Topic List Preview, avevamo bisogno di un modo per rendere questo tipo di contenuti opzionali e tenerli nascosti di default.

La soluzione è stata più semplice da implementare del previsto e ho deciso di condividerla qui nel caso in cui qualcun altro possa trarne beneficio. Avvertenza: linkerò a contenuti NSFW. Andiamo!

Richiedevamo già il tag #nsfw per tutti i post pertinenti e lo abbiamo applicato rigorosamente negli ultimi mesi. Il nostro plugin AdSense è configurato per non mostrare annunci su queste pagine, poiché ciò ci avrebbe (e lo ha già!) messo nei guai con Google. (Un grande grazie a @neil per aver aggiunto questa funzione!)

Utilizzando del CSS, abbiamo aggiunto un effetto sfocatura e un testo di sovrapposizione per tutti i media all’interno di tali argomenti. La sfocatura verrà rimossa al passaggio del mouse:

/* visualizza sfocatura NSFW e testo di sovrapposizione su tutti i media negli argomenti #nsfw */
.tag-nsfw { 
	.topic-body .cooked img, 
	.topic-body .cooked iframe, 
	.topic-body .cooked .lazyYT-container, 
	.topic-thumbnail img {
        filter: blur(10px);	
        -webkit-transition: .3s ease-in-out;
        transition: .2s ease-in-out;
	}

	.topic-body:hover .cooked img, 
	.topic-body:hover .cooked iframe,
	.topic-body:hover .cooked .lazyYT-container, 			
	.topic-thumbnail:hover img {
        filter: blur(0);	
        -webkit-transition: .3s ease-in-out;
        transition: .2s ease-in-out;
	}

	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before,
	.topic-thumbnail a:before {
	    z-index:2;
        padding: 5px;
        font-size:1em;
        position:absolute;

        color:#fff;
        content: '⚠️ Contenuto maturo - Passa il mouse per mostrare';
        background: #e86800;

	}
	
	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before {
        top: 15px;
        left: 10px;
	}

	.topic-thumbnail a:before {
        top: 65px;
        left: 20px;
	}
	
	.topic-body .cooked a.lightbox:hover:before, 
	.topic-body .cooked iframe:hover:before,
	.topic-thumbnail a:hover:before {		
	    display:none;
	}
}

Immagini e video in un argomento ora appaiono così:

E in tutte le gallerie a piastrelle TLP, abbiamo:

Successivamente, abbiamo aggiunto una preferenza che permette agli utenti di disabilitare la sfocatura per il proprio account. Si è rivelato più semplice da implementare di quanto pensassi, utilizzando campi personalizzati.

Abbiamo iniziato creando un campo personalizzato di tipo casella di controllo:

Poi, abbiamo riutilizzato del codice per aggiungere il tag ‘nsfw-always-show’ alla classe del body per questi utenti:

<!-- aggiungi le preferenze NSFW dell'utente corrente al tag body -->
<script type="text/discourse-plugin" version="0.8">

// https://meta.discourse.org/t/css-classes-for-group-membership-for-simplified-ui-mode/60838/2
if (window.jQuery) {
    window.jQuery(function ($) {
        var u = Discourse.User.current();

        // mostra sempre NSFW
        if (u.custom_fields.user_field_2) {
            console.log('mostra nsfw per l\'utente');
            $('body').addClass('nsfw-always-show' );
        }

    });
};

</script>

Un ultimo pezzo di CSS rimuove la sfocatura per questi utenti:

/* nascondi campi personalizzati dal modulo di registrazione */

.login-form .user-fields {display:none;}

/* disabilita sfocatura NSFW per gli utenti che lo hanno impostato nelle loro preferenze */

.nsfw-always-show .tag-nsfw {
	.topic-body .cooked img, 
	.topic-body .cooked iframe, 
	.topic-body .cooked .lazyYT-container, 
	.topic-thumbnail img {
        filter: blur(0px);	
	}
	
	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before,
	.topic-thumbnail a:before {
	    display:none;
	    content: none;
	}
}

Un problema noto con questo approccio è che non funziona ancora bene su mobile, poiché :hover non è supportato.

Se vuoi vedere questo in azione puoi visitare la nostra pagina del tag #nsfw, ma, beh, potresti vedere alcuni contenuti NSFW lì :slight_smile:

Spero che questo sia stato utile per qualcuno!

55 Mi Piace

Also, on desktop, if an image take some space on the screen, you can easily hover it by mistake.
Instead of hovering to unblur, what about dynamically add a button “show the nsfw image” on top or under each nsfw image ?

4 Mi Piace

At krita-artists.org We have slightly modified this to be on click and not on hover. However the setting used in the profile doesn’t work. Even if the user has set to show nsfw content he gets the blurred content. Is there a fix for it?

I can’t edit my OP anymore, but here’s the updated code. Mind sharing your ‘on click’ solution too?

<!-- add current user's nsfw preferences to body tag -->
<script type="text/discourse-plugin" version="0.8.7">

// https://meta.discourse.org/t/css-classes-for-group-membership-for-simplified-ui-mode/60838/2
if (window.jQuery) {
    window.jQuery(function ($) {

        let currentUser = api.getCurrentUser();
        
        if (currentUser) {
            api.container.lookup('store:main').find('user', currentUser.username).then((user) => {

                if (user.user_fields[2]) {
                    $('body').addClass('nsfw-always-show' );
                }
            });
        }
    });
};
</script>
2 Mi Piace

Our onlick solution is hacky and I think may not be ideal, we just removed blur from hover and added blur by default. The message was also changed to say user has click to go the post. Now the user has to click to go to post and click again to reveal the nsfw image in lightbox. Which is cumbersome but it prevents inadvertent hover and reveal. It might be good to use js to remove blur on click.

/* display nsfw blur and overlay text on any media in #nswf topics */
.tag-nsfw { 
.topic-thumbnail {
    overflow:hidden;
}

	.topic-body .cooked .lightbox img, 
	.topic-body .cooked iframe, 
	.topic-body .cooked .lazyYT-container, 
	.topic-thumbnail img {
    filter: blur(30px);	
    -webkit-transition: .3s ease-in-out;
    transition: .2s ease-in-out;
	}

	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before,
	.topic-thumbnail a:before {
	    z-index:2;
    padding: 5px;
    font-size:1em;
    position:absolute;

    color:#fff;
    content: '⚠️ Mature content - Click to see the picture';
    background: #000;

	}
	
	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before {
    top: 50%;
    left: 10px;
    right: 10px;
    text-align:center;
	}

	.topic-thumbnail a:before {
    top: 65px;
    left: 20px;
	}
	
}

/* hide custom fields from signup form */
.login-form .user-fields {display:none;}

/* disable nsfw blurring for users who set this in their preferences */
.nsfw-always-show .tag-nsfw {
	.topic-body .cooked img, 
	.topic-body .cooked iframe, 
	.topic-body .cooked .lazyYT-container, 
	.topic-thumbnail img {
    filter: blur(0px);	
	}
	
	.topic-body .cooked a.lightbox:before, 
	.topic-body .cooked iframe:before,
	.topic-thumbnail a:before {
	    display:none;
	    content: none;
	}
}

@bartv @Terrapop

If someone wants to take this on, its possible to use Discourse Image Filter - plugin - Discourse Meta to build a auto nsfw blurring plugin.

4 Mi Piace

Hi there, what would I have to do to remove the blur and instead make the text be hyperlinked as we want to have it link to a donation off-site.

1 Mi Piace

You can’t do that with CSS only I’m afraid; you’ll have to add your own code to do that.

2 Mi Piace

Just remove the filter: blur(10px); lines from the CSS.

5 Mi Piace

@bartv I’ve made the first post wiki, please feel free to update it as needed! :pray:

10 Mi Piace