1 | _ = require "underscore"
|
2 | Utils = require "../utils"
|
3 |
|
4 | class GenericAdapter
|
5 | @JIRA_NOTIFICATIONS_DISABLED: "jira-notifications-disabled"
|
6 | @JIRA_DM_COUNTS: "jira-dm-counts"
|
7 |
|
8 | constructor: (@robot) ->
|
9 | @disabledUsers = null
|
10 | @dmCounts = null
|
11 |
|
12 | @robot.brain.once "loaded", =>
|
13 | @disabledUsers = @robot.brain.get(GenericAdapter.JIRA_NOTIFICATIONS_DISABLED) or []
|
14 | @dmCounts = @robot.brain.get(GenericAdapter.JIRA_DM_COUNTS) or {}
|
15 |
|
16 | disableNotificationsFor: (user) ->
|
17 | @robot.logger.info "Disabling JIRA notifications for #{user.name}"
|
18 | @disabledUsers.push user.id
|
19 | @robot.brain.set GenericAdapter.JIRA_NOTIFICATIONS_DISABLED, _(@disabledUsers).unique()
|
20 | @robot.brain.save()
|
21 |
|
22 | enableNotificationsFor: (user) ->
|
23 | @robot.logger.info "Enabling JIRA notifications for #{user.name}"
|
24 | @disabledUsers = _(@disabledUsers).without user.id
|
25 | @robot.brain.set GenericAdapter.JIRA_NOTIFICATIONS_DISABLED, @disabledUsers
|
26 | @robot.brain.save()
|
27 |
|
28 | incrementDMCountFor: (user) ->
|
29 | return unless @dmCounts?
|
30 | return unless user?.id?
|
31 |
|
32 | @dmCounts[user.id] ||= 0
|
33 | @dmCounts[user.id]++
|
34 | @robot.brain.set GenericAdapter.JIRA_DM_COUNTS, @dmCounts
|
35 | @robot.brain.save()
|
36 |
|
37 | getDMCountFor: (user) ->
|
38 | return 0 unless @dmCounts?
|
39 | return 0 unless user?.id?
|
40 | @dmCounts[user.id] ||= 0
|
41 | return @dmCounts[user.id]
|
42 |
|
43 | send: (context, message) ->
|
44 | room = @getRoom context
|
45 | return unless room
|
46 |
|
47 | if _(message).isString()
|
48 | payload = message
|
49 | else if message.text or message.attachments
|
50 | payload = ""
|
51 |
|
52 | if message.text
|
53 | payload += "#{message.text}\n"
|
54 |
|
55 | if message.attachments
|
56 | for attachment in message.attachments
|
57 | payload += "#{attachment.fallback}\n"
|
58 | else
|
59 | Utils.Stats.increment "jirabot.message.empty"
|
60 | return @robot.logger.error "Unable to find a message to send", message
|
61 |
|
62 | @robot.send room: room.id, payload
|
63 |
|
64 | dm: (users, message) ->
|
65 | users = [ users ] unless _(users).isArray()
|
66 | for user in users when user
|
67 | if _(@disabledUsers).contains user.id
|
68 | Utils.Stats.increment "jirabot.surpress.notification"
|
69 | @robot.logger.debug "JIRA Notification surpressed for #{user.name}"
|
70 | else
|
71 | if message.author? and user.profile?.email is message.author.emailAddress
|
72 | @robot.logger.debug "JIRA Notification surpressed for #{user.name} because it would be a self-notification"
|
73 | continue
|
74 | message.text += "\n#{message.footer}" if message.text and message.footer and @getDMCountFor(user) < 3
|
75 | @send message: room: user.id, _(message).pick "attachments", "text"
|
76 | @incrementDMCountFor user
|
77 |
|
78 | getPermalink: (context) -> ""
|
79 |
|
80 | normalizeContext: (context) ->
|
81 | if _(context).isString()
|
82 | normalized = message: room: context
|
83 | else if context?.room
|
84 | normalized = message: context
|
85 | else if context?.message?.room
|
86 | normalized = context
|
87 | normalized
|
88 |
|
89 | getRoom: (context) ->
|
90 | context = @normalizeContext context
|
91 | id: context.message.room
|
92 | name: context.message.room
|
93 |
|
94 | getRoomName: (context) ->
|
95 | room = @getRoom context
|
96 | room.name
|
97 |
|
98 | getUsers: ->
|
99 | @robot.brain.users()
|
100 |
|
101 | module.exports = GenericAdapter
|