|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @copyright 2016 Matt Smith (Development Seed) |
| 4 | + * @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}. |
| 5 | + * Github.js is freely distributable. |
| 6 | + */ |
| 7 | + |
| 8 | +import Requestable from './Requestable'; |
| 9 | +import debug from 'debug'; |
| 10 | +const log = debug('github:watching'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Watching a Repository registers the user to receive notifications on new discussions, as well as events in the user's |
| 14 | + * activity feed. |
| 15 | + */ |
| 16 | +export default class Watching extends Requestable { |
| 17 | + /** |
| 18 | + * Watching wrapper |
| 19 | + * @param {Requestable.auth} [auth] - information required to authenticate to Github |
| 20 | + * @param {string} [apiBase=https://api.github.com] - the base Github API URL |
| 21 | + */ |
| 22 | + constructor(auth, apiBase) { |
| 23 | + super(auth, apiBase); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Get Watchers for repo |
| 28 | + * @see https://developer.github.com/v3/activity/watching/#list-watchers |
| 29 | + * @param {string} owner - username or organization |
| 30 | + * @param {string} repo - repo name |
| 31 | + * @param {Requestable.callback} [cb] - list of watchers |
| 32 | + * @return {Promise} - the promise for the http request |
| 33 | + */ |
| 34 | + listWatchers(owner, repo, cb) { |
| 35 | + log(`Fetching watchers for ${owner}/${repo}`); |
| 36 | + return this._requestAllPages(`/repos/${owner}/${repo}/subscribers`, undefined, cb); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Subscribe to repo |
| 41 | + * @see https://developer.github.com/v3/activity/watching/#set-a-repository-subscription |
| 42 | + * @param {string} owner - username or organization |
| 43 | + * @param {string} repo - repo name |
| 44 | + * @param {Requestable.callback} [cb] - subscription status |
| 45 | + * @return {Promise} - the promise for the http request |
| 46 | + */ |
| 47 | + subscribe(owner, repo, cb) { |
| 48 | + log(`Subscribing to ${owner}/${repo}`); |
| 49 | + return this._setSubscription(owner, repo, true, cb); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Ignore a repo |
| 54 | + * @see https://developer.github.com/v3/activity/watching/#set-a-repository-subscription |
| 55 | + * @param {string} owner - username or organization |
| 56 | + * @param {string} repo - repo name |
| 57 | + * @param {Requestable.callback} [cb] - subscription status |
| 58 | + * @return {Promise} - the promise for the http request |
| 59 | + */ |
| 60 | + ignore(owner, repo, cb) { |
| 61 | + log(`Ignoring ${owner}/${repo}`); |
| 62 | + return this._setSubscription(owner, repo, false, cb); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Remove all subscription settings for repo |
| 67 | + * @see https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription |
| 68 | + * @param {string} owner - username or organization |
| 69 | + * @param {string} repo - repo name |
| 70 | + * @param {Requestable.callback} [cb] - subscription status |
| 71 | + * @return {Promise} - the promise for the http request |
| 72 | + */ |
| 73 | + removeSubscription(owner, repo, cb) { |
| 74 | + log(`Removing subscription settings for ${owner}/${repo}`); |
| 75 | + return this._request204or404(`/repos/${owner}/${repo}/subscription`, undefined, cb, 'DELETE'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * General subscription method |
| 80 | + * @private |
| 81 | + * @see https://developer.github.com/v3/activity/watching/#set-a-repository-subscription |
| 82 | + * @param {string} owner - username or organization |
| 83 | + * @param {string} repo - repo name |
| 84 | + * @param {boolean} subscribed - subscribed or ignored |
| 85 | + * @param {Requestable.callback} [cb] - subscription status |
| 86 | + * @return {Promise} - the promise for the http request |
| 87 | + */ |
| 88 | + _setSubscription(owner, repo, subscribed, cb) { |
| 89 | + log(`Subscribing to ${owner}/${repo}`); |
| 90 | + return this._request('PUT', `/repos/${owner}/${repo}/subscription`, { |
| 91 | + subscribed, |
| 92 | + ignored: !subscribed |
| 93 | + }, cb); |
| 94 | + } |
| 95 | +} |
0 commit comments