広告開始時期の設定 (#9944)

* 広告開始時期の設定

* 過去のものも表示するように
This commit is contained in:
nenohi 2023-02-15 14:29:40 +09:00 committed by GitHub
parent 8caf288ac1
commit 71c42bef9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 8 deletions

View file

@ -777,6 +777,7 @@ popularPosts: "人気の投稿"
shareWithNote: "ノートで共有"
ads: "広告"
expiration: "期限"
startingperiod: "開始期間"
memo: "メモ"
priority: "優先度"
high: "高"

View file

@ -0,0 +1,9 @@
export class ad1676438468213 {
name = 'ad1676438468213';
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "ad" ADD "startAt" TIMESTAMP WITH TIME ZONE NOT NULL`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "role" DROP COLUMN "startAt"`);
}
}

View file

@ -18,6 +18,12 @@ export class Ad {
})
public expiresAt: Date;
@Index()
@Column('timestamp with time zone', {
comment: 'The expired date of the Ad.',
})
public startAt: Date;
@Column('varchar', {
length: 32, nullable: false,
})

View file

@ -20,9 +20,10 @@ export const paramDef = {
priority: { type: 'string' },
ratio: { type: 'integer' },
expiresAt: { type: 'integer' },
startAt: { type: 'integer' },
imageUrl: { type: 'string', minLength: 1 },
},
required: ['url', 'memo', 'place', 'priority', 'ratio', 'expiresAt', 'imageUrl'],
required: ['url', 'memo', 'place', 'priority', 'ratio', 'expiresAt', 'startAt', 'imageUrl'],
} as const;
// eslint-disable-next-line import/no-default-export
@ -39,6 +40,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
id: this.idService.genId(),
createdAt: new Date(),
expiresAt: new Date(ps.expiresAt),
startAt: new Date(ps.startAt),
url: ps.url,
imageUrl: ps.imageUrl,
priority: ps.priority,

View file

@ -32,8 +32,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
) {
super(meta, paramDef, async (ps, me) => {
const query = this.queryService.makePaginationQuery(this.adsRepository.createQueryBuilder('ad'), ps.sinceId, ps.untilId)
.andWhere('ad.expiresAt > :now', { now: new Date() });
const ads = await query.take(ps.limit).getMany();
return ads;

View file

@ -30,8 +30,9 @@ export const paramDef = {
priority: { type: 'string' },
ratio: { type: 'integer' },
expiresAt: { type: 'integer' },
startAt: { type: 'integer' },
},
required: ['id', 'memo', 'url', 'imageUrl', 'place', 'priority', 'ratio', 'expiresAt'],
required: ['id', 'memo', 'url', 'imageUrl', 'place', 'priority', 'ratio', 'expiresAt', 'startAt'],
} as const;
// eslint-disable-next-line import/no-default-export
@ -54,6 +55,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
memo: ps.memo,
imageUrl: ps.imageUrl,
expiresAt: new Date(ps.expiresAt),
startAt: new Date(ps.startAt),
});
});
}

View file

@ -1,4 +1,4 @@
import { IsNull, MoreThan } from 'typeorm';
import { IsNull, LessThanOrEqual, MoreThan } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import type { AdsRepository, EmojisRepository, UsersRepository } from '@/models/index.js';
import { MAX_NOTE_TEXT_LENGTH, DB_MAX_NOTE_TEXT_LENGTH } from '@/const.js';
@ -262,6 +262,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const ads = await this.adsRepository.find({
where: {
expiresAt: MoreThan(new Date()),
startAt: LessThanOrEqual(new Date()),
},
});

View file

@ -29,6 +29,9 @@
<MkInput v-model="ad.ratio" type="number">
<template #label>{{ i18n.ts.ratio }}</template>
</MkInput>
<MkInput v-model="ad.startAt" type="datetime-local">
<template #label>{{ i18n.ts.startingperiod }}</template>
</MkInput>
<MkInput v-model="ad.expiresAt" type="datetime-local">
<template #label>{{ i18n.ts.expiration }}</template>
</MkInput>
@ -66,11 +69,14 @@ const localTimeDiff = localTime.getTimezoneOffset() * 60 * 1000;
os.api('admin/ad/list').then(adsResponse => {
ads = adsResponse.map(r => {
const date = new Date(r.expiresAt);
date.setMilliseconds(date.getMilliseconds() - localTimeDiff);
const exdate = new Date(r.expiresAt);
const stdate = new Date(r.startAt);
exdate.setMilliseconds(exdate.getMilliseconds() - localTimeDiff);
stdate.setMilliseconds(stdate.getMilliseconds() - localTimeDiff);
return {
...r,
expiresAt: date.toISOString().slice(0, 16),
expiresAt: exdate.toISOString().slice(0, 16),
startAt: stdate.toISOString().slice(0, 16),
};
});
});
@ -85,6 +91,7 @@ function add() {
url: '',
imageUrl: null,
expiresAt: null,
startAt: null,
});
}
@ -106,11 +113,13 @@ function save(ad) {
os.apiWithDialog('admin/ad/create', {
...ad,
expiresAt: new Date(ad.expiresAt).getTime(),
startAt: new Date(ad.startAt).getTime(),
});
} else {
os.apiWithDialog('admin/ad/update', {
...ad,
expiresAt: new Date(ad.expiresAt).getTime(),
startAt: new Date(ad.startAt).getTime(),
});
}
}