Skip to content

Commit

Permalink
groups added
Browse files Browse the repository at this point in the history
  • Loading branch information
ChauhanAbhinav committed Aug 28, 2019
1 parent 53f196a commit a2c3c24
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 10 deletions.
85 changes: 85 additions & 0 deletions server/db/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,88 @@ let deleteContact = (mobile, contact)=>{
});
});
};

let createGroup = (mobile, contacts, group)=>{
// console.log(contacts);
document = [];
FLAG_ERROR = false;
contacts.forEach(cont => {
document.push({'mobile': cont, 'group': group});
});
// console.log(documents);

return new Promise((resolve, reject)=>{

coll = db.collection('groups');
coll.insertOne({'mobile': mobile, 'group': group}, function(err, data) {
if (err) {
FLAG_ERROR = true;
} else {
coll.insertMany(document, function(err, data) {
if(err) FLAG_ERROR = true;
else {
groupInfo = db.collection('groupInfo');
groupInfo.insertOne({'group': group, 'members': contacts}, function(err, data) {
if(err) FLAG_ERROR = true;

if(FLAG_ERROR) reject('Group is not created succesfully')
else
resolve('Group created succesfully');
});
}
});
}
});
});
}
let getAllGroups = (mobile)=>{

return new Promise((resolve, reject)=>{
// console.log(data);
db.collection('groups').find({'mobile' : mobile},{_id:0, mobile:0}).toArray(function(err, data) {
if (err)
reject(err);
else {
// console.log(data);
if(data.length == 0) {
reject('No group found');

}
else
{
// console.log('contact found: ', user)
resolve(data);
}
}
});

});
}

let deleteGroup = (mobile, group)=>{

return new Promise((resolve, reject)=>{
// console.log(data);
db.collection('groups').deleteOne({ $and: [{'mobile' : mobile}, {'group': group}]}, function(err, data) {
if (err)
reject(err);
else{
// send modified contact list
db.collection('groupInfo').update({'group': group},{}, function(err, data) {

});

getAllGroups(mobile)
.then(function (data) {
resolve(data)
}, function(err) {
reject(err);
});
}
});
});
}

// export service
service.ifRegistered = ifRegistered;
service.createUser = createUser;
Expand All @@ -180,4 +262,7 @@ service.addContact = addContact;
service.getAllContacts = getAllContacts;
service.getContact = getContact;
service.deleteContact = deleteContact;
service.createGroup = createGroup;
service.getAllGroups = getAllGroups;
service.deleteGroup = deleteGroup;
module.exports = service;
32 changes: 32 additions & 0 deletions server/routes/chatRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,36 @@ router.post('/deletecontact',(req, res)=>{

});

router.post('/creategroup',(req, res)=>{
// console.log("req",req.body);
userService.createGroup(req.body.mobile, req.body.selectedContacts, req.body.group)
.then(function (data) {
res.status(200).json(data);
}, function(err) {
// console.log(err);
res.status(400).json(err);
});
});

router.post('/grouplist',(req, res)=>{
// console.log("req",req.body);
userService.getAllGroups(req.body.mobile)
.then(function (data) {
res.status(200).json(data);
}, function(err) {
// console.log(err);
res.status(400).json(err);
});
});
router.post('/deletegroup',(req, res)=>{
// console.log(req.body);
userService.deleteGroup(req.body.mobile, req.body.group)
.then(function (data) {
res.status(200).json(data);
}, function(err) {
// console.log(err);
res.status(400).json(err);
})
});

module.exports = router;
36 changes: 36 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,39 @@ socket.on('disconnect', function(){
});

});



// socket.io -groups=====================================

io_group = io.of('/group'); // namespace public
io_group.on('connection', function(socket){

socket.on('adduser', function(username, group){

socket.username = username;
socket.room = group; //assign default public room
socket.join(socket.room);

// echo to client they've connected
socket.emit('server', 'you have connected to a private group: '+ socket.room);

// echo to public room that a person has connected to their room
socket.broadcast.to(socket.room).emit('server', '<i>' + socket.username + '</i> has connected to this room');


});

socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
// console.log('server recieve chat');
io_group.to(socket.room).emit('updatechat', socket.username, data);
});

socket.on('disconnect', function(){
socket.broadcast.to(socket.room).emit('server','<i>' + socket.username + '</i> has left the room');
socket.emit('end');
socket.leave(socket.room);
});

});
4 changes: 4 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { PublicComponent } from './public/public.component';
import { PrivateComponent } from './private/private.component';
import { ContactsComponent } from './contacts/contacts.component';
import { UsersComponent } from './users/users.component';
import { GroupComponent } from './group/group.component';
import { AuthGuardService } from './services/auth-guard.service';
import { GroupListComponent } from './group-list/group-list.component';

const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
Expand All @@ -16,8 +18,10 @@ const routes: Routes = [
{ path: '', redirectTo: 'contacts', pathMatch: 'full' },
{ path: 'public', component: PublicComponent, canDeactivate: [AuthGuardService]},
{ path: 'private/:contact/:room', component: PrivateComponent, canDeactivate: [AuthGuardService] },
{ path: 'group/:contact/:group', component: GroupComponent, canDeactivate: [AuthGuardService] },
{ path: 'contacts', component: ContactsComponent },
{ path: 'userlist', component: UsersComponent },
{ path: 'grouplist', component: GroupListComponent },
]
}
];
Expand Down
4 changes: 4 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { PublicComponent } from './public/public.component';
import { PrivateComponent } from './private/private.component';
import { ContactsComponent } from './contacts/contacts.component';
import { UsersComponent } from './users/users.component';
import { GroupComponent } from './group/group.component';
import { GroupListComponent } from './group-list/group-list.component';

@NgModule({
declarations: [
Expand All @@ -26,6 +28,8 @@ import { UsersComponent } from './users/users.component';
PrivateComponent,
ContactsComponent,
UsersComponent,
GroupComponent,
GroupListComponent,
],
imports: [
BrowserModule,
Expand Down
4 changes: 2 additions & 2 deletions src/app/contacts/contacts.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form [formGroup]="form" (ngSubmit)="submit()">
<form [formGroup]="form" (ngSubmit)="createGroup()">

<mat-toolbar>Your Contacts
<span class="spacer"></span>
Expand All @@ -10,7 +10,7 @@

<mat-checkbox [formControlName]='i' style="padding-right: 40px;" color="primary" >{{contacts[i].contact}}</mat-checkbox>

<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer;" (click)="startChat(contacts[i].contact,contacts[i].contact)">chat</mat-icon>
<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer;" (click)="startChat(contacts[i].contact,contacts[i].room)">chat</mat-icon>
<span class="spacer"></span>
<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer;" (click)="deleteContact(contacts[i].contact, i)">delete</mat-icon>

Expand Down
27 changes: 20 additions & 7 deletions src/app/contacts/contacts.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,30 @@ private addCheckboxes() {
this.contacts.map((o, i) => { // data, index
const control = new FormControl(false); // initialized as unchecked
(this.form.controls.contacts as FormArray).push(control);
// console.log(o);
});
}


private submit() {

const selectedContactsIds = this.form.value.contacts
private createGroup() {
let group = prompt('Choose a group name');
group = String(group);
const selectedContacts = this.form.value.contacts
.map((v, i) => v ? this.contacts[i].contact : null)
.filter(v => v !== null);
console.log(selectedContactsIds);
console.log(group);
this.chatService.createGroup(this.user, selectedContacts, group).subscribe(
res => {
// console.log('response', res);
if (res.status === 200) {
alert(res.body);
this.router.navigateByUrl('dashboard/group/' + this.user + '/' + group);
}
},
err => {
if (err.error) {
// console.log('Error:', err.error);
alert('Error: ' + err.error);
}
});

}


Expand Down
4 changes: 4 additions & 0 deletions src/app/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<mat-icon aria-hidden="false" aria-label="icon" style="padding-right: 10px" >contacts</mat-icon> Contacts </a>
<mat-divider></mat-divider>

<a mat-list-item [routerLink]="'grouplist'">
<mat-icon aria-hidden="false" aria-label="icon" style="padding-right: 10px" >group</mat-icon> Groups </a>
<mat-divider></mat-divider>

<a mat-list-item [routerLink]="'userlist'">
<mat-icon aria-hidden="false" aria-label="icon" style="padding-right: 10px">group</mat-icon>All Users </a>

Expand Down
Empty file.
16 changes: 16 additions & 0 deletions src/app/group-list/group-list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<mat-toolbar>Your Groups
</mat-toolbar>

<mat-list *ngIf="groups">
<mat-list-item *ngFor="let group of groups" style="width:100%">
<span style="display: inline-block;width:100px;">{{group.group}}</span>

<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer;" (click)="startChat(group.mobile,group.group)">chat</mat-icon>

<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer; margin-left:20px;" >info</mat-icon>
<span class="spacer"></span>
<mat-icon aria-hidden="false" aria-label="icon" style="cursor: pointer;" (click)="deleteGroup(group.mobile,group.group)">delete</mat-icon>
</mat-list-item>
</mat-list>
<mat-toolbar *ngIf="!groups">It is lonely here.</mat-toolbar>

25 changes: 25 additions & 0 deletions src/app/group-list/group-list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { GroupListComponent } from './group-list.component';

describe('GroupListComponent', () => {
let component: GroupListComponent;
let fixture: ComponentFixture<GroupListComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ GroupListComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(GroupListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
57 changes: 57 additions & 0 deletions src/app/group-list/group-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Component, OnInit } from '@angular/core';
import { ChatService } from './../services/chat.service';
import { LoginService } from '../services/login.service';
import {Router} from '@angular/router';

@Component({
selector: 'app-group-list',
templateUrl: './group-list.component.html',
styleUrls: ['./group-list.component.css']
})
export class GroupListComponent implements OnInit {
private groups;
private user;
constructor(private chatService: ChatService, private loginService: LoginService, private router: Router) {
this.user = loginService.getLoggedUser();
this.chatService.getAllGroups(this.user).subscribe(
res => {
if (res.status === 200) {
this.groups = res.body;
// console.log(this.groups);
}
},
err => {
console.log('Error:', err.error);
});
}
private startChat(user, group) {
if ((typeof user !== 'undefined') && (typeof group !== 'undefined')) {
this.router.navigateByUrl('dashboard/group/' + user + '/' + group);
} else {
alert('Not able to locate the room');
}
}

// delete group function
private deleteGroup(user, group) {
this.chatService.deleteGroup(user, group).subscribe(
res => {
if (res.status === 200) {
this.groups = res.body;
}
},
err => {
if (err.status === 400) {
// no user left
delete this.groups;
} else {
console.log('Error:', err.status);
alert('Error: ' + err.error);
}
});
}

ngOnInit() {
}

}
10 changes: 10 additions & 0 deletions src/app/group/group.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
div { background: #000000; padding: 1px; position: relative;right: 0; bottom: 0;width: 100%;border: 1px solid black }
div input { border: 0; padding: 10px; width: 100%; margin-right: 1px; outline:none;border: 1px solid black;background: #ffffff;
font-size: 15px;}
div button { width: calc(10% - 1px); border: none;border-radius: 0}
#conversation { list-style-type: none; margin: 0; padding: 0; }
#conversation li { padding: 5px 10px; }
.server { background: #444444; }

Loading

0 comments on commit a2c3c24

Please sign in to comment.