Skip to content

Commit

Permalink
Refactor utils
Browse files Browse the repository at this point in the history
Renamed src/headless/utils/core.js -> src/headless/utils/index.js
Move functions that rely on the `_converse` object to src/headles/utils/session.js
Move object-related functions to src/headless/utils/object.js
Remove `contains` and `contains.not` functions, they're not being used.
  • Loading branch information
jcbrand committed Sep 28, 2023
1 parent 70490b3 commit 11934b3
Show file tree
Hide file tree
Showing 48 changed files with 621 additions and 629 deletions.
4 changes: 2 additions & 2 deletions dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
muc_show_logs_before_join: true,
notify_all_room_messages: ['[email protected]'],
view_mode: 'fullscreen',
// websocket_url: 'wss://conversejs.org/xmpp-websocket',
websocket_url: 'ws://chat.example.org:5380/xmpp-websocket',
websocket_url: 'wss://conversejs.org/xmpp-websocket',
// websocket_url: 'ws://chat.example.org:5380/xmpp-websocket',
whitelisted_plugins: ['converse-debug'],
// connection_options: { worker: '/dist/shared-connection-worker.js' }
});
Expand Down
4 changes: 2 additions & 2 deletions src/headless/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import './shared/constants.js';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import api from './shared/api/index.js';
import _converse from './shared/_converse';
_converse.api = api;

import dayjs from 'dayjs';
import i18n from './shared/i18n';
import { converse } from './shared/api/public.js';

_converse.api = api;

dayjs.extend(advancedFormat);

/* START: Removable components
Expand Down
6 changes: 4 additions & 2 deletions src/headless/plugins/bosh.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import log from "../log.js";
import { BOSH_WAIT } from '../shared/constants.js';
import { Model } from '@converse/skeletor/src/model.js';
import { setUserJID, } from '../utils/init.js';
import { isTestEnv } from '../utils/session.js';
import { createStore } from '../utils/storage.js';

const { Strophe } = converse.env;

Expand All @@ -33,7 +35,7 @@ converse.plugins.add('converse-bosh', {
const id = BOSH_SESSION_ID;
if (!_converse.bosh_session) {
_converse.bosh_session = new Model({id});
_converse.bosh_session.browserStorage = _converse.createStore(id, "session");
_converse.bosh_session.browserStorage = createStore(id, "session");
await new Promise(resolve => _converse.bosh_session.fetch({'success': resolve, 'error': resolve}));
}
if (_converse.jid) {
Expand Down Expand Up @@ -93,7 +95,7 @@ converse.plugins.add('converse-bosh', {
_converse.connection.restore(jid, _converse.connection.onConnectStatusChanged);
return true;
} catch (e) {
!_converse.isTestEnv() && log.warn("Could not restore session for jid: "+jid+" Error message: "+e.message);
!isTestEnv() && log.warn("Could not restore session for jid: "+jid+" Error message: "+e.message);
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/headless/plugins/chat/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { filesize } from "filesize";
import { getMediaURLsMetadata } from '../../shared/parsers.js';
import { getOpenPromise } from '@converse/openpromise';
import { initStorage } from '../../utils/storage.js';
import { isUniView, isEmptyMessage } from '../../utils/core.js';
import { isEmptyMessage } from '../../utils/index.js';
import { isUniView } from '../../utils/session.js';
import { parseMessage } from './parsers.js';
import { sendMarker } from '../../shared/actions.js';

Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/chat/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import dayjs from 'dayjs';
import log from '../../log.js';
import u from '../../utils/core';
import u from '../../utils/index.js';
import { rejectMessage } from '../../shared/actions';

import {
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/chat/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import api, { converse } from '../../shared/api/index.js';
import log from '../../log.js';
import { isArchived, isHeadline, isServerMessage, } from '../../shared/parsers';
import { parseMessage } from './parsers.js';
import { shouldClearCache } from '../../utils/core.js';
import { shouldClearCache } from '../../utils/session.js';

const { Strophe, u } = converse.env;

Expand Down
9 changes: 5 additions & 4 deletions src/headless/plugins/disco/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sizzle from 'sizzle';
import { Collection } from '@converse/skeletor/src/collection';
import { Model } from '@converse/skeletor/src/model.js';
import { getOpenPromise } from '@converse/openpromise';
import { createStore } from '../../utils/storage.js';

const { Strophe } = converse.env;

Expand All @@ -28,21 +29,21 @@ class DiscoEntity extends Model {

this.dataforms = new Collection();
let id = `converse.dataforms-${this.get('jid')}`;
this.dataforms.browserStorage = _converse.createStore(id, 'session');
this.dataforms.browserStorage = createStore(id, 'session');

this.features = new Collection();
id = `converse.features-${this.get('jid')}`;
this.features.browserStorage = _converse.createStore(id, 'session');
this.features.browserStorage = createStore(id, 'session');
this.listenTo(this.features, 'add', this.onFeatureAdded);

this.fields = new Collection();
id = `converse.fields-${this.get('jid')}`;
this.fields.browserStorage = _converse.createStore(id, 'session');
this.fields.browserStorage = createStore(id, 'session');
this.listenTo(this.fields, 'add', this.onFieldAdded);

this.identities = new Collection();
id = `converse.identities-${this.get('jid')}`;
this.identities.browserStorage = _converse.createStore(id, 'session');
this.identities.browserStorage = createStore(id, 'session');
this.fetchFeatures(options);
}

Expand Down
5 changes: 3 additions & 2 deletions src/headless/plugins/disco/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import { Collection } from "@converse/skeletor/src/collection";
import { createStore } from '../../utils/storage.js';

const { Strophe, $iq } = converse.env;

Expand Down Expand Up @@ -65,7 +66,7 @@ export async function initializeDisco () {

_converse.disco_entities = new _converse.DiscoEntities();
const id = `converse.disco-entities-${_converse.bare_jid}`;
_converse.disco_entities.browserStorage = _converse.createStore(id, 'session');
_converse.disco_entities.browserStorage = createStore(id, 'session');

const collection = await _converse.disco_entities.fetchEntities();
if (collection.length === 0 || !collection.get(_converse.domain)) {
Expand Down Expand Up @@ -93,7 +94,7 @@ export function initStreamFeatures () {
const id = `converse.stream-features-${bare_jid}`;
api.promises.add('streamFeaturesAdded');
_converse.stream_features = new Collection();
_converse.stream_features.browserStorage = _converse.createStore(id, "session");
_converse.stream_features.browserStorage = createStore(id, "session");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/mam/placeholder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Model } from '@converse/skeletor/src/model.js';
import { getUniqueId } from '../../utils/core.js';
import { getUniqueId } from '../../utils/index.js';

export default class MAMPlaceholderMessage extends Model {

Expand Down
11 changes: 5 additions & 6 deletions src/headless/plugins/muc/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import api from '../../shared/api/index.js';
import log from '../../log';
import { Strophe } from 'strophe.js';

const { u } = converse.env;
import { getJIDFromURI } from '../../utils/jid.js';


export default {
Expand Down Expand Up @@ -35,9 +34,9 @@ export default {
if (jids === undefined) {
throw new TypeError('rooms.create: You need to provide at least one JID');
} else if (typeof jids === 'string') {
return api.rooms.get(u.getJIDFromURI(jids), attrs, true);
return api.rooms.get(getJIDFromURI(jids), attrs, true);
}
return jids.map(jid => api.rooms.get(u.getJIDFromURI(jid), attrs, true));
return jids.map(jid => api.rooms.get(getJIDFromURI(jid), attrs, true));
},

/**
Expand Down Expand Up @@ -142,7 +141,7 @@ export default {
await api.waitUntil('chatBoxesFetched');

async function _get (jid) {
jid = u.getJIDFromURI(jid);
jid = getJIDFromURI(jid);
let model = await api.chatboxes.get(jid);
if (!model && create) {
model = await api.chatboxes.create(jid, attrs, _converse.ChatRoom);
Expand Down
11 changes: 6 additions & 5 deletions src/headless/plugins/muc/muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { TimeoutError } from '../../shared/errors.js';
import { computeAffiliationsDelta, setAffiliations, getAffiliationList } from './affiliations/utils.js';
import { getOpenPromise } from '@converse/openpromise';
import { handleCorrection } from '../../shared/chat/utils.js';
import { initStorage } from '../../utils/storage.js';
import { initStorage, createStore } from '../../utils/storage.js';
import { isArchived, getMediaURLsMetadata } from '../../shared/parsers.js';
import { isUniView, getUniqueId, safeSave } from '../../utils/core.js';
import { getUniqueId, safeSave } from '../../utils/index.js';
import { isUniView } from '../../utils/session.js';
import { parseMUCMessage, parseMUCPresence } from './parsers.js';
import { sendMarker } from '../../shared/actions.js';

Expand Down Expand Up @@ -399,19 +400,19 @@ const ChatRoomMixin = {
}, {})
)
);
this.features.browserStorage = _converse.createStore(id, 'session');
this.features.browserStorage = createStore(id, 'session');
this.features.listenTo(_converse, 'beforeLogout', () => this.features.browserStorage.flush());

id = `converse.muc-config-${_converse.bare_jid}-${this.get('jid')}`;
this.config = new Model({ id });
this.config.browserStorage = _converse.createStore(id, 'session');
this.config.browserStorage = createStore(id, 'session');
this.config.listenTo(_converse, 'beforeLogout', () => this.config.browserStorage.flush());
},

initOccupants () {
this.occupants = new _converse.ChatRoomOccupants();
const id = `converse.occupants-${_converse.bare_jid}${this.get('jid')}`;
this.occupants.browserStorage = _converse.createStore(id, 'session');
this.occupants.browserStorage = createStore(id, 'session');
this.occupants.chatroom = this;
this.occupants.listenTo(_converse, 'beforeLogout', () => this.occupants.browserStorage.flush());
},
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/muc/occupants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Model } from '@converse/skeletor/src/model.js';
import { Strophe } from 'strophe.js';
import { getAffiliationList } from './affiliations/utils.js';
import { getAutoFetchedAffiliationLists } from './utils.js';
import { getUniqueId } from '../../utils/core.js';
import { getUniqueId } from '../../utils/index.js';

const { u } = converse.env;

Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/muc/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import log from '../../log.js';
import { ROLES } from './constants.js';
import { safeSave } from '../../utils/core.js';
import { safeSave } from '../../utils/index.js';

const { Strophe, sizzle, u } = converse.env;

Expand Down
3 changes: 2 additions & 1 deletion src/headless/plugins/ping/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import { isTestEnv } from '../../utils/session.js';

const { Strophe, $iq } = converse.env;

Expand Down Expand Up @@ -56,7 +57,7 @@ export function unregisterIntervalHandler () {
}

export function onEverySecond () {
if (_converse.isTestEnv() || !api.connection.authenticated()) {
if (isTestEnv() || !api.connection.authenticated()) {
return;
}
const ping_interval = api.settings.get('ping_interval');
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/roster/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Model } from '@converse/skeletor/src/model.js';
import { RosterFilter } from '../../plugins/roster/filter.js';
import { STATUS_WEIGHTS } from "../../shared/constants";
import { initStorage } from '../../utils/storage.js';
import { shouldClearCache } from '../../utils/core.js';
import { shouldClearCache } from '../../utils/session.js';

const { $pres } = converse.env;

Expand Down
5 changes: 3 additions & 2 deletions src/headless/plugins/smacks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import log from '../../log.js';
import { getOpenPromise } from '@converse/openpromise';
import { isTestEnv } from '../../utils/session.js';

const { Strophe } = converse.env;
const u = converse.env.utils;

function isStreamManagementSupported () {
if (api.connection.isType('bosh') && !_converse.isTestEnv()) {
if (api.connection.isType('bosh') && !isTestEnv()) {
return false;
}
return api.disco.stream.getFeature('sm', Strophe.NS.SM);
Expand Down Expand Up @@ -172,7 +173,7 @@ export async function sendEnableStanza () {
_converse.connection._addSysHandler(el => promise.resolve(saveSessionData(el)), Strophe.NS.SM, 'enabled');
_converse.connection._addSysHandler(el => promise.resolve(onFailedStanza(el)), Strophe.NS.SM, 'failed');

const resume = api.connection.isType('websocket') || _converse.isTestEnv();
const resume = api.connection.isType('websocket') || isTestEnv();
const stanza = u.toStanza(`<enable xmlns="${Strophe.NS.SM}" resume="${resume}"/>`);
api.send(stanza);
_converse.connection.flush();
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XMPPStatus from './status.js';
import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import status_api from './api.js';
import { shouldClearCache } from '../../utils/core.js';
import { shouldClearCache } from '../../utils/session.js';
import {
addStatusToMUCJoinPresence,
initStatus,
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/vcard/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _converse from '../../shared/_converse.js';
import api, { converse } from '../../shared/api/index.js';
import log from "../../log.js";
import { initStorage } from '../../utils/storage.js';
import { shouldClearCache } from '../../utils/core.js';
import { shouldClearCache } from '../../utils/session.js';

const { Strophe, $iq, u } = converse.env;

Expand Down
10 changes: 0 additions & 10 deletions src/headless/shared/_converse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import log from '../log.js';
import pluggable from 'pluggable.js/src/pluggable.js';
import { Events } from '@converse/skeletor/src/events.js';
import { Router } from '@converse/skeletor/src/router.js';
import { createStore, getDefaultStore } from '../utils/storage.js';
import { getInitSettings } from './settings/utils.js';
import { getOpenPromise } from '@converse/openpromise';
import { shouldClearCache } from '../utils/core.js';

import {
ACTIVE,
Expand Down Expand Up @@ -42,7 +39,6 @@ import {
const _converse = {
log,

shouldClearCache, // TODO: Should be moved to utils with next major release
VERSION_NAME,

templates: {},
Expand Down Expand Up @@ -86,12 +82,6 @@ const _converse = {
default_connection_options: {'explicitResourceBinding': true},
router: new Router(),

isTestEnv: () => {
return getInitSettings()['bosh_service_url'] === 'montague.lit/http-bind';
},

getDefaultStore,
createStore,

/**
* Translate the given string based on the current locale.
Expand Down
2 changes: 1 addition & 1 deletion src/headless/shared/api/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _converse from '../_converse.js';
import isFunction from '../../utils/core.js';
import { isFunction } from '../../utils/object.js';


export default {
Expand Down
3 changes: 2 additions & 1 deletion src/headless/shared/api/promise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _converse from '../_converse.js';
import { getOpenPromise } from '@converse/openpromise';
import { waitUntil, isFunction } from '../../utils/core.js';
import { waitUntil } from '../../utils/promise.js';
import { isFunction } from '../../utils/object.js';

export default {
/**
Expand Down
5 changes: 3 additions & 2 deletions src/headless/shared/api/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import dayjs from 'dayjs';
import i18n from '../i18n';
import log from '../../log.js';
import sizzle from 'sizzle';
import u, { setUnloadEvent } from '../../utils/core.js';
import u from '../../utils/index.js';
import { ANONYMOUS, CHAT_STATES, KEYCODES, VERSION_NAME } from '../constants.js';
import { setUnloadEvent, isTestEnv } from '../../utils/session.js';
import { Collection } from "@converse/skeletor/src/collection";
import { Model } from '@converse/skeletor/src/model.js';
import { Strophe, $build, $iq, $msg, $pres } from 'strophe.js';
Expand Down Expand Up @@ -124,7 +125,7 @@ export const converse = Object.assign(window.converse || {}, {
*/
api.trigger('initialized');

if (_converse.isTestEnv()) {
if (isTestEnv()) {
return _converse;
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/headless/shared/api/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _converse from '../_converse.js';
import presence_api from './presence.js';
import u, { replacePromise } from '../../utils/core.js';
import { isSameDomain } from '../../utils/jid.js';
import { replacePromise } from '../../utils/session.js';
import { attemptNonPreboundSession, initConnection, setUserJID } from '../../utils/init.js';
import { getOpenPromise } from '@converse/openpromise';
import { user_settings_api } from '../settings/api.js';
Expand Down Expand Up @@ -46,7 +47,7 @@ export default {
async login (jid, password, automatic=false) {
const { api } = _converse;
jid = jid || api.settings.get('jid');
if (!_converse.connection?.jid || (jid && !u.isSameDomain(_converse.connection.jid, jid))) {
if (!_converse.connection?.jid || (jid && !isSameDomain(_converse.connection.jid, jid))) {
initConnection();
}
if (api.settings.get("connection_options")?.worker && (await _converse.connection.restoreWorkerSession())) {
Expand Down
2 changes: 1 addition & 1 deletion src/headless/shared/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import api from '../api/index.js';
import { ANONYMOUS, BOSH_WAIT, LOGOUT } from '../../shared/constants.js';
import { CONNECTION_STATUS } from '../constants';
import { Strophe } from 'strophe.js';
import { clearSession, tearDown } from "../../utils/core.js";
import { clearSession, tearDown } from "../../utils/session.js";
import { getOpenPromise } from '@converse/openpromise';
import { setUserJID, } from '../../utils/init.js';

Expand Down
Loading

0 comments on commit 11934b3

Please sign in to comment.