mirror of
https://github.com/pixelfed/pixelfed.git
synced 2025-02-04 10:50:46 +00:00
Update DirectMessageController, fix pgsql bug
This commit is contained in:
parent
fcb785bb0f
commit
f1c28e7d0c
1 changed files with 245 additions and 121 deletions
|
@ -39,129 +39,253 @@ class DirectMessageController extends Controller
|
||||||
$action = $request->input('a', 'inbox');
|
$action = $request->input('a', 'inbox');
|
||||||
$page = $request->input('page');
|
$page = $request->input('page');
|
||||||
|
|
||||||
if($action == 'inbox') {
|
if(config('database.default') == 'pgsql') {
|
||||||
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
if($action == 'inbox') {
|
||||||
->whereToId($profile)
|
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
|
||||||
->with(['author','status'])
|
->whereToId($profile)
|
||||||
->whereIsHidden(false)
|
->with(['author','status'])
|
||||||
->groupBy('from_id')
|
->whereIsHidden(false)
|
||||||
->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);
|
}
|
||||||
}
|
})
|
||||||
})
|
->latest()
|
||||||
->limit(8)
|
->get()
|
||||||
->get()
|
->unique('from_id')
|
||||||
->map(function($r) use($profile) {
|
->take(8)
|
||||||
return $r->from_id !== $profile ? [
|
->map(function($r) use($profile) {
|
||||||
'id' => (string) $r->from_id,
|
return $r->from_id !== $profile ? [
|
||||||
'name' => $r->author->name,
|
'id' => (string) $r->from_id,
|
||||||
'username' => $r->author->username,
|
'name' => $r->author->name,
|
||||||
'avatar' => $r->author->avatarUrl(),
|
'username' => $r->author->username,
|
||||||
'url' => $r->author->url(),
|
'avatar' => $r->author->avatarUrl(),
|
||||||
'isLocal' => (bool) !$r->author->domain,
|
'url' => $r->author->url(),
|
||||||
'domain' => $r->author->domain,
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
'domain' => $r->author->domain,
|
||||||
'lastMessage' => $r->status->caption,
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
'messages' => []
|
'lastMessage' => $r->status->caption,
|
||||||
] : [
|
'messages' => []
|
||||||
'id' => (string) $r->to_id,
|
] : [
|
||||||
'name' => $r->recipient->name,
|
'id' => (string) $r->to_id,
|
||||||
'username' => $r->recipient->username,
|
'name' => $r->recipient->name,
|
||||||
'avatar' => $r->recipient->avatarUrl(),
|
'username' => $r->recipient->username,
|
||||||
'url' => $r->recipient->url(),
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
'isLocal' => (bool) !$r->recipient->domain,
|
'url' => $r->recipient->url(),
|
||||||
'domain' => $r->recipient->domain,
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
'domain' => $r->recipient->domain,
|
||||||
'lastMessage' => $r->status->caption,
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
'messages' => []
|
'lastMessage' => $r->status->caption,
|
||||||
];
|
'messages' => []
|
||||||
});
|
];
|
||||||
|
})->values();
|
||||||
|
}
|
||||||
|
|
||||||
|
if($action == 'sent') {
|
||||||
|
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
|
||||||
|
->whereFromId($profile)
|
||||||
|
->with(['author','status'])
|
||||||
|
->orderBy('id', 'desc')
|
||||||
|
->when($page, function($q, $page) {
|
||||||
|
if($page > 1) {
|
||||||
|
return $q->offset($page * 8 - 8);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->get()
|
||||||
|
->unique('to_id')
|
||||||
|
->take(8)
|
||||||
|
->map(function($r) use($profile) {
|
||||||
|
return $r->from_id !== $profile ? [
|
||||||
|
'id' => (string) $r->from_id,
|
||||||
|
'name' => $r->author->name,
|
||||||
|
'username' => $r->author->username,
|
||||||
|
'avatar' => $r->author->avatarUrl(),
|
||||||
|
'url' => $r->author->url(),
|
||||||
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
|
'domain' => $r->author->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
] : [
|
||||||
|
'id' => (string) $r->to_id,
|
||||||
|
'name' => $r->recipient->name,
|
||||||
|
'username' => $r->recipient->username,
|
||||||
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
|
'url' => $r->recipient->url(),
|
||||||
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
|
'domain' => $r->recipient->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if($action == 'filtered') {
|
||||||
|
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
|
||||||
|
->whereToId($profile)
|
||||||
|
->with(['author','status'])
|
||||||
|
->whereIsHidden(true)
|
||||||
|
->orderBy('id', 'desc')
|
||||||
|
->when($page, function($q, $page) {
|
||||||
|
if($page > 1) {
|
||||||
|
return $q->offset($page * 8 - 8);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->get()
|
||||||
|
->unique('from_id')
|
||||||
|
->take(8)
|
||||||
|
->map(function($r) use($profile) {
|
||||||
|
return $r->from_id !== $profile ? [
|
||||||
|
'id' => (string) $r->from_id,
|
||||||
|
'name' => $r->author->name,
|
||||||
|
'username' => $r->author->username,
|
||||||
|
'avatar' => $r->author->avatarUrl(),
|
||||||
|
'url' => $r->author->url(),
|
||||||
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
|
'domain' => $r->author->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
] : [
|
||||||
|
'id' => (string) $r->to_id,
|
||||||
|
'name' => $r->recipient->name,
|
||||||
|
'username' => $r->recipient->username,
|
||||||
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
|
'url' => $r->recipient->url(),
|
||||||
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
|
'domain' => $r->recipient->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} elseif(config('database.default') == 'mysql') {
|
||||||
|
if($action == 'inbox') {
|
||||||
|
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
||||||
|
->whereToId($profile)
|
||||||
|
->with(['author','status'])
|
||||||
|
->whereIsHidden(false)
|
||||||
|
->groupBy('from_id')
|
||||||
|
->latest()
|
||||||
|
->when($page, function($q, $page) {
|
||||||
|
if($page > 1) {
|
||||||
|
return $q->offset($page * 8 - 8);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->limit(8)
|
||||||
|
->get()
|
||||||
|
->map(function($r) use($profile) {
|
||||||
|
return $r->from_id !== $profile ? [
|
||||||
|
'id' => (string) $r->from_id,
|
||||||
|
'name' => $r->author->name,
|
||||||
|
'username' => $r->author->username,
|
||||||
|
'avatar' => $r->author->avatarUrl(),
|
||||||
|
'url' => $r->author->url(),
|
||||||
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
|
'domain' => $r->author->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
] : [
|
||||||
|
'id' => (string) $r->to_id,
|
||||||
|
'name' => $r->recipient->name,
|
||||||
|
'username' => $r->recipient->username,
|
||||||
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
|
'url' => $r->recipient->url(),
|
||||||
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
|
'domain' => $r->recipient->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if($action == 'sent') {
|
||||||
|
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
||||||
|
->whereFromId($profile)
|
||||||
|
->with(['author','status'])
|
||||||
|
->groupBy('to_id')
|
||||||
|
->orderBy('createdAt', 'desc')
|
||||||
|
->when($page, function($q, $page) {
|
||||||
|
if($page > 1) {
|
||||||
|
return $q->offset($page * 8 - 8);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->limit(8)
|
||||||
|
->get()
|
||||||
|
->map(function($r) use($profile) {
|
||||||
|
return $r->from_id !== $profile ? [
|
||||||
|
'id' => (string) $r->from_id,
|
||||||
|
'name' => $r->author->name,
|
||||||
|
'username' => $r->author->username,
|
||||||
|
'avatar' => $r->author->avatarUrl(),
|
||||||
|
'url' => $r->author->url(),
|
||||||
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
|
'domain' => $r->author->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
] : [
|
||||||
|
'id' => (string) $r->to_id,
|
||||||
|
'name' => $r->recipient->name,
|
||||||
|
'username' => $r->recipient->username,
|
||||||
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
|
'url' => $r->recipient->url(),
|
||||||
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
|
'domain' => $r->recipient->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if($action == 'filtered') {
|
||||||
|
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
||||||
|
->whereToId($profile)
|
||||||
|
->with(['author','status'])
|
||||||
|
->whereIsHidden(true)
|
||||||
|
->groupBy('from_id')
|
||||||
|
->orderBy('createdAt', 'desc')
|
||||||
|
->when($page, function($q, $page) {
|
||||||
|
if($page > 1) {
|
||||||
|
return $q->offset($page * 8 - 8);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->limit(8)
|
||||||
|
->get()
|
||||||
|
->map(function($r) use($profile) {
|
||||||
|
return $r->from_id !== $profile ? [
|
||||||
|
'id' => (string) $r->from_id,
|
||||||
|
'name' => $r->author->name,
|
||||||
|
'username' => $r->author->username,
|
||||||
|
'avatar' => $r->author->avatarUrl(),
|
||||||
|
'url' => $r->author->url(),
|
||||||
|
'isLocal' => (bool) !$r->author->domain,
|
||||||
|
'domain' => $r->author->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
] : [
|
||||||
|
'id' => (string) $r->to_id,
|
||||||
|
'name' => $r->recipient->name,
|
||||||
|
'username' => $r->recipient->username,
|
||||||
|
'avatar' => $r->recipient->avatarUrl(),
|
||||||
|
'url' => $r->recipient->url(),
|
||||||
|
'isLocal' => (bool) !$r->recipient->domain,
|
||||||
|
'domain' => $r->recipient->domain,
|
||||||
|
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
||||||
|
'lastMessage' => $r->status->caption,
|
||||||
|
'messages' => []
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($action == 'sent') {
|
return response()->json($dms->all());
|
||||||
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
|
||||||
->whereFromId($profile)
|
|
||||||
->with(['author','status'])
|
|
||||||
->groupBy('to_id')
|
|
||||||
->orderBy('createdAt', 'desc')
|
|
||||||
->when($page, function($q, $page) {
|
|
||||||
if($page > 1) {
|
|
||||||
return $q->offset($page * 8 - 8);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
->limit(8)
|
|
||||||
->get()
|
|
||||||
->map(function($r) use($profile) {
|
|
||||||
return $r->from_id !== $profile ? [
|
|
||||||
'id' => (string) $r->from_id,
|
|
||||||
'name' => $r->author->name,
|
|
||||||
'username' => $r->author->username,
|
|
||||||
'avatar' => $r->author->avatarUrl(),
|
|
||||||
'url' => $r->author->url(),
|
|
||||||
'isLocal' => (bool) !$r->author->domain,
|
|
||||||
'domain' => $r->author->domain,
|
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
|
||||||
'lastMessage' => $r->status->caption,
|
|
||||||
'messages' => []
|
|
||||||
] : [
|
|
||||||
'id' => (string) $r->to_id,
|
|
||||||
'name' => $r->recipient->name,
|
|
||||||
'username' => $r->recipient->username,
|
|
||||||
'avatar' => $r->recipient->avatarUrl(),
|
|
||||||
'url' => $r->recipient->url(),
|
|
||||||
'isLocal' => (bool) !$r->recipient->domain,
|
|
||||||
'domain' => $r->recipient->domain,
|
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
|
||||||
'lastMessage' => $r->status->caption,
|
|
||||||
'messages' => []
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if($action == 'filtered') {
|
|
||||||
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
|
|
||||||
->whereToId($profile)
|
|
||||||
->with(['author','status'])
|
|
||||||
->whereIsHidden(true)
|
|
||||||
->groupBy('from_id')
|
|
||||||
->orderBy('createdAt', 'desc')
|
|
||||||
->when($page, function($q, $page) {
|
|
||||||
if($page > 1) {
|
|
||||||
return $q->offset($page * 8 - 8);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
->limit(8)
|
|
||||||
->get()
|
|
||||||
->map(function($r) use($profile) {
|
|
||||||
return $r->from_id !== $profile ? [
|
|
||||||
'id' => (string) $r->from_id,
|
|
||||||
'name' => $r->author->name,
|
|
||||||
'username' => $r->author->username,
|
|
||||||
'avatar' => $r->author->avatarUrl(),
|
|
||||||
'url' => $r->author->url(),
|
|
||||||
'isLocal' => (bool) !$r->author->domain,
|
|
||||||
'domain' => $r->author->domain,
|
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
|
||||||
'lastMessage' => $r->status->caption,
|
|
||||||
'messages' => []
|
|
||||||
] : [
|
|
||||||
'id' => (string) $r->to_id,
|
|
||||||
'name' => $r->recipient->name,
|
|
||||||
'username' => $r->recipient->username,
|
|
||||||
'avatar' => $r->recipient->avatarUrl(),
|
|
||||||
'url' => $r->recipient->url(),
|
|
||||||
'isLocal' => (bool) !$r->recipient->domain,
|
|
||||||
'domain' => $r->recipient->domain,
|
|
||||||
'timeAgo' => $r->created_at->diffForHumans(null, true, true),
|
|
||||||
'lastMessage' => $r->status->caption,
|
|
||||||
'messages' => []
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json($dms);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(Request $request)
|
public function create(Request $request)
|
||||||
|
|
Loading…
Reference in a new issue