Group_message_profile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Who received a group message
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  27. /**
  28. * Data class for group direct messages for users
  29. *
  30. * @category GroupPrivateMessage
  31. * @package GNUsocial
  32. * @author Evan Prodromou <evan@status.net>
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. *
  35. * @see DB_DataObject
  36. */
  37. class Group_message_profile extends Managed_DataObject
  38. {
  39. public $__table = 'group_message_profile'; // table name
  40. public $to_profile; // int
  41. public $group_message_id; // varchar(36) primary_key not_null
  42. public $created; // datetime() not_null
  43. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  44. public static function schemaDef()
  45. {
  46. return array(
  47. 'fields' => array(
  48. 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'id of group direct message'),
  49. 'group_message_id' => array('type' => 'char', 'not null' => true, 'length' => 36, 'description' => 'related group message uuid'),
  50. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  51. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  52. ),
  53. 'primary key' => array('to_profile', 'group_message_id'),
  54. 'foreign keys' => array(
  55. 'group_message_profile_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
  56. 'group_message_profile_group_message_id_fkey' => array('group_message', array('group_message_id' => 'id')),
  57. ),
  58. 'indexes' => array(
  59. 'group_message_profile_group_message_id_idx' => array('group_message_id'),
  60. ),
  61. );
  62. }
  63. public function send($gm, $profile)
  64. {
  65. $gmp = new Group_message_profile();
  66. $gmp->group_message_id = $gm->id;
  67. $gmp->to_profile = $profile->id;
  68. $gmp->created = common_sql_now();
  69. $gmp->insert();
  70. // If it's not for the author, send email notification
  71. if ($gm->from_profile != $profile->id) {
  72. $gmp->notify();
  73. }
  74. return $gmp;
  75. }
  76. public function notify()
  77. {
  78. // XXX: add more here
  79. $this->notifyByMail();
  80. }
  81. public function notifyByMail()
  82. {
  83. $to = User::getKV('id', $this->to_profile);
  84. if (empty($to) || is_null($to->email) || !$to->emailnotifymsg) {
  85. return true;
  86. }
  87. $gm = Group_message::getKV('id', $this->group_message_id);
  88. $from_profile = Profile::getKV('id', $gm->from_profile);
  89. $group = $gm->getGroup();
  90. common_switch_locale($to->language);
  91. // TRANS: Subject for direct-message notification email.
  92. // TRANS: %1$s is the sending user's nickname, %2$s is the group nickname.
  93. $subject = sprintf(_m('New private message from %1$s to group %2$s'), $from_profile->nickname, $group->nickname);
  94. // TRANS: Body for direct-message notification email.
  95. // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
  96. // TRANS: %3$s is the message content, %4$s a URL to the message,
  97. // TRANS: %5$s is the StatusNet sitename.
  98. $body = sprintf(
  99. _m("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n"
  100. . "------------------------------------------------------\n"
  101. . "%4\$s\n"
  102. . "------------------------------------------------------\n\n"
  103. . "You can reply to their message here:\n\n"
  104. . "%5\$s\n\n"
  105. . "Do not reply to this email; it will not get to them.\n\n"
  106. . "With kind regards,\n"
  107. . "%6\$s"),
  108. $from_profile->getBestName(),
  109. $from_profile->nickname,
  110. $group->nickname,
  111. $gm->content,
  112. common_local_url('newmessage', ['to' => $from_profile->id]),
  113. common_config('site', 'name')
  114. ) . "\n";
  115. $headers = _mail_prepare_headers('message', $to->nickname, $from_profile->nickname);
  116. common_switch_locale();
  117. return mail_to_user($to, $subject, $body, $headers);
  118. }
  119. }