Skip to content

Commit

Permalink
Add Notifications API Support
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Aug 24, 2014
1 parent 583fa07 commit 1139849
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ part 'src/common/pull_request.dart';
part 'src/common/contents.dart';
part 'src/common/releases.dart';
part 'src/common/errors.dart';
part 'src/common/gists.dart';
part 'src/common/gists.dart';
part 'src/common/notifications.dart';
15 changes: 15 additions & 0 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,21 @@ class GitHub {
return null;
});
}

/**
* Fetches a Single Notification
*/
Future<Notification> notification(String id) {
return getJSON("/notification/threads/${id}", statusCode: 200, convert: Notification.fromJSON);
}

/**
* Fetches notifications for the current user. If [repository] is specified, it fetches notifications for that repository.
*/
Future<List<Notification>> notifications({RepositorySlug repository, bool all: false, bool participating: false}) {
var url = repository != null ? "/repos/${repository.fullName}/notifications" : "/notifications";
return getJSON(url, params: { "all": all, "participating": participating }, convert: (github, input) => copyOf(input.map((it) => Notification.fromJSON(github, it))));
}

/**
* Handles Get Requests that respond with JSON
Expand Down
61 changes: 61 additions & 0 deletions lib/src/common/notifications.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
part of github.common;

class Notification {
final GitHub github;

String id;
Repository repository;
NotificationSubject subject;
String reason;
bool unread;

@ApiName("updated_at")
DateTime updatedAt;

@ApiName("last_read_at")
DateTime lastReadAt;

Notification(this.github);

static Notification fromJSON(GitHub github, input) {
if (input == null) return null;

return new Notification(github)
..id = input['id']
..repository = Repository.fromJSON(github, input['repository'])
..subject = NotificationSubject.fromJSON(github, input['subject'])
..reason = input['reason']
..unread = input['unread']
..updatedAt = parseDateTime(input['updated_at'])
..lastReadAt = parseDateTime(input['last_read_at']);
}

/**
* Marks this notification as read.
*/
Future<bool> markAsRead({DateTime at}) {
var data = {};

if (at != null) data["last_read_at"] = at.toIso8601String();

return github.request("PUT", "/notifications/thread/${id}", body: JSON.encode(data)).then((response) {
return response.statusCode == 205;
});
}
}

class NotificationSubject {
final GitHub github;

String title;
String type;

NotificationSubject(this.github);

static NotificationSubject fromJSON(GitHub github, input) {
if (input == null) return null;
return new NotificationSubject(github)
..title = input['title']
..type = input['type'];
}
}

0 comments on commit 1139849

Please sign in to comment.