This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into terminal
- Loading branch information
Showing
5 changed files
with
73 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = fetchWrapper = (url) -> | ||
fetch(url) | ||
module.exports = fetchWrapper = (url, init) -> | ||
fetch(url, init) | ||
.then (response) -> response.text() | ||
.then (body) -> JSON.parse(body) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
path = require 'path' | ||
fetch = require '../fetch' | ||
{learnCo} = require '../config' | ||
submissionRegistry = [] | ||
cachedLessonTitles = {} | ||
|
||
getLessonTitle = (lessonID) -> | ||
title = cachedLessonTitles[lessonID] | ||
|
||
if title? | ||
return Promise.resolve(title) | ||
|
||
fetch("#{learnCo}/api/v1/lessons/#{lessonID}").then ({title}) => | ||
cachedLessonTitles[lessonID] = title || 'Learn IDE' | ||
return title | ||
|
||
icon = (passing) -> | ||
pass = path.resolve(__dirname, '..', '..', 'static', 'images', 'pass.png') | ||
fail = path.resolve(__dirname, '..', '..', 'static', 'images', 'fail.png') | ||
if passing is 'true' then pass else fail | ||
|
||
module.exports = ({submission_id, lesson_id, passing, message}) -> | ||
if not submissionRegistry.includes(submission_id) | ||
submissionRegistry.push(submission_id) | ||
|
||
getLessonTitle(lesson_id).then (title) => | ||
notif = new Notification(title, {body: message, icon: icon(passing)}) | ||
notif.onclick = -> notif.close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,37 @@ | ||
https = require 'https' | ||
querystring = require 'querystring' | ||
{EventEmitter} = require 'events' | ||
AtomSocket = require 'atom-socket' | ||
atomHelper = require './atom-helper' | ||
path = require 'path' | ||
fetch = require './fetch' | ||
{learnCo} = require './config' | ||
|
||
notificationStrategies = | ||
submission: require('./notifications/submission') | ||
|
||
module.exports = | ||
class Notifier extends EventEmitter | ||
constructor: (authToken) -> | ||
@authToken = authToken | ||
@notifRegistry = [] | ||
@notifTitles = {} | ||
@notificationTypes = ['submission'] | ||
class Notifier | ||
constructor: (token) -> | ||
@token = token | ||
|
||
activate: -> | ||
@authenticate().then ({id}) => | ||
@connect(id) | ||
|
||
@on 'notification-debug', (msg) -> | ||
console.log('NOTIFICATION DEBUG:', msg) | ||
|
||
@on 'new-notification', (data) => | ||
if atomHelper.isLastFocusedWindow() | ||
console.log('NOTIFICATION:', data) | ||
|
||
notif = new Notification data.displayTitle, | ||
body: data.message | ||
icon: @getIcon(data) | ||
|
||
notif.onclick = -> | ||
notif.close() | ||
|
||
@authenticate() | ||
.then => | ||
@connect() | ||
.catch (e) -> | ||
console.error 'error connecting to notification service:', e | ||
|
||
getIcon: (data) -> | ||
pass = path.resolve(__dirname, '..', 'static', 'images', 'pass.png') | ||
fail = path.resolve(__dirname, '..', 'static', 'images', 'fail.png') | ||
if data.passing is 'true' then pass else fail | ||
|
||
authenticate: => | ||
return new Promise (resolve, reject) => | ||
https.get | ||
host: 'learn.co' | ||
path: '/api/v1/users/me' | ||
headers: | ||
'Authorization': 'Bearer ' + @authToken | ||
, (response) => | ||
console.log 'NOTIFICATION RESPONSE:', response | ||
body = '' | ||
|
||
response.on 'data', (d) -> | ||
body += d | ||
|
||
response.on 'error', -> | ||
reject Error('Cannot subscribe to notifications. Problem parsing response.') | ||
|
||
response.on 'end', => | ||
try | ||
parsed = JSON.parse(body) | ||
|
||
if parsed.id | ||
@id = parsed.id | ||
|
||
resolve this | ||
else | ||
reject Error('Cannot subscribe to notifications. Not authorized.') | ||
catch | ||
reject Error('Cannot subscribe to notifications. Problem parsing response.') | ||
|
||
connect: => | ||
@connection = new AtomSocket('notif', 'wss://push.flatironschool.com:9443/ws/fis-user-' + @id) | ||
|
||
@connection.on 'open', (e) => | ||
this.emit 'notification-debug', 'Listening for notifications...' | ||
|
||
@connection.on 'message', (data) => | ||
try | ||
rawData = JSON.parse(data) | ||
eventData = querystring.parse rawData.text | ||
uid = @eventUid eventData | ||
|
||
if @notificationTypes.indexOf(eventData.type) >= 0 && !(@notifRegistry.indexOf(uid) >= 0) | ||
@notifRegistry.push(uid) | ||
|
||
@getDisplayTitle(eventData).then (title) => | ||
eventData.displayTitle = title | ||
this.emit 'new-notification', eventData | ||
catch err | ||
console.log err | ||
this.emit 'notification-debug', 'Error creating notification.' | ||
|
||
getDisplayTitle: (event) => | ||
# NOTE THAT FOR NOW THIS ONLY WORKS WITH LESSONS | ||
return new Promise (resolve, reject) => | ||
try | ||
displayTitle = @notifTitles[event.lesson_id] | ||
|
||
if displayTitle | ||
resolve displayTitle | ||
else | ||
https.get | ||
host: 'learn.co' | ||
path: '/api/v1/lessons/' + event.lesson_id | ||
, (response) => | ||
body = '' | ||
|
||
response.on 'data', (d) -> | ||
body += d | ||
|
||
response.on 'error', -> | ||
@notifTitles[event.lesson_id] = 'Learn IDE' | ||
resolve 'Learn IDE' | ||
authenticate: -> | ||
headers = new Headers({'Authorization': "Bearer #{@token}"}) | ||
fetch("#{learnCo}/api/v1/users/me", {headers}) | ||
|
||
response.on 'end', => | ||
try | ||
parsed = JSON.parse(body) | ||
connect: (userID) -> | ||
@ws = new AtomSocket('notif', "wss://push.flatironschool.com:9443/ws/fis-user-#{userID}") | ||
|
||
if parsed.title | ||
@notifTitles[event.lesson_id] = parsed.title | ||
resolve parsed.title | ||
else | ||
@notifTitles[event.lesson_id] = 'Learn IDE' | ||
resolve 'Learn IDE' | ||
catch | ||
@notifTitles[event.lesson_id] = 'Learn IDE' | ||
resolve 'Learn IDE' | ||
catch err | ||
console.log err | ||
@ws.on 'message', (msg) => | ||
@parseMessage(JSON.parse(msg)) | ||
|
||
eventUid: (event) => | ||
switch event.type | ||
when 'submission' then parseInt(event.submission_id) | ||
else null | ||
parseMessage: ({text}) -> | ||
if atomHelper.isLastFocusedWindow() | ||
data = querystring.parse(text) | ||
callback = notificationStrategies[data.type] | ||
callback?(data) | ||
|
||
deactivate: -> | ||
@connection.close() | ||
@ws.close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters