<commit_before>time = require 'time'

module.exports = class AlarmManager

    timeouts: {}

    constructor: (@timezone, @Alarm, @notificationhelper) ->

        # We load the existing alarms
        @Alarm.all (err, alarms) =>
            for alarm in alarms
                @addAlarmCounter alarm

    handleAlarm: (event, msg) =>

        if event is "alarm.create"
            @Alarm.find msg, (err, alarm) =>
                @addAlarmCounter alarm
        else if event is "alarm.update"
            @Alarm.find msg, (err, alarm) =>
                @updateAlarmCounter alarm
        else if event is "alarm.delete"
            @removeAlarmCounter msg

    addAlarmCounter: (alarm) ->

        triggerDate = new time.Date(alarm.trigg)
        triggerDate.setTimezone(@timezone)
        now = new time.Date()
        now.setTimezone(@timezone)

        delta = triggerDate.getTime() - now.getTime()
        #console.info delta
        #console.info triggerDate.toString()
        #console.info now.toString()
        #console.info "#{delta} ??"
        if delta > 0

            console.info "Notification in #{delta/1000} seconds."
            @timeouts[alarm._id] = setTimeout((
                () =>
                    @notificationhelper.createTemporary
                        text: "Reminder: #{alarm.description}"
                        resource:
                            app: 'agenda'
                            url: "alarms/#{alarm._id}"
                ), delta)
<commit_msg>use alarm.related when creating notification<commit_after>time = require 'time'

module.exports = class AlarmManager

    timeouts: {}

    constructor: (@timezone, @Alarm, @notificationhelper) ->

        # We load the existing alarms
        @Alarm.all (err, alarms) =>
            for alarm in alarms
                @addAlarmCounter alarm

    handleAlarm: (event, msg) =>

        if event is "alarm.create"
            @Alarm.find msg, (err, alarm) =>
                @addAlarmCounter alarm
        else if event is "alarm.update"
            @Alarm.find msg, (err, alarm) =>
                @updateAlarmCounter alarm
        else if event is "alarm.delete"
            @removeAlarmCounter msg

    addAlarmCounter: (alarm) ->

        triggerDate = new time.Date(alarm.trigg)
        triggerDate.setTimezone(@timezone)
        now = new time.Date()
        now.setTimezone(@timezone)

        delta = triggerDate.getTime() - now.getTime()
        #console.info delta
        #console.info triggerDate.toString()
        #console.info now.toString()
        #console.info "#{delta} ??"
        if delta > 0

            console.info "Notification in #{delta/1000} seconds."
            @timeouts[alarm._id] = setTimeout((
                () =>
                    resource = if alarm.related? then alarm.related
                    else
                        app: 'agenda'
                        url: "/" #TODO go to the alarm itself

                    @notificationhelper.createTemporary
                        text: "Reminder: #{alarm.description}"
                        resource: resource

                ), delta)

{'fraction_non_alphanumeric': 0.10294117647058823, 'fraction_numerical': 0.00319693094629156, 'mean_word_length': 1.6184100418410041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7426962', 'n_tokens_mistral': 866, 'n_tokens_neox': 813, 'n_words': 210}