-
-
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
650 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<DTO> { | ||
serialize(): DTO | ||
} |
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,60 @@ | ||
# ActivityPub Domain | ||
|
||
## Architecture | ||
|
||
### Service | ||
|
||
A service provides functionality for the application layer. Common operations include: | ||
- Retrieving data from the repository layer and constructing entities | ||
|
||
### Entity | ||
|
||
An entity is a representation of a real-world object or concept. The service layer is responsible for | ||
the creation of entities. Entities should define how they are serialized to a DTO. | ||
|
||
### Repository | ||
|
||
A repository provides functionality for interacting with a database. It should not be used directly by the application layer. Instead, it should be used by the service layer. DTOs are used to pass data between the repository and the service layer. | ||
|
||
### DTO | ||
|
||
A DTO is a data transfer object. It is 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` | ||
- ... | ||
|
||
## FAQs | ||
|
||
### 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 keeper 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,40 @@ | ||
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() | ||
|
||
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(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,34 @@ | ||
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() | ||
|
||
return result ?? null | ||
} | ||
|
||
async create(data: ActorDTO): Promise<ActorDTO> { | ||
const result = await this.db(this.tableName).insert(data) | ||
|
||
if (result.length !== 1) { | ||
throw new Error('Failed to create actor') | ||
} | ||
|
||
const object = await this.findById(data.id); | ||
|
||
if (!object) { | ||
throw new Error('Failed to create actor') | ||
} | ||
|
||
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,34 @@ | ||
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(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,34 @@ | ||
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() | ||
|
||
return result ?? null | ||
} | ||
|
||
async create(data: ObjectDTO): Promise<ObjectDTO> { | ||
const result = await this.db(this.tableName).insert(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,18 @@ | ||
import { Knex } from 'knex' | ||
|
||
import { Repository } from '../Repository' | ||
import { type SiteDTO } from '../entity/SiteEntity' | ||
|
||
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() | ||
|
||
return result ?? null | ||
} | ||
} |
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,59 @@ | ||
import { ActivityEntity, type ActivityDTO } from '../entity/ActivityEntity' | ||
import { ActivityRepository } from '../repository/ActivityRepository' | ||
import { ActorService } from '../service/ActorService' | ||
import { ObjectService } from '../service/ObjectService' | ||
|
||
export class ActivityService { | ||
constructor( | ||
private readonly activityRepository: ActivityRepository, | ||
private readonly actorService: ActorService, | ||
private readonly objectService: ObjectService, | ||
) {} | ||
|
||
async findById(id: string): Promise<ActivityEntity | null> { | ||
const activity = await this.activityRepository.findById(id) | ||
|
||
if (activity) { | ||
return await this.#buildActivity(activity) | ||
} | ||
|
||
return null | ||
} | ||
|
||
async findByIds(ids: string[]): Promise<ActivityEntity[]> { | ||
const serializedActivities = await this.activityRepository.findByIds(ids) | ||
const activities: ActivityEntity[] = [] | ||
|
||
for (const activity of serializedActivities) { | ||
const builtActivity = await this.#buildActivity(activity) | ||
|
||
if (builtActivity) { | ||
activities.push(builtActivity) | ||
} | ||
} | ||
|
||
return activities | ||
} | ||
|
||
async create(data: ActivityDTO): Promise<ActivityEntity> { | ||
const serializedActivity = await this.activityRepository.create(data) | ||
const activity = await this.#buildActivity(serializedActivity); | ||
|
||
if (!activity) { | ||
throw new Error('Failed to create activity') | ||
} | ||
|
||
return activity; | ||
} | ||
|
||
async #buildActivity(activity: ActivityDTO) { | ||
const object = await this.objectService.findById(activity.object_id) | ||
const actor = await this.actorService.findById(activity.actor_id) | ||
|
||
if (object && actor) { | ||
return new ActivityEntity(activity.id, activity.type, actor, object) | ||
} | ||
|
||
return null | ||
} | ||
} |
Oops, something went wrong.