| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Http\Controllers\Controller; |
| 6 |
use FluentCommunity\App\Models\Activity; |
| 7 |
use FluentCommunity\App\Models\Feed; |
| 8 |
use FluentCommunity\App\Models\Notification; |
| 9 |
use FluentCommunity\App\Models\NotificationSubscriber; |
| 10 |
use FluentCommunity\App\Models\BaseSpace; |
| 11 |
use FluentCommunity\App\Models\SpaceUserPivot; |
| 12 |
use FluentCommunity\App\Services\ProfileHelper; |
| 13 |
use FluentCommunity\Framework\Http\Request\Request; |
| 14 |
use FluentCommunity\Framework\Support\Arr; |
| 15 |
|
| 16 |
class ActivityController extends Controller |
| 17 |
{ |
| 18 |
public function getActivities(Request $request) |
| 19 |
{ |
| 20 |
$context = $request->get('context', []); |
| 21 |
|
| 22 |
$activities = Activity::where(function ($q) { |
| 23 |
$userId = get_current_user_id(); |
| 24 |
$q->where('is_public', 1); |
| 25 |
if (!$userId) { |
| 26 |
return $q; |
| 27 |
} |
| 28 |
|
| 29 |
$q->orWhere(function ($query) use ($userId) { |
| 30 |
$spaceIds = get_user_meta($userId, '_fcom_space_ids', true); |
| 31 |
if ($spaceIds) { |
| 32 |
$query->whereIn('space_id', $spaceIds); |
| 33 |
return $query; |
| 34 |
} |
| 35 |
}); |
| 36 |
}) |
| 37 |
->whereIn('action_name', ['feed_published', 'comment_added']) |
| 38 |
->with(['xprofile' => function ($q) { |
| 39 |
$q->select(ProfileHelper::getXProfilePublicFields()); |
| 40 |
}, 'feed', 'space']) |
| 41 |
->orderBy('id', 'DESC') |
| 42 |
->groupBy('feed_id', 'action_name'); |
| 43 |
|
| 44 |
$spaceId = null; |
| 45 |
$userId = null; |
| 46 |
|
| 47 |
if ($context && !empty($context['space_id'])) { |
| 48 |
$spaceId = (int) $context['space_id']; |
| 49 |
$activities->where('space_id', $context['space_id']); |
| 50 |
} else if($context && !empty($context['user_id'])) { |
| 51 |
$userId = (int) $context['user_id']; |
| 52 |
$activities->where('user_ID', $context['user_id']); |
| 53 |
} |
| 54 |
|
| 55 |
$activities = $activities->paginate(); |
| 56 |
|
| 57 |
$formattedActivities = []; |
| 58 |
|
| 59 |
foreach ($activities as $activity) { |
| 60 |
$message = $activity->getFormattedMessage(); |
| 61 |
if (!$message) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$route = $activity->feed->getJsRoute(); |
| 66 |
|
| 67 |
if ($activity->action_name == 'comment_added') { |
| 68 |
$route['query'] = [ |
| 69 |
'comment_id' => $activity->related_id |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
$formattedActivities[] = [ |
| 74 |
'id' => $activity->id, |
| 75 |
'message' => $message, |
| 76 |
'xprofile' => $activity->xprofile, |
| 77 |
'updated_at' => $activity->updated_at->format('Y-m-d H:i:s'), |
| 78 |
'route' => $route |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
if($spaceId) { |
| 83 |
$afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context); |
| 84 |
} else if($userId) { |
| 85 |
$afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context); |
| 86 |
} else { |
| 87 |
$afterContent = apply_filters('fluent_community/activity/after_contents', '', $context); |
| 88 |
} |
| 89 |
|
| 90 |
$returnData = [ |
| 91 |
'activities' => [ |
| 92 |
'data' => $formattedActivities, |
| 93 |
'total' => $activities->total(), |
| 94 |
'per_page' => $activities->perPage(), |
| 95 |
'current_page' => $activities->currentPage(), |
| 96 |
], |
| 97 |
'after_contents' => $afterContent, |
| 98 |
]; |
| 99 |
|
| 100 |
if(!$spaceId) { |
| 101 |
|
| 102 |
if(!$userId) { |
| 103 |
$returnData['pinned_posts'] = $this->getPinnedPosts(null, true); |
| 104 |
} |
| 105 |
|
| 106 |
return $returnData; |
| 107 |
} |
| 108 |
|
| 109 |
if ($request->get('with_pins')) { |
| 110 |
$returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending')); |
| 111 |
} |
| 112 |
|
| 113 |
if ($request->get('with_pening_count')) { |
| 114 |
$pendingCount = 0; |
| 115 |
$user = $this->getUser(); |
| 116 |
|
| 117 |
$space = BaseSpace::find($spaceId); |
| 118 |
|
| 119 |
if ($user && $space && $user->can('can_add_member', $space)) { |
| 120 |
$pendingCount = SpaceUserPivot::bySpace($space->id) |
| 121 |
->where('status', 'pending') |
| 122 |
->count(); |
| 123 |
} |
| 124 |
|
| 125 |
$returnData['pending_count'] = $pendingCount; |
| 126 |
} |
| 127 |
|
| 128 |
return $returnData; |
| 129 |
} |
| 130 |
|
| 131 |
private function getPinnedPosts($spaceId = null, $isTrending = false) |
| 132 |
{ |
| 133 |
$postsQuery = Feed::when($spaceId, function ($q) use ($spaceId) { |
| 134 |
$q->where('space_id', $spaceId); |
| 135 |
}) |
| 136 |
->byUserAccess(get_current_user_id()) |
| 137 |
->where('status', 'published') |
| 138 |
->with(['xprofile' => function ($q) { |
| 139 |
$q->select(ProfileHelper::getXProfilePublicFields()); |
| 140 |
}], 'space') |
| 141 |
->limit(5); |
| 142 |
|
| 143 |
if ($isTrending && !$spaceId) { |
| 144 |
// Find trending posts which are created within last 7 days and order by reactions and comments count |
| 145 |
// make the comments_count * 2 to give more priority to comments |
| 146 |
$postsQuery->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-7 days'))) |
| 147 |
->orderByRaw('(reactions_count + (comments_count * 2)) DESC'); |
| 148 |
} else { |
| 149 |
$postsQuery->orderBy('id', 'DESC') |
| 150 |
->where('priority', 1); |
| 151 |
} |
| 152 |
|
| 153 |
$posts = $postsQuery->get(); |
| 154 |
|
| 155 |
$formattedActivities = []; |
| 156 |
|
| 157 |
foreach ($posts as $post) { |
| 158 |
$formattedActivities[] = [ |
| 159 |
'id' => $post->id, |
| 160 |
'message' => $post->getHumanExcerpt(100), |
| 161 |
'permalink' => $post->getPermalink(), |
| 162 |
'xprofile' => $post->xprofile, |
| 163 |
'created_at' => $post->created_at->format('Y-m-d H:i:s'), |
| 164 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
return $formattedActivities; |
| 168 |
} |
| 169 |
} |
| 170 |
|