Add Event to create api

This commit is contained in:
ssmucny 2023-04-16 16:13:43 -04:00
parent 173a901eee
commit 453d25ff37
2 changed files with 48 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mf
import { extractHashtags } from '@/misc/extract-hashtags.js'; import { extractHashtags } from '@/misc/extract-hashtags.js';
import type { IMentionedRemoteUsers } from '@/models/entities/Note.js'; import type { IMentionedRemoteUsers } from '@/models/entities/Note.js';
import { Note } from '@/models/entities/Note.js'; import { Note } from '@/models/entities/Note.js';
import { Event, IEvent } from '@/models/entities/Event.js';
import type { ChannelFollowingsRepository, ChannelsRepository, InstancesRepository, MutedNotesRepository, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js'; import type { ChannelFollowingsRepository, ChannelsRepository, InstancesRepository, MutedNotesRepository, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
import type { DriveFile } from '@/models/entities/DriveFile.js'; import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { App } from '@/models/entities/App.js'; import type { App } from '@/models/entities/App.js';
@ -126,6 +127,7 @@ type Option = {
renote?: Note | null; renote?: Note | null;
files?: DriveFile[] | null; files?: DriveFile[] | null;
poll?: IPoll | null; poll?: IPoll | null;
event?: IEvent | null;
localOnly?: boolean | null; localOnly?: boolean | null;
reactionAcceptance?: Note['reactionAcceptance']; reactionAcceptance?: Note['reactionAcceptance'];
cw?: string | null; cw?: string | null;
@ -358,6 +360,7 @@ export class NoteCreateService implements OnApplicationShutdown {
name: data.name, name: data.name,
text: data.text, text: data.text,
hasPoll: data.poll != null, hasPoll: data.poll != null,
hasEvent: data.event != null,
cw: data.cw == null ? null : data.cw, cw: data.cw == null ? null : data.cw,
tags: tags.map(tag => normalizeForSearch(tag)), tags: tags.map(tag => normalizeForSearch(tag)),
emojis, emojis,
@ -402,11 +405,12 @@ export class NoteCreateService implements OnApplicationShutdown {
// 投稿を作成 // 投稿を作成
try { try {
if (insert.hasPoll) { if (insert.hasPoll || insert.hasEvent) {
// Start transaction // Start transaction
await this.db.transaction(async transactionalEntityManager => { await this.db.transaction(async transactionalEntityManager => {
await transactionalEntityManager.insert(Note, insert); await transactionalEntityManager.insert(Note, insert);
if (insert.hasPoll) {
const poll = new Poll({ const poll = new Poll({
noteId: insert.id, noteId: insert.id,
choices: data.poll!.choices, choices: data.poll!.choices,
@ -419,6 +423,22 @@ export class NoteCreateService implements OnApplicationShutdown {
}); });
await transactionalEntityManager.insert(Poll, poll); await transactionalEntityManager.insert(Poll, poll);
}
if (insert.hasEvent) {
const event = new Event({
noteId: insert.id,
start: data.event!.start,
end: data.event!.end ?? undefined,
title: data.event!.title,
metadata: data.event!.metadata,
noteVisibility: insert.visibility,
userId: user.id,
userHost: user.host,
});
await transactionalEntityManager.insert(Event, event);
}
}); });
} else { } else {
await this.notesRepository.insert(insert); await this.notesRepository.insert(insert);

View File

@ -144,6 +144,16 @@ export const paramDef = {
}, },
required: ['choices'], required: ['choices'],
}, },
event: {
type: 'object',
nullable: true,
properties: {
title: { type: 'string', minLength: 1, maxLength: 128, nullable: false },
start: { type: 'integer', nullable: false },
end: { type: 'integer', nullable: true },
metadata: { type: 'object' },
},
},
}, },
// (re)note with text, files and poll are optional // (re)note with text, files and poll are optional
anyOf: [ anyOf: [
@ -279,6 +289,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
text: ps.text ?? undefined, text: ps.text ?? undefined,
reply, reply,
renote, renote,
event: ps.event ? {
start: new Date(ps.event.start!),
end: ps.event.end ? new Date(ps.event.end) : null,
title: ps.event.title!,
metadata: ps.event.metadata ?? {},
} : undefined,
cw: ps.cw, cw: ps.cw,
localOnly: ps.localOnly, localOnly: ps.localOnly,
reactionAcceptance: ps.reactionAcceptance, reactionAcceptance: ps.reactionAcceptance,