Commit 0e51053d authored by Johannes Zellner's avatar Johannes Zellner

Ensure we don't send out notification for initial syncs

parent be655246
......@@ -50,18 +50,27 @@ function init(callback) {
port: config[config.defaultEnv].port,
user: config[config.defaultEnv].user,
password: config[config.defaultEnv].password,
database: config[config.defaultEnv].database
database: config[config.defaultEnv].database,
dateStrings: true
});
callback();
}
function projectPostprocess(p) {
if (p.lastSuccessfulSyncAt === '0000-00-00 00:00:00') p.lastSuccessfulSyncAt = 0;
return p;
}
function projectsList(userId, callback) {
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof callback, 'function');
db.query('SELECT * FROM projects WHERE userId=?', [ userId ], function (error, result) {
if (error) return callback(error);
result.forEach(projectPostprocess);
callback(null, result);
});
}
......@@ -71,10 +80,12 @@ function projectsAdd(project, callback) {
assert.strictEqual(typeof callback, 'function');
project.id = uuid();
project.enabled = true;
project.lastSuccessfulSyncAt = 0;
db.query('INSERT INTO projects SET ?', project, function (error) {
if (error) return callback(error);
callback(null, project);
callback(null, projectPostprocess(project));
});
}
......@@ -84,7 +95,7 @@ function projectsGet(projectId, callback) {
db.query('SELECT * FROM projects WHERE id=?', [ projectId ], function (error, result) {
if (error) return callback(error);
callback(null, result[0]);
callback(null, projectPostprocess(result[0]));
});
}
......@@ -94,7 +105,7 @@ function projectsUpdate(projectId, data, callback) {
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
db.query('UPDATE projects SET enabled=? WHERE id=?', [ data.enabled, projectId ], function (error) {
db.query('UPDATE projects SET ? WHERE id=?', [ data, projectId ], function (error) {
if (error) return callback(error);
callback(null);
});
......@@ -109,7 +120,6 @@ function projectsRemove(projectId, callback) {
db.query('DELETE FROM projects WHERE id=?', [ projectId ], function (error) {
if (error) return callback(error);
callback();
});
});
......
......@@ -92,17 +92,11 @@ function syncStarredByUser(user, callback) {
// do not overwhelm github api with async.each() we hit rate limits if we do
async.eachSeries(newProjects, function (project, callback) {
// we add projects first with release notification disabled
database.projects.add({ userId: user.id, name: project.name, enabled: false }, function (error, result) {
database.projects.add({ userId: user.id, name: project.name }, function (error, result) {
if (error) return callback(error);
// force an initial release sync without notification
syncReleasesByProject(user, result, function (error) {
// rollback the project record if we couldn't finish the initial sync
if (error) return database.projects.remove(result.id, callback);
// now activate the project notifications
database.projects.update(result.id, { enabled: true }, callback);
});
// force an initial release sync
syncReleasesByProject(user, result, callback);
});
}, function (error) {
if (error) return callback(error);
......@@ -124,6 +118,8 @@ function syncReleasesByProject(user, project, callback) {
assert.strictEqual(typeof project, 'object');
assert.strictEqual(typeof callback, 'function');
debug(`syncReleasesByProject: ${project.name} start sync releases. Last successful sync was at`, project.lastSuccessfulSyncAt);
github.getReleases(user.githubToken, project, function (error, result) {
if (error) return callback(error);
......@@ -142,17 +138,24 @@ function syncReleasesByProject(user, project, callback) {
github.getCommit(user.githubToken, project, release.sha, function (error, commit) {
if (error) return callback(error);
// if notifications for this project are enabled, we mark the release as not notified yet
release.notified = !project.enabled;
// before initial successful sync and if notifications for this project are enabled, we mark the release as not notified yet
release.notified = !project.lastSuccessfulSyncAt ? true : !project.enabled;
release.createdAt = new Date(commit.committer.date);
delete release.sha;
debug(`syncReleasesByProject: add for project ${project.name} release`, release);
debug(`syncReleasesByProject: ${project.name} add release ${release.version} notified ${release.notified}`);
database.releases.add(release, callback);
});
}, callback);
}, function (error) {
if (error) return callback(error);
debug(`syncReleasesByProject: ${project.name} successfully synced`);
// set the last successful sync time
database.projects.update(project.id, { lastSuccessfulSyncAt: new Date() }, callback);
});
});
});
}
......
......@@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS projects(
userId VARCHAR(128) NOT NULL,
name VARCHAR(512) NOT NULL,
enabled BOOLEAN DEFAULT true,
lastSuccessfulSyncAt TIMESTAMP,
FOREIGN KEY(userId) REFERENCES users(id),
PRIMARY KEY(id));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment