There is now a central function for item notifications

pull/2301/head
Michael Vogel 2016-01-28 22:58:05 +01:00
parent d6c1889a20
commit b3e86f4e6b
4 changed files with 100 additions and 253 deletions

View File

@ -14,6 +14,7 @@ require_once('include/queue_fn.php');
require_once('include/lock.php');
require_once('include/threads.php');
require_once('mod/share.php');
require_once('include/enotify.php');
function diaspora_dispatch_public($msg) {
@ -1598,47 +1599,6 @@ function diaspora_comment($importer,$xml,$msg) {
proc_run('php','include/notifier.php','comment-import',$message_id);
}
$myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0 ",
dbesc($parent_item['uri']),
intval($importer['uid'])
);
if(count($myconv)) {
$importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
foreach($myconv as $conv) {
// now if we find a match, it means we're in this conversation
if(! link_compare($conv['author-link'],$importer_url))
continue;
require_once('include/enotify.php');
$conv_parent = $conv['parent'];
notification(array(
'type' => NOTIFY_COMMENT,
'notify_flags' => $importer['notify-flags'],
'language' => $importer['language'],
'to_name' => $importer['username'],
'to_email' => $importer['email'],
'uid' => $importer['uid'],
'item' => $datarray,
'link' => $a->get_baseurl().'/display/'.urlencode($datarray['guid']),
'source_name' => $datarray['author-name'],
'source_link' => $datarray['author-link'],
'source_photo' => $datarray['author-avatar'],
'verb' => ACTIVITY_POST,
'otype' => 'item',
'parent' => $conv_parent,
'parent_uri' => $parent_uri
));
// only send one notification
break;
}
}
return;
}
@ -1793,7 +1753,6 @@ function diaspora_conversation($importer,$xml,$msg) {
intval($conversation['id'])
);
require_once('include/enotify.php');
notification(array(
'type' => NOTIFY_MAIL,
'notify_flags' => $importer['notify-flags'],

View File

@ -633,4 +633,98 @@ function notification($params) {
}
/**
* @brief Checks for item related notifications and sends them
*
* @param int $itemid ID of the item for which the check should be done
* @param int $uid User ID
* @param str $profile (Optional) Can be used for connector post. Otherwise empty.
*/
function check_item_notification($itemid, $uid, $profile = "", $defaulttype = "") {
$user = q("SELECT `notify-flags`, `language`, `username`, `email` FROM `user` WHERE `uid` = %d", intval($uid));
if (!$user)
return false;
$owner = q("SELECT `id`, `url` FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1", intval($uid));
if (!$owner)
return false;
$local_profile = $owner[0]["url"];
if ($profile == "")
$profile = $local_profile;
// Only act if it is a "real" post
// We need the additional check for the "local_profile" because of mixed situations on connector networks
$item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-name`, `author-link`, `author-avatar`, `guid`,
`parent-uri`, `uri`, `contact-id`
FROM `item` WHERE `id` = %d AND `verb` IN ('%s', '') AND `type` != 'activity' AND
`author-link` != '%s' AND `author-link` != '%s' LIMIT 1",
intval($itemid), dbesc(ACTIVITY_POST), dbesc($profile), dbesc($local_profile));
if (!$item)
return false;
// Generate the notification array
$params = array();
$params["uid"] = $uid;
$params["notify_flags"] = $user[0]["notify-flags"];
$params["language"] = $user[0]["language"];
$params["to_name"] = $user[0]["username"];
$params["to_email"] = $user[0]["email"];
$params["item"] = $item[0];
$params["parent"] = $item[0]["parent"];
$params["link"] = App::get_baseurl().'/display/'.urlencode($item[0]["guid"]);
$params["otype"] = 'item';
$params["source_name"] = $item[0]["author-name"];
$params["source_link"] = $item[0]["author-link"];
$params["source_photo"] = $item[0]["author-avatar"];
if ($item[0]["parent-uri"] === $item[0]["uri"]) {
// 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",
intval($item[0]['contact-id']),
intval($uid)
);
$send_notification = count($r);
if (!$send_notification) {
$tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
intval(TERM_OBJ_POST), intval($itemid), intval(TERM_MENTION), intval($uid));
if (count($tags)) {
foreach ($tags AS $tag) {
$r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
normalise_link($tag["url"]), intval($uid));
if (count($r))
$send_notification = true;
}
}
}
if ($send_notification) {
$params["type"] = NOTIFY_SHARE;
$params["verb"] = ACTIVITY_TAG;
}
}
// Is the user mentioned in this post?
if ($item[0]["mention"] OR strpos($item[0]["tag"], "=".$profile."]") OR ($defaulttype == NOTIFY_TAGSELF)) {
$params["type"] = NOTIFY_TAGSELF;
$params["verb"] = ACTIVITY_TAG;
}
// 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`
WHERE `thread`.`iid` = %d AND `thread`.`uid` = %d AND NOT `thread`.`ignored` AND
(`thread`.`mention` OR `item`.`author-link` = '%s')
LIMIT 1",
intval($item[0]["parent"]), intval($uid), dbesc($profile));
if ($parent AND !isset($params["type"])) {
$params["type"] = NOTIFY_COMMENT;
$params["verb"] = ACTIVITY_POST;
}
if (isset($params["type"]))
notification($params);
}
?>

View File

@ -16,6 +16,7 @@ require_once('include/ostatus.php');
require_once('include/feed.php');
require_once('include/Contact.php');
require_once('mod/share.php');
require_once('include/enotify.php');
require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
@ -1313,67 +1314,15 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
create_files_from_item($current_post);
// Only check for notifications on start posts
if ($arr['parent-uri'] === $arr['uri']) {
if ($arr['parent-uri'] === $arr['uri'])
add_thread($current_post);
logger('item_store: Check notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
// 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",
intval($arr['contact-id']),
intval($arr['uid'])
);
$send_notification = count($r);
if (!$send_notification) {
$tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
intval(TERM_OBJ_POST), intval($current_post), intval(TERM_MENTION), intval($arr['uid']));
if (count($tags)) {
foreach ($tags AS $tag) {
$r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
normalise_link($tag["url"]), intval($arr['uid']));
if (count($r))
$send_notification = true;
}
}
}
if ($send_notification) {
logger('item_store: Send notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
$u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
intval($arr['uid']));
$item = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d",
intval($current_post),
intval($arr['uid'])
);
$a = get_app();
require_once('include/enotify.php');
notification(array(
'type' => NOTIFY_SHARE,
'notify_flags' => $u[0]['notify-flags'],
'language' => $u[0]['language'],
'to_name' => $u[0]['username'],
'to_email' => $u[0]['email'],
'uid' => $u[0]['uid'],
'item' => $item[0],
'link' => $a->get_baseurl().'/display/'.urlencode($arr['guid']),
'source_name' => $item[0]['author-name'],
'source_link' => $item[0]['author-link'],
'source_photo' => $item[0]['author-avatar'],
'verb' => ACTIVITY_TAG,
'otype' => 'item',
'parent' => $arr['parent']
));
logger('item_store: Notification sent for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
}
} else {
else {
update_thread($parent_id);
add_shadow_entry($arr);
}
check_item_notification($current_post, $uid);
if ($notify)
proc_run('php', "include/notifier.php", $notify_type, $current_post);
@ -1569,37 +1518,6 @@ function tag_deliver($uid,$item_id) {
return;
}
// send a notification
// use a local photo if we have one
$r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
intval($u[0]['uid']),
dbesc(normalise_link($item['author-link']))
);
$photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
require_once('include/enotify.php');
notification(array(
'type' => NOTIFY_TAGSELF,
'notify_flags' => $u[0]['notify-flags'],
'language' => $u[0]['language'],
'to_name' => $u[0]['username'],
'to_email' => $u[0]['email'],
'uid' => $u[0]['uid'],
'item' => $item,
'link' => $a->get_baseurl() . '/display/'.urlencode(get_item_guid($item['id'])),
'source_name' => $item['author-name'],
'source_link' => $item['author-link'],
'source_photo' => $photo,
'verb' => ACTIVITY_TAG,
'otype' => 'item',
'parent' => $item['parent']
));
$arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
call_hooks('tagged', $arr);
@ -3245,33 +3163,7 @@ function local_delivery($importer,$data) {
}
if($posted_id && $parent) {
proc_run('php',"include/notifier.php","comment-import","$posted_id");
if((! $is_like) && (! $importer['self'])) {
require_once('include/enotify.php');
notification(array(
'type' => NOTIFY_COMMENT,
'notify_flags' => $importer['notify-flags'],
'language' => $importer['language'],
'to_name' => $importer['username'],
'to_email' => $importer['email'],
'uid' => $importer['importer_uid'],
'item' => $datarray,
'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
'source_name' => stripslashes($datarray['author-name']),
'source_link' => $datarray['author-link'],
'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
? $importer['thumb'] : $datarray['author-avatar']),
'verb' => ACTIVITY_POST,
'otype' => 'item',
'parent' => $parent,
'parent_uri' => $parent_uri,
));
}
}
return 0;
@ -3393,59 +3285,6 @@ function local_delivery($importer,$data) {
$posted_id = item_store($datarray);
// find out if our user is involved in this conversation and wants to be notified.
if(!x($datarray['type']) || $datarray['type'] != 'activity') {
$myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
dbesc($top_uri),
intval($importer['importer_uid'])
);
if(count($myconv)) {
$importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
// first make sure this isn't our own post coming back to us from a wall-to-wall event
if(! link_compare($datarray['author-link'],$importer_url)) {
foreach($myconv as $conv) {
// now if we find a match, it means we're in this conversation
if(! link_compare($conv['author-link'],$importer_url))
continue;
require_once('include/enotify.php');
$conv_parent = $conv['parent'];
notification(array(
'type' => NOTIFY_COMMENT,
'notify_flags' => $importer['notify-flags'],
'language' => $importer['language'],
'to_name' => $importer['username'],
'to_email' => $importer['email'],
'uid' => $importer['importer_uid'],
'item' => $datarray,
'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
'source_name' => stripslashes($datarray['author-name']),
'source_link' => $datarray['author-link'],
'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
? $importer['thumb'] : $datarray['author-avatar']),
'verb' => ACTIVITY_POST,
'otype' => 'item',
'parent' => $conv_parent,
'parent_uri' => $parent_uri
));
// only send one notification
break;
}
}
}
}
continue;
}
}

View File

@ -561,29 +561,6 @@ function ostatus_import($xml,$importer,&$contact, &$hub) {
}
logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
$item["id"] = $item_id;
if ($mention AND in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_SHARE))) {
$u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
$r = q("SELECT `parent` FROM `item` WHERE `id` = %d", intval($item_id));
notification(array(
'type' => NOTIFY_TAGSELF,
'notify_flags' => $u[0]["notify-flags"],
'language' => $u[0]["language"],
'to_name' => $u[0]["username"],
'to_email' => $u[0]["email"],
'uid' => $item["uid"],
'item' => $item,
'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item_id)),
'source_name' => $item["author-name"],
'source_link' => $item["author-link"],
'source_photo' => $item["author-avatar"],
'verb' => ACTIVITY_TAG,
'otype' => 'item',
'parent' => $r[0]["parent"]
));
}
}
}
@ -1028,28 +1005,6 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
// Add the conversation entry (but don't fetch the whole conversation)
ostatus_store_conversation($newitem, $conversation_url);
if ($mention) {
$u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($uid));
$r = q("SELECT `parent` FROM `item` WHERE `id` = %d", intval($newitem));
notification(array(
'type' => NOTIFY_TAGSELF,
'notify_flags' => $u[0]["notify-flags"],
'language' => $u[0]["language"],
'to_name' => $u[0]["username"],
'to_email' => $u[0]["email"],
'uid' => $uid,
'item' => $arr,
'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($newitem)),
'source_name' => $arr["author-name"],
'source_link' => $arr["author-link"],
'source_photo' => $arr["author-avatar"],
'verb' => ACTIVITY_TAG,
'otype' => 'item',
'parent' => $r[0]["parent"]
));
}
// If the newly created item is the top item then change the parent settings of the thread
// This shouldn't happen anymore. This is supposed to be absolote.
if ($arr["uri"] == $first_id) {