3 var wrap = require('./github-cache-wrapper');
4 var tentacles = require('./tentacles-client');
5 var userTokenSelector = require('./user-token-selector').user;
7 function GitHubMeService(user) {
9 this.accessToken = userTokenSelector(user);
12 GitHubMeService.prototype.getUser = function() {
13 return tentacles.user.getAuthUser({ accessToken: this.accessToken });
16 GitHubMeService.prototype.getEmail = function() {
17 return tentacles.userEmail
19 accessToken: this.accessToken,
20 headers: { Accept: 'application/json' } // Override the default. Remove once default is back to original
22 .then(function(emailHashes) {
23 var primaries = emailHashes
24 .filter(function(hash) {
35 // TODO: evaluate with upcoming changes
36 GitHubMeService.prototype.getOrgs = function() {
37 return tentacles.org.listForAuthUser({
38 accessToken: this.accessToken
42 GitHubMeService.prototype.getOrgMembership = function(org) {
43 return tentacles.orgMember.getMembershipForAuthUser(org, {
44 accessToken: this.accessToken
48 GitHubMeService.prototype.isOrgAdmin = function(org) {
49 return this.getOrgMembership(org)
50 .then(function(membership) {
51 if (!membership) return false;
52 if (membership.state !== 'active') return false;
53 return membership.role === 'admin';
55 .catch(function(err) {
56 if (err.statusCode === 404) return false;
61 GitHubMeService.prototype.isOrgMember = function(org) {
62 return this.getOrgMembership(org)
63 .then(function(membership) {
64 if (!membership) return false;
65 if (membership.state !== 'active') return false;
68 .catch(function(err) {
69 if (err.statusCode === 404) return false;
74 /* TODO: this will be affected by scope issues? */
75 GitHubMeService.prototype.getRepos = function() {
76 return tentacles.repo.listForAuthUser({
77 accessToken: this.accessToken
81 GitHubMeService.prototype.getFollowers = function(options) {
82 return tentacles.userFollower.listForUser(this.user.username, {
83 accessToken: this.accessToken,
84 firstPageOnly: options && options.firstPageOnly
88 // module.exports = GitHubMeService;
89 module.exports = wrap(GitHubMeService, function() {
90 return [this.accessToken || ''];