-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathStatusController.php
More file actions
107 lines (95 loc) · 4.6 KB
/
Copy pathStatusController.php
File metadata and controls
107 lines (95 loc) · 4.6 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace App\Http\Controllers\Backend\Transport;
use App\Enum\StatusVisibility;
use App\Http\Controllers\Backend\Support\MentionHelper;
use App\Http\Controllers\Controller;
use App\Models\Station;
use App\Models\Status;
use App\Models\Stopover;
use App\Models\User;
use Closure;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Facades\DB;
abstract class StatusController extends Controller
{
/**
* @param Status $status
*
* @return Station|null
*/
public static function getNextStationForStatus(Status $status): ?Station {
return $status->checkin->trip->stopovers
->filter(function(Stopover $stopover) {
return $stopover->arrival->isFuture();
})
->sortBy('arrival') //sort by real time and if not available by planned time
->first()?->station;
}
/**
* Prepare the body for printing in the frontend.
*
* @param Status $status
*
* @return string
*/
public static function getPrintableEscapedBody(Status $status): string {
//Get the body with mention links (this string is already escaped)
$body = MentionHelper::getBodyWithMentionLinks($status);
//Replace multiple line breaks with two line breaks
$body = preg_replace('~(\R{2})\R+~', '$1', $body);
//Replace line breaks with <br> tags
return nl2br($body);
}
/**
* @param User|null $viewingUser The user who is viewing the statuses (null = guest)
*
* @return Closure
*/
public static function filterStatusVisibility(?User $viewingUser = null): Closure {
return function(EloquentBuilder $query) use ($viewingUser) {
if ($viewingUser !== null) {
$query->whereNotExists(function(QueryBuilder $subQuery) use ($viewingUser) {
$subQuery->select(DB::raw(1))
->from('status_hidden_users')
->whereColumn('status_hidden_users.status_id', 'statuses.id')
->where('status_hidden_users.user_id', $viewingUser->id);
});
}
//Visibility checks: One of the following options must be true
$query->where(function(EloquentBuilder $visibility) use ($viewingUser) {
//Option 1: User is public AND status is public
$visibility->where(function(EloquentBuilder $query) use ($viewingUser) {
$query->where('users.private_profile', 0)
->whereIn('visibility', [StatusVisibility::PUBLIC->value] + ($viewingUser !== null ? [StatusVisibility::AUTHENTICATED->value] : []));
});
if ($viewingUser !== null) {
//Option 2: Status is from oneself
$visibility->orWhere('users.id', $viewingUser->id);
//Option 3: Status is from a followed BUT not unlisted or private or trusted users only
$visibility->orWhere(function(EloquentBuilder $query) use ($viewingUser) {
$query->whereIn('users.id', $viewingUser->follows()->select('follow_id'))
->whereNotIn('statuses.visibility', [
StatusVisibility::UNLISTED->value,
StatusVisibility::PRIVATE->value,
StatusVisibility::TRUSTED->value,
]);
});
//Option 4: Status is from a user who trusts the viewing user
$visibility->orWhere(function(EloquentBuilder $query) use ($viewingUser) {
$query->where('statuses.visibility', StatusVisibility::TRUSTED->value)
->whereExists(function(QueryBuilder $subQuery) use ($viewingUser) {
$subQuery->from('trusted_users')
->whereColumn('trusted_users.user_id', 'statuses.user_id')
->where('trusted_users.trusted_id', $viewingUser->id)
->where(function(QueryBuilder $expireQuery) {
$expireQuery->whereNull('trusted_users.expires_at')
->orWhere('trusted_users.expires_at', '>', now());
});
});
});
}
});
};
}
}