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.

145 lines
4.3 KiB

  1. <?php
  2. namespace App;
  3. use Movim\Model;
  4. class Subscription extends Model
  5. {
  6. use \Awobaz\Compoships\Compoships;
  7. public $incrementing = false;
  8. protected $primaryKey = ['jid', 'server', 'node'];
  9. protected $guarded = [];
  10. public const PUBLIC_NODE = 'urn:xmpp:pubsub:subscription';
  11. public const SPACE_NODE = '{https://movim.eu}spaces_subscriptions_node';
  12. public const PRIVATE_NODE = 'urn:xmpp:pubsub:movim-public-subscription';
  13. public const SUBSCRIPTION_XMLNS = 'urn:xmpp:pubsub:subscription:0';
  14. public static function saveMany(array $subscriptions)
  15. {
  16. return Subscription::insert($subscriptions);
  17. }
  18. public function info()
  19. {
  20. return $this->hasOne(Info::class, ['server', 'node'], ['server', 'node']);
  21. }
  22. public function contact()
  23. {
  24. return $this->hasOne(Contact::class, 'id', 'jid');
  25. }
  26. public function spaceRooms()
  27. {
  28. return $this->hasMany(Conference::class, ['space_server', 'space_node', 'user_id'], ['server', 'node', 'jid'])
  29. ->orderBy('pinned', 'desc')
  30. ->orderBy('name', 'asc')
  31. ->withCount('unreads', 'quoted');
  32. }
  33. public function setExtensions(?\SimpleXMLElement $extensions = null)
  34. {
  35. if ($extensions) {
  36. if (
  37. $extensions->notify
  38. && $extensions->notify->attributes()->xmlns == Conference::XMLNS_NOTIFICATIONS
  39. ) {
  40. if ($extensions->notify->never) {
  41. $this->notify = 0;
  42. }
  43. if ($extensions->notify->{'on-mention'}) {
  44. $this->notify = 1;
  45. }
  46. if ($extensions->notify->always) {
  47. $this->notify = 2;
  48. }
  49. unset($extensions->notify);
  50. }
  51. if (
  52. $extensions->pinned
  53. && $extensions->pinned->attributes()->xmlns == Conference::XMLNS_PINNED
  54. ) {
  55. $this->pinned = true;
  56. unset($extensions->pinned);
  57. }
  58. $this->extensions = $extensions->asXML();
  59. }
  60. }
  61. public function spaceAffiliations()
  62. {
  63. return $this->hasMany(Affiliation::class, ['server', 'node'], ['server', 'node']);
  64. }
  65. public function getNotifyAttribute(): ?string
  66. {
  67. return is_int($this->attributes['notify'])
  68. ? Conference::NOTIFICATIONS[$this->attributes['notify']]
  69. : null;
  70. }
  71. public function getCounterIdAttribute(): string
  72. {
  73. return cleanupId($this->server . $this->node . '-counter');
  74. }
  75. public function getUriAttribute(): string
  76. {
  77. return 'xmpp:' . $this->server . '?;node=' . $this->node;
  78. }
  79. public function spaceUnreads(User $user): int
  80. {
  81. return $user->unreads(space: [$this->server, $this->node]);
  82. }
  83. public function scopeSpaces($query, ?bool $yes = true)
  84. {
  85. return $query->where('space', $yes);
  86. }
  87. public function scopeSpace($query, string $server, string $node)
  88. {
  89. return $query->where('space', true)
  90. ->where('server', $server)
  91. ->where('node', $node);
  92. }
  93. public function scopeCommunities($query)
  94. {
  95. return $query->where('node', '!=', Post::MICROBLOG_NODE);
  96. }
  97. public function scopeNotComments($query)
  98. {
  99. return $query->where('node', 'not like', Post::COMMENTS_NODE . '/%');
  100. }
  101. public function toArray()
  102. {
  103. $now = \Carbon\Carbon::now();
  104. return [
  105. 'jid' => $this->attributes['jid'] ?? null,
  106. 'server' => $this->attributes['server'] ?? null,
  107. 'node' => $this->attributes['node'] ?? null,
  108. 'subid' => $this->attributes['subid'] ?? null,
  109. 'title' => $this->attributes['title'] ?? null,
  110. 'public' => $this->attributes['public'] ?? false,
  111. 'space' => $this->attributes['space'] ?? false,
  112. 'space_in' => $this->attributes['space_in'] ?? false,
  113. 'created_at' => $this->attributes['created_at'] ?? $now,
  114. 'updated_at' => $this->attributes['updated_at'] ?? $now,
  115. 'pinned' => $this->attributes['pinned'] ?? false,
  116. 'extensions' => $this->attributes['extensions'] ?? null,
  117. 'notify' => $this->attributes['notify'] ?? 1,
  118. ];
  119. }
  120. }