diff --git a/functions/crawler/src/index.php b/functions/crawler/src/index.php index fb53614..cd6f25c 100644 --- a/functions/crawler/src/index.php +++ b/functions/crawler/src/index.php @@ -28,8 +28,6 @@ require_once 'vendor/autoload.php'; If an error is thrown, a response with code 500 will be returned. */ -// require_once 'threading/ThreadQueue.php'; - include "crawl.php"; return function($req, $res) { diff --git a/functions/suggestions/src/Suggestions.php b/functions/suggestions/src/Suggestions.php index 2a60558..f66cfa8 100644 --- a/functions/suggestions/src/Suggestions.php +++ b/functions/suggestions/src/Suggestions.php @@ -9,10 +9,66 @@ use Appwrite\Query; class Suggestions { public static function crawlFederatedFeed($user, $client) { + $request = new \GuzzleHttp\Client(['base_uri' => $user['prefs']['user_server_uri']]); + + try { + $response = $request->request('GET', "/api/v1/timelines/public", [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $user['prefs']['user_token'] + ] + ]); + + $responseBody = (string)$response->getBody(); + $responseBody = json_decode($responseBody); + + $statuses = []; + + foreach($responseBody as $status) { + if(count($status->tags) == 0) { + continue; + } + + $points = self::scoreStatus($status, $user, $client); + + if($points['points'] !== 0) { + $statuses[] = $points; + } + } + + usort($statuses, function ($a, $b) { + var_dump($a); + // return 0 <=> 0; + return $b['points'] <=> $a['points']; + }); + + return $statuses; + + } catch (\GuzzleHttp\Exception\ClientException $e) { + return [$e->getMessage()]; + } } - public static function scoreStatus($status, $user_id, $client) { + public static function scoreStatus($status, $user, $client) { + $database = new Databases($client); + $query = new Query(); + $points = 0; + + foreach($status->tags as $tag) { + $record = $database->listDocuments(getenv('APPWRITE_DATABASE_ID'), 'hashtags', [ + $query->equal('user', $user['$id']), + $query->equal('hashtag', $tag->name) + ])['documents']; + + if(isset($record[0])) { + $points += $record[0]['points']; + } + } + + return [ + 'post' => $status->url, + 'points' => $points + ]; } } \ No newline at end of file