-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriends.js
More file actions
68 lines (56 loc) · 1.6 KB
/
friends.js
File metadata and controls
68 lines (56 loc) · 1.6 KB
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
// friends.js
import { get_db_connection } from './server/models/rdbms.js';
const db = get_db_connection();
export async function getFriendsForUser(userId) {
// make sure we have a live connection
await db.connect();
// “friends” table has (follower, following)
// here we return the users that “userId” is following
const [rows] = await db.send_sql(
`SELECT
u.user_id AS userId,
u.first_name AS firstName,
u.last_name AS lastName
FROM friends f
JOIN users u ON u.user_id = f.following
WHERE f.follower = ?
`,
[userId]
);
return rows; // [ { userId, firstName, lastName }, … ]
}
export async function getMutualsForUser(userId) {
const db = get_db_connection();
// 1) Who follows you?
const [followers] = await db.send_sql(
`SELECT
f.follower AS userId,
u.first_name AS firstName,
u.last_name AS lastName,
u.username
FROM friends f
INNER JOIN users u
ON f.follower = u.user_id
WHERE f.following = ?`,
[userId]
);
// 2) Who you follow?
const [following] = await db.send_sql(
`SELECT
f.following AS userId,
u.first_name AS firstName,
u.last_name AS lastName,
u.username
FROM friends f
INNER JOIN users u
ON f.following = u.user_id
WHERE f.follower = ?`,
[userId]
);
// 3) Deduplicate by userId
const map = new Map();
for (const u of followers) map.set(u.userId, u);
for (const u of following) map.set(u.userId, u);
// 4) Return an array of user objects
return Array.from(map.values());
}