Skip to content

Commit

Permalink
Merge pull request #21 from abhishek97/github
Browse files Browse the repository at this point in the history
login with github and sync gists
  • Loading branch information
championswimmer authored Aug 3, 2017
2 parents 4deb71a + ebf10a9 commit 20d74a6
Show file tree
Hide file tree
Showing 11 changed files with 2,436 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.sass-cache
node_modules
*.out
secrets.json
secrets.js
db
/snips.db
/dist/
152 changes: 151 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ const path = require('path');


const {app, BrowserWindow, ipcMain, globalShortcut, clipboard} = require('electron');
const config = require('./secrets');
const electronOauth2 = require('electron-oauth2');
const axios = require('axios');
const U = require('./utils');

let githubUser = (void 0)
const githubConfig = {
clientId: config.github.clientId,
clientSecret: config.github.secret,
authorizationUrl: 'http://github.com/login/oauth/authorize',
tokenUrl: 'https://github.com/login/oauth/access_token',
useBasicAuthorizationHeader: false,
redirectUri: 'http://localhost/'
};

const githubLogin = electronOauth2(githubConfig, {
alwaysOnTop: true,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false
}
})


const url = require('url');
Expand All @@ -33,7 +55,7 @@ app.on('ready', function () {
slashes: true
}));

// mainWindow.webContents.openDevTools()
//mainWindow.webContents.openDevTools()

require('./menu')
});
Expand All @@ -60,6 +82,8 @@ function newSnip() {

});

//snipWindow.openDevTools()

snipWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public_static', 'snip.html'),
protocol: 'file:',
Expand Down Expand Up @@ -123,6 +147,36 @@ ipcMain.on('new-snip-add', function (event, arg) {
if (snip.hotkey == undefined) {
snip.hotkey = null;
}
if (snip.makeGist) {
const dateString = U.now();
const fileName = `snipper-${snip.title}-${dateString}.${snip.language}`
const payload = {
description: snip.title,
public: true,
files: {
[fileName]: {
content: snip.code
}
}
}

githubLogin.getAccessToken({
scope: 'user,gist'
})
.then(token => {
return axios.post('https://api.github.com/gists', payload, {
headers: {
Authorization: `token ${token.access_token}`
},
json: true
})
})
.then(gist => {
})
.catch(err=> {
console.error(err)
})
}
if (snip._id) {
db.updateSnip(snip._id, {
title: snip.title,
Expand Down Expand Up @@ -200,9 +254,19 @@ ipcMain.on("openhelp", function (event) {
width: 800,
height: 600
});

helpWindow.loadURL('https://github.com/electron/electron/blob/master/docs/api/accelerator.md');
//mainWindow.openDevTools() //opens inspect console

/*githubLogin.getAccessToken({
scope: 'user,gist'
}).then(token => {
// yay with token
}).catch(err=> {
console.error(err)
})
*/

});

ipcMain.on('sort-dec', function (event, arg1, arg2) {
Expand All @@ -217,5 +281,91 @@ ipcMain.on('sort-inc', function (event, arg1, arg2) {
});
});

ipcMain.on('github-login', function (event, arg1, arg2) {
githubLogin.getAccessToken({
scope: 'user'
})
.then(token => {
return axios.get('https://api.github.com/user',{
headers: {
Authorization: `token ${token.access_token}`
},
json: true
})
})
.then(user => {
githubUser = user
mainWindow.webContents.send('githubUser', githubUser, false);
})
.catch(err=> {
console.error(err)
})
})

ipcMain.on('sync-gists', function (event) {
githubLogin.getAccessToken({
scope: 'user'
})
.then(token => {
return axios.get('https://api.github.com/gists',{
headers: {
Authorization: `token ${token.access_token}`
},
json: true
})
})
.then(response => {
const gists = response.data
const snipperGists = gists.filter(gist => {
const filteredFiles = Object.keys(gist.files).filter(key => {
return /^snipper-.*$/.test(key)
})

return filteredFiles.length != 0
})

const promises = snipperGists.map(gist => {
const fileName = Object.keys(gist.files)[0]
const file = gist.files[fileName]
const url = file.raw_url

return (
axios.get(url)
.then( ({data}) => {
file.content = data
return file
})
.then(file => {
return new Promise(resolve => {
db.insertSnip({
title: file.filename,
language: file.language,
code: file.content,
timestamp: Math.floor(Date.now() / 1000),
hotkey: null
}, function () {
resolve()
})
})
})
)
})
return Promise.all(promises)
})
.then( () => {
sendAllSnips(false)
})
.catch(err=> {
console.error(err)
})
})

ipcMain.on('getGithubUser', function (event, window) {
if (window === 'main')
mainWindow.webContents.send('githubUser', githubUser, false);
else
snipWindow.send('githubUser', githubUser, false)
})

module.exports = {sendAllSnips, newSnip};

2 changes: 1 addition & 1 deletion menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ if (process.platform === 'darwin') {
}

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
Menu.setApplicationMenu(menu)
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
},
"license": "ISC",
"dependencies": {
"axios": "^0.16.2",
"electron-oauth2": "^3.0.0",
"nedb": "^1.8.0",
"path": "^0.12.7",
"url": "^0.11.0"
Expand Down
5 changes: 5 additions & 0 deletions public_static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<input type="text" class="form-control" placeholder="Title" name="srch-term" id="srch-term">
<div class="input-group-btn">
<a class="btn btn-default"><i class="glyphicon glyphicon-search"></i></a>
<button type="submit" class="login-button btn btn-success" onclick="loginViaGithub()" id="loginGithubButton">Login via Github</button>
<button type="submit" class="login-button btn btn-success" onclick="syncGists()" id="syncGistButton">Sync Gists</button>
</div>
</div>
</form>
Expand Down Expand Up @@ -176,6 +178,9 @@ <h4 class="modal-title custom_align" id="Heading">Delete this Snip</h4>
.block:hover {
cursor: pointer;
}
.login-button {
margin-left: 10px
}

</style>

Expand Down
22 changes: 21 additions & 1 deletion public_static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let hotkey = ' <td> <div class="input-group" style="width:300px;" >'

window.onload = function () {
ipcRenderer.send('get-snips');
ipcRenderer.send('getGithubUser', 'main');
};

function todate(timestamp) {
Expand Down Expand Up @@ -135,6 +136,17 @@ ipcRenderer.on('hotkey-set-return', function (event, iftrue, id, message, hotkey

});

ipcRenderer.on('githubUser', function (event, data, newSession) {
if (data) {
//user is loggedIn via github
$('#loginGithubButton').hide()
$('#syncGistButton').show()
} else {
$('#loginGithubButton').show()
$('#syncGistButton').hide()
}
})

function SetHotKey(element) {

element_parent = element.parentNode.parentNode.parentNode.parentNode;
Expand Down Expand Up @@ -178,6 +190,10 @@ function newSnip() {
ipcRenderer.send('new-snip');
};

function syncGists() {
ipcRenderer.send('sync-gists');
}

$(".arrow-down, .arrow-up").click(function(e){

if($(this).hasClass('arrow-up') ){
Expand All @@ -189,4 +205,8 @@ $(".arrow-down, .arrow-up").click(function(e){

$(this).toggleClass('arrow-down arrow-up')

})
})

function loginViaGithub() {
ipcRenderer.send('github-login');
}
12 changes: 11 additions & 1 deletion public_static/snip.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ <h4 class="modal-title custom_align" id="Heading">New Snip</h4>
</div>
<div class="form-group">
<div id="editor" class="form-control" style="height: 300px"></div>
</div>
<div class="form-group">
<div class="checkbox checkbox-primary">
<input type="checkbox" id="makeGistCheckbox">
<label for="makeGistCheckbox">Make a gist</label>

</div>
</div>
</div>
<div class="modal-footer ">
Expand All @@ -51,6 +57,10 @@ <h4 class="modal-title custom_align" id="Heading">New Snip</h4>
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
</script>

<style type="text/css">
#makeGistCheckbox {
margin-left: 5px;
}
</style>
</body>
</html>
18 changes: 17 additions & 1 deletion public_static/snip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
*/
const {ipcRenderer} = require('electron')

window.onload = function () {
ipcRenderer.send('getGithubUser')
}

function newSnip() {
let title = document.getElementById("title");
let language = document.getElementById("language");
let makeGist = document.getElementById("makeGistCheckbox").checked;
let code = ace.edit("editor");

const snip = {
"title": title.value,
"language": language.value,
"code": code.getValue(),
"timestamp": Math.floor(Date.now() / 1000),
"hotkey": null
"hotkey": null,
"makeGist": makeGist
};
ipcRenderer.send('new-snip-add', JSON.stringify(snip))
}

function closeWin() {
ipcRenderer.send('close-snip-win');
}

ipcRenderer.on('githubUser', function (event, data, isNewSession) {
console.log(data)
if (data) {
//user is loggedIn via github
$('#gistCheckbox').show()
} else {
$('#gistCheckbox').hide()
}
})
6 changes: 6 additions & 0 deletions secrets-sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
github: {
clientId: process.env.GITHUB_CLIENT_ID || "get oauth2 client id from github",
secret: process.env.GITHUB_SECRET || "get oauth2 client secret from github"
}
}
18 changes: 18 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Created by abhishek on 29/07/17.
*/
'use strict';

module.exports = {
now () {
let dateString = "";
let newDate = new Date();

dateString += (newDate.getMonth() + 1) + "-";
dateString += newDate.getDate() + "-";
dateString += newDate.getFullYear();

return dateString;
},

}
Loading

0 comments on commit 20d74a6

Please sign in to comment.