MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / amd / src / notification_repository.js
CommitLineData
a0e358a6
RW
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * Retrieves notifications from the server.
18 *
9e8a29c9 19 * @module core_message/notification_repository
a0e358a6
RW
20 * @class notification_repository
21 * @package message
22 * @copyright 2015 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since 3.2
25 */
26define(['core/ajax', 'core/notification'], function(ajax, notification) {
195a683b
RW
27 /**
28 * Retrieve a list of notifications from the server.
29 *
30 * @param {object} args The request arguments:
31 * @return {jQuery promise}
32 */
a0e358a6
RW
33 var query = function(args) {
34 if (typeof args.limit === 'undefined') {
35 args.limit = 20;
36 }
37
38 if (typeof args.offset === 'undefined') {
39 args.offset = 0;
40 }
41
42 var request = {
ada7695d 43 methodname: 'core_message_get_popup_notifications',
a0e358a6
RW
44 args: args
45 };
46
47 var promise = ajax.call([request])[0];
48
49 promise.fail(notification.exception);
50
51 return promise;
52 };
53
195a683b
RW
54 /**
55 * Get the number of unread notifications from the server.
56 *
57 * @param {object} args The request arguments:
58 * @return {jQuery promise}
59 */
a0e358a6
RW
60 var countUnread = function(args) {
61 var request = {
ada7695d 62 methodname: 'core_message_get_unread_popup_notification_count',
a0e358a6
RW
63 args: args
64 };
65
66 var promise = ajax.call([request])[0];
67
68 promise.fail(notification.exception);
69
70 return promise;
71 };
72
195a683b
RW
73 /**
74 * Mark all notifications for the given user as read.
75 *
76 * @param {object} args The request arguments:
77 * @return {jQuery promise}
78 */
a0e358a6
RW
79 var markAllAsRead = function(args) {
80 var request = {
81 methodname: 'core_message_mark_all_notifications_as_read',
82 args: args
83 };
84
85 var promise = ajax.call([request])[0];
86
87 promise.fail(notification.exception);
88
89 return promise;
90 };
91
92 return {
93 query: query,
94 countUnread: countUnread,
95 markAllAsRead: markAllAsRead,
96 };
97});