Skip to content

Commit f0f17f5

Browse files
author
Dara Hayes
committed
feat: group chat names and pictures
1 parent 1fef3a6 commit f0f17f5

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

server/modules/chats/index.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,27 @@ const resolvers: Resolvers = {
9494
const currentUser = await injector.get(Auth).currentUser();
9595

9696
if (!currentUser) return null;
97-
const participant = await injector.get(Chats).firstRecipient({
98-
chatId: chat.id,
99-
userId: currentUser.id
100-
});
97+
const participants = await injector.get(Chats).participants(chat.id)
98+
99+
const participantsWithoutCurrentUser = participants.filter((participant) => {
100+
return participant.id !== currentUser.id
101+
})
101102

102-
return participant ? participant.name : null;
103+
104+
if (participantsWithoutCurrentUser.length === 1) {
105+
const otherParticipant = participantsWithoutCurrentUser[0]
106+
return otherParticipant.name || otherParticipant.username || ""
107+
}
108+
109+
const names = participantsWithoutCurrentUser.reduce((prev, participant) => {
110+
const name = participant.name || participant.username || ""
111+
if (prev === "") {
112+
return name
113+
}
114+
return `${prev}, ${name}`
115+
}, "")
116+
117+
return names
103118
},
104119

105120
// pulling details of participant of chat that is not the current user but belongs to current user chat room
@@ -108,14 +123,19 @@ const resolvers: Resolvers = {
108123

109124
if (!currentUser) return null;
110125

111-
const participant = await injector.get(Chats).firstRecipient({
112-
chatId: chat.id,
113-
userId: currentUser.id
114-
});
126+
const participants = await injector.get(Chats).participants(chat.id)
127+
128+
const participantsWithoutCurrentUser = participants.filter((participant) => {
129+
return participant.id !== currentUser.id
130+
})
115131

116-
return participant && participant.picture
132+
if (participantsWithoutCurrentUser.length === 1) {
133+
const participant = participantsWithoutCurrentUser[0]
134+
return participant && participant.picture
117135
? participant.picture
118136
: injector.get(UnsplashApi).getRandomPhoto();
137+
}
138+
return injector.get(UnsplashApi).getRandomPhoto();
119139
},
120140

121141
// pulling all messages for X chat room

server/modules/chats/unsplash.api.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,29 @@ export class UnsplashApi {
1616
baseURL = "https://api.unsplash.com/";
1717

1818
async getRandomPhoto() {
19-
const trackedRandomPhoto = await trackProvider(
20-
async ({ query, orientation }: RandomPhotoInput) => {
21-
const response = await axios.get<RandomPhoto>("photos/random", {
22-
baseURL: this.baseURL,
23-
headers: {
24-
Authorization:
25-
"Client-ID 4d048cfb4383b407eff92e4a2a5ec36c0a866be85e64caafa588c110efad350d"
26-
},
27-
params: {
28-
query,
29-
orientation
30-
}
31-
});
19+
20+
const getPhoto = async ({ query, orientation }: RandomPhotoInput) => {
21+
const response = await axios.get<RandomPhoto>("photos/random", {
22+
baseURL: this.baseURL,
23+
headers: {
24+
Authorization:
25+
"Client-ID 4d048cfb4383b407eff92e4a2a5ec36c0a866be85e64caafa588c110efad350d"
26+
},
27+
params: {
28+
query,
29+
orientation
30+
}
31+
});
3232

33-
return response.data;
34-
},
35-
{
36-
provider: "Unsplash",
37-
method: "RandomPhoto",
38-
location: resolve(__dirname, "../logs/main")
39-
}
40-
);
33+
return response.data;
34+
}
4135

4236
try {
43-
return (await trackedRandomPhoto({
37+
const randomPhoto = await getPhoto({
4438
query: "portrait",
4539
orientation: "squarish"
46-
})).urls.small;
40+
})
41+
return randomPhoto.urls.small;
4742
} catch (err) {
4843
console.error("Cannot retrieve random photo:", err);
4944
return null;

0 commit comments

Comments
 (0)