Skip to content

Commit

Permalink
Make room commands take room names optionally, fix access for isusera…
Browse files Browse the repository at this point in the history
…llowed and make room commands work from outside the room
  • Loading branch information
cmaish committed May 21, 2013
1 parent bf2d5a4 commit 8fc9aa5
Show file tree
Hide file tree
Showing 18 changed files with 645 additions and 75 deletions.
570 changes: 555 additions & 15 deletions JabbR.Tests/CommandManagerFacts.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion JabbR/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@
};

chat.client.userUnallowed = function (user, room) {
ui.addMessage('You have revoked ' + user + '"s access to ' + room, 'notification', this.state.activeRoom);
ui.addMessage('You have revoked ' + user + '\'s access to ' + room, 'notification', this.state.activeRoom);
};

// Called when you make someone an owner
Expand Down
7 changes: 4 additions & 3 deletions JabbR/Commands/AddOwnerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JabbR.Commands
{
[Command("addowner", "Add an owner a user as an owner to the specified room. Only works if you're an owner of that room.", "user room", "room")]
[Command("addowner", "Add an owner a user as an owner to the specified room. Only works if you're an owner of that room.", "user [room]", "room")]
public class AddOwnerCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand All @@ -17,12 +17,13 @@ public override void Execute(CommandContext context, CallerContext callerContext

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);

if (args.Length == 1)
string roomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room?");
}

string roomName = args[1];
ChatRoom targetRoom = context.Repository.VerifyRoom(roomName);

context.Service.AddOwner(callingUser, targetUser, targetRoom);
Expand Down
7 changes: 4 additions & 3 deletions JabbR/Commands/AllowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JabbR.Commands
{
[Command("allow", "Give a user permission to a private room. Only works if you're an owner of that room.", "user room", "room")]
[Command("allow", "Give a user permission to a private room. Only works if you're an owner of that room.", "user [room]", "room")]
public class AllowCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand All @@ -17,12 +17,13 @@ public override void Execute(CommandContext context, CallerContext callerContext

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);

if (args.Length == 1)
string roomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room?");
}

string roomName = args[1];
ChatRoom targetRoom = context.Repository.VerifyRoom(roomName);

context.Service.AllowUser(callingUser, targetUser, targetRoom);
Expand Down
2 changes: 1 addition & 1 deletion JabbR/Commands/AllowedCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override void Execute(CommandContext context, CallerContext callerContext
{
string targetRoomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(targetRoomName) || targetRoomName.Equals("Lobby", StringComparison.InvariantCultureIgnoreCase))
if (String.IsNullOrEmpty(targetRoomName))
{
throw new InvalidOperationException("Which room?");
}
Expand Down
7 changes: 4 additions & 3 deletions JabbR/Commands/CloseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace JabbR.Commands
{
[Command("close", "Close a room. Only works if you're an owner of that room.", "room", "room")]
[Command("close", "Close a room. Only works if you're an owner of that room.", "[room]", "room")]
public class CloseCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
if (args.Length == 0)
string roomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room do you want to close?");
}

string roomName = args[0];
ChatRoom room = context.Repository.VerifyRoom(roomName);

// Before I close the room, I need to grab a copy of -all- the users in that room.
Expand Down
21 changes: 16 additions & 5 deletions JabbR/Commands/InviteCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@

namespace JabbR.Commands
{
[Command("invitecode", "Show the current invite code.", "", "room")]
[Command("invitecode", "Show the current invite code.", "[room]", "room")]
public class InviteCodeCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
ChatRoom room = context.Repository.VerifyUserRoom(context.Cache, callingUser, callerContext.RoomName);
string targetRoomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(room.InviteCode))
if (String.IsNullOrEmpty(targetRoomName))
{
context.Service.SetInviteCode(callingUser, room, RandomUtils.NextInviteCode());
throw new InvalidOperationException("Which room?");
}

context.NotificationService.PostNotification(room, callingUser, String.Format("Invite Code for this room: {0}", room.InviteCode));
ChatRoom targetRoom = context.Repository.VerifyRoom(targetRoomName, mustBeOpen: false);

// ensure the user could join the room if they wanted to
callingUser.EnsureAllowed(targetRoom);

if (String.IsNullOrEmpty(targetRoom.InviteCode))
{
context.Service.SetInviteCode(callingUser, targetRoom, RandomUtils.NextInviteCode());
}

ChatRoom callingRoom = context.Repository.GetRoomByName(callerContext.RoomName);
context.NotificationService.PostNotification(callingRoom, callingUser, String.Format("Invite Code for {0}: {1}", targetRoomName, targetRoom.InviteCode));
}
}
}
7 changes: 4 additions & 3 deletions JabbR/Commands/InviteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JabbR.Commands
{
[Command("invite", "Invite a user to join a room.", "user room", "room")]
[Command("invite", "Invite a user to join a room.", "user [room]", "room")]
public class InviteCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand All @@ -22,12 +22,13 @@ public override void Execute(CommandContext context, CallerContext callerContext
throw new InvalidOperationException("You can't invite yourself!");
}

if (args.Length == 1)
string roomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Invite them to which room?");
}

string roomName = args[1];
ChatRoom targetRoom = context.Repository.VerifyRoom(roomName);

context.NotificationService.Invite(callingUser, targetUser, targetRoom);
Expand Down
19 changes: 10 additions & 9 deletions JabbR/Commands/KickCommand.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
using System;
using System.Linq;
using JabbR.Models;

namespace JabbR.Commands
{
[Command("kick", "Kick a user from the room. Note, this is only valid for owners of the room.", "user", "user")]
[Command("kick", "Kick a user from the room. Note, this is only valid for owners of the room.", "user [room]", "user")]
public class KickCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, Models.ChatUser callingUser, string[] args)
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
if (args.Length == 0)
{
throw new InvalidOperationException("Who are you trying to kick?");
}

ChatRoom room = context.Repository.VerifyUserRoom(context.Cache, callingUser, callerContext.RoomName);
string targetUserName = args[0];

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);

string targetRoomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (context.Repository.GetOnlineUsers(room).Count() == 1)
if (String.IsNullOrEmpty(targetRoomName))
{
throw new InvalidOperationException("You're the only person in here...");
throw new InvalidOperationException("Which room?");
}

string targetUserName = args[0];

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);
ChatRoom room = context.Repository.VerifyRoom(targetRoomName);

context.Service.KickUser(callingUser, targetUser, room);

Expand Down
16 changes: 7 additions & 9 deletions JabbR/Commands/LeaveCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JabbR.Models;
using System;
using JabbR.Models;

namespace JabbR.Commands
{
Expand All @@ -7,17 +8,14 @@ public class LeaveCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
ChatRoom room = null;
if (args.Length == 0)
string targetRoomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(targetRoomName))
{
room = context.Repository.VerifyUserRoom(context.Cache, callingUser, callerContext.RoomName);
throw new InvalidOperationException("Which room?");
}
else
{
string roomName = args[0];

room = context.Repository.VerifyRoom(roomName, mustBeOpen: false);
}
ChatRoom room = context.Repository.VerifyRoom(targetRoomName);

context.Service.LeaveRoom(callingUser, room);

Expand Down
10 changes: 7 additions & 3 deletions JabbR/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

namespace JabbR.Commands
{
[Command("list", "Show a list of users in the room.", "room", "room")]
[Command("list", "Show a list of users in the room.", "[room]", "room")]
public class ListCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
if (args.Length == 0)
string roomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("List users in which room?");
}

string roomName = args[0];
ChatRoom room = context.Repository.VerifyRoom(roomName);

// ensure the user could join the room if they wanted to
callingUser.EnsureAllowed(room);

var names = context.Repository.GetOnlineUsers(room).Select(s => s.Name);

context.NotificationService.ListUsers(room, names);
Expand Down
7 changes: 4 additions & 3 deletions JabbR/Commands/LockCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

namespace JabbR.Commands
{
[Command("lock", "Make a room private. Only works if you're the creator of that room.", "room", "room")]
[Command("lock", "Make a room private. Only works if you're the creator of that room.", "[room]", "room")]
public class LockCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
if (args.Length == 0)
string roomName = args.Length > 0 ? args[0] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room do you want to lock?");
}

string roomName = args[0];
ChatRoom room = context.Repository.VerifyRoom(roomName);

context.Service.LockRoom(callingUser, room);
Expand Down
7 changes: 4 additions & 3 deletions JabbR/Commands/RemoveOwnerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JabbR.Commands
{
[Command("removeowner", "Remove an owner from the specified room. Only works if you're the creator of that room.", "user room", "room")]
[Command("removeowner", "Remove an owner from the specified room. Only works if you're the creator of that room.", "user [room]", "room")]
public class RemoveOwnerCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand All @@ -17,12 +17,13 @@ public override void Execute(CommandContext context, CallerContext callerContext

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);

if (args.Length == 1)
string roomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room?");
}

string roomName = args[1];
ChatRoom targetRoom = context.Repository.VerifyRoom(roomName);

context.Service.RemoveOwner(callingUser, targetUser, targetRoom);
Expand Down
21 changes: 16 additions & 5 deletions JabbR/Commands/ResetInviteCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@

namespace JabbR.Commands
{
[Command("resetinvitecode", "Reset the current invite code. This will render the previous invite code invalid.", "", "room")]
[Command("resetinvitecode", "Reset the current invite code. This will render the previous invite code invalid.", "[room]", "room")]
public class ResetInviteCodeCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, Models.ChatUser callingUser, string[] args)
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
{
ChatRoom room = context.Repository.VerifyUserRoom(context.Cache, callingUser, callerContext.RoomName);
string targetRoomName = args.Length > 0 ? args[0] : callerContext.RoomName;

context.Service.SetInviteCode(callingUser, room, RandomUtils.NextInviteCode());
if (String.IsNullOrEmpty(targetRoomName))
{
throw new InvalidOperationException("Which room?");
}

context.NotificationService.PostNotification(room, callingUser, String.Format("Invite Code for this room: {0}", room.InviteCode));
ChatRoom targetRoom = context.Repository.VerifyRoom(targetRoomName, mustBeOpen: false);

// ensure the user could join the room if they wanted to
callingUser.EnsureAllowed(targetRoom);

context.Service.SetInviteCode(callingUser, targetRoom, RandomUtils.NextInviteCode());

ChatRoom callingRoom = context.Repository.GetRoomByName(callerContext.RoomName);
context.NotificationService.PostNotification(callingRoom, callingUser, String.Format("Invite Code for {0}: {1}", targetRoomName, targetRoom.InviteCode));
}
}
}
7 changes: 4 additions & 3 deletions JabbR/Commands/UnAllowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JabbR.Commands
{
[Command("unallow", "Revoke a user's permission to a private room. Only works if you're an owner of that room.", "user room", "room")]
[Command("unallow", "Revoke a user's permission to a private room. Only works if you're an owner of that room.", "user [room]", "room")]
public class UnAllowCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand All @@ -17,12 +17,13 @@ public override void Execute(CommandContext context, CallerContext callerContext

ChatUser targetUser = context.Repository.VerifyUser(targetUserName);

if (args.Length == 1)
string roomName = args.Length > 1 ? args[1] : callerContext.RoomName;

if (String.IsNullOrEmpty(roomName))
{
throw new InvalidOperationException("Which room?");
}

string roomName = args[1];
ChatRoom targetRoom = context.Repository.VerifyRoom(roomName);

context.Service.UnallowUser(callingUser, targetUser, targetRoom);
Expand Down
2 changes: 1 addition & 1 deletion JabbR/Commands/WelcomeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace JabbR.Commands
{
[Command("welcome", "Set the room's welcome message. Type welcome to clear the room's welcome message.", "message", "room")]
[Command("welcome", "Set the room's welcome message. Type welcome to clear the room's welcome message.", "[message]", "room")]
public class WelcomeCommand : UserCommand
{
public override void Execute(CommandContext context, CallerContext callerContext, ChatUser callingUser, string[] args)
Expand Down
3 changes: 2 additions & 1 deletion JabbR/Hubs/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,10 @@ void INotificationService.SendPrivateMessage(ChatUser fromUser, ChatUser toUser,

void INotificationService.PostNotification(ChatRoom room, ChatUser user, string message)
{
string roomName = room == null ? null : room.Name;
foreach (var client in user.ConnectedClients)
{
Clients.Client(client.Id).postNotification(message, room.Name);
Clients.Client(client.Id).postNotification(message, roomName);
}
}

Expand Down
5 changes: 1 addition & 4 deletions JabbR/Models/ModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JabbR.Models
{
Expand All @@ -18,7 +15,7 @@ public static void EnsureAllowed(this ChatUser user, ChatRoom room)

public static bool IsUserAllowed(this ChatRoom room, ChatUser user)
{
return room.AllowedUsers.Contains(user) || user.IsAdmin;
return room.AllowedUsers.Contains(user) || room.Owners.Contains(user) || user.IsAdmin;
}
}
}

0 comments on commit 8fc9aa5

Please sign in to comment.