NotificationService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\{
  6. Notification,
  7. Profile
  8. };
  9. use App\Transformer\Api\NotificationTransformer;
  10. use League\Fractal;
  11. use League\Fractal\Serializer\ArraySerializer;
  12. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  13. class NotificationService {
  14. const CACHE_KEY = 'pf:services:notifications:ids:';
  15. const MASTODON_TYPES = [
  16. 'follow',
  17. 'follow_request',
  18. 'mention',
  19. 'reblog',
  20. 'favourite',
  21. 'poll',
  22. 'status'
  23. ];
  24. public static function get($id, $start = 0, $stop = 400)
  25. {
  26. $res = collect([]);
  27. $key = self::CACHE_KEY . $id;
  28. $stop = $stop > 400 ? 400 : $stop;
  29. $ids = Redis::zrangebyscore($key, $start, $stop);
  30. if(empty($ids)) {
  31. $ids = self::coldGet($id, $start, $stop);
  32. }
  33. foreach($ids as $id) {
  34. $n = self::getNotification($id);
  35. if($n != null) {
  36. $res->push($n);
  37. }
  38. }
  39. return $res;
  40. }
  41. public static function coldGet($id, $start = 0, $stop = 400)
  42. {
  43. $stop = $stop > 400 ? 400 : $stop;
  44. $ids = Notification::whereProfileId($id)
  45. ->latest()
  46. ->skip($start)
  47. ->take($stop)
  48. ->pluck('id');
  49. foreach($ids as $key) {
  50. self::set($id, $key);
  51. }
  52. return $ids;
  53. }
  54. public static function getMax($id = false, $start = 0, $limit = 10)
  55. {
  56. $ids = self::getRankedMaxId($id, $start, $limit);
  57. if(empty($ids)) {
  58. return [];
  59. }
  60. $res = collect([]);
  61. foreach($ids as $id) {
  62. $n = self::getNotification($id);
  63. if($n != null) {
  64. $res->push($n);
  65. }
  66. }
  67. return $res->toArray();
  68. }
  69. public static function getMin($id = false, $start = 0, $limit = 10)
  70. {
  71. $ids = self::getRankedMinId($id, $start, $limit);
  72. if(empty($ids)) {
  73. return [];
  74. }
  75. $res = collect([]);
  76. foreach($ids as $id) {
  77. $n = self::getNotification($id);
  78. if($n != null) {
  79. $res->push($n);
  80. }
  81. }
  82. return $res->toArray();
  83. }
  84. public static function getMaxMastodon($id = false, $start = 0, $limit = 10)
  85. {
  86. $ids = self::getRankedMaxId($id, $start, $limit);
  87. if(empty($ids)) {
  88. return [];
  89. }
  90. $res = collect([]);
  91. foreach($ids as $id) {
  92. $n = self::getNotification($id);
  93. if($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
  94. $n['account'] = AccountService::getMastodon($n['account']['id']);
  95. if(isset($n['relationship'])) {
  96. unset($n['relationship']);
  97. }
  98. if(isset($n['status'])) {
  99. $n['status'] = StatusService::getMastodon($n['status']['id'], false);
  100. }
  101. $res->push($n);
  102. }
  103. }
  104. return $res->toArray();
  105. }
  106. public static function getMinMastodon($id = false, $start = 0, $limit = 10)
  107. {
  108. $ids = self::getRankedMinId($id, $start, $limit);
  109. if(empty($ids)) {
  110. return [];
  111. }
  112. $res = collect([]);
  113. foreach($ids as $id) {
  114. $n = self::getNotification($id);
  115. if($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
  116. $n['account'] = AccountService::getMastodon($n['account']['id']);
  117. if(isset($n['relationship'])) {
  118. unset($n['relationship']);
  119. }
  120. if(isset($n['status'])) {
  121. $n['status'] = StatusService::getMastodon($n['status']['id'], false);
  122. }
  123. $res->push($n);
  124. }
  125. }
  126. return $res->toArray();
  127. }
  128. public static function getRankedMaxId($id = false, $start = null, $limit = 10)
  129. {
  130. if(!$start || !$id) {
  131. return [];
  132. }
  133. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
  134. 'withscores' => true,
  135. 'limit' => [1, $limit]
  136. ]));
  137. }
  138. public static function getRankedMinId($id = false, $end = null, $limit = 10)
  139. {
  140. if(!$end || !$id) {
  141. return [];
  142. }
  143. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
  144. 'withscores' => true,
  145. 'limit' => [0, $limit]
  146. ]));
  147. }
  148. public static function set($id, $val)
  149. {
  150. return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
  151. }
  152. public static function del($id, $val)
  153. {
  154. Cache::forget('service:notification:' . $val);
  155. return Redis::zrem(self::CACHE_KEY . $id, $val);
  156. }
  157. public static function add($id, $val)
  158. {
  159. return self::set($id, $val);
  160. }
  161. public static function rem($id, $val)
  162. {
  163. return self::del($id, $val);
  164. }
  165. public static function count($id)
  166. {
  167. return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
  168. }
  169. public static function getNotification($id)
  170. {
  171. return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) {
  172. $n = Notification::with('item')->find($id);
  173. if(!$n) {
  174. return null;
  175. }
  176. $fractal = new Fractal\Manager();
  177. $fractal->setSerializer(new ArraySerializer());
  178. $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
  179. return $fractal->createData($resource)->toArray();
  180. });
  181. }
  182. public static function setNotification(Notification $notification)
  183. {
  184. return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) {
  185. $fractal = new Fractal\Manager();
  186. $fractal->setSerializer(new ArraySerializer());
  187. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  188. return $fractal->createData($resource)->toArray();
  189. });
  190. }
  191. public static function warmCache($id, $stop = 400, $force = false)
  192. {
  193. if(self::count($id) == 0 || $force == true) {
  194. $ids = Notification::whereProfileId($id)
  195. ->latest()
  196. ->limit($stop)
  197. ->pluck('id');
  198. foreach($ids as $key) {
  199. self::set($id, $key);
  200. }
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. }