You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9KB

  1. <?php
  2. if (!defined('GNUSOCIAL')) {
  3. exit(1);
  4. }
  5. class BrowserNotificationSettings extends Managed_DataObject
  6. {
  7. public $__table = 'browser_notifications'; // table name
  8. public $user_id; // int(10)
  9. public $enabled; // boolean
  10. public $mentions_only; // boolean
  11. public static function save($user, $settings)
  12. {
  13. $bns = new BrowserNotificationSettings();
  14. $bns->user_id = $user->id;
  15. $bns->enabled = $settings['enabled'];
  16. $bns->mentions_only = $settings['mentions_only'];
  17. // First time saving settings
  18. if (empty(self::getByUserId($user->id))) {
  19. $bns->insert();
  20. } else { // Updating existing settings
  21. $bns->update();
  22. }
  23. }
  24. public static function getDefaults()
  25. {
  26. return array(
  27. 'enabled' => true,
  28. 'mentions_only' => false
  29. );
  30. }
  31. public function toJSON()
  32. {
  33. return json_encode(array(
  34. 'enabled' => $this->enabled,
  35. 'mentions_only' => $this->mentions_only
  36. ));
  37. }
  38. public static function getByUserId($userid)
  39. {
  40. $user_settings = self::getKV('user_id', $userid);
  41. $user_settings->enabled = (boolean)$user_settings->enabled;
  42. $user_settings->mentions_only = (boolean)$user_settings->mentions_only;
  43. return $user_settings;
  44. }
  45. public static function schemaDef()
  46. {
  47. return array(
  48. 'fields' => array(
  49. 'user_id' => array('type' => 'int(10)', 'not null' => true),
  50. 'enabled' => array('type' => 'int', 'size' => 'tiny', 'default' => 1),
  51. 'mentions_only' => array('type' => 'int', 'size' => 'tiny', 'default' => 0),
  52. ),
  53. 'primary key' => array('user_id'),
  54. 'foreign keys' => array(
  55. 'browsernotifications_user_id_fkey' => array('user', array('user_id' => 'id'))
  56. )
  57. );
  58. }
  59. }