ReWork Notification Model/Module/Object/Repository/Factory
- Introduce Repository for interaction with "notify" table - Introduce Factory for read-only notification objects (they're just loosely based on notification the table!) - Introduce Objects for type-safe usage at the presentation layer - Reworked Model, which is now fully based on the notify table, including generated fields (cache, ..)
This commit is contained in:
+25
-24
@@ -5905,19 +5905,20 @@ function api_friendica_notification($type)
|
||||
if ($a->argc!==3) {
|
||||
throw new BadRequestException("Invalid argument count");
|
||||
}
|
||||
$notes = DI::notification()->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50);
|
||||
|
||||
$notifications = DI::notification()->select([], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]);
|
||||
|
||||
if ($type == "xml") {
|
||||
$xmlnotes = [];
|
||||
if (!empty($notes)) {
|
||||
foreach ($notes as $note) {
|
||||
$xmlnotes[] = ["@attributes" => $note];
|
||||
if (!empty($notifications)) {
|
||||
foreach ($notifications as $notification) {
|
||||
$xmlnotes[] = ["@attributes" => $notification->toArray()];
|
||||
}
|
||||
}
|
||||
|
||||
$notes = $xmlnotes;
|
||||
$notifications = $xmlnotes;
|
||||
}
|
||||
return api_format_data("notes", $type, ['note' => $notes]);
|
||||
return api_format_data("notes", $type, ['note' => $notifications->getArrayCopy()]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5935,37 +5936,37 @@ function api_friendica_notification($type)
|
||||
*/
|
||||
function api_friendica_notification_seen($type)
|
||||
{
|
||||
$a = DI::app();
|
||||
$a = DI::app();
|
||||
$user_info = api_get_user($a);
|
||||
|
||||
if (api_user() === false || $user_info === false) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
if ($a->argc!==4) {
|
||||
if ($a->argc !== 4) {
|
||||
throw new BadRequestException("Invalid argument count");
|
||||
}
|
||||
|
||||
$id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0);
|
||||
|
||||
$nm = DI::notification();
|
||||
$note = $nm->getByID($id);
|
||||
if (is_null($note)) {
|
||||
throw new BadRequestException("Invalid argument");
|
||||
}
|
||||
try {
|
||||
$notification = DI::notification()->getByID($id);
|
||||
$notification->setSeen();
|
||||
|
||||
$nm->setSeen($note);
|
||||
if ($note['otype']=='item') {
|
||||
// would be really better with an ItemsManager and $im->getByID() :-P
|
||||
$item = Item::selectFirstForUser(api_user(), [], ['id' => $note['iid'], 'uid' => api_user()]);
|
||||
if (DBA::isResult($item)) {
|
||||
// we found the item, return it to the user
|
||||
$ret = api_format_items([$item], $user_info, false, $type);
|
||||
$data = ['status' => $ret];
|
||||
return api_format_data("status", $type, $data);
|
||||
if ($notification->otype == 'item') {
|
||||
// would be really better with an ItemsManager and $im->getByID() :-P
|
||||
$item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->iid, 'uid' => api_user()]);
|
||||
if (DBA::isResult($item)) {
|
||||
// we found the item, return it to the user
|
||||
$ret = api_format_items([$item], $user_info, false, $type);
|
||||
$data = ['status' => $ret];
|
||||
return api_format_data("status", $type, $data);
|
||||
}
|
||||
// the item can't be found, but we set the notification as seen, so we count this as a success
|
||||
}
|
||||
// the item can't be found, but we set the note as seen, so we count this as a success
|
||||
return api_format_data('result', $type, ['result' => "success"]);
|
||||
} catch (NotFoundException $e) {
|
||||
throw new BadRequestException('Invalid argument');
|
||||
}
|
||||
return api_format_data('result', $type, ['result' => "success"]);
|
||||
}
|
||||
|
||||
/// @TODO move to top of file or somewhere better
|
||||
|
||||
+16
-62
@@ -482,47 +482,25 @@ function notification($params)
|
||||
$notify_id = 0;
|
||||
|
||||
if ($show_in_notification_page) {
|
||||
Logger::log("adding notification entry", Logger::DEBUG);
|
||||
$notification = DI::notification()->insert([
|
||||
'name' => $params['source_name'],
|
||||
'url' => $params['source_link'],
|
||||
'photo' => $params['source_photo'],
|
||||
'uid' => $params['uid'],
|
||||
'iid' => $item_id,
|
||||
'parent' => $parent_id,
|
||||
'type' => $params['type'],
|
||||
'verb' => $params['verb'],
|
||||
'otype' => $params['otype'],
|
||||
]);
|
||||
|
||||
/// @TODO One statement is enough
|
||||
$datarray = [];
|
||||
$datarray['name'] = $params['source_name'];
|
||||
$datarray['name_cache'] = strip_tags(BBCode::convert($params['source_name']));
|
||||
$datarray['url'] = $params['source_link'];
|
||||
$datarray['photo'] = $params['source_photo'];
|
||||
$datarray['date'] = DateTimeFormat::utcNow();
|
||||
$datarray['uid'] = $params['uid'];
|
||||
$datarray['link'] = $itemlink;
|
||||
$datarray['iid'] = $item_id;
|
||||
$datarray['parent'] = $parent_id;
|
||||
$datarray['type'] = $params['type'];
|
||||
$datarray['verb'] = $params['verb'];
|
||||
$datarray['otype'] = $params['otype'];
|
||||
$datarray['abort'] = false;
|
||||
$notification->link = DI::baseUrl() . '/notification/view/' . $notification->id;
|
||||
$notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]);
|
||||
|
||||
Hook::callAll('enotify_store', $datarray);
|
||||
DI::notification()->update($notification);
|
||||
|
||||
if ($datarray['abort']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// create notification entry in DB
|
||||
$fields = ['name' => $datarray['name'], 'url' => $datarray['url'],
|
||||
'photo' => $datarray['photo'], 'date' => $datarray['date'], 'uid' => $datarray['uid'],
|
||||
'link' => $datarray['link'], 'iid' => $datarray['iid'], 'parent' => $datarray['parent'],
|
||||
'type' => $datarray['type'], 'verb' => $datarray['verb'], 'otype' => $datarray['otype'],
|
||||
'name_cache' => $datarray["name_cache"]];
|
||||
DBA::insert('notify', $fields);
|
||||
|
||||
$notify_id = DBA::lastInsertId();
|
||||
|
||||
$itemlink = DI::baseUrl().'/notification/view/'.$notify_id;
|
||||
$msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $itemlink]);
|
||||
$msg_cache = format_notification_message($datarray['name_cache'], strip_tags(BBCode::convert($msg)));
|
||||
|
||||
$fields = ['msg' => $msg, 'msg_cache' => $msg_cache];
|
||||
$condition = ['id' => $notify_id, 'uid' => $params['uid']];
|
||||
DBA::update('notify', $fields, $condition);
|
||||
$itemlink = $notification->link;
|
||||
$notify_id = $notification->id;
|
||||
}
|
||||
|
||||
// send email notification if notification preferences permit
|
||||
@@ -732,27 +710,3 @@ function check_item_notification($itemid, $uid, $notification_type) {
|
||||
|
||||
notification($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a notification message with the notification author
|
||||
*
|
||||
* Replace the name with {0} but ensure to make that only once. The {0} is used
|
||||
* later and prints the name in bold.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $message
|
||||
* @return string Formatted message
|
||||
*/
|
||||
function format_notification_message($name, $message) {
|
||||
if ($name != '') {
|
||||
$pos = strpos($message, $name);
|
||||
} else {
|
||||
$pos = false;
|
||||
}
|
||||
|
||||
if ($pos !== false) {
|
||||
$message = substr_replace($message, '{0}', $pos, strlen($name));
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user