2021-01-11 12:38:34 +01:00
|
|
|
import $ from 'cafy';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { publishMainStream } from '@/services/stream';
|
|
|
|
import define from '../../../define';
|
|
|
|
import { RegistryItems } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2021-01-11 12:38:34 +01:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
requireCredential: true as const,
|
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
key: {
|
2021-12-09 15:58:30 +01:00
|
|
|
validator: $.str.min(1),
|
2021-01-11 12:38:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
value: {
|
2021-12-09 15:58:30 +01:00
|
|
|
validator: $.nullable.any,
|
2021-01-11 12:38:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
scope: {
|
|
|
|
validator: $.optional.arr($.str.match(/^[a-zA-Z0-9_]+$/)),
|
|
|
|
default: [],
|
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2021-01-11 12:38:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
const query = RegistryItems.createQueryBuilder('item')
|
|
|
|
.where('item.domain IS NULL')
|
|
|
|
.andWhere('item.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('item.key = :key', { key: ps.key })
|
|
|
|
.andWhere('item.scope = :scope', { scope: ps.scope });
|
|
|
|
|
|
|
|
const existingItem = await query.getOne();
|
|
|
|
|
|
|
|
if (existingItem) {
|
|
|
|
await RegistryItems.update(existingItem.id, {
|
|
|
|
updatedAt: new Date(),
|
2021-12-09 15:58:30 +01:00
|
|
|
value: ps.value,
|
2021-01-11 12:38:34 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await RegistryItems.insert({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
userId: user.id,
|
|
|
|
domain: null,
|
|
|
|
scope: ps.scope,
|
|
|
|
key: ps.key,
|
2021-12-09 15:58:30 +01:00
|
|
|
value: ps.value,
|
2021-01-11 12:38:34 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: サードパーティアプリが傍受出来てしまうのでどうにかする
|
|
|
|
publishMainStream(user.id, 'registryUpdated', {
|
|
|
|
scope: ps.scope,
|
|
|
|
key: ps.key,
|
2021-12-09 15:58:30 +01:00
|
|
|
value: ps.value,
|
2021-01-11 12:38:34 +01:00
|
|
|
});
|
|
|
|
});
|