| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Activity; |
| 6 |
use FluentCommunity\App\Models\XProfile; |
| 7 |
use FluentCommunity\App\Services\Helper; |
| 8 |
|
| 9 |
class ActivityMonitorHandler |
| 10 |
{ |
| 11 |
public function register() |
| 12 |
{ |
| 13 |
add_action('fluent_community/comment_added', [$this, 'handleNewCommentEvent'], 10, 2); |
| 14 |
add_action('fluent_community/feed/created', [$this, 'handleFeedCreated'], 10, 1); |
| 15 |
|
| 16 |
add_action('fluent_communit/track_activity', [$this, 'trackActivity'], 10); |
| 17 |
|
| 18 |
add_action('profile_update', function ($userId) { |
| 19 |
$user = get_user_by('ID', $userId); |
| 20 |
$xprofile = XProfile::where('user_id', $userId)->first(); |
| 21 |
if (!$xprofile) { |
| 22 |
return; |
| 23 |
} |
| 24 |
$firstName = $user->first_name; |
| 25 |
$lastName = $user->last_name; |
| 26 |
$displayName = trim($firstName . ' ' . $lastName); |
| 27 |
if ($displayName && $xprofile->display_name != $displayName) { |
| 28 |
$xprofile->display_name = $displayName; |
| 29 |
$xprofile->save(); |
| 30 |
} |
| 31 |
}); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
public function handleNewCommentEvent($comment, $feed) |
| 37 |
{ |
| 38 |
$isPublic = 1; |
| 39 |
|
| 40 |
if ($feed->space_id) { |
| 41 |
$isPublic = $feed->space->privacy == 'public'; |
| 42 |
} |
| 43 |
|
| 44 |
$data = [ |
| 45 |
'user_id' => $comment->user_id, |
| 46 |
'feed_id' => $feed->id, |
| 47 |
'space_id' => $feed->space_id, |
| 48 |
'related_id' => $comment->id, |
| 49 |
'action_name' => 'comment_added', |
| 50 |
'is_public' => $isPublic, |
| 51 |
]; |
| 52 |
|
| 53 |
Activity::create($data); |
| 54 |
|
| 55 |
do_action('fluent_communit/track_activity'); |
| 56 |
} |
| 57 |
|
| 58 |
public function handleFeedCreated($feed) |
| 59 |
{ |
| 60 |
$isPublic = 1; |
| 61 |
|
| 62 |
if ($feed->space_id) { |
| 63 |
$isPublic = $feed->space->privacy == 'public'; |
| 64 |
} |
| 65 |
|
| 66 |
$data = [ |
| 67 |
'user_id' => $feed->user_id, |
| 68 |
'feed_id' => $feed->id, |
| 69 |
'space_id' => $feed->space_id, |
| 70 |
'action_name' => 'feed_published', |
| 71 |
'is_public' => $isPublic, |
| 72 |
]; |
| 73 |
|
| 74 |
Activity::create($data); |
| 75 |
|
| 76 |
do_action('fluent_communit/track_activity'); |
| 77 |
} |
| 78 |
|
| 79 |
public function trackActivity() |
| 80 |
{ |
| 81 |
$currentProfile = Helper::getCurrentProfile(); |
| 82 |
|
| 83 |
if (!$currentProfile) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$currentProfile->last_activity = current_time('mysql'); |
| 88 |
$currentProfile->save(); |
| 89 |
} |
| 90 |
} |
| 91 |
|