diff --git a/.gitignore b/.gitignore index 4abe20b..1a9130b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ # https://dart.dev/guides/libraries/private-files # Created by `dart pub` .dart_tool/ -.env \ No newline at end of file +.env + +/app_logs.log \ No newline at end of file diff --git a/lib/prompt_chat.dart b/lib/prompt_chat.dart index 08fed2f..edb8bdc 100644 --- a/lib/prompt_chat.dart +++ b/lib/prompt_chat.dart @@ -40,12 +40,12 @@ class ChatAPI { // Register a user Future registerUser(String? username, String? password) async { if (username == null || password == null) { - throw InvalidCredentialsException(); } if (!isPasswordValid(password)) { - print("Password must be atleast 8 characters long, having atleast a number & a special character. Please try again."); - throw WeakPasswordException(); + print( + "Password must be atleast 8 characters long, having atleast a number & a special character. Please try again."); + throw WeakPasswordException(); } validUsername(username); var newUser = User(username, password, false); @@ -221,6 +221,9 @@ class ChatAPI { throw Exception( "Please enter the required credentials, or login to continue."); } + if (servers.any((element) => element.serverName == serverName)) { + throw Exception("Server with same name already exists!"); + } JoinPerm joinPerm = getJoinPerm(serverPerm); var creator = getUser(userName); var newServer = createNewServer(serverName, joinPerm); @@ -567,20 +570,18 @@ class ChatAPI { var reqServer = getServer(servername); var reqUser = getUser(username); reqServer.checkAccessLevels(username, [1, 2]); - var inviteCode = InviteCode(reqUser, "",reqServer); - for(var invitecode in reqServer.inviteCodes ){ - if(invitecode.code == inviteCode.code){ + var inviteCode = InviteCode(reqUser, "", reqServer); + for (var invitecode in reqServer.inviteCodes) { + if (invitecode.code == inviteCode.code) { return createInviteCode(servername, username); - } - else if (invitecode.inviter == reqUser){ + } else if (invitecode.inviter == reqUser) { return invitecode.code; } } reqServer.inviteCodes.add(inviteCode); - await DatabaseIO.addToDB(inviteCode,"invitecodes"); + await DatabaseIO.addToDB(inviteCode, "invitecodes"); inviteCodes.add(inviteCode); return inviteCode.code; - } // join server using invite code @@ -596,7 +597,7 @@ class ChatAPI { invite.invitedUsers.add(reqUser); } - void sendDm(String recieverusername, String message, String senderusername){ + void sendDm(String recieverusername, String message, String senderusername) { User sender = getUser(senderusername); User reciever = getUser(recieverusername); DirectMessage dm = DirectMessage(sender, reciever, message); @@ -604,24 +605,24 @@ class ChatAPI { dm.send(); } - Future> getRecievedDms(String username) async{ + Future> getRecievedDms(String username) async { User user = getUser(username); List dms = await DirectMessage.getMessages(user); List messages = []; - for(DirectMessage dm in dms){ - if(dm.receiver.username == user.username){ + for (DirectMessage dm in dms) { + if (dm.receiver.username == user.username) { messages.add("${dm.sender.username} : ${dm.message}"); } } return messages; } - Future> getSentDms(String username) async{ + Future> getSentDms(String username) async { User user = getUser(username); List dms = await DirectMessage.getMessages(user); List messages = []; - for(DirectMessage dm in dms){ - if(dm.sender.username == user.username){ + for (DirectMessage dm in dms) { + if (dm.sender.username == user.username) { messages.add("${dm.receiver.username} : ${dm.message}"); } }