Spaces:
Running
Running
File size: 13,630 Bytes
6abfd9d | 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | import { Injectable } from "@nestjs/common";
import { z } from "zod";
import {
router,
publicProcedure,
protectedProcedure,
adminPermissionProcedure,
} from "../trpc/trpc.init";
import { NetworkService } from "./network.service";
import { TrustScoreEngine } from "./trust-score.service";
import { VerificationPipeline } from "./verification-pipeline.service";
import {
AdminPermission,
createGuestPublicProfileSchema,
updateGuestPublicProfileSchema,
guestNetworkSearchSchema,
createNetworkBookingRequestSchema,
respondToBookingRequestSchema,
createNetworkBookmarkSchema,
updateNetworkBookmarkSchema,
createHostReviewSchema,
trackProfileViewSchema,
requestBadgeSchema,
revokeBadgeSchema,
getVerificationStatusSchema,
getTrustScoreSchema,
recomputeAllTrustScoresSchema,
BookmarkStatus,
ProfileViewSource,
VerificationBadgeType,
} from "@micprotocol/shared-types";
@Injectable()
export class NetworkRouter {
constructor(
private readonly networkService: NetworkService,
private readonly trustScoreEngine: TrustScoreEngine,
private readonly verificationPipeline: VerificationPipeline,
) {}
get router() {
return router({
// βββ Profile Management βββββββββββββββββββββββ
createProfile: protectedProcedure
.input(createGuestPublicProfileSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.createProfile(ctx.userId!, input);
}),
updateProfile: protectedProcedure
.input(updateGuestPublicProfileSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.updateProfile(ctx.userId!, input);
}),
getMyProfile: protectedProcedure
.query(async ({ ctx }) => {
return this.networkService.getMyProfile(ctx.userId!);
}),
// Public profile by vanity URL
getProfile: publicProcedure
.input(z.object({ vanityUrl: z.string() }))
.query(async ({ input }) => {
return this.networkService.getProfileByVanityUrl(input.vanityUrl);
}),
getProfileById: publicProcedure
.input(z.object({ id: z.string().uuid() }))
.query(async ({ input }) => {
return this.networkService.getProfileById(input.id);
}),
// βββ Discovery Search βββββββββββββββββββββββββ
search: publicProcedure
.input(guestNetworkSearchSchema)
.query(async ({ input }) => {
return this.networkService.searchProfiles(input);
}),
trending: publicProcedure
.input(
z.object({
region: z.string().optional(),
limit: z.number().int().min(1).max(50).default(10),
})
)
.query(async ({ input }) => {
return this.networkService.getTrending(input.region, input.limit);
}),
// βββ Profile Views ββββββββββββββββββββββββββββ
trackView: publicProcedure
.input(trackProfileViewSchema)
.mutation(async ({ input }) => {
return this.networkService.trackView(input);
}),
getViewStats: protectedProcedure
.input(
z.object({
profileId: z.string().uuid(),
days: z.number().int().min(1).max(365).default(30),
})
)
.query(async ({ ctx, input }) => {
return this.networkService.getProfileViewStats(
ctx.userId!,
input.profileId,
input.days,
);
}),
// βββ Bookmarks ββββββββββββββββββββββββββββββββ
bookmark: protectedProcedure
.input(createNetworkBookmarkSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.createBookmark(input, ctx.userId!);
}),
updateBookmark: protectedProcedure
.input(
z.object({
id: z.string().uuid(),
data: updateNetworkBookmarkSchema,
})
)
.mutation(async ({ ctx, input }) => {
return this.networkService.updateBookmark(ctx.userId!, input.id, input.data);
}),
removeBookmark: protectedProcedure
.input(z.object({ id: z.string().uuid() }))
.mutation(async ({ ctx, input }) => {
return this.networkService.removeBookmark(ctx.userId!, input.id);
}),
listBookmarks: protectedProcedure
.input(
z.object({
podcastId: z.string().uuid(),
status: z.nativeEnum(BookmarkStatus).optional(),
})
)
.query(async ({ ctx, input }) => {
return this.networkService.listBookmarks(
input.podcastId,
ctx.userId!,
input.status,
);
}),
// βββ Booking Requests βββββββββββββββββββββββββ
createBookingRequest: protectedProcedure
.input(createNetworkBookingRequestSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.createBookingRequest(input, ctx.userId!);
}),
respondToBookingRequest: protectedProcedure
.input(respondToBookingRequestSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.respondToBookingRequest(
input.requestId,
ctx.userId!,
input.status,
);
}),
listBookingRequests: protectedProcedure
.input(
z.object({
profileId: z.string().uuid(),
status: z.string().optional(),
})
)
.query(async ({ ctx, input }) => {
return this.networkService.listBookingRequests(
ctx.userId!,
input.profileId,
input.status,
);
}),
listSentBookingRequests: protectedProcedure
.input(
z.object({
podcastId: z.string().uuid(),
status: z.string().optional(),
})
)
.query(async ({ ctx, input }) => {
return this.networkService.listSentBookingRequests(
ctx.userId!,
input.podcastId,
input.status,
);
}),
// βββ Reviews ββββββββββββββββββββββββββββββββββ
submitReview: protectedProcedure
.input(createHostReviewSchema)
.mutation(async ({ ctx, input }) => {
return this.networkService.submitReview(ctx.userId!, input);
}),
// βββ Reputation βββββββββββββββββββββββββββββββ
getReputationInsights: protectedProcedure
.input(z.object({ profileId: z.string().uuid() }))
.query(async ({ ctx, input }) => {
return this.networkService.getReputationInsights(ctx.userId!, input.profileId);
}),
recalculateReputation: protectedProcedure
.input(z.object({ profileId: z.string().uuid() }))
.mutation(async ({ ctx, input }) => {
return this.networkService.recalculateReputation(ctx.userId!, input.profileId);
}),
// βββ Recommendations ββββββββββββββββββββββββββ
getRecommendations: protectedProcedure
.input(
z.object({
profileId: z.string().uuid(),
limit: z.number().int().min(1).max(20).default(5),
})
)
.query(async ({ ctx, input }) => {
return this.networkService.getRecommendations(
ctx.userId!,
input.profileId,
input.limit
);
}),
getProfileNetworkStats: protectedProcedure
.input(z.object({ profileId: z.string().uuid() }))
.query(async ({ ctx, input }) => {
return this.networkService.getProfileNetworkStats(ctx.userId!, input.profileId);
}),
// βββ Podcast Network Stats ββββββββββββββββββββ
stats: protectedProcedure
.input(z.object({ podcastId: z.string().uuid() }))
.query(async ({ ctx, input }) => {
return this.networkService.getPodcastNetworkStats(input.podcastId, ctx.userId!);
}),
// βββ Admin ββββββββββββββββββββββββββββββββββββ
verifyProfile: adminPermissionProcedure(AdminPermission.MODERATION_RESOLVE)
.input(
z.object({
profileId: z.string().uuid(),
status: z.enum(["basic", "verified", "featured"]),
})
)
.mutation(async ({ input, ctx }) => {
// Defense in depth: verifyProfile() re-checks the caller inside the
// service via assertPlatformAdmin.
return this.networkService.verifyProfile(
ctx.userId!, // Pass current user for authorization
input.profileId,
input.status,
);
}),
// βββ Trust Score βββββββββββββββββββββββββββββ
getTrustScore: protectedProcedure
.input(getTrustScoreSchema)
.query(async ({ input }) => {
return this.trustScoreEngine.getTrustScore(input.profileId);
}),
recomputeTrustScore: adminPermissionProcedure(AdminPermission.OPS_HEALTH)
.input(z.object({ profileId: z.string().uuid() }))
.mutation(async ({ input }) => {
return this.trustScoreEngine.computeAndPersist(input.profileId);
}),
recomputeAllTrustScores: adminPermissionProcedure(AdminPermission.OPS_HEALTH)
.input(recomputeAllTrustScoresSchema)
.mutation(async () => {
return this.trustScoreEngine.recomputeAll();
}),
// βββ Verification Pipeline βββββββββββββββββββ
requestBadge: protectedProcedure
.input(requestBadgeSchema)
.mutation(async ({ ctx, input }) => {
return this.verificationPipeline.requestBadge({
profileId: input.profileId,
badgeType: input.badgeType as any,
evidence: input.evidence,
adminUserId: input.badgeType === "identity" ? ctx.userId! : undefined,
});
}),
revokeBadge: adminPermissionProcedure(AdminPermission.MODERATION_TAKEDOWN)
.input(revokeBadgeSchema)
.mutation(async ({ ctx, input }) => {
// Defense in depth: revokeBadge() re-checks the caller inside the
// service via assertPlatformAdmin.
return this.verificationPipeline.revokeBadge(
ctx.userId!,
input.profileId,
input.badgeType as any,
);
}),
getVerificationStatus: protectedProcedure
.input(getVerificationStatusSchema)
.query(async ({ input }) => {
return this.verificationPipeline.getVerificationStatus(input.profileId);
}),
// Grants verification badges β a moderation decision surface (and it
// targets arbitrary profileIds), so it requires MODERATION_RESOLVE.
autoVerifyBadges: adminPermissionProcedure(AdminPermission.MODERATION_RESOLVE)
.input(z.object({ profileId: z.string().uuid() }))
.mutation(async ({ input }) => {
return this.verificationPipeline.autoVerifyEligibleBadges(input.profileId);
}),
// βββ Network Health ββββββββββββββββββββββββββ
getNetworkHealth: adminPermissionProcedure(AdminPermission.OPS_READ).query(async () => {
return this.networkService.getNetworkHealth();
}),
});
}
} |