Merge pull request #566 from MrPetovan/task/add-content_filter-hook

Add content_filter hook
This commit is contained in:
Michael Vogel 2018-04-06 06:44:00 +02:00 committed by GitHub
commit c4bd245a1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 119 additions and 112 deletions

View file

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-03-29 22:43+0700\n"
"POT-Creation-Date: 2018-04-01 11:11-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,62 +17,62 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: langfilter.php:57
#: langfilter.php:58
msgid "Language Filter"
msgstr ""
#: langfilter.php:58
#: langfilter.php:59
msgid ""
"This addon tries to identify the language posts are writen in. If it does "
"not match any language specifed below, posts will be hidden by collapsing "
"them."
msgstr ""
#: langfilter.php:59
#: langfilter.php:60
msgid "Use the language filter"
msgstr ""
#: langfilter.php:60
#: langfilter.php:61
msgid "Able to read"
msgstr ""
#: langfilter.php:60
#: langfilter.php:61
msgid ""
"List of abbreviations (iso2 codes) for languages you speak, comma separated. "
"For example \"de,it\"."
msgstr ""
#: langfilter.php:61
#: langfilter.php:62
msgid "Minimum confidence in language detection"
msgstr ""
#: langfilter.php:61
#: langfilter.php:62
msgid ""
"Minimum confidence in language detection being correct, from 0 to 100. Posts "
"will not be filtered when the confidence of language detection is below this "
"percent value."
msgstr ""
#: langfilter.php:62
#: langfilter.php:63
msgid "Minimum length of message body"
msgstr ""
#: langfilter.php:62
#: langfilter.php:63
msgid ""
"Minimum number of characters in message body for filter to be used. Posts "
"shorter than this will not be filtered. Note: Language detection is "
"unreliable for short content (<200 characters)."
msgstr ""
#: langfilter.php:63
#: langfilter.php:64
msgid "Save Settings"
msgstr ""
#: langfilter.php:104
#: langfilter.php:105
msgid "Language Filter Settings saved."
msgstr ""
#: langfilter.php:181
#: langfilter.php:182
#, php-format
msgid "Hidden content in %s - Click to open/close"
msgid "Filtered language: %s"
msgstr ""

View file

@ -19,13 +19,14 @@ use Friendica\Core\PConfig;
function langfilter_install()
{
Addon::registerHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body', 10);
Addon::registerHook('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter', 10);
Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
}
function langfilter_uninstall()
{
Addon::unregisterHook('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter');
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_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
@ -114,7 +115,7 @@ function langfilter_addon_settings_post(App $a, &$b)
* expand it again.
*/
function langfilter_prepare_body(App $a, &$b)
function langfilter_prepare_body_content_filter(App $a, &$hook_data)
{
$logged_user = local_user();
if (!$logged_user) {
@ -124,7 +125,7 @@ function langfilter_prepare_body(App $a, &$b)
// Never filter own messages
// TODO: find a better way to extract this
$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;
}
@ -138,24 +139,26 @@ function langfilter_prepare_body(App $a, &$b)
if (!$minlen) {
$minlen = 32;
}
if (strlen($b['item']['body']) < $minlen) {
if (strlen($hook_data['item']['body']) < $minlen) {
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');
// Don't filter if no spoken languages are configured
if (!$spoken_config)
if (!$read_languages_string) {
return;
$spoken_languages = explode(',', $spoken_config);
}
$read_languages_array = explode(',', $read_languages_string);
// Extract the language of the post
$opts = $b['item']['postopts'];
$opts = $hook_data['item']['postopts'];
if (!$opts) {
// no options associated to post
return;
}
if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) {
// no lang options associated to post
return;
@ -174,10 +177,8 @@ function langfilter_prepare_body(App $a, &$b)
if (!$iso2) {
return;
}
$spoken = in_array($iso2, $spoken_languages);
if (!$spoken) {
$rnd = random_string(8);
$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>';
if (!in_array($iso2, $read_languages_array)) {
$hook_data['filter_reasons'][] = L10n::t('Filtered language: %s', ucfirst($lang));
}
}