Skip to content
This repository has been archived by the owner on Sep 20, 2018. It is now read-only.

Import all calendars, refs #53 #63

Merged
merged 5 commits into from Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/locales/en.json
@@ -1,5 +1,5 @@
{
"notif_import_contact": "Import of %{total} contacts done",
"notif_import_photo": "Importation de %{total} photos terminée",
"notif_import_event": "Importation de %{total} évenements terminée"
"notif_import_photo": "Import of %{total} photos done",
"notif_import_event": "Import of %{total} events done"
}
37 changes: 26 additions & 11 deletions server/utils/import_calendar.coffee
Expand Up @@ -72,43 +72,58 @@ fetchCalendar = (calendarId, callback) ->
module.exports = (access_token, callback)->
oauth2Client.setCredentials access_token: access_token

getCalendarId (err, calendarId) ->
if err or not calendarId
calendar.calendarList.list auth: oauth2Client, (err, response) ->
if err
log.error err if err
return callback new Error 'cant get primary calendar'
return callback new Error 'cant get calendar'

totalNumber = 0
numberProcessed = 0
allEvents = []

# concat all events. Needed to get the total number of events to import
concatEvents = (calendarItem, next) ->
fetchCalendar calendarItem.id, (err, gEvents) ->
return next err if err

allEvents = allEvents.concat gEvents
next null

# @TODO fetch other calendars ?
fetchCalendar calendarId, (err, gEvents) ->
async.eachSeries response.items, concatEvents, (err) ->
return callback err if err

numberProcessed = 0
async.eachSeries gEvents, (gEvent, next)->
async.eachSeries allEvents, (gEvent, next)->
unless Event.validGoogleEvent gEvent
log.error "invalid event"
log.error gEvent

realtimer.sendCalendar
number: ++numberProcessed
total: gEvents.length
total: allEvents.length
next null
else
cozyEvent = Event.fromGoogleEvent gEvent
cozyEvent.tags = ['google calendar']
if gEvent.organizer?.displayName?
tag = "(Google) #{gEvent.organizer.displayName}"
else
tag = 'google calendar'
cozyEvent.tags = [ tag ]
log.debug "cozy create 1 event"
Event.createIfNotExist cozyEvent, (err) ->
return callback err if err
log.error err if err
realtimer.sendCalendar
number: ++numberProcessed
total: gEvents.length
total: allEvents.length

setTimeout next, 100
, (err)->
return callback err if err

log.info "create notification for events"
_ = localizationManager.t
notification.createOrUpdatePersistent "leave-google-calendar",
notification.createOrUpdatePersistent \
"leave-google-calendar",
app: 'import-from-google'
text: _ 'notif_import_event', total: numberProcessed
resource:
Expand Down