Skip to content

Commit

Permalink
Remove use of checkCapability function in favour of more explicit che…
Browse files Browse the repository at this point in the history
…cks.
  • Loading branch information
rtibbles committed Dec 19, 2024
1 parent 2862153 commit 3a3a72b
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@
import urls from 'kolibri/urls';
import logger from 'kolibri-logging';
import { ref, watch } from 'vue';
import pluginData from 'kolibri-plugin-data';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import UiAlert from 'kolibri-design-system/lib/keen/UiAlert';
import { availableLanguages, currentLanguage, sortLanguages } from 'kolibri/utils/i18n';
import BottomAppBar from 'kolibri/components/BottomAppBar';
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
import { checkCapability } from 'kolibri/utils/appCapabilities';
import useUser from 'kolibri/composables/useUser';
import useSnackbar from 'kolibri/composables/useSnackbar';
import commonDeviceStrings from '../commonDeviceStrings';
Expand Down Expand Up @@ -603,7 +603,7 @@
}
},
canCheckMeteredConnection() {
return checkCapability('check_is_metered');
return pluginData.canCheckMeteredConnection;
},
showDisabledAlert() {
return this.isRemoteContent || !this.canRestart;
Expand Down
2 changes: 2 additions & 0 deletions kolibri/plugins/device/kolibri_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from kolibri.core.auth.constants.user_kinds import SUPERUSER
from kolibri.core.device.hooks import CheckIsMeteredHook
from kolibri.core.hooks import NavigationHook
from kolibri.core.hooks import RoleBasedRedirectHook
from kolibri.core.webpack.hooks import WebpackBundleHook
Expand Down Expand Up @@ -28,6 +29,7 @@ def plugin_data(self):
return {
"isRemoteContent": OPTIONS["Deployment"]["REMOTE_CONTENT"],
"canRestart": bool(OPTIONS["Deployment"]["RESTART_HOOKS"]),
"canCheckMeteredConnection": CheckIsMeteredHook.is_registered,
"deprecationWarnings": {
"ie11": any_ie11_users(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@

<script>
import pluginData from 'kolibri-plugin-data';
import urls from 'kolibri/urls';
import client from 'kolibri/client';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import appCapabilities, { checkCapability } from 'kolibri/utils/appCapabilities';
import logger from 'kolibri-logging';
import useUser from 'kolibri/composables/useUser';
import checkMeteredConnection from 'kolibri-common/utils/checkMeteredConnection';
const logging = logger.getLogger(__filename);
Expand Down Expand Up @@ -85,10 +86,10 @@
},
},
mounted() {
if (checkCapability('check_is_metered')) {
if (pluginData.canCheckMeteredConnection) {
this.loading = true;
appCapabilities.checkIsMetered().then(isMetered => {
checkMeteredConnection().then(isMetered => {
this.activeConnectionIsMetered = isMetered;
// Fetch the DeviceSettings#extra_settings value
Expand Down
11 changes: 6 additions & 5 deletions kolibri/plugins/learn/assets/src/views/LibraryPage/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@
import { get, set } from '@vueuse/core';
import { onMounted, getCurrentInstance, ref, watch } from 'vue';
import pluginData from 'kolibri-plugin-data';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
import useUser from 'kolibri/composables/useUser';
import samePageCheckGenerator from 'kolibri-common/utils/samePageCheckGenerator';
import ContentNodeResource from 'kolibri-common/apiResources/ContentNodeResource';
import { mapState } from 'vuex';
import MeteredConnectionNotificationModal from 'kolibri-common/components/MeteredConnectionNotificationModal.vue';
import appCapabilities, { checkCapability } from 'kolibri/utils/appCapabilities';
import checkMeteredConnection from 'kolibri-common/utils/checkMeteredConnection';
import LearningActivityChip from 'kolibri-common/components/ResourceDisplayAndSearch/LearningActivityChip.vue';
import { searchKeys } from 'kolibri-common/composables/useBaseSearch';
import SidePanelModal from 'kolibri-common/components/SidePanelModal';
Expand All @@ -209,6 +209,7 @@
import SearchResultsGrid from '../SearchResultsGrid';
import LearnAppBarPage from '../LearnAppBarPage';
import PostSetupModalGroup from '../../../../../device/assets/src/views/PostSetupModalGroup.vue';
import MeteredConnectionNotificationModal from './MeteredConnectionNotificationModal';
import ResumableContentGrid from './ResumableContentGrid';
import OtherLibraries from './OtherLibraries';
Expand Down Expand Up @@ -453,7 +454,7 @@
if (!validUser) {
return false;
}
if (!checkCapability('check_is_metered')) {
if (!pluginData.canCheckMeteredConnection) {
return true;
}
if (this.allowDownloadOnMeteredConnection) {
Expand Down Expand Up @@ -531,9 +532,9 @@
!this.deviceId &&
this.isUserLoggedIn &&
!this.allowDownloadOnMeteredConnection &&
checkCapability('check_is_metered')
pluginData.canCheckMeteredConnection
) {
appCapabilities.checkIsMetered().then(isMetered => {
checkMeteredConnection().then(isMetered => {
this.usingMeteredConnection = isMetered;
});
}
Expand Down
2 changes: 2 additions & 0 deletions kolibri/plugins/learn/kolibri_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from kolibri.core.auth.constants.user_kinds import ANONYMOUS
from kolibri.core.auth.constants.user_kinds import LEARNER
from kolibri.core.content.hooks import ContentNodeDisplayHook
from kolibri.core.device.hooks import CheckIsMeteredHook
from kolibri.core.device.utils import allow_learner_unassigned_resource_access
from kolibri.core.device.utils import get_device_setting
from kolibri.core.device.utils import is_landing_page
Expand Down Expand Up @@ -63,6 +64,7 @@ def plugin_data(self):
"allowDownloadOnMeteredConnection": get_device_setting(
"allow_download_on_metered_connection"
),
"canCheckMeteredConnection": CheckIsMeteredHook.is_registered,
"allowGuestAccess": get_device_setting("allow_guest_access"),
"allowLearnerDownloads": get_device_setting(
"allow_learner_download_resources"
Expand Down
16 changes: 10 additions & 6 deletions kolibri/plugins/setup_wizard/assets/src/machines/wizardMachine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import uniq from 'lodash/uniq';
import { checkCapability } from 'kolibri/utils/appCapabilities';
import { get } from '@vueuse/core';
import pluginData from 'kolibri-plugin-data';
import { Presets } from 'kolibri/constants';
import useUser from 'kolibri/composables/useUser';
import { DeviceTypePresets, FacilityTypePresets, LodTypePresets, UsePresets } from '../constants';

/**
Expand All @@ -23,8 +25,8 @@ import { DeviceTypePresets, FacilityTypePresets, LodTypePresets, UsePresets } fr
* OR
* Know that we will need to directly copy and paste them into the visualizer
*
* For now, this means copying the imports from `../constants` and writing a
* function in place of `checkCapability`.
* For now, this means copying the imports from `../constants` and writing an
* object in place of `pluginData`.
*/

/**
Expand All @@ -46,8 +48,10 @@ import { DeviceTypePresets, FacilityTypePresets, LodTypePresets, UsePresets } fr
/* eslint-disable-next-line */
import { assign, createMachine } from 'xstate';

// NOTE: Uncomment the following function if you're using the visualizer
// const checkCapability = capabilityToCheck => ["get_os_user"].includes(capabilityToCheck);
// NOTE: Uncomment the following object if you're using the visualizer
// const pluginData = { canGetOsUser: true };

const { isAppContext } = useUser();

const initialContext = {
onMyOwnOrGroup: null,
Expand Down Expand Up @@ -553,7 +557,7 @@ export const wizardMachine = createMachine(
isGroupSetup: context => {
return context.onMyOwnOrGroup === UsePresets.GROUP;
},
canGetOsUser: () => checkCapability('get_os_user'),
canGetOsUser: () => get(isAppContext) && pluginData.canGetOSUser,
isNewFacility: context => {
return context.facilityNewOrImport === FacilityTypePresets.NEW;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
import { interpret } from 'xstate';
import { mapState } from 'vuex';
import pluginData from 'kolibri-plugin-data';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import useUser from 'kolibri/composables/useUser';
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
import { checkCapability } from 'kolibri/utils/appCapabilities';
import Lockr from 'lockr';
import { wizardMachine } from '../machines/wizardMachine';
import LoadingPage from './submission-states/LoadingPage';
Expand All @@ -47,7 +48,8 @@
mixins: [commonCoreStrings],
setup() {
const { windowIsLarge } = useKResponsiveWindow();
return { windowIsLarge };
const { isAppContext } = useUser();
return { isAppContext, windowIsLarge };
},
data() {
return {
Expand Down Expand Up @@ -112,7 +114,10 @@
synchronizeRouteAndMachine(savedState);
} else {
// Or set the app context state on the machine and proceed to the first state
this.service.send({ type: 'CONTINUE', value: checkCapability('get_os_user') });
this.service.send({
type: 'CONTINUE',
value: this.isAppContext && pluginData.canGetOSUser,
});
}
this.service.start(savedState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import omitBy from 'lodash/omitBy';
import get from 'lodash/get';
import pluginData from 'kolibri-plugin-data';
import AppError from 'kolibri/components/error/AppError';
import useUser from 'kolibri/composables/useUser';
import { currentLanguage } from 'kolibri/utils/i18n';
import { checkCapability } from 'kolibri/utils/appCapabilities';
import redirectBrowser from 'kolibri/utils/redirectBrowser';
import KolibriLoadingSnippet from 'kolibri-common/components/KolibriLoadingSnippet';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
Expand All @@ -55,6 +56,10 @@
components: { AppError, KolibriLoadingSnippet },
inject: ['wizardService'],
mixins: [commonCoreStrings],
setup() {
const { isAppContext } = useUser();
return { isAppContext };
},
computed: {
coreError() {
if (this.$store) {
Expand Down Expand Up @@ -111,7 +116,7 @@
// Learner cannot edit a password they cannot set
this.learnerCanLoginWithNoPassword ||
// OS on my own users don't use password to sign in
(this.isOnMyOwnSetup && checkCapability('get_os_user'))
(this.isOnMyOwnSetup && this.isAppContext && pluginData.canGetOSUser)
) {
return false; // Learner cannot edit a password they cannot set
} else {
Expand All @@ -123,7 +128,7 @@
let superuser = null;
// We need the superuser information unless the superuser will be created at login,
// based on the os user - this is only the case for on my own setup.
if (!(this.isOnMyOwnSetup && checkCapability('get_os_user'))) {
if (!(this.isOnMyOwnSetup && this.isAppContext && pluginData.canGetOSUser)) {
// Here we see if we've set a firstImportedLodUser -- if they exist, they must be the
// superuser as they were the first imported user.
if (this.wizardContext('firstImportedLodUser')) {
Expand Down
5 changes: 4 additions & 1 deletion kolibri/plugins/setup_wizard/kolibri_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from kolibri.core.device.hooks import GetOSUserHook
from kolibri.core.device.hooks import SetupHook
from kolibri.core.webpack import hooks as webpack_hooks
from kolibri.plugins import KolibriPluginBase
Expand All @@ -20,7 +21,9 @@ def name(self, lang):

@property
def plugin_data(self):
return {}
return {
"canGetOSUser": GetOSUserHook.is_registered,
}


@register_hook
Expand Down
12 changes: 12 additions & 0 deletions packages/kolibri-common/utils/checkMeteredConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import client from 'kolibri/client';
import urls from 'kolibri/urls';

/**
* @returns Promise<Boolean>
* Returns a function that returns a Promise that resolves to something responding to
* `data.value` whether it succeeds or fails.
*/
export default function checkIsMetered() {
const urlFunction = urls['kolibri:core:check_metered_connection'];
return client({ url: urlFunction() }).then(response => response.data);
}
28 changes: 0 additions & 28 deletions packages/kolibri/utils/appCapabilities.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import client from 'kolibri/client';
import logger from 'kolibri-logging';
import urls from 'kolibri/urls';
import useUser from 'kolibri/composables/useUser';
import { get } from '@vueuse/core';
import plugin_data from 'kolibri-plugin-data';

const logging = logger.getLogger(__filename);

const appCapabilities = plugin_data.appCapabilities || {};

// Check that we are in an appcontext, if not disable all capabilities
// this means that consumers of this API can rely solely on the existence
// check of methods in this API to know if they can call these or not.
export const checkCapability = key => {
const { isAppContext } = useUser();
return get(isAppContext) && appCapabilities[key];
};

// Use a janky getter to return a method here to only expose functions
// that are available so that we have a single API for both existence
// checks and exposing the functions.
Expand All @@ -27,22 +14,7 @@ export default {
* Returns a function that returns a Promise that resolves to something responding to
* `data.value` whether it succeeds or fails.
*/
checkIsMetered() {
if (!checkCapability('check_is_metered')) {
return Promise.resolve(null);
}

const urlFunction = urls['kolibri:core:check_metered_connection'];
if (!urlFunction || !checkCapability('check_is_metered')) {
logging.warn('Checking if the device is metered is not supported on this platform');
return Promise.resolve(null);
}
return client({ url: urlFunction(), method: 'GET' }).then(response => response.data);
},
get shareFile() {
if (!checkCapability('share_file')) {
return; // eslint-disable-line getter-return
}
// Deliberately using an options object here for the function signature,
// rather than positional arguments, so that we can evolve the API but
// maintain backwards compatibility.
Expand Down

0 comments on commit 3a3a72b

Please sign in to comment.