-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from DashHub-ai/feature/pinning-messages
Add messages pinning
- Loading branch information
Showing
60 changed files
with
1,399 additions
and
37 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,24 @@ | ||
import type { Kysely } from 'kysely'; | ||
|
||
import { addIdColumn, addTimestampColumns } from './utils'; | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
await db.schema | ||
.createTable('pinned_messages') | ||
.$call(addIdColumn) | ||
.$call(addTimestampColumns) | ||
.addColumn('creator_user_id', 'integer', col => col.notNull().references('users.id').onDelete('restrict')) | ||
.addColumn('message_id', 'uuid', col => col.references('messages.id').onDelete('restrict')) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex('pinned_messages_uniq_index') | ||
.on('pinned_messages') | ||
.unique() | ||
.columns(['creator_user_id', 'message_id']) | ||
.execute(); | ||
} | ||
|
||
export async function down(db: Kysely<any>): Promise<void> { | ||
await db.schema.dropTable('pinned_messages').execute(); | ||
} |
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
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
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
71 changes: 71 additions & 0 deletions
71
apps/backend/src/modules/api/controllers/dashboard/pinned-messages.controller.ts
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,71 @@ | ||
import { taskEither as TE } from 'fp-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import { inject, injectable } from 'tsyringe'; | ||
|
||
import { | ||
ofSdkSuccess, | ||
type PinnedMessagesSdk, | ||
SdkPinMessageInputV, | ||
SdkSearchPinnedMessagesInputV, | ||
} from '@llm/sdk'; | ||
import { ConfigService } from '~/modules/config'; | ||
import { PinnedMessagesService } from '~/modules/pinned-messages'; | ||
|
||
import { | ||
mapDbRecordAlreadyExistsToSdkError, | ||
rejectUnsafeSdkErrors, | ||
sdkSchemaValidator, | ||
serializeSdkResponseTE, | ||
} from '../../helpers'; | ||
import { AuthorizedController } from '../shared/authorized.controller'; | ||
|
||
@injectable() | ||
export class PinnedMessagesController extends AuthorizedController { | ||
constructor( | ||
@inject(ConfigService) configService: ConfigService, | ||
@inject(PinnedMessagesService) pinnedMessagesService: PinnedMessagesService, | ||
) { | ||
super(configService); | ||
|
||
this.router | ||
.get( | ||
'/search', | ||
sdkSchemaValidator('query', SdkSearchPinnedMessagesInputV), | ||
async context => pipe( | ||
context.req.valid('query'), | ||
pinnedMessagesService.asUser(context.var.jwt).search, | ||
rejectUnsafeSdkErrors, | ||
serializeSdkResponseTE<ReturnType<PinnedMessagesSdk['search']>>(context), | ||
), | ||
) | ||
.get( | ||
'/all', | ||
async context => pipe( | ||
pinnedMessagesService.asUser(context.var.jwt).findAll(), | ||
rejectUnsafeSdkErrors, | ||
serializeSdkResponseTE<ReturnType<PinnedMessagesSdk['all']>>(context), | ||
), | ||
) | ||
.post( | ||
'/', | ||
sdkSchemaValidator('json', SdkPinMessageInputV), | ||
async context => pipe( | ||
context.req.valid('json'), | ||
pinnedMessagesService.asUser(context.var.jwt).create, | ||
mapDbRecordAlreadyExistsToSdkError, | ||
rejectUnsafeSdkErrors, | ||
serializeSdkResponseTE<ReturnType<PinnedMessagesSdk['create']>>(context), | ||
), | ||
) | ||
.delete( | ||
'/:id', | ||
async context => pipe( | ||
Number(context.req.param('id')), | ||
pinnedMessagesService.asUser(context.var.jwt).delete, | ||
TE.map(ofSdkSuccess), | ||
rejectUnsafeSdkErrors, | ||
serializeSdkResponseTE<ReturnType<PinnedMessagesSdk['delete']>>(context), | ||
), | ||
); | ||
} | ||
} |
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
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
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
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
2 changes: 2 additions & 0 deletions
2
apps/backend/src/modules/pinned-messages/elasticsearch/index.ts
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,2 @@ | ||
export * from './pinned-messages-es-index.repo'; | ||
export * from './pinned-messages-es-search.repo'; |
71 changes: 71 additions & 0 deletions
71
apps/backend/src/modules/pinned-messages/elasticsearch/pinned-messages-es-index.repo.ts
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,71 @@ | ||
import { array as A, taskEither as TE } from 'fp-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import snakecaseKeys from 'snakecase-keys'; | ||
import { inject, injectable } from 'tsyringe'; | ||
|
||
import { tryOrThrowTE } from '@llm/commons'; | ||
import { | ||
createAutocompleteFieldAnalyzeSettings, | ||
createBaseAutocompleteFieldMappings, | ||
createBaseDatedRecordMappings, | ||
createElasticsearchIndexRepo, | ||
createIdNameObjectMapping, | ||
createIdObjectMapping, | ||
ElasticsearchRepo, | ||
type EsDocument, | ||
} from '~/modules/elasticsearch'; | ||
|
||
import type { PinnedMessageTableRowWithRelations } from '../pinned-messages.tables'; | ||
|
||
import { PinnedMessagesRepo } from '../pinned-messages.repo'; | ||
|
||
const PinnedMessagesAbstractEsIndexRepo = createElasticsearchIndexRepo({ | ||
indexName: 'dashboard-pinned-messages', | ||
schema: { | ||
mappings: { | ||
dynamic: false, | ||
properties: { | ||
...createBaseDatedRecordMappings(), | ||
creator: createIdNameObjectMapping(), | ||
message: createIdObjectMapping( | ||
createBaseAutocompleteFieldMappings('content'), | ||
'keyword', | ||
), | ||
}, | ||
}, | ||
settings: { | ||
'index.number_of_replicas': 1, | ||
'analysis': createAutocompleteFieldAnalyzeSettings(), | ||
}, | ||
}, | ||
}); | ||
|
||
export type PinnedMessagesEsDocument = EsDocument<PinnedMessageTableRowWithRelations>; | ||
|
||
@injectable() | ||
export class PinnedMessagesEsIndexRepo extends PinnedMessagesAbstractEsIndexRepo<PinnedMessagesEsDocument> { | ||
constructor( | ||
@inject(ElasticsearchRepo) elasticsearchRepo: ElasticsearchRepo, | ||
@inject(PinnedMessagesRepo) private readonly pinnedMessagesRepo: PinnedMessagesRepo, | ||
) { | ||
super(elasticsearchRepo); | ||
} | ||
|
||
protected async findEntities(ids: number[]): Promise<PinnedMessagesEsDocument[]> { | ||
return pipe( | ||
this.pinnedMessagesRepo.findWithRelationsByIds({ ids }), | ||
TE.map( | ||
A.map(entity => ({ | ||
...snakecaseKeys(entity, { deep: true }), | ||
_id: String(entity.id), | ||
})), | ||
), | ||
tryOrThrowTE, | ||
)(); | ||
} | ||
|
||
protected createAllEntitiesIdsIterator = () => | ||
this.pinnedMessagesRepo.createIdsIterator({ | ||
chunkSize: 100, | ||
}); | ||
} |
Oops, something went wrong.