Skip to content

Commit

Permalink
sets up initial repo
Browse files Browse the repository at this point in the history
  • Loading branch information
abi-sheks committed Dec 11, 2024
0 parents commit 4ba0ef7
Show file tree
Hide file tree
Showing 22 changed files with 1,669 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO_URI="mongodb://localhost:27017/<db_name>"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
.env
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
156 changes: 156 additions & 0 deletions bin/prompt_chat.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'package:prompt_chat/prompt_chat.dart';
import 'dart:io';

void main(List<String> arguments) {
ChatAPI api = ChatAPI();
Future.wait([api.populateArrays()]).then((value) => {runApp(api)});
//rest of the application cannot start until above function completes.
}

void runApp(ChatAPI api) async {
String? currUsername;
String? currentCommand;
currUsername = api.getCurrentLoggedIn();
print(
"Welcome to prompt_chat! Read the documentation to get started on using the interface. Type \"exit\" to close the application.");
loop:
while (true) {
try {
currentCommand = stdin.readLineSync();
if (currentCommand == null) {
throw Exception("Please enter a command");
}
var ccs = currentCommand.split(" ");
switch (ccs[0]) {
case "register":
{
await api.registerUser(ccs[1], ccs[2]);
print("Registration successful!");
break;
}
case "login":
{
await api.loginUser(ccs[1], ccs[2]);
currUsername = ccs[1];
print("Login successful!");
break;
}
case "logout":
{
await api.logoutUser(currUsername);
currUsername = null;
print("Successfully logged out, see you again!");
break;
}
case 'current-session' :
{
print("$currUsername is logged in");
}
case "create-server":
{
await api.createServer(ccs[1], currUsername, ccs[2]);
print("Created server succesfully");
break;
}
case "add-member":
{
await api.addMemberToServer(ccs[1], ccs[2], currUsername);
print("Added member successfully");
break;
}
case "add-category":
{
await api.addCategoryToServer(ccs[1], ccs[2], currUsername);
print("Added category successfully");
break;
}
case "add-channel":
{
await api.addChannelToServer(
ccs[1], ccs[2], ccs[3], ccs[4], ccs[5], currUsername);
print("Added channel successfully");
break;
}
case "send-msg":
{
print("Enter the text message to be sent");
var message = stdin.readLineSync();
await api.sendMessageInServer(
ccs[1], currUsername, ccs[2], message);
print('Message sent succesfully');
break;
}
case "display-users":
{
api.displayUsers();
break;
}
case "display-channels":
{
api.displayChannels();
break;
}
case "create-role":
{
await api.createRole(ccs[1], ccs[2], ccs[3], currUsername);
print("Role created successfully");
break;
}
case "assign-role":
{
await api.addRoleToUser(ccs[1], ccs[2], ccs[3], currUsername);
print("Role assigned successfully");
break;
}
case "channel-to-cat":
{
await api.addChannelToCategory(
ccs[1], ccs[2], ccs[3], currUsername);
print("Channel added to category");
break;
}
case "change-perm":
{
await api.changePermission(ccs[1], ccs[2], ccs[3], currUsername);
print("Permission changed successfully.");
break;
}
case "change-ownership":
{
await api.changeOwnership(ccs[1], currUsername, ccs[2]);
break;
}
case "leave-server":
{
print("Are you sure you want to proceed? (y/n)");
var confirm = stdin.readLineSync();
if (confirm == null) {
break;
}
confirm = confirm.toLowerCase();
if (confirm == "y" || confirm == "yes") {
await api.leaveServer(ccs[1], currUsername);
print("Member deleted");
}
break;
}
case 'join-server':
{
await api.joinServer(ccs[1], currUsername);
print("Server joined successfully.");
}
case "exit":
{
print("See you soon!");
break loop;
}
default:
{
print("Please enter a valid command.");
}
}
} on Exception catch (e) {
print("$e");
}
}
}
26 changes: 26 additions & 0 deletions lib/cli/category.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:prompt_chat/cli/channel.dart';

class Category {
List<Channel> channels = [];
late String categoryName;
Category({required this.categoryName, required this.channels});
Map<String, dynamic> toMap() {
var mappedChannels = channels.map((e) => e.toMap()).toList();
return {
'categoryName': categoryName,
'channels': mappedChannels,
};
}

static Category fromMap(Map<String, dynamic> map) {
late List<Channel> unmappedChannels;
if(map['channels'] == null) {
unmappedChannels = [];
}
unmappedChannels = (map['channels'] as List)
.map((channel) => Channel.fromMap(channel))
.toList();
return Category(
categoryName: map['categoryName'], channels: unmappedChannels);
}
}
48 changes: 48 additions & 0 deletions lib/cli/channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:prompt_chat/cli/message.dart';
import 'package:prompt_chat/enum/permissions.dart';
import 'package:prompt_chat/enum/channel_type.dart';


class Channel {
late String channelName;
late Permission permission;
late ChannelType type;
List<Message> messages = [];
Channel({required this.channelName, required this.messages, required this.type, this.permission = Permission.member});
Map<String, dynamic> toMap() {
var mappedMessages = messages.map((message) => message.toMap()).toList();
return {
'channelName' : channelName,
'permission' : permission.toString(),
'channelType' : type.toString(),
'messages' : mappedMessages,
'finder' : "finder",
};
}
static Channel fromMap(Map<String, dynamic> map) {
late Permission perm;
late ChannelType chanType;
if(map['permission'] == "Permission.owner") {
perm = Permission.owner;
}
if(map['permission'] == "Permission.moderator") {
perm = Permission.moderator;
}
if(map['permission'] == "Permission.member") {
perm = Permission.member;
}
if(map['channelType'] == "ChannelType.text") {
chanType = ChannelType.text;
}
if(map['channelType'] == "ChannelType.voice") {
chanType = ChannelType.voice;
}
if(map['channelType'] == "ChannelType.video") {
chanType = ChannelType.video;
}
var unmappedMessages = (map['messages'] as List).map((message) => Message.fromMap(message)).toList();

return Channel(channelName: map['channelName'], messages: unmappedMessages, type: chanType, permission: perm);
}

}
3 changes: 3 additions & 0 deletions lib/cli/exceptions/invalid_creds.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class InvalidCredentialsException {
final String message = "Invalid credentials";
}
18 changes: 18 additions & 0 deletions lib/cli/message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:prompt_chat/cli/user.dart';

class Message {
final String content;
final User sender;
Message(this.content, this.sender);

Map<String, dynamic> toMap() {
return {
'content' : content,
'sender' : sender.toMap(),
'finder' : "finder",
};
}
static Message fromMap(Map<String, dynamic> map) {
return Message(map['content'], User.fromMap(map['sender']));
}
}
35 changes: 35 additions & 0 deletions lib/cli/role.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:prompt_chat/cli/user.dart';
import 'package:prompt_chat/enum/permissions.dart';

class Role {
List<User> holders = [];
final String roleName;
final Permission accessLevel;
Role({required this.roleName, required this.accessLevel, required this.holders});

Map<String, dynamic> toMap() {
var mappedHolders = holders.map((holder) => holder.toMap()).toList();
return {
'roleName' : roleName,
'holders' : mappedHolders,
'accessLevel' : accessLevel.toString(),
"finder" : "finder",
};
}
static Role fromMap(Map<String, dynamic> map) {
late Permission perm;
late List<User> unmappedHolders;
if(map['accessLevel'] == "Permission.owner") {
perm = Permission.owner;
}
else if(map['accessLevel'] == "Permission.moderator") {
perm = Permission.moderator;
}
else perm = Permission.member;
if(map['holders'] == null) {
unmappedHolders = [];
}
unmappedHolders = (map['holders'] as List).map((holder) => User.fromMap(holder)).toList();
return Role(roleName: map['roleName'], accessLevel: perm, holders: unmappedHolders);
}
}
Loading

0 comments on commit 4ba0ef7

Please sign in to comment.