-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
executable file
·315 lines (280 loc) · 9.88 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { GraphQLServer, PubSub } from "graphql-yoga";
import { MongoClient, ObjectId } from "mongodb";
import { createWriteStream } from 'fs'
const express = require("express");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const storeUpload = async ({ stream, filename }): Promise<any> => {
const path = `uploads/${filename}`
return new Promise((resolve, reject) =>
stream
.pipe(createWriteStream(path))
.on('finish', () => resolve(path))
.on('error', reject),
)
}
const processUpload = async upload => {
if (upload == null) return null;
const { createReadStream, filename, mimetype, encoding } = await upload
const stream = createReadStream()
return storeUpload({ stream, filename });
}
const processFiles = async files => {
return {
thumbnail: await processUpload(files.thumbnail),
file: await processUpload(files.file)
}
}
const main = async () => {
const MONGO_URL = "mongodb://localhost:27017";
const client = await MongoClient.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = client.db("chaty");
const Users = db.collection("users");
const Conversations = db.collection("conversations");
const Messages = db.collection("messages");
function getUserId(ctx, object = true) {
const Authorization = ctx.request.get("Authorization");
if (Authorization) {
const token = Authorization.replace("Bearer ", "");
const { userId } = jwt.verify(token, ctx.secret);
return (object ? ObjectId(userId) : userId) || Error("Please Login first!");
}
throw Error("Please Login first!");
}
var count = 0;
const resolvers = {
Subscription: {
data: {
subscribe: (parent, { id }, { pubsub }) => {
return pubsub.asyncIterator(id);
}
},
},
Query: {
conversations(parent, args, context) {
const _id = getUserId(context);
return context.Conversations.find({ users: _id }).toArray();
},
users(parent, args, { Users }) {
return Users.find({}).toArray();
},
async messages(parent, { id }, { Conversations, Messages }) {
const conversation = await Conversations.findOne({ _id: ObjectId(id) });
if (conversation == null) return { error: 'Conversation Not Found!' }
return Messages.find({ _id: { $in: conversation.messages } }).toArray();
},
async profile(parent, args, context) {
try {
const user = await context.Users.findOne({ _id: getUserId(context) });
return user;
} catch ({ message }) {
return { error: message };
}
},
async search(parent, { query }, context) {
try {
getUserId(context);
if (!query) return [];
let regex = { $regex: query, $options: 'i' };
return await context.Users.find({ name: regex }).toArray();
} catch ({ message }) {
return { error: message };
}
},
},
Mutation: {
async signup(
parent,
{ phoneNumber, password, name },
{ Users, secret }
) {
const hash = await bcrypt.hash(password, 10);
const data = {
phoneNumber,
password: hash,
name,
createdAt: Date.now(),
modifiedAt: Date.now()
};
const { ops } = await Users.insertOne(data);
const [user] = ops;
return {
userId: user._id,
token: jwt.sign({ userId: user._id }, secret)
};
},
async login(parent, { phoneNumber, password }, { Users, secret }) {
const user = await Users.findOne({ phoneNumber });
if (!user) {
return { error: `Can't find User with Phone Number: ${phoneNumber}` };
}
const valid = await bcrypt.compare(password, user.password);
if (!valid) {
return { error: "Wrong password please try again!" };
}
return {
userId: user._id,
token: jwt.sign({ userId: user._id }, secret)
};
},
async saveProfile(
parent,
data,
context
) {
if (data.profilePhoto) {
data.profilePhoto = await processUpload(data.profilePhoto)
}
const { ok, value } = await context.Users.findOneAndUpdate({ _id: getUserId(context) }, {
$set: data
}, { returnOriginal: false });
return value;
},
async addConversation(parent, { users }, context) {
try {
const userId = getUserId(context);
const { ops } = await context.Conversations.insertOne({
users: [userId, ...users.map((id: string) => ObjectId(id))],
messages: [],
createdAt: Date.now(),
modifiedAt: Date.now()
});
let [conversation] = ops;
users.forEach(userId => context.pubsub.publish(userId, { data: { conversation, update: false } }));
return conversation;
} catch ({ message }) {
return { error: message };
}
},
async sendMessage(parent, { conversation, content, files }, context) {
try {
const sender = getUserId(context);
if (files) {
files = await Promise.all(files.map(async (file) => await processFiles(file)))
}
const { ops } = await context.Messages.insertOne({
content,
sender: sender,
createdAt: Date.now(),
modifiedAt: Date.now(),
files
});
let [message] = ops
await context.Conversations.updateOne(
{ _id: ObjectId(conversation) },
{ $push: { messages: message._id }, $set: { modifiedAt: Date.now() } }
);
context.pubsub.publish(conversation, { data: { message, update: false } });
return message;
} catch ({ message }) {
return { error: message };
}
},
async deleteMessage(parent, { id }, context) {
try {
const sender = getUserId(context);
const result = await context.Conversations.findOne({ messages: ObjectId(id), users: sender });
if (result) {
const { deletedCount } = await context.Messages.deleteOne({ _id: ObjectId(id) });
return deletedCount > 0 ? { success: 'Message Deleted' } : { error: 'Failed to Delete Message' };
}
return { error: "Failed to Delete Message" };
} catch ({ message }) {
return { error: message };
}
},
async deleteConversation(parent, { id }, context) {
try {
const sender = getUserId(context);
const { ok, value } = await context.Conversations.findOneAndUpdate({ _id: ObjectId(id) }, {
$pull: { users: sender }
}, { returnOriginal: false });
if (value.users.length === 0) {
await context.Messages.delete({ _id: { $in: value.messages } });
await context.Conversations.deleteOne({ _id: value._id });
} else {
value.users.forEach(userId => context.pubsub.publish(userId.toString(), { data: { conversation: value, update: true } }));
}
// await context.Conversations.deleteOne({ _id: ObjectId(id), users: [] });
return ok ? { success: 'Conversation Deleted' } : { error: 'Failed to Delete Conversation' };
} catch ({ message }) {
return { error: message };
}
},
async singleUpload(obj, { file }) {
const { createReadStream, filename, mimetype, encoding } = await file;
},
},
User: {
conversations(parent, args, { Conversations }) {
return Conversations.find({ users: parent._id }).toArray();
}
},
Conversation: {
users(parent, args, { Users }) {
return Users.find({ _id: { $in: parent.users } }).toArray();
},
messages(parent, args, { Messages }) {
return Messages.find({ _id: { $in: parent.messages } }).toArray();
},
lastMessage(parent, args, { Messages }) {
if (parent.messages.length > 0) {
let id = parent.messages.pop();
return Messages.findOne({ _id: id });
}
return null;
}
},
Message: {
sender(parent, args, { Users }) {
return Users.findOne({ _id: parent.sender });
},
isLoggedUser(parent, args, ctx) {
let _u = getUserId(ctx);
return parent.sender == _u;
}
}
};
// const processUpload = async upload => {
// const { createReadStream, filename, mimetype, encoding } = await upload
// const stream = createReadStream()
// const { id, path } = await storeUpload({ stream, filename })
// return recordFile({ id, filename, mimetype, encoding, path })
// }
// const storeUpload = async ({ stream, filename }): Promise<any> => {
// const id = shortid.generate()
// const path = `${uploadDir}/${id}-${filename}`
// return new Promise((resolve, reject) =>
// stream
// .pipe(createWriteStream(path))
// .on('finish', () => resolve({ id, path }))
// .on('error', reject),
// )
// }
const directiveResolvers = {
isEmail: (next, user, data, ctx, info) => {
if (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(user.email)) return next();
throw new Error(`Please Enter Valid Email ${info.email} field`);
}
};
const pubsub = new PubSub();
const server = new GraphQLServer({
typeDefs: "./schema.graphql",
resolvers,
context: ({ request }) => ({
request,
Users,
Conversations,
Messages,
pubsub,
secret: process.env.npm_package_JWT_SECRET || "ww(gst7)nmJ6W}S"
}),
directiveResolvers
});
server.express.use("/uploads", express.static("uploads"));
server.start(() => console.log("Server is running on localhost:4000"));
};
main();