summaryrefslogtreecommitdiff
path: root/app/Http/Controllers/FeedController.php
blob: 5b9c36a0644f5f1e188802e682b7496c913e4e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Article;
use App\Discussion;
use App\Category;
use App\Libraries\Helper;

class FeedController extends Controller
{

    public function new()
    {
	// creating rss feed with our most recent 20 posts
	$articles = Article::orderBy('created_at', 'desc')->take(20)->get();
	return Helper::makeFeed($articles, "new");
    }

    public function mastodon()
    {
	$articles = Article::orderBy('created_at', 'desc')->take(20)->get();
	return Helper::makeFeed($articles, "mastodon");
    }

    public function popular()
    {

	$articles = new Article;
	$articles = $articles->setTable('view_popular');
	$articles = $articles->take(20)->get();
	return Helper::makeFeed($articles, "popular");
    }

    public function search(Request $request) {
	    $search_unsafe = $request->input("q");

	    if ( "" == $search_unsafe ) {
		    $search_unsafe = "";
	    }

	    $search_unsafe = explode(",", $request->input("q"));

	    $articles = new Article;

	    if ( "on" == $request->input("onlypopular") ) {
	    	$articles = $articles->setTable('view_popular');
	    }

	    foreach($search_unsafe as $q) {
		    $q = Helper::escapeLike($q);
		    $q = "%".$q."%";
		    $articles = $articles->where(function ($query) use ($q) {
		    		$query->whereHas('getCategories', function ($query) use ($q){
		    			$query->where('name', 'like', $q);
	    			})
		    		->orWhere('title', 'like', $q)
				->orWhere('url', 'like', $q)
				->orWhere('excerpt_html', 'like', $q);
				});
	    }
	    $count = $articles->count();

	    if ( "on" == $request->input("onlypopular") ) {
	    	$articles = $articles->orderBy('impact', 'desc');
	    }
	    $articles = $articles->orderBy('created_at', 'desc');

	    return Helper::makeFeed($articles->take(20)->get(), $request->input("q"));
    }
}