Dmitrii Filippov | 587086b | 2019-09-03 16:10:06 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2019 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Dmitrii Filippov | 3592f65 | 2020-03-19 16:00:26 +0100 | [diff] [blame] | 17 | import {GrDisplayNameUtils} from '../gr-display-name-utils/gr-display-name-utils.js'; |
| 18 | |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 19 | /** |
| 20 | * @enum {string} |
| 21 | */ |
Dmitrii Filippov | d85c72c | 2020-04-07 13:39:25 +0200 | [diff] [blame] | 22 | export const SUGGESTIONS_PROVIDERS_USERS_TYPES = { |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 23 | REVIEWER: 'reviewers', |
| 24 | CC: 'ccs', |
| 25 | ANY: 'any', |
| 26 | }; |
Dmitrii Filippov | 587086b | 2019-09-03 16:10:06 +0200 | [diff] [blame] | 27 | |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 28 | export class GrReviewerSuggestionsProvider { |
| 29 | static create(restApi, changeNumber, usersType) { |
| 30 | switch (usersType) { |
Dmitrii Filippov | d85c72c | 2020-04-07 13:39:25 +0200 | [diff] [blame] | 31 | case SUGGESTIONS_PROVIDERS_USERS_TYPES.REVIEWER: |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 32 | return new GrReviewerSuggestionsProvider(restApi, changeNumber, |
| 33 | input => restApi.getChangeSuggestedReviewers(changeNumber, |
| 34 | input)); |
Dmitrii Filippov | d85c72c | 2020-04-07 13:39:25 +0200 | [diff] [blame] | 35 | case SUGGESTIONS_PROVIDERS_USERS_TYPES.CC: |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 36 | return new GrReviewerSuggestionsProvider(restApi, changeNumber, |
| 37 | input => restApi.getChangeSuggestedCCs(changeNumber, input)); |
Dmitrii Filippov | d85c72c | 2020-04-07 13:39:25 +0200 | [diff] [blame] | 38 | case SUGGESTIONS_PROVIDERS_USERS_TYPES.ANY: |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 39 | return new GrReviewerSuggestionsProvider(restApi, changeNumber, |
| 40 | input => restApi.getSuggestedAccounts( |
| 41 | `cansee:${changeNumber} ${input}`)); |
| 42 | default: |
| 43 | throw new Error(`Unknown users type: ${usersType}`); |
| 44 | } |
Dmitrii Filippov | 587086b | 2019-09-03 16:10:06 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 47 | constructor(restAPI, changeNumber, apiCall) { |
| 48 | this._changeNumber = changeNumber; |
| 49 | this._apiCall = apiCall; |
| 50 | this._restAPI = restAPI; |
| 51 | } |
Milutin Kristofic | ade40e07 | 2019-09-15 09:23:03 +0200 | [diff] [blame] | 52 | |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 53 | init() { |
| 54 | if (this._initPromise) { |
Dmitrii Filippov | 587086b | 2019-09-03 16:10:06 +0200 | [diff] [blame] | 55 | return this._initPromise; |
| 56 | } |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 57 | const getConfigPromise = this._restAPI.getConfig().then(cfg => { |
| 58 | this._config = cfg; |
| 59 | }); |
| 60 | const getLoggedInPromise = this._restAPI.getLoggedIn().then(loggedIn => { |
| 61 | this._loggedIn = loggedIn; |
| 62 | }); |
| 63 | this._initPromise = Promise.all([getConfigPromise, getLoggedInPromise]) |
| 64 | .then(() => { |
| 65 | this._initialized = true; |
| 66 | }); |
| 67 | return this._initPromise; |
Dmitrii Filippov | 587086b | 2019-09-03 16:10:06 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Dmitrii Filippov | 44f47c0 | 2020-03-24 13:53:22 +0100 | [diff] [blame] | 70 | getSuggestions(input) { |
| 71 | if (!this._initialized || !this._loggedIn) { |
| 72 | return Promise.resolve([]); |
| 73 | } |
| 74 | |
| 75 | return this._apiCall(input) |
| 76 | .then(reviewers => (reviewers || [])); |
| 77 | } |
| 78 | |
| 79 | makeSuggestionItem(suggestion) { |
| 80 | if (suggestion.account) { |
| 81 | // Reviewer is an account suggestion from getChangeSuggestedReviewers. |
| 82 | return { |
| 83 | name: GrDisplayNameUtils.getAccountDisplayName(this._config, |
| 84 | suggestion.account), |
| 85 | value: suggestion, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | if (suggestion.group) { |
| 90 | // Reviewer is a group suggestion from getChangeSuggestedReviewers. |
| 91 | return { |
| 92 | name: GrDisplayNameUtils.getGroupDisplayName(suggestion.group), |
| 93 | value: suggestion, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | if (suggestion._account_id) { |
| 98 | // Reviewer is an account suggestion from getSuggestedAccounts. |
| 99 | return { |
| 100 | name: GrDisplayNameUtils.getAccountDisplayName(this._config, |
| 101 | suggestion), |
| 102 | value: {account: suggestion, count: 1}, |
| 103 | }; |
| 104 | } |
| 105 | } |
| 106 | } |