Skip to content

Commit

Permalink
Merge pull request #743 from TheRestartProject/RES-1992_archive_groups
Browse files Browse the repository at this point in the history
RES-1992 add ability to archive inactive groups
  • Loading branch information
edwh authored Nov 12, 2024
2 parents 8eacd60 + 1ea24f2 commit 675e94a
Show file tree
Hide file tree
Showing 37 changed files with 412 additions and 49 deletions.
1 change: 1 addition & 0 deletions app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Group extends Model implements Auditable
'phone',
'network_data',
'email',
'archived_at'
];

protected $appends = ['ShareableLink', 'auto_approve'];
Expand Down
2 changes: 0 additions & 2 deletions app/GroupTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class GroupTags extends Model
{
use HasFactory;

// This is a magic value which is set on the live system, and is used to identify inactive groups.
// It's not ideal to have this hardcoded, but the data exists.
const INACTIVE = 10;

protected $table = 'group_tags';
Expand Down
46 changes: 39 additions & 7 deletions app/Http/Controllers/API/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ public static function getGroupList()
* operationId="getGroupListv2",
* tags={"Groups"},
* summary="Get list of group names",
* @OA\Parameter(
* name="includeArchived",
* description="Include archived groups",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* )
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
Expand All @@ -252,14 +261,25 @@ public static function getGroupList()
*/

public static function listNamesv2(Request $request) {
$request->validate([
'includeArchived' => ['string', 'in:true,false'],
]);

// We only return the group id and name, for speed.
$groups = Group::select('idgroups', 'name')->get();
$query = Group::select('idgroups', 'name', 'archived_at');

if (!$request->has('includeArchived') || $request->get('includeArchived') == 'false') {
$query = $query->whereNull('archived_at');
}

$groups = $query->get();
$ret = [];

foreach ($groups as $group) {
$ret[] = [
'id' => $group->idgroups,
'name' => $group->name,
'archived_at' => $group->archived_at ? Carbon::parse($group->archived_at)->toIso8601String() : null
];
}

Expand Down Expand Up @@ -826,7 +846,13 @@ public function createGroupv2(Request $request) {
* property="network_data",
* @OA\Schema()
* ),
* )
* @OA\Property(
* property="archived_at",
* title="archived_at",
* description="If present, this group has been archived and is no longer active.",
* format="date-time",
* )
* )
* )
* ),
* @OA\Response(
Expand All @@ -845,7 +871,9 @@ public function createGroupv2(Request $request) {
public function updateGroupv2(Request $request, $idGroup) {
$user = $this->getUser();

list($name, $area, $postcode, $location, $phone, $website, $description, $timezone, $latitude, $longitude, $country, $network_data, $email) = $this->validateGroupParams(
list($name, $area, $postcode, $location, $phone, $website, $description, $timezone,
$latitude, $longitude, $country, $network_data, $email,
$archived_at) = $this->validateGroupParams(
$request,
false
);
Expand Down Expand Up @@ -874,10 +902,11 @@ public function updateGroupv2(Request $request, $idGroup) {
'email' => $email,
];

if ($user->hasRole('Administrator') || $user->hasRole('NetworkCoordinator')) {
// Not got permission to update these.
$data['area'] = $request->area;
$data['postcode'] = $request->postcode;
if ($user->hasRole('Administrator') || ($user->hasRole('NetworkCoordinator') && $isCoordinatorForGroup)) {
// Got permission to update these.
$data['area'] = $area;
$data['postcode'] = $postcode;
$data['archived_at'] = $archived_at;
}

if (isset($_FILES) && !empty($_FILES)) {
Expand Down Expand Up @@ -977,6 +1006,7 @@ private function validateGroupParams(Request $request, $create): array {
$request->validate([
'name' => ['max:255'],
'location' => ['max:255'],
'archived_at' => ['date'],
]);
}

Expand All @@ -990,6 +1020,7 @@ private function validateGroupParams(Request $request, $create): array {
$timezone = $request->input('timezone');
$network_data = $request->input('network_data');
$email = $request->input('email');
$archived_at = $request->input('archived_at');

$latitude = null;
$longitude = null;
Expand Down Expand Up @@ -1030,6 +1061,7 @@ private function validateGroupParams(Request $request, $create): array {
$country_code,
$network_data,
$email,
$archived_at
);
}
}
18 changes: 16 additions & 2 deletions app/Http/Controllers/API/NetworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public function getNetworkv2($id)
* type="boolean"
* )
* ),
* @OA\Parameter(
* name="includeArchived",
* description="Include archived groups",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* )
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
Expand Down Expand Up @@ -171,11 +180,16 @@ public function getNetworkGroupsv2(Request $request, $id)

// We use a query rather than $network->groups so that the filtering by date is done in the database rather
// than getting all groups and filtering in PHP. This is faster.
$groups = Group::join('group_network', 'group_network.group_id', '=', 'groups.idgroups')
$query = Group::join('group_network', 'group_network.group_id', '=', 'groups.idgroups')
->where('group_network.network_id', $id)
->where('groups.updated_at', '>=', $start)
->where('groups.updated_at', '<=', $end)->get();
->where('groups.updated_at', '<=', $end);

if (!$request->has('includeArchived') || $request->get('includeArchived') == 'false') {
$query = $query->whereNull('archived_at');
}

$groups = $query->get();

if ($request->get('includeDetails', false)) {
return \App\Http\Resources\GroupCollection::make($groups);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function index()
->whereNull('users_groups.deleted_at')
->orderBy('groups.name', 'ASC')
->groupBy('groups.idgroups', 'groups.name')
->select(['groups.idgroups', 'groups.name', 'users_groups.role'])
->select(['groups.idgroups', 'groups.name', 'users_groups.role', 'groups.archived_at'])
->take(5)
->get();

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Illuminate\Support\Facades\Log;
use Notification;
use Spatie\ValidationRules\Rules\Delimited;
use Carbon\Carbon;

class GroupController extends Controller
{
Expand Down Expand Up @@ -530,6 +531,7 @@ public static function expandGroups($groups, $your_groupids, $nearby_groupids)
'group_tags' => $group->group_tags()->get()->pluck('id'),
'following' => in_array($group->idgroups, $your_groupids),
'nearby' => in_array($group->idgroups, $nearby_groupids),
'archived_at' => $group->archived_at ? Carbon::parse($group->archived_at)->toIso8601String() : null
];
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Resources/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@
* title="updated_at",
* description="The last change to this group. This includes changes which affect the stats.",
* format="date-time",
* ),
* @OA\Property(
* property="archived_at",
* title="archived_at",
* description="If present, this group has been archived and is no longer active.",
* format="date-time",
* )
* )
*/
Expand Down Expand Up @@ -296,6 +302,7 @@ public function toArray($request)
'network_data' => $networkData,
'full' => true,
'email' => $this->email,
'archived_at' => $this->archived_at ? Carbon::parse($this->archived_at)->toIso8601String() : null
];

$ret['hosts'] = $this->resource->all_confirmed_hosts_count;
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Resources/GroupSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@
* description="Indicates that this is a summary result, not full group information.",
* format="boolean",
* example="true"
* )
* ),
* @OA\Property(
* property="archived_at",
* title="archived_at",
* description="If present, this group has been archived and is no longer active.",
* format="date-time",
* ),
* )
*/

Expand All @@ -88,6 +94,7 @@ public function toArray($request)
'location' => new GroupLocation($this),
'networks' => new NetworkSummaryCollection($this->resource->networks),
'updated_at' => Carbon::parse($this->updated_at)->toIso8601String(),
'archived_at' => $this->archived_at ? Carbon::parse($this->archived_at)->toIso8601String() : null,
'summary' => true
];

Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/AddUserToDiscourseGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function handle(UserFollowedGroup $event)
return;
}

if ($event->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

// add user to the network groups for the group the user followed.
$repairGroup = $event->group;
$user = $event->user;
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/AddUserToDiscourseThreadForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function handle(UserConfirmedEvent $e) {
$event = Party::find($e->idevents);
$user = User::find($e->iduser);

if ($event->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

// Might not exist - timing windows.
if ($event && $user && $event->discourse_thread) {
// We need a host of the event to add the user to the thread.
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/CreateDiscourseGroupForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function handle(ApproveGroup $event)
return;
}

if ($event->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

$group = $event->group;
$group->createDiscourseGroup();
}
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/CreateDiscourseThreadForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function handle(ApproveEvent $event)
return;
}

if ($theParty->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

// Get the user who created the event.
$users = EventsUsers::where('event', $partyId)->get();

Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/CreateWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function handle(ApproveEvent $event)
return;
}

if ($theParty->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

$theParty->approved = true;
$theParty->save();

Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/CreateWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function handle(ApproveGroup $event)
return;
}

if ($group->archived_at) {
// Suppress notifications for archived groups.
return;
}

$group->approved = true;
$group->save();

Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/DeleteEventFromWordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function handle(EventDeleted $event)

$repairEvent = $event->repairEvent;

if ($repairEvent->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

try {
if ($repairEvent->shouldPushToWordPress() && ! empty($repairEvent->wordpress_post_id)) {
$this->wpClient->deletePost($repairEvent->wordpress_post_id);
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/EditWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function handle(EditEvent $event)
return;
}

if ($theParty->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

try {
if (is_numeric($theParty->wordpress_post_id)) {
$startTimestamp = strtotime($theParty->event_start_utc);
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/EditWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function handle(EditGroup $event)
return;
}

if ($group->archived_at) {
// Suppress notifications for archived groups.
return;
}

try {
if (is_numeric($group->wordpress_post_id)) {
$custom_fields = [
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/NotifyApprovedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function handle(ApproveEvent $event)

$group = Group::findOrFail($theParty->group);

if ($group->archived_at) {
// Suppress notifications for archived groups.
return;
}

// Only send notifications if the event is in the future.
// We don't want to send emails to Restarters about past events being added.
if ($theParty->isUpcoming()) {
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/RemoveUserFromDiscourseThreadForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function handle(UserLeftEvent $e) {
$event = Party::find($e->idevents);
$user = User::find($e->iduser);

if ($event->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

// Might not exist - timing windows.
if ($event && $user && $event->discourse_thread) {
// We need a host of the event to add the user to the thread.
Expand Down
5 changes: 5 additions & 0 deletions app/Listeners/SendAdminModerateEventPhotosNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function handle(EventImagesUploaded $event)
$this->event = $event;
$this->party = $event->party;

if ($this->party->theGroup->archived_at) {
// Suppress notifications for archived groups.
return;
}

Fixometer::usersWhoHavePreference('admin-moderate-event-photos')->each(function (User $user) {
if ($this->shouldSendNotification($user)) {
$user->notify(new AdminModerationEventPhotos([
Expand Down
8 changes: 2 additions & 6 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ public function groupsNearby($numberOfGroups = 10, $createdSince = null, $nearby

$groupsNearbyQuery = Group::select(
DB::raw('*, ( 6371 * acos( cos( radians('.$this->latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$this->longitude.') ) + sin( radians('.$this->latitude.') ) * sin( radians( latitude ) ) ) ) AS dist')
)->leftJoin('grouptags_groups', function ($join) {
// Exclude any groups tagged with the special value of 10, which is 'Inactive'.
$join->on('group', '=', 'idgroups');
$join->on('group_tag', '=', DB::raw(GroupTags::INACTIVE));
})->where(function ($q) {
$q->whereNull('grouptags_groups.id');
)->where(function ($q) {
$q->whereNull('archived_at');

// Only show approved groups.
$q->where('approved', true);
Expand Down
Loading

0 comments on commit 675e94a

Please sign in to comment.