diff --git a/apps/api/src/modules/dakao/dakao.publisher.spec.ts b/apps/api/src/modules/dakao/dakao.publisher.spec.ts new file mode 100644 index 00000000..abc0e676 --- /dev/null +++ b/apps/api/src/modules/dakao/dakao.publisher.spec.ts @@ -0,0 +1,73 @@ +import { SecretService } from '../global/secret/secret.service'; +import { NatsService } from '../global/nats/nats.service'; +import { DakaoCommandEnvelope, DakaoSendMessagePayload } from './dakao.types'; +import { DakaoPublisher } from './dakao.publisher'; + +function makePublisher() { + const natsService = { + publish: jest.fn().mockResolvedValue(undefined), + subscribe: jest.fn(), + } as unknown as NatsService; + const secretService = {} as SecretService; + const publisher = new DakaoPublisher(natsService, secretService, { + instanceId: 'test', + }); + + return { publisher, natsService }; +} + +function getPublishedPayload(natsService: NatsService): DakaoSendMessagePayload { + const publish = natsService.publish as jest.Mock; + const envelope = publish.mock.calls[0][1] as DakaoCommandEnvelope< + DakaoSendMessagePayload + >; + return envelope.payload; +} + +describe('DakaoPublisher', () => { + it('keeps valid mention segments intact', async () => { + const { publisher, natsService } = makePublisher(); + + await publisher.sendMessage({ + chatroomId: '18474390111480678', + segments: [ + { + kind: 'mention', + userId: '6215844374579825921', + displayText: '날으는꼴뚜기/95/여/D', + }, + { kind: 'text', text: ' 신청 완료' }, + ], + }); + + expect(getPublishedPayload(natsService).segments).toEqual([ + { + kind: 'mention', + userId: '6215844374579825921', + displayText: '날으는꼴뚜기/95/여/D', + }, + { kind: 'text', text: ' 신청 완료' }, + ]); + }); + + it('downgrades invalid mention segments to text before publishing', async () => { + const { publisher, natsService } = makePublisher(); + + await publisher.sendMessage({ + chatroomId: '18474390111480678', + segments: [ + { + kind: 'mention', + userId: '-123', + displayText: '소쿠리', + }, + { kind: 'text', text: ' 신청 완료' }, + ], + }); + + expect(getPublishedPayload(natsService).segments).toEqual([ + { kind: 'text', text: '@소쿠리' }, + { kind: 'text', text: ' 신청 완료' }, + ]); + }); +}); diff --git a/apps/api/src/modules/dakao/dakao.publisher.ts b/apps/api/src/modules/dakao/dakao.publisher.ts index 051fa714..21d49c99 100644 --- a/apps/api/src/modules/dakao/dakao.publisher.ts +++ b/apps/api/src/modules/dakao/dakao.publisher.ts @@ -69,7 +69,7 @@ export class DakaoPublisher implements OnModuleInit, OnModuleDestroy { }): Promise { const payload: DakaoSendMessagePayload = { chatroomId: params.chatroomId, - segments: params.segments, + segments: this.normalizeSegments(params.segments), }; const envelope: DakaoCommandEnvelope = { schema: 'dakao.command.v1', @@ -83,6 +83,63 @@ export class DakaoPublisher implements OnModuleInit, OnModuleDestroy { await this.natsService.publish(this.sendMessageSubject, envelope); } + private normalizeSegments(segments: DakaoSegment[]): DakaoSegment[] { + const normalized: DakaoSegment[] = []; + + for (const segment of segments) { + if (segment.kind === 'text') { + if (segment.text.length > 0) normalized.push(segment); + continue; + } + + if (segment.kind === 'mention') { + const userId = segment.userId.trim(); + const displayText = segment.displayText.trim(); + + if (this.isPositiveIntegerString(userId) && displayText.length > 0) { + normalized.push({ kind: 'mention', userId, displayText }); + continue; + } + + const fallbackText = this.renderMentionFallback(displayText, userId); + this.logger.warn( + `Dakao mention downgraded to text because userId/displayText is invalid: userId="${userId || '-'}" displayText="${displayText || '-'}"`, + ); + normalized.push({ kind: 'text', text: fallbackText }); + continue; + } + + const id = segment.id.trim(); + const altText = segment.altText.trim(); + if ( + id.length > 0 && + altText.length > 0 && + !/[()@]/.test(altText) + ) { + normalized.push({ kind: 'emoticon', id, altText }); + continue; + } + + this.logger.warn( + `Dakao emoticon downgraded to text because id/altText is invalid: id="${id || '-'}" altText="${altText || '-'}"`, + ); + normalized.push({ kind: 'text', text: `(${altText || '이모티콘'})` }); + } + + return normalized.length > 0 + ? normalized + : [{ kind: 'text', text: '명령 처리 결과를 보낼 수 없습니다.' }]; + } + + private isPositiveIntegerString(value: string): boolean { + return /^[1-9]\d*$/.test(value); + } + + private renderMentionFallback(displayText: string, userId: string): string { + const label = displayText || userId || '사용자'; + return label.startsWith('@') ? label : `@${label}`; + } + private handleReply(reply: DakaoResultEnvelope): void { if (reply.status === 'succeeded') { this.logger.log( diff --git a/apps/api/src/modules/global/database/schema.ts b/apps/api/src/modules/global/database/schema.ts index e82aa392..2c3cdc56 100644 --- a/apps/api/src/modules/global/database/schema.ts +++ b/apps/api/src/modules/global/database/schema.ts @@ -524,21 +524,33 @@ UserSchema.index( { communityId: 1, identityId: 1 }, { unique: true, - partialFilterExpression: { identityId: { $exists: true } }, + partialFilterExpression: { identityId: { $type: 'objectId' } }, }, ); -UserSchema.index({ identityId: 1 }, { sparse: true }); +UserSchema.index( + { identityId: 1 }, + { partialFilterExpression: { identityId: { $type: 'objectId' } } }, +); UserSchema.index( { communityId: 1, kakaoTalkId: 1 }, - { unique: true, sparse: true }, + { + unique: true, + partialFilterExpression: { kakaoTalkId: { $type: 'string' } }, + }, ); UserSchema.index( { communityId: 1, discordId: 1 }, - { unique: true, sparse: true }, + { + unique: true, + partialFilterExpression: { discordId: { $type: 'string' } }, + }, ); UserSchema.index( { communityId: 1, 'riotAccounts.puuid': 1 }, - { unique: true, sparse: true }, + { + unique: true, + partialFilterExpression: { 'riotAccounts.puuid': { $type: 'string' } }, + }, ); export const RiotMatchSchema = new Schema( diff --git a/apps/web/src/pages/Login.tsx b/apps/web/src/pages/Login.tsx index 7f7e639c..7bbca0e1 100644 --- a/apps/web/src/pages/Login.tsx +++ b/apps/web/src/pages/Login.tsx @@ -19,7 +19,7 @@ function LoginButton() { }) } > - Microsoft로 로그인 + Entra ID 로 로그인 ); } @@ -30,7 +30,7 @@ export default function LoginPage() {

로그인

- Microsoft Entra ID 계정으로 로그인하면 LOLYMPUS 멤버십을 불러옵니다. + Entra ID 계정으로 LOLYMPUS 정보를 불러옵니다.

{isMsalConfigured && msalInstance ? (