uploader.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. Manages all uploads of image files.
  3. Focuses the upload through Slack or Mail, depending on configuration.
  4. :Author: Andreas Lindlbauer (@alindl)
  5. :Copyright: (c) 2021 University of Salzburg
  6. :Organization: Center for Human-Computer Interaction
  7. """
  8. __license__ = "GNU GPLv3"
  9. __docformat__ = 'reStructuredText'
  10. from time import time
  11. from .feedbacker import send_feedback, Message
  12. from .mailer import send_mail, send_reminder
  13. from .read_config import get_key, get_bool_key
  14. from .slack_processor import SlackBot
  15. def upload_to_slack(pictures, slack_bot):
  16. """Uploads the pictures to slack and a message depending on request situation.
  17. Checks the temp request file for a request and sends messages depending on,
  18. if there is a request.
  19. If the last request ran out, this person is getting notified.
  20. If not, tell person, trigger has been received.
  21. Upload to slack
  22. """
  23. channel = get_key('Slack', 'channel_name')
  24. request = slack_bot.queue.get()
  25. if request['request']:
  26. if time() <= request['ts']:
  27. message = "Received the trigger in time, you will get the photo!"
  28. channel = request['channel']
  29. else:
  30. # Hmm, this should never happen ... keep it in, just to be sure
  31. message = "Hey, sorry but you missed your request window, but I told you that."
  32. SlackBot.send_ephemeral_message(SlackBot,
  33. msg=message,
  34. user=request['user'],
  35. channel=request['channel'])
  36. # Make sure we pick the right file
  37. SlackBot.upload_file(SlackBot, pictures[0], channel)
  38. send_feedback(Message.UPLOAD)
  39. if get_bool_key('Output', 'enhance'):
  40. SlackBot.upload_file(SlackBot, pictures[1], channel)
  41. if request['request']:
  42. request['request'] = False
  43. request['user'] = None
  44. request['real_name'] = None
  45. request['channel'] = None
  46. request['ts'] = float(get_key('Slack', 'request_period'))
  47. slack_bot.queue.put(request)
  48. print("after slack upload")
  49. def upload_by_mail(pictures, mail_list):
  50. """Gather mail list, send task to upload."""
  51. addresses = []
  52. print(mail_list)
  53. # Mail can be activated but not mapped to this button
  54. if len(mail_list) > 0:
  55. print("mail list has mails in it")
  56. for mail_num in mail_list:
  57. addresses.append(get_key(mail_num, "address"))
  58. print("after action")
  59. send_mail(addresses, pictures)
  60. else:
  61. print("Mail activated but no mails choosen on this button")
  62. print("after mail upload")
  63. def get_upload_targets():
  64. """Construct and return unset dict of targets."""
  65. # Possible actions:
  66. # normal All available targets
  67. # all_mail All available emails
  68. # [slack]{, Mail#} Only specific emails and/or slack
  69. upload_targets = {'slack': False}
  70. for mail in range(int(get_key('Output', 'num_mail'))):
  71. upload_targets['mail_' + str(mail)] = False
  72. return upload_targets
  73. def set_upload_targets(actions):
  74. """Translate meta targets into actual targets, set them accordingly and return."""
  75. upload_targets = get_upload_targets()
  76. slack = get_bool_key('Output', 'slack')
  77. email = get_bool_key('Output', 'mail')
  78. # A little bit long but very explicit about what should happen
  79. if actions[0] == "normal":
  80. if slack:
  81. upload_targets['slack'] = True
  82. if email:
  83. for mail in range(int(get_key('Output', 'num_mail'))):
  84. upload_targets['Mail' + str(mail)] = True
  85. elif actions[0] == "all_mail":
  86. if email:
  87. for mail in range(int(get_key('Output', 'num_mail'))):
  88. upload_targets['Mail' + str(mail)] = True
  89. else:
  90. for action in actions:
  91. if action in upload_targets:
  92. upload_targets[action] = True
  93. return upload_targets
  94. def save_all_targets(all_targets, targets):
  95. """Set actual targets from button to dict."""
  96. # FIXME: Seems a bit convoluted.
  97. for action, activated in targets.items():
  98. all_targets[action] = activated or all_targets[action]
  99. return all_targets
  100. def send_reminders(all_targets):
  101. """Send reminders for unsent pictures because of internet outage."""
  102. mail_list = []
  103. msg = "There are unsent pictures on the Whiteboardbot SD-Card, \
  104. because of an Internet outage on the last trigger. \
  105. They are automatically going to get deleted in 30 days."
  106. for action, activated in all_targets.items():
  107. if activated:
  108. if action == 'slack':
  109. SlackBot.send_message(SlackBot, msg, attachment=None,
  110. channel=get_key("Slack", "channel_name"))
  111. else:
  112. mail_list.append(action)
  113. if len(mail_list) > 0:
  114. addresses = []
  115. print(mail_list)
  116. for mail_num in mail_list:
  117. addresses.append(get_key(mail_num, "address"))
  118. # TODO check if this handover worked correctly
  119. send_reminder(addresses, msg)
  120. return time()
  121. def upload(button, pictures, enhancer=None, slack_bot=None):
  122. """Check upload targets and upload accordingly."""
  123. # TODO Test this
  124. # bool(int('1'))) ? I know, I'm fun at parties
  125. email = get_bool_key('Output', 'mail')
  126. actions = eval(button['action'])
  127. upload_targets = set_upload_targets(actions)
  128. mail_list = []
  129. # Why trust positions of file names, if we could trust,
  130. # that files ready for upload are always *.jpg
  131. uploads = []
  132. for picture in pictures:
  133. if picture.find(".jpg") != -1:
  134. uploads.append(picture)
  135. if enhancer is not None:
  136. enhancer.join() # Waits until enhancer terminates
  137. for action, activated in upload_targets.items():
  138. if activated:
  139. if action == 'slack':
  140. upload_to_slack(uploads, slack_bot)
  141. else: # Can only be either slack or mail
  142. mail_list.append(action)
  143. if email:
  144. upload_by_mail(uploads, mail_list)
  145. print("done uploading")