[langfilter] Use content_filter hook

This commit is contained in:
Hypolite Petovan 2018-04-01 02:31:37 -04:00
parent 35628d9229
commit 05a0e445ab
1 changed files with 13 additions and 12 deletions

View File

@ -19,13 +19,14 @@ use Friendica\Core\PConfig;
function langfilter_install() function langfilter_install()
{ {
Addon::registerHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body', 10); Addon::registerHook('content_filter', 'addon/langfilter/langfilter.php', 'langfilter_content_filter', 10);
Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings'); Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post'); Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
} }
function langfilter_uninstall() function langfilter_uninstall()
{ {
Addon::unregisterHook('content_filter', 'addon/langfilter/langfilter.php', 'langfilter_content_filter');
Addon::unregisterHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body'); Addon::unregisterHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
Addon::unregisterHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings'); Addon::unregisterHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
Addon::unregisterHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post'); Addon::unregisterHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
@ -114,7 +115,7 @@ function langfilter_addon_settings_post(App $a, &$b)
* expand it again. * expand it again.
*/ */
function langfilter_prepare_body(App $a, &$b) function langfilter_content_filter(App $a, &$hook_data)
{ {
$logged_user = local_user(); $logged_user = local_user();
if (!$logged_user) { if (!$logged_user) {
@ -124,7 +125,7 @@ function langfilter_prepare_body(App $a, &$b)
// Never filter own messages // Never filter own messages
// TODO: find a better way to extract this // TODO: find a better way to extract this
$logged_user_profile = $a->get_baseurl() . '/profile/' . $a->user['nickname']; $logged_user_profile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
if ($logged_user_profile == $b['item']['author-link']) { if ($logged_user_profile == $hook_data['item']['author-link']) {
return; return;
} }
@ -138,24 +139,26 @@ function langfilter_prepare_body(App $a, &$b)
if (!$minlen) { if (!$minlen) {
$minlen = 32; $minlen = 32;
} }
if (strlen($b['item']['body']) < $minlen) { if (strlen($hook_data['item']['body']) < $minlen) {
return; return;
} }
$spoken_config = PConfig::get(local_user(), 'langfilter', 'languages'); $read_languages_string = PConfig::get(local_user(), 'langfilter', 'languages');
$minconfidence = PConfig::get(local_user(), 'langfilter', 'minconfidence'); $minconfidence = PConfig::get(local_user(), 'langfilter', 'minconfidence');
// Don't filter if no spoken languages are configured // Don't filter if no spoken languages are configured
if (!$spoken_config) if (!$read_languages_string) {
return; return;
$spoken_languages = explode(',', $spoken_config); }
$read_languages_array = explode(',', $read_languages_string);
// Extract the language of the post // Extract the language of the post
$opts = $b['item']['postopts']; $opts = $hook_data['item']['postopts'];
if (!$opts) { if (!$opts) {
// no options associated to post // no options associated to post
return; return;
} }
if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) { if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) {
// no lang options associated to post // no lang options associated to post
return; return;
@ -174,10 +177,8 @@ function langfilter_prepare_body(App $a, &$b)
if (!$iso2) { if (!$iso2) {
return; return;
} }
$spoken = in_array($iso2, $spoken_languages);
if (!$spoken) { if (!in_array($iso2, $read_languages_array)) {
$rnd = random_string(8); $hook_data['filter_reasons'][] = L10n::t('Filtered language: %s', ucfirst($lang));
$b['html'] = '<div id="langfilter-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'langfilter-' . $rnd . '\'); >' . L10n::t('Hidden content in %s - Click to open/close', $lang) . '</div><div id="langfilter-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
} }
} }