Merge pull request #4182 from annando/notify-public-post

Sends notifications for public posts with "uid=0"
This commit is contained in:
Hypolite Petovan 2018-01-07 15:54:57 -05:00 committed by GitHub
commit 6496a721ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 41 deletions

View file

@ -43,7 +43,7 @@ define('FRIENDICA_PLATFORM', 'Friendica');
define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_CODENAME', 'Asparagus');
define('FRIENDICA_VERSION', '3.6-dev'); define('FRIENDICA_VERSION', '3.6-dev');
define('DFRN_PROTOCOL_VERSION', '2.23'); define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1238); define('DB_UPDATE_VERSION', 1239);
define('NEW_UPDATE_ROUTINE_VERSION', 1170); define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/** /**

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 3.6-dev (Asparagus) -- Friendica 3.6-dev (Asparagus)
-- DB_UPDATE_VERSION 1238 -- DB_UPDATE_VERSION 1239
-- ------------------------------------------ -- ------------------------------------------
@ -972,7 +972,7 @@ CREATE TABLE IF NOT EXISTS `thread` (
INDEX `uid_created` (`uid`,`created`), INDEX `uid_created` (`uid`,`created`),
INDEX `uid_commented` (`uid`,`commented`), INDEX `uid_commented` (`uid`,`commented`),
INDEX `uid_wall_created` (`uid`,`wall`,`created`), INDEX `uid_wall_created` (`uid`,`wall`,`created`),
INDEX `private_wall_received` (`private`,`wall`,`received`) INDEX `private_wall_commented` (`private`,`wall`,`commented`)
) DEFAULT COLLATE utf8mb4_general_ci; ) DEFAULT COLLATE utf8mb4_general_ci;
-- --

View file

@ -106,11 +106,8 @@ function notification($params)
} }
if ($params['type'] == NOTIFY_COMMENT) { if ($params['type'] == NOTIFY_COMMENT) {
$p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1", $p = dba::select('thread', ['ignored'], ['iid' => $parent_id], ['limit' => 1]);
intval($parent_id), if (DBM::is_result($p) && $p["ignored"]) {
intval($params['uid'])
);
if ($p && count($p) && ($p[0]["ignored"])) {
logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG); logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
return; return;
} }
@ -134,13 +131,10 @@ function notification($params)
$p = null; $p = null;
if ($params['otype'] === 'item' && $parent_id) { if ($params['otype'] === 'item' && $parent_id) {
$p = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $p = dba::select('item', [], ['id' => $parent_id], ['limit' => 1]);
intval($parent_id),
intval($params['uid'])
);
} }
$item_post_type = item_post_type($p[0]); $item_post_type = item_post_type($p);
// "a post" // "a post"
$dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'), $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
@ -153,12 +147,12 @@ function notification($params)
$dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'), $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
$itemlink, $itemlink,
$p[0]['author-name'], $p['author-name'],
$item_post_type); $item_post_type);
} }
// "your post" // "your post"
if ($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall']) { if ($p['owner-name'] == $p['author-name'] && $p['wall']) {
$dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'), $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
$itemlink, $itemlink,
@ -648,6 +642,22 @@ function notification($params)
return false; return false;
} }
/**
* @brief Checks for users who should be notified
*
* @param int $itemid ID of the item for which the check should be done
*/
function check_user_notification($itemid) {
// fetch all users in the thread
$users = dba::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $itemid);
while ($user = dba::fetch($users)) {
check_item_notification($itemid, $user['uid']);
}
dba::close($users);
}
/** /**
* @brief Checks for item related notifications and sends them * @brief Checks for item related notifications and sends them
* *
@ -661,19 +671,22 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
$profiles = $notification_data["profiles"]; $profiles = $notification_data["profiles"];
$user = q("SELECT `notify-flags`, `language`, `username`, `email`, `nickname` FROM `user` WHERE `uid` = %d", intval($uid)); $fields = ['notify-flags', 'language', 'username', 'email', 'nickname'];
if (!$user) $user = dba::select('user', $fields, ['uid' => $uid], ['limit' => 1]);
if (!DBM::is_result($user)) {
return false; return false;
}
$owner = q("SELECT `id`, `url` FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1", intval($uid)); $owner = dba::select('contact', ['url'], ['self' => true, 'uid' => $uid], ['limit' => 1]);
if (!$owner) if (!DBM::is_result($owner)) {
return false; return false;
}
// This is our regular URL format // This is our regular URL format
$profiles[] = $owner[0]["url"]; $profiles[] = $owner["url"];
// Notifications from Diaspora are often with an URL in the Diaspora format // Notifications from Diaspora are often with an URL in the Diaspora format
$profiles[] = System::baseUrl()."/u/".$user[0]["nickname"]; $profiles[] = System::baseUrl()."/u/".$user["nickname"];
$profiles2 = array(); $profiles2 = array();
@ -721,10 +734,10 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
// Generate the notification array // Generate the notification array
$params = array(); $params = array();
$params["uid"] = $uid; $params["uid"] = $uid;
$params["notify_flags"] = $user[0]["notify-flags"]; $params["notify_flags"] = $user["notify-flags"];
$params["language"] = $user[0]["language"]; $params["language"] = $user["language"];
$params["to_name"] = $user[0]["username"]; $params["to_name"] = $user["username"];
$params["to_email"] = $user[0]["email"]; $params["to_email"] = $user["email"];
$params["item"] = $item[0]; $params["item"] = $item[0];
$params["parent"] = $item[0]["parent"]; $params["parent"] = $item[0]["parent"];
$params["link"] = System::baseUrl().'/display/'.urlencode($item[0]["guid"]); $params["link"] = System::baseUrl().'/display/'.urlencode($item[0]["guid"]);
@ -735,11 +748,7 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
if ($item[0]["parent-uri"] === $item[0]["uri"]) { if ($item[0]["parent-uri"] === $item[0]["uri"]) {
// Send a notification for every new post? // Send a notification for every new post?
$r = q("SELECT `notify_new_posts` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `notify_new_posts` LIMIT 1", $send_notification = dba::exists('contact', ['id' => $item[0]['contact-id'], 'notify_new_posts' => true]);
intval($item[0]['contact-id']),
intval($uid)
);
$send_notification = DBM::is_result($r);
if (!$send_notification) { if (!$send_notification) {
$tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d", $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
@ -747,10 +756,11 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
if (DBM::is_result($tags)) { if (DBM::is_result($tags)) {
foreach ($tags AS $tag) { foreach ($tags AS $tag) {
$r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`", $condition = ['nurl' => normalise_link($tag["url"]), 'uid' => $uid, 'notify_new_posts' => true];
normalise_link($tag["url"]), intval($uid)); $r = dba::exists('contact', $condition);
if (DBM::is_result($r)) if ($r) {
$send_notification = true; $send_notification = true;
}
} }
} }
} }
@ -776,10 +786,10 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
// Is it a post that the user had started or where he interacted? // Is it a post that the user had started or where he interacted?
$parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid` $parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid`
WHERE `thread`.`iid` = %d AND `thread`.`uid` = %d AND NOT `thread`.`ignored` AND WHERE `thread`.`iid` = %d AND NOT `thread`.`ignored` AND
(`thread`.`mention` OR `item`.`author-link` IN ($profile_list)) (`thread`.`mention` OR `item`.`author-link` IN ($profile_list))
LIMIT 1", LIMIT 1",
intval($item[0]["parent"]), intval($uid)); intval($item[0]["parent"]));
if ($parent && !isset($params["type"])) { if ($parent && !isset($params["type"])) {
$params["type"] = NOTIFY_COMMENT; $params["type"] = NOTIFY_COMMENT;

View file

@ -1129,7 +1129,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
add_shadow_entry($current_post); add_shadow_entry($current_post);
} }
check_item_notification($current_post, $uid); check_user_notification($current_post);
if ($notify) { if ($notify) {
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "Notifier", $notify_type, $current_post); Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "Notifier", $notify_type, $current_post);

View file

@ -190,14 +190,14 @@ function community_getitems($start, $itemspage, $content)
item_joins() . " AND `contact`.`self` item_joins() . " AND `contact`.`self`
WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated` WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
AND NOT `thread`.`private` AND `thread`.`wall` AND NOT `thread`.`private` AND `thread`.`wall`
ORDER BY `thread`.`received` DESC LIMIT " . intval($start) . ", " . intval($itemspage) ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
); );
return dba::inArray($r); return dba::inArray($r);
} elseif ($content == 'global') { } elseif ($content == 'global') {
$r = dba::p("SELECT " . item_fieldlists() . " FROM `thread` $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() . INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() .
"WHERE `thread`.`uid` = 0 AND `verb` = ? "WHERE `thread`.`uid` = 0 AND `verb` = ?
ORDER BY `thread`.`created` DESC LIMIT " . intval($start) . ", " . intval($itemspage), ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage),
ACTIVITY_POST ACTIVITY_POST
); );
return dba::inArray($r); return dba::inArray($r);

View file

@ -1067,6 +1067,8 @@ function item_post(App $a) {
create_tags_from_item($post_id); create_tags_from_item($post_id);
create_files_from_item($post_id); create_files_from_item($post_id);
check_user_notification($post_id);
// Insert an item entry for UID=0 for global entries. // Insert an item entry for UID=0 for global entries.
// We now do it in the background to save some time. // We now do it in the background to save some time.
// This is important in interactive environments like the frontend or the API. // This is important in interactive environments like the frontend or the API.

View file

@ -1601,7 +1601,7 @@ class DBStructure {
"uid_created" => array("uid","created"), "uid_created" => array("uid","created"),
"uid_commented" => array("uid","commented"), "uid_commented" => array("uid","commented"),
"uid_wall_created" => array("uid","wall","created"), "uid_wall_created" => array("uid","wall","created"),
"private_wall_received" => array("private","wall","received"), "private_wall_commented" => array("private","wall","commented"),
) )
); );
$database["tokens"] = array( $database["tokens"] = array(

View file

@ -151,6 +151,7 @@ class Post extends BaseObject
} else { } else {
$edpost = array("editpost/" . $item['id'], t("Edit")); $edpost = array("editpost/" . $item['id'], t("Edit"));
} }
$dropping = in_array($item['uid'], [0, local_user()]);
} else { } else {
$edpost = false; $edpost = false;
} }
@ -764,6 +765,14 @@ class Post extends BaseObject
$qcomment = (($qc) ? explode("\n", $qc) : null); $qcomment = (($qc) ? explode("\n", $qc) : null);
} }
// Fetch the user id from the parent when the owner user is empty
$uid = $conv->getProfileOwner();
$parent_uid = $this->getDataValue('uid');
if (!empty($parent_uid) && empty($uid) && ($uid != $parent_uid)) {
$uid = $parent_uid;
}
$template = get_markup_template($this->getCommentBoxTemplate()); $template = get_markup_template($this->getCommentBoxTemplate());
$comment_box = replace_macros($template, array( $comment_box = replace_macros($template, array(
'$return_path' => $a->query_string, '$return_path' => $a->query_string,
@ -773,7 +782,7 @@ class Post extends BaseObject
'$id' => $this->getId(), '$id' => $this->getId(),
'$parent' => $this->getId(), '$parent' => $this->getId(),
'$qcomment' => $qcomment, '$qcomment' => $qcomment,
'$profile_uid' => $conv->getProfileOwner(), '$profile_uid' => $uid,
'$mylink' => $a->remove_baseurl($a->contact['url']), '$mylink' => $a->remove_baseurl($a->contact['url']),
'$mytitle' => t('This is you'), '$mytitle' => t('This is you'),
'$myphoto' => $a->remove_baseurl($a->contact['thumb']), '$myphoto' => $a->remove_baseurl($a->contact['thumb']),

View file

@ -67,7 +67,7 @@ class Thread extends BaseObject
$this->writable = can_write_wall($this->profile_owner) || $writable; $this->writable = can_write_wall($this->profile_owner) || $writable;
break; break;
case 'community': case 'community':
$this->profile_owner = local_user(); $this->profile_owner = 0;
$this->writable = $writable; $this->writable = $writable;
break; break;
default: default: