From 734e89259b1cd7e719c6e3fdc641d9bcc8b44a2f Mon Sep 17 00:00:00 2001
From: Daniel Supernault <danielsupernault@gmail.com>
Date: Mon, 10 Jun 2019 20:11:52 -0600
Subject: [PATCH] Add PublicTimelineService

---
 app/Services/PublicTimelineService.php | 67 ++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 app/Services/PublicTimelineService.php

diff --git a/app/Services/PublicTimelineService.php b/app/Services/PublicTimelineService.php
new file mode 100644
index 000000000..ded2fa247
--- /dev/null
+++ b/app/Services/PublicTimelineService.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Services;
+
+use Redis;
+use App\{
+	Profile,
+	Status,
+	UserFilter
+};
+
+class PublicTimelineService {
+
+	const CACHE_KEY = 'pf:services:timeline:public';
+
+	public static function get($start = 0, $stop = 10)
+	{
+		if($stop > 100) {
+			$stop = 100;
+		}
+		$tl = [];
+		$keys = Redis::zrevrange(self::CACHE_KEY, $start, $stop);
+		foreach($keys as $key) {
+			array_push($tl, StatusService::get($key));
+		}
+		return $tl;
+	}
+
+	public static function add($val)
+	{
+		return Redis::zadd(self::CACHE_KEY, 1, $val);
+	}
+
+	public static function rem($val)
+	{
+		return Redis::zrem(self::CACHE_KEY, $val);
+	}
+
+	public static function del($val)
+	{
+		return self::rem($val);
+	}
+
+	public static function count()
+	{
+		return Redis::zcount(self::CACHE_KEY, '-inf', '+inf');
+	}
+
+	public static function warmCache($force = false, $limit = 100)
+	{
+		if(self::count() == 0 || $force == true) {
+			$ids = Status::whereNull('uri')
+				->whereNull('in_reply_to_id')
+				->whereNull('reblog_of_id')
+				->whereIn('type', ['photo', 'photo:album'])
+				->whereScope('public')
+				->latest()
+				->limit($limit)
+				->pluck('id');
+			foreach($ids as $id) {
+				self::add($id);
+			}
+			return 1;
+		}
+		return 0;
+	}
+}
\ No newline at end of file