-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
689 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Entity<TDTO> { | ||
serialize(): TDTO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) {} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.