Skip to content

Commit

Permalink
User invite codes (#757)
Browse files Browse the repository at this point in the history
* getUserInviteCodes lex

* small change

* implement user invite code creation/getting

* transactionally ensure we dont allow duplicate creates

* testing & fixes

* clean up & allow admin creation for a particular user

* fix dev-env

* user -> accnt & add admin disable codes route

* proposed admin inv schemas

* more admin routes for inv codes

* tests for invite admin views

* pr feedback

* refactor & return usedBy + more details on getAccountInviteCodes

* adding invite info into moderation views

* tests passing
  • Loading branch information
dholms committed Apr 5, 2023
1 parent 47cb640 commit 83828c5
Show file tree
Hide file tree
Showing 40 changed files with 1,654 additions and 24 deletions.
14 changes: 12 additions & 2 deletions lexicons/com/atproto/admin/defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@
"email": {"type": "string"},
"relatedRecords": {"type": "array", "items": {"type": "unknown"}},
"indexedAt": {"type": "string", "format": "datetime"},
"moderation": {"type": "ref", "ref": "#moderation"}
"moderation": {"type": "ref", "ref": "#moderation"},
"invitedBy": {"type": "ref", "ref": "com.atproto.server.defs#inviteCode"},
"invites": {
"type": "array",
"items": {"type": "ref", "ref": "com.atproto.server.defs#inviteCode"}
}
}
},
"repoViewDetail": {
Expand All @@ -140,7 +145,12 @@
"email": {"type": "string"},
"relatedRecords": {"type": "array", "items": {"type": "unknown"}},
"indexedAt": {"type": "string", "format": "datetime"},
"moderation": {"type": "ref", "ref": "#moderationDetail"}
"moderation": {"type": "ref", "ref": "#moderationDetail"},
"invitedBy": {"type": "ref", "ref": "com.atproto.server.defs#inviteCode"},
"invites": {
"type": "array",
"items": {"type": "ref", "ref": "com.atproto.server.defs#inviteCode"}
}
}
},
"repoRef": {
Expand Down
26 changes: 26 additions & 0 deletions lexicons/com/atproto/admin/disableInviteCodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"lexicon": 1,
"id": "com.atproto.admin.disableInviteCodes",
"defs": {
"main": {
"type": "procedure",
"description": "Disable some set of codes and/or all codes associated with a set of users",
"input": {
"encoding": "application/json",
"schema": {
"type": "object",
"properties": {
"codes": {
"type": "array",
"items": {"type": "string"}
},
"accounts": {
"type": "array",
"items": {"type": "string"}
}
}
}
}
}
}
}
39 changes: 39 additions & 0 deletions lexicons/com/atproto/admin/getInviteCodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"lexicon": 1,
"id": "com.atproto.admin.getInviteCodes",
"defs": {
"main": {
"type": "query",
"description": "Admin view of invite codes",
"parameters": {
"type": "params",
"properties": {
"sort": {
"type": "string",
"knownValues": [
"recent",
"usage"
],
"default": "recent"
},
"limit": {"type": "integer", "minimum": 1, "maximum": 500, "default": 100},
"cursor": {"type": "string"}
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["codes"],
"properties": {
"cursor": {"type": "string"},
"codes": {
"type": "array",
"items": {"type": "ref", "ref": "com.atproto.server.defs#inviteCode"}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions lexicons/com/atproto/admin/searchRepos.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"type": "params",
"properties": {
"term": {"type": "string"},
"invitedBy": {"type": "string"},
"limit": {"type": "integer", "minimum": 1, "maximum": 100, "default": 50},
"cursor": {"type": "string"}
}
Expand Down
3 changes: 2 additions & 1 deletion lexicons/com/atproto/server/createInviteCode.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"type": "object",
"required": ["useCount"],
"properties": {
"useCount": {"type": "integer"}
"useCount": {"type": "integer"},
"forAccount": {"type": "string", "format": "did"}
}
}
},
Expand Down
30 changes: 30 additions & 0 deletions lexicons/com/atproto/server/defs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"lexicon": 1,
"id": "com.atproto.server.defs",
"defs": {
"inviteCode": {
"type": "object",
"required": ["code", "available", "disabled", "forAccount", "createdBy", "createdAt", "uses"],
"properties": {
"code": {"type": "string"},
"available": {"type": "integer"},
"disabled": {"type": "boolean"},
"forAccount": {"type": "string"},
"createdBy": {"type": "string"},
"createdAt": {"type": "string", "format": "datetime"},
"uses": {
"type": "array",
"items": {"type": "ref", "ref": "#inviteCodeUse"}
}
}
},
"inviteCodeUse": {
"type": "object",
"required": ["usedBy", "usedAt"],
"properties": {
"usedBy": {"type": "string", "format": "did"},
"usedAt": {"type": "string", "format": "datetime"}
}
}
}
}
36 changes: 36 additions & 0 deletions lexicons/com/atproto/server/getAccountInviteCodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"lexicon": 1,
"id": "com.atproto.server.getAccountInviteCodes",
"defs": {
"main": {
"type": "query",
"description": "Get all invite codes for a given account",
"parameters": {
"type": "params",
"properties": {
"includeUsed": { "type": "boolean", "default": true },
"createAvailable": { "type": "boolean", "default": true }
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["codes"],
"properties": {
"codes": {
"type": "array",
"items": {
"type": "ref",
"ref": "com.atproto.server.defs#inviteCode"
}
}
}
}
},
"errors": [
{"name": "DuplicateCreate"}
]
}
}
}
41 changes: 41 additions & 0 deletions packages/api/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { schemas } from './lexicons'
import { CID } from 'multiformats/cid'
import * as ComAtprotoAdminDefs from './types/com/atproto/admin/defs'
import * as ComAtprotoAdminDisableInviteCodes from './types/com/atproto/admin/disableInviteCodes'
import * as ComAtprotoAdminGetInviteCodes from './types/com/atproto/admin/getInviteCodes'
import * as ComAtprotoAdminGetModerationAction from './types/com/atproto/admin/getModerationAction'
import * as ComAtprotoAdminGetModerationActions from './types/com/atproto/admin/getModerationActions'
import * as ComAtprotoAdminGetModerationReport from './types/com/atproto/admin/getModerationReport'
Expand All @@ -34,9 +36,11 @@ import * as ComAtprotoRepoUploadBlob from './types/com/atproto/repo/uploadBlob'
import * as ComAtprotoServerCreateAccount from './types/com/atproto/server/createAccount'
import * as ComAtprotoServerCreateInviteCode from './types/com/atproto/server/createInviteCode'
import * as ComAtprotoServerCreateSession from './types/com/atproto/server/createSession'
import * as ComAtprotoServerDefs from './types/com/atproto/server/defs'
import * as ComAtprotoServerDeleteAccount from './types/com/atproto/server/deleteAccount'
import * as ComAtprotoServerDeleteSession from './types/com/atproto/server/deleteSession'
import * as ComAtprotoServerDescribeServer from './types/com/atproto/server/describeServer'
import * as ComAtprotoServerGetAccountInviteCodes from './types/com/atproto/server/getAccountInviteCodes'
import * as ComAtprotoServerGetSession from './types/com/atproto/server/getSession'
import * as ComAtprotoServerRefreshSession from './types/com/atproto/server/refreshSession'
import * as ComAtprotoServerRequestAccountDelete from './types/com/atproto/server/requestAccountDelete'
Expand Down Expand Up @@ -86,6 +90,8 @@ import * as AppBskyRichtextFacet from './types/app/bsky/richtext/facet'
import * as AppBskyUnspeccedGetPopular from './types/app/bsky/unspecced/getPopular'

export * as ComAtprotoAdminDefs from './types/com/atproto/admin/defs'
export * as ComAtprotoAdminDisableInviteCodes from './types/com/atproto/admin/disableInviteCodes'
export * as ComAtprotoAdminGetInviteCodes from './types/com/atproto/admin/getInviteCodes'
export * as ComAtprotoAdminGetModerationAction from './types/com/atproto/admin/getModerationAction'
export * as ComAtprotoAdminGetModerationActions from './types/com/atproto/admin/getModerationActions'
export * as ComAtprotoAdminGetModerationReport from './types/com/atproto/admin/getModerationReport'
Expand All @@ -112,9 +118,11 @@ export * as ComAtprotoRepoUploadBlob from './types/com/atproto/repo/uploadBlob'
export * as ComAtprotoServerCreateAccount from './types/com/atproto/server/createAccount'
export * as ComAtprotoServerCreateInviteCode from './types/com/atproto/server/createInviteCode'
export * as ComAtprotoServerCreateSession from './types/com/atproto/server/createSession'
export * as ComAtprotoServerDefs from './types/com/atproto/server/defs'
export * as ComAtprotoServerDeleteAccount from './types/com/atproto/server/deleteAccount'
export * as ComAtprotoServerDeleteSession from './types/com/atproto/server/deleteSession'
export * as ComAtprotoServerDescribeServer from './types/com/atproto/server/describeServer'
export * as ComAtprotoServerGetAccountInviteCodes from './types/com/atproto/server/getAccountInviteCodes'
export * as ComAtprotoServerGetSession from './types/com/atproto/server/getSession'
export * as ComAtprotoServerRefreshSession from './types/com/atproto/server/refreshSession'
export * as ComAtprotoServerRequestAccountDelete from './types/com/atproto/server/requestAccountDelete'
Expand Down Expand Up @@ -240,6 +248,28 @@ export class AdminNS {
this._service = service
}

disableInviteCodes(
data?: ComAtprotoAdminDisableInviteCodes.InputSchema,
opts?: ComAtprotoAdminDisableInviteCodes.CallOptions,
): Promise<ComAtprotoAdminDisableInviteCodes.Response> {
return this._service.xrpc
.call('com.atproto.admin.disableInviteCodes', opts?.qp, data, opts)
.catch((e) => {
throw ComAtprotoAdminDisableInviteCodes.toKnownErr(e)
})
}

getInviteCodes(
params?: ComAtprotoAdminGetInviteCodes.QueryParams,
opts?: ComAtprotoAdminGetInviteCodes.CallOptions,
): Promise<ComAtprotoAdminGetInviteCodes.Response> {
return this._service.xrpc
.call('com.atproto.admin.getInviteCodes', params, undefined, opts)
.catch((e) => {
throw ComAtprotoAdminGetInviteCodes.toKnownErr(e)
})
}

getModerationAction(
params?: ComAtprotoAdminGetModerationAction.QueryParams,
opts?: ComAtprotoAdminGetModerationAction.CallOptions,
Expand Down Expand Up @@ -569,6 +599,17 @@ export class ServerNS {
})
}

getAccountInviteCodes(
params?: ComAtprotoServerGetAccountInviteCodes.QueryParams,
opts?: ComAtprotoServerGetAccountInviteCodes.CallOptions,
): Promise<ComAtprotoServerGetAccountInviteCodes.Response> {
return this._service.xrpc
.call('com.atproto.server.getAccountInviteCodes', params, undefined, opts)
.catch((e) => {
throw ComAtprotoServerGetAccountInviteCodes.toKnownErr(e)
})
}

getSession(
params?: ComAtprotoServerGetSession.QueryParams,
opts?: ComAtprotoServerGetSession.CallOptions,
Expand Down
Loading

0 comments on commit 83828c5

Please sign in to comment.