2019-07-10 09:50:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Promise from 'bluebird';
|
|
|
|
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import sdk from './';
|
|
|
|
import Modal from './Modal';
|
|
|
|
|
|
|
|
export class TermsNotSignedError extends Error {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a service that may have terms & conditions that
|
2019-07-10 13:17:15 +00:00
|
|
|
* require agreement from the user before the user can use that service.
|
2019-07-10 09:50:10 +00:00
|
|
|
*/
|
|
|
|
export class Service {
|
|
|
|
/**
|
2019-07-10 11:08:26 +00:00
|
|
|
* @param {MatrixClient.SERVICE_TYPES} serviceType The type of service
|
2019-07-10 09:50:10 +00:00
|
|
|
* @param {string} baseUrl The Base URL of the service (ie. before '/_matrix')
|
|
|
|
* @param {string} accessToken The user's access token for the service
|
|
|
|
*/
|
|
|
|
constructor(serviceType, baseUrl, accessToken) {
|
|
|
|
this.serviceType = serviceType;
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.accessToken = accessToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Present a popup to the user prompting them to agree to terms and conditions
|
|
|
|
*
|
2019-07-10 13:20:36 +00:00
|
|
|
* @param {Service[]} services Object with keys 'serviceType', 'baseUrl', 'accessToken'
|
2019-07-10 09:50:10 +00:00
|
|
|
* @returns {Promise} resolves when the user agreed to all necessary terms or rejects
|
|
|
|
* if they cancel.
|
|
|
|
*/
|
|
|
|
export function presentTermsForServices(services) {
|
|
|
|
return startTermsFlow(services, dialogTermsInteractionCallback);
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:07:31 +00:00
|
|
|
/**
|
2019-07-10 13:22:50 +00:00
|
|
|
* Start a flow where the user is presented with terms & conditions for some services
|
|
|
|
*
|
|
|
|
* @param {function} interactionCallback Function called with an array of:
|
|
|
|
* { service: {Service}, terms: {terms response from API} }
|
|
|
|
* Must return a Promise which resolves with a list of URLs of documents agreed to
|
|
|
|
* @returns {Promise} resolves when the user agreed to all necessary terms or rejects
|
|
|
|
* if they cancel.
|
|
|
|
*/
|
2019-07-10 09:50:10 +00:00
|
|
|
export async function startTermsFlow(services, interactionCallback) {
|
|
|
|
const termsPromises = services.map(
|
|
|
|
(s) => MatrixClientPeg.get().getTerms(s.serviceType, s.baseUrl, s.accessToken),
|
|
|
|
);
|
|
|
|
|
2019-07-10 13:25:30 +00:00
|
|
|
/*
|
|
|
|
* a /terms response looks like:
|
|
|
|
* {
|
|
|
|
* "policies": {
|
|
|
|
* "terms_of_service": {
|
|
|
|
* "version": "2.0",
|
|
|
|
* "en": {
|
|
|
|
* "name": "Terms of Service",
|
|
|
|
* "url": "https://example.org/somewhere/terms-2.0-en.html"
|
|
|
|
* },
|
|
|
|
* "fr": {
|
|
|
|
* "name": "Conditions d'utilisation",
|
|
|
|
* "url": "https://example.org/somewhere/terms-2.0-fr.html"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2019-07-10 09:50:10 +00:00
|
|
|
const terms = await Promise.all(termsPromises);
|
2019-07-10 13:27:29 +00:00
|
|
|
const policiesAndServicePairs = terms.map((t, i) => { return { 'service': services[i], 'policies': t.policies }; });
|
2019-07-10 09:50:10 +00:00
|
|
|
|
2019-07-10 14:12:05 +00:00
|
|
|
const agreedUrls = await interactionCallback(policiesAndServicePairs);
|
2019-07-10 09:50:10 +00:00
|
|
|
console.log("User has agreed to URLs", agreedUrls);
|
|
|
|
|
2019-07-10 14:12:05 +00:00
|
|
|
const agreePromises = policiesAndServicePairs.map((policiesAndService) => {
|
2019-07-10 09:50:10 +00:00
|
|
|
// filter the agreed URL list for ones that are actually for this service
|
|
|
|
// (one URL may be used for multiple services)
|
|
|
|
// Not a particularly efficient loop but probably fine given the numbers involved
|
|
|
|
const urlsForService = agreedUrls.filter((url) => {
|
2019-07-10 14:12:05 +00:00
|
|
|
for (const policy of Object.values(policiesAndService.policies)) {
|
|
|
|
for (const lang of Object.keys(policy)) {
|
2019-07-10 09:50:10 +00:00
|
|
|
if (lang === 'version') continue;
|
2019-07-10 14:12:05 +00:00
|
|
|
if (policy[lang].url === url) return true;
|
2019-07-10 09:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (urlsForService.length === 0) return Promise.resolve();
|
|
|
|
|
|
|
|
return MatrixClientPeg.get().agreeToTerms(
|
2019-07-10 14:12:05 +00:00
|
|
|
policiesAndService.service.serviceType,
|
|
|
|
policiesAndService.service.baseUrl,
|
|
|
|
policiesAndService.service.accessToken,
|
2019-07-10 09:50:10 +00:00
|
|
|
urlsForService,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return Promise.all(agreePromises);
|
|
|
|
}
|
|
|
|
|
2019-07-10 14:12:05 +00:00
|
|
|
function dialogTermsInteractionCallback(policiesAndServicePairs) {
|
2019-07-10 09:50:10 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-07-10 14:12:05 +00:00
|
|
|
console.log("Terms that need agreement", policiesAndServicePairs);
|
2019-07-10 09:50:10 +00:00
|
|
|
const TermsDialog = sdk.getComponent("views.dialogs.TermsDialog");
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Terms of Service', '', TermsDialog, {
|
2019-07-10 14:12:05 +00:00
|
|
|
policiesAndServicePairs,
|
2019-07-10 09:50:10 +00:00
|
|
|
onFinished: (done, agreedUrls) => {
|
|
|
|
if (!done) {
|
|
|
|
reject(new TermsNotSignedError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(agreedUrls);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|