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.

188 lines
5.9 KiB

  1. <?php
  2. namespace App;
  3. use Awobaz\Compoships\Database\Eloquent\Model;
  4. use Illuminate\Database\Capsule\Manager as DB;
  5. class Session extends Model
  6. {
  7. protected $fillable = ['id'];
  8. protected $keyType = 'string';
  9. protected $with = ['serverCapability'];
  10. public $incrementing = false;
  11. public function user()
  12. {
  13. return $this->belongsTo(User::class);
  14. }
  15. public function presences()
  16. {
  17. return $this->hasMany(Presence::class);
  18. }
  19. public function ownPresences()
  20. {
  21. return $this->hasMany(Presence::class, ['jid', 'session_id'], ['user_id', 'id']);
  22. }
  23. public function presence()
  24. {
  25. return $this->hasOne(Presence::class, ['jid', 'resource', 'session_id'], ['user_id', 'resource', 'id']);
  26. }
  27. public function serverCapability()
  28. {
  29. return $this->hasOne(Info::class, 'server', 'host')->where('node', '');
  30. }
  31. public function contacts()
  32. {
  33. return $this->hasMany(Roster::class);
  34. }
  35. public function conferences()
  36. {
  37. return $this->hasMany(Conference::class)->orderBy('conference');
  38. }
  39. public function topContacts()
  40. {
  41. return $this->contacts()->leftJoinSub(
  42. DB::table('messages')
  43. ->select('jidfrom as id', DB::raw('count(*) as number'))
  44. ->where('published', '>=', date('Y-m-d', strtotime('-4 week')))
  45. ->where('type', 'chat')
  46. ->where('user_id', $this->user_id)
  47. ->groupBy('jidfrom'),
  48. 'top',
  49. function ($join) {
  50. $join->on('top.id', 'rosters.jid');
  51. }
  52. )
  53. ->orderByRaw('-top.number')
  54. ->orderBy('jid');
  55. }
  56. public function topContactsToChat()
  57. {
  58. return $this->topContacts()->join(DB::raw('(
  59. select min(value) as value, jid as pjid
  60. from presences
  61. group by jid) as presences
  62. '), 'presences.pjid', '=', 'rosters.jid')
  63. ->where('value', '<', 5)
  64. ->whereNotIn('rosters.jid', function ($query) {
  65. $query->select('jid')
  66. ->from('open_chats')
  67. ->where('user_id', $this->user_id);
  68. })
  69. ->where('rosters.jid', '!=', $this->user_id)
  70. ->with('presence.capability');
  71. }
  72. /**
  73. * @brief Communities subscribed by my contacts that the session is not part of
  74. */
  75. public function interestingCommunities(int $limit = 10)
  76. {
  77. $params = [$this->id, $this->user_id, $limit];
  78. $where = "(server, node) in (
  79. select server, node from (
  80. select count(*) as count, subscriptions.server, subscriptions.node, recents.published
  81. from subscriptions
  82. join (select max(published) as published, server, node
  83. from posts
  84. group by server, node) as recents on recents.server = subscriptions.server and recents.node = subscriptions.node
  85. where public = true
  86. and jid in (
  87. select jid from rosters where session_id = ?
  88. )
  89. and (subscriptions.server, subscriptions.node) not in (select server, node from subscriptions where jid = ?)
  90. group by subscriptions.server, subscriptions.node, published
  91. order by published desc, count desc
  92. limit ?
  93. ) as sub";
  94. $configuration = Configuration::get();
  95. if ($configuration->restrictsuggestions) {
  96. array_push($params, '%.' . $this->user->session->host);
  97. $where .= " where server like ?";
  98. }
  99. $where .= ')';
  100. return Info::whereRaw($where, $params);
  101. }
  102. public function init(string $username, string $password, string $host, string $sessionId, string $timezone)
  103. {
  104. $this->id = $sessionId;
  105. $this->timezone = $timezone;
  106. $this->host = $host;
  107. $this->username = $username;
  108. $this->user_id = $username . '@' . $host;
  109. $this->resource = 'movim' . \generateKey();
  110. $this->hash = password_hash(Session::hashSession($this->username, $password, $this->host), PASSWORD_DEFAULT);
  111. $this->active = false;
  112. }
  113. public function getUploadService()
  114. {
  115. return Info::where(function ($query) {
  116. $query->where('parent', $this->host)
  117. ->orWhere('server', $this->host);
  118. })
  119. ->whereCategory('store')
  120. ->whereType('file')
  121. ->where('features', 'like', '%urn:xmpp:http:upload:0%')
  122. ->first();
  123. }
  124. public function getChatroomsServices()
  125. {
  126. return Info::where('parent', $this->host)
  127. ->whereCategory('conference')
  128. ->whereType('text')
  129. ->whereDoesntHave('identities', function ($query) {
  130. $query->where('category', 'gateway');
  131. })
  132. ->get();
  133. }
  134. public function getMujiService()
  135. {
  136. return Info::where('parent', $this->host)
  137. ->whereCategory('conference')
  138. ->whereType('text')
  139. ->whereDoesntHave('identities', function ($query) {
  140. $query->where('category', 'gateway');
  141. })
  142. ->first();
  143. }
  144. public function getCommentsService()
  145. {
  146. return Info::where('server', 'comments.' . $this->host)
  147. ->where('parent', $this->host)
  148. ->whereCategory('pubsub')
  149. ->whereType('service')
  150. ->first();
  151. }
  152. public function getSpacesService()
  153. {
  154. return Info::where('server', 'spaces.' . $this->host)
  155. ->where('parent', $this->host)
  156. ->whereCategory('pubsub')
  157. ->whereType('service')
  158. ->first();
  159. }
  160. public static function hashSession(string $username, string $password, string $host): string
  161. {
  162. return $username . "\e" . $password . "\e" . $host;
  163. }
  164. }