Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Add communities.create() API
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Feb 8, 2021
1 parent be16fe7 commit e35f69e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
47 changes: 44 additions & 3 deletions api/communities.js
@@ -1,9 +1,20 @@
import { publicUserDbs, privateUserDbs, publicServerDb, onDatabaseChange, catchupIndexes } from '../db/index.js'
import { publicUserDbs, createUser, catchupIndexes } from '../db/index.js'
import { isHyperUrl, constructEntryUrl } from '../lib/strings.js'
import { createValidator } from '../lib/schemas.js'
import { fetchUserId, fetchUserInfo } from '../lib/network.js'
import { listCommunityMembers, listCommunityMemberships } from '../db/getters.js'

const createParam = createValidator({
type: 'object',
required: ['username', 'displayName'],
additionalProperties: false,
properties: {
username: {type: 'string', pattern: "^([a-zA-Z][a-zA-Z0-9]{2,63})$"},
displayName: {type: 'string', minLength: 1, maxLength: 64},
description: {type: 'string', maxLength: 256}
}
})

const listParam = createValidator({
type: 'object',
additionalProperties: false,
Expand All @@ -18,8 +29,38 @@ const listParam = createValidator({
})

export function setup (wsServer) {
wsServer.register('communities.create', async ([userId], client) => {
// TODO
wsServer.register('communities.create', async ([info], client) => {
if (!client?.auth) throw new Error('Must be logged in')

const publicCitizenDb = publicUserDbs.get(client.auth.userId)
if (!publicCitizenDb) throw new Error('User database not found')

info = info || {}
createParam.assert(info)

// create the community user
const communityUser = await createUser({
type: 'community',
username: info.username,
profile: {
displayName: info.displayName,
description: info.description
}
})
const communityInfo = {
userId: communityUser.userId,
dbUrl: communityUser.publicUserDb.url
}

// add membership records for the creator of the community
const joinDate = (new Date()).toISOString()
const membershipValue = {community: communityInfo, joinDate}
const memberValue = {user: {userId: client.auth.userId, dbUrl: publicCitizenDb.url}, joinDate}
await publicCitizenDb.memberships.put(communityInfo.userId, membershipValue)
await communityUser.publicUserDb.members.put(client.auth.userId, memberValue)
/* dont await */ catchupIndexes(communityUser.publicUserDb)

return communityInfo
})

wsServer.register('communities.listMembers', async ([communityUserId, opts], client) => {
Expand Down
4 changes: 4 additions & 0 deletions db/index.js
Expand Up @@ -69,6 +69,10 @@ export async function createUser ({type, username, email, profile}) {
if (type === 'citizen') schemas.get('ctzn.network/account').assertValid(account)
schemas.get('ctzn.network/user').assertValid(user)

if (publicUserDbs.has(userId)) {
throw new Error('Username already in use.')
}

let publicUserDb
let privateUserDb
if (type === 'citizen') {
Expand Down

0 comments on commit e35f69e

Please sign in to comment.