Update DirectMessageController, add timestamps to threads

This commit is contained in:
Daniel Supernault 2024-06-17 23:02:16 -06:00
parent 6e0d1ef41b
commit b24d2554a8
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,31 +2,27 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Auth, Cache; use App\DirectMessage;
use Illuminate\Http\Request;
use App\{
DirectMessage,
Media,
Notification,
Profile,
Status,
User,
UserFilter,
UserSetting
};
use App\Services\MediaPathService;
use App\Services\MediaBlocklistService;
use App\Jobs\StatusPipeline\NewStatusPipeline;
use App\Jobs\StatusPipeline\StatusDelete;
use Illuminate\Support\Str;
use App\Util\ActivityPub\Helpers;
use App\Services\AccountService;
use App\Services\StatusService;
use App\Services\WebfingerService;
use App\Models\Conversation;
use App\Jobs\DirectPipeline\DirectDeletePipeline; use App\Jobs\DirectPipeline\DirectDeletePipeline;
use App\Jobs\DirectPipeline\DirectDeliverPipeline; use App\Jobs\DirectPipeline\DirectDeliverPipeline;
use App\Jobs\StatusPipeline\StatusDelete;
use App\Media;
use App\Models\Conversation;
use App\Notification;
use App\Profile;
use App\Services\AccountService;
use App\Services\MediaBlocklistService;
use App\Services\MediaPathService;
use App\Services\StatusService;
use App\Services\UserFilterService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Services\WebfingerService;
use App\Status;
use App\UserFilter;
use App\Util\ActivityPub\Helpers;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class DirectMessageController extends Controller class DirectMessageController extends Controller
{ {
@ -39,25 +35,25 @@ class DirectMessageController extends Controller
{ {
$this->validate($request, [ $this->validate($request, [
'a' => 'nullable|string|in:inbox,sent,filtered', 'a' => 'nullable|string|in:inbox,sent,filtered',
'page' => 'nullable|integer|min:1|max:99' 'page' => 'nullable|integer|min:1|max:99',
]); ]);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) { if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
return []; return [];
} }
$profile = $user->profile_id; $profile = $user->profile_id;
$action = $request->input('a', 'inbox'); $action = $request->input('a', 'inbox');
$page = $request->input('page'); $page = $request->input('page');
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
if($action == 'inbox') { if ($action == 'inbox') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(false) ->whereIsHidden(false)
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
@ -65,232 +61,232 @@ class DirectMessageController extends Controller
->get() ->get()
->unique('from_id') ->unique('from_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
})->values(); })->values();
} }
if($action == 'sent') { if ($action == 'sent') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereFromId($profile) ->whereFromId($profile)
->with(['author','status']) ->with(['author', 'status'])
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->get() ->get()
->unique('to_id') ->unique('to_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'filtered') { if ($action == 'filtered') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(true) ->whereIsHidden(true)
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->get() ->get()
->unique('from_id') ->unique('from_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
} elseif(config('database.default') == 'mysql') { } elseif (config('database.default') == 'mysql') {
if($action == 'inbox') { if ($action == 'inbox') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(false) ->whereIsHidden(false)
->groupBy('from_id') ->groupBy('from_id')
->latest() ->latest()
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'sent') { if ($action == 'sent') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereFromId($profile) ->whereFromId($profile)
->with(['author','status']) ->with(['author', 'status'])
->groupBy('to_id') ->groupBy('to_id')
->orderBy('createdAt', 'desc') ->orderBy('createdAt', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'filtered') { if ($action == 'filtered') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(true) ->whereIsHidden(true)
->groupBy('from_id') ->groupBy('from_id')
->orderBy('createdAt', 'desc') ->orderBy('createdAt', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
@ -304,11 +300,11 @@ class DirectMessageController extends Controller
$this->validate($request, [ $this->validate($request, [
'to_id' => 'required', 'to_id' => 'required',
'message' => 'required|string|min:1|max:500', 'message' => 'required|string|min:1|max:500',
'type' => 'required|in:text,emoji' 'type' => 'required|in:text,emoji',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
abort_if($user->created_at->gt(now()->subHours(72)), 400, 'You need to wait a bit before you can DM another account'); abort_if($user->created_at->gt(now()->subHours(72)), 400, 'You need to wait a bit before you can DM another account');
$profile = $user->profile; $profile = $user->profile;
$recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id')); $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
@ -316,8 +312,8 @@ class DirectMessageController extends Controller
abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403); abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
$msg = $request->input('message'); $msg = $request->input('message');
if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) { if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
if($recipient->follows($profile) == true) { if ($recipient->follows($profile) == true) {
$hidden = false; $hidden = false;
} else { } else {
$hidden = true; $hidden = true;
@ -346,23 +342,23 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $recipient->id, 'to_id' => $recipient->id,
'from_id' => $profile->id 'from_id' => $profile->id,
], ],
[ [
'type' => $dm->type, 'type' => $dm->type,
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => $hidden 'is_hidden' => $hidden,
] ]
); );
if(filter_var($msg, FILTER_VALIDATE_URL)) { if (filter_var($msg, FILTER_VALIDATE_URL)) {
if(Helpers::validateUrl($msg)) { if (Helpers::validateUrl($msg)) {
$dm->type = 'link'; $dm->type = 'link';
$dm->meta = [ $dm->meta = [
'domain' => parse_url($msg, PHP_URL_HOST), 'domain' => parse_url($msg, PHP_URL_HOST),
'local' => parse_url($msg, PHP_URL_HOST) == 'local' => parse_url($msg, PHP_URL_HOST) ==
parse_url(config('app.url'), PHP_URL_HOST) parse_url(config('app.url'), PHP_URL_HOST),
]; ];
$dm->save(); $dm->save();
} }
@ -374,7 +370,7 @@ class DirectMessageController extends Controller
->whereFilterType('dm.mute') ->whereFilterType('dm.mute')
->exists(); ->exists();
if($recipient->domain == null && $hidden == false && !$nf) { if ($recipient->domain == null && $hidden == false && ! $nf) {
$notification = new Notification(); $notification = new Notification();
$notification->profile_id = $recipient->id; $notification->profile_id = $recipient->id;
$notification->actor_id = $profile->id; $notification->actor_id = $profile->id;
@ -384,7 +380,7 @@ class DirectMessageController extends Controller
$notification->save(); $notification->save();
} }
if($recipient->domain) { if ($recipient->domain) {
$this->remoteDeliver($dm); $this->remoteDeliver($dm);
} }
@ -396,9 +392,9 @@ class DirectMessageController extends Controller
'type' => $dm->type, 'type' => $dm->type,
'text' => $dm->status->caption, 'text' => $dm->status->caption,
'media' => null, 'media' => null,
'timeAgo' => $dm->created_at->diffForHumans(null,null,true), 'timeAgo' => $dm->created_at->diffForHumans(null, null, true),
'seen' => $dm->read_at != null, 'seen' => $dm->read_at != null,
'meta' => $dm->meta 'meta' => $dm->meta,
]; ];
return response()->json($res); return response()->json($res);
@ -407,10 +403,10 @@ class DirectMessageController extends Controller
public function thread(Request $request) public function thread(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'pid' => 'required' 'pid' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$uid = $user->profile_id; $uid = $user->profile_id;
$pid = $request->input('pid'); $pid = $request->input('pid');
@ -419,40 +415,40 @@ class DirectMessageController extends Controller
$r = Profile::findOrFail($pid); $r = Profile::findOrFail($pid);
if($min_id) { if ($min_id) {
$res = DirectMessage::select('*') $res = DirectMessage::select('*')
->where('id', '>', $min_id) ->where('id', '>', $min_id)
->where(function($q) use($pid,$uid) { ->where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} else if ($max_id) { } elseif ($max_id) {
$res = DirectMessage::select('*') $res = DirectMessage::select('*')
->where('id', '<', $max_id) ->where('id', '<', $max_id)
->where(function($q) use($pid,$uid) { ->where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} else { } else {
$res = DirectMessage::where(function($q) use($pid,$uid) { $res = DirectMessage::where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} }
$res = $res->filter(function($s) { $res = $res->filter(function ($s) {
return $s && $s->status; return $s && $s->status;
}) })
->map(function($s) use ($uid) { ->map(function ($s) use ($uid) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'hidden' => (bool) $s->is_hidden, 'hidden' => (bool) $s->is_hidden,
@ -460,39 +456,40 @@ class DirectMessageController extends Controller
'type' => $s->type, 'type' => $s->type,
'text' => $s->status->caption, 'text' => $s->status->caption,
'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null, 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
'timeAgo' => $s->created_at->diffForHumans(null,null,true), 'created_at' => $s->created_at->format('c'),
'timeAgo' => $s->created_at->diffForHumans(null, null, true),
'seen' => $s->read_at != null, 'seen' => $s->read_at != null,
'reportId' => (string) $s->status_id, 'reportId' => (string) $s->status_id,
'meta' => json_decode($s->meta,true) 'meta' => json_decode($s->meta, true),
]; ];
}) })
->values(); ->values();
$filters = UserFilterService::mutes($uid);
$w = [ $w = [
'id' => (string) $r->id, 'id' => (string) $r->id,
'name' => $r->name, 'name' => $r->name,
'username' => $r->username, 'username' => $r->username,
'avatar' => $r->avatarUrl(), 'avatar' => $r->avatarUrl(),
'url' => $r->url(), 'url' => $r->url(),
'muted' => UserFilter::whereUserId($uid) 'muted' => in_array($r->id, $filters),
->whereFilterableId($r->id) 'isLocal' => (bool) ! $r->domain,
->whereFilterableType('App\Profile')
->whereFilterType('dm.mute')
->first() ? true : false,
'isLocal' => (bool) !$r->domain,
'domain' => $r->domain, 'domain' => $r->domain,
'created_at' => $r->created_at->format('c'),
'updated_at' => $r->updated_at->format('c'),
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => '', 'lastMessage' => '',
'messages' => $res 'messages' => $res,
]; ];
return response()->json($w, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); return response()->json($w, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function delete(Request $request) public function delete(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$sid = $request->input('id'); $sid = $request->input('id');
@ -507,30 +504,30 @@ class DirectMessageController extends Controller
$recipient = AccountService::get($dm->to_id); $recipient = AccountService::get($dm->to_id);
if(!$recipient) { if (! $recipient) {
return response('', 422); return response('', 422);
} }
if($recipient['local'] == false) { if ($recipient['local'] == false) {
$dmc = $dm; $dmc = $dm;
$this->remoteDelete($dmc); $this->remoteDelete($dmc);
} else { } else {
StatusDelete::dispatch($status)->onQueue('high'); StatusDelete::dispatch($status)->onQueue('high');
} }
if(Conversation::whereStatusId($sid)->count()) { if (Conversation::whereStatusId($sid)->count()) {
$latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id]) $latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id])
->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) ->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
->latest() ->latest()
->first(); ->first();
if($latest->status_id == $sid) { if ($latest->status_id == $sid) {
Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
->update([ ->update([
'updated_at' => $latest->updated_at, 'updated_at' => $latest->updated_at,
'status_id' => $latest->status_id, 'status_id' => $latest->status_id,
'type' => $latest->type, 'type' => $latest->type,
'is_hidden' => false 'is_hidden' => false,
]); ]);
Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id]) Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id])
@ -538,19 +535,19 @@ class DirectMessageController extends Controller
'updated_at' => $latest->updated_at, 'updated_at' => $latest->updated_at,
'status_id' => $latest->status_id, 'status_id' => $latest->status_id,
'type' => $latest->type, 'type' => $latest->type,
'is_hidden' => false 'is_hidden' => false,
]); ]);
} else { } else {
Conversation::where([ Conversation::where([
'status_id' => $sid, 'status_id' => $sid,
'to_id' => $dm->from_id, 'to_id' => $dm->from_id,
'from_id' => $dm->to_id 'from_id' => $dm->to_id,
])->delete(); ])->delete();
Conversation::where([ Conversation::where([
'status_id' => $sid, 'status_id' => $sid,
'from_id' => $dm->from_id, 'from_id' => $dm->from_id,
'to_id' => $dm->to_id 'to_id' => $dm->to_id,
])->delete(); ])->delete();
} }
} }
@ -558,41 +555,43 @@ class DirectMessageController extends Controller
StatusService::del($status->id, true); StatusService::del($status->id, true);
$status->forceDeleteQuietly(); $status->forceDeleteQuietly();
return [200]; return [200];
} }
public function get(Request $request, $id) public function get(Request $request, $id)
{ {
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$dm = DirectMessage::whereStatusId($id)->firstOrFail(); $dm = DirectMessage::whereStatusId($id)->firstOrFail();
abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404); abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
return response()->json($dm, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($dm, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function mediaUpload(Request $request) public function mediaUpload(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'file' => function() { 'file' => function () {
return [ return [
'required', 'required',
'mimetypes:' . config_cache('pixelfed.media_types'), 'mimetypes:'.config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
]; ];
}, },
'to_id' => 'required' 'to_id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$profile = $user->profile; $profile = $user->profile;
$recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id')); $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403); abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) { if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
if($recipient->follows($profile) == true) { if ($recipient->follows($profile) == true) {
$hidden = false; $hidden = false;
} else { } else {
$hidden = true; $hidden = true;
@ -601,8 +600,8 @@ class DirectMessageController extends Controller
$hidden = false; $hidden = false;
} }
if(config_cache('pixelfed.enforce_account_limit') == true) { if (config_cache('pixelfed.enforce_account_limit') == true) {
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) { $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) {
return Media::whereUserId($user->id)->sum('size') / 1000; return Media::whereUserId($user->id)->sum('size') / 1000;
}); });
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
@ -613,11 +612,11 @@ class DirectMessageController extends Controller
$photo = $request->file('file'); $photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), $mimes) == false) { if (in_array($photo->getMimeType(), $mimes) == false) {
abort(403, 'Invalid or unsupported mime type.'); abort(403, 'Invalid or unsupported mime type.');
} }
$storagePath = MediaPathService::get($user, 2) . Str::random(8); $storagePath = MediaPathService::get($user, 2).Str::random(8);
$path = $photo->storePublicly($storagePath); $path = $photo->storePublicly($storagePath);
$hash = \hash_file('sha256', $photo); $hash = \hash_file('sha256', $photo);
@ -656,17 +655,17 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $recipient->id, 'to_id' => $recipient->id,
'from_id' => $profile->id 'from_id' => $profile->id,
], ],
[ [
'type' => $dm->type, 'type' => $dm->type,
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => $hidden 'is_hidden' => $hidden,
] ]
); );
if($recipient->domain) { if ($recipient->domain) {
$this->remoteDeliver($dm); $this->remoteDeliver($dm);
} }
@ -674,7 +673,7 @@ class DirectMessageController extends Controller
'id' => $dm->id, 'id' => $dm->id,
'reportId' => (string) $dm->status_id, 'reportId' => (string) $dm->status_id,
'type' => $dm->type, 'type' => $dm->type,
'url' => $media->url() 'url' => $media->url(),
]; ];
} }
@ -686,26 +685,26 @@ class DirectMessageController extends Controller
]); ]);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) { if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
return []; return [];
} }
$q = $request->input('q'); $q = $request->input('q');
$r = $request->input('remote', false); $r = $request->input('remote', false);
if($r && !Str::of($q)->contains('.')) { if ($r && ! Str::of($q)->contains('.')) {
return []; return [];
} }
if($r && Helpers::validateUrl($q)) { if ($r && Helpers::validateUrl($q)) {
Helpers::profileFetch($q); Helpers::profileFetch($q);
} }
if(Str::of($q)->startsWith('@')) { if (Str::of($q)->startsWith('@')) {
if(strlen($q) < 3) { if (strlen($q) < 3) {
return []; return [];
} }
if(substr_count($q, '@') == 2) { if (substr_count($q, '@') == 2) {
WebfingerService::lookup($q); WebfingerService::lookup($q);
} }
$q = mb_substr($q, 1); $q = mb_substr($q, 1);
@ -718,21 +717,22 @@ class DirectMessageController extends Controller
$blocked->push($request->user()->profile_id); $blocked->push($request->user()->profile_id);
$results = Profile::select('id','domain','username') $results = Profile::select('id', 'domain', 'username')
->whereNotIn('id', $blocked) ->whereNotIn('id', $blocked)
->where('username','like','%'.$q.'%') ->where('username', 'like', '%'.$q.'%')
->orderBy('domain') ->orderBy('domain')
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) { ->map(function ($r) {
$acct = AccountService::get($r->id); $acct = AccountService::get($r->id);
return [ return [
'local' => (bool) !$r->domain, 'local' => (bool) ! $r->domain,
'id' => (string) $r->id, 'id' => (string) $r->id,
'name' => $r->username, 'name' => $r->username,
'privacy' => true, 'privacy' => true,
'avatar' => $r->avatarUrl(), 'avatar' => $r->avatarUrl(),
'account' => $acct 'account' => $acct,
]; ];
}); });
@ -743,13 +743,13 @@ class DirectMessageController extends Controller
{ {
$this->validate($request, [ $this->validate($request, [
'pid' => 'required', 'pid' => 'required',
'sid' => 'required' 'sid' => 'required',
]); ]);
$pid = $request->input('pid'); $pid = $request->input('pid');
$sid = $request->input('sid'); $sid = $request->input('sid');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$dms = DirectMessage::whereToId($request->user()->profile_id) $dms = DirectMessage::whereToId($request->user()->profile_id)
->whereFromId($pid) ->whereFromId($pid)
@ -757,7 +757,7 @@ class DirectMessageController extends Controller
->get(); ->get();
$now = now(); $now = now();
foreach($dms as $dm) { foreach ($dms as $dm) {
$dm->read_at = $now; $dm->read_at = $now;
$dm->save(); $dm->save();
} }
@ -768,11 +768,11 @@ class DirectMessageController extends Controller
public function mute(Request $request) public function mute(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$fid = $request->input('id'); $fid = $request->input('id');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -781,7 +781,7 @@ class DirectMessageController extends Controller
'user_id' => $pid, 'user_id' => $pid,
'filterable_id' => $fid, 'filterable_id' => $fid,
'filterable_type' => 'App\Profile', 'filterable_type' => 'App\Profile',
'filter_type' => 'dm.mute' 'filter_type' => 'dm.mute',
] ]
); );
@ -791,11 +791,11 @@ class DirectMessageController extends Controller
public function unmute(Request $request) public function unmute(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$fid = $request->input('id'); $fid = $request->input('id');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -821,7 +821,7 @@ class DirectMessageController extends Controller
'type' => 'Mention', 'type' => 'Mention',
'href' => $dm->recipient->permalink(), 'href' => $dm->recipient->permalink(),
'name' => $dm->recipient->emailUrl(), 'name' => $dm->recipient->emailUrl(),
] ],
]; ];
$body = [ $body = [
@ -856,7 +856,7 @@ class DirectMessageController extends Controller
]; ];
})->toArray(), })->toArray(),
'tag' => $tags, 'tag' => $tags,
] ],
]; ];
DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high'); DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high');
@ -873,14 +873,14 @@ class DirectMessageController extends Controller
], ],
'id' => $dm->status->permalink('#delete'), 'id' => $dm->status->permalink('#delete'),
'to' => [ 'to' => [
'https://www.w3.org/ns/activitystreams#Public' 'https://www.w3.org/ns/activitystreams#Public',
], ],
'type' => 'Delete', 'type' => 'Delete',
'actor' => $dm->status->profile->permalink(), 'actor' => $dm->status->profile->permalink(),
'object' => [ 'object' => [
'id' => $dm->status->url(), 'id' => $dm->status->url(),
'type' => 'Tombstone' 'type' => 'Tombstone',
] ],
]; ];
DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high'); DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high');
} }