Skip to content

Commit

Permalink
Removed OAuth stuff for initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexryan committed Aug 11, 2015
1 parent f93dd27 commit 8a607ad
Showing 1 changed file with 1 addition and 122 deletions.
123 changes: 1 addition & 122 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ var Tray = require('tray');
var path = require('path');
var shell = require('shell');
var asana = require('asana');
var options = require('./options');
var ipc = require("ipc");

// Report crashes to our server.
require('crash-reporter').start();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;
var appIcon = null;

// oauth
var client = asana.Client.create({
clientId: options.client_id,
clientSecret: options.client_secret,
redirectUri: options.redirect_uri
});
var access_token_set = false;

function getSavedOrDefaultStateData() {
// Read in the saved state
var savedStatePath = app.getPath("userData") + "/saved_state";
Expand All @@ -43,67 +31,6 @@ function getSavedOrDefaultStateData() {
url: "https://app.asana.com"};
}

// helper to show tasks due today and marked today in tray
function updateTrayContents(list) {
contextMenu = new Menu();
var markedToday = [];
var dueToday = [];
list.forEach(function(item) {
if (item.due_on === new Date().toJSON().slice(0,10)) {
dueToday.push(new MenuItem({
label: item.name,
click: function() {
mainWindow.send("load-task", item.id);
mainWindow.show();
mainWindow.focus();
}
}));
} else {
markedToday.push(new MenuItem({
label: item.name,
click: function() {
mainWindow.send("load-task", item.id);
mainWindow.show();
mainWindow.focus();
}
}));
}
});
contextMenu.append(new MenuItem({ label: 'DUE TODAY', enabled: false }));
dueToday.forEach(function(task) {
contextMenu.append(task);
});
contextMenu.append(new MenuItem({ type: 'separator' }));
contextMenu.append(new MenuItem({ label: 'MARKED TODAY', enabled: false }));
markedToday.forEach(function(task) {
contextMenu.append(task);
});

appIcon.setContextMenu(contextMenu);
}

// helper to get all tasks filtered by due today or marked today
function queryTasks() {
client.users.me().then(function(user) {
var userId = user.id;
var workspaceId = user.workspaces[0].id;
return client.tasks.findAll({
assignee: userId,
completed_since: 'now',
workspace: workspaceId,
opt_fields: 'id,name,assignee_status,completed,due_on'
});
}).then(function(response) {
console.log("response data", response.data);
return response.data;
}).filter(function(task) {
return task.due_on === new Date().toJSON().slice(0,10) ||
task.assignee_status === 'today';
}).then(function(list) {
updateTrayContents(list);
});
}

app.on('before-quit', function() {
mainWindow.forceClose = true;
});
Expand Down Expand Up @@ -141,62 +68,15 @@ app.on('ready', function() {
// Restore the last window size and position
mainWindow.setBounds(stateData.bounds);

// Create the tray
var iconImage = path.join(__dirname, 'icon/','asanaicon.png');
appIcon = new Tray(iconImage);
appIcon.setToolTip('Asana');

// Oauth authentication
var oauthUrl = 'https://app.asana.com/-/oauth_authorize?'
+ 'client_id=' + options.client_id
+ '&redirect_uri=' + options.redirect_uri
+ '&access_type=offline'
+ '&response_type=' + 'code';
mainWindow.loadUrl(oauthUrl, {
mainWindow.loadUrl("https://app.asana.com/", {
userAgent: userAgent
});
var code;
mainWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl) {
var raw_code = /code=([^&]*)/.exec(newUrl) || null;
code = (raw_code && raw_code.length > 1) ? decodeURIComponent(raw_code[1]) : null;

// code = code.split("-")[0];
// if (code[code.length - 1] === "#") {
// code = code.substr(0, code.length - 1);
// }
console.log("hey", code);

// authenticate with access token
if (code && !access_token_set && newUrl.indexOf("https://app.asana.com/?code=") === 0) {
client.app.accessTokenFromCode(code).then(function(credentials) {
client.useOauth({
credentials: credentials
});
access_token_set = true;
});

mainWindow.webContents.loadUrl(stateData.url, {
userAgent: userAgent
});
}

// set tray values initially
if (access_token_set) {
queryTasks();
}
});

// Make certain parts of the window draggable
// update tray contents every 1 minute
mainWindow.webContents.on('did-finish-load', function() {
var cssPath = path.resolve(__dirname, "frameless.css");
var css = fs.readFileSync(cssPath, {encoding: 'utf8'});
mainWindow.webContents.insertCSS(css);
setInterval(function() {
if (access_token_set) {
queryTasks();
}
}, 60000);
});

// Make links open in the default system browser
Expand Down Expand Up @@ -231,7 +111,6 @@ app.on('ready', function() {
});

ipc.on('show-window', function() {
console.log("showing window");
mainWindow.show();
mainWindow.focus();
});
Expand Down

0 comments on commit 8a607ad

Please sign in to comment.