Skip to content
This repository was archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
Added public group search and listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Dern committed Nov 22, 2020
1 parent fa115bb commit e507f4d
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 75 deletions.
71 changes: 61 additions & 10 deletions app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Http\Requests\Groups\InviteUserRequest;
use Notification;
use App\Notifications\InvitedToJoinGroup;
use App\Notifications\AsksToJoinGroup;

class GroupController extends Controller
{
Expand All @@ -26,9 +27,21 @@ public function __construct()
*/
public function index(Request $request)
{
$user = $request->user();
$user = $request->user();
$search = $request->input('search');

$query = Group::visible()
->whereNotIn('id', $user->groups->pluck('id'))
->with('creator:id,name')
->withCount('activeUsers');

if (!empty($search)) {
$query = $query->where('groups.name', 'like', '%' . $search . '%');
}

return Group::visible()->whereNotIn('id', $user->groups->pluck('id'))->withCount('activeUsers')->simplePaginate(25);
return $query
->orderBy('name')
->simplePaginate(25);
}

/**
Expand All @@ -55,10 +68,10 @@ public function indexMyGroups(Request $request)
$user = $request->user();

return $user->groups()->withCount('activeUsers', 'pendingUsers')
->whereNotIn('status', [
Group::$STATUS_REJECTED,
Group::$STATUS_LEFT
])->orderBy('position')->orderBy('id')->get();
->whereNotIn('status', [
Group::$STATUS_REJECTED,
Group::$STATUS_LEFT
])->orderBy('position')->orderBy('id')->get();
}

/**
Expand Down Expand Up @@ -108,9 +121,10 @@ public function update(UpdateRequest $request, Group $group)
{
$validated = $request->validated();

$group->name = $validated['name'];
$group->description = $validated['description'];
$group->invite_only = $validated['invite_only'];
$group->name = $validated['name'];
$group->description = $validated['description'];
$group->invite_only = $validated['invite_only'];
$group->auto_accept_users = $validated['auto_accept_users'];

$group->save();

Expand Down Expand Up @@ -175,6 +189,12 @@ public function updatePositions(Request $request)
*/
public function inviteUser(InviteUserRequest $request, Group $group)
{
$user = $request->user();

if (!$user->can('invite', $group)) {
abort(403);
}

$validated = $request->validated();
$invitedUser = User::where('email', $validated['email'])->first();

Expand Down Expand Up @@ -204,6 +224,21 @@ public function acceptInvitation(Request $request, Group $group)
}
}

public function approveUser(Request $request, Group $group, User $user)
{
$creator = $request->user();

if (!$creator->can('approve', $group)) {
abort(403);
}

$user->updateGroupStatus($group, Group::$STATUS_ACCEPTED);

//TODO: Add history entry

return redirect()->route('account.groups');
}

public function rejectInvitation(Request $request, Group $group)
{
$user = $request->user();
Expand All @@ -217,7 +252,23 @@ public function rejectInvitation(Request $request, Group $group)
public function leave(Request $request, Group $group)
{
$user = $request->user();
$user->updateGroupStatus($group, Group::$STATUS_LEFT);
$user->groups()->detach($group);

//TODO: Add history entry
}

public function join(Request $request, Group $group)
{
$user = $request->user();

if ($group->auto_accept_users) {
$user->updateGroupStatus($group, Group::$STATUS_ACCEPTED);
} else {
$user->updateGroupStatus($group, Group::$STATUS_JOINING);

Notification::route('mail', $group->creator->email)
->notify(new AsksToJoinGroup($request->user(), $group));
}

//TODO: Add history entry
}
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Requests/Groups/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function rules()
'invite_only' => [
'boolean',
],
'auto_accept_users' => [
'boolean',
],
];
}
}
3 changes: 3 additions & 0 deletions app/Http/Requests/Groups/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function rules()
'invite_only' => [
'boolean',
],
'auto_accept_users' => [
'boolean',
],
];
}
}
6 changes: 4 additions & 2 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Group extends Model
'description',
'user_id',
'invite_only',
'auto_accept_users'
];

/**
Expand All @@ -72,8 +73,9 @@ class Group extends Model
* @var array
*/
protected $casts = [
'invite_only' => 'boolean',
'feed_item_states_count' => 'integer',
'invite_only' => 'boolean',
'auto_accept_users' => 'boolean',
'feed_item_states_count' => 'integer',
];

/**
Expand Down
29 changes: 23 additions & 6 deletions app/Models/Policies/GroupPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function viewAny(User $user)
*/
public function view(User $user, Group $group)
{
return $this->checkGroupAuthorization($user, $group);
return $this->checkGroupAuthorization($user, $group, [
Group::$STATUS_ACCEPTED
]);
}

/**
Expand Down Expand Up @@ -71,6 +73,21 @@ public function invite(User $user, Group $group)
]);
}

/**
* Determine whether the user can approve someone to join specified group.
*
* @param \App\Models\User $user
* @param \App\Models\Group $group
* @return mixed
*/
public function approve(User $user, Group $group)
{
return $this->checkGroupAuthorization($user, $group, [
Group::$STATUS_OWN,
Group::$STATUS_CREATED,
]);
}

/**
* Determine whether the user can delete the model.
*
Expand Down Expand Up @@ -122,12 +139,12 @@ private function checkGroupAuthorization(User $user, Group $group, $statuses = [
}

$userGroup = $user->groups()->active()->find($group->id);

if (!empty($userGroup)) {
return true;
if (!$userGroup) {
return false;
}

if (!empty($statuses) && in_array($userGroup->pivot->status, $statuses)) {
if (!empty($statuses) && $userGroup->pivot && in_array($userGroup->pivot->status, $statuses)) {
return true;
}

Expand Down
83 changes: 83 additions & 0 deletions app/Notifications/AsksToJoinGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\URL;
use App\Models\User;
use App\Models\Group;

class AsksToJoinGroup extends Notification implements ShouldQueue
{
use Queueable;

/**
* Inviting user
*
* @var \App\Models\User
*/
protected $user;

/**
* Group to invite a user in
*
* @var \App\Models\Group
*/
protected $group;

/**
* Create a new notification instance.
*
* @param \App\Models\User $user User asking to join group
* @param \App\Models\Group $group
* @return void
*/
public function __construct(User $user, Group $group)
{
$this->user = $user;
$this->group = $group;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->from(env('MAIL_FROM_ADDRESS'))
->line(sprintf('Hello there ! %s wants to join a group you created in Cyca: %s.', $this->user->name, $this->group->name))
->action(sprintf('Accept %s in %s', $this->user->name, $this->group->name), URL::signedRoute('group.signed_approve_user', ['user' => $this->user->id, 'group' => $this->group->id]))
->line('You can safely ignore this email if you prefer to decline.')
->line('Thank you for using our application!');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
|--------------------------------------------------------------------------
*/

'version' => '0.8.9',
'version' => '0.8.10',

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'group.index',
'group.index_active',
'group.invite_user',
'group.join',
'group.leave',
'group.my_groups',
'group.reject_invitation',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddAutoAcceptUsersToGroupsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('groups', function (Blueprint $table) {
$table->boolean('auto_accept_users')->default(false)->after('invite_only');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('groups', function (Blueprint $table) {
$table->dropColumn(['auto_accept_users']);
});
}
}
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/groups.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/highlights.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/history.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/import.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/themes-browser.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"/js/app.js": "/js/app.js?id=1d555fd70203cffc9c80",
"/themes/cyca-dark/theme.css": "/themes/cyca-dark/theme.css?id=e29a17c9206c1115cdc5",
"/themes/cyca-light/theme.css": "/themes/cyca-light/theme.css?id=8477ce9a3672bfafbcf7",
"/js/groups.js": "/js/groups.js?id=b2ec2701fad753638af7",
"/js/highlights.js": "/js/highlights.js?id=16f38cce999df6250471",
"/js/history.js": "/js/history.js?id=8087cec05785ced6f72b",
"/js/import.js": "/js/import.js?id=71b164490001afbe4d7c",
"/js/themes-browser.js": "/js/themes-browser.js?id=55ba7c074c4c4e52bc19",
"/js/app.js": "/js/app.js?id=78811c630a18dc4c3b90",
"/themes/cyca-dark/theme.css": "/themes/cyca-dark/theme.css?id=afb6085e0137be5723c3",
"/themes/cyca-light/theme.css": "/themes/cyca-light/theme.css?id=be5a16cf0e6f85afa3b1",
"/js/groups.js": "/js/groups.js?id=f8ceca1188bafecc6853",
"/js/highlights.js": "/js/highlights.js?id=e22cbaca83e2f10cef32",
"/js/history.js": "/js/history.js?id=04f1837a5d38b6c7385f",
"/js/import.js": "/js/import.js?id=dc4db8077d2231b2ff27",
"/js/themes-browser.js": "/js/themes-browser.js?id=9590890f2ae0200cf94c",
"/themes/cyca-dark/theme.json": "/themes/cyca-dark/theme.json?id=e6af9f523b70bc467aa4",
"/themes/cyca-light/theme.json": "/themes/cyca-light/theme.json?id=bb59033be8f29999311e"
}
8 changes: 8 additions & 0 deletions resources/css/base/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ textarea {
background-color: theme("colors.input.bg");
}

.form-group input.alt,
input.alt,
select.alt,
textarea.alt {
color: theme("colors.input.alt.text");
background-color: theme("colors.input.alt.bg");
}

input[type="color"] {
@apply p-1;
}
2 changes: 1 addition & 1 deletion resources/css/components/badge.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* Badge - used to display number of unread feed items, for instance */
.badge {
@apply flex-none py-0.5 rounded text-xs px-2 ml-2;
@apply flex-none py-0.5 rounded text-xs px-2 ml-2 whitespace-no-wrap;
}

.badge.default {
Expand Down
Loading

0 comments on commit e507f4d

Please sign in to comment.