BrowserNotificationSettings.php 1.9 KB

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