|
|
@ -27,7 +27,7 @@ import { |
|
|
|
UserItemType, |
|
|
|
GroupItemType, |
|
|
|
BlockType, |
|
|
|
Group, |
|
|
|
UserInverseSubscription, |
|
|
|
} from '../../types/collections' |
|
|
|
|
|
|
|
import { PluginOptions } from '../../types' |
|
|
@ -168,6 +168,12 @@ function getRoute(server: FastifyInstance<Server, IncomingMessage, ServerRespons |
|
|
|
id: string |
|
|
|
} |
|
|
|
|
|
|
|
interface Subscription { |
|
|
|
from: string |
|
|
|
to: string |
|
|
|
pending: boolean |
|
|
|
} |
|
|
|
|
|
|
|
const options: RouteShorthandOptions = { |
|
|
|
schema: { |
|
|
|
params: { |
|
|
@ -190,10 +196,9 @@ function getRoute(server: FastifyInstance<Server, IncomingMessage, ServerRespons |
|
|
|
const user = await getUser(server.database.client, request.params.id) |
|
|
|
if (!user) return notFoundError(reply) |
|
|
|
|
|
|
|
let subscribed = false |
|
|
|
let subscribedToYou = false |
|
|
|
const subscriptions: Subscription[] = [] |
|
|
|
|
|
|
|
if (request.viewer) { |
|
|
|
if (request.viewer && request.viewer.id !== user.id) { |
|
|
|
const viewer = await getItem<User>({ container: userContainer, id: request.viewer.id }) |
|
|
|
if (!viewer) return serverError(reply) |
|
|
|
if (!viewer.groupId) return unauthorizedError(reply) |
|
|
@ -201,23 +206,46 @@ function getRoute(server: FastifyInstance<Server, IncomingMessage, ServerRespons |
|
|
|
const blocks = await getUserBlocks(server.database.client, user.id, [viewer.id, viewer.groupId], request.log) |
|
|
|
if (blocks.length > 0) return unauthorizedError(reply) |
|
|
|
|
|
|
|
subscribed = !!(await getItem<UserSubscription>({ |
|
|
|
const subscription = (await queryItems<UserSubscription>({ |
|
|
|
container: userContainer, |
|
|
|
id: user.id, |
|
|
|
partitionKey: viewer.id, |
|
|
|
})) |
|
|
|
query: createQuerySpec('SELECT * FROM Users u WHERE u.pk = @pk AND u.userId = @userId AND u.t = @type', { |
|
|
|
userId: viewer.id, |
|
|
|
pk: user.id, |
|
|
|
type: UserItemType.Subscription, |
|
|
|
}), |
|
|
|
logger: request.log, |
|
|
|
}))[0] |
|
|
|
|
|
|
|
if (subscription) { |
|
|
|
subscriptions.push({ |
|
|
|
from: subscription.userId, |
|
|
|
to: subscription.pk, |
|
|
|
pending: subscription.pending, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
subscribedToYou = !!(await getItem<UserSubscription>({ |
|
|
|
const inverseSubscription = (await queryItems<UserInverseSubscription>({ |
|
|
|
container: userContainer, |
|
|
|
id: viewer.id, |
|
|
|
partitionKey: user.id, |
|
|
|
})) |
|
|
|
query: createQuerySpec('SELECT * FROM Users u WHERE u.pk = @pk AND u.userId = @userId AND u.t = @type', { |
|
|
|
userId: user.id, |
|
|
|
pk: viewer.id, |
|
|
|
type: UserItemType.InverseSubscription, |
|
|
|
}), |
|
|
|
logger: request.log, |
|
|
|
}))[0] |
|
|
|
|
|
|
|
if (inverseSubscription) { |
|
|
|
subscriptions.push({ |
|
|
|
from: inverseSubscription.pk, |
|
|
|
to: inverseSubscription.userId, |
|
|
|
pending: inverseSubscription.pending, |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return { |
|
|
|
...user, |
|
|
|
subscribed, |
|
|
|
subscribedToYou, |
|
|
|
subscriptions, |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
@ -297,13 +325,21 @@ function subscribeRoute(server: FastifyInstance<Server, IncomingMessage, ServerR |
|
|
|
if (blocks.length > 0) return badRequestError(reply, 'Invalid operation') |
|
|
|
|
|
|
|
await userContainer.items.create<UserSubscription>({ |
|
|
|
id: user.id, |
|
|
|
pk: request.viewer.id, |
|
|
|
userId: request.viewer.id, |
|
|
|
pk: user.id, |
|
|
|
t: UserItemType.Subscription, |
|
|
|
pending, |
|
|
|
created: Date.now(), |
|
|
|
}) |
|
|
|
|
|
|
|
await userContainer.items.create<UserInverseSubscription>({ |
|
|
|
userId: user.id, |
|
|
|
pk: request.viewer.id, |
|
|
|
t: UserItemType.InverseSubscription, |
|
|
|
pending, |
|
|
|
created: Date.now(), |
|
|
|
}) |
|
|
|
|
|
|
|
reply.code(204) |
|
|
|
}) |
|
|
|
} |
|
|
@ -335,7 +371,7 @@ function unsubscribeRoute(server: FastifyInstance<Server, IncomingMessage, Serve |
|
|
|
if (!user) return notFoundError(reply) |
|
|
|
if (!viewer) return serverError(reply) |
|
|
|
|
|
|
|
const subscriptionQuery = createQuerySpec(`SELECT u.id FROM Users u WHERE u.id = @user AND u.pk = @viewer AND u.t = @type`, { |
|
|
|
const subscriptionQuery = createQuerySpec(`SELECT u.id, u.pk FROM Users u WHERE u.userId = @user AND u.pk = @viewer AND u.t = @type`, { |
|
|
|
user: user.id, |
|
|
|
viewer: viewer.id, |
|
|
|
type: UserItemType.Subscription, |
|
|
@ -343,7 +379,18 @@ function unsubscribeRoute(server: FastifyInstance<Server, IncomingMessage, Serve |
|
|
|
|
|
|
|
const subscriptions = await queryItems<UserSubscription>({ container: userContainer, query: subscriptionQuery, logger: request.log }) |
|
|
|
for (const subscription of subscriptions) { |
|
|
|
await userContainer.item(subscription.id!, viewer.id).delete() |
|
|
|
await userContainer.item(subscription.id!, subscription.pk).delete() |
|
|
|
} |
|
|
|
|
|
|
|
const inverseSubscriptionQuery = createQuerySpec(`SELECT u.id FROM Users u WHERE u.userId = @viewer AND u.pk = @user AND u.t = @type`, { |
|
|
|
user: user.id, |
|
|
|
viewer: viewer.id, |
|
|
|
type: UserItemType.InverseSubscription, |
|
|
|
}) |
|
|
|
|
|
|
|
const inverseSubscriptions = await queryItems<UserInverseSubscription>({ container: userContainer, query: inverseSubscriptionQuery, logger: request.log }) |
|
|
|
for (const inverseSubscription of inverseSubscriptions) { |
|
|
|
await userContainer.item(inverseSubscription.id!, inverseSubscription.pk).delete() |
|
|
|
} |
|
|
|
|
|
|
|
reply.code(204) |
|
|
|