123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- defined('GNUSOCIAL') || die();
- class SensitiveContentPlugin extends Plugin
- {
- public $blockerimage = 'img/blocker.png';
- public $hideforvisitors = false;
- const PLUGIN_VERSION = '0.1.0';
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = [
- 'name' => 'Sensitive Content',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'MoonMan',
- 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SensitiveContent/',
- 'description' => _m('Mark, hide/show sensitive notices like on Twitter.'),
- ];
- return true;
- }
-
- public function getBlockerImage(): string
- {
- return Plugin::staticPath('SensitiveContent', '') . $this->blockerimage;
- }
-
- public function shouldHide($profile): bool
- {
- if (isset($profile) && $profile instanceof Profile) {
- return $this->getHideSensitive($profile);
- }
- return $this->hideforvisitors;
- }
- public function getHideSensitive(Profile $profile): bool
- {
- $c = Cache::instance();
-
-
-
-
-
-
-
-
- $hidesensitive = $profile->getPref('MoonMan', 'hide_sensitive', '0');
- if (!empty($c)) {
-
- $c->set(Cache::key('profile:hide_sensitive:' . $profile->id), $hidesensitive);
- }
-
- if (is_null($hidesensitive)) {
- return false;
- }
- if (is_numeric($hidesensitive)) {
- return (bool)$hidesensitive;
- }
- return false;
- }
-
- public function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
- {
- $twitter_status['tags'] = $notice->getTags();
- }
- public function onTwitterUserArray($profile, &$twitter_user, $scoped)
- {
- if ($scoped instanceof Profile && $scoped->sameAs($profile)) {
- $twitter_user['hide_sensitive'] = $this->getHideSensitive($scoped);
- }
- }
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('settings/sensitivecontent',
- ['action' => 'sensitivecontentsettings']);
- }
- public function onEndAccountSettingsNav($action)
- {
- $action->menuItem(common_local_url('sensitivecontentsettings'),
- _m('MENU', 'Sensitive Content'),
- _m('Settings for display of sensitive content.'));
- return true;
- }
- public function onEndShowStyles(Action $action)
- {
- $blocker = $this->getBlockerImage();
- $styles = <<<EOB
- /* default no show */
- html .tagcontainer > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
- display: none;
- }
- html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
- display: block;
- /* hugh, two magic numbers below, sorry :( */
- width: 98%;
- height: 90%;
- position: absolute;
- /* z-index: 100; */
- /* background-color: #d4baba; */
- background-color: black;
- background-image: url({$blocker});
- background-repeat: no-repeat;
- background-position: center center;
- background-size: contain;
- transition: opacity 1s ease-in-out;
- }
- html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker.reveal {
- opacity: 0;
- }
- EOB;
- $action->style($styles);
- }
- public function onStartShowAttachmentRepresentation($out, $file)
- {
- $classes = 'sensitive-blocker';
- $thumbnail = null;
- try {
- $thumbnail = $file->getThumbnail();
- } catch (Exception $e) {
- $thumbnail = null;
- }
-
-
- $out->elementStart('div', [
- 'class' => 'attachment-wrapper',
-
- ]);
- $out->elementStart('div', [
- 'class' => $classes,
- 'onclick' => 'toggleSpoiler(event)',
-
- ]);
- $out->raw(' ');
- $out->elementEnd('div');
- }
- public function onEndShowAttachmentRepresentation($out, $file)
- {
- $out->elementEnd('div');
- }
- public function onEndShowScripts(Action $action)
- {
- $profile = $action->getScoped();
- $hidesensitive = $this->shouldHide($profile);
- $hidesensitive_string = $hidesensitive ? 'true' : 'false';
- $inline = <<<EOB
- window.hidesensitive = {$hidesensitive_string};
- function toggleSpoiler(evt) {
- if (window.hidesensitive) evt.target.classList.toggle('reveal');
- }
- EOB;
- $action->inlineScript($inline);
- }
- public function onEndOpenNoticeListItemElement(NoticeListItem $nli)
- {
- $rawtags = $nli->getNotice()->getTags();
- $classes = 'tagcontainer';
- foreach ($rawtags as $tag) {
- $classes = $classes . ' data-tag-' . $tag;
- }
- $nli->elementStart('span', ['class' => $classes]);
-
- }
- public function onStartCloseNoticeListItemElement(NoticeListItem $nli)
- {
- $nli->elementEnd('span');
- }
- public function onStartHtmlElement($action, &$attrs)
- {
- $profile = Profile::current();
- $hidesensitive = $this->shouldHide($profile);
- $attrs = array_merge($attrs,
- ['data-hidesensitive' => ($hidesensitive ? 'true' : 'false')]
- );
- }
-
- public function onQvitterEndShowHeadElements(Action $action)
- {
- $blocker = $this->getBlockerImage();
- common_log(LOG_DEBUG, 'SENSITIVECONTENT ' . $blocker);
- $styles = <<<EOB
- .sensitive-blocker {
- display: none;
- }
- div.stream-item.notice.sensitive-notice .sensitive-blocker {
- display: block;
- width: 100%;
- height: 100%;
- position: absolute;
- z-index: 100;
- background-color: #d4baba;
- background-image: url({$blocker});
- background-repeat: no-repeat;
- background-position: center center;
- background-size: contain;
- transition: opacity 1s ease-in-out;
- }
- .sensitive-blocker:hover {
- opacity: .5;
- }
- div.stream-item.notice.expanded.sensitive-notice .sensitive-blocker {
- display: none;
- background-color: transparent;
- background-image: none;
- }
- EOB;
- $action->style($styles);
- }
- public function onQvitterEndShowScripts(Action $action)
- {
- $action->script(Plugin::staticPath('SensitiveContent', '') . 'js/sensitivecontent.js');
- }
- }
|