Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 23 additions & 5 deletions base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: omnibox-local
services:
backend:
ports:
- "${OBB_PORT:-8000}:${OBB_PORT:-8000}"
- '${OBB_PORT:-8000}:${OBB_PORT:-8000}'
env_file:
- .env
environment:
Expand All @@ -17,7 +17,14 @@ services:
minio:
condition: service_healthy
healthcheck:
test: [ "CMD", "wget", "-q", "-O-", "http://127.0.0.1:${OBB_PORT:-8000}/api/v1/health" ]
test:
[
'CMD',
'wget',
'-q',
'-O-',
'http://127.0.0.1:${OBB_PORT:-8000}/api/v1/health',
]
interval: 5s
timeout: 3s
retries: 5
Expand All @@ -32,7 +39,18 @@ services:
- POSTGRES_PASSWORD=${OBB_DB_PASSWORD:-omnibox}
- POSTGRES_PORT=${OBB_DB_PORT:-5432}
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "${OBB_DB_DATABASE:-omnibox}", "-U", "${OBB_DB_USERNAME:-omnibox}", "-p", "${OBB_DB_PORT:-5432}" ]
test:
[
'CMD',
'pg_isready',
'-q',
'-d',
'${OBB_DB_DATABASE:-omnibox}',
'-U',
'${OBB_DB_USERNAME:-omnibox}',
'-p',
'${OBB_DB_PORT:-5432}',
]
interval: 5s
timeout: 3s
retries: 5
Expand All @@ -45,7 +63,7 @@ services:
MINIO_ROOT_USER: username
MINIO_ROOT_PASSWORD: password
healthcheck:
test: [ "CMD", "curl", "-I", "http://127.0.0.1:9000/minio/health/live" ]
test: ['CMD', 'curl', '-I', 'http://127.0.0.1:9000/minio/health/live']
interval: 5s
timeout: 3s
retries: 5
Expand Down Expand Up @@ -79,7 +97,7 @@ services:

init:
image: alpine/curl:8.10.0
command:
command:
- 'http://backend:${OBB_PORT:-8000}/internal/api/v1/sign-up'
- '-H'
- 'Content-Type: application/json'
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/passport-jwt": "^3.0.13",
"@types/passport-local": "^1.0.38",
"@types/supertest": "^6.0.3",
"@types/ws": "^8.18.1",
"eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
Expand Down Expand Up @@ -107,7 +108,8 @@
"rxjs": "^7.8.2",
"socket.io": "^4.8.1",
"typeorm": "^0.3.27",
"typeorm-naming-strategies": "^4.1.0"
"typeorm-naming-strategies": "^4.1.0",
"ws": "^8.18.3"
},
"packageManager": "pnpm@10.17.1"
}
33 changes: 31 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/auth/wechat/wechat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ export class WechatController extends SocialController {

@Public()
@Get('auth-url')
getAuthUrl() {
return this.wechatService.authUrl();
getAuthUrl(
@Query('isH5') isH5?: boolean,
@Query('source') source?: 'h5' | 'web',
@Query('h5_redirect') h5Redirect?: string,
) {
// 兼容旧的isH5参数
const finalSource = source || (isH5 ? 'h5' : 'web');
return this.wechatService.authUrl(finalSource, h5Redirect);
}

@Public()
Expand Down
24 changes: 23 additions & 1 deletion src/auth/wechat/wechat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,22 @@ export class WechatService {
};
}

async authUrl(): Promise<string> {
async authUrl(
source: 'h5' | 'web' = 'web',
h5Redirect?: string,
): Promise<string> {
const state = await this.socialService.generateState('weixin');

// 在state中保存source和h5_redirect信息
const stateInfo = await this.socialService.getState(state);
if (stateInfo) {
stateInfo['source'] = source;
if (h5Redirect) {
stateInfo['h5_redirect'] = h5Redirect;
}
await this.socialService.updateState(state, stateInfo);
}

return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appId}&redirect_uri=${encodeURIComponent(this.redirectUri)}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
}

Expand Down Expand Up @@ -199,6 +213,8 @@ export class WechatService {
sub: wechatUser.id,
username: wechatUser.username,
}),
source: stateInfo['source'] || 'web',
h5_redirect: stateInfo['h5_redirect'],
};
stateInfo.userInfo = returnValue;
await this.socialService.updateState(state, stateInfo);
Expand All @@ -216,6 +232,8 @@ export class WechatService {
sub: existingUser.id,
username: existingUser.username,
}),
source: stateInfo['source'] || 'web',
h5_redirect: stateInfo['h5_redirect'],
};
stateInfo.userInfo = returnValue;
await this.socialService.updateState(state, stateInfo);
Expand All @@ -229,6 +247,8 @@ export class WechatService {
sub: wechatUser.id,
username: wechatUser.username,
}),
source: stateInfo['source'] || 'web',
h5_redirect: stateInfo['h5_redirect'],
};
stateInfo.userInfo = returnValue;
await this.socialService.updateState(state, stateInfo);
Expand Down Expand Up @@ -264,6 +284,8 @@ export class WechatService {
sub: wechatUser.id,
username: wechatUser.username,
}),
source: stateInfo['source'] || 'web',
h5_redirect: stateInfo['h5_redirect'],
};
stateInfo.userInfo = returnValue;
await this.socialService.updateState(state, stateInfo);
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ async function bootstrap() {
configureApp(app);

const configService = app.get(ConfigService);
await app.listen(parseInt(configService.get('OBB_PORT', '8000')));
const port = parseInt(configService.get('OBB_PORT', '8000'));

await app.listen(port);
}

bootstrap().catch(console.error);
3 changes: 3 additions & 0 deletions src/namespace-resources/namespace-resources.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ export class NamespaceResourcesController {
@UserId() userId: string,
@Param('namespaceId') namespaceId: string,
@Query('limit') limit?: string,
@Query('offset') offset?: string,
): Promise<ResourceMetaDto[]> {
const take = Number.isFinite(Number(limit)) ? Number(limit) : 10;
const skip = Number.isFinite(Number(offset)) ? Number(offset) : 0;
return await this.namespaceResourcesService.recent(
namespaceId,
userId,
take,
skip,
);
}

Expand Down
14 changes: 13 additions & 1 deletion src/namespace-resources/namespace-resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,25 @@ export class NamespaceResourcesService {
namespaceId: string,
userId: string,
limit: number = 10,
offset: number = 0,
): Promise<ResourceMetaDto[]> {
const allVisible = await this.getUserVisibleResources(userId, namespaceId);
const sorted = allVisible
.filter((r) => r.parentId !== null)
.filter((r) => r.resourceType !== ResourceType.FOLDER)
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
const take = Math.max(1, Math.min(100, limit));
return sorted.slice(0, take);
const skip = Math.max(0, offset);
const resources = sorted.slice(skip, skip + take);
const firstAttachments =
await this.resourceAttachmentsService.getFirstAttachments(
namespaceId,
resources.map((r) => r.id),
);
for (const resource of resources) {
resource.firstAttachment = firstAttachments.get(resource.id);
}
return resources;
}

// Alias for clarity and reuse across modules
Expand Down
19 changes: 18 additions & 1 deletion src/resource-attachments/resource-attachments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, HttpStatus } from '@nestjs/common';
import { AppException } from 'omniboxd/common/exceptions/app.exception';
import { I18nService } from 'nestjs-i18n';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, EntityManager } from 'typeorm';
import { Repository, EntityManager, In } from 'typeorm';
import { ResourceAttachment } from 'omniboxd/attachments/entities/resource-attachment.entity';

@Injectable()
Expand Down Expand Up @@ -130,4 +130,21 @@ export class ResourceAttachmentsService {
},
});
}

async getFirstAttachments(
namespaceId: string,
resourceIds: string[],
): Promise<Map<string, string>> {
const attachments = await this.resourceAttachmentRepository.find({
where: { namespaceId, resourceId: In(resourceIds) },
order: { id: 'ASC' },
});
const firstAttachments = new Map<string, string>();
for (const attachment of attachments) {
if (!firstAttachments.has(attachment.resourceId)) {
firstAttachments.set(attachment.resourceId, attachment.attachmentId);
}
}
return firstAttachments;
}
}
11 changes: 11 additions & 0 deletions src/resources/dto/resource-meta.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ export class ResourceMetaDto {
@Expose({ name: 'attrs' })
attrs: Record<string, any>;

@Expose({ name: 'content' })
content: string;
fileId: string | null;

@Expose({ name: 'first_attachment' })
firstAttachment?: string;

static fromEntity(resource: Resource) {
const dto = new ResourceMetaDto();
dto.id = resource.id;
Expand All @@ -40,6 +45,12 @@ export class ResourceMetaDto {
dto.globalPermission = resource.globalPermission;
dto.createdAt = resource.createdAt;
dto.updatedAt = resource.updatedAt;
// Remove markdown images ![alt](url) and HTML images <img...>
const contentWithoutImages = resource.content
.replace(/!\[.*?\]\(.*?\)/g, '')
.replace(/<img[^>]*>/gi, '')
.trim();
dto.content = contentWithoutImages.slice(0, 100);
dto.attrs = { ...resource.attrs };
delete dto.attrs.transcript;
delete dto.attrs.video_info;
Expand Down
3 changes: 3 additions & 0 deletions src/resources/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class ResourcesService {
'fileId',
'createdAt',
'updatedAt',
'content',
],
where: { namespaceId, id: resourceId },
});
Expand Down Expand Up @@ -145,6 +146,7 @@ export class ResourcesService {
'createdAt',
'updatedAt',
'attrs',
'content',
],
where: {
namespaceId,
Expand Down Expand Up @@ -191,6 +193,7 @@ export class ResourcesService {
'createdAt',
'updatedAt',
'attrs',
'content',
],
where: {
namespaceId,
Expand Down
Loading