Skip to content

Commit 6e1644f

Browse files
committed
stopping user from creating dms with themselves and duplicates dms
1 parent 8383af1 commit 6e1644f

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

chathaven/backend/src/controllers/conversationsController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export const createDirectMessages = async (req: Request, res: Response) => {
6464
// See if there are any common conversationIds in their directMessages arrays
6565
const existingConversation = await Conversation.findOne({
6666
conversationId: {
67-
$in: creator.directMessages.concat(addedUser.directMessages),
67+
$in: creator.directMessages.filter((id) =>
68+
addedUser.directMessages.includes(id)
69+
),
6870
},
6971
});
7072

chathaven/frontend/src/app/components/chat/component/channelSideBar/channelSideBar.component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export class ChannelSidebarComponent implements OnInit, OnDestroy {
8282
? this.refreshDirectMessageList()
8383
: this.refreshChannelList();
8484
});
85+
86+
// ADD THIS
87+
this.dataService.refreshDirectMessages$.subscribe(() => {
88+
this.refreshDirectMessageList();
89+
});
90+
8591
this.dataService.currentTeamId.subscribe((teamId) => {
8692
this.selectedTeamId = teamId;
8793
});
@@ -575,4 +581,4 @@ export class ChannelSidebarComponent implements OnInit, OnDestroy {
575581
return this.selectedChannelId ? conversationId : '';
576582
}
577583
}
578-
}
584+
}

chathaven/frontend/src/app/components/chat/component/informationSideBar/informationSideBar.component.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ export class InformationSidebarComponent implements OnInit, OnDestroy {
105105
// Lifecycle Hooks
106106
async ngOnInit() {
107107
this.loginUser = this.userService.getUser();
108-
109-
if (!this.loginUser) {
110-
console.error('User not found');
111-
}
108+
if (!this.loginUser) {
109+
this.loginUser = await this.userService.user$.toPromise();
110+
}
111+
if (!this.loginUser) {
112+
console.error('User not found');
113+
}
112114
}
113115

114116
ngOnDestroy() {
@@ -344,20 +346,41 @@ export class InformationSidebarComponent implements OnInit, OnDestroy {
344346
async createCoversation(memberId: string) {
345347
const sender = this.loginUser;
346348
const receiver = await this.backendService.getUserById(memberId);
349+
347350
if (sender && receiver?.userId) {
348351
const conversationName = `${sender.username}, ${receiver.username}`;
349-
const conversationId = await this.backendService.createDirectMessages(
350-
conversationName,
351-
sender.userId,
352-
receiver.userId
353-
);
354-
this.dataService.selectTeam('');
355-
this.dataService.selectChannel('');
356-
if (conversationId) {
357-
this.dataService.selectConversation(conversationId.conversationId);
352+
353+
try {
354+
const conversation = await this.backendService.createDirectMessages(
355+
conversationName,
356+
sender.userId,
357+
receiver.userId
358+
);
359+
360+
if (conversation?.conversationId) {
361+
this.dataService.selectTeam('');
362+
this.dataService.selectChannel('');
363+
this.dataService.selectConversation(conversation.conversationId);
364+
365+
// Trigger refresh of Direct Message list
366+
this.dataService.triggerDirectMessagesRefresh();
367+
368+
this.dataService.toggleIsDirectMessage(true);
369+
370+
await this.refreshList(); // Optional: refresh inbox/requests after DM created
371+
372+
alert('Direct Message successfully created');
373+
} else {
374+
alert('Failed to create conversation');
375+
}
376+
} catch (error) {
377+
console.error('Error creating conversation:', error);
378+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
379+
alert(`Failed to create conversation: ${errorMessage}`);
358380
}
359-
this.dataService.toggleIsDirectMessage(true);
360-
alert('Direct Messages successfully created');
381+
} else {
382+
console.error('Sender or Receiver not found');
383+
alert('Failed to create conversation: Sender or Receiver not found');
361384
}
362385
}
363386

@@ -643,4 +666,4 @@ export class InformationSidebarComponent implements OnInit, OnDestroy {
643666
this.isInputFocused = false;
644667
this.onSearchQueryChange();
645668
}
646-
}
669+
}

chathaven/frontend/src/services/data.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject } from 'rxjs';
2+
import { BehaviorSubject, Subject } from 'rxjs';
33

44
@Injectable()
55
export class DataService {
@@ -24,6 +24,10 @@ export class DataService {
2424
private messageIdSource = new BehaviorSubject<string>('');
2525
selectedMessageId = this.messageIdSource.asObservable();
2626

27+
// ADD THIS FOR DM REFRESH
28+
private refreshDirectMessagesSource = new Subject<void>();
29+
refreshDirectMessages$ = this.refreshDirectMessagesSource.asObservable();
30+
2731
selectTeam(selectedTeamId: string) {
2832
this.teamId.next(selectedTeamId);
2933
}
@@ -59,4 +63,9 @@ export class DataService {
5963
selectMessage(messageId: string) {
6064
this.messageIdSource.next(messageId);
6165
}
62-
}
66+
67+
// ADD THIS FUNCTION TO TRIGGER REFRESH
68+
triggerDirectMessagesRefresh() {
69+
this.refreshDirectMessagesSource.next();
70+
}
71+
}

0 commit comments

Comments
 (0)