@atproto/ozone 0.1.125 → 0.1.127

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/api/index.d.ts.map +1 -1
  3. package/dist/api/index.js +10 -0
  4. package/dist/api/index.js.map +1 -1
  5. package/dist/api/safelink/addRule.d.ts +4 -0
  6. package/dist/api/safelink/addRule.d.ts.map +1 -0
  7. package/dist/api/safelink/addRule.js +37 -0
  8. package/dist/api/safelink/addRule.js.map +1 -0
  9. package/dist/api/safelink/queryEvents.d.ts +4 -0
  10. package/dist/api/safelink/queryEvents.d.ts.map +1 -0
  11. package/dist/api/safelink/queryEvents.js +29 -0
  12. package/dist/api/safelink/queryEvents.js.map +1 -0
  13. package/dist/api/safelink/queryRules.d.ts +4 -0
  14. package/dist/api/safelink/queryRules.d.ts.map +1 -0
  15. package/dist/api/safelink/queryRules.js +43 -0
  16. package/dist/api/safelink/queryRules.js.map +1 -0
  17. package/dist/api/safelink/removeRule.d.ts +4 -0
  18. package/dist/api/safelink/removeRule.d.ts.map +1 -0
  19. package/dist/api/safelink/removeRule.js +35 -0
  20. package/dist/api/safelink/removeRule.js.map +1 -0
  21. package/dist/api/safelink/updateRule.d.ts +4 -0
  22. package/dist/api/safelink/updateRule.d.ts.map +1 -0
  23. package/dist/api/safelink/updateRule.js +37 -0
  24. package/dist/api/safelink/updateRule.js.map +1 -0
  25. package/dist/api/util.d.ts +8 -0
  26. package/dist/api/util.d.ts.map +1 -1
  27. package/dist/api/util.js +33 -1
  28. package/dist/api/util.js.map +1 -1
  29. package/dist/context.d.ts +3 -0
  30. package/dist/context.d.ts.map +1 -1
  31. package/dist/context.js +12 -6
  32. package/dist/context.js.map +1 -1
  33. package/dist/db/migrations/20250609T110704000Z-safelink.d.ts +4 -0
  34. package/dist/db/migrations/20250609T110704000Z-safelink.d.ts.map +1 -0
  35. package/dist/db/migrations/20250609T110704000Z-safelink.js +51 -0
  36. package/dist/db/migrations/20250609T110704000Z-safelink.js.map +1 -0
  37. package/dist/db/migrations/index.d.ts +1 -0
  38. package/dist/db/migrations/index.d.ts.map +1 -1
  39. package/dist/db/migrations/index.js +2 -1
  40. package/dist/db/migrations/index.js.map +1 -1
  41. package/dist/db/schema/index.d.ts +2 -1
  42. package/dist/db/schema/index.d.ts.map +1 -1
  43. package/dist/db/schema/safelink.d.ts +31 -0
  44. package/dist/db/schema/safelink.d.ts.map +1 -0
  45. package/dist/db/schema/safelink.js +6 -0
  46. package/dist/db/schema/safelink.js.map +1 -0
  47. package/dist/lexicon/index.d.ts +15 -0
  48. package/dist/lexicon/index.d.ts.map +1 -1
  49. package/dist/lexicon/index.js +40 -1
  50. package/dist/lexicon/index.js.map +1 -1
  51. package/dist/lexicon/lexicons.d.ts +11024 -10188
  52. package/dist/lexicon/lexicons.d.ts.map +1 -1
  53. package/dist/lexicon/lexicons.js +448 -0
  54. package/dist/lexicon/lexicons.js.map +1 -1
  55. package/dist/lexicon/types/tools/ozone/safelink/addRule.d.ts +47 -0
  56. package/dist/lexicon/types/tools/ozone/safelink/addRule.d.ts.map +1 -0
  57. package/dist/lexicon/types/tools/ozone/safelink/addRule.js +7 -0
  58. package/dist/lexicon/types/tools/ozone/safelink/addRule.js.map +1 -0
  59. package/dist/lexicon/types/tools/ozone/safelink/defs.d.ts +47 -0
  60. package/dist/lexicon/types/tools/ozone/safelink/defs.d.ts.map +1 -0
  61. package/dist/lexicon/types/tools/ozone/safelink/defs.js +25 -0
  62. package/dist/lexicon/types/tools/ozone/safelink/defs.js.map +1 -0
  63. package/dist/lexicon/types/tools/ozone/safelink/queryEvents.d.ts +51 -0
  64. package/dist/lexicon/types/tools/ozone/safelink/queryEvents.d.ts.map +1 -0
  65. package/dist/lexicon/types/tools/ozone/safelink/queryEvents.js +7 -0
  66. package/dist/lexicon/types/tools/ozone/safelink/queryEvents.js.map +1 -0
  67. package/dist/lexicon/types/tools/ozone/safelink/queryRules.d.ts +57 -0
  68. package/dist/lexicon/types/tools/ozone/safelink/queryRules.d.ts.map +1 -0
  69. package/dist/lexicon/types/tools/ozone/safelink/queryRules.js +7 -0
  70. package/dist/lexicon/types/tools/ozone/safelink/queryRules.js.map +1 -0
  71. package/dist/lexicon/types/tools/ozone/safelink/removeRule.d.ts +45 -0
  72. package/dist/lexicon/types/tools/ozone/safelink/removeRule.d.ts.map +1 -0
  73. package/dist/lexicon/types/tools/ozone/safelink/removeRule.js +7 -0
  74. package/dist/lexicon/types/tools/ozone/safelink/removeRule.js.map +1 -0
  75. package/dist/lexicon/types/tools/ozone/safelink/updateRule.d.ts +47 -0
  76. package/dist/lexicon/types/tools/ozone/safelink/updateRule.d.ts.map +1 -0
  77. package/dist/lexicon/types/tools/ozone/safelink/updateRule.js +7 -0
  78. package/dist/lexicon/types/tools/ozone/safelink/updateRule.js.map +1 -0
  79. package/dist/mod-service/status.d.ts +6 -0
  80. package/dist/mod-service/status.d.ts.map +1 -1
  81. package/dist/safelink/service.d.ts +69 -0
  82. package/dist/safelink/service.d.ts.map +1 -0
  83. package/dist/safelink/service.js +179 -0
  84. package/dist/safelink/service.js.map +1 -0
  85. package/package.json +3 -3
  86. package/src/api/index.ts +10 -0
  87. package/src/api/safelink/addRule.ts +48 -0
  88. package/src/api/safelink/queryEvents.ts +32 -0
  89. package/src/api/safelink/queryRules.ts +58 -0
  90. package/src/api/safelink/removeRule.ts +42 -0
  91. package/src/api/safelink/updateRule.ts +48 -0
  92. package/src/api/util.ts +38 -0
  93. package/src/context.ts +11 -0
  94. package/src/db/migrations/20250609T110704000Z-safelink.ts +53 -0
  95. package/src/db/migrations/index.ts +1 -0
  96. package/src/db/schema/index.ts +3 -1
  97. package/src/db/schema/safelink.ts +39 -0
  98. package/src/lexicon/index.ts +70 -0
  99. package/src/lexicon/lexicons.ts +455 -0
  100. package/src/lexicon/types/tools/ozone/safelink/addRule.ts +64 -0
  101. package/src/lexicon/types/tools/ozone/safelink/defs.ts +76 -0
  102. package/src/lexicon/types/tools/ozone/safelink/queryEvents.ts +68 -0
  103. package/src/lexicon/types/tools/ozone/safelink/queryRules.ts +74 -0
  104. package/src/lexicon/types/tools/ozone/safelink/removeRule.ts +62 -0
  105. package/src/lexicon/types/tools/ozone/safelink/updateRule.ts +64 -0
  106. package/src/safelink/service.ts +304 -0
  107. package/tests/__snapshots__/get-starter-pack.test.ts.snap +69 -0
  108. package/tests/__snapshots__/get-subjects.test.ts.snap +6 -0
  109. package/tests/__snapshots__/safelink.test.ts.snap +179 -0
  110. package/tests/__snapshots__/team.test.ts.snap +24 -0
  111. package/tests/__snapshots__/verification-listener.test.ts.snap +6 -0
  112. package/tests/__snapshots__/verification.test.ts.snap +12 -0
  113. package/tests/communication-templates.test.ts +7 -7
  114. package/tests/safelink.test.ts +534 -0
  115. package/tsconfig.build.tsbuildinfo +1 -1
  116. package/tsconfig.tests.tsbuildinfo +1 -1
@@ -0,0 +1,76 @@
1
+ /**
2
+ * GENERATED CODE - DO NOT MODIFY
3
+ */
4
+ import { type ValidationResult, BlobRef } from '@atproto/lexicon'
5
+ import { CID } from 'multiformats/cid'
6
+ import { validate as _validate } from '../../../../lexicons'
7
+ import {
8
+ type $Typed,
9
+ is$typed as _is$typed,
10
+ type OmitKey,
11
+ } from '../../../../util'
12
+
13
+ const is$typed = _is$typed,
14
+ validate = _validate
15
+ const id = 'tools.ozone.safelink.defs'
16
+
17
+ /** An event for URL safety decisions */
18
+ export interface Event {
19
+ $type?: 'tools.ozone.safelink.defs#event'
20
+ /** Auto-incrementing row ID */
21
+ id: number
22
+ eventType: EventType
23
+ /** The URL that this rule applies to */
24
+ url: string
25
+ pattern: PatternType
26
+ action: ActionType
27
+ reason: ReasonType
28
+ /** DID of the user who created this rule */
29
+ createdBy: string
30
+ createdAt: string
31
+ /** Optional comment about the decision */
32
+ comment?: string
33
+ }
34
+
35
+ const hashEvent = 'event'
36
+
37
+ export function isEvent<V>(v: V) {
38
+ return is$typed(v, id, hashEvent)
39
+ }
40
+
41
+ export function validateEvent<V>(v: V) {
42
+ return validate<Event & V>(v, id, hashEvent)
43
+ }
44
+
45
+ export type EventType = 'addRule' | 'updateRule' | 'removeRule' | (string & {})
46
+ export type PatternType = 'domain' | 'url' | (string & {})
47
+ export type ActionType = 'block' | 'warn' | 'whitelist' | (string & {})
48
+ export type ReasonType = 'csam' | 'spam' | 'phishing' | 'none' | (string & {})
49
+
50
+ /** Input for creating a URL safety rule */
51
+ export interface UrlRule {
52
+ $type?: 'tools.ozone.safelink.defs#urlRule'
53
+ /** The URL or domain to apply the rule to */
54
+ url: string
55
+ pattern: PatternType
56
+ action: ActionType
57
+ reason: ReasonType
58
+ /** Optional comment about the decision */
59
+ comment?: string
60
+ /** DID of the user added the rule. */
61
+ createdBy: string
62
+ /** Timestamp when the rule was created */
63
+ createdAt: string
64
+ /** Timestamp when the rule was last updated */
65
+ updatedAt: string
66
+ }
67
+
68
+ const hashUrlRule = 'urlRule'
69
+
70
+ export function isUrlRule<V>(v: V) {
71
+ return is$typed(v, id, hashUrlRule)
72
+ }
73
+
74
+ export function validateUrlRule<V>(v: V) {
75
+ return validate<UrlRule & V>(v, id, hashUrlRule)
76
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * GENERATED CODE - DO NOT MODIFY
3
+ */
4
+ import express from 'express'
5
+ import { type ValidationResult, BlobRef } from '@atproto/lexicon'
6
+ import { CID } from 'multiformats/cid'
7
+ import { validate as _validate } from '../../../../lexicons'
8
+ import {
9
+ type $Typed,
10
+ is$typed as _is$typed,
11
+ type OmitKey,
12
+ } from '../../../../util'
13
+ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
14
+ import type * as ToolsOzoneSafelinkDefs from './defs.js'
15
+
16
+ const is$typed = _is$typed,
17
+ validate = _validate
18
+ const id = 'tools.ozone.safelink.queryEvents'
19
+
20
+ export interface QueryParams {}
21
+
22
+ export interface InputSchema {
23
+ /** Cursor for pagination */
24
+ cursor?: string
25
+ /** Maximum number of results to return */
26
+ limit: number
27
+ /** Filter by specific URLs or domains */
28
+ urls?: string[]
29
+ /** Filter by pattern type */
30
+ patternType?: string
31
+ /** Sort direction */
32
+ sortDirection: 'asc' | 'desc' | (string & {})
33
+ }
34
+
35
+ export interface OutputSchema {
36
+ /** Next cursor for pagination. Only present if there are more results. */
37
+ cursor?: string
38
+ events: ToolsOzoneSafelinkDefs.Event[]
39
+ }
40
+
41
+ export interface HandlerInput {
42
+ encoding: 'application/json'
43
+ body: InputSchema
44
+ }
45
+
46
+ export interface HandlerSuccess {
47
+ encoding: 'application/json'
48
+ body: OutputSchema
49
+ headers?: { [key: string]: string }
50
+ }
51
+
52
+ export interface HandlerError {
53
+ status: number
54
+ message?: string
55
+ }
56
+
57
+ export type HandlerOutput = HandlerError | HandlerSuccess | HandlerPipeThrough
58
+ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
59
+ auth: HA
60
+ params: QueryParams
61
+ input: HandlerInput
62
+ req: express.Request
63
+ res: express.Response
64
+ resetRouteRateLimits: () => Promise<void>
65
+ }
66
+ export type Handler<HA extends HandlerAuth = never> = (
67
+ ctx: HandlerReqCtx<HA>,
68
+ ) => Promise<HandlerOutput> | HandlerOutput
@@ -0,0 +1,74 @@
1
+ /**
2
+ * GENERATED CODE - DO NOT MODIFY
3
+ */
4
+ import express from 'express'
5
+ import { type ValidationResult, BlobRef } from '@atproto/lexicon'
6
+ import { CID } from 'multiformats/cid'
7
+ import { validate as _validate } from '../../../../lexicons'
8
+ import {
9
+ type $Typed,
10
+ is$typed as _is$typed,
11
+ type OmitKey,
12
+ } from '../../../../util'
13
+ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
14
+ import type * as ToolsOzoneSafelinkDefs from './defs.js'
15
+
16
+ const is$typed = _is$typed,
17
+ validate = _validate
18
+ const id = 'tools.ozone.safelink.queryRules'
19
+
20
+ export interface QueryParams {}
21
+
22
+ export interface InputSchema {
23
+ /** Cursor for pagination */
24
+ cursor?: string
25
+ /** Maximum number of results to return */
26
+ limit: number
27
+ /** Filter by specific URLs or domains */
28
+ urls?: string[]
29
+ /** Filter by pattern type */
30
+ patternType?: string
31
+ /** Filter by action types */
32
+ actions?: string[]
33
+ /** Filter by reason type */
34
+ reason?: string
35
+ /** Filter by rule creator */
36
+ createdBy?: string
37
+ /** Sort direction */
38
+ sortDirection: 'asc' | 'desc' | (string & {})
39
+ }
40
+
41
+ export interface OutputSchema {
42
+ /** Next cursor for pagination. Only present if there are more results. */
43
+ cursor?: string
44
+ rules: ToolsOzoneSafelinkDefs.UrlRule[]
45
+ }
46
+
47
+ export interface HandlerInput {
48
+ encoding: 'application/json'
49
+ body: InputSchema
50
+ }
51
+
52
+ export interface HandlerSuccess {
53
+ encoding: 'application/json'
54
+ body: OutputSchema
55
+ headers?: { [key: string]: string }
56
+ }
57
+
58
+ export interface HandlerError {
59
+ status: number
60
+ message?: string
61
+ }
62
+
63
+ export type HandlerOutput = HandlerError | HandlerSuccess | HandlerPipeThrough
64
+ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
65
+ auth: HA
66
+ params: QueryParams
67
+ input: HandlerInput
68
+ req: express.Request
69
+ res: express.Response
70
+ resetRouteRateLimits: () => Promise<void>
71
+ }
72
+ export type Handler<HA extends HandlerAuth = never> = (
73
+ ctx: HandlerReqCtx<HA>,
74
+ ) => Promise<HandlerOutput> | HandlerOutput
@@ -0,0 +1,62 @@
1
+ /**
2
+ * GENERATED CODE - DO NOT MODIFY
3
+ */
4
+ import express from 'express'
5
+ import { type ValidationResult, BlobRef } from '@atproto/lexicon'
6
+ import { CID } from 'multiformats/cid'
7
+ import { validate as _validate } from '../../../../lexicons'
8
+ import {
9
+ type $Typed,
10
+ is$typed as _is$typed,
11
+ type OmitKey,
12
+ } from '../../../../util'
13
+ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
14
+ import type * as ToolsOzoneSafelinkDefs from './defs.js'
15
+
16
+ const is$typed = _is$typed,
17
+ validate = _validate
18
+ const id = 'tools.ozone.safelink.removeRule'
19
+
20
+ export interface QueryParams {}
21
+
22
+ export interface InputSchema {
23
+ /** The URL or domain to remove the rule for */
24
+ url: string
25
+ pattern: ToolsOzoneSafelinkDefs.PatternType
26
+ /** Optional comment about why the rule is being removed */
27
+ comment?: string
28
+ /** Optional DID of the user. Only respected when using admin auth. */
29
+ createdBy?: string
30
+ }
31
+
32
+ export type OutputSchema = ToolsOzoneSafelinkDefs.Event
33
+
34
+ export interface HandlerInput {
35
+ encoding: 'application/json'
36
+ body: InputSchema
37
+ }
38
+
39
+ export interface HandlerSuccess {
40
+ encoding: 'application/json'
41
+ body: OutputSchema
42
+ headers?: { [key: string]: string }
43
+ }
44
+
45
+ export interface HandlerError {
46
+ status: number
47
+ message?: string
48
+ error?: 'RuleNotFound'
49
+ }
50
+
51
+ export type HandlerOutput = HandlerError | HandlerSuccess | HandlerPipeThrough
52
+ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
53
+ auth: HA
54
+ params: QueryParams
55
+ input: HandlerInput
56
+ req: express.Request
57
+ res: express.Response
58
+ resetRouteRateLimits: () => Promise<void>
59
+ }
60
+ export type Handler<HA extends HandlerAuth = never> = (
61
+ ctx: HandlerReqCtx<HA>,
62
+ ) => Promise<HandlerOutput> | HandlerOutput
@@ -0,0 +1,64 @@
1
+ /**
2
+ * GENERATED CODE - DO NOT MODIFY
3
+ */
4
+ import express from 'express'
5
+ import { type ValidationResult, BlobRef } from '@atproto/lexicon'
6
+ import { CID } from 'multiformats/cid'
7
+ import { validate as _validate } from '../../../../lexicons'
8
+ import {
9
+ type $Typed,
10
+ is$typed as _is$typed,
11
+ type OmitKey,
12
+ } from '../../../../util'
13
+ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
14
+ import type * as ToolsOzoneSafelinkDefs from './defs.js'
15
+
16
+ const is$typed = _is$typed,
17
+ validate = _validate
18
+ const id = 'tools.ozone.safelink.updateRule'
19
+
20
+ export interface QueryParams {}
21
+
22
+ export interface InputSchema {
23
+ /** The URL or domain to update the rule for */
24
+ url: string
25
+ pattern: ToolsOzoneSafelinkDefs.PatternType
26
+ action: ToolsOzoneSafelinkDefs.ActionType
27
+ reason: ToolsOzoneSafelinkDefs.ReasonType
28
+ /** Optional comment about the update */
29
+ comment?: string
30
+ /** Optional DID to credit as the creator. Only respected for admin_token authentication. */
31
+ createdBy?: string
32
+ }
33
+
34
+ export type OutputSchema = ToolsOzoneSafelinkDefs.Event
35
+
36
+ export interface HandlerInput {
37
+ encoding: 'application/json'
38
+ body: InputSchema
39
+ }
40
+
41
+ export interface HandlerSuccess {
42
+ encoding: 'application/json'
43
+ body: OutputSchema
44
+ headers?: { [key: string]: string }
45
+ }
46
+
47
+ export interface HandlerError {
48
+ status: number
49
+ message?: string
50
+ error?: 'RuleNotFound'
51
+ }
52
+
53
+ export type HandlerOutput = HandlerError | HandlerSuccess | HandlerPipeThrough
54
+ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
55
+ auth: HA
56
+ params: QueryParams
57
+ input: HandlerInput
58
+ req: express.Request
59
+ res: express.Response
60
+ resetRouteRateLimits: () => Promise<void>
61
+ }
62
+ export type Handler<HA extends HandlerAuth = never> = (
63
+ ctx: HandlerReqCtx<HA>,
64
+ ) => Promise<HandlerOutput> | HandlerOutput
@@ -0,0 +1,304 @@
1
+ import { Selectable } from 'kysely'
2
+ import { ToolsOzoneSafelinkDefs } from '@atproto/api'
3
+ import { InvalidRequestError } from '@atproto/xrpc-server'
4
+ import {
5
+ SafelinkActionType,
6
+ SafelinkPatternType,
7
+ SafelinkReasonType,
8
+ } from '../api/util'
9
+ import { Database } from '../db'
10
+ import { SafelinkEvent, SafelinkRule } from '../db/schema/safelink'
11
+
12
+ export type SafelinkRuleServiceCreator = (db: Database) => SafelinkRuleService
13
+
14
+ export class SafelinkRuleService {
15
+ constructor(public db: Database) {}
16
+
17
+ static creator() {
18
+ return (db: Database) => new SafelinkRuleService(db)
19
+ }
20
+
21
+ formatEvent(event: Selectable<SafelinkEvent>): ToolsOzoneSafelinkDefs.Event {
22
+ return {
23
+ id: event.id,
24
+ eventType: event.eventType,
25
+ url: event.url,
26
+ pattern: event.pattern,
27
+ action: event.action,
28
+ reason: event.reason,
29
+ createdBy: event.createdBy,
30
+ createdAt: new Date(event.createdAt).toISOString(),
31
+ comment: event.comment || undefined,
32
+ }
33
+ }
34
+
35
+ async addRule({
36
+ url,
37
+ pattern,
38
+ action,
39
+ reason,
40
+ createdBy,
41
+ comment,
42
+ }: {
43
+ url: string
44
+ pattern: SafelinkPatternType
45
+ action: SafelinkActionType
46
+ reason: SafelinkReasonType
47
+ createdBy: string
48
+ comment?: string
49
+ }): Promise<Selectable<SafelinkEvent>> {
50
+ const existingRule = await this.getActiveRule(url, pattern)
51
+ if (existingRule) {
52
+ throw new InvalidRequestError(
53
+ 'A rule for this URL/domain already exists',
54
+ 'RuleAlreadyExists',
55
+ )
56
+ }
57
+
58
+ const now = new Date().toISOString()
59
+ const rule = {
60
+ url,
61
+ pattern,
62
+ action,
63
+ reason,
64
+ createdBy,
65
+ comment: comment || null,
66
+ createdAt: now,
67
+ }
68
+
69
+ return await this.db.transaction(async (txn) => {
70
+ const event = await txn.db
71
+ .insertInto('safelink_event')
72
+ .values({
73
+ eventType: 'addRule',
74
+ ...rule,
75
+ })
76
+ .returningAll()
77
+ .executeTakeFirstOrThrow()
78
+
79
+ await txn.db
80
+ .insertInto('safelink_rule')
81
+ .values({ ...rule, updatedAt: now })
82
+ .execute()
83
+
84
+ return event
85
+ })
86
+ }
87
+
88
+ async updateRule({
89
+ url,
90
+ pattern,
91
+ action,
92
+ reason,
93
+ createdBy,
94
+ comment,
95
+ }: {
96
+ url: string
97
+ pattern: SafelinkPatternType
98
+ action: SafelinkActionType
99
+ reason: SafelinkReasonType
100
+ createdBy: string
101
+ comment?: string
102
+ }): Promise<Selectable<SafelinkEvent>> {
103
+ const existingRule = await this.getActiveRule(url, pattern)
104
+ if (!existingRule) {
105
+ throw new InvalidRequestError(
106
+ 'No active rule found for this URL/domain',
107
+ 'RuleNotFound',
108
+ )
109
+ }
110
+
111
+ const now = new Date().toISOString()
112
+ const rule = {
113
+ action,
114
+ reason,
115
+ createdBy,
116
+ comment: comment || null,
117
+ }
118
+
119
+ return await this.db.transaction(async (txn) => {
120
+ const event = await txn.db
121
+ .insertInto('safelink_event')
122
+ .values({
123
+ createdAt: now,
124
+ url: existingRule.url,
125
+ pattern: existingRule.pattern,
126
+ eventType: 'updateRule',
127
+ ...rule,
128
+ })
129
+ .returningAll()
130
+ .executeTakeFirstOrThrow()
131
+
132
+ await txn.db
133
+ .updateTable('safelink_rule')
134
+ .set(rule)
135
+ .where('url', '=', existingRule.url)
136
+ .where('pattern', '=', existingRule.pattern)
137
+ .execute()
138
+
139
+ return event
140
+ })
141
+ }
142
+
143
+ async removeRule({
144
+ url,
145
+ pattern,
146
+ createdBy,
147
+ comment,
148
+ }: {
149
+ url: string
150
+ pattern: SafelinkPatternType
151
+ createdBy: string
152
+ comment?: string
153
+ }): Promise<Selectable<SafelinkEvent>> {
154
+ const existingRule = await this.getActiveRule(url, pattern)
155
+ if (!existingRule) {
156
+ throw new InvalidRequestError(
157
+ 'No active rule found for this URL/domain',
158
+ 'RuleNotFound',
159
+ )
160
+ }
161
+
162
+ return await this.db.transaction(async (txn) => {
163
+ const event = await txn.db
164
+ .insertInto('safelink_event')
165
+ .values({
166
+ eventType: 'removeRule',
167
+ url,
168
+ pattern,
169
+ action: existingRule.action,
170
+ reason: existingRule.reason,
171
+ createdBy,
172
+ comment: comment || null,
173
+ createdAt: new Date().toISOString(),
174
+ })
175
+ .returningAll()
176
+ .executeTakeFirstOrThrow()
177
+
178
+ await txn.db
179
+ .deleteFrom('safelink_rule')
180
+ .where('url', '=', url)
181
+ .where('pattern', '=', pattern)
182
+ .execute()
183
+
184
+ return event
185
+ })
186
+ }
187
+
188
+ async getActiveRule(url: string, pattern: SafelinkPatternType) {
189
+ const rule = await this.db.db
190
+ .selectFrom('safelink_rule')
191
+ .selectAll()
192
+ .where('url', '=', url)
193
+ .where('pattern', '=', pattern)
194
+ .executeTakeFirst()
195
+
196
+ if (!rule) {
197
+ return null
198
+ }
199
+
200
+ return rule
201
+ }
202
+
203
+ async getActiveRules({
204
+ cursor,
205
+ limit = 50,
206
+ urls,
207
+ patternType,
208
+ actions,
209
+ reason,
210
+ createdBy,
211
+ direction = 'desc',
212
+ }: {
213
+ cursor?: string
214
+ limit?: number
215
+ urls?: string[]
216
+ patternType?: SafelinkPatternType
217
+ actions?: SafelinkActionType[]
218
+ reason?: SafelinkReasonType
219
+ createdBy?: string
220
+ direction?: 'asc' | 'desc'
221
+ } = {}): Promise<{
222
+ rules: Selectable<SafelinkRule>[]
223
+ cursor?: string
224
+ }> {
225
+ let query = this.db.db.selectFrom('safelink_rule').selectAll()
226
+
227
+ if (urls && urls.length > 0) {
228
+ query = query.where('url', 'in', urls)
229
+ }
230
+
231
+ if (patternType) {
232
+ query = query.where('pattern', '=', patternType)
233
+ }
234
+
235
+ if (actions && actions.length > 0) {
236
+ query = query.where('action', 'in', actions)
237
+ }
238
+
239
+ if (reason) {
240
+ query = query.where('reason', '=', reason)
241
+ }
242
+
243
+ if (createdBy) {
244
+ query = query.where('createdBy', '=', createdBy)
245
+ }
246
+
247
+ if (cursor) {
248
+ query = query.where(
249
+ 'id',
250
+ direction === 'asc' ? '>' : '<',
251
+ parseInt(cursor, 10),
252
+ )
253
+ }
254
+
255
+ const rules = await query.orderBy('id', direction).limit(limit).execute()
256
+
257
+ return {
258
+ rules,
259
+ cursor: rules.at(-1)?.id?.toString(),
260
+ }
261
+ }
262
+
263
+ async queryEvents({
264
+ cursor,
265
+ limit = 50,
266
+ urls,
267
+ patternType,
268
+ direction = 'desc',
269
+ }: {
270
+ cursor?: string
271
+ limit?: number
272
+ urls?: string[]
273
+ patternType?: SafelinkPatternType
274
+ direction?: 'asc' | 'desc'
275
+ } = {}): Promise<{
276
+ events: Selectable<SafelinkEvent>[]
277
+ cursor?: string
278
+ }> {
279
+ let query = this.db.db.selectFrom('safelink_event').selectAll()
280
+
281
+ if (urls && urls.length > 0) {
282
+ query = query.where('url', 'in', urls)
283
+ }
284
+
285
+ if (patternType) {
286
+ query = query.where('pattern', '=', patternType)
287
+ }
288
+
289
+ if (cursor) {
290
+ query = query.where(
291
+ 'id',
292
+ direction === 'asc' ? '>' : '<',
293
+ parseInt(cursor, 10),
294
+ )
295
+ }
296
+
297
+ const events = await query.orderBy('id', direction).limit(limit).execute()
298
+
299
+ return {
300
+ events,
301
+ cursor: events.at(-1)?.id?.toString(),
302
+ }
303
+ }
304
+ }