Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification API #1270

Closed
caercam opened this issue Apr 22, 2015 · 4 comments
Closed

Notification API #1270

caercam opened this issue Apr 22, 2015 · 4 comments
Assignees
Labels

Comments

@caercam
Copy link
Contributor

caercam commented Apr 22, 2015

That's related to the AJAX notifications handling mentioned on the official board, I plan to build a PHP Class to handle the mechanic behind notifications. Will be done for version 1.1 (along with AJAX).

@caercam caercam changed the title Build a notification API Notification API Apr 22, 2015
@caercam
Copy link
Contributor Author

caercam commented Apr 23, 2015

@Studio384 Almost done! To which branch shall I pull-request?

@Studio384
Copy link
Member

lunadev please, that's the branch where the next major update is being developed from right now.

Can you write some documentation down? Perhaps in this ticket?

@caercam
Copy link
Contributor Author

caercam commented Apr 23, 2015

The code is quite commented already, but here's the basics.

Introduced two things: LunaNotification class to manipulate individual notifications, and a include/notifications.php file containing functions standalone function to manipulate notifications in general.

Basic Documentation

LunaNotification Class
  • get_instance($id): static method to retrieve a notification by its ID
  • create(): private method to insert the current notification object into the database
  • remove(): private method to remove the current notification object from the database
  • add($user, $message, $link, $icon): public, static method to create a new notification
  • delete($id): public, static method to remove a notification
include/notifications.php general functions
  • new_notification($user, $message, $link, $icon): create a new notification. Shortcut for LunaNotification::add().
  • delete_notification($id): remove a notification. Shortcut for LunaNotification::delete().
  • get_notification($notification): retrieve a notification. Shortcut LunaNotification::get_instance()`.
  • delete_user_notifications($user_id, $viewed): delete user's notifications. Remove all notifications or viewed/unviewed ones if specified.
  • set_user_notifications_viewed($user_id): mark user's notifications as viewed.
  • get_user_notifications($user_id, $viewed, $count, $limit): retrieve all user's notifications, with a bunch of options (viewed/unviewed, limit number, count only).
  • get_user_notifications_total($user_id, $viewed): Shortcut for get_user_notifications() to get only the number of notifications.
  • get_user_viewed_notifications($user_id): Shortcut for get_user_notifications() to get only viewed notifications.
  • get_user_unviewed_notifications($user_id): Shortcut for get_user_notifications() to get only unviewed notifications.
  • has_notifications($user_id = null, $viewed): Check if user has any notification. Shortcut for get_user_notifications_total() with boolean casted return value.
  • has_viewed_notifications($user_id): Check if user has any viewed notification. Shortcut for has_notifications().
  • has_unviewed_notifications($user_id): Check if user has any unviewed notification. Shortcut for has_notifications().

Code examples

Create a notification, object way
// Init a notification
$notification = new LunaNotification(
    array(
        'user_id' => 2,
        'message' => 'Hello World!',
        'link'    => '#',
        'icon'    => 'fa-plane',
    )
);
// Save it
$notification->create();
// Remove it
$notification->remove();
Accessing notification:
// Fetch an existing notification
get_notification($id);
// Actually an alias of
LunaNotification::get_instance($id);
Get user's unviewed notifications, if any:
if (has_unviewed_notifications(2)) {
    $notifications = get_user_unviewed_notifications(2);
}

It still lacks some methods/shortcuts which I'll implement later when updating the AJAX part, like the possibility to mark a specific notification as viewed or not viewed, $notification->get() and $notification->set() methods, possibly a way to update a notification's message of icon, etc etc :)

@Studio384
Copy link
Member

Thanks for the amazing work. :) I mainly asked for some documentation to put it in the Developers section of the general Luna documentation. Easier for a general overview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants