| 1 | import { schema } from '@ghost/db'; |
| 2 | import { ApiProperty, ApiPropertyOptional |
| 3 | import { Transform, Type |
| 4 | import { |
| 5 | IsBoolean, |
| 6 | IsIn, |
| 7 | IsInt, |
| 8 | IsISO8601, |
| 9 | IsOptional, |
| 10 | IsString, |
| 11 | Max, |
| 12 | Min, |
| 13 | ValidateNested, |
| 14 | } from 'class-validator'; |
| 15 | |
| 16 | const reasons = schema |
| 17 | const watchLevels = [ |
| 18 | 'participating', |
| 19 | ...schema.repositoryWatchLevel. |
| 20 | ] as const; |
| 21 | export type WatchLevel |
| 22 | |
| 23 | export class NotificationRepositoryDTO |
| 24 | @ApiProperty({ example |
| 25 | @IsString() |
| 26 | owner: string; |
| 27 | |
| 28 | @ApiProperty({ example |
| 29 | @IsString() |
| 30 | slug: string; |
| 31 | } |
| 32 | |
| 33 | export class NotificationThreadDTO |
| 34 | @ApiProperty() |
| 35 | @IsInt() |
| 36 | number: number; |
| 37 | |
| 38 | @ApiProperty() |
| 39 | @IsString() |
| 40 | title: string; |
| 41 | |
| 42 | @ApiProperty() |
| 43 | @IsBoolean() |
| 44 | isPullRequest: boolean; |
| 45 | |
| 46 | @ApiProperty({ enum |
| 47 | @IsIn(['open' |
| 48 | state: 'open |
| 49 | } |
| 50 | |
| 51 | export class NotificationDTO |
| 52 | @ApiProperty() |
| 53 | @IsString() |
| 54 | id: string; |
| 55 | |
| 56 | @ApiProperty({ |
| 57 | enum: reasons, |
| 58 | description: 'Why the viewer is notified, the most specific that applies.', |
| 59 | }) |
| 60 | @IsIn(reasons) |
| 61 | reason: (typeof |
| 62 | |
| 63 | @ApiProperty({ |
| 64 | description: 'The latest event on the thread.', |
| 65 | example: 'issue.commented', |
| 66 | }) |
| 67 | @IsString() |
| 68 | eventType: string; |
| 69 | |
| 70 | @ApiProperty({ |
| 71 | type: String, |
| 72 | nullable: true, |
| 73 | description: 'Who caused the latest event.', |
| 74 | }) |
| 75 | @IsString() |
| 76 | @IsOptional() |
| 77 | actorUsername: string |
| 78 | |
| 79 | @ApiProperty() |
| 80 | @IsBoolean() |
| 81 | unread: boolean; |
| 82 | |
| 83 | @ApiProperty() |
| 84 | @IsISO8601() |
| 85 | updatedAt: string; |
| 86 | |
| 87 | @ApiProperty({ type |
| 88 | @ValidateNested() |
| 89 | @Type(() => |
| 90 | repository: NotificationRepositoryDTO |
| 91 | |
| 92 | @ApiProperty({ type |
| 93 | @ValidateNested() |
| 94 | @Type(() => |
| 95 | thread: NotificationThreadDTO |
| 96 | } |
| 97 | |
| 98 | export class GetNotificationsQueryDTO |
| 99 | @ApiPropertyOptional({ |
| 100 | default: false, |
| 101 | description: 'Only unread notifications.', |
| 102 | }) |
| 103 | @Transform(({ |
| 104 | @IsBoolean() |
| 105 | @IsOptional() |
| 106 | unread?: boolean |
| 107 | |
| 108 | @ApiPropertyOptional({ |
| 109 | description: |
| 110 | 'Opaque cursor returned as `nextCursor` by the previous page. Omit for the first page.', |
| 111 | }) |
| 112 | @IsString() |
| 113 | @IsOptional() |
| 114 | cursor?: string |
| 115 | |
| 116 | @ApiPropertyOptional({ |
| 117 | description: 'Page size.', |
| 118 | minimum: 1, |
| 119 | maximum: 100, |
| 120 | default: 25, |
| 121 | }) |
| 122 | @Type(() => |
| 123 | @IsInt() |
| 124 | @Min(1) |
| 125 | @Max(100) |
| 126 | @IsOptional() |
| 127 | limit?: number |
| 128 | } |
| 129 | |
| 130 | export class GetNotificationsResponseDTO |
| 131 | @ApiProperty({ |
| 132 | type: [NotificationDTO], |
| 133 | description: 'Most recently active first.', |
| 134 | }) |
| 135 | @ValidateNested({ each |
| 136 | @Type(() => |
| 137 | notifications: NotificationDTO |
| 138 | |
| 139 | @ApiProperty({ type |
| 140 | @IsString() |
| 141 | @IsOptional() |
| 142 | nextCursor: string |
| 143 | } |
| 144 | |
| 145 | export class UnreadCountResponseDTO |
| 146 | @ApiProperty() |
| 147 | @IsInt() |
| 148 | count: number; |
| 149 | } |
| 150 | |
| 151 | export class UpdateNotificationRequestDTO |
| 152 | @ApiProperty() |
| 153 | @IsBoolean() |
| 154 | unread: boolean; |
| 155 | } |
| 156 | |
| 157 | export class IssueSubscriptionDTO |
| 158 | @ApiProperty({ |
| 159 | description: 'Whether activity on the thread notifies the viewer.', |
| 160 | }) |
| 161 | @IsBoolean() |
| 162 | subscribed: boolean; |
| 163 | } |
| 164 | |
| 165 | export class RepositoryWatchDTO |
| 166 | @ApiProperty({ |
| 167 | enum: watchLevels, |
| 168 | description: |
| 169 | '`participating` notifies about threads the viewer takes part in, `all` about every thread, `ignore` about nothing, mentions included.', |
| 170 | }) |
| 171 | @IsIn(watchLevels) |
| 172 | level: WatchLevel |
| 173 | } |