Likes-Notify.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * This task handles notifying users when something is liked.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. class Likes_Notify_Background extends SMF_BackgroundTask
  15. {
  16. public function execute()
  17. {
  18. global $smcFunc, $sourcedir;
  19. $author = false;
  20. // We need to figure out who the owner of this is.
  21. if ($this->_details['content_type'] == 'msg')
  22. {
  23. $request = $smcFunc['db_query']('', '
  24. SELECT mem.id_member, mem.id_group, mem.id_post_group, mem.additional_groups, b.member_groups
  25. FROM {db_prefix}messages AS m
  26. INNER JOIN {db_prefix}members AS mem ON (m.id_member = mem.id_member)
  27. INNER JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
  28. WHERE id_msg = {int:msg}',
  29. array(
  30. 'msg' => $this->_details['content_id'],
  31. )
  32. );
  33. if ($row = $smcFunc['db_fetch_assoc']($request))
  34. {
  35. // Before we assign the author, let's just check that the author can see the board this is in...
  36. // as it'd suck to notify someone their post was liked when in a board they can't see.
  37. $groups = explode(',', $row['additional_groups']);
  38. $groups[] = $row['id_group'];
  39. $groups[] = $row['id_post_group'];
  40. $allowed = explode(',', $row['member_groups']);
  41. // If the user is in group 1 anywhere, they can see everything anyway.
  42. if (in_array(1, $groups) || count(array_intersect($allowed, $groups)) != 0)
  43. $author = $row['id_member'];
  44. }
  45. $smcFunc['db_free_result']($request);
  46. }
  47. else
  48. {
  49. // This isn't something we know natively how to support. Call the hooks, if they're dealing with it, return false, otherwise return the user id.
  50. $hook_results = call_integration_hook('integrate_find_like_author', array($this->_details['content_type'], $this->_details['content_id']));
  51. foreach ($hook_results as $result)
  52. if (!empty($result))
  53. {
  54. $author = $result;
  55. break;
  56. }
  57. }
  58. // If we didn't have a member... leave.
  59. if (empty($author))
  60. return true;
  61. // If the person who sent the notification is the person whose content it is, do nothing.
  62. if ($author == $this->_details['sender_id'])
  63. return true;
  64. require_once($sourcedir . '/Subs-Notify.php');
  65. $prefs = getNotifyPrefs($author, $this->_details['content_type'] . '_like', true);
  66. // The likes setup doesn't support email notifications because that would be too many emails.
  67. // As a result, the value should really just be non empty.
  68. // Check the value. If no value or it's empty, they didn't want alerts, oh well.
  69. if (empty($prefs[$author][$this->_details['content_type'] . '_like']))
  70. return true;
  71. // Issue, update, move on.
  72. $smcFunc['db_insert']('insert',
  73. '{db_prefix}user_alerts',
  74. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
  75. 'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  76. array($this->_details['time'], $author, $this->_details['sender_id'], $this->_details['sender_name'],
  77. $this->_details['content_type'], $this->_details['content_id'], 'like', 0, ''),
  78. array('id_alert')
  79. );
  80. updateMemberData($author, array('alerts' => '+'));
  81. return true;
  82. }
  83. }
  84. ?>