Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications #200

Merged
merged 8 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
notification methods on tables
  • Loading branch information
dholms committed Sep 29, 2022
commit 441e79288911ed79f431a9f3008bf7f44f6abee3
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"rules": {
"no-var": "error",
"prefer-const": "warn",
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export class Database {

const table = this.findTableForCollection(uri.collection)
await table.set(uri, obj)

const notifs = table.notifsForRecord(uri, obj)
}

async deleteRecord(uri: AdxUri) {
Expand Down
26 changes: 26 additions & 0 deletions packages/server/src/db/notifications/user-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AdxUri } from '@adxp/common'
import { Entity, Column, PrimaryColumn } from 'typeorm'
import * as post from '../records/post'

@Entity({ name: 'user_notifications' })
export class UserNotification {
@PrimaryColumn('varchar')
userDid: string

@PrimaryColumn('vachar')
recordUri: string

@Column('varchar')
reason: string

@Column('varchar')
reasonSubject: string

@Column('varchar')
createdAt: string
}

export const processRecordNotifications = async (uri: AdxUri, obj: unknown) => {
if (post.isValidSchema(obj)) {
} else if(post.)
}
16 changes: 15 additions & 1 deletion packages/server/src/db/records/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Repository,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import { User } from '../user'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand Down Expand Up @@ -92,6 +92,19 @@ const translateDbObj = (dbObj: BadgeIndex): Badge.Record => {
return badge
}

const notifsForRecord = (uri: AdxUri, obj: unknown): Notification[] => {
if (!isValidSchema(obj)) {
throw new Error(`Record does not match schema: ${type}`)
}
const notif = {
userDid: obj.subject,
author: uri.host,
recordUri: uri.toString(),
reason: 'badge',
}
return [notif]
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Badge.Record, BadgeIndex> => {
Expand All @@ -104,6 +117,7 @@ export const makePlugin = (
set: setFn(repository),
delete: deleteFn(repository),
translateDbObj,
notifsForRecord,
}
}

Expand Down
16 changes: 15 additions & 1 deletion packages/server/src/db/records/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Repository,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import { User } from '../user'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand Down Expand Up @@ -76,6 +76,19 @@ const translateDbObj = (dbObj: FollowIndex): Follow.Record => {
}
}

const notifsForRecord = (uri: AdxUri, obj: unknown): Notification[] => {
if (!isValidSchema(obj)) {
throw new Error(`Record does not match schema: ${type}`)
}
const notif = {
userDid: obj.subject,
author: uri.host,
recordUri: uri.toString(),
reason: 'follow',
}
return [notif]
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Follow.Record, FollowIndex> => {
Expand All @@ -88,6 +101,7 @@ export const makePlugin = (
set: setFn(repository),
delete: deleteFn(repository),
translateDbObj,
notifsForRecord,
}
}

Expand Down
18 changes: 17 additions & 1 deletion packages/server/src/db/records/like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Repository,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import { User } from '../user'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand Down Expand Up @@ -76,6 +76,21 @@ const translateDbObj = (dbObj: LikeIndex): Like.Record => {
}
}

const notifsForRecord = (uri: AdxUri, obj: unknown): Notification[] => {
if (!isValidSchema(obj)) {
throw new Error(`Record does not match schema: ${type}`)
}
const subjectUri = new AdxUri(obj.subject)
const notif = {
userDid: subjectUri.host,
author: uri.host,
recordUri: uri.toString(),
reason: 'like',
reasonSubject: subjectUri.toString(),
}
return [notif]
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Like.Record, LikeIndex> => {
Expand All @@ -88,6 +103,7 @@ export const makePlugin = (
set: setFn(repository),
delete: deleteFn(repository),
translateDbObj,
notifsForRecord,
}
}

Expand Down
32 changes: 30 additions & 2 deletions packages/server/src/db/records/post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AdxUri } from '@adxp/uri'
import * as Post from '../../lexicon/types/todo/social/post'
import { DataSource, Entity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import { User } from '../user'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand Down Expand Up @@ -128,10 +128,37 @@ const translateDbObj = (dbObj: PostIndex): Post.Record => {
}
}

const notifsForRecord = (uri: AdxUri, obj: unknown): Notification[] => {
if (!isValidSchema(obj)) {
throw new Error(`Record does not match schema: ${type}`)
}
const notifs: Notification[] = []
for (const entity of obj.entities || []) {
if (entity.type === 'mention') {
notifs.push({
userDid: entity.value,
author: uri.host,
recordUri: uri.toString(),
reason: 'mention',
})
}
}
if (obj.reply?.parent) {
const parentUri = new AdxUri(obj.reply.parent)
notifs.push({
userDid: parentUri.host,
author: uri.host,
recordUri: uri.toString(),
reason: 'reply',
reasonSubject: parentUri.toString(),
})
}
return notifs
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Post.Record, PostIndex> => {
const repository = db.getRepository(PostIndex)
return {
collection: type,
tableName,
Expand All @@ -140,6 +167,7 @@ export const makePlugin = (
set: setFn(db),
delete: deleteFn(db),
translateDbObj,
notifsForRecord,
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/server/src/db/records/profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AdxUri } from '@adxp/uri'
import * as Profile from '../../lexicon/types/todo/social/profile'
import { DataSource, Entity, Column, PrimaryColumn } from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import schemas from '../schemas'
import { collectionToTableName } from '../util'

Expand Down Expand Up @@ -98,10 +98,13 @@ const translateDbObj = (dbObj: ProfileIndex): Profile.Record => {
}
}

const notifsForRecord = (_uri: AdxUri, _obj: unknown): Notification[] => {
return []
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Profile.Record, ProfileIndex> => {
const repository = db.getRepository(ProfileIndex)
return {
collection: type,
tableName,
Expand All @@ -110,6 +113,7 @@ export const makePlugin = (
set: setFn(db),
delete: deleteFn(db),
translateDbObj,
notifsForRecord,
}
}

Expand Down
18 changes: 17 additions & 1 deletion packages/server/src/db/records/repost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Repository,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
import { DbRecordPlugin, Notification } from '../types'
import { User } from '../user'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand Down Expand Up @@ -79,6 +79,21 @@ const translateDbObj = (dbObj: RepostIndex): Repost.Record => {
}
}

const notifsForRecord = (uri: AdxUri, obj: unknown): Notification[] => {
if (!isValidSchema(obj)) {
throw new Error(`Record does not match schema: ${type}`)
}
const subjectUri = new AdxUri(obj.subject)
const notif = {
userDid: subjectUri.host,
author: uri.host,
recordUri: uri.toString(),
reason: 'repost',
reasonSubject: subjectUri.toString(),
}
return [notif]
}

export const makePlugin = (
db: DataSource,
): DbRecordPlugin<Repost.Record, RepostIndex> => {
Expand All @@ -91,6 +106,7 @@ export const makePlugin = (
set: setFn(repository),
delete: deleteFn(repository),
translateDbObj,
notifsForRecord,
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/server/src/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@ export type DbRecordPlugin<T, S> = {
set: (uri: AdxUri, obj: unknown) => Promise<void>
delete: (uri: AdxUri) => Promise<void>
translateDbObj: (dbObj: S) => T
notifsForRecord: (uri: AdxUri, obj: unknown) => Notification[]
}

export type Notification = {
userDid: string
author: string
recordUri: string
reason: string
reasonSubject?: string
}

export type NotificationReason =
| 'like'
| 'repost'
| 'follow'
| 'badge'
| 'mention'
| 'reply'