Skip to content

Commit

Permalink
✨ Add Search against normalized email column
Browse files Browse the repository at this point in the history
  • Loading branch information
foysalit committed Apr 22, 2024
1 parent 01f15c3 commit ec0bebd
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 85 deletions.
17 changes: 14 additions & 3 deletions lexicons/com/atproto/admin/searchAccounts.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,37 @@
"parameters": {
"type": "params",
"properties": {
"email": { "type": "string" }
"email": { "type": "string" },
"cursor": { "type": "string" }
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["cursor", "accounts"],
"required": ["accounts"],
"properties": {
"cursor": { "type": "string" },
"accounts": {
"type": "array",
"items": {
"type": "ref",
"ref": "com.atproto.admin.defs#accountView"
"ref": "#accountSearchResult"
}
}
}
}
}
},
"accountSearchResult": {
"type": "object",
"required": ["did", "email"],
"properties": {
"did": { "type": "string" },
"email": { "type": "string" },
"normalizedEmail": { "type": "string" },
"handle": { "type": "string" }
}
}
}
}
25 changes: 23 additions & 2 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,16 @@ export const schemaDict = {
email: {
type: 'string',
},
cursor: {
type: 'string',
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['cursor', 'accounts'],
required: ['accounts'],
properties: {
cursor: {
type: 'string',
Expand All @@ -398,13 +401,31 @@ export const schemaDict = {
type: 'array',
items: {
type: 'ref',
ref: 'lex:com.atproto.admin.defs#accountView',
ref: 'lex:com.atproto.admin.searchAccounts#accountSearchResult',
},
},
},
},
},
},
accountSearchResult: {
type: 'object',
required: ['did', 'email'],
properties: {
did: {
type: 'string',
},
email: {
type: 'string',
},
normalizedEmail: {
type: 'string',
},
handle: {
type: 'string',
},
},
},
},
},
ComAtprotoAdminSendEmail: {
Expand Down
29 changes: 26 additions & 3 deletions packages/api/src/client/types/com/atproto/admin/searchAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { ValidationResult, BlobRef } from '@atproto/lexicon'
import { isObj, hasProp } from '../../../../util'
import { lexicons } from '../../../../lexicons'
import { CID } from 'multiformats/cid'
import * as ComAtprotoAdminDefs from './defs'

export interface QueryParams {
email?: string
cursor?: string
}

export type InputSchema = undefined

export interface OutputSchema {
cursor: string
accounts: ComAtprotoAdminDefs.AccountView[]
cursor?: string
accounts: AccountSearchResult[]
[k: string]: unknown
}

Expand All @@ -35,3 +35,26 @@ export function toKnownErr(e: any) {
}
return e
}

export interface AccountSearchResult {
did: string
email: string
normalizedEmail?: string
handle?: string
[k: string]: unknown
}

export function isAccountSearchResult(v: unknown): v is AccountSearchResult {
return (
isObj(v) &&
hasProp(v, '$type') &&
v.$type === 'com.atproto.admin.searchAccounts#accountSearchResult'
)
}

export function validateAccountSearchResult(v: unknown): ValidationResult {
return lexicons.validate(
'com.atproto.admin.searchAccounts#accountSearchResult',
v,
)
}
25 changes: 23 additions & 2 deletions packages/bsky/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,16 @@ export const schemaDict = {
email: {
type: 'string',
},
cursor: {
type: 'string',
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['cursor', 'accounts'],
required: ['accounts'],
properties: {
cursor: {
type: 'string',
Expand All @@ -398,13 +401,31 @@ export const schemaDict = {
type: 'array',
items: {
type: 'ref',
ref: 'lex:com.atproto.admin.defs#accountView',
ref: 'lex:com.atproto.admin.searchAccounts#accountSearchResult',
},
},
},
},
},
},
accountSearchResult: {
type: 'object',
required: ['did', 'email'],
properties: {
did: {
type: 'string',
},
email: {
type: 'string',
},
normalizedEmail: {
type: 'string',
},
handle: {
type: 'string',
},
},
},
},
},
ComAtprotoAdminSendEmail: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { lexicons } from '../../../../lexicons'
import { isObj, hasProp } from '../../../../util'
import { CID } from 'multiformats/cid'
import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
import * as ComAtprotoAdminDefs from './defs'

export interface QueryParams {
email?: string
cursor?: string
}

export type InputSchema = undefined

export interface OutputSchema {
cursor: string
accounts: ComAtprotoAdminDefs.AccountView[]
cursor?: string
accounts: AccountSearchResult[]
[k: string]: unknown
}

Expand Down Expand Up @@ -45,3 +45,26 @@ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
export type Handler<HA extends HandlerAuth = never> = (
ctx: HandlerReqCtx<HA>,
) => Promise<HandlerOutput> | HandlerOutput

export interface AccountSearchResult {
did: string
email: string
normalizedEmail?: string
handle?: string
[k: string]: unknown
}

export function isAccountSearchResult(v: unknown): v is AccountSearchResult {
return (
isObj(v) &&
hasProp(v, '$type') &&
v.$type === 'com.atproto.admin.searchAccounts#accountSearchResult'
)
}

export function validateAccountSearchResult(v: unknown): ValidationResult {
return lexicons.validate(
'com.atproto.admin.searchAccounts#accountSearchResult',
v,
)
}
25 changes: 23 additions & 2 deletions packages/ozone/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,16 @@ export const schemaDict = {
email: {
type: 'string',
},
cursor: {
type: 'string',
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['cursor', 'accounts'],
required: ['accounts'],
properties: {
cursor: {
type: 'string',
Expand All @@ -398,13 +401,31 @@ export const schemaDict = {
type: 'array',
items: {
type: 'ref',
ref: 'lex:com.atproto.admin.defs#accountView',
ref: 'lex:com.atproto.admin.searchAccounts#accountSearchResult',
},
},
},
},
},
},
accountSearchResult: {
type: 'object',
required: ['did', 'email'],
properties: {
did: {
type: 'string',
},
email: {
type: 'string',
},
normalizedEmail: {
type: 'string',
},
handle: {
type: 'string',
},
},
},
},
},
ComAtprotoAdminSendEmail: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { lexicons } from '../../../../lexicons'
import { isObj, hasProp } from '../../../../util'
import { CID } from 'multiformats/cid'
import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'
import * as ComAtprotoAdminDefs from './defs'

export interface QueryParams {
email?: string
cursor?: string
}

export type InputSchema = undefined

export interface OutputSchema {
cursor: string
accounts: ComAtprotoAdminDefs.AccountView[]
cursor?: string
accounts: AccountSearchResult[]
[k: string]: unknown
}

Expand Down Expand Up @@ -45,3 +45,26 @@ export type HandlerReqCtx<HA extends HandlerAuth = never> = {
export type Handler<HA extends HandlerAuth = never> = (
ctx: HandlerReqCtx<HA>,
) => Promise<HandlerOutput> | HandlerOutput

export interface AccountSearchResult {
did: string
email: string
normalizedEmail?: string
handle?: string
[k: string]: unknown
}

export function isAccountSearchResult(v: unknown): v is AccountSearchResult {
return (
isObj(v) &&
hasProp(v, '$type') &&
v.$type === 'com.atproto.admin.searchAccounts#accountSearchResult'
)
}

export function validateAccountSearchResult(v: unknown): ValidationResult {
return lexicons.validate(
'com.atproto.admin.searchAccounts#accountSearchResult',
v,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Kysely } from 'kysely'

export async function up(db: Kysely<unknown>): Promise<void> {
await db.schema
.alterTable('account')
.addColumn('normalizedEmail', 'varchar')
.execute()
}

export async function down(db: Kysely<unknown>): Promise<void> {
await db.schema.alterTable('account').dropColumn('normalizedEmail').execute()
}
2 changes: 2 additions & 0 deletions packages/pds/src/account-manager/db/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as mig001 from './001-init'
import * as mig002 from './002-account-deactivation'
import * as mig003 from './003-normalized-email'

export default {
'001': mig001,
'002': mig002,
'003': mig003,
}
1 change: 1 addition & 0 deletions packages/pds/src/account-manager/db/schema/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Generated, Selectable } from 'kysely'
export interface Account {
did: string
email: string
normalizedEmail: string | null
passwordScrypt: string
emailConfirmedAt: string | null
invitesDisabled: Generated<0 | 1>
Expand Down

0 comments on commit ec0bebd

Please sign in to comment.