Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meteor 3 #183

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ jobs:
name: Javascript standard lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- name: cache dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-18-${{ hashFiles('**/package-lock.json') }}
key: ${{ runner.os }}-node-20-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-18-
${{ runner.os }}-node-20-
- name: Install dependencies
run: npm ci
- name: Run lint
Expand Down
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions client/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export const MonitorInternals = {
},

reportIdle(time) {
return Meteor.call('user-status-idle', time);
return Meteor.callAsync('user-status-idle', time);
},

reportActive(time) {
return Meteor.call('user-status-active', time);
return Meteor.callAsync('user-status-active', time);
}

};
Expand Down
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Package.describe({
name: 'mizzao:user-status',
summary: 'User connection and idle state tracking for Meteor',
version: '1.2.0',
version: '2.0.0-beta.0',
git: 'https://github.com/Meteor-Community-Packages/meteor-user-status.git'
});

Package.onUse((api) => {
api.versionsFrom(['1.7.0.5', '2.7', '2.8.1', '2.15']);
api.versionsFrom(['2.8.1', '2.15', '3.0-beta.4']);

api.use('ecmascript');
api.use('accounts-base');
Expand Down
68 changes: 34 additions & 34 deletions server/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const statusEvents = new (EventEmitter)();
- "false" if user is online and idle
- null if user is offline
*/
statusEvents.on('connectionLogin', (advice) => {
statusEvents.on('connectionLogin', async (advice) => {
const update = {
$set: {
'status.online': true,
Expand All @@ -41,9 +41,9 @@ statusEvents.on('connectionLogin', (advice) => {

// unless ALL existing connections are idle (including this new one),
// the user connection becomes active.
const conns = UserConnections.find({
const conns = await UserConnections.find({
userId: advice.userId
}).fetch();
}).fetchAsync();
if (!conns.every(c => c.idle)) {
update.$set['status.idle'] = false;
update.$unset = {
Expand All @@ -52,17 +52,17 @@ statusEvents.on('connectionLogin', (advice) => {
}
// in other case, idle field remains true and no update to lastActivity.

Meteor.users.update(advice.userId, update);
Meteor.users.updateAsync(advice.userId, update);
});

statusEvents.on('connectionLogout', (advice) => {
const conns = UserConnections.find({
statusEvents.on('connectionLogout', async (advice) => {
const conns = await UserConnections.find({
userId: advice.userId
}).fetch();
}).fetchAsync();
if (conns.length === 0) {
// Go offline if we are the last connection for this user
// This includes removing all idle information
Meteor.users.update(advice.userId, {
Meteor.users.updateAsync(advice.userId, {
$set: {
'status.online': false
},
Expand All @@ -75,7 +75,7 @@ statusEvents.on('connectionLogout', (advice) => {
/*
All remaining connections are idle:
- If the last active connection quit, then we should go idle with the most recent activity

- If an idle connection quit, nothing should happen; specifically, if the
most recently active idle connection quit, we shouldn't tick the value backwards.
This may result in a no-op so we can be smart and skip the update.
Expand All @@ -85,7 +85,7 @@ statusEvents.on('connectionLogout', (advice) => {
} // The dropped connection was already idle

const latestLastActivity = new Date(Math.max(...conns.map(conn => conn.lastActivity)));
Meteor.users.update(advice.userId, {
Meteor.users.updateAsync(advice.userId, {
$set: {
'status.idle': true,
'status.lastActivity': latestLastActivity
Expand All @@ -101,10 +101,10 @@ statusEvents.on('connectionLogout', (advice) => {
TODO: There is a race condition when switching between tabs, leaving the user inactive while idle goes from one tab to the other.
It can probably be smoothed out.
*/
statusEvents.on('connectionIdle', (advice) => {
const conns = UserConnections.find({
statusEvents.on('connectionIdle', async (advice) => {
const conns = await UserConnections.find({
userId: advice.userId
}).fetch();
}).fetchAsync();
if (!conns.every(c => c.idle)) {
return;
}
Expand All @@ -113,16 +113,16 @@ statusEvents.on('connectionIdle', (advice) => {

// TODO: the race happens here where everyone was idle when we looked for them but now one of them isn't.
const latestLastActivity = new Date(Math.max(...conns.map(conn => conn.lastActivity)));
Meteor.users.update(advice.userId, {
Meteor.users.updateAsync(advice.userId, {
$set: {
'status.idle': true,
'status.lastActivity': latestLastActivity
}
});
});

statusEvents.on('connectionActive', (advice) => {
Meteor.users.update(advice.userId, {
statusEvents.on('connectionActive', async (advice) => {
Meteor.users.updateAsync(advice.userId, {
$set: {
'status.idle': false
},
Expand All @@ -133,11 +133,11 @@ statusEvents.on('connectionActive', (advice) => {
});

// Reset online status on startup (users will reconnect)
const onStartup = (selector) => {
const onStartup = async (selector) => {
if (selector == null) {
selector = Meteor?.settings?.packages?.['mizzao:user-status']?.startupQuerySelector || {};
}
return Meteor.users.update(selector, {
return await Meteor.users.updateAsync(selector, {
$set: {
'status.online': false
},
Expand All @@ -154,17 +154,17 @@ const onStartup = (selector) => {
Local session modification functions - also used in testing
*/

const addSession = (connection) => {
UserConnections.upsert(connection.id, {
const addSession = async (connection) => {
UserConnections.upsertAsync(connection.id, {
$set: {
ipAddr: connection.clientAddress,
userAgent: connection.httpHeaders['user-agent']
}
});
};

const loginSession = (connection, date, userId) => {
UserConnections.upsert(connection.id, {
const loginSession = async (connection, date, userId) => {
UserConnections.upsertAsync(connection.id, {
$set: {
userId,
loginTime: date
Expand All @@ -181,9 +181,9 @@ const loginSession = (connection, date, userId) => {
};

// Possibly trigger a logout event if this connection was previously associated with a user ID
const tryLogoutSession = (connection, date) => {
const tryLogoutSession = async (connection, date) => {
let conn;
if ((conn = UserConnections.findOne({
if ((conn = await UserConnections.findOneAsync({
_id: connection.id,
userId: {
$exists: true
Expand All @@ -193,7 +193,7 @@ const tryLogoutSession = (connection, date) => {
}

// Yes, this is actually a user logging out.
UserConnections.upsert(connection.id, {
UserConnections.upsertAsync(connection.id, {
$unset: {
userId: null,
loginTime: null
Expand All @@ -208,13 +208,13 @@ const tryLogoutSession = (connection, date) => {
});
};

const removeSession = (connection, date) => {
tryLogoutSession(connection, date);
UserConnections.remove(connection.id);
const removeSession = async (connection, date) => {
await tryLogoutSession(connection, date);
UserConnections.removeAsync(connection.id);
};

const idleSession = (connection, date, userId) => {
UserConnections.update(connection.id, {
const idleSession = async (connection, date, userId) => {
UserConnections.updateAsync(connection.id, {
$set: {
idle: true,
lastActivity: date
Expand All @@ -228,8 +228,8 @@ const idleSession = (connection, date, userId) => {
});
};

const activeSession = (connection, date, userId) => {
UserConnections.update(connection.id, {
const activeSession = async (connection, date, userId) => {
UserConnections.updateAsync(connection.id, {
$set: {
idle: false
},
Expand All @@ -251,8 +251,8 @@ const activeSession = (connection, date, userId) => {
Meteor.startup(onStartup);

// Opening and closing of DDP connections
Meteor.onConnection((connection) => {
addSession(connection);
Meteor.onConnection(async (connection) => {
await addSession(connection);

return connection.onClose(() => removeSession(connection, new Date()));
});
Expand Down