generated from Hebilicious/serverless-esbuild-template
-
Notifications
You must be signed in to change notification settings - Fork 71
/
follow.ts
191 lines (170 loc) · 5.73 KB
/
follow.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
import { DynamoDB } from "aws-sdk"
import { Item } from "./base"
import { getClient } from "./client"
import { User } from "./user"
import { executeTransactWrite } from "./utils"
export class Follow extends Item {
followedUsername: string
followingUsername: string
constructor(followedUsername: string, followingUsername: string) {
super()
this.followedUsername = followedUsername
this.followingUsername = followingUsername
}
static fromItem(item?: DynamoDB.AttributeMap): Follow {
if (!item) throw new Error("No item!")
return new Follow(item.followedUsername.S, item.followingUsername.S)
}
get pk(): string {
return `FOLLOW#${this.followedUsername}`
}
get sk(): string {
return `FOLLOW#${this.followingUsername}`
}
get gsi1pk(): string {
return this.sk
}
get gsi1sk(): string {
return this.pk
}
toItem(): Record<string, unknown> {
return {
...this.keys(),
GSI1PK: { S: this.gsi1pk },
GSI1SK: { S: this.gsi1sk },
followedUsername: { S: this.followedUsername},
followingUsername: { S: this.followingUsername},
}
}
}
export const followUser = async (followedUsername: string, followingUsername: string) => {
const client = getClient()
const followedUser = new User(followedUsername);
const followingUser = new User(followingUsername);
const follow = new Follow(followedUsername, followingUsername)
try {
await executeTransactWrite({
client,
params: {
TransactItems: [
{
Put: {
TableName: process.env.TABLE_NAME,
Item: follow.toItem(),
ConditionExpression: "attribute_not_exists(PK)"
}
},
{
Update: {
TableName: process.env.TABLE_NAME,
Key: followedUser.keys(),
ConditionExpression: "attribute_exists(PK)",
UpdateExpression: "SET #followerCount = #followerCount+ :inc",
ExpressionAttributeNames: {
"#followerCount": "followerCount"
},
ExpressionAttributeValues: {
":inc": { N: "1" }
}
}
},
{
Update: {
TableName: process.env.TABLE_NAME,
Key: followingUser.keys(),
ConditionExpression: "attribute_exists(PK)",
UpdateExpression: "SET #followingCount = #followingCount+ :inc",
ExpressionAttributeNames: {
"#followingCount": "followingCount"
},
ExpressionAttributeValues: {
":inc": { N: "1" }
}
}
}
]
}
})
return follow
} catch (error) {
console.log(error)
throw error
}
}
export const listFollowersOfUser = async (username: string): Promise<User[]> => {
const client = getClient()
const follow = new Follow(username, "")
let follows: Follow[];
try {
const resp = await client
.query({
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "PK = :pk",
ExpressionAttributeValues: {
":pk": { S: follow.pk }
}
})
.promise()
follows = resp.Items.map((item) => Follow.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
const keys = follows.map((follow) => {
const user = new User(follow.followingUsername)
return user.keys()
})
try {
const resp = await client.batchGetItem({
RequestItems: {
[process.env.TABLE_NAME]: {
Keys: keys
}
}
}).promise()
return resp.Responses[process.env.TABLE_NAME].map((item) => User.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
}
export const listFollowedByUser = async (username: string): Promise<User[]> => {
const client = getClient()
const follow = new Follow("", username)
let follows: Follow[];
console.log(follow)
console.log(follow.gsi1pk)
try {
const resp = await client
.query({
TableName: process.env.TABLE_NAME,
IndexName: "GSI1",
KeyConditionExpression: "GSI1PK = :gsi1pk",
ExpressionAttributeValues: {
":gsi1pk": { S: follow.gsi1pk }
}
})
.promise()
follows = resp.Items.map((item) => Follow.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
const keys = follows.map((follow) => {
const user = new User(follow.followedUsername)
return user.keys()
})
try {
const resp = await client.batchGetItem({
RequestItems: {
[process.env.TABLE_NAME]: {
Keys: keys
}
}
}).promise()
return resp.Responses[process.env.TABLE_NAME].map((item) => User.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
}