Merge pull request #5182 from pixelfed/staging

Update DirectMessageController, add timestamps to threads
This commit is contained in:
daniel 2024-06-17 23:04:20 -06:00 committed by GitHub
commit 2f67185fec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,31 +2,27 @@
namespace App\Http\Controllers;
use Auth, Cache;
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\DirectMessage;
use App\Jobs\DirectPipeline\DirectDeletePipeline;
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\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
{
@ -39,7 +35,7 @@ class DirectMessageController extends Controller
{
$this->validate($request, [
'a' => 'nullable|string|in:inbox,sent,filtered',
'page' => 'nullable|integer|min:1|max:99'
'page' => 'nullable|integer|min:1|max:99',
]);
$user = $request->user();
@ -76,7 +72,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -87,7 +83,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
})->values();
}
@ -116,7 +112,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -127,7 +123,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
});
}
@ -157,7 +153,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -168,7 +164,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
});
}
@ -198,7 +194,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -209,7 +205,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
});
}
@ -238,7 +234,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -249,7 +245,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
});
}
@ -279,7 +275,7 @@ class DirectMessageController extends Controller
'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
] : [
'id' => (string) $r->to_id,
'name' => $r->recipient->name,
@ -290,7 +286,7 @@ class DirectMessageController extends Controller
'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption,
'messages' => []
'messages' => [],
];
});
}
@ -304,7 +300,7 @@ class DirectMessageController extends Controller
$this->validate($request, [
'to_id' => 'required',
'message' => 'required|string|min:1|max:500',
'type' => 'required|in:text,emoji'
'type' => 'required|in:text,emoji',
]);
$user = $request->user();
@ -346,13 +342,13 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert(
[
'to_id' => $recipient->id,
'from_id' => $profile->id
'from_id' => $profile->id,
],
[
'type' => $dm->type,
'status_id' => $status->id,
'dm_id' => $dm->id,
'is_hidden' => $hidden
'is_hidden' => $hidden,
]
);
@ -362,7 +358,7 @@ class DirectMessageController extends Controller
$dm->meta = [
'domain' => 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();
}
@ -398,7 +394,7 @@ class DirectMessageController extends Controller
'media' => null,
'timeAgo' => $dm->created_at->diffForHumans(null, null, true),
'seen' => $dm->read_at != null,
'meta' => $dm->meta
'meta' => $dm->meta,
];
return response()->json($res);
@ -407,7 +403,7 @@ class DirectMessageController extends Controller
public function thread(Request $request)
{
$this->validate($request, [
'pid' => 'required'
'pid' => 'required',
]);
$user = $request->user();
abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
@ -423,7 +419,7 @@ class DirectMessageController extends Controller
$res = DirectMessage::select('*')
->where('id', '>', $min_id)
->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]]);
})
->latest()
@ -433,7 +429,7 @@ class DirectMessageController extends Controller
$res = DirectMessage::select('*')
->where('id', '<', $max_id)
->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]]);
})
->latest()
@ -441,7 +437,7 @@ class DirectMessageController extends Controller
->get();
} else {
$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]]);
})
->latest()
@ -460,30 +456,31 @@ class DirectMessageController extends Controller
'type' => $s->type,
'text' => $s->status->caption,
'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
'created_at' => $s->created_at->format('c'),
'timeAgo' => $s->created_at->diffForHumans(null, null, true),
'seen' => $s->read_at != null,
'reportId' => (string) $s->status_id,
'meta' => json_decode($s->meta,true)
'meta' => json_decode($s->meta, true),
];
})
->values();
$filters = UserFilterService::mutes($uid);
$w = [
'id' => (string) $r->id,
'name' => $r->name,
'username' => $r->username,
'avatar' => $r->avatarUrl(),
'url' => $r->url(),
'muted' => UserFilter::whereUserId($uid)
->whereFilterableId($r->id)
->whereFilterableType('App\Profile')
->whereFilterType('dm.mute')
->first() ? true : false,
'muted' => in_array($r->id, $filters),
'isLocal' => (bool) ! $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),
'lastMessage' => '',
'messages' => $res
'messages' => $res,
];
return response()->json($w, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
@ -492,7 +489,7 @@ class DirectMessageController extends Controller
public function delete(Request $request)
{
$this->validate($request, [
'id' => 'required'
'id' => 'required',
]);
$sid = $request->input('id');
@ -530,7 +527,7 @@ class DirectMessageController extends Controller
'updated_at' => $latest->updated_at,
'status_id' => $latest->status_id,
'type' => $latest->type,
'is_hidden' => false
'is_hidden' => false,
]);
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,
'status_id' => $latest->status_id,
'type' => $latest->type,
'is_hidden' => false
'is_hidden' => false,
]);
} else {
Conversation::where([
'status_id' => $sid,
'to_id' => $dm->from_id,
'from_id' => $dm->to_id
'from_id' => $dm->to_id,
])->delete();
Conversation::where([
'status_id' => $sid,
'from_id' => $dm->from_id,
'to_id' => $dm->to_id
'to_id' => $dm->to_id,
])->delete();
}
}
@ -558,6 +555,7 @@ class DirectMessageController extends Controller
StatusService::del($status->id, true);
$status->forceDeleteQuietly();
return [200];
}
@ -569,6 +567,7 @@ class DirectMessageController extends Controller
$pid = $request->user()->profile_id;
$dm = DirectMessage::whereStatusId($id)->firstOrFail();
abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
return response()->json($dm, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
@ -582,7 +581,7 @@ class DirectMessageController extends Controller
'max:'.config_cache('pixelfed.max_photo_size'),
];
},
'to_id' => 'required'
'to_id' => 'required',
]);
$user = $request->user();
@ -656,13 +655,13 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert(
[
'to_id' => $recipient->id,
'from_id' => $profile->id
'from_id' => $profile->id,
],
[
'type' => $dm->type,
'status_id' => $status->id,
'dm_id' => $dm->id,
'is_hidden' => $hidden
'is_hidden' => $hidden,
]
);
@ -674,7 +673,7 @@ class DirectMessageController extends Controller
'id' => $dm->id,
'reportId' => (string) $dm->status_id,
'type' => $dm->type,
'url' => $media->url()
'url' => $media->url(),
];
}
@ -726,13 +725,14 @@ class DirectMessageController extends Controller
->get()
->map(function ($r) {
$acct = AccountService::get($r->id);
return [
'local' => (bool) ! $r->domain,
'id' => (string) $r->id,
'name' => $r->username,
'privacy' => true,
'avatar' => $r->avatarUrl(),
'account' => $acct
'account' => $acct,
];
});
@ -743,7 +743,7 @@ class DirectMessageController extends Controller
{
$this->validate($request, [
'pid' => 'required',
'sid' => 'required'
'sid' => 'required',
]);
$pid = $request->input('pid');
@ -768,7 +768,7 @@ class DirectMessageController extends Controller
public function mute(Request $request)
{
$this->validate($request, [
'id' => 'required'
'id' => 'required',
]);
$user = $request->user();
@ -781,7 +781,7 @@ class DirectMessageController extends Controller
'user_id' => $pid,
'filterable_id' => $fid,
'filterable_type' => 'App\Profile',
'filter_type' => 'dm.mute'
'filter_type' => 'dm.mute',
]
);
@ -791,7 +791,7 @@ class DirectMessageController extends Controller
public function unmute(Request $request)
{
$this->validate($request, [
'id' => 'required'
'id' => 'required',
]);
$user = $request->user();
@ -821,7 +821,7 @@ class DirectMessageController extends Controller
'type' => 'Mention',
'href' => $dm->recipient->permalink(),
'name' => $dm->recipient->emailUrl(),
]
],
];
$body = [
@ -856,7 +856,7 @@ class DirectMessageController extends Controller
];
})->toArray(),
'tag' => $tags,
]
],
];
DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high');
@ -873,14 +873,14 @@ class DirectMessageController extends Controller
],
'id' => $dm->status->permalink('#delete'),
'to' => [
'https://www.w3.org/ns/activitystreams#Public'
'https://www.w3.org/ns/activitystreams#Public',
],
'type' => 'Delete',
'actor' => $dm->status->profile->permalink(),
'object' => [
'id' => $dm->status->url(),
'type' => 'Tombstone'
]
'type' => 'Tombstone',
],
];
DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high');
}