{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-app-android/pulls/comments/28151668","id":28151668,"diff_hunk":"@@ -0,0 +1,159 @@\n+package org.edx.mobile.module.notification;\n+\n+import com.parse.ParseException;\n+import com.parse.ParsePush;\n+import com.parse.SaveCallback;\n+\n+import org.edx.mobile.base.MainApplication;\n+import org.edx.mobile.logger.Logger;\n+import org.edx.mobile.model.api.CourseEntry;\n+import org.edx.mobile.model.api.EnrolledCoursesResponse;\n+import org.edx.mobile.module.prefs.UserBasedPrefManager;\n+\n+import java.util.ArrayList;\n+import java.util.List;\n+\n+/**\n+ * Created by hanning on 4/9/15.\n+ */\n+public class ParseNotificationDelegate implements NotificationDelegate{\n+    private static final Logger logger = new Logger(ParseNotificationDelegate.class.getName());\n+\n+    ParseNotificationDelegate() {\n+    }\n+\n+    /**\n+     * if a user logs out, they should not receive the push notification\n+     */\n+    public void unsubscribeAll() {\n+        new ParseSyncTask(MainApplication.instance().getApplicationContext()) {\n+            @Override\n+            public void onFinish(Void result) {\n+                if (subscribedChannels != null) {\n+                    for (String channel : subscribedChannels) {\n+                        UserNotificationManager.instance.subscribeAndUnsubscribeToParse(channel, false);\n+                    }\n+                }\n+            }\n+        }.execute();\n+    }\n+\n+    /**\n+     * if user login,  he should subscribe channels based on local pref setting.\n+     */\n+    public void resubscribeAll() {\n+        UserBasedPrefManager prefManager = UserBasedPrefManager.getInstance(UserBasedPrefManager.UserPrefType.NOTIFICATION);\n+        NotificationPreference preference = prefManager.getNotificationPreference();\n+        for (EdxLocalParseChannel pc : preference) {\n+            if (pc.isSubscribed())\n+                subscribeAndUnsubscribeToNotificationServer(pc.getChannelId(), true);\n+        }\n+    }\n+\n+    //check if local subscribed is not in parse server\n+    //then try to subscribe it\n+    public void syncWithNotificationServer() {\n+        new ParseSyncTask(MainApplication.instance().getApplicationContext()) {\n+            @Override\n+            public void onFinish(Void result) {\n+                UserBasedPrefManager prefManager = UserBasedPrefManager.getInstance(UserBasedPrefManager.UserPrefType.NOTIFICATION);\n+                NotificationPreference preference = prefManager.getNotificationPreference();\n+                for (EdxLocalParseChannel pc : preference) {\n+                    if (pc.isSubscribed() && subscribedChannels != null &&\n+                            !subscribedChannels.contains(pc.getChannelId()))\n+                        subscribeAndUnsubscribeToNotificationServer(pc.getChannelId(), true);\n+                    if (!pc.isSubscribed() && subscribedChannels != null &&\n+                            subscribedChannels.contains(pc.getChannelId()))\n+                        subscribeAndUnsubscribeToNotificationServer(pc.getChannelId(), false);\n+                }\n+            }\n+        }.execute();\n+\n+    }\n+\n+    /**\n+     * sync with current course enrollment.\n+     * based on current course enrollment, we subscribe/unsubscribe to the Parse\n+     * and update the local preference.\n+     *\n+     * @param responses\n+     */\n+    public void checkCourseEnrollment(List<EnrolledCoursesResponse> responses) {\n+        List<CourseEntry> activeList = new ArrayList<>();\n+        for (EnrolledCoursesResponse response : responses) {\n+            if (response.isCourse() && response.isIs_active()) {\n+                activeList.add(response.getCourse());\n+            }\n+        }\n+        if (activeList.isEmpty())\n+            return;\n+\n+        UserBasedPrefManager prefManager = UserBasedPrefManager.getInstance(UserBasedPrefManager.UserPrefType.NOTIFICATION);\n+        NotificationPreference preference = prefManager.getNotificationPreference();\n+        List<CourseEntry> newCourseList = preference.filterForNewCourses(activeList);\n+        for (CourseEntry courseEntry : newCourseList) {\n+            EdxLocalParseChannel pc = new EdxLocalParseChannel(courseEntry.getId(), true);\n+            subscribeAndUnsubscribeToNotificationServer(pc.getChannelId(), true);\n+            preference.add(pc);\n+        }\n+\n+        List<EdxLocalParseChannel> inactivCourseList = preference.filterForInactiveCourses(activeList);\n+        for (EdxLocalParseChannel pc : inactivCourseList) {","path":"VideoLocker/src/main/java/org/edx/mobile/module/notification/ParseNotificationDelegate.java","position":101,"original_position":101,"commit_id":"2374b55d02177cbabc10494ddce7b9bd43f8552b","original_commit_id":"2374b55d02177cbabc10494ddce7b9bd43f8552b","user":{"login":"BenjiLee","id":7101723,"avatar_url":"https://avatars.githubusercontent.com/u/7101723?v=3","gravatar_id":"","url":"https://api.github.com/users/BenjiLee","html_url":"https://github.com/BenjiLee","followers_url":"https://api.github.com/users/BenjiLee/followers","following_url":"https://api.github.com/users/BenjiLee/following{/other_user}","gists_url":"https://api.github.com/users/BenjiLee/gists{/gist_id}","starred_url":"https://api.github.com/users/BenjiLee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BenjiLee/subscriptions","organizations_url":"https://api.github.com/users/BenjiLee/orgs","repos_url":"https://api.github.com/users/BenjiLee/repos","events_url":"https://api.github.com/users/BenjiLee/events{/privacy}","received_events_url":"https://api.github.com/users/BenjiLee/received_events","type":"User","site_admin":false},"body":"`inactivCourseList` --> `inactiveCourseList`","created_at":"2015-04-10T15:02:53Z","updated_at":"2015-04-10T15:02:53Z","html_url":"https://github.com/edx/edx-app-android/pull/210#discussion_r28151668","pull_request_url":"https://api.github.com/repos/edx/edx-app-android/pulls/210","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/comments/28151668"},"html":{"href":"https://github.com/edx/edx-app-android/pull/210#discussion_r28151668"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/210"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-app-android/pulls/210","id":32989402,"html_url":"https://github.com/edx/edx-app-android/pull/210","diff_url":"https://github.com/edx/edx-app-android/pull/210.diff","patch_url":"https://github.com/edx/edx-app-android/pull/210.patch","issue_url":"https://api.github.com/repos/edx/edx-app-android/issues/210","number":210,"state":"open","locked":false,"title":"Notification- setup and subscribe/unsubscribe","user":{"login":"hanningni","id":11091163,"avatar_url":"https://avatars.githubusercontent.com/u/11091163?v=3","gravatar_id":"","url":"https://api.github.com/users/hanningni","html_url":"https://github.com/hanningni","followers_url":"https://api.github.com/users/hanningni/followers","following_url":"https://api.github.com/users/hanningni/following{/other_user}","gists_url":"https://api.github.com/users/hanningni/gists{/gist_id}","starred_url":"https://api.github.com/users/hanningni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hanningni/subscriptions","organizations_url":"https://api.github.com/users/hanningni/orgs","repos_url":"https://api.github.com/users/hanningni/repos","events_url":"https://api.github.com/users/hanningni/events{/privacy}","received_events_url":"https://api.github.com/users/hanningni/received_events","type":"User","site_admin":false},"body":"@aleffert  @BenjiLee  \r\nplease review. \r\n\r\nhttps://openedx.atlassian.net/browse/MA-577\r\nMobile Notifications: App-side Backend - Setup and Subscribe/Unsubscribe\r\n","created_at":"2015-04-09T19:46:31Z","updated_at":"2015-04-10T15:02:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"74c8abd3f05b291062580504ca08610f5b233c42","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-app-android/pulls/210/commits","review_comments_url":"https://api.github.com/repos/edx/edx-app-android/pulls/210/comments","review_comment_url":"https://api.github.com/repos/edx/edx-app-android/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-app-android/issues/210/comments","statuses_url":"https://api.github.com/repos/edx/edx-app-android/statuses/2374b55d02177cbabc10494ddce7b9bd43f8552b","head":{"label":"edx:hanning/notification-init","ref":"hanning/notification-init","sha":"2374b55d02177cbabc10494ddce7b9bd43f8552b","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":29251742,"name":"edx-app-android","full_name":"edx/edx-app-android","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-app-android","description":"","fork":false,"url":"https://api.github.com/repos/edx/edx-app-android","forks_url":"https://api.github.com/repos/edx/edx-app-android/forks","keys_url":"https://api.github.com/repos/edx/edx-app-android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-app-android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-app-android/teams","hooks_url":"https://api.github.com/repos/edx/edx-app-android/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-app-android/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-app-android/events","assignees_url":"https://api.github.com/repos/edx/edx-app-android/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-app-android/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-app-android/tags","blobs_url":"https://api.github.com/repos/edx/edx-app-android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-app-android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-app-android/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-app-android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-app-android/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-app-android/languages","stargazers_url":"https://api.github.com/repos/edx/edx-app-android/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-app-android/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-app-android/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-app-android/subscription","commits_url":"https://api.github.com/repos/edx/edx-app-android/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-app-android/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-app-android/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-app-android/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-app-android/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-app-android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-app-android/merges","archive_url":"https://api.github.com/repos/edx/edx-app-android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-app-android/downloads","issues_url":"https://api.github.com/repos/edx/edx-app-android/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-app-android/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-app-android/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-app-android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-app-android/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-app-android/releases{/id}","created_at":"2015-01-14T15:49:47Z","updated_at":"2015-04-09T20:30:41Z","pushed_at":"2015-04-10T14:14:45Z","git_url":"git://github.com/edx/edx-app-android.git","ssh_url":"git@github.com:edx/edx-app-android.git","clone_url":"https://github.com/edx/edx-app-android.git","svn_url":"https://github.com/edx/edx-app-android","homepage":null,"size":33484,"stargazers_count":14,"watchers_count":14,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":20,"mirror_url":null,"open_issues_count":3,"forks":20,"open_issues":3,"watchers":14,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"2da3de6701b5a04364e43a59ca3ab4d4631b9816","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":29251742,"name":"edx-app-android","full_name":"edx/edx-app-android","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-app-android","description":"","fork":false,"url":"https://api.github.com/repos/edx/edx-app-android","forks_url":"https://api.github.com/repos/edx/edx-app-android/forks","keys_url":"https://api.github.com/repos/edx/edx-app-android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-app-android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-app-android/teams","hooks_url":"https://api.github.com/repos/edx/edx-app-android/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-app-android/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-app-android/events","assignees_url":"https://api.github.com/repos/edx/edx-app-android/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-app-android/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-app-android/tags","blobs_url":"https://api.github.com/repos/edx/edx-app-android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-app-android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-app-android/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-app-android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-app-android/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-app-android/languages","stargazers_url":"https://api.github.com/repos/edx/edx-app-android/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-app-android/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-app-android/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-app-android/subscription","commits_url":"https://api.github.com/repos/edx/edx-app-android/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-app-android/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-app-android/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-app-android/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-app-android/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-app-android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-app-android/merges","archive_url":"https://api.github.com/repos/edx/edx-app-android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-app-android/downloads","issues_url":"https://api.github.com/repos/edx/edx-app-android/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-app-android/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-app-android/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-app-android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-app-android/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-app-android/releases{/id}","created_at":"2015-01-14T15:49:47Z","updated_at":"2015-04-09T20:30:41Z","pushed_at":"2015-04-10T14:14:45Z","git_url":"git://github.com/edx/edx-app-android.git","ssh_url":"git@github.com:edx/edx-app-android.git","clone_url":"https://github.com/edx/edx-app-android.git","svn_url":"https://github.com/edx/edx-app-android","homepage":null,"size":33484,"stargazers_count":14,"watchers_count":14,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":20,"mirror_url":null,"open_issues_count":3,"forks":20,"open_issues":3,"watchers":14,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/210"},"html":{"href":"https://github.com/edx/edx-app-android/pull/210"},"issue":{"href":"https://api.github.com/repos/edx/edx-app-android/issues/210"},"comments":{"href":"https://api.github.com/repos/edx/edx-app-android/issues/210/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/210/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-app-android/pulls/210/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-app-android/statuses/2374b55d02177cbabc10494ddce7b9bd43f8552b"}}}}