Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Sep 1, 2024
1 parent c8dc4ca commit c552794
Show file tree
Hide file tree
Showing 22 changed files with 689 additions and 47 deletions.
3 changes: 3 additions & 0 deletions src/_/Entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Entity<TDTO> {
serialize(): TDTO
}
77 changes: 77 additions & 0 deletions src/_/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ActivityPub Domain

## Architecture

### Service

Provides functionality for the application layer. Common operations include:
- Retrieving data from the repository layer and constructing entities

### Entity

Represents a real-world object or concept. The service layer is responsible for
the creation of entities. Entities currently define how they are serialized to a
DTO in lieu of a data-mapper layer.

### Repository

Provides functionality for interacting with a database. Typically used by the
service layer. DTOs are used to pass data between the repository and the service
layer.

### DTO

Used to pass data between the repository and the service layer.

## Database

<img src="./database.png" alt="DB Schema" />

- `sites` - Information about each site that utilises the service
- `actors` - ActivityPub actors associated with objects and activities
- `objects` - ActivityPub objects associated with activities
- `activities` - ActivityPub activities
- Pivot table that references `actors` and `objects`
- `inbox` - Received activities for an actor
- Pivot table that references `actors` and `activities`
- ...

## Conventions

- If an entity references another entity, when it is created, the referenced entity
should also be created. This reference is normally indicated via a `<entity>_id` field
in the entity's DTO.

## Notes, Thoughts, Questions

### How does data get scoped?

When a request is received, the hostname is extracted from the request and used to
determine the site that the request is scoped to. The only data that requires scoping
is actor data within the context of a site (i.e the same actor can be present on
multiple sites). Site specific actor data includes:
- Inbox

### Why is actvities a pivot table?

ActivityPub activities largely follow the same structure:

```json
{
"id": "...",
"type": "...",
"actor": {},
"object": {},
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/data-integrity/v1"
]
}
```

Rather than storing repated actor and object data, we can store a single actor and
object entity and reference them in the activity. This reduces the amount of data
that needs to be stored and allows for easier querying and indexing. This also makes
it easier to keep actor data up-to-date, as we only need to update the data in one
place as well as allowing multiple activities to reference the same actor or object
without having to duplicate the data.
8 changes: 8 additions & 0 deletions src/_/Repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Knex } from 'knex'

export abstract class Repository {
constructor(
protected readonly db: Knex,
protected readonly tableName: string,
) {}
}
Binary file added src/_/db.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions src/_/entity/ActivityEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { type Entity } from '../Entity'
import { ActorEntity } from './ActorEntity'
import { ObjectEntity } from './ObjectEntity'

export enum ActivityType {
LIKE = 'Like'
}

export type ActivityDTO = {
id: string
type: ActivityType
actor_id: string
object_id: string
}

export class ActivityEntity implements Entity<ActivityDTO> {
constructor(
readonly id: string,
readonly type: ActivityType,
readonly actor: ActorEntity,
readonly object: ObjectEntity,
) {}

serialize() {
return {
id: this.id,
type: this.type,
actor_id: this.actor.id,
object_id: this.object.id,
}
}
}
20 changes: 20 additions & 0 deletions src/_/entity/ActorEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Entity } from '../Entity'

export type ActorDTO = {
id: string
data: JSON
}

export class ActorEntity implements Entity<ActorDTO> {
constructor(
readonly id: string,
readonly data: JSON,
) {}

serialize() {
return {
id: this.id,
data: this.data,
}
}
}
20 changes: 20 additions & 0 deletions src/_/entity/ObjectEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Entity } from '../Entity'

export type ObjectDTO = {
id: string
data: JSON
};

export class ObjectEntity implements Entity<ObjectDTO> {
constructor(
readonly id: string,
readonly data: JSON,
) {}

serialize() {
return {
id: this.id,
data: this.data,
}
}
}
20 changes: 20 additions & 0 deletions src/_/entity/SiteEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Entity } from '../Entity'

export type SiteDTO = {
id: number
hostname: string
}

export class SiteEntity implements Entity<SiteDTO> {
constructor(
readonly id: number,
readonly hostname: string,
) {}

serialize() {
return {
id: this.id,
hostname: this.hostname,
}
}
}
38 changes: 38 additions & 0 deletions src/_/repository/ActivityRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Knex } from 'knex'

import { Repository } from '../Repository'
import { type ActivityDTO } from '../entity/ActivityEntity'

export class ActivityRepository extends Repository {
constructor(db: Knex) {
super(db, 'activities')
}

async findById(id: string): Promise<ActivityDTO | null> {
const result = await this.db(this.tableName).where('id', id).first<ActivityDTO | undefined>()

return result ?? null
}

async findByIds(ids: string[]): Promise<ActivityDTO[]> {
const results = await this.db(this.tableName).whereIn('id', ids)

return results
}

async create(data: ActivityDTO): Promise<ActivityDTO> {
const result = await this.db(this.tableName).insert<number[]>(data)

if (result.length !== 1) {
throw new Error('Failed to create activity')
}

const activity = await this.findById(data.id);

if (!activity) {
throw new Error('Failed to create activity')
}

return activity
}
}
32 changes: 32 additions & 0 deletions src/_/repository/ActorRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Knex } from 'knex'

import { Repository } from '../Repository'
import { type ActorDTO } from '../entity/ActorEntity'

export class ActorRepository extends Repository {
constructor(db: Knex) {
super(db, 'actors')
}

async findById(id: string): Promise<ActorDTO | null> {
const result = await this.db(this.tableName).where('id', id).first<ActorDTO | undefined>()

return result ?? null
}

async create(data: ActorDTO): Promise<ActorDTO> {
const result = await this.db(this.tableName).insert<number[]>(data)

if (result.length !== 1) {
throw new Error('Failed to create actor')
}

const actor = await this.findById(data.id);

if (!actor) {
throw new Error('Failed to create actor')
}

return actor
}
}
32 changes: 32 additions & 0 deletions src/_/repository/InboxRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Knex } from 'knex'

import { Repository } from '../Repository'

type InboxItemDTO = {
site_id: number
actor_id: string
activity_id: string
}

export class InboxRepository extends Repository {
constructor(db: Knex) {
super(db, 'inbox')
}

async findByActorId(actorId: string, siteId: number): Promise<InboxItemDTO[]> {
const results = await this.db(this.tableName)
.where('actor_id', actorId)
.where('site_id', siteId)
.select<InboxItemDTO[]>('*')

return results
}

async create(data: InboxItemDTO): Promise<void> {
const result = await this.db(this.tableName).insert<number[]>(data)

if (result.length !== 1) {
throw new Error('Failed to create inbox item')
}
}
}
32 changes: 32 additions & 0 deletions src/_/repository/ObjectRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Knex } from 'knex'

import { Repository } from '../Repository'
import { type ObjectDTO } from '../entity/ObjectEntity'

export class ObjectRepository extends Repository {
constructor(db: Knex) {
super(db, 'objects')
}

async findById(id: string): Promise<ObjectDTO | null> {
const result = await this.db(this.tableName).where('id', id).first<ObjectDTO | undefined>()

return result ?? null
}

async create(data: ObjectDTO): Promise<ObjectDTO> {
const result = await this.db(this.tableName).insert<number[]>(data)

if (result.length !== 1) {
throw new Error('Failed to create object')
}

const object = await this.findById(data.id);

if (!object) {
throw new Error('Failed to create object')
}

return object
}
}
40 changes: 40 additions & 0 deletions src/_/repository/SiteRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Knex } from 'knex'

import { Repository } from '../Repository'
import { type SiteDTO } from '../entity/SiteEntity'

type CreateSiteDTO = Omit<SiteDTO, 'id'>

export class SiteRepository extends Repository {
constructor(db: Knex) {
super(db, 'sites')
}

async findById(id: number): Promise<SiteDTO | null> {
const result = await this.db(this.tableName).where('id', id).first<SiteDTO | undefined>()

return result ?? null
}

async findByHostname(hostname: string): Promise<SiteDTO | null> {
const result = await this.db(this.tableName).where('hostname', hostname).first<SiteDTO | undefined>()

return result ?? null
}

async create(data: CreateSiteDTO): Promise<SiteDTO> {
const result = await this.db(this.tableName).insert<number[]>(data)

if (result.length !== 1) {
throw new Error('Failed to create site')
}

const object = await this.findById(result[0]);

if (!object) {
throw new Error('Failed to create site')
}

return object
}
}
Loading

0 comments on commit c552794

Please sign in to comment.