-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.ts
More file actions
170 lines (154 loc) · 4.44 KB
/
Copy pathstack.ts
File metadata and controls
170 lines (154 loc) · 4.44 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
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
import { internalMutation, internalQuery } from '~/convex/_generated/server'
import { authAction, authMutation, authQuery, getUserId } from '~/convex/utils'
import type { Stack } from '~/convex/types'
import type { Doc, Id } from '~/convex/_generated/dataModel'
import { ConvexError, v } from 'convex/values'
import { getManyFrom } from 'convex-helpers/server/relationships'
import { internal } from '~/convex/_generated/api'
export const internalGetStack = internalQuery({
args: {
stackId: v.id('stacks')
},
handler: async ({ db }, { stackId }) => {
return await db.get(stackId)
}
})
export const getMyUserStacks = authQuery({
handler: async ({ db, user }) => {
if (!user) return []
return await getManyFrom(db, 'stacks', 'by_userId', user._id)
}
})
export const getOtherUserStacks = authQuery({
args: {
userId: v.id('users')
},
handler: async ({ db }, { userId }) => {
return await getManyFrom(db, 'stacks', 'by_userId', userId)
}
})
export const getPublicStack = authQuery({
handler: async ({ db }, { stackId }: { stackId: Id<'stacks'> }) => {
const stack = await db.get(stackId)
if (!stack?.isPublic) {
throw new ConvexError('Stack is not public')
}
return stack
}
})
// get stack by id
export const getUserStack = authQuery({
handler: async ({ db, user }, { stackId }: { stackId: Id<'stacks'> }) => {
if (!user) return {} as Doc<'stacks'>
// get stack by id and userId
return await db
.query('stacks')
.withIndex('by_userId', (q) => q.eq('userId', user._id))
.filter((q) => q.eq(q.field('_id'), stackId))
.first()
}
})
export const saveStack = authMutation(
async ({ db, user }, { stack }: { stack: Stack }) => {
const newStack = { ...stack, userId: user._id }
return db.insert('stacks', newStack)
}
)
export const deleteStack = authMutation(
async ({ db }, { stackId }: { stackId: Id<'stacks'> }) => {
const stackLikesIds = await getManyFrom(
db,
'stackLikes',
'by_stackId',
stackId
)
await Promise.all(stackLikesIds.map(({ _id }) => db.delete(_id)))
return await db.delete(stackId)
}
)
export const updateStack = authMutation(
async (
{ db, user },
{ stackId, stack }: { stackId: Id<'stacks'>; stack: Stack }
) => {
const updatedStack = { ...stack, userId: user._id }
return await db.patch(stackId, updatedStack)
}
)
export const recentlyAddedStacks = authQuery(async (ctx) => {
if (!ctx.user) {
return []
}
return ctx.db
.query('stacks')
.filter((q) => q.eq(q.field('isPublic'), true))
.order('desc')
.take(6)
})
export const requestedFeedback = authQuery(async (ctx) => {
if (!ctx.user) {
return []
}
return ctx.db
.query('stacks')
.filter((q) =>
q.and(
q.eq(q.field('isPublic'), true),
q.eq(q.field('isOpenForFeedbacks'), true)
)
)
.order('desc')
.take(6)
})
export const risingStacks = authQuery(async (ctx) => {
if (!ctx.user) {
return []
}
const sevenDaysAgo = new Date().getTime() - 7 * 24 * 60 * 60 * 1000
// Get all stackIds that have been liked in the last 7 days
const stacks = await ctx.db
.query('stackLikes')
.withIndex('by_creation_time', (q) => q.gt('_creationTime', sevenDaysAgo))
.collect()
// Count the number of likes for each stack
const stacksLikes = stacks.reduce(
(acc, { stackId }) => {
acc[stackId] = (acc[stackId] || 0) + 1
return acc
},
{} as Record<Id<'stacks'>, number>
)
// Sort the stacks by the number of likes
const sortedStacks = Object.entries(stacksLikes)
.sort((a, b) => b[1] - a[1])
.slice(0, 6)
// Fetch stack details for the top liked stacks
return await Promise.all(
sortedStacks.map(([stackId]) => ctx.db.get(stackId as Id<'stacks'>))
)
})
export const internalUpdateCoverImage = internalMutation({
args: {
stackId: v.id('stacks'),
coverImage: v.string()
},
handler: async ({ db }, { stackId, coverImage }) => {
return await db.patch(stackId, { coverImage })
}
})
export const updateStackCoverImage = authAction({
args: {
stackId: v.id('stacks'),
coverImage: v.string()
},
handler: async ({ runAction, runMutation }, { stackId, coverImage }) => {
const coverImageUrl = await runAction(internal.imageKit.uploadCoverImage, {
coverImage,
name: stackId
})
await runMutation(internal.stack.internalUpdateCoverImage, {
stackId,
coverImage: coverImageUrl
})
}
})